blob: 2f7018821531e2443d69906a097859d93df00ced [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))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
James Smarte59058c2008-08-24 21:49:00 -0400408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
James Smarted957682007-06-17 19:56:37 -0500418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
James Smarte59058c2008-08-24 21:49:00 -0400425/**
James Smart3621a712009-04-06 18:47:14 -0400426 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
James Smarted957682007-06-17 19:56:37 -0500435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
James Smarte59058c2008-08-24 21:49:00 -0400442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
James Smart2e0fef82007-06-17 19:56:36 -0500451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458 return iocbq;
459}
460
James Smarte59058c2008-08-24 21:49:00 -0400461/**
James Smartda0436e2009-05-22 14:51:39 -0400462 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
463 * @phba: Pointer to HBA context object.
464 * @xritag: XRI value.
465 *
466 * This function clears the sglq pointer from the array of acive
467 * sglq's. The xritag that is passed in is used to index into the
468 * array. Before the xritag can be used it needs to be adjusted
469 * by subtracting the xribase.
470 *
471 * Returns sglq ponter = success, NULL = Failure.
472 **/
473static struct lpfc_sglq *
474__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
475{
476 uint16_t adj_xri;
477 struct lpfc_sglq *sglq;
478 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
479 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
480 return NULL;
481 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
482 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
483 return sglq;
484}
485
486/**
487 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
488 * @phba: Pointer to HBA context object.
489 * @xritag: XRI value.
490 *
491 * This function returns the sglq pointer from the array of acive
492 * sglq's. The xritag that is passed in is used to index into the
493 * array. Before the xritag can be used it needs to be adjusted
494 * by subtracting the xribase.
495 *
496 * Returns sglq ponter = success, NULL = Failure.
497 **/
James Smart0f65ff62010-02-26 14:14:23 -0500498struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400499__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
500{
501 uint16_t adj_xri;
502 struct lpfc_sglq *sglq;
503 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
504 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
505 return NULL;
506 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
507 return sglq;
508}
509
510/**
511 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
512 * @phba: Pointer to HBA context object.
513 *
514 * This function is called with hbalock held. This function
515 * Gets a new driver sglq object from the sglq list. If the
516 * list is not empty then it is successful, it returns pointer to the newly
517 * allocated sglq object else it returns NULL.
518 **/
519static struct lpfc_sglq *
520__lpfc_sli_get_sglq(struct lpfc_hba *phba)
521{
522 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
523 struct lpfc_sglq *sglq = NULL;
524 uint16_t adj_xri;
525 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400526 if (!sglq)
527 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400528 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
529 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
James Smart0f65ff62010-02-26 14:14:23 -0500530 sglq->state = SGL_ALLOCATED;
James Smartda0436e2009-05-22 14:51:39 -0400531 return sglq;
532}
533
534/**
James Smart3621a712009-04-06 18:47:14 -0400535 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400536 * @phba: Pointer to HBA context object.
537 *
538 * This function is called with no lock held. This function
539 * allocates a new driver iocb object from the iocb pool. If the
540 * allocation is successful, it returns pointer to the newly
541 * allocated iocb object else it returns NULL.
542 **/
James Smart2e0fef82007-06-17 19:56:36 -0500543struct lpfc_iocbq *
544lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500545{
James Smart2e0fef82007-06-17 19:56:36 -0500546 struct lpfc_iocbq * iocbq = NULL;
547 unsigned long iflags;
548
549 spin_lock_irqsave(&phba->hbalock, iflags);
550 iocbq = __lpfc_sli_get_iocbq(phba);
551 spin_unlock_irqrestore(&phba->hbalock, iflags);
552 return iocbq;
553}
554
James Smarte59058c2008-08-24 21:49:00 -0400555/**
James Smart4f774512009-05-22 14:52:35 -0400556 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
557 * @phba: Pointer to HBA context object.
558 * @iocbq: Pointer to driver iocb object.
559 *
560 * This function is called with hbalock held to release driver
561 * iocb object to the iocb pool. The iotag in the iocb object
562 * does not change for each use of the iocb object. This function
563 * clears all other fields of the iocb object when it is freed.
564 * The sqlq structure that holds the xritag and phys and virtual
565 * mappings for the scatter gather list is retrieved from the
566 * active array of sglq. The get of the sglq pointer also clears
567 * the entry in the array. If the status of the IO indiactes that
568 * this IO was aborted then the sglq entry it put on the
569 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
570 * IO has good status or fails for any other reason then the sglq
571 * entry is added to the free list (lpfc_sgl_list).
572 **/
573static void
574__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
575{
576 struct lpfc_sglq *sglq;
577 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
578 unsigned long iflag;
579
580 if (iocbq->sli4_xritag == NO_XRI)
581 sglq = NULL;
582 else
583 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
584 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500585 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
586 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400587 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588 iflag);
589 list_add(&sglq->list,
590 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591 spin_unlock_irqrestore(
592 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500593 } else {
594 sglq->state = SGL_FREED;
James Smart4f774512009-05-22 14:52:35 -0400595 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart0f65ff62010-02-26 14:14:23 -0500596 }
James Smart4f774512009-05-22 14:52:35 -0400597 }
598
599
600 /*
601 * Clean all volatile data fields, preserve iotag and node struct.
602 */
603 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
604 iocbq->sli4_xritag = NO_XRI;
605 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
606}
607
608/**
James Smart3772a992009-05-22 14:50:54 -0400609 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
610 * @phba: Pointer to HBA context object.
611 * @iocbq: Pointer to driver iocb object.
612 *
613 * This function is called with hbalock held to release driver
614 * iocb object to the iocb pool. The iotag in the iocb object
615 * does not change for each use of the iocb object. This function
616 * clears all other fields of the iocb object when it is freed.
617 **/
618static void
619__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
620{
621 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
622
623 /*
624 * Clean all volatile data fields, preserve iotag and node struct.
625 */
626 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
627 iocbq->sli4_xritag = NO_XRI;
628 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
629}
630
631/**
James Smart3621a712009-04-06 18:47:14 -0400632 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400633 * @phba: Pointer to HBA context object.
634 * @iocbq: Pointer to driver iocb object.
635 *
636 * This function is called with hbalock held to release driver
637 * iocb object to the iocb pool. The iotag in the iocb object
638 * does not change for each use of the iocb object. This function
639 * clears all other fields of the iocb object when it is freed.
640 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100641static void
James Smart2e0fef82007-06-17 19:56:36 -0500642__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
643{
James Smart3772a992009-05-22 14:50:54 -0400644 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500645}
646
James Smarte59058c2008-08-24 21:49:00 -0400647/**
James Smart3621a712009-04-06 18:47:14 -0400648 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400649 * @phba: Pointer to HBA context object.
650 * @iocbq: Pointer to driver iocb object.
651 *
652 * This function is called with no lock held to release the iocb to
653 * iocb pool.
654 **/
James Smart2e0fef82007-06-17 19:56:36 -0500655void
656lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
657{
658 unsigned long iflags;
659
660 /*
661 * Clean all volatile data fields, preserve iotag and node struct.
662 */
663 spin_lock_irqsave(&phba->hbalock, iflags);
664 __lpfc_sli_release_iocbq(phba, iocbq);
665 spin_unlock_irqrestore(&phba->hbalock, iflags);
666}
667
James Smarte59058c2008-08-24 21:49:00 -0400668/**
James Smarta257bf92009-04-06 18:48:10 -0400669 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
670 * @phba: Pointer to HBA context object.
671 * @iocblist: List of IOCBs.
672 * @ulpstatus: ULP status in IOCB command field.
673 * @ulpWord4: ULP word-4 in IOCB command field.
674 *
675 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
676 * on the list by invoking the complete callback function associated with the
677 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
678 * fields.
679 **/
680void
681lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
682 uint32_t ulpstatus, uint32_t ulpWord4)
683{
684 struct lpfc_iocbq *piocb;
685
686 while (!list_empty(iocblist)) {
687 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
688
689 if (!piocb->iocb_cmpl)
690 lpfc_sli_release_iocbq(phba, piocb);
691 else {
692 piocb->iocb.ulpStatus = ulpstatus;
693 piocb->iocb.un.ulpWord[4] = ulpWord4;
694 (piocb->iocb_cmpl) (phba, piocb, piocb);
695 }
696 }
697 return;
698}
699
700/**
James Smart3621a712009-04-06 18:47:14 -0400701 * lpfc_sli_iocb_cmd_type - Get the iocb type
702 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400703 *
704 * This function is called by ring event handler function to get the iocb type.
705 * This function translates the iocb command to an iocb command type used to
706 * decide the final disposition of each completed IOCB.
707 * The function returns
708 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
709 * LPFC_SOL_IOCB if it is a solicited iocb completion
710 * LPFC_ABORT_IOCB if it is an abort iocb
711 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
712 *
713 * The caller is not required to hold any lock.
714 **/
dea31012005-04-17 16:05:31 -0500715static lpfc_iocb_type
716lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
717{
718 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
719
720 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
721 return 0;
722
723 switch (iocb_cmnd) {
724 case CMD_XMIT_SEQUENCE_CR:
725 case CMD_XMIT_SEQUENCE_CX:
726 case CMD_XMIT_BCAST_CN:
727 case CMD_XMIT_BCAST_CX:
728 case CMD_ELS_REQUEST_CR:
729 case CMD_ELS_REQUEST_CX:
730 case CMD_CREATE_XRI_CR:
731 case CMD_CREATE_XRI_CX:
732 case CMD_GET_RPI_CN:
733 case CMD_XMIT_ELS_RSP_CX:
734 case CMD_GET_RPI_CR:
735 case CMD_FCP_IWRITE_CR:
736 case CMD_FCP_IWRITE_CX:
737 case CMD_FCP_IREAD_CR:
738 case CMD_FCP_IREAD_CX:
739 case CMD_FCP_ICMND_CR:
740 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500741 case CMD_FCP_TSEND_CX:
742 case CMD_FCP_TRSP_CX:
743 case CMD_FCP_TRECEIVE_CX:
744 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500745 case CMD_ADAPTER_MSG:
746 case CMD_ADAPTER_DUMP:
747 case CMD_XMIT_SEQUENCE64_CR:
748 case CMD_XMIT_SEQUENCE64_CX:
749 case CMD_XMIT_BCAST64_CN:
750 case CMD_XMIT_BCAST64_CX:
751 case CMD_ELS_REQUEST64_CR:
752 case CMD_ELS_REQUEST64_CX:
753 case CMD_FCP_IWRITE64_CR:
754 case CMD_FCP_IWRITE64_CX:
755 case CMD_FCP_IREAD64_CR:
756 case CMD_FCP_IREAD64_CX:
757 case CMD_FCP_ICMND64_CR:
758 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500759 case CMD_FCP_TSEND64_CX:
760 case CMD_FCP_TRSP64_CX:
761 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500762 case CMD_GEN_REQUEST64_CR:
763 case CMD_GEN_REQUEST64_CX:
764 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400765 case DSSCMD_IWRITE64_CR:
766 case DSSCMD_IWRITE64_CX:
767 case DSSCMD_IREAD64_CR:
768 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500769 type = LPFC_SOL_IOCB;
770 break;
771 case CMD_ABORT_XRI_CN:
772 case CMD_ABORT_XRI_CX:
773 case CMD_CLOSE_XRI_CN:
774 case CMD_CLOSE_XRI_CX:
775 case CMD_XRI_ABORTED_CX:
776 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400777 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500778 type = LPFC_ABORT_IOCB;
779 break;
780 case CMD_RCV_SEQUENCE_CX:
781 case CMD_RCV_ELS_REQ_CX:
782 case CMD_RCV_SEQUENCE64_CX:
783 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400784 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500785 case CMD_IOCB_RCV_SEQ64_CX:
786 case CMD_IOCB_RCV_ELS64_CX:
787 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500788 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500789 type = LPFC_UNSOL_IOCB;
790 break;
James Smart3163f722008-02-08 18:50:25 -0500791 case CMD_IOCB_XMIT_MSEQ64_CR:
792 case CMD_IOCB_XMIT_MSEQ64_CX:
793 case CMD_IOCB_RCV_SEQ_LIST64_CX:
794 case CMD_IOCB_RCV_ELS_LIST64_CX:
795 case CMD_IOCB_CLOSE_EXTENDED_CN:
796 case CMD_IOCB_ABORT_EXTENDED_CN:
797 case CMD_IOCB_RET_HBQE64_CN:
798 case CMD_IOCB_FCP_IBIDIR64_CR:
799 case CMD_IOCB_FCP_IBIDIR64_CX:
800 case CMD_IOCB_FCP_ITASKMGT64_CX:
801 case CMD_IOCB_LOGENTRY_CN:
802 case CMD_IOCB_LOGENTRY_ASYNC_CN:
803 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700804 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500805 type = LPFC_UNKNOWN_IOCB;
806 break;
dea31012005-04-17 16:05:31 -0500807 default:
808 type = LPFC_UNKNOWN_IOCB;
809 break;
810 }
811
812 return type;
813}
814
James Smarte59058c2008-08-24 21:49:00 -0400815/**
James Smart3621a712009-04-06 18:47:14 -0400816 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400817 * @phba: Pointer to HBA context object.
818 *
819 * This function is called from SLI initialization code
820 * to configure every ring of the HBA's SLI interface. The
821 * caller is not required to hold any lock. This function issues
822 * a config_ring mailbox command for each ring.
823 * This function returns zero if successful else returns a negative
824 * error code.
825 **/
dea31012005-04-17 16:05:31 -0500826static int
James Smarted957682007-06-17 19:56:37 -0500827lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500828{
829 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500830 LPFC_MBOXQ_t *pmb;
831 MAILBOX_t *pmbox;
832 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500833
James Smarted957682007-06-17 19:56:37 -0500834 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835 if (!pmb)
836 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400837 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500838 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500839 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500840 lpfc_config_ring(phba, i, pmb);
841 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
842 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400844 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500845 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
846 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400847 rc, pmbox->mbxCommand,
848 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500849 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500850 ret = -ENXIO;
851 break;
dea31012005-04-17 16:05:31 -0500852 }
853 }
James Smarted957682007-06-17 19:56:37 -0500854 mempool_free(pmb, phba->mbox_mem_pool);
855 return ret;
dea31012005-04-17 16:05:31 -0500856}
857
James Smarte59058c2008-08-24 21:49:00 -0400858/**
James Smart3621a712009-04-06 18:47:14 -0400859 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400860 * @phba: Pointer to HBA context object.
861 * @pring: Pointer to driver SLI ring object.
862 * @piocb: Pointer to the driver iocb object.
863 *
864 * This function is called with hbalock held. The function adds the
865 * new iocb to txcmplq of the given ring. This function always returns
866 * 0. If this function is called for ELS ring, this function checks if
867 * there is a vport associated with the ELS command. This function also
868 * starts els_tmofunc timer if this is an ELS command.
869 **/
dea31012005-04-17 16:05:31 -0500870static int
James Smart2e0fef82007-06-17 19:56:36 -0500871lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
872 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500873{
dea31012005-04-17 16:05:31 -0500874 list_add_tail(&piocb->list, &pring->txcmplq);
875 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500876 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
877 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
878 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
879 if (!piocb->vport)
880 BUG();
881 else
882 mod_timer(&piocb->vport->els_tmofunc,
883 jiffies + HZ * (phba->fc_ratov << 1));
884 }
885
dea31012005-04-17 16:05:31 -0500886
James Smart2e0fef82007-06-17 19:56:36 -0500887 return 0;
dea31012005-04-17 16:05:31 -0500888}
889
James Smarte59058c2008-08-24 21:49:00 -0400890/**
James Smart3621a712009-04-06 18:47:14 -0400891 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400892 * @phba: Pointer to HBA context object.
893 * @pring: Pointer to driver SLI ring object.
894 *
895 * This function is called with hbalock held to get next
896 * iocb in txq of the given ring. If there is any iocb in
897 * the txq, the function returns first iocb in the list after
898 * removing the iocb from the list, else it returns NULL.
899 **/
dea31012005-04-17 16:05:31 -0500900static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500901lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500902{
dea31012005-04-17 16:05:31 -0500903 struct lpfc_iocbq *cmd_iocb;
904
James Smart858c9f62007-06-17 19:56:39 -0500905 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
906 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500907 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500908 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500909}
910
James Smarte59058c2008-08-24 21:49:00 -0400911/**
James Smart3621a712009-04-06 18:47:14 -0400912 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400913 * @phba: Pointer to HBA context object.
914 * @pring: Pointer to driver SLI ring object.
915 *
916 * This function is called with hbalock held and the caller must post the
917 * iocb without releasing the lock. If the caller releases the lock,
918 * iocb slot returned by the function is not guaranteed to be available.
919 * The function returns pointer to the next available iocb slot if there
920 * is available slot in the ring, else it returns NULL.
921 * If the get index of the ring is ahead of the put index, the function
922 * will post an error attention event to the worker thread to take the
923 * HBA to offline state.
924 **/
dea31012005-04-17 16:05:31 -0500925static IOCB_t *
926lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
927{
James Smart34b02dc2008-08-24 21:49:55 -0400928 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500929 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500930 if ((pring->next_cmdidx == pring->cmdidx) &&
931 (++pring->next_cmdidx >= max_cmd_idx))
932 pring->next_cmdidx = 0;
933
934 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
935
936 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
937
938 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400940 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200941 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400942 pring->ringno,
dea31012005-04-17 16:05:31 -0500943 pring->local_getidx, max_cmd_idx);
944
James Smart2e0fef82007-06-17 19:56:36 -0500945 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500946 /*
947 * All error attention handlers are posted to
948 * worker thread
949 */
950 phba->work_ha |= HA_ERATT;
951 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500952
James Smart5e9d9b82008-06-14 22:52:53 -0400953 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500954
955 return NULL;
956 }
957
958 if (pring->local_getidx == pring->next_cmdidx)
959 return NULL;
960 }
961
James Smarted957682007-06-17 19:56:37 -0500962 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500963}
964
James Smarte59058c2008-08-24 21:49:00 -0400965/**
James Smart3621a712009-04-06 18:47:14 -0400966 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400967 * @phba: Pointer to HBA context object.
968 * @iocbq: Pointer to driver iocb object.
969 *
970 * This function gets an iotag for the iocb. If there is no unused iotag and
971 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
972 * array and assigns a new iotag.
973 * The function returns the allocated iotag if successful, else returns zero.
974 * Zero is not a valid iotag.
975 * The caller is not required to hold any lock.
976 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500977uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500978lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500979{
James Smart2e0fef82007-06-17 19:56:36 -0500980 struct lpfc_iocbq **new_arr;
981 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500982 size_t new_len;
983 struct lpfc_sli *psli = &phba->sli;
984 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500985
James Smart2e0fef82007-06-17 19:56:36 -0500986 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500987 iotag = psli->last_iotag;
988 if(++iotag < psli->iocbq_lookup_len) {
989 psli->last_iotag = iotag;
990 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500991 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500992 iocbq->iotag = iotag;
993 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500994 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500995 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
996 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500997 spin_unlock_irq(&phba->hbalock);
998 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500999 GFP_KERNEL);
1000 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001001 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001002 old_arr = psli->iocbq_lookup;
1003 if (new_len <= psli->iocbq_lookup_len) {
1004 /* highly unprobable case */
1005 kfree(new_arr);
1006 iotag = psli->last_iotag;
1007 if(++iotag < psli->iocbq_lookup_len) {
1008 psli->last_iotag = iotag;
1009 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001010 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001011 iocbq->iotag = iotag;
1012 return iotag;
1013 }
James Smart2e0fef82007-06-17 19:56:36 -05001014 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001015 return 0;
1016 }
1017 if (psli->iocbq_lookup)
1018 memcpy(new_arr, old_arr,
1019 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001020 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001021 psli->iocbq_lookup = new_arr;
1022 psli->iocbq_lookup_len = new_len;
1023 psli->last_iotag = iotag;
1024 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001025 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001026 iocbq->iotag = iotag;
1027 kfree(old_arr);
1028 return iotag;
1029 }
James Smart8f6d98d2006-08-01 07:34:00 -04001030 } else
James Smart2e0fef82007-06-17 19:56:36 -05001031 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001032
James Bottomley604a3e32005-10-29 10:28:33 -05001033 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001034 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1035 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001036
James Bottomley604a3e32005-10-29 10:28:33 -05001037 return 0;
dea31012005-04-17 16:05:31 -05001038}
1039
James Smarte59058c2008-08-24 21:49:00 -04001040/**
James Smart3621a712009-04-06 18:47:14 -04001041 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001042 * @phba: Pointer to HBA context object.
1043 * @pring: Pointer to driver SLI ring object.
1044 * @iocb: Pointer to iocb slot in the ring.
1045 * @nextiocb: Pointer to driver iocb object which need to be
1046 * posted to firmware.
1047 *
1048 * This function is called with hbalock held to post a new iocb to
1049 * the firmware. This function copies the new iocb to ring iocb slot and
1050 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1051 * a completion call back for this iocb else the function will free the
1052 * iocb object.
1053 **/
dea31012005-04-17 16:05:31 -05001054static void
1055lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1056 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1057{
1058 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001059 * Set up an iotag
dea31012005-04-17 16:05:31 -05001060 */
James Bottomley604a3e32005-10-29 10:28:33 -05001061 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001062
James Smarte2a0a9d2008-12-04 22:40:02 -05001063
James Smarta58cbd52007-08-02 11:09:43 -04001064 if (pring->ringno == LPFC_ELS_RING) {
1065 lpfc_debugfs_slow_ring_trc(phba,
1066 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1067 *(((uint32_t *) &nextiocb->iocb) + 4),
1068 *(((uint32_t *) &nextiocb->iocb) + 6),
1069 *(((uint32_t *) &nextiocb->iocb) + 7));
1070 }
1071
dea31012005-04-17 16:05:31 -05001072 /*
1073 * Issue iocb command to adapter
1074 */
James Smart92d7f7b2007-06-17 19:56:38 -05001075 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001076 wmb();
1077 pring->stats.iocb_cmd++;
1078
1079 /*
1080 * If there is no completion routine to call, we can release the
1081 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1082 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1083 */
1084 if (nextiocb->iocb_cmpl)
1085 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001086 else
James Smart2e0fef82007-06-17 19:56:36 -05001087 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001088
1089 /*
1090 * Let the HBA know what IOCB slot will be the next one the
1091 * driver will put a command into.
1092 */
1093 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001094 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001095}
1096
James Smarte59058c2008-08-24 21:49:00 -04001097/**
James Smart3621a712009-04-06 18:47:14 -04001098 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001099 * @phba: Pointer to HBA context object.
1100 * @pring: Pointer to driver SLI ring object.
1101 *
1102 * The caller is not required to hold any lock for calling this function.
1103 * This function updates the chip attention bits for the ring to inform firmware
1104 * that there are pending work to be done for this ring and requests an
1105 * interrupt when there is space available in the ring. This function is
1106 * called when the driver is unable to post more iocbs to the ring due
1107 * to unavailability of space in the ring.
1108 **/
dea31012005-04-17 16:05:31 -05001109static void
James Smart2e0fef82007-06-17 19:56:36 -05001110lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001111{
1112 int ringno = pring->ringno;
1113
1114 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1115
1116 wmb();
1117
1118 /*
1119 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1120 * The HBA will tell us when an IOCB entry is available.
1121 */
1122 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1123 readl(phba->CAregaddr); /* flush */
1124
1125 pring->stats.iocb_cmd_full++;
1126}
1127
James Smarte59058c2008-08-24 21:49:00 -04001128/**
James Smart3621a712009-04-06 18:47:14 -04001129 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001130 * @phba: Pointer to HBA context object.
1131 * @pring: Pointer to driver SLI ring object.
1132 *
1133 * This function updates the chip attention register bit for the
1134 * given ring to inform HBA that there is more work to be done
1135 * in this ring. The caller is not required to hold any lock.
1136 **/
dea31012005-04-17 16:05:31 -05001137static void
James Smart2e0fef82007-06-17 19:56:36 -05001138lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001139{
1140 int ringno = pring->ringno;
1141
1142 /*
1143 * Tell the HBA that there is work to do in this ring.
1144 */
James Smart34b02dc2008-08-24 21:49:55 -04001145 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1146 wmb();
1147 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1148 readl(phba->CAregaddr); /* flush */
1149 }
dea31012005-04-17 16:05:31 -05001150}
1151
James Smarte59058c2008-08-24 21:49:00 -04001152/**
James Smart3621a712009-04-06 18:47:14 -04001153 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001154 * @phba: Pointer to HBA context object.
1155 * @pring: Pointer to driver SLI ring object.
1156 *
1157 * This function is called with hbalock held to post pending iocbs
1158 * in the txq to the firmware. This function is called when driver
1159 * detects space available in the ring.
1160 **/
dea31012005-04-17 16:05:31 -05001161static void
James Smart2e0fef82007-06-17 19:56:36 -05001162lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001163{
1164 IOCB_t *iocb;
1165 struct lpfc_iocbq *nextiocb;
1166
1167 /*
1168 * Check to see if:
1169 * (a) there is anything on the txq to send
1170 * (b) link is up
1171 * (c) link attention events can be processed (fcp ring only)
1172 * (d) IOCB processing is not blocked by the outstanding mbox command.
1173 */
1174 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001175 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001176 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001177 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001178
1179 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1180 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1181 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1182
1183 if (iocb)
1184 lpfc_sli_update_ring(phba, pring);
1185 else
1186 lpfc_sli_update_full_ring(phba, pring);
1187 }
1188
1189 return;
1190}
1191
James Smarte59058c2008-08-24 21:49:00 -04001192/**
James Smart3621a712009-04-06 18:47:14 -04001193 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001194 * @phba: Pointer to HBA context object.
1195 * @hbqno: HBQ number.
1196 *
1197 * This function is called with hbalock held to get the next
1198 * available slot for the given HBQ. If there is free slot
1199 * available for the HBQ it will return pointer to the next available
1200 * HBQ entry else it will return NULL.
1201 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001202static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001203lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1204{
1205 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1206
1207 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1208 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1209 hbqp->next_hbqPutIdx = 0;
1210
1211 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001212 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001213 uint32_t getidx = le32_to_cpu(raw_index);
1214
1215 hbqp->local_hbqGetIdx = getidx;
1216
1217 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1218 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001219 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001220 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001221 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001222 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001223 hbqp->entry_count);
1224
1225 phba->link_state = LPFC_HBA_ERROR;
1226 return NULL;
1227 }
1228
1229 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1230 return NULL;
1231 }
1232
James Smart51ef4c22007-08-02 11:10:31 -04001233 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1234 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001235}
1236
James Smarte59058c2008-08-24 21:49:00 -04001237/**
James Smart3621a712009-04-06 18:47:14 -04001238 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001239 * @phba: Pointer to HBA context object.
1240 *
1241 * This function is called with no lock held to free all the
1242 * hbq buffers while uninitializing the SLI interface. It also
1243 * frees the HBQ buffers returned by the firmware but not yet
1244 * processed by the upper layers.
1245 **/
James Smarted957682007-06-17 19:56:37 -05001246void
1247lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1248{
James Smart92d7f7b2007-06-17 19:56:38 -05001249 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1250 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001251 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001252 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001253 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001254
James Smart51ef4c22007-08-02 11:10:31 -04001255 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001256 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001257 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001258 for (i = 0; i < hbq_count; ++i) {
1259 list_for_each_entry_safe(dmabuf, next_dmabuf,
1260 &phba->hbqs[i].hbq_buffer_list, list) {
1261 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1262 list_del(&hbq_buf->dbuf.list);
1263 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1264 }
James Smarta8adb832007-10-27 13:37:53 -04001265 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001266 }
James Smart3163f722008-02-08 18:50:25 -05001267 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001268 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1269 list) {
James Smart3163f722008-02-08 18:50:25 -05001270 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271 list_del(&hbq_buf->dbuf.list);
1272 if (hbq_buf->tag == -1) {
1273 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274 (phba, hbq_buf);
1275 } else {
1276 hbqno = hbq_buf->tag >> 16;
1277 if (hbqno >= LPFC_MAX_HBQS)
1278 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1279 (phba, hbq_buf);
1280 else
1281 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1282 hbq_buf);
1283 }
1284 }
1285
1286 /* Mark the HBQs not in use */
1287 phba->hbq_in_use = 0;
1288 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001289}
1290
James Smarte59058c2008-08-24 21:49:00 -04001291/**
James Smart3621a712009-04-06 18:47:14 -04001292 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001293 * @phba: Pointer to HBA context object.
1294 * @hbqno: HBQ number.
1295 * @hbq_buf: Pointer to HBQ buffer.
1296 *
1297 * This function is called with the hbalock held to post a
1298 * hbq buffer to the firmware. If the function finds an empty
1299 * slot in the HBQ, it will post the buffer. The function will return
1300 * pointer to the hbq entry if it successfully post the buffer
1301 * else it will return NULL.
1302 **/
James Smart3772a992009-05-22 14:50:54 -04001303static int
James Smarted957682007-06-17 19:56:37 -05001304lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001305 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001306{
James Smart3772a992009-05-22 14:50:54 -04001307 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1308}
1309
1310/**
1311 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1312 * @phba: Pointer to HBA context object.
1313 * @hbqno: HBQ number.
1314 * @hbq_buf: Pointer to HBQ buffer.
1315 *
1316 * This function is called with the hbalock held to post a hbq buffer to the
1317 * firmware. If the function finds an empty slot in the HBQ, it will post the
1318 * buffer and place it on the hbq_buffer_list. The function will return zero if
1319 * it successfully post the buffer else it will return an error.
1320 **/
1321static int
1322lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1323 struct hbq_dmabuf *hbq_buf)
1324{
James Smarted957682007-06-17 19:56:37 -05001325 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001326 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001327
1328 /* Get next HBQ entry slot to use */
1329 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1330 if (hbqe) {
1331 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1332
James Smart92d7f7b2007-06-17 19:56:38 -05001333 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1334 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001335 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001336 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001337 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1338 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1339 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001340 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1341 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001342 /* flush */
James Smarted957682007-06-17 19:56:37 -05001343 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001344 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001345 return 0;
1346 } else
1347 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001348}
1349
James Smart4f774512009-05-22 14:52:35 -04001350/**
1351 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1352 * @phba: Pointer to HBA context object.
1353 * @hbqno: HBQ number.
1354 * @hbq_buf: Pointer to HBQ buffer.
1355 *
1356 * This function is called with the hbalock held to post an RQE to the SLI4
1357 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1358 * the hbq_buffer_list and return zero, otherwise it will return an error.
1359 **/
1360static int
1361lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1362 struct hbq_dmabuf *hbq_buf)
1363{
1364 int rc;
1365 struct lpfc_rqe hrqe;
1366 struct lpfc_rqe drqe;
1367
1368 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1369 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1370 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1371 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1372 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1373 &hrqe, &drqe);
1374 if (rc < 0)
1375 return rc;
1376 hbq_buf->tag = rc;
1377 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1378 return 0;
1379}
1380
James Smarte59058c2008-08-24 21:49:00 -04001381/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001382static struct lpfc_hbq_init lpfc_els_hbq = {
1383 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001384 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001385 .mask_count = 0,
1386 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001387 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001388 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001389 .init_count = 40,
1390 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001391};
James Smarted957682007-06-17 19:56:37 -05001392
James Smarte59058c2008-08-24 21:49:00 -04001393/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001394static struct lpfc_hbq_init lpfc_extra_hbq = {
1395 .rn = 1,
1396 .entry_count = 200,
1397 .mask_count = 0,
1398 .profile = 0,
1399 .ring_mask = (1 << LPFC_EXTRA_RING),
1400 .buffer_count = 0,
1401 .init_count = 0,
1402 .add_count = 5,
1403};
1404
James Smarte59058c2008-08-24 21:49:00 -04001405/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001406struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001407 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001408 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001409};
1410
James Smarte59058c2008-08-24 21:49:00 -04001411/**
James Smart3621a712009-04-06 18:47:14 -04001412 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001413 * @phba: Pointer to HBA context object.
1414 * @hbqno: HBQ number.
1415 * @count: Number of HBQ buffers to be posted.
1416 *
James Smartd7c255b2008-08-24 21:50:00 -04001417 * This function is called with no lock held to post more hbq buffers to the
1418 * given HBQ. The function returns the number of HBQ buffers successfully
1419 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001420 **/
James Smart311464e2007-08-02 11:10:37 -04001421static int
James Smart92d7f7b2007-06-17 19:56:38 -05001422lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1423{
James Smartd7c255b2008-08-24 21:50:00 -04001424 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001425 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001426 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001427 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001428 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001429 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001430
James Smartd7c255b2008-08-24 21:50:00 -04001431 if ((phba->hbqs[hbqno].buffer_count + count) >
1432 lpfc_hbq_defs[hbqno]->entry_count)
1433 count = lpfc_hbq_defs[hbqno]->entry_count -
1434 phba->hbqs[hbqno].buffer_count;
1435 if (!count)
1436 return 0;
1437 /* Allocate HBQ entries */
1438 for (i = 0; i < count; i++) {
1439 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1440 if (!hbq_buffer)
1441 break;
1442 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1443 }
James Smart3163f722008-02-08 18:50:25 -05001444 /* Check whether HBQ is still in use */
1445 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001446 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001447 goto err;
1448 while (!list_empty(&hbq_buf_list)) {
1449 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1450 dbuf.list);
1451 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1452 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001453 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001454 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001455 posted++;
1456 } else
James Smart51ef4c22007-08-02 11:10:31 -04001457 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001458 }
James Smart3163f722008-02-08 18:50:25 -05001459 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001460 return posted;
1461err:
1462 spin_unlock_irqrestore(&phba->hbalock, flags);
1463 while (!list_empty(&hbq_buf_list)) {
1464 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1465 dbuf.list);
1466 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467 }
James Smart92d7f7b2007-06-17 19:56:38 -05001468 return 0;
James Smarted957682007-06-17 19:56:37 -05001469}
1470
James Smarte59058c2008-08-24 21:49:00 -04001471/**
James Smart3621a712009-04-06 18:47:14 -04001472 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001473 * @phba: Pointer to HBA context object.
1474 * @qno: HBQ number.
1475 *
1476 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001477 * is called with no lock held. The function returns the number of HBQ entries
1478 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001479 **/
James Smarted957682007-06-17 19:56:37 -05001480int
James Smart92d7f7b2007-06-17 19:56:38 -05001481lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001482{
James Smartdef9c7a2009-12-21 17:02:28 -05001483 if (phba->sli_rev == LPFC_SLI_REV4)
1484 return 0;
1485 else
1486 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1487 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001488}
1489
James Smarte59058c2008-08-24 21:49:00 -04001490/**
James Smart3621a712009-04-06 18:47:14 -04001491 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001492 * @phba: Pointer to HBA context object.
1493 * @qno: HBQ queue number.
1494 *
1495 * This function is called from SLI initialization code path with
1496 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001497 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001498 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001499static int
James Smart92d7f7b2007-06-17 19:56:38 -05001500lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001501{
James Smartdef9c7a2009-12-21 17:02:28 -05001502 if (phba->sli_rev == LPFC_SLI_REV4)
1503 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1504 lpfc_hbq_defs[qno]->entry_count);
1505 else
1506 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1507 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001508}
1509
James Smarte59058c2008-08-24 21:49:00 -04001510/**
James Smart3772a992009-05-22 14:50:54 -04001511 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1512 * @phba: Pointer to HBA context object.
1513 * @hbqno: HBQ number.
1514 *
1515 * This function removes the first hbq buffer on an hbq list and returns a
1516 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1517 **/
1518static struct hbq_dmabuf *
1519lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1520{
1521 struct lpfc_dmabuf *d_buf;
1522
1523 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1524 if (!d_buf)
1525 return NULL;
1526 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1527}
1528
1529/**
James Smart3621a712009-04-06 18:47:14 -04001530 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001531 * @phba: Pointer to HBA context object.
1532 * @tag: Tag of the hbq buffer.
1533 *
1534 * This function is called with hbalock held. This function searches
1535 * for the hbq buffer associated with the given tag in the hbq buffer
1536 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1537 * it returns NULL.
1538 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001539static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001540lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1541{
James Smart92d7f7b2007-06-17 19:56:38 -05001542 struct lpfc_dmabuf *d_buf;
1543 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001544 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001545
James Smart51ef4c22007-08-02 11:10:31 -04001546 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001547 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001548 return NULL;
1549
James Smart3772a992009-05-22 14:50:54 -04001550 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001551 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001552 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001553 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001554 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001555 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001556 }
1557 }
James Smart3772a992009-05-22 14:50:54 -04001558 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001560 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001561 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001562 return NULL;
James Smarted957682007-06-17 19:56:37 -05001563}
1564
James Smarte59058c2008-08-24 21:49:00 -04001565/**
James Smart3621a712009-04-06 18:47:14 -04001566 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001567 * @phba: Pointer to HBA context object.
1568 * @hbq_buffer: Pointer to HBQ buffer.
1569 *
1570 * This function is called with hbalock. This function gives back
1571 * the hbq buffer to firmware. If the HBQ does not have space to
1572 * post the buffer, it will free the buffer.
1573 **/
James Smarted957682007-06-17 19:56:37 -05001574void
James Smart51ef4c22007-08-02 11:10:31 -04001575lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001576{
1577 uint32_t hbqno;
1578
James Smart51ef4c22007-08-02 11:10:31 -04001579 if (hbq_buffer) {
1580 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001581 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001582 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001583 }
1584}
1585
James Smarte59058c2008-08-24 21:49:00 -04001586/**
James Smart3621a712009-04-06 18:47:14 -04001587 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001588 * @mbxCommand: mailbox command code.
1589 *
1590 * This function is called by the mailbox event handler function to verify
1591 * that the completed mailbox command is a legitimate mailbox command. If the
1592 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1593 * and the mailbox event handler will take the HBA offline.
1594 **/
dea31012005-04-17 16:05:31 -05001595static int
1596lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1597{
1598 uint8_t ret;
1599
1600 switch (mbxCommand) {
1601 case MBX_LOAD_SM:
1602 case MBX_READ_NV:
1603 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001604 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001605 case MBX_RUN_BIU_DIAG:
1606 case MBX_INIT_LINK:
1607 case MBX_DOWN_LINK:
1608 case MBX_CONFIG_LINK:
1609 case MBX_CONFIG_RING:
1610 case MBX_RESET_RING:
1611 case MBX_READ_CONFIG:
1612 case MBX_READ_RCONFIG:
1613 case MBX_READ_SPARM:
1614 case MBX_READ_STATUS:
1615 case MBX_READ_RPI:
1616 case MBX_READ_XRI:
1617 case MBX_READ_REV:
1618 case MBX_READ_LNK_STAT:
1619 case MBX_REG_LOGIN:
1620 case MBX_UNREG_LOGIN:
1621 case MBX_READ_LA:
1622 case MBX_CLEAR_LA:
1623 case MBX_DUMP_MEMORY:
1624 case MBX_DUMP_CONTEXT:
1625 case MBX_RUN_DIAGS:
1626 case MBX_RESTART:
1627 case MBX_UPDATE_CFG:
1628 case MBX_DOWN_LOAD:
1629 case MBX_DEL_LD_ENTRY:
1630 case MBX_RUN_PROGRAM:
1631 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001632 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001633 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001634 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001635 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001636 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001637 case MBX_LOAD_AREA:
1638 case MBX_RUN_BIU_DIAG64:
1639 case MBX_CONFIG_PORT:
1640 case MBX_READ_SPARM64:
1641 case MBX_READ_RPI64:
1642 case MBX_REG_LOGIN64:
1643 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001644 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001645 case MBX_SET_DEBUG:
1646 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001647 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001648 case MBX_REG_VPI:
1649 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001650 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001651 case MBX_PORT_CAPABILITIES:
1652 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001653 case MBX_SLI4_CONFIG:
1654 case MBX_SLI4_REQ_FTRS:
1655 case MBX_REG_FCFI:
1656 case MBX_UNREG_FCFI:
1657 case MBX_REG_VFI:
1658 case MBX_UNREG_VFI:
1659 case MBX_INIT_VPI:
1660 case MBX_INIT_VFI:
1661 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001662 ret = mbxCommand;
1663 break;
1664 default:
1665 ret = MBX_SHUTDOWN;
1666 break;
1667 }
James Smart2e0fef82007-06-17 19:56:36 -05001668 return ret;
dea31012005-04-17 16:05:31 -05001669}
James Smarte59058c2008-08-24 21:49:00 -04001670
1671/**
James Smart3621a712009-04-06 18:47:14 -04001672 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001673 * @phba: Pointer to HBA context object.
1674 * @pmboxq: Pointer to mailbox command.
1675 *
1676 * This is completion handler function for mailbox commands issued from
1677 * lpfc_sli_issue_mbox_wait function. This function is called by the
1678 * mailbox event handler function with no lock held. This function
1679 * will wake up thread waiting on the wait queue pointed by context1
1680 * of the mailbox.
1681 **/
James Smart04c68492009-05-22 14:52:52 -04001682void
James Smart2e0fef82007-06-17 19:56:36 -05001683lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001684{
1685 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001686 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001687
1688 /*
1689 * If pdone_q is empty, the driver thread gave up waiting and
1690 * continued running.
1691 */
James Smart7054a602007-04-25 09:52:34 -04001692 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001693 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001694 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1695 if (pdone_q)
1696 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001697 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001698 return;
1699}
1700
James Smarte59058c2008-08-24 21:49:00 -04001701
1702/**
James Smart3621a712009-04-06 18:47:14 -04001703 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001704 * @phba: Pointer to HBA context object.
1705 * @pmb: Pointer to mailbox object.
1706 *
1707 * This function is the default mailbox completion handler. It
1708 * frees the memory resources associated with the completed mailbox
1709 * command. If the completed command is a REG_LOGIN mailbox command,
1710 * this function will issue a UREG_LOGIN to re-claim the RPI.
1711 **/
dea31012005-04-17 16:05:31 -05001712void
James Smart2e0fef82007-06-17 19:56:36 -05001713lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001714{
1715 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001716 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001717 int rc;
James Smart695a8142010-01-26 23:08:03 -05001718 struct lpfc_vport *vport = pmb->vport;
James Smart7054a602007-04-25 09:52:34 -04001719
dea31012005-04-17 16:05:31 -05001720 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001721
dea31012005-04-17 16:05:31 -05001722 if (mp) {
1723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1724 kfree(mp);
1725 }
James Smart7054a602007-04-25 09:52:34 -04001726
James Smart04c68492009-05-22 14:52:52 -04001727 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1728 (phba->sli_rev == LPFC_SLI_REV4))
1729 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1730
James Smart7054a602007-04-25 09:52:34 -04001731 /*
1732 * If a REG_LOGIN succeeded after node is destroyed or node
1733 * is in re-discovery driver need to cleanup the RPI.
1734 */
James Smart2e0fef82007-06-17 19:56:36 -05001735 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001736 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1737 !pmb->u.mb.mbxStatus) {
1738 rpi = pmb->u.mb.un.varWords[0];
1739 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1740 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001741 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001742 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1743 if (rc != MBX_NOT_FINISHED)
1744 return;
1745 }
1746
James Smart695a8142010-01-26 23:08:03 -05001747 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1748 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1749 !(phba->pport->load_flag & FC_UNLOADING) &&
1750 !pmb->u.mb.mbxStatus) {
1751 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1752 pmb->vport = vport;
1753 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1754 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1755 if (rc != MBX_NOT_FINISHED)
1756 return;
1757 }
1758
James Smart04c68492009-05-22 14:52:52 -04001759 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1760 lpfc_sli4_mbox_cmd_free(phba, pmb);
1761 else
1762 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001763}
1764
James Smarte59058c2008-08-24 21:49:00 -04001765/**
James Smart3621a712009-04-06 18:47:14 -04001766 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001767 * @phba: Pointer to HBA context object.
1768 *
1769 * This function is called with no lock held. This function processes all
1770 * the completed mailbox commands and gives it to upper layers. The interrupt
1771 * service routine processes mailbox completion interrupt and adds completed
1772 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1773 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1774 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1775 * function returns the mailbox commands to the upper layer by calling the
1776 * completion handler function of each mailbox.
1777 **/
dea31012005-04-17 16:05:31 -05001778int
James Smart2e0fef82007-06-17 19:56:36 -05001779lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001780{
James Smart92d7f7b2007-06-17 19:56:38 -05001781 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001782 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001783 int rc;
1784 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001785
1786 phba->sli.slistat.mbox_event++;
1787
James Smart92d7f7b2007-06-17 19:56:38 -05001788 /* Get all completed mailboxe buffers into the cmplq */
1789 spin_lock_irq(&phba->hbalock);
1790 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1791 spin_unlock_irq(&phba->hbalock);
1792
dea31012005-04-17 16:05:31 -05001793 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001794 do {
1795 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1796 if (pmb == NULL)
1797 break;
1798
James Smart04c68492009-05-22 14:52:52 -04001799 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001800
James Smart858c9f62007-06-17 19:56:39 -05001801 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1802 if (pmb->vport) {
1803 lpfc_debugfs_disc_trc(pmb->vport,
1804 LPFC_DISC_TRC_MBOX_VPORT,
1805 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1806 (uint32_t)pmbox->mbxCommand,
1807 pmbox->un.varWords[0],
1808 pmbox->un.varWords[1]);
1809 }
1810 else {
1811 lpfc_debugfs_disc_trc(phba->pport,
1812 LPFC_DISC_TRC_MBOX,
1813 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1814 (uint32_t)pmbox->mbxCommand,
1815 pmbox->un.varWords[0],
1816 pmbox->un.varWords[1]);
1817 }
1818 }
1819
dea31012005-04-17 16:05:31 -05001820 /*
1821 * It is a fatal error if unknown mbox command completion.
1822 */
1823 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1824 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001825 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001827 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001828 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001829 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001830 pmbox->mbxCommand,
1831 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001832 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001833 phba->work_hs = HS_FFER3;
1834 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001835 continue;
dea31012005-04-17 16:05:31 -05001836 }
1837
dea31012005-04-17 16:05:31 -05001838 if (pmbox->mbxStatus) {
1839 phba->sli.slistat.mbox_stat_err++;
1840 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1841 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001842 lpfc_printf_log(phba, KERN_INFO,
1843 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001844 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001845 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001846 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001847 pmb->vport ? pmb->vport->vpi :0,
1848 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001849 lpfc_sli4_mbox_opcode_get(phba,
1850 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001851 pmbox->mbxStatus,
1852 pmbox->un.varWords[0],
1853 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001854 pmbox->mbxStatus = 0;
1855 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001856 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001857 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001858 continue;
dea31012005-04-17 16:05:31 -05001859 }
1860 }
1861
1862 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001863 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001864 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001865 "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 -05001866 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001867 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001868 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001869 pmb->mbox_cmpl,
1870 *((uint32_t *) pmbox),
1871 pmbox->un.varWords[0],
1872 pmbox->un.varWords[1],
1873 pmbox->un.varWords[2],
1874 pmbox->un.varWords[3],
1875 pmbox->un.varWords[4],
1876 pmbox->un.varWords[5],
1877 pmbox->un.varWords[6],
1878 pmbox->un.varWords[7]);
1879
James Smart92d7f7b2007-06-17 19:56:38 -05001880 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001881 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001882 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001883 return 0;
dea31012005-04-17 16:05:31 -05001884}
James Smart92d7f7b2007-06-17 19:56:38 -05001885
James Smarte59058c2008-08-24 21:49:00 -04001886/**
James Smart3621a712009-04-06 18:47:14 -04001887 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001888 * @phba: Pointer to HBA context object.
1889 * @pring: Pointer to driver SLI ring object.
1890 * @tag: buffer tag.
1891 *
1892 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1893 * is set in the tag the buffer is posted for a particular exchange,
1894 * the function will return the buffer without replacing the buffer.
1895 * If the buffer is for unsolicited ELS or CT traffic, this function
1896 * returns the buffer and also posts another buffer to the firmware.
1897 **/
James Smart76bb24e2007-10-27 13:38:00 -04001898static struct lpfc_dmabuf *
1899lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001900 struct lpfc_sli_ring *pring,
1901 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001902{
James Smart9f1e1b52008-12-04 22:39:40 -05001903 struct hbq_dmabuf *hbq_entry;
1904
James Smart76bb24e2007-10-27 13:38:00 -04001905 if (tag & QUE_BUFTAG_BIT)
1906 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001907 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1908 if (!hbq_entry)
1909 return NULL;
1910 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001911}
James Smart57127f12007-10-27 13:37:05 -04001912
James Smart3772a992009-05-22 14:50:54 -04001913/**
1914 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1915 * @phba: Pointer to HBA context object.
1916 * @pring: Pointer to driver SLI ring object.
1917 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1918 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1919 * @fch_type: the type for the first frame of the sequence.
1920 *
1921 * This function is called with no lock held. This function uses the r_ctl and
1922 * type of the received sequence to find the correct callback function to call
1923 * to process the sequence.
1924 **/
1925static int
1926lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1927 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1928 uint32_t fch_type)
1929{
1930 int i;
1931
1932 /* unSolicited Responses */
1933 if (pring->prt[0].profile) {
1934 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1935 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1936 saveq);
1937 return 1;
1938 }
1939 /* We must search, based on rctl / type
1940 for the right routine */
1941 for (i = 0; i < pring->num_mask; i++) {
1942 if ((pring->prt[i].rctl == fch_r_ctl) &&
1943 (pring->prt[i].type == fch_type)) {
1944 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1945 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1946 (phba, pring, saveq);
1947 return 1;
1948 }
1949 }
1950 return 0;
1951}
James Smarte59058c2008-08-24 21:49:00 -04001952
1953/**
James Smart3621a712009-04-06 18:47:14 -04001954 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001955 * @phba: Pointer to HBA context object.
1956 * @pring: Pointer to driver SLI ring object.
1957 * @saveq: Pointer to the unsolicited iocb.
1958 *
1959 * This function is called with no lock held by the ring event handler
1960 * when there is an unsolicited iocb posted to the response ring by the
1961 * firmware. This function gets the buffer associated with the iocbs
1962 * and calls the event handler for the ring. This function handles both
1963 * qring buffers and hbq buffers.
1964 * When the function returns 1 the caller can free the iocb object otherwise
1965 * upper layer functions will free the iocb objects.
1966 **/
dea31012005-04-17 16:05:31 -05001967static int
1968lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1969 struct lpfc_iocbq *saveq)
1970{
1971 IOCB_t * irsp;
1972 WORD5 * w5p;
1973 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001974 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001975 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001976 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001977
1978 match = 0;
1979 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001980
1981 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1982 if (pring->lpfc_sli_rcv_async_status)
1983 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1984 else
1985 lpfc_printf_log(phba,
1986 KERN_WARNING,
1987 LOG_SLI,
1988 "0316 Ring %d handler: unexpected "
1989 "ASYNC_STATUS iocb received evt_code "
1990 "0x%x\n",
1991 pring->ringno,
1992 irsp->un.asyncstat.evt_code);
1993 return 1;
1994 }
1995
James Smart3163f722008-02-08 18:50:25 -05001996 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1997 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1998 if (irsp->ulpBdeCount > 0) {
1999 dmzbuf = lpfc_sli_get_buff(phba, pring,
2000 irsp->un.ulpWord[3]);
2001 lpfc_in_buf_free(phba, dmzbuf);
2002 }
2003
2004 if (irsp->ulpBdeCount > 1) {
2005 dmzbuf = lpfc_sli_get_buff(phba, pring,
2006 irsp->unsli3.sli3Words[3]);
2007 lpfc_in_buf_free(phba, dmzbuf);
2008 }
2009
2010 if (irsp->ulpBdeCount > 2) {
2011 dmzbuf = lpfc_sli_get_buff(phba, pring,
2012 irsp->unsli3.sli3Words[7]);
2013 lpfc_in_buf_free(phba, dmzbuf);
2014 }
2015
2016 return 1;
2017 }
2018
James Smart92d7f7b2007-06-17 19:56:38 -05002019 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002020 if (irsp->ulpBdeCount != 0) {
2021 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002022 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002023 if (!saveq->context2)
2024 lpfc_printf_log(phba,
2025 KERN_ERR,
2026 LOG_SLI,
2027 "0341 Ring %d Cannot find buffer for "
2028 "an unsolicited iocb. tag 0x%x\n",
2029 pring->ringno,
2030 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002031 }
2032 if (irsp->ulpBdeCount == 2) {
2033 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002034 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002035 if (!saveq->context3)
2036 lpfc_printf_log(phba,
2037 KERN_ERR,
2038 LOG_SLI,
2039 "0342 Ring %d Cannot find buffer for an"
2040 " unsolicited iocb. tag 0x%x\n",
2041 pring->ringno,
2042 irsp->unsli3.sli3Words[7]);
2043 }
2044 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002045 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002046 if (irsp->ulpBdeCount != 0) {
2047 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2048 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002049 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002050 lpfc_printf_log(phba,
2051 KERN_ERR,
2052 LOG_SLI,
2053 "0343 Ring %d Cannot find "
2054 "buffer for an unsolicited iocb"
2055 ". tag 0x%x\n", pring->ringno,
2056 irsp->un.ulpWord[3]);
2057 }
2058 if (irsp->ulpBdeCount == 2) {
2059 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2060 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002061 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002062 lpfc_printf_log(phba,
2063 KERN_ERR,
2064 LOG_SLI,
2065 "0344 Ring %d Cannot find "
2066 "buffer for an unsolicited "
2067 "iocb. tag 0x%x\n",
2068 pring->ringno,
2069 irsp->unsli3.sli3Words[7]);
2070 }
2071 }
James Smart92d7f7b2007-06-17 19:56:38 -05002072 }
James Smart9c2face2008-01-11 01:53:18 -05002073 if (irsp->ulpBdeCount != 0 &&
2074 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2075 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2076 int found = 0;
2077
2078 /* search continue save q for same XRI */
2079 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2080 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2081 list_add_tail(&saveq->list, &iocbq->list);
2082 found = 1;
2083 break;
2084 }
2085 }
2086 if (!found)
2087 list_add_tail(&saveq->clist,
2088 &pring->iocb_continue_saveq);
2089 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2090 list_del_init(&iocbq->clist);
2091 saveq = iocbq;
2092 irsp = &(saveq->iocb);
2093 } else
2094 return 0;
2095 }
2096 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2097 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2098 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002099 Rctl = FC_RCTL_ELS_REQ;
2100 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002101 } else {
2102 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2103 Rctl = w5p->hcsw.Rctl;
2104 Type = w5p->hcsw.Type;
2105
2106 /* Firmware Workaround */
2107 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2108 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2109 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002110 Rctl = FC_RCTL_ELS_REQ;
2111 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002112 w5p->hcsw.Rctl = Rctl;
2113 w5p->hcsw.Type = Type;
2114 }
2115 }
James Smart92d7f7b2007-06-17 19:56:38 -05002116
James Smart3772a992009-05-22 14:50:54 -04002117 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002118 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002119 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002120 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002121 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002122
James Smart92d7f7b2007-06-17 19:56:38 -05002123 return 1;
dea31012005-04-17 16:05:31 -05002124}
2125
James Smarte59058c2008-08-24 21:49:00 -04002126/**
James Smart3621a712009-04-06 18:47:14 -04002127 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002128 * @phba: Pointer to HBA context object.
2129 * @pring: Pointer to driver SLI ring object.
2130 * @prspiocb: Pointer to response iocb object.
2131 *
2132 * This function looks up the iocb_lookup table to get the command iocb
2133 * corresponding to the given response iocb using the iotag of the
2134 * response iocb. This function is called with the hbalock held.
2135 * This function returns the command iocb object if it finds the command
2136 * iocb else returns NULL.
2137 **/
dea31012005-04-17 16:05:31 -05002138static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002139lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2140 struct lpfc_sli_ring *pring,
2141 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002142{
dea31012005-04-17 16:05:31 -05002143 struct lpfc_iocbq *cmd_iocb = NULL;
2144 uint16_t iotag;
2145
James Bottomley604a3e32005-10-29 10:28:33 -05002146 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002147
James Bottomley604a3e32005-10-29 10:28:33 -05002148 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2149 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002150 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002151 pring->txcmplq_cnt--;
2152 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002153 }
2154
dea31012005-04-17 16:05:31 -05002155 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002156 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002157 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002158 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002159 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002160 return NULL;
2161}
2162
James Smarte59058c2008-08-24 21:49:00 -04002163/**
James Smart3772a992009-05-22 14:50:54 -04002164 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2165 * @phba: Pointer to HBA context object.
2166 * @pring: Pointer to driver SLI ring object.
2167 * @iotag: IOCB tag.
2168 *
2169 * This function looks up the iocb_lookup table to get the command iocb
2170 * corresponding to the given iotag. This function is called with the
2171 * hbalock held.
2172 * This function returns the command iocb object if it finds the command
2173 * iocb else returns NULL.
2174 **/
2175static struct lpfc_iocbq *
2176lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2177 struct lpfc_sli_ring *pring, uint16_t iotag)
2178{
2179 struct lpfc_iocbq *cmd_iocb;
2180
2181 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2182 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2183 list_del_init(&cmd_iocb->list);
2184 pring->txcmplq_cnt--;
2185 return cmd_iocb;
2186 }
2187
2188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2189 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2190 iotag, phba->sli.last_iotag);
2191 return NULL;
2192}
2193
2194/**
James Smart3621a712009-04-06 18:47:14 -04002195 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002196 * @phba: Pointer to HBA context object.
2197 * @pring: Pointer to driver SLI ring object.
2198 * @saveq: Pointer to the response iocb to be processed.
2199 *
2200 * This function is called by the ring event handler for non-fcp
2201 * rings when there is a new response iocb in the response ring.
2202 * The caller is not required to hold any locks. This function
2203 * gets the command iocb associated with the response iocb and
2204 * calls the completion handler for the command iocb. If there
2205 * is no completion handler, the function will free the resources
2206 * associated with command iocb. If the response iocb is for
2207 * an already aborted command iocb, the status of the completion
2208 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2209 * This function always returns 1.
2210 **/
dea31012005-04-17 16:05:31 -05002211static int
James Smart2e0fef82007-06-17 19:56:36 -05002212lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002213 struct lpfc_iocbq *saveq)
2214{
James Smart2e0fef82007-06-17 19:56:36 -05002215 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002216 int rc = 1;
2217 unsigned long iflag;
2218
2219 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002220 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002221 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002222 spin_unlock_irqrestore(&phba->hbalock, iflag);
2223
dea31012005-04-17 16:05:31 -05002224 if (cmdiocbp) {
2225 if (cmdiocbp->iocb_cmpl) {
2226 /*
James Smartea2151b2008-09-07 11:52:10 -04002227 * If an ELS command failed send an event to mgmt
2228 * application.
2229 */
2230 if (saveq->iocb.ulpStatus &&
2231 (pring->ringno == LPFC_ELS_RING) &&
2232 (cmdiocbp->iocb.ulpCommand ==
2233 CMD_ELS_REQUEST64_CR))
2234 lpfc_send_els_failure_event(phba,
2235 cmdiocbp, saveq);
2236
2237 /*
dea31012005-04-17 16:05:31 -05002238 * Post all ELS completions to the worker thread.
2239 * All other are passed to the completion callback.
2240 */
2241 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002242 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2243 (cmdiocbp->iocb_flag &
2244 LPFC_DRIVER_ABORTED)) {
2245 spin_lock_irqsave(&phba->hbalock,
2246 iflag);
James Smart07951072007-04-25 09:51:38 -04002247 cmdiocbp->iocb_flag &=
2248 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002249 spin_unlock_irqrestore(&phba->hbalock,
2250 iflag);
James Smart07951072007-04-25 09:51:38 -04002251 saveq->iocb.ulpStatus =
2252 IOSTAT_LOCAL_REJECT;
2253 saveq->iocb.un.ulpWord[4] =
2254 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002255
2256 /* Firmware could still be in progress
2257 * of DMAing payload, so don't free data
2258 * buffer till after a hbeat.
2259 */
James Smart341af102010-01-26 23:07:37 -05002260 spin_lock_irqsave(&phba->hbalock,
2261 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002262 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002263 spin_unlock_irqrestore(&phba->hbalock,
2264 iflag);
2265 }
James Smart0f65ff62010-02-26 14:14:23 -05002266 if (phba->sli_rev == LPFC_SLI_REV4) {
2267 if (saveq->iocb_flag &
2268 LPFC_EXCHANGE_BUSY) {
2269 /* Set cmdiocb flag for the
2270 * exchange busy so sgl (xri)
2271 * will not be released until
2272 * the abort xri is received
2273 * from hba.
2274 */
2275 spin_lock_irqsave(
2276 &phba->hbalock, iflag);
2277 cmdiocbp->iocb_flag |=
2278 LPFC_EXCHANGE_BUSY;
2279 spin_unlock_irqrestore(
2280 &phba->hbalock, iflag);
2281 }
2282 if (cmdiocbp->iocb_flag &
2283 LPFC_DRIVER_ABORTED) {
2284 /*
2285 * Clear LPFC_DRIVER_ABORTED
2286 * bit in case it was driver
2287 * initiated abort.
2288 */
2289 spin_lock_irqsave(
2290 &phba->hbalock, iflag);
2291 cmdiocbp->iocb_flag &=
2292 ~LPFC_DRIVER_ABORTED;
2293 spin_unlock_irqrestore(
2294 &phba->hbalock, iflag);
2295 cmdiocbp->iocb.ulpStatus =
2296 IOSTAT_LOCAL_REJECT;
2297 cmdiocbp->iocb.un.ulpWord[4] =
2298 IOERR_ABORT_REQUESTED;
2299 /*
2300 * For SLI4, irsiocb contains
2301 * NO_XRI in sli_xritag, it
2302 * shall not affect releasing
2303 * sgl (xri) process.
2304 */
2305 saveq->iocb.ulpStatus =
2306 IOSTAT_LOCAL_REJECT;
2307 saveq->iocb.un.ulpWord[4] =
2308 IOERR_SLI_ABORTED;
2309 spin_lock_irqsave(
2310 &phba->hbalock, iflag);
2311 saveq->iocb_flag |=
2312 LPFC_DELAY_MEM_FREE;
2313 spin_unlock_irqrestore(
2314 &phba->hbalock, iflag);
2315 }
James Smart07951072007-04-25 09:51:38 -04002316 }
dea31012005-04-17 16:05:31 -05002317 }
James Smart2e0fef82007-06-17 19:56:36 -05002318 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002319 } else
2320 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002321 } else {
2322 /*
2323 * Unknown initiating command based on the response iotag.
2324 * This could be the case on the ELS ring because of
2325 * lpfc_els_abort().
2326 */
2327 if (pring->ringno != LPFC_ELS_RING) {
2328 /*
2329 * Ring <ringno> handler: unexpected completion IoTag
2330 * <IoTag>
2331 */
James Smarta257bf92009-04-06 18:48:10 -04002332 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002333 "0322 Ring %d handler: "
2334 "unexpected completion IoTag x%x "
2335 "Data: x%x x%x x%x x%x\n",
2336 pring->ringno,
2337 saveq->iocb.ulpIoTag,
2338 saveq->iocb.ulpStatus,
2339 saveq->iocb.un.ulpWord[4],
2340 saveq->iocb.ulpCommand,
2341 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002342 }
2343 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002344
dea31012005-04-17 16:05:31 -05002345 return rc;
2346}
2347
James Smarte59058c2008-08-24 21:49:00 -04002348/**
James Smart3621a712009-04-06 18:47:14 -04002349 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002350 * @phba: Pointer to HBA context object.
2351 * @pring: Pointer to driver SLI ring object.
2352 *
2353 * This function is called from the iocb ring event handlers when
2354 * put pointer is ahead of the get pointer for a ring. This function signal
2355 * an error attention condition to the worker thread and the worker
2356 * thread will transition the HBA to offline state.
2357 **/
James Smart2e0fef82007-06-17 19:56:36 -05002358static void
2359lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002360{
James Smart34b02dc2008-08-24 21:49:55 -04002361 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002362 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002363 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002364 * rsp ring <portRspMax>
2365 */
2366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002367 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002368 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002369 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002370 pring->numRiocb);
2371
James Smart2e0fef82007-06-17 19:56:36 -05002372 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002373
2374 /*
2375 * All error attention handlers are posted to
2376 * worker thread
2377 */
2378 phba->work_ha |= HA_ERATT;
2379 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002380
James Smart5e9d9b82008-06-14 22:52:53 -04002381 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002382
2383 return;
2384}
2385
James Smarte59058c2008-08-24 21:49:00 -04002386/**
James Smart3621a712009-04-06 18:47:14 -04002387 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002388 * @ptr: Pointer to address of HBA context object.
2389 *
2390 * This function is invoked by the Error Attention polling timer when the
2391 * timer times out. It will check the SLI Error Attention register for
2392 * possible attention events. If so, it will post an Error Attention event
2393 * and wake up worker thread to process it. Otherwise, it will set up the
2394 * Error Attention polling timer for the next poll.
2395 **/
2396void lpfc_poll_eratt(unsigned long ptr)
2397{
2398 struct lpfc_hba *phba;
2399 uint32_t eratt = 0;
2400
2401 phba = (struct lpfc_hba *)ptr;
2402
2403 /* Check chip HA register for error event */
2404 eratt = lpfc_sli_check_eratt(phba);
2405
2406 if (eratt)
2407 /* Tell the worker thread there is work to do */
2408 lpfc_worker_wake_up(phba);
2409 else
2410 /* Restart the timer for next eratt poll */
2411 mod_timer(&phba->eratt_poll, jiffies +
2412 HZ * LPFC_ERATT_POLL_INTERVAL);
2413 return;
2414}
2415
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002416
James Smarte59058c2008-08-24 21:49:00 -04002417/**
James Smart3621a712009-04-06 18:47:14 -04002418 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002419 * @phba: Pointer to HBA context object.
2420 * @pring: Pointer to driver SLI ring object.
2421 * @mask: Host attention register mask for this ring.
2422 *
2423 * This function is called from the interrupt context when there is a ring
2424 * event for the fcp ring. The caller does not hold any lock.
2425 * The function processes each response iocb in the response ring until it
2426 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2427 * LE bit set. The function will call the completion handler of the command iocb
2428 * if the response iocb indicates a completion for a command iocb or it is
2429 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2430 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002431 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002432 * to check it explicitly.
2433 */
2434int
James Smart2e0fef82007-06-17 19:56:36 -05002435lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2436 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002437{
James Smart34b02dc2008-08-24 21:49:55 -04002438 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002439 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002440 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002441 struct lpfc_iocbq *cmdiocbq = NULL;
2442 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002443 uint32_t status;
2444 uint32_t portRspPut, portRspMax;
2445 int rc = 1;
2446 lpfc_iocb_type type;
2447 unsigned long iflag;
2448 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002449
James Smart2e0fef82007-06-17 19:56:36 -05002450 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002451 pring->stats.iocb_event++;
2452
dea31012005-04-17 16:05:31 -05002453 /*
2454 * The next available response entry should never exceed the maximum
2455 * entries. If it does, treat it as an adapter hardware error.
2456 */
2457 portRspMax = pring->numRiocb;
2458 portRspPut = le32_to_cpu(pgp->rspPutInx);
2459 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002460 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002461 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002462 return 1;
2463 }
James Smart45ed1192009-10-02 15:17:02 -04002464 if (phba->fcp_ring_in_use) {
2465 spin_unlock_irqrestore(&phba->hbalock, iflag);
2466 return 1;
2467 } else
2468 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002469
2470 rmb();
2471 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002472 /*
2473 * Fetch an entry off the ring and copy it into a local data
2474 * structure. The copy involves a byte-swap since the
2475 * network byte order and pci byte orders are different.
2476 */
James Smarted957682007-06-17 19:56:37 -05002477 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002478 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002479
2480 if (++pring->rspidx >= portRspMax)
2481 pring->rspidx = 0;
2482
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002483 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2484 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002485 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002486 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002487 irsp = &rspiocbq.iocb;
2488
dea31012005-04-17 16:05:31 -05002489 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2490 pring->stats.iocb_rsp++;
2491 rsp_cmpl++;
2492
2493 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002494 /*
2495 * If resource errors reported from HBA, reduce
2496 * queuedepths of the SCSI device.
2497 */
2498 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2499 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2500 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002501 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002502 spin_lock_irqsave(&phba->hbalock, iflag);
2503 }
2504
dea31012005-04-17 16:05:31 -05002505 /* Rsp ring <ringno> error: IOCB */
2506 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002507 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002508 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002509 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002510 irsp->un.ulpWord[0],
2511 irsp->un.ulpWord[1],
2512 irsp->un.ulpWord[2],
2513 irsp->un.ulpWord[3],
2514 irsp->un.ulpWord[4],
2515 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002516 *(uint32_t *)&irsp->un1,
2517 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002518 }
2519
2520 switch (type) {
2521 case LPFC_ABORT_IOCB:
2522 case LPFC_SOL_IOCB:
2523 /*
2524 * Idle exchange closed via ABTS from port. No iocb
2525 * resources need to be recovered.
2526 */
2527 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002529 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002530 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002531 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002532 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002533 break;
2534 }
2535
James Bottomley604a3e32005-10-29 10:28:33 -05002536 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2537 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002538 if (unlikely(!cmdiocbq))
2539 break;
2540 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2541 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2542 if (cmdiocbq->iocb_cmpl) {
2543 spin_unlock_irqrestore(&phba->hbalock, iflag);
2544 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2545 &rspiocbq);
2546 spin_lock_irqsave(&phba->hbalock, iflag);
2547 }
dea31012005-04-17 16:05:31 -05002548 break;
James Smarta4bc3372006-12-02 13:34:16 -05002549 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002550 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002551 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002552 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002553 break;
dea31012005-04-17 16:05:31 -05002554 default:
2555 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2556 char adaptermsg[LPFC_MAX_ADPTMSG];
2557 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2558 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2559 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002560 dev_warn(&((phba->pcidev)->dev),
2561 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002562 phba->brd_no, adaptermsg);
2563 } else {
2564 /* Unknown IOCB command */
2565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002566 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002567 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002568 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002569 irsp->ulpStatus,
2570 irsp->ulpIoTag,
2571 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002572 }
2573 break;
2574 }
2575
2576 /*
2577 * The response IOCB has been processed. Update the ring
2578 * pointer in SLIM. If the port response put pointer has not
2579 * been updated, sync the pgp->rspPutInx and fetch the new port
2580 * response put pointer.
2581 */
James Smarted957682007-06-17 19:56:37 -05002582 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002583
2584 if (pring->rspidx == portRspPut)
2585 portRspPut = le32_to_cpu(pgp->rspPutInx);
2586 }
2587
2588 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2589 pring->stats.iocb_rsp_full++;
2590 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2591 writel(status, phba->CAregaddr);
2592 readl(phba->CAregaddr);
2593 }
2594 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2595 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2596 pring->stats.iocb_cmd_empty++;
2597
2598 /* Force update of the local copy of cmdGetInx */
2599 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2600 lpfc_sli_resume_iocb(phba, pring);
2601
2602 if ((pring->lpfc_sli_cmd_available))
2603 (pring->lpfc_sli_cmd_available) (phba, pring);
2604
2605 }
2606
James Smart45ed1192009-10-02 15:17:02 -04002607 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002608 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002609 return rc;
2610}
2611
James Smarte59058c2008-08-24 21:49:00 -04002612/**
James Smart3772a992009-05-22 14:50:54 -04002613 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2614 * @phba: Pointer to HBA context object.
2615 * @pring: Pointer to driver SLI ring object.
2616 * @rspiocbp: Pointer to driver response IOCB object.
2617 *
2618 * This function is called from the worker thread when there is a slow-path
2619 * response IOCB to process. This function chains all the response iocbs until
2620 * seeing the iocb with the LE bit set. The function will call
2621 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2622 * completion of a command iocb. The function will call the
2623 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2624 * The function frees the resources or calls the completion handler if this
2625 * iocb is an abort completion. The function returns NULL when the response
2626 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2627 * this function shall chain the iocb on to the iocb_continueq and return the
2628 * response iocb passed in.
2629 **/
2630static struct lpfc_iocbq *
2631lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2632 struct lpfc_iocbq *rspiocbp)
2633{
2634 struct lpfc_iocbq *saveq;
2635 struct lpfc_iocbq *cmdiocbp;
2636 struct lpfc_iocbq *next_iocb;
2637 IOCB_t *irsp = NULL;
2638 uint32_t free_saveq;
2639 uint8_t iocb_cmd_type;
2640 lpfc_iocb_type type;
2641 unsigned long iflag;
2642 int rc;
2643
2644 spin_lock_irqsave(&phba->hbalock, iflag);
2645 /* First add the response iocb to the countinueq list */
2646 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2647 pring->iocb_continueq_cnt++;
2648
2649 /* Now, determine whetehr the list is completed for processing */
2650 irsp = &rspiocbp->iocb;
2651 if (irsp->ulpLe) {
2652 /*
2653 * By default, the driver expects to free all resources
2654 * associated with this iocb completion.
2655 */
2656 free_saveq = 1;
2657 saveq = list_get_first(&pring->iocb_continueq,
2658 struct lpfc_iocbq, list);
2659 irsp = &(saveq->iocb);
2660 list_del_init(&pring->iocb_continueq);
2661 pring->iocb_continueq_cnt = 0;
2662
2663 pring->stats.iocb_rsp++;
2664
2665 /*
2666 * If resource errors reported from HBA, reduce
2667 * queuedepths of the SCSI device.
2668 */
2669 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2670 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2671 spin_unlock_irqrestore(&phba->hbalock, iflag);
2672 phba->lpfc_rampdown_queue_depth(phba);
2673 spin_lock_irqsave(&phba->hbalock, iflag);
2674 }
2675
2676 if (irsp->ulpStatus) {
2677 /* Rsp ring <ringno> error: IOCB */
2678 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2679 "0328 Rsp Ring %d error: "
2680 "IOCB Data: "
2681 "x%x x%x x%x x%x "
2682 "x%x x%x x%x x%x "
2683 "x%x x%x x%x x%x "
2684 "x%x x%x x%x x%x\n",
2685 pring->ringno,
2686 irsp->un.ulpWord[0],
2687 irsp->un.ulpWord[1],
2688 irsp->un.ulpWord[2],
2689 irsp->un.ulpWord[3],
2690 irsp->un.ulpWord[4],
2691 irsp->un.ulpWord[5],
2692 *(((uint32_t *) irsp) + 6),
2693 *(((uint32_t *) irsp) + 7),
2694 *(((uint32_t *) irsp) + 8),
2695 *(((uint32_t *) irsp) + 9),
2696 *(((uint32_t *) irsp) + 10),
2697 *(((uint32_t *) irsp) + 11),
2698 *(((uint32_t *) irsp) + 12),
2699 *(((uint32_t *) irsp) + 13),
2700 *(((uint32_t *) irsp) + 14),
2701 *(((uint32_t *) irsp) + 15));
2702 }
2703
2704 /*
2705 * Fetch the IOCB command type and call the correct completion
2706 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2707 * get freed back to the lpfc_iocb_list by the discovery
2708 * kernel thread.
2709 */
2710 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2711 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2712 switch (type) {
2713 case LPFC_SOL_IOCB:
2714 spin_unlock_irqrestore(&phba->hbalock, iflag);
2715 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2716 spin_lock_irqsave(&phba->hbalock, iflag);
2717 break;
2718
2719 case LPFC_UNSOL_IOCB:
2720 spin_unlock_irqrestore(&phba->hbalock, iflag);
2721 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2722 spin_lock_irqsave(&phba->hbalock, iflag);
2723 if (!rc)
2724 free_saveq = 0;
2725 break;
2726
2727 case LPFC_ABORT_IOCB:
2728 cmdiocbp = NULL;
2729 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2730 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2731 saveq);
2732 if (cmdiocbp) {
2733 /* Call the specified completion routine */
2734 if (cmdiocbp->iocb_cmpl) {
2735 spin_unlock_irqrestore(&phba->hbalock,
2736 iflag);
2737 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2738 saveq);
2739 spin_lock_irqsave(&phba->hbalock,
2740 iflag);
2741 } else
2742 __lpfc_sli_release_iocbq(phba,
2743 cmdiocbp);
2744 }
2745 break;
2746
2747 case LPFC_UNKNOWN_IOCB:
2748 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2749 char adaptermsg[LPFC_MAX_ADPTMSG];
2750 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2751 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2752 MAX_MSG_DATA);
2753 dev_warn(&((phba->pcidev)->dev),
2754 "lpfc%d: %s\n",
2755 phba->brd_no, adaptermsg);
2756 } else {
2757 /* Unknown IOCB command */
2758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2759 "0335 Unknown IOCB "
2760 "command Data: x%x "
2761 "x%x x%x x%x\n",
2762 irsp->ulpCommand,
2763 irsp->ulpStatus,
2764 irsp->ulpIoTag,
2765 irsp->ulpContext);
2766 }
2767 break;
2768 }
2769
2770 if (free_saveq) {
2771 list_for_each_entry_safe(rspiocbp, next_iocb,
2772 &saveq->list, list) {
2773 list_del(&rspiocbp->list);
2774 __lpfc_sli_release_iocbq(phba, rspiocbp);
2775 }
2776 __lpfc_sli_release_iocbq(phba, saveq);
2777 }
2778 rspiocbp = NULL;
2779 }
2780 spin_unlock_irqrestore(&phba->hbalock, iflag);
2781 return rspiocbp;
2782}
2783
2784/**
2785 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002786 * @phba: Pointer to HBA context object.
2787 * @pring: Pointer to driver SLI ring object.
2788 * @mask: Host attention register mask for this ring.
2789 *
James Smart3772a992009-05-22 14:50:54 -04002790 * This routine wraps the actual slow_ring event process routine from the
2791 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002792 **/
James Smart3772a992009-05-22 14:50:54 -04002793void
James Smart2e0fef82007-06-17 19:56:36 -05002794lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2795 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002796{
James Smart3772a992009-05-22 14:50:54 -04002797 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2798}
2799
2800/**
2801 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2802 * @phba: Pointer to HBA context object.
2803 * @pring: Pointer to driver SLI ring object.
2804 * @mask: Host attention register mask for this ring.
2805 *
2806 * This function is called from the worker thread when there is a ring event
2807 * for non-fcp rings. The caller does not hold any lock. The function will
2808 * remove each response iocb in the response ring and calls the handle
2809 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2810 **/
2811static void
2812lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2813 struct lpfc_sli_ring *pring, uint32_t mask)
2814{
James Smart34b02dc2008-08-24 21:49:55 -04002815 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002816 IOCB_t *entry;
2817 IOCB_t *irsp = NULL;
2818 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002819 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002820 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002821 uint32_t status;
dea31012005-04-17 16:05:31 -05002822
James Smart34b02dc2008-08-24 21:49:55 -04002823 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002824 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002825 pring->stats.iocb_event++;
2826
dea31012005-04-17 16:05:31 -05002827 /*
2828 * The next available response entry should never exceed the maximum
2829 * entries. If it does, treat it as an adapter hardware error.
2830 */
2831 portRspMax = pring->numRiocb;
2832 portRspPut = le32_to_cpu(pgp->rspPutInx);
2833 if (portRspPut >= portRspMax) {
2834 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002835 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002836 * rsp ring <portRspMax>
2837 */
James Smarted957682007-06-17 19:56:37 -05002838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002839 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002840 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002841 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002842
James Smart2e0fef82007-06-17 19:56:36 -05002843 phba->link_state = LPFC_HBA_ERROR;
2844 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002845
2846 phba->work_hs = HS_FFER3;
2847 lpfc_handle_eratt(phba);
2848
James Smart3772a992009-05-22 14:50:54 -04002849 return;
dea31012005-04-17 16:05:31 -05002850 }
2851
2852 rmb();
dea31012005-04-17 16:05:31 -05002853 while (pring->rspidx != portRspPut) {
2854 /*
2855 * Build a completion list and call the appropriate handler.
2856 * The process is to get the next available response iocb, get
2857 * a free iocb from the list, copy the response data into the
2858 * free iocb, insert to the continuation list, and update the
2859 * next response index to slim. This process makes response
2860 * iocb's in the ring available to DMA as fast as possible but
2861 * pays a penalty for a copy operation. Since the iocb is
2862 * only 32 bytes, this penalty is considered small relative to
2863 * the PCI reads for register values and a slim write. When
2864 * the ulpLe field is set, the entire Command has been
2865 * received.
2866 */
James Smarted957682007-06-17 19:56:37 -05002867 entry = lpfc_resp_iocb(phba, pring);
2868
James Smart858c9f62007-06-17 19:56:39 -05002869 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002870 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002871 if (rspiocbp == NULL) {
2872 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002873 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002874 break;
2875 }
2876
James Smarted957682007-06-17 19:56:37 -05002877 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2878 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002879 irsp = &rspiocbp->iocb;
2880
2881 if (++pring->rspidx >= portRspMax)
2882 pring->rspidx = 0;
2883
James Smarta58cbd52007-08-02 11:09:43 -04002884 if (pring->ringno == LPFC_ELS_RING) {
2885 lpfc_debugfs_slow_ring_trc(phba,
2886 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2887 *(((uint32_t *) irsp) + 4),
2888 *(((uint32_t *) irsp) + 6),
2889 *(((uint32_t *) irsp) + 7));
2890 }
2891
James Smarted957682007-06-17 19:56:37 -05002892 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002893
James Smart3772a992009-05-22 14:50:54 -04002894 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895 /* Handle the response IOCB */
2896 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2897 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002898
2899 /*
2900 * If the port response put pointer has not been updated, sync
2901 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2902 * response put pointer.
2903 */
2904 if (pring->rspidx == portRspPut) {
2905 portRspPut = le32_to_cpu(pgp->rspPutInx);
2906 }
2907 } /* while (pring->rspidx != portRspPut) */
2908
James Smart92d7f7b2007-06-17 19:56:38 -05002909 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002910 /* At least one response entry has been freed */
2911 pring->stats.iocb_rsp_full++;
2912 /* SET RxRE_RSP in Chip Att register */
2913 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2914 writel(status, phba->CAregaddr);
2915 readl(phba->CAregaddr); /* flush */
2916 }
2917 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2918 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2919 pring->stats.iocb_cmd_empty++;
2920
2921 /* Force update of the local copy of cmdGetInx */
2922 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2923 lpfc_sli_resume_iocb(phba, pring);
2924
2925 if ((pring->lpfc_sli_cmd_available))
2926 (pring->lpfc_sli_cmd_available) (phba, pring);
2927
2928 }
2929
James Smart2e0fef82007-06-17 19:56:36 -05002930 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002931 return;
dea31012005-04-17 16:05:31 -05002932}
2933
James Smarte59058c2008-08-24 21:49:00 -04002934/**
James Smart4f774512009-05-22 14:52:35 -04002935 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2936 * @phba: Pointer to HBA context object.
2937 * @pring: Pointer to driver SLI ring object.
2938 * @mask: Host attention register mask for this ring.
2939 *
2940 * This function is called from the worker thread when there is a pending
2941 * ELS response iocb on the driver internal slow-path response iocb worker
2942 * queue. The caller does not hold any lock. The function will remove each
2943 * response iocb from the response worker queue and calls the handle
2944 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2945 **/
2946static void
2947lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2948 struct lpfc_sli_ring *pring, uint32_t mask)
2949{
2950 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002951 struct hbq_dmabuf *dmabuf;
2952 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002953 unsigned long iflag;
2954
James Smart45ed1192009-10-02 15:17:02 -04002955 spin_lock_irqsave(&phba->hbalock, iflag);
2956 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2957 spin_unlock_irqrestore(&phba->hbalock, iflag);
2958 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002959 /* Get the response iocb from the head of work queue */
2960 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002961 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002962 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002963 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002964
2965 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2966 case CQE_CODE_COMPL_WQE:
2967 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2968 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04002969 /* Translate ELS WCQE to response IOCBQ */
2970 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2971 irspiocbq);
2972 if (irspiocbq)
2973 lpfc_sli_sp_handle_rspiocb(phba, pring,
2974 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04002975 break;
2976 case CQE_CODE_RECEIVE:
2977 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2978 cq_event);
2979 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2980 break;
2981 default:
2982 break;
2983 }
James Smart4f774512009-05-22 14:52:35 -04002984 }
2985}
2986
2987/**
James Smart3621a712009-04-06 18:47:14 -04002988 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002989 * @phba: Pointer to HBA context object.
2990 * @pring: Pointer to driver SLI ring object.
2991 *
2992 * This function aborts all iocbs in the given ring and frees all the iocb
2993 * objects in txq. This function issues an abort iocb for all the iocb commands
2994 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2995 * the return of this function. The caller is not required to hold any locks.
2996 **/
James Smart2e0fef82007-06-17 19:56:36 -05002997void
dea31012005-04-17 16:05:31 -05002998lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2999{
James Smart2534ba72007-04-25 09:52:20 -04003000 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003001 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003002
James Smart92d7f7b2007-06-17 19:56:38 -05003003 if (pring->ringno == LPFC_ELS_RING) {
3004 lpfc_fabric_abort_hba(phba);
3005 }
3006
dea31012005-04-17 16:05:31 -05003007 /* Error everything on txq and txcmplq
3008 * First do the txq.
3009 */
James Smart2e0fef82007-06-17 19:56:36 -05003010 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003011 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003012 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003013
3014 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003015 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3016 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3017
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003019
James Smarta257bf92009-04-06 18:48:10 -04003020 /* Cancel all the IOCBs from the completions list */
3021 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3022 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003023}
3024
James Smarte59058c2008-08-24 21:49:00 -04003025/**
James Smart3621a712009-04-06 18:47:14 -04003026 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003027 * @phba: Pointer to HBA context object.
3028 *
3029 * This function flushes all iocbs in the fcp ring and frees all the iocb
3030 * objects in txq and txcmplq. This function will not issue abort iocbs
3031 * for all the iocb commands in txcmplq, they will just be returned with
3032 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3033 * slot has been permanently disabled.
3034 **/
3035void
3036lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3037{
3038 LIST_HEAD(txq);
3039 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003040 struct lpfc_sli *psli = &phba->sli;
3041 struct lpfc_sli_ring *pring;
3042
3043 /* Currently, only one fcp ring */
3044 pring = &psli->ring[psli->fcp_ring];
3045
3046 spin_lock_irq(&phba->hbalock);
3047 /* Retrieve everything on txq */
3048 list_splice_init(&pring->txq, &txq);
3049 pring->txq_cnt = 0;
3050
3051 /* Retrieve everything on the txcmplq */
3052 list_splice_init(&pring->txcmplq, &txcmplq);
3053 pring->txcmplq_cnt = 0;
3054 spin_unlock_irq(&phba->hbalock);
3055
3056 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003057 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3058 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003059
3060 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003061 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3062 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003063}
3064
3065/**
James Smart3772a992009-05-22 14:50:54 -04003066 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003067 * @phba: Pointer to HBA context object.
3068 * @mask: Bit mask to be checked.
3069 *
3070 * This function reads the host status register and compares
3071 * with the provided bit mask to check if HBA completed
3072 * the restart. This function will wait in a loop for the
3073 * HBA to complete restart. If the HBA does not restart within
3074 * 15 iterations, the function will reset the HBA again. The
3075 * function returns 1 when HBA fail to restart otherwise returns
3076 * zero.
3077 **/
James Smart3772a992009-05-22 14:50:54 -04003078static int
3079lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003080{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003081 uint32_t status;
3082 int i = 0;
3083 int retval = 0;
dea31012005-04-17 16:05:31 -05003084
Jamie Wellnitz41415862006-02-28 19:25:27 -05003085 /* Read the HBA Host Status Register */
3086 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003087
Jamie Wellnitz41415862006-02-28 19:25:27 -05003088 /*
3089 * Check status register every 100ms for 5 retries, then every
3090 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3091 * every 2.5 sec for 4.
3092 * Break our of the loop if errors occurred during init.
3093 */
3094 while (((status & mask) != mask) &&
3095 !(status & HS_FFERM) &&
3096 i++ < 20) {
dea31012005-04-17 16:05:31 -05003097
Jamie Wellnitz41415862006-02-28 19:25:27 -05003098 if (i <= 5)
3099 msleep(10);
3100 else if (i <= 10)
3101 msleep(500);
3102 else
3103 msleep(2500);
dea31012005-04-17 16:05:31 -05003104
Jamie Wellnitz41415862006-02-28 19:25:27 -05003105 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003106 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003107 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003108 lpfc_sli_brdrestart(phba);
3109 }
3110 /* Read the HBA Host Status Register */
3111 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003112 }
dea31012005-04-17 16:05:31 -05003113
Jamie Wellnitz41415862006-02-28 19:25:27 -05003114 /* Check to see if any errors occurred during init */
3115 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3117 "2751 Adapter failed to restart, "
3118 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3119 status,
3120 readl(phba->MBslimaddr + 0xa8),
3121 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003122 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003123 retval = 1;
3124 }
dea31012005-04-17 16:05:31 -05003125
Jamie Wellnitz41415862006-02-28 19:25:27 -05003126 return retval;
dea31012005-04-17 16:05:31 -05003127}
3128
James Smartda0436e2009-05-22 14:51:39 -04003129/**
3130 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3131 * @phba: Pointer to HBA context object.
3132 * @mask: Bit mask to be checked.
3133 *
3134 * This function checks the host status register to check if HBA is
3135 * ready. This function will wait in a loop for the HBA to be ready
3136 * If the HBA is not ready , the function will will reset the HBA PCI
3137 * function again. The function returns 1 when HBA fail to be ready
3138 * otherwise returns zero.
3139 **/
3140static int
3141lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3142{
3143 uint32_t status;
3144 int retval = 0;
3145
3146 /* Read the HBA Host Status Register */
3147 status = lpfc_sli4_post_status_check(phba);
3148
3149 if (status) {
3150 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3151 lpfc_sli_brdrestart(phba);
3152 status = lpfc_sli4_post_status_check(phba);
3153 }
3154
3155 /* Check to see if any errors occurred during init */
3156 if (status) {
3157 phba->link_state = LPFC_HBA_ERROR;
3158 retval = 1;
3159 } else
3160 phba->sli4_hba.intr_enable = 0;
3161
3162 return retval;
3163}
3164
3165/**
3166 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3167 * @phba: Pointer to HBA context object.
3168 * @mask: Bit mask to be checked.
3169 *
3170 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3171 * from the API jump table function pointer from the lpfc_hba struct.
3172 **/
3173int
3174lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3175{
3176 return phba->lpfc_sli_brdready(phba, mask);
3177}
3178
James Smart92908312006-03-07 15:04:13 -05003179#define BARRIER_TEST_PATTERN (0xdeadbeef)
3180
James Smarte59058c2008-08-24 21:49:00 -04003181/**
James Smart3621a712009-04-06 18:47:14 -04003182 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003183 * @phba: Pointer to HBA context object.
3184 *
3185 * This function is called before resetting an HBA. This
3186 * function requests HBA to quiesce DMAs before a reset.
3187 **/
James Smart2e0fef82007-06-17 19:56:36 -05003188void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003189{
James Smart65a29c12006-07-06 15:50:50 -04003190 uint32_t __iomem *resp_buf;
3191 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003192 volatile uint32_t mbox;
3193 uint32_t hc_copy;
3194 int i;
3195 uint8_t hdrtype;
3196
3197 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3198 if (hdrtype != 0x80 ||
3199 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3200 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3201 return;
3202
3203 /*
3204 * Tell the other part of the chip to suspend temporarily all
3205 * its DMA activity.
3206 */
James Smart65a29c12006-07-06 15:50:50 -04003207 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003208
3209 /* Disable the error attention */
3210 hc_copy = readl(phba->HCregaddr);
3211 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3212 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003213 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003214
3215 if (readl(phba->HAregaddr) & HA_ERATT) {
3216 /* Clear Chip error bit */
3217 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003218 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003219 }
3220
3221 mbox = 0;
3222 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3223 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3224
3225 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003226 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003227 writel(mbox, mbox_buf);
3228
3229 for (i = 0;
3230 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3231 mdelay(1);
3232
3233 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003234 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003235 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003236 goto restore_hc;
3237 else
3238 goto clear_errat;
3239 }
3240
3241 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3242 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3243 mdelay(1);
3244
3245clear_errat:
3246
3247 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3248 mdelay(1);
3249
3250 if (readl(phba->HAregaddr) & HA_ERATT) {
3251 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003252 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003253 }
3254
3255restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003256 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003257 writel(hc_copy, phba->HCregaddr);
3258 readl(phba->HCregaddr); /* flush */
3259}
3260
James Smarte59058c2008-08-24 21:49:00 -04003261/**
James Smart3621a712009-04-06 18:47:14 -04003262 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003263 * @phba: Pointer to HBA context object.
3264 *
3265 * This function issues a kill_board mailbox command and waits for
3266 * the error attention interrupt. This function is called for stopping
3267 * the firmware processing. The caller is not required to hold any
3268 * locks. This function calls lpfc_hba_down_post function to free
3269 * any pending commands after the kill. The function will return 1 when it
3270 * fails to kill the board else will return 0.
3271 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003272int
James Smart2e0fef82007-06-17 19:56:36 -05003273lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003274{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003275 struct lpfc_sli *psli;
3276 LPFC_MBOXQ_t *pmb;
3277 uint32_t status;
3278 uint32_t ha_copy;
3279 int retval;
3280 int i = 0;
3281
3282 psli = &phba->sli;
3283
3284 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003285 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003286 "0329 Kill HBA Data: x%x x%x\n",
3287 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003288
James Smart98c9ea52007-10-27 13:37:33 -04003289 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3290 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003291 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003292
3293 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003294 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003295 status = readl(phba->HCregaddr);
3296 status &= ~HC_ERINT_ENA;
3297 writel(status, phba->HCregaddr);
3298 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003299 phba->link_flag |= LS_IGNORE_ERATT;
3300 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003301
3302 lpfc_kill_board(phba, pmb);
3303 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3304 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3305
3306 if (retval != MBX_SUCCESS) {
3307 if (retval != MBX_BUSY)
3308 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3310 "2752 KILL_BOARD command failed retval %d\n",
3311 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003312 spin_lock_irq(&phba->hbalock);
3313 phba->link_flag &= ~LS_IGNORE_ERATT;
3314 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003315 return 1;
3316 }
3317
James Smartf4b4c682009-05-22 14:53:12 -04003318 spin_lock_irq(&phba->hbalock);
3319 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3320 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003321
Jamie Wellnitz41415862006-02-28 19:25:27 -05003322 mempool_free(pmb, phba->mbox_mem_pool);
3323
3324 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3325 * attention every 100ms for 3 seconds. If we don't get ERATT after
3326 * 3 seconds we still set HBA_ERROR state because the status of the
3327 * board is now undefined.
3328 */
3329 ha_copy = readl(phba->HAregaddr);
3330
3331 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3332 mdelay(100);
3333 ha_copy = readl(phba->HAregaddr);
3334 }
3335
3336 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003337 if (ha_copy & HA_ERATT) {
3338 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003339 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003340 }
James Smart2e0fef82007-06-17 19:56:36 -05003341 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003342 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003343 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003344 phba->link_flag &= ~LS_IGNORE_ERATT;
3345 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003346
Jamie Wellnitz41415862006-02-28 19:25:27 -05003347 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003348 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003349
James Smart2e0fef82007-06-17 19:56:36 -05003350 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003351}
3352
James Smarte59058c2008-08-24 21:49:00 -04003353/**
James Smart3772a992009-05-22 14:50:54 -04003354 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003355 * @phba: Pointer to HBA context object.
3356 *
3357 * This function resets the HBA by writing HC_INITFF to the control
3358 * register. After the HBA resets, this function resets all the iocb ring
3359 * indices. This function disables PCI layer parity checking during
3360 * the reset.
3361 * This function returns 0 always.
3362 * The caller is not required to hold any locks.
3363 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003364int
James Smart2e0fef82007-06-17 19:56:36 -05003365lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003366{
3367 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003368 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003369 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003370 int i;
dea31012005-04-17 16:05:31 -05003371
Jamie Wellnitz41415862006-02-28 19:25:27 -05003372 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003373
Jamie Wellnitz41415862006-02-28 19:25:27 -05003374 /* Reset HBA */
3375 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003376 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003377 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003378
3379 /* perform board reset */
3380 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003381 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003382 phba->pport->fc_myDID = 0;
3383 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003384
Jamie Wellnitz41415862006-02-28 19:25:27 -05003385 /* Turn off parity checking and serr during the physical reset */
3386 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3387 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3388 (cfg_value &
3389 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3390
James Smart3772a992009-05-22 14:50:54 -04003391 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3392
Jamie Wellnitz41415862006-02-28 19:25:27 -05003393 /* Now toggle INITFF bit in the Host Control Register */
3394 writel(HC_INITFF, phba->HCregaddr);
3395 mdelay(1);
3396 readl(phba->HCregaddr); /* flush */
3397 writel(0, phba->HCregaddr);
3398 readl(phba->HCregaddr); /* flush */
3399
3400 /* Restore PCI cmd register */
3401 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003402
3403 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003404 for (i = 0; i < psli->num_rings; i++) {
3405 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003406 pring->flag = 0;
3407 pring->rspidx = 0;
3408 pring->next_cmdidx = 0;
3409 pring->local_getidx = 0;
3410 pring->cmdidx = 0;
3411 pring->missbufcnt = 0;
3412 }
dea31012005-04-17 16:05:31 -05003413
James Smart2e0fef82007-06-17 19:56:36 -05003414 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003415 return 0;
3416}
3417
James Smarte59058c2008-08-24 21:49:00 -04003418/**
James Smartda0436e2009-05-22 14:51:39 -04003419 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3420 * @phba: Pointer to HBA context object.
3421 *
3422 * This function resets a SLI4 HBA. This function disables PCI layer parity
3423 * checking during resets the device. The caller is not required to hold
3424 * any locks.
3425 *
3426 * This function returns 0 always.
3427 **/
3428int
3429lpfc_sli4_brdreset(struct lpfc_hba *phba)
3430{
3431 struct lpfc_sli *psli = &phba->sli;
3432 uint16_t cfg_value;
3433 uint8_t qindx;
3434
3435 /* Reset HBA */
3436 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3437 "0295 Reset HBA Data: x%x x%x\n",
3438 phba->pport->port_state, psli->sli_flag);
3439
3440 /* perform board reset */
3441 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003442 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003443 phba->pport->fc_myDID = 0;
3444 phba->pport->fc_prevDID = 0;
3445
3446 /* Turn off parity checking and serr during the physical reset */
3447 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3448 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3449 (cfg_value &
3450 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3451
3452 spin_lock_irq(&phba->hbalock);
3453 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3454 phba->fcf.fcf_flag = 0;
3455 /* Clean up the child queue list for the CQs */
3456 list_del_init(&phba->sli4_hba.mbx_wq->list);
3457 list_del_init(&phba->sli4_hba.els_wq->list);
3458 list_del_init(&phba->sli4_hba.hdr_rq->list);
3459 list_del_init(&phba->sli4_hba.dat_rq->list);
3460 list_del_init(&phba->sli4_hba.mbx_cq->list);
3461 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003462 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3463 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3464 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3465 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3466 spin_unlock_irq(&phba->hbalock);
3467
3468 /* Now physically reset the device */
3469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3470 "0389 Performing PCI function reset!\n");
3471 /* Perform FCoE PCI function reset */
3472 lpfc_pci_function_reset(phba);
3473
3474 return 0;
3475}
3476
3477/**
3478 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003479 * @phba: Pointer to HBA context object.
3480 *
3481 * This function is called in the SLI initialization code path to
3482 * restart the HBA. The caller is not required to hold any lock.
3483 * This function writes MBX_RESTART mailbox command to the SLIM and
3484 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3485 * function to free any pending commands. The function enables
3486 * POST only during the first initialization. The function returns zero.
3487 * The function does not guarantee completion of MBX_RESTART mailbox
3488 * command before the return of this function.
3489 **/
James Smartda0436e2009-05-22 14:51:39 -04003490static int
3491lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003492{
3493 MAILBOX_t *mb;
3494 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 volatile uint32_t word0;
3496 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003497 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003498
James Smart2e0fef82007-06-17 19:56:36 -05003499 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003500
James Smart0d878412009-10-02 15:16:56 -04003501 /* Take PCIe device Advanced Error Reporting (AER) state */
3502 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3503
Jamie Wellnitz41415862006-02-28 19:25:27 -05003504 psli = &phba->sli;
3505
3506 /* Restart HBA */
3507 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003508 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003509 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003510
3511 word0 = 0;
3512 mb = (MAILBOX_t *) &word0;
3513 mb->mbxCommand = MBX_RESTART;
3514 mb->mbxHc = 1;
3515
James Smart92908312006-03-07 15:04:13 -05003516 lpfc_reset_barrier(phba);
3517
Jamie Wellnitz41415862006-02-28 19:25:27 -05003518 to_slim = phba->MBslimaddr;
3519 writel(*(uint32_t *) mb, to_slim);
3520 readl(to_slim); /* flush */
3521
3522 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003523 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003524 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003525 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003526 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003527 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003528 writel(*(uint32_t *) mb, to_slim);
3529 readl(to_slim); /* flush */
3530
3531 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003532 phba->pport->stopped = 0;
3533 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003534 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003535 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536
James Smart64ba8812006-08-02 15:24:34 -04003537 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3538 psli->stats_start = get_seconds();
3539
James Smarteaf15d52008-12-04 22:39:29 -05003540 /* Give the INITFF and Post time to settle. */
3541 mdelay(100);
dea31012005-04-17 16:05:31 -05003542
James Smart0d878412009-10-02 15:16:56 -04003543 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3544 if (hba_aer_enabled)
3545 pci_disable_pcie_error_reporting(phba->pcidev);
3546
Jamie Wellnitz41415862006-02-28 19:25:27 -05003547 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003548
3549 return 0;
3550}
3551
James Smarte59058c2008-08-24 21:49:00 -04003552/**
James Smartda0436e2009-05-22 14:51:39 -04003553 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3554 * @phba: Pointer to HBA context object.
3555 *
3556 * This function is called in the SLI initialization code path to restart
3557 * a SLI4 HBA. The caller is not required to hold any lock.
3558 * At the end of the function, it calls lpfc_hba_down_post function to
3559 * free any pending commands.
3560 **/
3561static int
3562lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3563{
3564 struct lpfc_sli *psli = &phba->sli;
3565
3566
3567 /* Restart HBA */
3568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3569 "0296 Restart HBA Data: x%x x%x\n",
3570 phba->pport->port_state, psli->sli_flag);
3571
3572 lpfc_sli4_brdreset(phba);
3573
3574 spin_lock_irq(&phba->hbalock);
3575 phba->pport->stopped = 0;
3576 phba->link_state = LPFC_INIT_START;
3577 phba->hba_flag = 0;
3578 spin_unlock_irq(&phba->hbalock);
3579
3580 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3581 psli->stats_start = get_seconds();
3582
3583 lpfc_hba_down_post(phba);
3584
3585 return 0;
3586}
3587
3588/**
3589 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3590 * @phba: Pointer to HBA context object.
3591 *
3592 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3593 * API jump table function pointer from the lpfc_hba struct.
3594**/
3595int
3596lpfc_sli_brdrestart(struct lpfc_hba *phba)
3597{
3598 return phba->lpfc_sli_brdrestart(phba);
3599}
3600
3601/**
James Smart3621a712009-04-06 18:47:14 -04003602 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003603 * @phba: Pointer to HBA context object.
3604 *
3605 * This function is called after a HBA restart to wait for successful
3606 * restart of the HBA. Successful restart of the HBA is indicated by
3607 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3608 * iteration, the function will restart the HBA again. The function returns
3609 * zero if HBA successfully restarted else returns negative error code.
3610 **/
dea31012005-04-17 16:05:31 -05003611static int
3612lpfc_sli_chipset_init(struct lpfc_hba *phba)
3613{
3614 uint32_t status, i = 0;
3615
3616 /* Read the HBA Host Status Register */
3617 status = readl(phba->HSregaddr);
3618
3619 /* Check status register to see what current state is */
3620 i = 0;
3621 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3622
3623 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3624 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3625 * 4.
3626 */
3627 if (i++ >= 20) {
3628 /* Adapter failed to init, timeout, status reg
3629 <status> */
James Smarted957682007-06-17 19:56:37 -05003630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003631 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003632 "timeout, status reg x%x, "
3633 "FW Data: A8 x%x AC x%x\n", status,
3634 readl(phba->MBslimaddr + 0xa8),
3635 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003636 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003637 return -ETIMEDOUT;
3638 }
3639
3640 /* Check to see if any errors occurred during init */
3641 if (status & HS_FFERM) {
3642 /* ERROR: During chipset initialization */
3643 /* Adapter failed to init, chipset, status reg
3644 <status> */
James Smarted957682007-06-17 19:56:37 -05003645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003646 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003647 "chipset, status reg x%x, "
3648 "FW Data: A8 x%x AC x%x\n", status,
3649 readl(phba->MBslimaddr + 0xa8),
3650 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003651 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003652 return -EIO;
3653 }
3654
3655 if (i <= 5) {
3656 msleep(10);
3657 } else if (i <= 10) {
3658 msleep(500);
3659 } else {
3660 msleep(2500);
3661 }
3662
3663 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003664 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003665 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003666 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003667 }
3668 /* Read the HBA Host Status Register */
3669 status = readl(phba->HSregaddr);
3670 }
3671
3672 /* Check to see if any errors occurred during init */
3673 if (status & HS_FFERM) {
3674 /* ERROR: During chipset initialization */
3675 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003677 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003678 "status reg x%x, "
3679 "FW Data: A8 x%x AC x%x\n", status,
3680 readl(phba->MBslimaddr + 0xa8),
3681 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003682 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003683 return -EIO;
3684 }
3685
3686 /* Clear all interrupt enable conditions */
3687 writel(0, phba->HCregaddr);
3688 readl(phba->HCregaddr); /* flush */
3689
3690 /* setup host attn register */
3691 writel(0xffffffff, phba->HAregaddr);
3692 readl(phba->HAregaddr); /* flush */
3693 return 0;
3694}
3695
James Smarte59058c2008-08-24 21:49:00 -04003696/**
James Smart3621a712009-04-06 18:47:14 -04003697 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003698 *
3699 * This function calculates and returns the number of HBQs required to be
3700 * configured.
3701 **/
James Smart78b2d852007-08-02 11:10:21 -04003702int
James Smarted957682007-06-17 19:56:37 -05003703lpfc_sli_hbq_count(void)
3704{
James Smart92d7f7b2007-06-17 19:56:38 -05003705 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003706}
3707
James Smarte59058c2008-08-24 21:49:00 -04003708/**
James Smart3621a712009-04-06 18:47:14 -04003709 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003710 *
3711 * This function adds the number of hbq entries in every HBQ to get
3712 * the total number of hbq entries required for the HBA and returns
3713 * the total count.
3714 **/
James Smarted957682007-06-17 19:56:37 -05003715static int
3716lpfc_sli_hbq_entry_count(void)
3717{
3718 int hbq_count = lpfc_sli_hbq_count();
3719 int count = 0;
3720 int i;
3721
3722 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003723 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003724 return count;
3725}
3726
James Smarte59058c2008-08-24 21:49:00 -04003727/**
James Smart3621a712009-04-06 18:47:14 -04003728 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003729 *
3730 * This function calculates amount of memory required for all hbq entries
3731 * to be configured and returns the total memory required.
3732 **/
dea31012005-04-17 16:05:31 -05003733int
James Smarted957682007-06-17 19:56:37 -05003734lpfc_sli_hbq_size(void)
3735{
3736 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3737}
3738
James Smarte59058c2008-08-24 21:49:00 -04003739/**
James Smart3621a712009-04-06 18:47:14 -04003740 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003741 * @phba: Pointer to HBA context object.
3742 *
3743 * This function is called during the SLI initialization to configure
3744 * all the HBQs and post buffers to the HBQ. The caller is not
3745 * required to hold any locks. This function will return zero if successful
3746 * else it will return negative error code.
3747 **/
James Smarted957682007-06-17 19:56:37 -05003748static int
3749lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3750{
3751 int hbq_count = lpfc_sli_hbq_count();
3752 LPFC_MBOXQ_t *pmb;
3753 MAILBOX_t *pmbox;
3754 uint32_t hbqno;
3755 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003756
James Smart92d7f7b2007-06-17 19:56:38 -05003757 /* Get a Mailbox buffer to setup mailbox
3758 * commands for HBA initialization
3759 */
James Smarted957682007-06-17 19:56:37 -05003760 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3761
3762 if (!pmb)
3763 return -ENOMEM;
3764
James Smart04c68492009-05-22 14:52:52 -04003765 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003766
3767 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3768 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003769 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003770
3771 hbq_entry_index = 0;
3772 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3773 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3774 phba->hbqs[hbqno].hbqPutIdx = 0;
3775 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3776 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003777 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003778 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3779 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003780 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3781
3782 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3783 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3784 mbxStatus <status>, ring <num> */
3785
3786 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003787 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003788 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003789 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003790 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003791 pmbox->mbxStatus, hbqno);
3792
3793 phba->link_state = LPFC_HBA_ERROR;
3794 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003795 return ENXIO;
3796 }
3797 }
3798 phba->hbq_count = hbq_count;
3799
James Smarted957682007-06-17 19:56:37 -05003800 mempool_free(pmb, phba->mbox_mem_pool);
3801
James Smart92d7f7b2007-06-17 19:56:38 -05003802 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003803 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3804 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003805 return 0;
3806}
3807
James Smarte59058c2008-08-24 21:49:00 -04003808/**
James Smart4f774512009-05-22 14:52:35 -04003809 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3810 * @phba: Pointer to HBA context object.
3811 *
3812 * This function is called during the SLI initialization to configure
3813 * all the HBQs and post buffers to the HBQ. The caller is not
3814 * required to hold any locks. This function will return zero if successful
3815 * else it will return negative error code.
3816 **/
3817static int
3818lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3819{
3820 phba->hbq_in_use = 1;
3821 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3822 phba->hbq_count = 1;
3823 /* Initially populate or replenish the HBQs */
3824 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3825 return 0;
3826}
3827
3828/**
James Smart3621a712009-04-06 18:47:14 -04003829 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003830 * @phba: Pointer to HBA context object.
3831 * @sli_mode: sli mode - 2/3
3832 *
3833 * This function is called by the sli intialization code path
3834 * to issue config_port mailbox command. This function restarts the
3835 * HBA firmware and issues a config_port mailbox command to configure
3836 * the SLI interface in the sli mode specified by sli_mode
3837 * variable. The caller is not required to hold any locks.
3838 * The function returns 0 if successful, else returns negative error
3839 * code.
3840 **/
James Smart93996272008-08-24 21:50:30 -04003841int
3842lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003843{
3844 LPFC_MBOXQ_t *pmb;
3845 uint32_t resetcount = 0, rc = 0, done = 0;
3846
3847 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3848 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003849 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003850 return -ENOMEM;
3851 }
3852
James Smarted957682007-06-17 19:56:37 -05003853 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003854 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003855 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003856 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003857 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003858 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003859 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003860 rc = lpfc_sli_chipset_init(phba);
3861 if (rc)
3862 break;
3863
James Smart2e0fef82007-06-17 19:56:36 -05003864 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003865 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003866 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003867 resetcount++;
3868
James Smarted957682007-06-17 19:56:37 -05003869 /* Call pre CONFIG_PORT mailbox command initialization. A
3870 * value of 0 means the call was successful. Any other
3871 * nonzero value is a failure, but if ERESTART is returned,
3872 * the driver may reset the HBA and try again.
3873 */
dea31012005-04-17 16:05:31 -05003874 rc = lpfc_config_port_prep(phba);
3875 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003876 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003877 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003878 } else if (rc)
dea31012005-04-17 16:05:31 -05003879 break;
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003881 lpfc_config_port(phba, pmb);
3882 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003883 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3884 LPFC_SLI3_HBQ_ENABLED |
3885 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003886 LPFC_SLI3_INB_ENABLED |
3887 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003888 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003890 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003891 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003892 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003893 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003894 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003895 spin_unlock_irq(&phba->hbalock);
3896 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003897 } else {
3898 /* Allow asynchronous mailbox command to go through */
3899 spin_lock_irq(&phba->hbalock);
3900 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3901 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003902 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003903 }
dea31012005-04-17 16:05:31 -05003904 }
James Smarted957682007-06-17 19:56:37 -05003905 if (!done) {
3906 rc = -EINVAL;
3907 goto do_prep_failed;
3908 }
James Smart04c68492009-05-22 14:52:52 -04003909 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3910 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003911 rc = -ENXIO;
3912 goto do_prep_failed;
3913 }
James Smart04c68492009-05-22 14:52:52 -04003914 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003915 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003916 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3917 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3918 phba->max_vpi : phba->max_vports;
3919
James Smart34b02dc2008-08-24 21:49:55 -04003920 } else
3921 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003922 if (pmb->u.mb.un.varCfgPort.gdss)
3923 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3924 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003925 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003926 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003927 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003928 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003929 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003930 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003931 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3932 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3933 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3934 phba->inb_last_counter =
3935 phba->mbox->us.s3_inb_pgp.counter;
3936 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003937 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003938 phba->port_gp = phba->mbox->us.s3_pgp.port;
3939 phba->inb_ha_copy = NULL;
3940 phba->inb_counter = NULL;
3941 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003942
3943 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003944 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003945 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3946 else
3947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3948 "0443 Adapter did not grant "
3949 "BlockGuard\n");
3950 }
James Smart34b02dc2008-08-24 21:49:55 -04003951 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003952 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003953 phba->port_gp = phba->mbox->us.s2.port;
3954 phba->inb_ha_copy = NULL;
3955 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003956 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003957 }
James Smart92d7f7b2007-06-17 19:56:38 -05003958do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003959 mempool_free(pmb, phba->mbox_mem_pool);
3960 return rc;
3961}
3962
James Smarte59058c2008-08-24 21:49:00 -04003963
3964/**
James Smart3621a712009-04-06 18:47:14 -04003965 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003966 * @phba: Pointer to HBA context object.
3967 *
3968 * This function is the main SLI intialization function. This function
3969 * is called by the HBA intialization code, HBA reset code and HBA
3970 * error attention handler code. Caller is not required to hold any
3971 * locks. This function issues config_port mailbox command to configure
3972 * the SLI, setup iocb rings and HBQ rings. In the end the function
3973 * calls the config_port_post function to issue init_link mailbox
3974 * command and to start the discovery. The function will return zero
3975 * if successful, else it will return negative error code.
3976 **/
James Smarted957682007-06-17 19:56:37 -05003977int
3978lpfc_sli_hba_setup(struct lpfc_hba *phba)
3979{
3980 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003981 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003982
3983 switch (lpfc_sli_mode) {
3984 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003985 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003987 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003988 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003989 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003990 break;
3991 }
James Smarted957682007-06-17 19:56:37 -05003992 mode = 2;
3993 break;
3994 case 0:
3995 case 3:
3996 break;
3997 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003999 "1819 Unrecognized lpfc_sli_mode "
4000 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004001
4002 break;
4003 }
4004
James Smart93996272008-08-24 21:50:30 -04004005 rc = lpfc_sli_config_port(phba, mode);
4006
James Smarted957682007-06-17 19:56:37 -05004007 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004009 "1820 Unable to select SLI-3. "
4010 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004011 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004012 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004013 if (rc)
dea31012005-04-17 16:05:31 -05004014 goto lpfc_sli_hba_setup_error;
4015
James Smart0d878412009-10-02 15:16:56 -04004016 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4017 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4018 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4019 if (!rc) {
4020 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4021 "2709 This device supports "
4022 "Advanced Error Reporting (AER)\n");
4023 spin_lock_irq(&phba->hbalock);
4024 phba->hba_flag |= HBA_AER_ENABLED;
4025 spin_unlock_irq(&phba->hbalock);
4026 } else {
4027 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4028 "2708 This device does not support "
4029 "Advanced Error Reporting (AER)\n");
4030 phba->cfg_aer_support = 0;
4031 }
4032 }
4033
James Smarted957682007-06-17 19:56:37 -05004034 if (phba->sli_rev == 3) {
4035 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4036 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004037 } else {
4038 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4039 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004040 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004041 }
4042
4043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004044 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4045 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004046 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004047
4048 if (rc)
4049 goto lpfc_sli_hba_setup_error;
4050
James Smart93996272008-08-24 21:50:30 -04004051 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004052 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4053 rc = lpfc_sli_hbq_setup(phba);
4054 if (rc)
4055 goto lpfc_sli_hba_setup_error;
4056 }
James Smart04c68492009-05-22 14:52:52 -04004057 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004058 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004059 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004060
4061 rc = lpfc_config_port_post(phba);
4062 if (rc)
4063 goto lpfc_sli_hba_setup_error;
4064
James Smarted957682007-06-17 19:56:37 -05004065 return rc;
4066
James Smart92d7f7b2007-06-17 19:56:38 -05004067lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004068 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004070 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004071 return rc;
4072}
4073
James Smartda0436e2009-05-22 14:51:39 -04004074/**
4075 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4076 * @phba: Pointer to HBA context object.
4077 * @mboxq: mailbox pointer.
4078 * This function issue a dump mailbox command to read config region
4079 * 23 and parse the records in the region and populate driver
4080 * data structure.
4081 **/
4082static int
4083lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4084 LPFC_MBOXQ_t *mboxq)
4085{
4086 struct lpfc_dmabuf *mp;
4087 struct lpfc_mqe *mqe;
4088 uint32_t data_length;
4089 int rc;
4090
4091 /* Program the default value of vlan_id and fc_map */
4092 phba->valid_vlan = 0;
4093 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4094 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4095 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4096
4097 mqe = &mboxq->u.mqe;
4098 if (lpfc_dump_fcoe_param(phba, mboxq))
4099 return -ENOMEM;
4100
4101 mp = (struct lpfc_dmabuf *) mboxq->context1;
4102 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4103
4104 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4105 "(%d):2571 Mailbox cmd x%x Status x%x "
4106 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4108 "CQ: x%x x%x x%x x%x\n",
4109 mboxq->vport ? mboxq->vport->vpi : 0,
4110 bf_get(lpfc_mqe_command, mqe),
4111 bf_get(lpfc_mqe_status, mqe),
4112 mqe->un.mb_words[0], mqe->un.mb_words[1],
4113 mqe->un.mb_words[2], mqe->un.mb_words[3],
4114 mqe->un.mb_words[4], mqe->un.mb_words[5],
4115 mqe->un.mb_words[6], mqe->un.mb_words[7],
4116 mqe->un.mb_words[8], mqe->un.mb_words[9],
4117 mqe->un.mb_words[10], mqe->un.mb_words[11],
4118 mqe->un.mb_words[12], mqe->un.mb_words[13],
4119 mqe->un.mb_words[14], mqe->un.mb_words[15],
4120 mqe->un.mb_words[16], mqe->un.mb_words[50],
4121 mboxq->mcqe.word0,
4122 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4123 mboxq->mcqe.trailer);
4124
4125 if (rc) {
4126 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4127 kfree(mp);
4128 return -EIO;
4129 }
4130 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004131 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4133 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004134 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004135 }
James Smartda0436e2009-05-22 14:51:39 -04004136
4137 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4138 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4139 kfree(mp);
4140 return 0;
4141}
4142
4143/**
4144 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4145 * @phba: pointer to lpfc hba data structure.
4146 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4147 * @vpd: pointer to the memory to hold resulting port vpd data.
4148 * @vpd_size: On input, the number of bytes allocated to @vpd.
4149 * On output, the number of data bytes in @vpd.
4150 *
4151 * This routine executes a READ_REV SLI4 mailbox command. In
4152 * addition, this routine gets the port vpd data.
4153 *
4154 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004155 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004156 * ENOMEM - could not allocated memory.
4157 **/
4158static int
4159lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4160 uint8_t *vpd, uint32_t *vpd_size)
4161{
4162 int rc = 0;
4163 uint32_t dma_size;
4164 struct lpfc_dmabuf *dmabuf;
4165 struct lpfc_mqe *mqe;
4166
4167 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4168 if (!dmabuf)
4169 return -ENOMEM;
4170
4171 /*
4172 * Get a DMA buffer for the vpd data resulting from the READ_REV
4173 * mailbox command.
4174 */
4175 dma_size = *vpd_size;
4176 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4177 dma_size,
4178 &dmabuf->phys,
4179 GFP_KERNEL);
4180 if (!dmabuf->virt) {
4181 kfree(dmabuf);
4182 return -ENOMEM;
4183 }
4184 memset(dmabuf->virt, 0, dma_size);
4185
4186 /*
4187 * The SLI4 implementation of READ_REV conflicts at word1,
4188 * bits 31:16 and SLI4 adds vpd functionality not present
4189 * in SLI3. This code corrects the conflicts.
4190 */
4191 lpfc_read_rev(phba, mboxq);
4192 mqe = &mboxq->u.mqe;
4193 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4194 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4195 mqe->un.read_rev.word1 &= 0x0000FFFF;
4196 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4197 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4198
4199 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4200 if (rc) {
4201 dma_free_coherent(&phba->pcidev->dev, dma_size,
4202 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004203 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004204 return -EIO;
4205 }
4206
James Smartda0436e2009-05-22 14:51:39 -04004207 /*
4208 * The available vpd length cannot be bigger than the
4209 * DMA buffer passed to the port. Catch the less than
4210 * case and update the caller's size.
4211 */
4212 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4213 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4214
4215 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4216 dma_free_coherent(&phba->pcidev->dev, dma_size,
4217 dmabuf->virt, dmabuf->phys);
4218 kfree(dmabuf);
4219 return 0;
4220}
4221
4222/**
4223 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4224 * @phba: pointer to lpfc hba data structure.
4225 *
4226 * This routine is called to explicitly arm the SLI4 device's completion and
4227 * event queues
4228 **/
4229static void
4230lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4231{
4232 uint8_t fcp_eqidx;
4233
4234 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4235 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004236 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4237 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4238 LPFC_QUEUE_REARM);
4239 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4240 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4241 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4242 LPFC_QUEUE_REARM);
4243}
4244
4245/**
4246 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4247 * @phba: Pointer to HBA context object.
4248 *
4249 * This function is the main SLI4 device intialization PCI function. This
4250 * function is called by the HBA intialization code, HBA reset code and
4251 * HBA error attention handler code. Caller is not required to hold any
4252 * locks.
4253 **/
4254int
4255lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4256{
4257 int rc;
4258 LPFC_MBOXQ_t *mboxq;
4259 struct lpfc_mqe *mqe;
4260 uint8_t *vpd;
4261 uint32_t vpd_size;
4262 uint32_t ftr_rsp = 0;
4263 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4264 struct lpfc_vport *vport = phba->pport;
4265 struct lpfc_dmabuf *mp;
4266
4267 /* Perform a PCI function reset to start from clean */
4268 rc = lpfc_pci_function_reset(phba);
4269 if (unlikely(rc))
4270 return -ENODEV;
4271
4272 /* Check the HBA Host Status Register for readyness */
4273 rc = lpfc_sli4_post_status_check(phba);
4274 if (unlikely(rc))
4275 return -ENODEV;
4276 else {
4277 spin_lock_irq(&phba->hbalock);
4278 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4279 spin_unlock_irq(&phba->hbalock);
4280 }
4281
4282 /*
4283 * Allocate a single mailbox container for initializing the
4284 * port.
4285 */
4286 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4287 if (!mboxq)
4288 return -ENOMEM;
4289
4290 /*
4291 * Continue initialization with default values even if driver failed
4292 * to read FCoE param config regions
4293 */
4294 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004296 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004297
4298 /* Issue READ_REV to collect vpd and FW information. */
4299 vpd_size = PAGE_SIZE;
4300 vpd = kzalloc(vpd_size, GFP_KERNEL);
4301 if (!vpd) {
4302 rc = -ENOMEM;
4303 goto out_free_mbox;
4304 }
4305
4306 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4307 if (unlikely(rc))
4308 goto out_free_vpd;
4309
4310 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004311 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4312 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4313 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004314
4315 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4316 LPFC_DCBX_CEE_MODE)
4317 phba->hba_flag |= HBA_FIP_SUPPORT;
4318 else
4319 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4320
James Smartf1126682009-06-10 17:22:44 -04004321 if (phba->sli_rev != LPFC_SLI_REV4 ||
4322 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4324 "0376 READ_REV Error. SLI Level %d "
4325 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004326 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004327 rc = -EIO;
4328 goto out_free_vpd;
4329 }
James Smartda0436e2009-05-22 14:51:39 -04004330 /*
4331 * Evaluate the read rev and vpd data. Populate the driver
4332 * state with the results. If this routine fails, the failure
4333 * is not fatal as the driver will use generic values.
4334 */
4335 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4336 if (unlikely(!rc)) {
4337 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4338 "0377 Error %d parsing vpd. "
4339 "Using defaults.\n", rc);
4340 rc = 0;
4341 }
4342
James Smartf1126682009-06-10 17:22:44 -04004343 /* Save information as VPD data */
4344 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4345 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4346 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4347 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4348 &mqe->un.read_rev);
4349 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4350 &mqe->un.read_rev);
4351 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4352 &mqe->un.read_rev);
4353 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4354 &mqe->un.read_rev);
4355 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4356 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4357 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4358 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4359 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4360 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4361 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4362 "(%d):0380 READ_REV Status x%x "
4363 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4364 mboxq->vport ? mboxq->vport->vpi : 0,
4365 bf_get(lpfc_mqe_status, mqe),
4366 phba->vpd.rev.opFwName,
4367 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4368 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004369
4370 /*
4371 * Discover the port's supported feature set and match it against the
4372 * hosts requests.
4373 */
4374 lpfc_request_features(phba, mboxq);
4375 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4376 if (unlikely(rc)) {
4377 rc = -EIO;
4378 goto out_free_vpd;
4379 }
4380
4381 /*
4382 * The port must support FCP initiator mode as this is the
4383 * only mode running in the host.
4384 */
4385 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4386 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4387 "0378 No support for fcpi mode.\n");
4388 ftr_rsp++;
4389 }
4390
4391 /*
4392 * If the port cannot support the host's requested features
4393 * then turn off the global config parameters to disable the
4394 * feature in the driver. This is not a fatal error.
4395 */
4396 if ((phba->cfg_enable_bg) &&
4397 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4398 ftr_rsp++;
4399
4400 if (phba->max_vpi && phba->cfg_enable_npiv &&
4401 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4402 ftr_rsp++;
4403
4404 if (ftr_rsp) {
4405 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4406 "0379 Feature Mismatch Data: x%08x %08x "
4407 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4408 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4409 phba->cfg_enable_npiv, phba->max_vpi);
4410 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4411 phba->cfg_enable_bg = 0;
4412 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4413 phba->cfg_enable_npiv = 0;
4414 }
4415
4416 /* These SLI3 features are assumed in SLI4 */
4417 spin_lock_irq(&phba->hbalock);
4418 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4419 spin_unlock_irq(&phba->hbalock);
4420
4421 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004422 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4423 if (rc) {
4424 phba->link_state = LPFC_HBA_ERROR;
4425 rc = -ENOMEM;
4426 goto out_free_vpd;
4427 }
4428
James Smartda0436e2009-05-22 14:51:39 -04004429 mboxq->vport = vport;
4430 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4431 mp = (struct lpfc_dmabuf *) mboxq->context1;
4432 if (rc == MBX_SUCCESS) {
4433 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4434 rc = 0;
4435 }
4436
4437 /*
4438 * This memory was allocated by the lpfc_read_sparam routine. Release
4439 * it to the mbuf pool.
4440 */
4441 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4442 kfree(mp);
4443 mboxq->context1 = NULL;
4444 if (unlikely(rc)) {
4445 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4446 "0382 READ_SPARAM command failed "
4447 "status %d, mbxStatus x%x\n",
4448 rc, bf_get(lpfc_mqe_status, mqe));
4449 phba->link_state = LPFC_HBA_ERROR;
4450 rc = -EIO;
4451 goto out_free_vpd;
4452 }
4453
4454 if (phba->cfg_soft_wwnn)
4455 u64_to_wwn(phba->cfg_soft_wwnn,
4456 vport->fc_sparam.nodeName.u.wwn);
4457 if (phba->cfg_soft_wwpn)
4458 u64_to_wwn(phba->cfg_soft_wwpn,
4459 vport->fc_sparam.portName.u.wwn);
4460 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4461 sizeof(struct lpfc_name));
4462 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4463 sizeof(struct lpfc_name));
4464
4465 /* Update the fc_host data structures with new wwn. */
4466 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4467 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4468
4469 /* Register SGL pool to the device using non-embedded mailbox command */
4470 rc = lpfc_sli4_post_sgl_list(phba);
4471 if (unlikely(rc)) {
4472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004473 "0582 Error %d during sgl post operation\n",
4474 rc);
James Smartda0436e2009-05-22 14:51:39 -04004475 rc = -ENODEV;
4476 goto out_free_vpd;
4477 }
4478
4479 /* Register SCSI SGL pool to the device */
4480 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4481 if (unlikely(rc)) {
4482 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004483 "0383 Error %d during scsi sgl post "
4484 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004485 /* Some Scsi buffers were moved to the abort scsi list */
4486 /* A pci function reset will repost them */
4487 rc = -ENODEV;
4488 goto out_free_vpd;
4489 }
4490
4491 /* Post the rpi header region to the device. */
4492 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4493 if (unlikely(rc)) {
4494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4495 "0393 Error %d during rpi post operation\n",
4496 rc);
4497 rc = -ENODEV;
4498 goto out_free_vpd;
4499 }
James Smartda0436e2009-05-22 14:51:39 -04004500
4501 /* Set up all the queues to the device */
4502 rc = lpfc_sli4_queue_setup(phba);
4503 if (unlikely(rc)) {
4504 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4505 "0381 Error %d during queue setup.\n ", rc);
4506 goto out_stop_timers;
4507 }
4508
4509 /* Arm the CQs and then EQs on device */
4510 lpfc_sli4_arm_cqeq_intr(phba);
4511
4512 /* Indicate device interrupt mode */
4513 phba->sli4_hba.intr_enable = 1;
4514
4515 /* Allow asynchronous mailbox command to go through */
4516 spin_lock_irq(&phba->hbalock);
4517 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4518 spin_unlock_irq(&phba->hbalock);
4519
4520 /* Post receive buffers to the device */
4521 lpfc_sli4_rb_setup(phba);
4522
James Smartfc2b9892010-02-26 14:15:29 -05004523 /* Reset HBA FCF states after HBA reset */
4524 phba->fcf.fcf_flag = 0;
4525 phba->fcf.current_rec.flag = 0;
4526
James Smartda0436e2009-05-22 14:51:39 -04004527 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004528 mod_timer(&vport->els_tmofunc,
4529 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004530
4531 /* Start heart beat timer */
4532 mod_timer(&phba->hb_tmofunc,
4533 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4534 phba->hb_outstanding = 0;
4535 phba->last_completion_time = jiffies;
4536
4537 /* Start error attention (ERATT) polling timer */
4538 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4539
4540 /*
4541 * The port is ready, set the host's link state to LINK_DOWN
4542 * in preparation for link interrupts.
4543 */
4544 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4545 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4546 lpfc_set_loopback_flag(phba);
4547 /* Change driver state to LPFC_LINK_DOWN right before init link */
4548 spin_lock_irq(&phba->hbalock);
4549 phba->link_state = LPFC_LINK_DOWN;
4550 spin_unlock_irq(&phba->hbalock);
4551 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4552 if (unlikely(rc != MBX_NOT_FINISHED)) {
4553 kfree(vpd);
4554 return 0;
4555 } else
4556 rc = -EIO;
4557
4558 /* Unset all the queues set up in this routine when error out */
4559 if (rc)
4560 lpfc_sli4_queue_unset(phba);
4561
4562out_stop_timers:
4563 if (rc)
4564 lpfc_stop_hba_timers(phba);
4565out_free_vpd:
4566 kfree(vpd);
4567out_free_mbox:
4568 mempool_free(mboxq, phba->mbox_mem_pool);
4569 return rc;
4570}
James Smarte59058c2008-08-24 21:49:00 -04004571
4572/**
James Smart3621a712009-04-06 18:47:14 -04004573 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004574 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004575 *
James Smarte59058c2008-08-24 21:49:00 -04004576 * This is the callback function for mailbox timer. The mailbox
4577 * timer is armed when a new mailbox command is issued and the timer
4578 * is deleted when the mailbox complete. The function is called by
4579 * the kernel timer code when a mailbox does not complete within
4580 * expected time. This function wakes up the worker thread to
4581 * process the mailbox timeout and returns. All the processing is
4582 * done by the worker thread function lpfc_mbox_timeout_handler.
4583 **/
dea31012005-04-17 16:05:31 -05004584void
4585lpfc_mbox_timeout(unsigned long ptr)
4586{
James Smart92d7f7b2007-06-17 19:56:38 -05004587 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004588 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004589 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004590
James Smart2e0fef82007-06-17 19:56:36 -05004591 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004592 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004593 if (!tmo_posted)
4594 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4595 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4596
James Smart5e9d9b82008-06-14 22:52:53 -04004597 if (!tmo_posted)
4598 lpfc_worker_wake_up(phba);
4599 return;
dea31012005-04-17 16:05:31 -05004600}
4601
James Smarte59058c2008-08-24 21:49:00 -04004602
4603/**
James Smart3621a712009-04-06 18:47:14 -04004604 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004605 * @phba: Pointer to HBA context object.
4606 *
4607 * This function is called from worker thread when a mailbox command times out.
4608 * The caller is not required to hold any locks. This function will reset the
4609 * HBA and recover all the pending commands.
4610 **/
dea31012005-04-17 16:05:31 -05004611void
4612lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4613{
James Smart2e0fef82007-06-17 19:56:36 -05004614 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004615 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004616 struct lpfc_sli *psli = &phba->sli;
4617 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004618
James Smarta257bf92009-04-06 18:48:10 -04004619 /* Check the pmbox pointer first. There is a race condition
4620 * between the mbox timeout handler getting executed in the
4621 * worklist and the mailbox actually completing. When this
4622 * race condition occurs, the mbox_active will be NULL.
4623 */
4624 spin_lock_irq(&phba->hbalock);
4625 if (pmbox == NULL) {
4626 lpfc_printf_log(phba, KERN_WARNING,
4627 LOG_MBOX | LOG_SLI,
4628 "0353 Active Mailbox cleared - mailbox timeout "
4629 "exiting\n");
4630 spin_unlock_irq(&phba->hbalock);
4631 return;
4632 }
4633
dea31012005-04-17 16:05:31 -05004634 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004635 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004636 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004637 mb->mbxCommand,
4638 phba->pport->port_state,
4639 phba->sli.sli_flag,
4640 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004641 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004642
James Smart1dcb58e2007-04-25 09:51:30 -04004643 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4644 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4645 * it to fail all oustanding SCSI IO.
4646 */
James Smart2e0fef82007-06-17 19:56:36 -05004647 spin_lock_irq(&phba->pport->work_port_lock);
4648 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4649 spin_unlock_irq(&phba->pport->work_port_lock);
4650 spin_lock_irq(&phba->hbalock);
4651 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004652 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004653 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004654
4655 pring = &psli->ring[psli->fcp_ring];
4656 lpfc_sli_abort_iocb_ring(phba, pring);
4657
4658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004659 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004660
4661 /* Reset the HBA device */
4662 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004663}
4664
James Smarte59058c2008-08-24 21:49:00 -04004665/**
James Smart3772a992009-05-22 14:50:54 -04004666 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004667 * @phba: Pointer to HBA context object.
4668 * @pmbox: Pointer to mailbox object.
4669 * @flag: Flag indicating how the mailbox need to be processed.
4670 *
4671 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004672 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4673 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004674 * The mailbox command can be submitted in polling mode, in which case
4675 * this function will wait in a polling loop for the completion of the
4676 * mailbox.
4677 * If the mailbox is submitted in no_wait mode (not polling) the
4678 * function will submit the command and returns immediately without waiting
4679 * for the mailbox completion. The no_wait is supported only when HBA
4680 * is in SLI2/SLI3 mode - interrupts are enabled.
4681 * The SLI interface allows only one mailbox pending at a time. If the
4682 * mailbox is issued in polling mode and there is already a mailbox
4683 * pending, then the function will return an error. If the mailbox is issued
4684 * in NO_WAIT mode and there is a mailbox pending already, the function
4685 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4686 * The sli layer owns the mailbox object until the completion of mailbox
4687 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4688 * return codes the caller owns the mailbox command after the return of
4689 * the function.
4690 **/
James Smart3772a992009-05-22 14:50:54 -04004691static int
4692lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4693 uint32_t flag)
dea31012005-04-17 16:05:31 -05004694{
dea31012005-04-17 16:05:31 -05004695 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004696 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004697 uint32_t status, evtctr;
4698 uint32_t ha_copy;
4699 int i;
James Smart09372822008-01-11 01:52:54 -05004700 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004701 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004702 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004703 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004704 int processing_queue = 0;
4705
4706 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4707 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004708 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004709 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004710 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4711 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4712 return MBX_SUCCESS;
4713 }
James Smart58da1ff2008-04-07 10:15:56 -04004714 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004715 pmbox = lpfc_mbox_get(phba);
4716 if (!pmbox) {
4717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4718 return MBX_SUCCESS;
4719 }
4720 }
dea31012005-04-17 16:05:31 -05004721
James Smarted957682007-06-17 19:56:37 -05004722 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004723 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004724 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004726 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004727 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004728 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004729 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004730 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004731 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004732 }
4733 }
4734
Linas Vepstas8d63f372007-02-14 14:28:36 -06004735 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004736 if (unlikely(pci_channel_offline(phba->pcidev))) {
4737 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4738 goto out_not_finished;
4739 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004740
James Smarta257bf92009-04-06 18:48:10 -04004741 /* If HBA has a deferred error attention, fail the iocb. */
4742 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4743 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4744 goto out_not_finished;
4745 }
4746
dea31012005-04-17 16:05:31 -05004747 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004748
James Smart3772a992009-05-22 14:50:54 -04004749 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004750 status = MBX_SUCCESS;
4751
James Smart2e0fef82007-06-17 19:56:36 -05004752 if (phba->link_state == LPFC_HBA_ERROR) {
4753 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004754
4755 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4757 "(%d):0311 Mailbox command x%x cannot "
4758 "issue Data: x%x x%x\n",
4759 pmbox->vport ? pmbox->vport->vpi : 0,
4760 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004761 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004762 }
4763
James Smart92908312006-03-07 15:04:13 -05004764 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4765 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004766 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004767 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4768 "(%d):2528 Mailbox command x%x cannot "
4769 "issue Data: x%x x%x\n",
4770 pmbox->vport ? pmbox->vport->vpi : 0,
4771 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004772 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004773 }
4774
dea31012005-04-17 16:05:31 -05004775 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4776 /* Polling for a mbox command when another one is already active
4777 * is not allowed in SLI. Also, the driver must have established
4778 * SLI2 mode to queue and process multiple mbox commands.
4779 */
4780
4781 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004782 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004783
4784 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4786 "(%d):2529 Mailbox command x%x "
4787 "cannot issue Data: x%x x%x\n",
4788 pmbox->vport ? pmbox->vport->vpi : 0,
4789 pmbox->u.mb.mbxCommand,
4790 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004791 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004792 }
4793
James Smart3772a992009-05-22 14:50:54 -04004794 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004795 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004796 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004797 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4798 "(%d):2530 Mailbox command x%x "
4799 "cannot issue Data: x%x x%x\n",
4800 pmbox->vport ? pmbox->vport->vpi : 0,
4801 pmbox->u.mb.mbxCommand,
4802 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004803 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004804 }
4805
dea31012005-04-17 16:05:31 -05004806 /* Another mailbox command is still being processed, queue this
4807 * command to be processed later.
4808 */
4809 lpfc_mbox_put(phba, pmbox);
4810
4811 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004812 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004813 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004814 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004815 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4816 mb->mbxCommand, phba->pport->port_state,
4817 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004818
4819 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004820 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004821
James Smart858c9f62007-06-17 19:56:39 -05004822 if (pmbox->vport) {
4823 lpfc_debugfs_disc_trc(pmbox->vport,
4824 LPFC_DISC_TRC_MBOX_VPORT,
4825 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4826 (uint32_t)mb->mbxCommand,
4827 mb->un.varWords[0], mb->un.varWords[1]);
4828 }
4829 else {
4830 lpfc_debugfs_disc_trc(phba->pport,
4831 LPFC_DISC_TRC_MBOX,
4832 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4833 (uint32_t)mb->mbxCommand,
4834 mb->un.varWords[0], mb->un.varWords[1]);
4835 }
4836
James Smart2e0fef82007-06-17 19:56:36 -05004837 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004838 }
4839
dea31012005-04-17 16:05:31 -05004840 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4841
4842 /* If we are not polling, we MUST be in SLI2 mode */
4843 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004844 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004845 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004846 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004847 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004848 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004849 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4850 "(%d):2531 Mailbox command x%x "
4851 "cannot issue Data: x%x x%x\n",
4852 pmbox->vport ? pmbox->vport->vpi : 0,
4853 pmbox->u.mb.mbxCommand,
4854 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004855 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004856 }
4857 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004858 mod_timer(&psli->mbox_tmo, (jiffies +
4859 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004860 }
4861
4862 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004863 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004864 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004865 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004866 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004867 mb->mbxCommand, phba->pport->port_state,
4868 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004869
James Smart858c9f62007-06-17 19:56:39 -05004870 if (mb->mbxCommand != MBX_HEARTBEAT) {
4871 if (pmbox->vport) {
4872 lpfc_debugfs_disc_trc(pmbox->vport,
4873 LPFC_DISC_TRC_MBOX_VPORT,
4874 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4875 (uint32_t)mb->mbxCommand,
4876 mb->un.varWords[0], mb->un.varWords[1]);
4877 }
4878 else {
4879 lpfc_debugfs_disc_trc(phba->pport,
4880 LPFC_DISC_TRC_MBOX,
4881 "MBOX Send: cmd:x%x mb:x%x x%x",
4882 (uint32_t)mb->mbxCommand,
4883 mb->un.varWords[0], mb->un.varWords[1]);
4884 }
4885 }
4886
dea31012005-04-17 16:05:31 -05004887 psli->slistat.mbox_cmd++;
4888 evtctr = psli->slistat.mbox_event;
4889
4890 /* next set own bit for the adapter and copy over command word */
4891 mb->mbxOwner = OWN_CHIP;
4892
James Smart3772a992009-05-22 14:50:54 -04004893 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004894 /* Populate mbox extension offset word. */
4895 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4896 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4897 = (uint8_t *)phba->mbox_ext
4898 - (uint8_t *)phba->mbox;
4899 }
4900
4901 /* Copy the mailbox extension data */
4902 if (pmbox->in_ext_byte_len && pmbox->context2) {
4903 lpfc_sli_pcimem_bcopy(pmbox->context2,
4904 (uint8_t *)phba->mbox_ext,
4905 pmbox->in_ext_byte_len);
4906 }
4907 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004908 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004909 } else {
James Smart7a470272010-03-15 11:25:20 -04004910 /* Populate mbox extension offset word. */
4911 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4912 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4913 = MAILBOX_HBA_EXT_OFFSET;
4914
4915 /* Copy the mailbox extension data */
4916 if (pmbox->in_ext_byte_len && pmbox->context2) {
4917 lpfc_memcpy_to_slim(phba->MBslimaddr +
4918 MAILBOX_HBA_EXT_OFFSET,
4919 pmbox->context2, pmbox->in_ext_byte_len);
4920
4921 }
James Smart92908312006-03-07 15:04:13 -05004922 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004923 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004924 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004925 }
4926
4927 /* First copy mbox command data to HBA SLIM, skip past first
4928 word */
4929 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4930 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4931 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4932
4933 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004934 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004935 to_slim = phba->MBslimaddr;
4936 writel(ldata, to_slim);
4937 readl(to_slim); /* flush */
4938
4939 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4940 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004941 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004942 }
4943 }
4944
4945 wmb();
dea31012005-04-17 16:05:31 -05004946
4947 switch (flag) {
4948 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004949 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004950 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004951 /* Interrupt board to do it */
4952 writel(CA_MBATT, phba->CAregaddr);
4953 readl(phba->CAregaddr); /* flush */
4954 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004955 break;
4956
4957 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004958 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004959 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004960 /* Interrupt board to do it */
4961 writel(CA_MBATT, phba->CAregaddr);
4962 readl(phba->CAregaddr); /* flush */
4963
James Smart3772a992009-05-22 14:50:54 -04004964 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004965 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004966 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004967 word0 = le32_to_cpu(word0);
4968 } else {
4969 /* First read mbox status word */
4970 word0 = readl(phba->MBslimaddr);
4971 }
4972
4973 /* Read the HBA Host Attention Register */
4974 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004975 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4976 mb->mbxCommand) *
4977 1000) + jiffies;
4978 i = 0;
dea31012005-04-17 16:05:31 -05004979 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004980 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4981 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004982 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004983 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004984 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004985 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004986 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004987 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004988 }
4989
4990 /* Check if we took a mbox interrupt while we were
4991 polling */
4992 if (((word0 & OWN_CHIP) != OWN_CHIP)
4993 && (evtctr != psli->slistat.mbox_event))
4994 break;
4995
James Smart09372822008-01-11 01:52:54 -05004996 if (i++ > 10) {
4997 spin_unlock_irqrestore(&phba->hbalock,
4998 drvr_flag);
4999 msleep(1);
5000 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5001 }
dea31012005-04-17 16:05:31 -05005002
James Smart3772a992009-05-22 14:50:54 -04005003 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005004 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005005 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005006 word0 = le32_to_cpu(word0);
5007 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5008 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005009 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005010 /* Check real SLIM for any errors */
5011 slimword0 = readl(phba->MBslimaddr);
5012 slimmb = (MAILBOX_t *) & slimword0;
5013 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5014 && slimmb->mbxStatus) {
5015 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005016 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005017 word0 = slimword0;
5018 }
5019 }
5020 } else {
5021 /* First copy command data */
5022 word0 = readl(phba->MBslimaddr);
5023 }
5024 /* Read the HBA Host Attention Register */
5025 ha_copy = readl(phba->HAregaddr);
5026 }
5027
James Smart3772a992009-05-22 14:50:54 -04005028 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005029 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005030 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005031 /* Copy the mailbox extension data */
5032 if (pmbox->out_ext_byte_len && pmbox->context2) {
5033 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5034 pmbox->context2,
5035 pmbox->out_ext_byte_len);
5036 }
dea31012005-04-17 16:05:31 -05005037 } else {
5038 /* First copy command data */
5039 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5040 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005041 /* Copy the mailbox extension data */
5042 if (pmbox->out_ext_byte_len && pmbox->context2) {
5043 lpfc_memcpy_from_slim(pmbox->context2,
5044 phba->MBslimaddr +
5045 MAILBOX_HBA_EXT_OFFSET,
5046 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005047 }
5048 }
5049
5050 writel(HA_MBATT, phba->HAregaddr);
5051 readl(phba->HAregaddr); /* flush */
5052
5053 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5054 status = mb->mbxStatus;
5055 }
5056
James Smart2e0fef82007-06-17 19:56:36 -05005057 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5058 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005059
5060out_not_finished:
5061 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005062 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005063 lpfc_mbox_cmpl_put(phba, pmbox);
5064 }
5065 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005066}
5067
James Smarte59058c2008-08-24 21:49:00 -04005068/**
James Smartf1126682009-06-10 17:22:44 -04005069 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5070 * @phba: Pointer to HBA context object.
5071 *
5072 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5073 * the driver internal pending mailbox queue. It will then try to wait out the
5074 * possible outstanding mailbox command before return.
5075 *
5076 * Returns:
5077 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5078 * the outstanding mailbox command timed out.
5079 **/
5080static int
5081lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5082{
5083 struct lpfc_sli *psli = &phba->sli;
5084 uint8_t actcmd = MBX_HEARTBEAT;
5085 int rc = 0;
5086 unsigned long timeout;
5087
5088 /* Mark the asynchronous mailbox command posting as blocked */
5089 spin_lock_irq(&phba->hbalock);
5090 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5091 if (phba->sli.mbox_active)
5092 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5093 spin_unlock_irq(&phba->hbalock);
5094 /* Determine how long we might wait for the active mailbox
5095 * command to be gracefully completed by firmware.
5096 */
5097 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5098 jiffies;
5099 /* Wait for the outstnading mailbox command to complete */
5100 while (phba->sli.mbox_active) {
5101 /* Check active mailbox complete status every 2ms */
5102 msleep(2);
5103 if (time_after(jiffies, timeout)) {
5104 /* Timeout, marked the outstanding cmd not complete */
5105 rc = 1;
5106 break;
5107 }
5108 }
5109
5110 /* Can not cleanly block async mailbox command, fails it */
5111 if (rc) {
5112 spin_lock_irq(&phba->hbalock);
5113 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5114 spin_unlock_irq(&phba->hbalock);
5115 }
5116 return rc;
5117}
5118
5119/**
5120 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5121 * @phba: Pointer to HBA context object.
5122 *
5123 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5124 * commands from the driver internal pending mailbox queue. It makes sure
5125 * that there is no outstanding mailbox command before resuming posting
5126 * asynchronous mailbox commands. If, for any reason, there is outstanding
5127 * mailbox command, it will try to wait it out before resuming asynchronous
5128 * mailbox command posting.
5129 **/
5130static void
5131lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5132{
5133 struct lpfc_sli *psli = &phba->sli;
5134
5135 spin_lock_irq(&phba->hbalock);
5136 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5137 /* Asynchronous mailbox posting is not blocked, do nothing */
5138 spin_unlock_irq(&phba->hbalock);
5139 return;
5140 }
5141
5142 /* Outstanding synchronous mailbox command is guaranteed to be done,
5143 * successful or timeout, after timing-out the outstanding mailbox
5144 * command shall always be removed, so just unblock posting async
5145 * mailbox command and resume
5146 */
5147 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5148 spin_unlock_irq(&phba->hbalock);
5149
5150 /* wake up worker thread to post asynchronlous mailbox command */
5151 lpfc_worker_wake_up(phba);
5152}
5153
5154/**
James Smartda0436e2009-05-22 14:51:39 -04005155 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5156 * @phba: Pointer to HBA context object.
5157 * @mboxq: Pointer to mailbox object.
5158 *
5159 * The function posts a mailbox to the port. The mailbox is expected
5160 * to be comletely filled in and ready for the port to operate on it.
5161 * This routine executes a synchronous completion operation on the
5162 * mailbox by polling for its completion.
5163 *
5164 * The caller must not be holding any locks when calling this routine.
5165 *
5166 * Returns:
5167 * MBX_SUCCESS - mailbox posted successfully
5168 * Any of the MBX error values.
5169 **/
5170static int
5171lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5172{
5173 int rc = MBX_SUCCESS;
5174 unsigned long iflag;
5175 uint32_t db_ready;
5176 uint32_t mcqe_status;
5177 uint32_t mbx_cmnd;
5178 unsigned long timeout;
5179 struct lpfc_sli *psli = &phba->sli;
5180 struct lpfc_mqe *mb = &mboxq->u.mqe;
5181 struct lpfc_bmbx_create *mbox_rgn;
5182 struct dma_address *dma_address;
5183 struct lpfc_register bmbx_reg;
5184
5185 /*
5186 * Only one mailbox can be active to the bootstrap mailbox region
5187 * at a time and there is no queueing provided.
5188 */
5189 spin_lock_irqsave(&phba->hbalock, iflag);
5190 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5191 spin_unlock_irqrestore(&phba->hbalock, iflag);
5192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5193 "(%d):2532 Mailbox command x%x (x%x) "
5194 "cannot issue Data: x%x x%x\n",
5195 mboxq->vport ? mboxq->vport->vpi : 0,
5196 mboxq->u.mb.mbxCommand,
5197 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5198 psli->sli_flag, MBX_POLL);
5199 return MBXERR_ERROR;
5200 }
5201 /* The server grabs the token and owns it until release */
5202 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5203 phba->sli.mbox_active = mboxq;
5204 spin_unlock_irqrestore(&phba->hbalock, iflag);
5205
5206 /*
5207 * Initialize the bootstrap memory region to avoid stale data areas
5208 * in the mailbox post. Then copy the caller's mailbox contents to
5209 * the bmbx mailbox region.
5210 */
5211 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5212 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5213 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5214 sizeof(struct lpfc_mqe));
5215
5216 /* Post the high mailbox dma address to the port and wait for ready. */
5217 dma_address = &phba->sli4_hba.bmbx.dma_address;
5218 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5219
5220 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5221 * 1000) + jiffies;
5222 do {
5223 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5224 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5225 if (!db_ready)
5226 msleep(2);
5227
5228 if (time_after(jiffies, timeout)) {
5229 rc = MBXERR_ERROR;
5230 goto exit;
5231 }
5232 } while (!db_ready);
5233
5234 /* Post the low mailbox dma address to the port. */
5235 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5236 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5237 * 1000) + jiffies;
5238 do {
5239 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5240 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5241 if (!db_ready)
5242 msleep(2);
5243
5244 if (time_after(jiffies, timeout)) {
5245 rc = MBXERR_ERROR;
5246 goto exit;
5247 }
5248 } while (!db_ready);
5249
5250 /*
5251 * Read the CQ to ensure the mailbox has completed.
5252 * If so, update the mailbox status so that the upper layers
5253 * can complete the request normally.
5254 */
5255 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5256 sizeof(struct lpfc_mqe));
5257 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5258 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5259 sizeof(struct lpfc_mcqe));
5260 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5261
5262 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5263 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5264 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5265 rc = MBXERR_ERROR;
5266 }
5267
5268 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5269 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5270 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5271 " x%x x%x CQ: x%x x%x x%x x%x\n",
5272 mboxq->vport ? mboxq->vport->vpi : 0,
5273 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5274 bf_get(lpfc_mqe_status, mb),
5275 mb->un.mb_words[0], mb->un.mb_words[1],
5276 mb->un.mb_words[2], mb->un.mb_words[3],
5277 mb->un.mb_words[4], mb->un.mb_words[5],
5278 mb->un.mb_words[6], mb->un.mb_words[7],
5279 mb->un.mb_words[8], mb->un.mb_words[9],
5280 mb->un.mb_words[10], mb->un.mb_words[11],
5281 mb->un.mb_words[12], mboxq->mcqe.word0,
5282 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5283 mboxq->mcqe.trailer);
5284exit:
5285 /* We are holding the token, no needed for lock when release */
5286 spin_lock_irqsave(&phba->hbalock, iflag);
5287 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5288 phba->sli.mbox_active = NULL;
5289 spin_unlock_irqrestore(&phba->hbalock, iflag);
5290 return rc;
5291}
5292
5293/**
5294 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5295 * @phba: Pointer to HBA context object.
5296 * @pmbox: Pointer to mailbox object.
5297 * @flag: Flag indicating how the mailbox need to be processed.
5298 *
5299 * This function is called by discovery code and HBA management code to submit
5300 * a mailbox command to firmware with SLI-4 interface spec.
5301 *
5302 * Return codes the caller owns the mailbox command after the return of the
5303 * function.
5304 **/
5305static int
5306lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5307 uint32_t flag)
5308{
5309 struct lpfc_sli *psli = &phba->sli;
5310 unsigned long iflags;
5311 int rc;
5312
James Smart8fa38512009-07-19 10:01:03 -04005313 rc = lpfc_mbox_dev_check(phba);
5314 if (unlikely(rc)) {
5315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5316 "(%d):2544 Mailbox command x%x (x%x) "
5317 "cannot issue Data: x%x x%x\n",
5318 mboxq->vport ? mboxq->vport->vpi : 0,
5319 mboxq->u.mb.mbxCommand,
5320 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5321 psli->sli_flag, flag);
5322 goto out_not_finished;
5323 }
5324
James Smartda0436e2009-05-22 14:51:39 -04005325 /* Detect polling mode and jump to a handler */
5326 if (!phba->sli4_hba.intr_enable) {
5327 if (flag == MBX_POLL)
5328 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5329 else
5330 rc = -EIO;
5331 if (rc != MBX_SUCCESS)
5332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5333 "(%d):2541 Mailbox command x%x "
5334 "(x%x) cannot issue Data: x%x x%x\n",
5335 mboxq->vport ? mboxq->vport->vpi : 0,
5336 mboxq->u.mb.mbxCommand,
5337 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5338 psli->sli_flag, flag);
5339 return rc;
5340 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005341 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5342 "(%d):2542 Try to issue mailbox command "
5343 "x%x (x%x) synchronously ahead of async"
5344 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005345 mboxq->vport ? mboxq->vport->vpi : 0,
5346 mboxq->u.mb.mbxCommand,
5347 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5348 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005349 /* Try to block the asynchronous mailbox posting */
5350 rc = lpfc_sli4_async_mbox_block(phba);
5351 if (!rc) {
5352 /* Successfully blocked, now issue sync mbox cmd */
5353 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5354 if (rc != MBX_SUCCESS)
5355 lpfc_printf_log(phba, KERN_ERR,
5356 LOG_MBOX | LOG_SLI,
5357 "(%d):2597 Mailbox command "
5358 "x%x (x%x) cannot issue "
5359 "Data: x%x x%x\n",
5360 mboxq->vport ?
5361 mboxq->vport->vpi : 0,
5362 mboxq->u.mb.mbxCommand,
5363 lpfc_sli4_mbox_opcode_get(phba,
5364 mboxq),
5365 psli->sli_flag, flag);
5366 /* Unblock the async mailbox posting afterward */
5367 lpfc_sli4_async_mbox_unblock(phba);
5368 }
5369 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005370 }
5371
5372 /* Now, interrupt mode asynchrous mailbox command */
5373 rc = lpfc_mbox_cmd_check(phba, mboxq);
5374 if (rc) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5376 "(%d):2543 Mailbox command x%x (x%x) "
5377 "cannot issue Data: x%x x%x\n",
5378 mboxq->vport ? mboxq->vport->vpi : 0,
5379 mboxq->u.mb.mbxCommand,
5380 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5381 psli->sli_flag, flag);
5382 goto out_not_finished;
5383 }
James Smartda0436e2009-05-22 14:51:39 -04005384
5385 /* Put the mailbox command to the driver internal FIFO */
5386 psli->slistat.mbox_busy++;
5387 spin_lock_irqsave(&phba->hbalock, iflags);
5388 lpfc_mbox_put(phba, mboxq);
5389 spin_unlock_irqrestore(&phba->hbalock, iflags);
5390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5391 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5392 "x%x (x%x) x%x x%x x%x\n",
5393 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5394 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5395 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5396 phba->pport->port_state,
5397 psli->sli_flag, MBX_NOWAIT);
5398 /* Wake up worker thread to transport mailbox command from head */
5399 lpfc_worker_wake_up(phba);
5400
5401 return MBX_BUSY;
5402
5403out_not_finished:
5404 return MBX_NOT_FINISHED;
5405}
5406
5407/**
5408 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5409 * @phba: Pointer to HBA context object.
5410 *
5411 * This function is called by worker thread to send a mailbox command to
5412 * SLI4 HBA firmware.
5413 *
5414 **/
5415int
5416lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5417{
5418 struct lpfc_sli *psli = &phba->sli;
5419 LPFC_MBOXQ_t *mboxq;
5420 int rc = MBX_SUCCESS;
5421 unsigned long iflags;
5422 struct lpfc_mqe *mqe;
5423 uint32_t mbx_cmnd;
5424
5425 /* Check interrupt mode before post async mailbox command */
5426 if (unlikely(!phba->sli4_hba.intr_enable))
5427 return MBX_NOT_FINISHED;
5428
5429 /* Check for mailbox command service token */
5430 spin_lock_irqsave(&phba->hbalock, iflags);
5431 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5432 spin_unlock_irqrestore(&phba->hbalock, iflags);
5433 return MBX_NOT_FINISHED;
5434 }
5435 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5436 spin_unlock_irqrestore(&phba->hbalock, iflags);
5437 return MBX_NOT_FINISHED;
5438 }
5439 if (unlikely(phba->sli.mbox_active)) {
5440 spin_unlock_irqrestore(&phba->hbalock, iflags);
5441 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5442 "0384 There is pending active mailbox cmd\n");
5443 return MBX_NOT_FINISHED;
5444 }
5445 /* Take the mailbox command service token */
5446 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5447
5448 /* Get the next mailbox command from head of queue */
5449 mboxq = lpfc_mbox_get(phba);
5450
5451 /* If no more mailbox command waiting for post, we're done */
5452 if (!mboxq) {
5453 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5454 spin_unlock_irqrestore(&phba->hbalock, iflags);
5455 return MBX_SUCCESS;
5456 }
5457 phba->sli.mbox_active = mboxq;
5458 spin_unlock_irqrestore(&phba->hbalock, iflags);
5459
5460 /* Check device readiness for posting mailbox command */
5461 rc = lpfc_mbox_dev_check(phba);
5462 if (unlikely(rc))
5463 /* Driver clean routine will clean up pending mailbox */
5464 goto out_not_finished;
5465
5466 /* Prepare the mbox command to be posted */
5467 mqe = &mboxq->u.mqe;
5468 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5469
5470 /* Start timer for the mbox_tmo and log some mailbox post messages */
5471 mod_timer(&psli->mbox_tmo, (jiffies +
5472 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5473
5474 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5475 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5476 "x%x x%x\n",
5477 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5478 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5479 phba->pport->port_state, psli->sli_flag);
5480
5481 if (mbx_cmnd != MBX_HEARTBEAT) {
5482 if (mboxq->vport) {
5483 lpfc_debugfs_disc_trc(mboxq->vport,
5484 LPFC_DISC_TRC_MBOX_VPORT,
5485 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5486 mbx_cmnd, mqe->un.mb_words[0],
5487 mqe->un.mb_words[1]);
5488 } else {
5489 lpfc_debugfs_disc_trc(phba->pport,
5490 LPFC_DISC_TRC_MBOX,
5491 "MBOX Send: cmd:x%x mb:x%x x%x",
5492 mbx_cmnd, mqe->un.mb_words[0],
5493 mqe->un.mb_words[1]);
5494 }
5495 }
5496 psli->slistat.mbox_cmd++;
5497
5498 /* Post the mailbox command to the port */
5499 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5500 if (rc != MBX_SUCCESS) {
5501 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5502 "(%d):2533 Mailbox command x%x (x%x) "
5503 "cannot issue Data: x%x x%x\n",
5504 mboxq->vport ? mboxq->vport->vpi : 0,
5505 mboxq->u.mb.mbxCommand,
5506 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5507 psli->sli_flag, MBX_NOWAIT);
5508 goto out_not_finished;
5509 }
5510
5511 return rc;
5512
5513out_not_finished:
5514 spin_lock_irqsave(&phba->hbalock, iflags);
5515 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5516 __lpfc_mbox_cmpl_put(phba, mboxq);
5517 /* Release the token */
5518 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5519 phba->sli.mbox_active = NULL;
5520 spin_unlock_irqrestore(&phba->hbalock, iflags);
5521
5522 return MBX_NOT_FINISHED;
5523}
5524
5525/**
5526 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5527 * @phba: Pointer to HBA context object.
5528 * @pmbox: Pointer to mailbox object.
5529 * @flag: Flag indicating how the mailbox need to be processed.
5530 *
5531 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5532 * the API jump table function pointer from the lpfc_hba struct.
5533 *
5534 * Return codes the caller owns the mailbox command after the return of the
5535 * function.
5536 **/
5537int
5538lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5539{
5540 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5541}
5542
5543/**
5544 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5545 * @phba: The hba struct for which this call is being executed.
5546 * @dev_grp: The HBA PCI-Device group number.
5547 *
5548 * This routine sets up the mbox interface API function jump table in @phba
5549 * struct.
5550 * Returns: 0 - success, -ENODEV - failure.
5551 **/
5552int
5553lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5554{
5555
5556 switch (dev_grp) {
5557 case LPFC_PCI_DEV_LP:
5558 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5559 phba->lpfc_sli_handle_slow_ring_event =
5560 lpfc_sli_handle_slow_ring_event_s3;
5561 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5562 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5563 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5564 break;
5565 case LPFC_PCI_DEV_OC:
5566 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5567 phba->lpfc_sli_handle_slow_ring_event =
5568 lpfc_sli_handle_slow_ring_event_s4;
5569 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5570 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5571 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5572 break;
5573 default:
5574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5575 "1420 Invalid HBA PCI-device group: 0x%x\n",
5576 dev_grp);
5577 return -ENODEV;
5578 break;
5579 }
5580 return 0;
5581}
5582
5583/**
James Smart3621a712009-04-06 18:47:14 -04005584 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005585 * @phba: Pointer to HBA context object.
5586 * @pring: Pointer to driver SLI ring object.
5587 * @piocb: Pointer to address of newly added command iocb.
5588 *
5589 * This function is called with hbalock held to add a command
5590 * iocb to the txq when SLI layer cannot submit the command iocb
5591 * to the ring.
5592 **/
James Smart858c9f62007-06-17 19:56:39 -05005593static void
James Smart92d7f7b2007-06-17 19:56:38 -05005594__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005595 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005596{
5597 /* Insert the caller's iocb in the txq tail for later processing. */
5598 list_add_tail(&piocb->list, &pring->txq);
5599 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005600}
5601
James Smarte59058c2008-08-24 21:49:00 -04005602/**
James Smart3621a712009-04-06 18:47:14 -04005603 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005604 * @phba: Pointer to HBA context object.
5605 * @pring: Pointer to driver SLI ring object.
5606 * @piocb: Pointer to address of newly added command iocb.
5607 *
5608 * This function is called with hbalock held before a new
5609 * iocb is submitted to the firmware. This function checks
5610 * txq to flush the iocbs in txq to Firmware before
5611 * submitting new iocbs to the Firmware.
5612 * If there are iocbs in the txq which need to be submitted
5613 * to firmware, lpfc_sli_next_iocb returns the first element
5614 * of the txq after dequeuing it from txq.
5615 * If there is no iocb in the txq then the function will return
5616 * *piocb and *piocb is set to NULL. Caller needs to check
5617 * *piocb to find if there are more commands in the txq.
5618 **/
dea31012005-04-17 16:05:31 -05005619static struct lpfc_iocbq *
5620lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005621 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005622{
5623 struct lpfc_iocbq * nextiocb;
5624
5625 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5626 if (!nextiocb) {
5627 nextiocb = *piocb;
5628 *piocb = NULL;
5629 }
5630
5631 return nextiocb;
5632}
5633
James Smarte59058c2008-08-24 21:49:00 -04005634/**
James Smart3772a992009-05-22 14:50:54 -04005635 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005636 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005637 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005638 * @piocb: Pointer to command iocb.
5639 * @flag: Flag indicating if this command can be put into txq.
5640 *
James Smart3772a992009-05-22 14:50:54 -04005641 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5642 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5643 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5644 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5645 * this function allows only iocbs for posting buffers. This function finds
5646 * next available slot in the command ring and posts the command to the
5647 * available slot and writes the port attention register to request HBA start
5648 * processing new iocb. If there is no slot available in the ring and
5649 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5650 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005651 *
James Smart3772a992009-05-22 14:50:54 -04005652 * This function is called with hbalock held. The function will return success
5653 * after it successfully submit the iocb to firmware or after adding to the
5654 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005655 **/
James Smart98c9ea52007-10-27 13:37:33 -04005656static int
James Smart3772a992009-05-22 14:50:54 -04005657__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005658 struct lpfc_iocbq *piocb, uint32_t flag)
5659{
5660 struct lpfc_iocbq *nextiocb;
5661 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005662 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005663
James Smart92d7f7b2007-06-17 19:56:38 -05005664 if (piocb->iocb_cmpl && (!piocb->vport) &&
5665 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5666 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5667 lpfc_printf_log(phba, KERN_ERR,
5668 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005669 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005670 piocb->iocb.ulpCommand);
5671 dump_stack();
5672 return IOCB_ERROR;
5673 }
5674
5675
Linas Vepstas8d63f372007-02-14 14:28:36 -06005676 /* If the PCI channel is in offline state, do not post iocbs. */
5677 if (unlikely(pci_channel_offline(phba->pcidev)))
5678 return IOCB_ERROR;
5679
James Smarta257bf92009-04-06 18:48:10 -04005680 /* If HBA has a deferred error attention, fail the iocb. */
5681 if (unlikely(phba->hba_flag & DEFER_ERATT))
5682 return IOCB_ERROR;
5683
dea31012005-04-17 16:05:31 -05005684 /*
5685 * We should never get an IOCB if we are in a < LINK_DOWN state
5686 */
James Smart2e0fef82007-06-17 19:56:36 -05005687 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005688 return IOCB_ERROR;
5689
5690 /*
5691 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005692 * outstanding event.
dea31012005-04-17 16:05:31 -05005693 */
James Smart0b727fe2007-10-27 13:37:25 -04005694 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005695 goto iocb_busy;
5696
James Smart2e0fef82007-06-17 19:56:36 -05005697 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005698 /*
James Smart2680eea2007-04-25 09:52:55 -04005699 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005700 * can be issued if the link is not up.
5701 */
5702 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005703 case CMD_GEN_REQUEST64_CR:
5704 case CMD_GEN_REQUEST64_CX:
5705 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5706 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005707 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005708 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5709 MENLO_TRANSPORT_TYPE))
5710
5711 goto iocb_busy;
5712 break;
dea31012005-04-17 16:05:31 -05005713 case CMD_QUE_RING_BUF_CN:
5714 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005715 /*
5716 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5717 * completion, iocb_cmpl MUST be 0.
5718 */
5719 if (piocb->iocb_cmpl)
5720 piocb->iocb_cmpl = NULL;
5721 /*FALLTHROUGH*/
5722 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005723 case CMD_CLOSE_XRI_CN:
5724 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005725 break;
5726 default:
5727 goto iocb_busy;
5728 }
5729
5730 /*
5731 * For FCP commands, we must be in a state where we can process link
5732 * attention events.
5733 */
5734 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005735 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005736 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005737 }
dea31012005-04-17 16:05:31 -05005738
dea31012005-04-17 16:05:31 -05005739 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5740 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5741 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5742
5743 if (iocb)
5744 lpfc_sli_update_ring(phba, pring);
5745 else
5746 lpfc_sli_update_full_ring(phba, pring);
5747
5748 if (!piocb)
5749 return IOCB_SUCCESS;
5750
5751 goto out_busy;
5752
5753 iocb_busy:
5754 pring->stats.iocb_cmd_delay++;
5755
5756 out_busy:
5757
5758 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005759 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005760 return IOCB_SUCCESS;
5761 }
5762
5763 return IOCB_BUSY;
5764}
5765
James Smart3772a992009-05-22 14:50:54 -04005766/**
James Smart4f774512009-05-22 14:52:35 -04005767 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5768 * @phba: Pointer to HBA context object.
5769 * @piocb: Pointer to command iocb.
5770 * @sglq: Pointer to the scatter gather queue object.
5771 *
5772 * This routine converts the bpl or bde that is in the IOCB
5773 * to a sgl list for the sli4 hardware. The physical address
5774 * of the bpl/bde is converted back to a virtual address.
5775 * If the IOCB contains a BPL then the list of BDE's is
5776 * converted to sli4_sge's. If the IOCB contains a single
5777 * BDE then it is converted to a single sli_sge.
5778 * The IOCB is still in cpu endianess so the contents of
5779 * the bpl can be used without byte swapping.
5780 *
5781 * Returns valid XRI = Success, NO_XRI = Failure.
5782**/
5783static uint16_t
5784lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5785 struct lpfc_sglq *sglq)
5786{
5787 uint16_t xritag = NO_XRI;
5788 struct ulp_bde64 *bpl = NULL;
5789 struct ulp_bde64 bde;
5790 struct sli4_sge *sgl = NULL;
5791 IOCB_t *icmd;
5792 int numBdes = 0;
5793 int i = 0;
5794
5795 if (!piocbq || !sglq)
5796 return xritag;
5797
5798 sgl = (struct sli4_sge *)sglq->sgl;
5799 icmd = &piocbq->iocb;
5800 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5801 numBdes = icmd->un.genreq64.bdl.bdeSize /
5802 sizeof(struct ulp_bde64);
5803 /* The addrHigh and addrLow fields within the IOCB
5804 * have not been byteswapped yet so there is no
5805 * need to swap them back.
5806 */
5807 bpl = (struct ulp_bde64 *)
5808 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5809
5810 if (!bpl)
5811 return xritag;
5812
5813 for (i = 0; i < numBdes; i++) {
5814 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005815 sgl->addr_hi = bpl->addrHigh;
5816 sgl->addr_lo = bpl->addrLow;
5817
James Smart4f774512009-05-22 14:52:35 -04005818 if ((i+1) == numBdes)
5819 bf_set(lpfc_sli4_sge_last, sgl, 1);
5820 else
5821 bf_set(lpfc_sli4_sge_last, sgl, 0);
5822 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005823 /* swap the size field back to the cpu so we
5824 * can assign it to the sgl.
5825 */
5826 bde.tus.w = le32_to_cpu(bpl->tus.w);
5827 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005828 bpl++;
5829 sgl++;
5830 }
5831 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5832 /* The addrHigh and addrLow fields of the BDE have not
5833 * been byteswapped yet so they need to be swapped
5834 * before putting them in the sgl.
5835 */
5836 sgl->addr_hi =
5837 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5838 sgl->addr_lo =
5839 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005840 bf_set(lpfc_sli4_sge_last, sgl, 1);
5841 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005842 sgl->sge_len =
5843 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005844 }
5845 return sglq->sli4_xritag;
5846}
5847
5848/**
5849 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5850 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005851 *
5852 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005853 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5854 * held.
James Smart4f774512009-05-22 14:52:35 -04005855 *
5856 * Return: index into SLI4 fast-path FCP queue index.
5857 **/
5858static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005859lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005860{
James Smart8fa38512009-07-19 10:01:03 -04005861 ++phba->fcp_qidx;
5862 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5863 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005864
James Smart8fa38512009-07-19 10:01:03 -04005865 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005866}
5867
5868/**
5869 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5870 * @phba: Pointer to HBA context object.
5871 * @piocb: Pointer to command iocb.
5872 * @wqe: Pointer to the work queue entry.
5873 *
5874 * This routine converts the iocb command to its Work Queue Entry
5875 * equivalent. The wqe pointer should not have any fields set when
5876 * this routine is called because it will memcpy over them.
5877 * This routine does not set the CQ_ID or the WQEC bits in the
5878 * wqe.
5879 *
5880 * Returns: 0 = Success, IOCB_ERROR = Failure.
5881 **/
5882static int
5883lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5884 union lpfc_wqe *wqe)
5885{
James Smart5ffc2662009-11-18 15:39:44 -05005886 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005887 uint8_t ct = 0;
5888 uint32_t fip;
5889 uint32_t abort_tag;
5890 uint8_t command_type = ELS_COMMAND_NON_FIP;
5891 uint8_t cmnd;
5892 uint16_t xritag;
5893 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005894 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005895 int numBdes, i;
5896 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005897
James Smart45ed1192009-10-02 15:17:02 -04005898 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005899 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005900 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005901 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005902 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005903 command_type = ELS_COMMAND_FIP;
5904 else
5905 command_type = ELS_COMMAND_NON_FIP;
5906
James Smart4f774512009-05-22 14:52:35 -04005907 /* Some of the fields are in the right position already */
5908 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5909 abort_tag = (uint32_t) iocbq->iotag;
5910 xritag = iocbq->sli4_xritag;
5911 wqe->words[7] = 0; /* The ct field has moved so reset */
5912 /* words0-2 bpl convert bde */
5913 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005914 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5915 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005916 bpl = (struct ulp_bde64 *)
5917 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5918 if (!bpl)
5919 return IOCB_ERROR;
5920
5921 /* Should already be byte swapped. */
5922 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5923 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5924 /* swap the size field back to the cpu so we
5925 * can assign it to the sgl.
5926 */
5927 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005928 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5929 total_len = 0;
5930 for (i = 0; i < numBdes; i++) {
5931 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5932 total_len += bde.tus.f.bdeSize;
5933 }
James Smart4f774512009-05-22 14:52:35 -04005934 } else
James Smart5ffc2662009-11-18 15:39:44 -05005935 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005936
5937 iocbq->iocb.ulpIoTag = iocbq->iotag;
5938 cmnd = iocbq->iocb.ulpCommand;
5939
5940 switch (iocbq->iocb.ulpCommand) {
5941 case CMD_ELS_REQUEST64_CR:
5942 if (!iocbq->iocb.ulpLe) {
5943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5944 "2007 Only Limited Edition cmd Format"
5945 " supported 0x%x\n",
5946 iocbq->iocb.ulpCommand);
5947 return IOCB_ERROR;
5948 }
James Smart5ffc2662009-11-18 15:39:44 -05005949 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005950 /* Els_reguest64 has a TMO */
5951 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5952 iocbq->iocb.ulpTimeout);
5953 /* Need a VF for word 4 set the vf bit*/
5954 bf_set(els_req64_vf, &wqe->els_req, 0);
5955 /* And a VFID for word 12 */
5956 bf_set(els_req64_vfid, &wqe->els_req, 0);
5957 /*
5958 * Set ct field to 3, indicates that the context_tag field
5959 * contains the FCFI and remote N_Port_ID is
5960 * in word 5.
5961 */
5962
5963 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5964 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5965 iocbq->iocb.ulpContext);
5966
James Smart4f774512009-05-22 14:52:35 -04005967 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5968 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5969 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005970
5971 if (command_type == ELS_COMMAND_FIP) {
5972 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5973 >> LPFC_FIP_ELS_ID_SHIFT);
5974 }
5975 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5976
James Smart4f774512009-05-22 14:52:35 -04005977 break;
James Smart5ffc2662009-11-18 15:39:44 -05005978 case CMD_XMIT_SEQUENCE64_CX:
5979 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5980 iocbq->iocb.un.ulpWord[3]);
5981 wqe->generic.word3 = 0;
5982 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05005983 /* The entire sequence is transmitted for this IOCB */
5984 xmit_len = total_len;
5985 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005986 case CMD_XMIT_SEQUENCE64_CR:
5987 /* word3 iocb=io_tag32 wqe=payload_offset */
5988 /* payload offset used for multilpe outstanding
5989 * sequences on the same exchange
5990 */
5991 wqe->words[3] = 0;
5992 /* word4 relative_offset memcpy */
5993 /* word5 r_ctl/df_ctl memcpy */
5994 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005995 wqe->xmit_sequence.xmit_len = xmit_len;
5996 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005997 break;
5998 case CMD_XMIT_BCAST64_CN:
5999 /* word3 iocb=iotag32 wqe=payload_len */
6000 wqe->words[3] = 0; /* no definition for this in wqe */
6001 /* word4 iocb=rsvd wqe=rsvd */
6002 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6003 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6004 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6005 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6006 break;
6007 case CMD_FCP_IWRITE64_CR:
6008 command_type = FCP_COMMAND_DATA_OUT;
6009 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6010 * confusing.
6011 * word3 is payload_len: byte offset to the sgl entry for the
6012 * fcp_command.
6013 * word4 is total xfer len, same as the IOCB->ulpParameter.
6014 * word5 is initial xfer len 0 = wait for xfer-ready
6015 */
6016
6017 /* Always wait for xfer-ready before sending data */
6018 wqe->fcp_iwrite.initial_xfer_len = 0;
6019 /* word 4 (xfer length) should have been set on the memcpy */
6020
6021 /* allow write to fall through to read */
6022 case CMD_FCP_IREAD64_CR:
6023 /* FCP_CMD is always the 1st sgl entry */
6024 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05006025 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04006026
6027 /* word 4 (xfer length) should have been set on the memcpy */
6028
6029 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6030 iocbq->iocb.ulpFCP2Rcvy);
6031 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6032 /* The XC bit and the XS bit are similar. The driver never
6033 * tracked whether or not the exchange was previouslly open.
6034 * XC = Exchange create, 0 is create. 1 is already open.
6035 * XS = link cmd: 1 do not close the exchange after command.
6036 * XS = 0 close exchange when command completes.
6037 * The only time we would not set the XC bit is when the XS bit
6038 * is set and we are sending our 2nd or greater command on
6039 * this exchange.
6040 */
James Smart4f774512009-05-22 14:52:35 -04006041 /* Always open the exchange */
6042 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6043
6044 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6045 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006046 break;
6047 case CMD_FCP_ICMND64_CR:
6048 /* Always open the exchange */
6049 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6050
6051 wqe->words[4] = 0;
6052 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6053 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006054 break;
6055 case CMD_GEN_REQUEST64_CR:
6056 /* word3 command length is described as byte offset to the
6057 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6058 * sgl[0] = cmnd
6059 * sgl[1] = rsp.
6060 *
6061 */
James Smart5ffc2662009-11-18 15:39:44 -05006062 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006063 /* Word4 parameter copied in the memcpy */
6064 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6065 /* word6 context tag copied in memcpy */
6066 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6067 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6069 "2015 Invalid CT %x command 0x%x\n",
6070 ct, iocbq->iocb.ulpCommand);
6071 return IOCB_ERROR;
6072 }
6073 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6074 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6075 iocbq->iocb.ulpTimeout);
6076
6077 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6078 command_type = OTHER_COMMAND;
6079 break;
6080 case CMD_XMIT_ELS_RSP64_CX:
6081 /* words0-2 BDE memcpy */
6082 /* word3 iocb=iotag32 wqe=rsvd */
6083 wqe->words[3] = 0;
6084 /* word4 iocb=did wge=rsvd. */
6085 wqe->words[4] = 0;
6086 /* word5 iocb=rsvd wge=did */
6087 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6088 iocbq->iocb.un.elsreq64.remoteID);
6089
6090 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6091 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6092
6093 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6094 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6095 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6096 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6097 iocbq->vport->vpi + phba->vpi_base);
6098 command_type = OTHER_COMMAND;
6099 break;
6100 case CMD_CLOSE_XRI_CN:
6101 case CMD_ABORT_XRI_CN:
6102 case CMD_ABORT_XRI_CX:
6103 /* words 0-2 memcpy should be 0 rserved */
6104 /* port will send abts */
6105 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6106 /*
6107 * The link is down so the fw does not need to send abts
6108 * on the wire.
6109 */
6110 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6111 else
6112 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6113 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006114 wqe->words[5] = 0;
6115 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6116 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6117 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006118 /*
6119 * The abort handler will send us CMD_ABORT_XRI_CN or
6120 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6121 */
6122 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6123 cmnd = CMD_ABORT_XRI_CX;
6124 command_type = OTHER_COMMAND;
6125 xritag = 0;
6126 break;
James Smart6669f9b2009-10-02 15:16:45 -04006127 case CMD_XMIT_BLS_RSP64_CX:
6128 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6129 * we re-construct this WQE here based on information in
6130 * iocbq from scratch.
6131 */
6132 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006133 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006134 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006135 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6136 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6137 LPFC_ABTS_UNSOL_INT) {
6138 /* ABTS sent by initiator to CT exchange, the
6139 * RX_ID field will be filled with the newly
6140 * allocated responder XRI.
6141 */
6142 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6143 iocbq->sli4_xritag);
6144 } else {
6145 /* ABTS sent by responder to CT exchange, the
6146 * RX_ID field will be filled with the responder
6147 * RX_ID from ABTS.
6148 */
6149 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6150 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6151 }
James Smart6669f9b2009-10-02 15:16:45 -04006152 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6153 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6154 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6155 iocbq->iocb.ulpContext);
6156 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6157 command_type = OTHER_COMMAND;
6158 break;
James Smart4f774512009-05-22 14:52:35 -04006159 case CMD_XRI_ABORTED_CX:
6160 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6161 /* words0-2 are all 0's no bde */
6162 /* word3 and word4 are rsvrd */
6163 wqe->words[3] = 0;
6164 wqe->words[4] = 0;
6165 /* word5 iocb=rsvd wge=did */
6166 /* There is no remote port id in the IOCB? */
6167 /* Let this fall through and fail */
6168 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6169 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6170 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6171 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6172 default:
6173 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6174 "2014 Invalid command 0x%x\n",
6175 iocbq->iocb.ulpCommand);
6176 return IOCB_ERROR;
6177 break;
6178
6179 }
6180 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6181 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6182 wqe->generic.abort_tag = abort_tag;
6183 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6184 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6185 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6186 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6187
6188 return 0;
6189}
6190
6191/**
6192 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6193 * @phba: Pointer to HBA context object.
6194 * @ring_number: SLI ring number to issue iocb on.
6195 * @piocb: Pointer to command iocb.
6196 * @flag: Flag indicating if this command can be put into txq.
6197 *
6198 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6199 * an iocb command to an HBA with SLI-4 interface spec.
6200 *
6201 * This function is called with hbalock held. The function will return success
6202 * after it successfully submit the iocb to firmware or after adding to the
6203 * txq.
6204 **/
6205static int
6206__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6207 struct lpfc_iocbq *piocb, uint32_t flag)
6208{
6209 struct lpfc_sglq *sglq;
6210 uint16_t xritag;
6211 union lpfc_wqe wqe;
6212 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006213
6214 if (piocb->sli4_xritag == NO_XRI) {
6215 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006216 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006217 sglq = NULL;
6218 else {
6219 sglq = __lpfc_sli_get_sglq(phba);
6220 if (!sglq)
6221 return IOCB_ERROR;
6222 piocb->sli4_xritag = sglq->sli4_xritag;
6223 }
6224 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6225 sglq = NULL; /* These IO's already have an XRI and
6226 * a mapped sgl.
6227 */
6228 } else {
6229 /* This is a continuation of a commandi,(CX) so this
6230 * sglq is on the active list
6231 */
6232 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6233 if (!sglq)
6234 return IOCB_ERROR;
6235 }
6236
6237 if (sglq) {
6238 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6239 if (xritag != sglq->sli4_xritag)
6240 return IOCB_ERROR;
6241 }
6242
6243 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6244 return IOCB_ERROR;
6245
James Smart341af102010-01-26 23:07:37 -05006246 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6247 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006248 /*
6249 * For FCP command IOCB, get a new WQ index to distribute
6250 * WQE across the WQsr. On the other hand, for abort IOCB,
6251 * it carries the same WQ index to the original command
6252 * IOCB.
6253 */
James Smart341af102010-01-26 23:07:37 -05006254 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006255 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6256 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6257 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006258 return IOCB_ERROR;
6259 } else {
6260 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6261 return IOCB_ERROR;
6262 }
6263 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6264
6265 return 0;
6266}
6267
6268/**
James Smart3772a992009-05-22 14:50:54 -04006269 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6270 *
6271 * This routine wraps the actual lockless version for issusing IOCB function
6272 * pointer from the lpfc_hba struct.
6273 *
6274 * Return codes:
6275 * IOCB_ERROR - Error
6276 * IOCB_SUCCESS - Success
6277 * IOCB_BUSY - Busy
6278 **/
6279static inline int
6280__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6281 struct lpfc_iocbq *piocb, uint32_t flag)
6282{
6283 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6284}
6285
6286/**
6287 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6288 * @phba: The hba struct for which this call is being executed.
6289 * @dev_grp: The HBA PCI-Device group number.
6290 *
6291 * This routine sets up the SLI interface API function jump table in @phba
6292 * struct.
6293 * Returns: 0 - success, -ENODEV - failure.
6294 **/
6295int
6296lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6297{
6298
6299 switch (dev_grp) {
6300 case LPFC_PCI_DEV_LP:
6301 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6302 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6303 break;
James Smart4f774512009-05-22 14:52:35 -04006304 case LPFC_PCI_DEV_OC:
6305 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6306 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6307 break;
James Smart3772a992009-05-22 14:50:54 -04006308 default:
6309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6310 "1419 Invalid HBA PCI-device group: 0x%x\n",
6311 dev_grp);
6312 return -ENODEV;
6313 break;
6314 }
6315 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6316 return 0;
6317}
James Smart92d7f7b2007-06-17 19:56:38 -05006318
James Smarte59058c2008-08-24 21:49:00 -04006319/**
James Smart3621a712009-04-06 18:47:14 -04006320 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006321 * @phba: Pointer to HBA context object.
6322 * @pring: Pointer to driver SLI ring object.
6323 * @piocb: Pointer to command iocb.
6324 * @flag: Flag indicating if this command can be put into txq.
6325 *
6326 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6327 * function. This function gets the hbalock and calls
6328 * __lpfc_sli_issue_iocb function and will return the error returned
6329 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6330 * functions which do not hold hbalock.
6331 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006332int
James Smart3772a992009-05-22 14:50:54 -04006333lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006334 struct lpfc_iocbq *piocb, uint32_t flag)
6335{
6336 unsigned long iflags;
6337 int rc;
6338
6339 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006340 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006341 spin_unlock_irqrestore(&phba->hbalock, iflags);
6342
6343 return rc;
6344}
6345
James Smarte59058c2008-08-24 21:49:00 -04006346/**
James Smart3621a712009-04-06 18:47:14 -04006347 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006348 * @phba: Pointer to HBA context object.
6349 *
6350 * This function is called while driver attaches with the
6351 * HBA to setup the extra ring. The extra ring is used
6352 * only when driver needs to support target mode functionality
6353 * or IP over FC functionalities.
6354 *
6355 * This function is called with no lock held.
6356 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006357static int
6358lpfc_extra_ring_setup( struct lpfc_hba *phba)
6359{
6360 struct lpfc_sli *psli;
6361 struct lpfc_sli_ring *pring;
6362
6363 psli = &phba->sli;
6364
6365 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006366
6367 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006368 pring = &psli->ring[psli->fcp_ring];
6369 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6370 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6371 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6372 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6373
James Smarta4bc3372006-12-02 13:34:16 -05006374 /* and give them to the extra ring */
6375 pring = &psli->ring[psli->extra_ring];
6376
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006377 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6378 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6379 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6380 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6381
6382 /* Setup default profile for this ring */
6383 pring->iotag_max = 4096;
6384 pring->num_mask = 1;
6385 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006386 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6387 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006388 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6389 return 0;
6390}
6391
James Smarte59058c2008-08-24 21:49:00 -04006392/**
James Smart3621a712009-04-06 18:47:14 -04006393 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006394 * @phba: Pointer to HBA context object.
6395 * @pring: Pointer to driver SLI ring object.
6396 * @iocbq: Pointer to iocb object.
6397 *
6398 * This function is called by the slow ring event handler
6399 * function when there is an ASYNC event iocb in the ring.
6400 * This function is called with no lock held.
6401 * Currently this function handles only temperature related
6402 * ASYNC events. The function decodes the temperature sensor
6403 * event message and posts events for the management applications.
6404 **/
James Smart98c9ea52007-10-27 13:37:33 -04006405static void
James Smart57127f12007-10-27 13:37:05 -04006406lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6407 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6408{
6409 IOCB_t *icmd;
6410 uint16_t evt_code;
6411 uint16_t temp;
6412 struct temp_event temp_event_data;
6413 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006414 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006415
6416 icmd = &iocbq->iocb;
6417 evt_code = icmd->un.asyncstat.evt_code;
6418 temp = icmd->ulpContext;
6419
6420 if ((evt_code != ASYNC_TEMP_WARN) &&
6421 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006422 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006423 lpfc_printf_log(phba,
6424 KERN_ERR,
6425 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006426 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006427 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006428 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6429 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6430 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6431 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006432 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006433 icmd->un.asyncstat.evt_code,
6434 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6435 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6436 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6437 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6438
James Smart57127f12007-10-27 13:37:05 -04006439 return;
6440 }
6441 temp_event_data.data = (uint32_t)temp;
6442 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6443 if (evt_code == ASYNC_TEMP_WARN) {
6444 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6445 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006446 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006447 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006448 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006449 "corrective action. temperature : %d Celsius\n",
6450 temp);
6451 }
6452 if (evt_code == ASYNC_TEMP_SAFE) {
6453 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6454 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006455 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006456 LOG_TEMP,
6457 "0340 Adapter temperature is OK now. "
6458 "temperature : %d Celsius\n",
6459 temp);
6460 }
6461
6462 /* Send temperature change event to applications */
6463 shost = lpfc_shost_from_vport(phba->pport);
6464 fc_host_post_vendor_event(shost, fc_get_event_number(),
6465 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006466 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006467
6468}
6469
6470
James Smarte59058c2008-08-24 21:49:00 -04006471/**
James Smart3621a712009-04-06 18:47:14 -04006472 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006473 * @phba: Pointer to HBA context object.
6474 *
6475 * lpfc_sli_setup sets up rings of the SLI interface with
6476 * number of iocbs per ring and iotags. This function is
6477 * called while driver attach to the HBA and before the
6478 * interrupts are enabled. So there is no need for locking.
6479 *
6480 * This function always returns 0.
6481 **/
dea31012005-04-17 16:05:31 -05006482int
6483lpfc_sli_setup(struct lpfc_hba *phba)
6484{
James Smarted957682007-06-17 19:56:37 -05006485 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006486 struct lpfc_sli *psli = &phba->sli;
6487 struct lpfc_sli_ring *pring;
6488
6489 psli->num_rings = MAX_CONFIGURED_RINGS;
6490 psli->sli_flag = 0;
6491 psli->fcp_ring = LPFC_FCP_RING;
6492 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006493 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006494
James Bottomley604a3e32005-10-29 10:28:33 -05006495 psli->iocbq_lookup = NULL;
6496 psli->iocbq_lookup_len = 0;
6497 psli->last_iotag = 0;
6498
dea31012005-04-17 16:05:31 -05006499 for (i = 0; i < psli->num_rings; i++) {
6500 pring = &psli->ring[i];
6501 switch (i) {
6502 case LPFC_FCP_RING: /* ring 0 - FCP */
6503 /* numCiocb and numRiocb are used in config_port */
6504 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6505 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6506 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6507 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6508 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6509 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006510 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006511 SLI3_IOCB_CMD_SIZE :
6512 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006513 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006514 SLI3_IOCB_RSP_SIZE :
6515 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006516 pring->iotag_ctr = 0;
6517 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006518 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006519 pring->fast_iotag = pring->iotag_max;
6520 pring->num_mask = 0;
6521 break;
James Smarta4bc3372006-12-02 13:34:16 -05006522 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006523 /* numCiocb and numRiocb are used in config_port */
6524 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6525 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006526 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006527 SLI3_IOCB_CMD_SIZE :
6528 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006529 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006530 SLI3_IOCB_RSP_SIZE :
6531 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006532 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006533 pring->num_mask = 0;
6534 break;
6535 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6536 /* numCiocb and numRiocb are used in config_port */
6537 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6538 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006539 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006540 SLI3_IOCB_CMD_SIZE :
6541 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006542 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006543 SLI3_IOCB_RSP_SIZE :
6544 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006545 pring->fast_iotag = 0;
6546 pring->iotag_ctr = 0;
6547 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006548 pring->lpfc_sli_rcv_async_status =
6549 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006550 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006551 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006552 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6553 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006554 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006555 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006556 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006557 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6558 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006559 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006560 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006561 pring->prt[2].profile = 0; /* Mask 2 */
6562 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006563 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006564 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006565 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006566 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006567 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006568 pring->prt[3].profile = 0; /* Mask 3 */
6569 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006570 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006571 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006572 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006573 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006574 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006575 /* abort unsolicited sequence */
6576 pring->prt[4].profile = 0; /* Mask 4 */
6577 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6578 pring->prt[4].type = FC_TYPE_BLS;
6579 pring->prt[4].lpfc_sli_rcv_unsol_event =
6580 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006581 break;
6582 }
James Smarted957682007-06-17 19:56:37 -05006583 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006584 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006585 }
James Smarted957682007-06-17 19:56:37 -05006586 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006587 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006588 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6589 "SLI2 SLIM Data: x%x x%lx\n",
6590 phba->brd_no, totiocbsize,
6591 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006592 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006593 if (phba->cfg_multi_ring_support == 2)
6594 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006595
6596 return 0;
6597}
6598
James Smarte59058c2008-08-24 21:49:00 -04006599/**
James Smart3621a712009-04-06 18:47:14 -04006600 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006601 * @phba: Pointer to HBA context object.
6602 *
6603 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6604 * ring. This function also initializes ring indices of each ring.
6605 * This function is called during the initialization of the SLI
6606 * interface of an HBA.
6607 * This function is called with no lock held and always returns
6608 * 1.
6609 **/
dea31012005-04-17 16:05:31 -05006610int
James Smart2e0fef82007-06-17 19:56:36 -05006611lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006612{
6613 struct lpfc_sli *psli;
6614 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006615 int i;
dea31012005-04-17 16:05:31 -05006616
6617 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006618 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006619 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006620 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006621 /* Initialize list headers for txq and txcmplq as double linked lists */
6622 for (i = 0; i < psli->num_rings; i++) {
6623 pring = &psli->ring[i];
6624 pring->ringno = i;
6625 pring->next_cmdidx = 0;
6626 pring->local_getidx = 0;
6627 pring->cmdidx = 0;
6628 INIT_LIST_HEAD(&pring->txq);
6629 INIT_LIST_HEAD(&pring->txcmplq);
6630 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006631 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006632 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006633 }
James Smart2e0fef82007-06-17 19:56:36 -05006634 spin_unlock_irq(&phba->hbalock);
6635 return 1;
dea31012005-04-17 16:05:31 -05006636}
6637
James Smarte59058c2008-08-24 21:49:00 -04006638/**
James Smart04c68492009-05-22 14:52:52 -04006639 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6640 * @phba: Pointer to HBA context object.
6641 *
6642 * This routine flushes the mailbox command subsystem. It will unconditionally
6643 * flush all the mailbox commands in the three possible stages in the mailbox
6644 * command sub-system: pending mailbox command queue; the outstanding mailbox
6645 * command; and completed mailbox command queue. It is caller's responsibility
6646 * to make sure that the driver is in the proper state to flush the mailbox
6647 * command sub-system. Namely, the posting of mailbox commands into the
6648 * pending mailbox command queue from the various clients must be stopped;
6649 * either the HBA is in a state that it will never works on the outstanding
6650 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6651 * mailbox command has been completed.
6652 **/
6653static void
6654lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6655{
6656 LIST_HEAD(completions);
6657 struct lpfc_sli *psli = &phba->sli;
6658 LPFC_MBOXQ_t *pmb;
6659 unsigned long iflag;
6660
6661 /* Flush all the mailbox commands in the mbox system */
6662 spin_lock_irqsave(&phba->hbalock, iflag);
6663 /* The pending mailbox command queue */
6664 list_splice_init(&phba->sli.mboxq, &completions);
6665 /* The outstanding active mailbox command */
6666 if (psli->mbox_active) {
6667 list_add_tail(&psli->mbox_active->list, &completions);
6668 psli->mbox_active = NULL;
6669 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6670 }
6671 /* The completed mailbox command queue */
6672 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6673 spin_unlock_irqrestore(&phba->hbalock, iflag);
6674
6675 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6676 while (!list_empty(&completions)) {
6677 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6678 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6679 if (pmb->mbox_cmpl)
6680 pmb->mbox_cmpl(phba, pmb);
6681 }
6682}
6683
6684/**
James Smart3621a712009-04-06 18:47:14 -04006685 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006686 * @vport: Pointer to virtual port object.
6687 *
6688 * lpfc_sli_host_down is called to clean up the resources
6689 * associated with a vport before destroying virtual
6690 * port data structures.
6691 * This function does following operations:
6692 * - Free discovery resources associated with this virtual
6693 * port.
6694 * - Free iocbs associated with this virtual port in
6695 * the txq.
6696 * - Send abort for all iocb commands associated with this
6697 * vport in txcmplq.
6698 *
6699 * This function is called with no lock held and always returns 1.
6700 **/
dea31012005-04-17 16:05:31 -05006701int
James Smart92d7f7b2007-06-17 19:56:38 -05006702lpfc_sli_host_down(struct lpfc_vport *vport)
6703{
James Smart858c9f62007-06-17 19:56:39 -05006704 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006705 struct lpfc_hba *phba = vport->phba;
6706 struct lpfc_sli *psli = &phba->sli;
6707 struct lpfc_sli_ring *pring;
6708 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006709 int i;
6710 unsigned long flags = 0;
6711 uint16_t prev_pring_flag;
6712
6713 lpfc_cleanup_discovery_resources(vport);
6714
6715 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006716 for (i = 0; i < psli->num_rings; i++) {
6717 pring = &psli->ring[i];
6718 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006719 /* Only slow rings */
6720 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006721 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006722 /* Set the lpfc data pending flag */
6723 set_bit(LPFC_DATA_READY, &phba->data_flags);
6724 }
James Smart92d7f7b2007-06-17 19:56:38 -05006725 /*
6726 * Error everything on the txq since these iocbs have not been
6727 * given to the FW yet.
6728 */
James Smart92d7f7b2007-06-17 19:56:38 -05006729 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6730 if (iocb->vport != vport)
6731 continue;
James Smart858c9f62007-06-17 19:56:39 -05006732 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006733 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006734 }
6735
6736 /* Next issue ABTS for everything on the txcmplq */
6737 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6738 list) {
6739 if (iocb->vport != vport)
6740 continue;
6741 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6742 }
6743
6744 pring->flag = prev_pring_flag;
6745 }
6746
6747 spin_unlock_irqrestore(&phba->hbalock, flags);
6748
James Smarta257bf92009-04-06 18:48:10 -04006749 /* Cancel all the IOCBs from the completions list */
6750 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6751 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006752 return 1;
6753}
6754
James Smarte59058c2008-08-24 21:49:00 -04006755/**
James Smart3621a712009-04-06 18:47:14 -04006756 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006757 * @phba: Pointer to HBA context object.
6758 *
6759 * This function cleans up all iocb, buffers, mailbox commands
6760 * while shutting down the HBA. This function is called with no
6761 * lock held and always returns 1.
6762 * This function does the following to cleanup driver resources:
6763 * - Free discovery resources for each virtual port
6764 * - Cleanup any pending fabric iocbs
6765 * - Iterate through the iocb txq and free each entry
6766 * in the list.
6767 * - Free up any buffer posted to the HBA
6768 * - Free mailbox commands in the mailbox queue.
6769 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006770int
James Smart2e0fef82007-06-17 19:56:36 -05006771lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006772{
James Smart2534ba72007-04-25 09:52:20 -04006773 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006774 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006775 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006776 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006777 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006778 int i;
6779
6780 /* Shutdown the mailbox command sub-system */
6781 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006782
dea31012005-04-17 16:05:31 -05006783 lpfc_hba_down_prep(phba);
6784
James Smart92d7f7b2007-06-17 19:56:38 -05006785 lpfc_fabric_abort_hba(phba);
6786
James Smart2e0fef82007-06-17 19:56:36 -05006787 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006788 for (i = 0; i < psli->num_rings; i++) {
6789 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006790 /* Only slow rings */
6791 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006792 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006793 /* Set the lpfc data pending flag */
6794 set_bit(LPFC_DATA_READY, &phba->data_flags);
6795 }
dea31012005-04-17 16:05:31 -05006796
6797 /*
6798 * Error everything on the txq since these iocbs have not been
6799 * given to the FW yet.
6800 */
James Smart2534ba72007-04-25 09:52:20 -04006801 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006802 pring->txq_cnt = 0;
6803
dea31012005-04-17 16:05:31 -05006804 }
James Smart2e0fef82007-06-17 19:56:36 -05006805 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006806
James Smarta257bf92009-04-06 18:48:10 -04006807 /* Cancel all the IOCBs from the completions list */
6808 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6809 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006810
James Smart0ff10d42008-01-11 01:52:36 -05006811 spin_lock_irqsave(&phba->hbalock, flags);
6812 list_splice_init(&phba->elsbuf, &completions);
6813 phba->elsbuf_cnt = 0;
6814 phba->elsbuf_prev_cnt = 0;
6815 spin_unlock_irqrestore(&phba->hbalock, flags);
6816
6817 while (!list_empty(&completions)) {
6818 list_remove_head(&completions, buf_ptr,
6819 struct lpfc_dmabuf, list);
6820 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6821 kfree(buf_ptr);
6822 }
6823
dea31012005-04-17 16:05:31 -05006824 /* Return any active mbox cmds */
6825 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006826
James Smartda0436e2009-05-22 14:51:39 -04006827 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006828 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006829 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006830
James Smartda0436e2009-05-22 14:51:39 -04006831 return 1;
6832}
James Smart92d7f7b2007-06-17 19:56:38 -05006833
James Smartda0436e2009-05-22 14:51:39 -04006834/**
6835 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6836 * @phba: Pointer to HBA context object.
6837 *
6838 * This function cleans up all queues, iocb, buffers, mailbox commands while
6839 * shutting down the SLI4 HBA FCoE function. This function is called with no
6840 * lock held and always returns 1.
6841 *
6842 * This function does the following to cleanup driver FCoE function resources:
6843 * - Free discovery resources for each virtual port
6844 * - Cleanup any pending fabric iocbs
6845 * - Iterate through the iocb txq and free each entry in the list.
6846 * - Free up any buffer posted to the HBA.
6847 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6848 * - Free mailbox commands in the mailbox queue.
6849 **/
6850int
6851lpfc_sli4_hba_down(struct lpfc_hba *phba)
6852{
6853 /* Stop the SLI4 device port */
6854 lpfc_stop_port(phba);
6855
6856 /* Tear down the queues in the HBA */
6857 lpfc_sli4_queue_unset(phba);
6858
6859 /* unregister default FCFI from the HBA */
6860 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6861
dea31012005-04-17 16:05:31 -05006862 return 1;
6863}
6864
James Smarte59058c2008-08-24 21:49:00 -04006865/**
James Smart3621a712009-04-06 18:47:14 -04006866 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006867 * @srcp: Source memory pointer.
6868 * @destp: Destination memory pointer.
6869 * @cnt: Number of words required to be copied.
6870 *
6871 * This function is used for copying data between driver memory
6872 * and the SLI memory. This function also changes the endianness
6873 * of each word if native endianness is different from SLI
6874 * endianness. This function can be called with or without
6875 * lock.
6876 **/
dea31012005-04-17 16:05:31 -05006877void
6878lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6879{
6880 uint32_t *src = srcp;
6881 uint32_t *dest = destp;
6882 uint32_t ldata;
6883 int i;
6884
6885 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6886 ldata = *src;
6887 ldata = le32_to_cpu(ldata);
6888 *dest = ldata;
6889 src++;
6890 dest++;
6891 }
6892}
6893
James Smarte59058c2008-08-24 21:49:00 -04006894
6895/**
James Smarta0c87cb2009-07-19 10:01:10 -04006896 * lpfc_sli_bemem_bcopy - SLI memory copy function
6897 * @srcp: Source memory pointer.
6898 * @destp: Destination memory pointer.
6899 * @cnt: Number of words required to be copied.
6900 *
6901 * This function is used for copying data between a data structure
6902 * with big endian representation to local endianness.
6903 * This function can be called with or without lock.
6904 **/
6905void
6906lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6907{
6908 uint32_t *src = srcp;
6909 uint32_t *dest = destp;
6910 uint32_t ldata;
6911 int i;
6912
6913 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6914 ldata = *src;
6915 ldata = be32_to_cpu(ldata);
6916 *dest = ldata;
6917 src++;
6918 dest++;
6919 }
6920}
6921
6922/**
James Smart3621a712009-04-06 18:47:14 -04006923 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006924 * @phba: Pointer to HBA context object.
6925 * @pring: Pointer to driver SLI ring object.
6926 * @mp: Pointer to driver buffer object.
6927 *
6928 * This function is called with no lock held.
6929 * It always return zero after adding the buffer to the postbufq
6930 * buffer list.
6931 **/
dea31012005-04-17 16:05:31 -05006932int
James Smart2e0fef82007-06-17 19:56:36 -05006933lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6934 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006935{
6936 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6937 later */
James Smart2e0fef82007-06-17 19:56:36 -05006938 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006939 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006940 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006941 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006942 return 0;
6943}
6944
James Smarte59058c2008-08-24 21:49:00 -04006945/**
James Smart3621a712009-04-06 18:47:14 -04006946 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006947 * @phba: Pointer to HBA context object.
6948 *
6949 * When HBQ is enabled, buffers are searched based on tags. This function
6950 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6951 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6952 * does not conflict with tags of buffer posted for unsolicited events.
6953 * The function returns the allocated tag. The function is called with
6954 * no locks held.
6955 **/
James Smart76bb24e2007-10-27 13:38:00 -04006956uint32_t
6957lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6958{
6959 spin_lock_irq(&phba->hbalock);
6960 phba->buffer_tag_count++;
6961 /*
6962 * Always set the QUE_BUFTAG_BIT to distiguish between
6963 * a tag assigned by HBQ.
6964 */
6965 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6966 spin_unlock_irq(&phba->hbalock);
6967 return phba->buffer_tag_count;
6968}
6969
James Smarte59058c2008-08-24 21:49:00 -04006970/**
James Smart3621a712009-04-06 18:47:14 -04006971 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006972 * @phba: Pointer to HBA context object.
6973 * @pring: Pointer to driver SLI ring object.
6974 * @tag: Buffer tag.
6975 *
6976 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6977 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6978 * iocb is posted to the response ring with the tag of the buffer.
6979 * This function searches the pring->postbufq list using the tag
6980 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6981 * iocb. If the buffer is found then lpfc_dmabuf object of the
6982 * buffer is returned to the caller else NULL is returned.
6983 * This function is called with no lock held.
6984 **/
James Smart76bb24e2007-10-27 13:38:00 -04006985struct lpfc_dmabuf *
6986lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6987 uint32_t tag)
6988{
6989 struct lpfc_dmabuf *mp, *next_mp;
6990 struct list_head *slp = &pring->postbufq;
6991
6992 /* Search postbufq, from the begining, looking for a match on tag */
6993 spin_lock_irq(&phba->hbalock);
6994 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6995 if (mp->buffer_tag == tag) {
6996 list_del_init(&mp->list);
6997 pring->postbufq_cnt--;
6998 spin_unlock_irq(&phba->hbalock);
6999 return mp;
7000 }
7001 }
7002
7003 spin_unlock_irq(&phba->hbalock);
7004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007005 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007006 "ring %d Data x%lx x%p x%p x%x\n",
7007 pring->ringno, (unsigned long) tag,
7008 slp->next, slp->prev, pring->postbufq_cnt);
7009
7010 return NULL;
7011}
dea31012005-04-17 16:05:31 -05007012
James Smarte59058c2008-08-24 21:49:00 -04007013/**
James Smart3621a712009-04-06 18:47:14 -04007014 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007015 * @phba: Pointer to HBA context object.
7016 * @pring: Pointer to driver SLI ring object.
7017 * @phys: DMA address of the buffer.
7018 *
7019 * This function searches the buffer list using the dma_address
7020 * of unsolicited event to find the driver's lpfc_dmabuf object
7021 * corresponding to the dma_address. The function returns the
7022 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7023 * This function is called by the ct and els unsolicited event
7024 * handlers to get the buffer associated with the unsolicited
7025 * event.
7026 *
7027 * This function is called with no lock held.
7028 **/
dea31012005-04-17 16:05:31 -05007029struct lpfc_dmabuf *
7030lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7031 dma_addr_t phys)
7032{
7033 struct lpfc_dmabuf *mp, *next_mp;
7034 struct list_head *slp = &pring->postbufq;
7035
7036 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007037 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007038 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7039 if (mp->phys == phys) {
7040 list_del_init(&mp->list);
7041 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007042 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007043 return mp;
7044 }
7045 }
7046
James Smart2e0fef82007-06-17 19:56:36 -05007047 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007049 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007050 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007051 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007052 slp->next, slp->prev, pring->postbufq_cnt);
7053 return NULL;
7054}
7055
James Smarte59058c2008-08-24 21:49:00 -04007056/**
James Smart3621a712009-04-06 18:47:14 -04007057 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007058 * @phba: Pointer to HBA context object.
7059 * @cmdiocb: Pointer to driver command iocb object.
7060 * @rspiocb: Pointer to driver response iocb object.
7061 *
7062 * This function is the completion handler for the abort iocbs for
7063 * ELS commands. This function is called from the ELS ring event
7064 * handler with no lock held. This function frees memory resources
7065 * associated with the abort iocb.
7066 **/
dea31012005-04-17 16:05:31 -05007067static void
James Smart2e0fef82007-06-17 19:56:36 -05007068lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7069 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007070{
James Smart2e0fef82007-06-17 19:56:36 -05007071 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007072 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007073 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007074 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7075
7076 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007077
7078 if (irsp->ulpStatus) {
7079 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7080 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7081
James Smart2e0fef82007-06-17 19:56:36 -05007082 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007083 if (phba->sli_rev < LPFC_SLI_REV4) {
7084 if (abort_iotag != 0 &&
7085 abort_iotag <= phba->sli.last_iotag)
7086 abort_iocb =
7087 phba->sli.iocbq_lookup[abort_iotag];
7088 } else
7089 /* For sli4 the abort_tag is the XRI,
7090 * so the abort routine puts the iotag of the iocb
7091 * being aborted in the context field of the abort
7092 * IOCB.
7093 */
7094 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007095
James Smart92d7f7b2007-06-17 19:56:38 -05007096 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007097 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007098 "with tag %x context %x, abort status %x, "
7099 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007100 abort_iocb, abort_iotag, abort_context,
7101 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007102
7103 /*
James Smart58da1ff2008-04-07 10:15:56 -04007104 * If the iocb is not found in Firmware queue the iocb
7105 * might have completed already. Do not free it again.
7106 */
James Smart9b379602008-04-07 10:16:00 -04007107 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007108 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7109 spin_unlock_irq(&phba->hbalock);
7110 lpfc_sli_release_iocbq(phba, cmdiocb);
7111 return;
7112 }
7113 /* For SLI4 the ulpContext field for abort IOCB
7114 * holds the iotag of the IOCB being aborted so
7115 * the local abort_context needs to be reset to
7116 * match the aborted IOCBs ulpContext.
7117 */
7118 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7119 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007120 }
7121 /*
James Smart2680eea2007-04-25 09:52:55 -04007122 * make sure we have the right iocbq before taking it
7123 * off the txcmplq and try to call completion routine.
7124 */
James Smart2e0fef82007-06-17 19:56:36 -05007125 if (!abort_iocb ||
7126 abort_iocb->iocb.ulpContext != abort_context ||
7127 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7128 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007129 else if (phba->sli_rev < LPFC_SLI_REV4) {
7130 /*
7131 * leave the SLI4 aborted command on the txcmplq
7132 * list and the command complete WCQE's XB bit
7133 * will tell whether the SGL (XRI) can be released
7134 * immediately or to the aborted SGL list for the
7135 * following abort XRI from the HBA.
7136 */
James Smart92d7f7b2007-06-17 19:56:38 -05007137 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007138 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007139 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007140
James Smart0ff10d42008-01-11 01:52:36 -05007141 /* Firmware could still be in progress of DMAing
7142 * payload, so don't free data buffer till after
7143 * a hbeat.
7144 */
James Smart341af102010-01-26 23:07:37 -05007145 spin_lock_irq(&phba->hbalock);
James Smart0ff10d42008-01-11 01:52:36 -05007146 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007147 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007148 spin_unlock_irq(&phba->hbalock);
7149
James Smart92d7f7b2007-06-17 19:56:38 -05007150 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007151 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007152 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007153 }
7154 }
7155
James Bottomley604a3e32005-10-29 10:28:33 -05007156 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007157 return;
7158}
7159
James Smarte59058c2008-08-24 21:49:00 -04007160/**
James Smart3621a712009-04-06 18:47:14 -04007161 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007162 * @phba: Pointer to HBA context object.
7163 * @cmdiocb: Pointer to driver command iocb object.
7164 * @rspiocb: Pointer to driver response iocb object.
7165 *
7166 * The function is called from SLI ring event handler with no
7167 * lock held. This function is the completion handler for ELS commands
7168 * which are aborted. The function frees memory resources used for
7169 * the aborted ELS commands.
7170 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007171static void
7172lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7173 struct lpfc_iocbq *rspiocb)
7174{
7175 IOCB_t *irsp = &rspiocb->iocb;
7176
7177 /* ELS cmd tag <ulpIoTag> completes */
7178 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007179 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007180 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007181 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007182 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007183 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7184 lpfc_ct_free_iocb(phba, cmdiocb);
7185 else
7186 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007187 return;
7188}
7189
James Smarte59058c2008-08-24 21:49:00 -04007190/**
James Smart3621a712009-04-06 18:47:14 -04007191 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007192 * @phba: Pointer to HBA context object.
7193 * @pring: Pointer to driver SLI ring object.
7194 * @cmdiocb: Pointer to driver command iocb object.
7195 *
7196 * This function issues an abort iocb for the provided command
7197 * iocb. This function is called with hbalock held.
7198 * The function returns 0 when it fails due to memory allocation
7199 * failure or when the command iocb is an abort request.
7200 **/
dea31012005-04-17 16:05:31 -05007201int
James Smart2e0fef82007-06-17 19:56:36 -05007202lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7203 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007204{
James Smart2e0fef82007-06-17 19:56:36 -05007205 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007206 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007207 IOCB_t *icmd = NULL;
7208 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007209 int retval = IOCB_ERROR;
7210
James Smart92d7f7b2007-06-17 19:56:38 -05007211 /*
7212 * There are certain command types we don't want to abort. And we
7213 * don't want to abort commands that are already in the process of
7214 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007215 */
7216 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007217 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007218 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7219 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007220 return 0;
7221
James Smart858c9f62007-06-17 19:56:39 -05007222 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7223 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007224 */
James Smart858c9f62007-06-17 19:56:39 -05007225 if ((vport->load_flag & FC_UNLOADING) &&
7226 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007227 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7228 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7229 else
7230 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007231 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007232 }
dea31012005-04-17 16:05:31 -05007233
7234 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007235 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007236 if (abtsiocbp == NULL)
7237 return 0;
dea31012005-04-17 16:05:31 -05007238
James Smart07951072007-04-25 09:51:38 -04007239 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007240 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007241 */
7242 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7243
dea31012005-04-17 16:05:31 -05007244 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007245 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7246 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007247 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007248 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007249 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7250 }
James Smartda0436e2009-05-22 14:51:39 -04007251 else
7252 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007253 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007254 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007255
James Smart5ffc2662009-11-18 15:39:44 -05007256 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7257 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007258 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7259 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007260
James Smart2e0fef82007-06-17 19:56:36 -05007261 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007262 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7263 else
7264 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7265
7266 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007267
James Smarte8b62012007-08-02 11:10:09 -04007268 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7269 "0339 Abort xri x%x, original iotag x%x, "
7270 "abort cmd iotag x%x\n",
7271 iabt->un.acxri.abortContextTag,
7272 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007273 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007274
James Smartd7c255b2008-08-24 21:50:00 -04007275 if (retval)
7276 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007277abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007278 /*
7279 * Caller to this routine should check for IOCB_ERROR
7280 * and handle it properly. This routine no longer removes
7281 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007282 */
James Smart2e0fef82007-06-17 19:56:36 -05007283 return retval;
dea31012005-04-17 16:05:31 -05007284}
7285
James Smarte59058c2008-08-24 21:49:00 -04007286/**
James Smart3621a712009-04-06 18:47:14 -04007287 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007288 * @iocbq: Pointer to driver iocb object.
7289 * @vport: Pointer to driver virtual port object.
7290 * @tgt_id: SCSI ID of the target.
7291 * @lun_id: LUN ID of the scsi device.
7292 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7293 *
James Smart3621a712009-04-06 18:47:14 -04007294 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007295 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7296 * 0 if the filtering criteria is met for the given iocb and will return
7297 * 1 if the filtering criteria is not met.
7298 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7299 * given iocb is for the SCSI device specified by vport, tgt_id and
7300 * lun_id parameter.
7301 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7302 * given iocb is for the SCSI target specified by vport and tgt_id
7303 * parameters.
7304 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7305 * given iocb is for the SCSI host associated with the given vport.
7306 * This function is called with no locks held.
7307 **/
dea31012005-04-17 16:05:31 -05007308static int
James Smart51ef4c22007-08-02 11:10:31 -04007309lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7310 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007311 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007312{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007313 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007314 int rc = 1;
7315
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007316 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7317 return rc;
7318
James Smart51ef4c22007-08-02 11:10:31 -04007319 if (iocbq->vport != vport)
7320 return rc;
7321
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007322 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007323
James Smart495a7142008-06-14 22:52:59 -04007324 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007325 return rc;
7326
7327 switch (ctx_cmd) {
7328 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007329 if ((lpfc_cmd->rdata->pnode) &&
7330 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7331 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007332 rc = 0;
7333 break;
7334 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007335 if ((lpfc_cmd->rdata->pnode) &&
7336 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007337 rc = 0;
7338 break;
dea31012005-04-17 16:05:31 -05007339 case LPFC_CTX_HOST:
7340 rc = 0;
7341 break;
7342 default:
7343 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007344 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007345 break;
7346 }
7347
7348 return rc;
7349}
7350
James Smarte59058c2008-08-24 21:49:00 -04007351/**
James Smart3621a712009-04-06 18:47:14 -04007352 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007353 * @vport: Pointer to virtual port.
7354 * @tgt_id: SCSI ID of the target.
7355 * @lun_id: LUN ID of the scsi device.
7356 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7357 *
7358 * This function returns number of FCP commands pending for the vport.
7359 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7360 * commands pending on the vport associated with SCSI device specified
7361 * by tgt_id and lun_id parameters.
7362 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7363 * commands pending on the vport associated with SCSI target specified
7364 * by tgt_id parameter.
7365 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7366 * commands pending on the vport.
7367 * This function returns the number of iocbs which satisfy the filter.
7368 * This function is called without any lock held.
7369 **/
dea31012005-04-17 16:05:31 -05007370int
James Smart51ef4c22007-08-02 11:10:31 -04007371lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7372 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007373{
James Smart51ef4c22007-08-02 11:10:31 -04007374 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007375 struct lpfc_iocbq *iocbq;
7376 int sum, i;
dea31012005-04-17 16:05:31 -05007377
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007378 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7379 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007380
James Smart51ef4c22007-08-02 11:10:31 -04007381 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7382 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007383 sum++;
dea31012005-04-17 16:05:31 -05007384 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007385
dea31012005-04-17 16:05:31 -05007386 return sum;
7387}
7388
James Smarte59058c2008-08-24 21:49:00 -04007389/**
James Smart3621a712009-04-06 18:47:14 -04007390 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007391 * @phba: Pointer to HBA context object
7392 * @cmdiocb: Pointer to command iocb object.
7393 * @rspiocb: Pointer to response iocb object.
7394 *
7395 * This function is called when an aborted FCP iocb completes. This
7396 * function is called by the ring event handler with no lock held.
7397 * This function frees the iocb.
7398 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007399void
James Smart2e0fef82007-06-17 19:56:36 -05007400lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7401 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007402{
James Bottomley604a3e32005-10-29 10:28:33 -05007403 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007404 return;
7405}
7406
James Smarte59058c2008-08-24 21:49:00 -04007407/**
James Smart3621a712009-04-06 18:47:14 -04007408 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007409 * @vport: Pointer to virtual port.
7410 * @pring: Pointer to driver SLI ring object.
7411 * @tgt_id: SCSI ID of the target.
7412 * @lun_id: LUN ID of the scsi device.
7413 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7414 *
7415 * This function sends an abort command for every SCSI command
7416 * associated with the given virtual port pending on the ring
7417 * filtered by lpfc_sli_validate_fcp_iocb function.
7418 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7419 * FCP iocbs associated with lun specified by tgt_id and lun_id
7420 * parameters
7421 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7422 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7423 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7424 * FCP iocbs associated with virtual port.
7425 * This function returns number of iocbs it failed to abort.
7426 * This function is called with no locks held.
7427 **/
dea31012005-04-17 16:05:31 -05007428int
James Smart51ef4c22007-08-02 11:10:31 -04007429lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7430 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007431{
James Smart51ef4c22007-08-02 11:10:31 -04007432 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007433 struct lpfc_iocbq *iocbq;
7434 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007435 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007436 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007437 int i;
dea31012005-04-17 16:05:31 -05007438
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007439 for (i = 1; i <= phba->sli.last_iotag; i++) {
7440 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007441
James Smart51ef4c22007-08-02 11:10:31 -04007442 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007443 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007444 continue;
7445
7446 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007447 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007448 if (abtsiocb == NULL) {
7449 errcnt++;
7450 continue;
7451 }
dea31012005-04-17 16:05:31 -05007452
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007453 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007454 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7455 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007456 if (phba->sli_rev == LPFC_SLI_REV4)
7457 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7458 else
7459 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007460 abtsiocb->iocb.ulpLe = 1;
7461 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007462 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007463
James Smart5ffc2662009-11-18 15:39:44 -05007464 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7465 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007466 if (iocbq->iocb_flag & LPFC_IO_FCP)
7467 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007468
James Smart2e0fef82007-06-17 19:56:36 -05007469 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007470 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7471 else
7472 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7473
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007474 /* Setup callback routine and issue the command. */
7475 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007476 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7477 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007478 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007479 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007480 errcnt++;
7481 continue;
7482 }
7483 }
7484
7485 return errcnt;
7486}
7487
James Smarte59058c2008-08-24 21:49:00 -04007488/**
James Smart3621a712009-04-06 18:47:14 -04007489 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007490 * @phba: Pointer to HBA context object.
7491 * @cmdiocbq: Pointer to command iocb.
7492 * @rspiocbq: Pointer to response iocb.
7493 *
7494 * This function is the completion handler for iocbs issued using
7495 * lpfc_sli_issue_iocb_wait function. This function is called by the
7496 * ring event handler function without any lock held. This function
7497 * can be called from both worker thread context and interrupt
7498 * context. This function also can be called from other thread which
7499 * cleans up the SLI layer objects.
7500 * This function copy the contents of the response iocb to the
7501 * response iocb memory object provided by the caller of
7502 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7503 * sleeps for the iocb completion.
7504 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007505static void
7506lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7507 struct lpfc_iocbq *cmdiocbq,
7508 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007509{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007510 wait_queue_head_t *pdone_q;
7511 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007512 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007513
James Smart2e0fef82007-06-17 19:56:36 -05007514 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007515 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7516 if (cmdiocbq->context2 && rspiocbq)
7517 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7518 &rspiocbq->iocb, sizeof(IOCB_t));
7519
James Smart0f65ff62010-02-26 14:14:23 -05007520 /* Set the exchange busy flag for task management commands */
7521 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7522 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7523 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7524 cur_iocbq);
7525 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7526 }
7527
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007528 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007529 if (pdone_q)
7530 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007531 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007532 return;
7533}
7534
James Smarte59058c2008-08-24 21:49:00 -04007535/**
James Smartd11e31d2009-06-10 17:23:06 -04007536 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7537 * @phba: Pointer to HBA context object..
7538 * @piocbq: Pointer to command iocb.
7539 * @flag: Flag to test.
7540 *
7541 * This routine grabs the hbalock and then test the iocb_flag to
7542 * see if the passed in flag is set.
7543 * Returns:
7544 * 1 if flag is set.
7545 * 0 if flag is not set.
7546 **/
7547static int
7548lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7549 struct lpfc_iocbq *piocbq, uint32_t flag)
7550{
7551 unsigned long iflags;
7552 int ret;
7553
7554 spin_lock_irqsave(&phba->hbalock, iflags);
7555 ret = piocbq->iocb_flag & flag;
7556 spin_unlock_irqrestore(&phba->hbalock, iflags);
7557 return ret;
7558
7559}
7560
7561/**
James Smart3621a712009-04-06 18:47:14 -04007562 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007563 * @phba: Pointer to HBA context object..
7564 * @pring: Pointer to sli ring.
7565 * @piocb: Pointer to command iocb.
7566 * @prspiocbq: Pointer to response iocb.
7567 * @timeout: Timeout in number of seconds.
7568 *
7569 * This function issues the iocb to firmware and waits for the
7570 * iocb to complete. If the iocb command is not
7571 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7572 * Caller should not free the iocb resources if this function
7573 * returns IOCB_TIMEDOUT.
7574 * The function waits for the iocb completion using an
7575 * non-interruptible wait.
7576 * This function will sleep while waiting for iocb completion.
7577 * So, this function should not be called from any context which
7578 * does not allow sleeping. Due to the same reason, this function
7579 * cannot be called with interrupt disabled.
7580 * This function assumes that the iocb completions occur while
7581 * this function sleep. So, this function cannot be called from
7582 * the thread which process iocb completion for this ring.
7583 * This function clears the iocb_flag of the iocb object before
7584 * issuing the iocb and the iocb completion handler sets this
7585 * flag and wakes this thread when the iocb completes.
7586 * The contents of the response iocb will be copied to prspiocbq
7587 * by the completion handler when the command completes.
7588 * This function returns IOCB_SUCCESS when success.
7589 * This function is called with no lock held.
7590 **/
dea31012005-04-17 16:05:31 -05007591int
James Smart2e0fef82007-06-17 19:56:36 -05007592lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007593 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007594 struct lpfc_iocbq *piocb,
7595 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007596 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007597{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007598 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007599 long timeleft, timeout_req = 0;
7600 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007601 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007602
7603 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007604 * If the caller has provided a response iocbq buffer, then context2
7605 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007606 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007607 if (prspiocbq) {
7608 if (piocb->context2)
7609 return IOCB_ERROR;
7610 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007611 }
7612
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007613 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7614 piocb->context_un.wait_queue = &done_q;
7615 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007616
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007617 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7618 creg_val = readl(phba->HCregaddr);
7619 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7620 writel(creg_val, phba->HCregaddr);
7621 readl(phba->HCregaddr); /* flush */
7622 }
7623
James Smartda0436e2009-05-22 14:51:39 -04007624 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007625 if (retval == IOCB_SUCCESS) {
7626 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007627 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007628 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007629 timeout_req);
dea31012005-04-17 16:05:31 -05007630
James Smart7054a602007-04-25 09:52:34 -04007631 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7632 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007633 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007634 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007636 "0338 IOCB wait timeout error - no "
7637 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007638 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007639 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007641 "0330 IOCB wake NOT set, "
7642 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007643 timeout, (timeleft / jiffies));
7644 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007645 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007646 } else {
7647 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007648 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007649 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007650 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007651 }
7652
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007653 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7654 creg_val = readl(phba->HCregaddr);
7655 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7656 writel(creg_val, phba->HCregaddr);
7657 readl(phba->HCregaddr); /* flush */
7658 }
7659
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007660 if (prspiocbq)
7661 piocb->context2 = NULL;
7662
7663 piocb->context_un.wait_queue = NULL;
7664 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007665 return retval;
7666}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007667
James Smarte59058c2008-08-24 21:49:00 -04007668/**
James Smart3621a712009-04-06 18:47:14 -04007669 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007670 * @phba: Pointer to HBA context object.
7671 * @pmboxq: Pointer to driver mailbox object.
7672 * @timeout: Timeout in number of seconds.
7673 *
7674 * This function issues the mailbox to firmware and waits for the
7675 * mailbox command to complete. If the mailbox command is not
7676 * completed within timeout seconds, it returns MBX_TIMEOUT.
7677 * The function waits for the mailbox completion using an
7678 * interruptible wait. If the thread is woken up due to a
7679 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7680 * should not free the mailbox resources, if this function returns
7681 * MBX_TIMEOUT.
7682 * This function will sleep while waiting for mailbox completion.
7683 * So, this function should not be called from any context which
7684 * does not allow sleeping. Due to the same reason, this function
7685 * cannot be called with interrupt disabled.
7686 * This function assumes that the mailbox completion occurs while
7687 * this function sleep. So, this function cannot be called from
7688 * the worker thread which processes mailbox completion.
7689 * This function is called in the context of HBA management
7690 * applications.
7691 * This function returns MBX_SUCCESS when successful.
7692 * This function is called with no lock held.
7693 **/
dea31012005-04-17 16:05:31 -05007694int
James Smart2e0fef82007-06-17 19:56:36 -05007695lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007696 uint32_t timeout)
7697{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007698 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007699 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007700 unsigned long flag;
dea31012005-04-17 16:05:31 -05007701
7702 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007703 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007704 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007705
James Smart495a7142008-06-14 22:52:59 -04007706 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007707 /* setup wake call as IOCB callback */
7708 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7709 /* setup context field to pass wait_queue pointer to wake function */
7710 pmboxq->context1 = &done_q;
7711
dea31012005-04-17 16:05:31 -05007712 /* now issue the command */
7713 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7714
7715 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007716 wait_event_interruptible_timeout(done_q,
7717 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7718 timeout * HZ);
7719
James Smart858c9f62007-06-17 19:56:39 -05007720 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007721 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007722 /*
7723 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7724 * else do not free the resources.
7725 */
7726 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007727 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007728 else {
James Smart7054a602007-04-25 09:52:34 -04007729 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007730 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7731 }
7732 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007733 }
7734
dea31012005-04-17 16:05:31 -05007735 return retval;
7736}
7737
James Smarte59058c2008-08-24 21:49:00 -04007738/**
James Smart3772a992009-05-22 14:50:54 -04007739 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007740 * @phba: Pointer to HBA context.
7741 *
James Smart3772a992009-05-22 14:50:54 -04007742 * This function is called to shutdown the driver's mailbox sub-system.
7743 * It first marks the mailbox sub-system is in a block state to prevent
7744 * the asynchronous mailbox command from issued off the pending mailbox
7745 * command queue. If the mailbox command sub-system shutdown is due to
7746 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7747 * the mailbox sub-system flush routine to forcefully bring down the
7748 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7749 * as with offline or HBA function reset), this routine will wait for the
7750 * outstanding mailbox command to complete before invoking the mailbox
7751 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007752 **/
James Smart3772a992009-05-22 14:50:54 -04007753void
7754lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007755{
James Smart3772a992009-05-22 14:50:54 -04007756 struct lpfc_sli *psli = &phba->sli;
7757 uint8_t actcmd = MBX_HEARTBEAT;
7758 unsigned long timeout;
7759
7760 spin_lock_irq(&phba->hbalock);
7761 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7762 spin_unlock_irq(&phba->hbalock);
7763
7764 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7765 spin_lock_irq(&phba->hbalock);
7766 if (phba->sli.mbox_active)
7767 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7768 spin_unlock_irq(&phba->hbalock);
7769 /* Determine how long we might wait for the active mailbox
7770 * command to be gracefully completed by firmware.
7771 */
7772 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7773 1000) + jiffies;
7774 while (phba->sli.mbox_active) {
7775 /* Check active mailbox complete status every 2ms */
7776 msleep(2);
7777 if (time_after(jiffies, timeout))
7778 /* Timeout, let the mailbox flush routine to
7779 * forcefully release active mailbox command
7780 */
7781 break;
7782 }
7783 }
7784 lpfc_sli_mbox_sys_flush(phba);
7785}
7786
7787/**
7788 * lpfc_sli_eratt_read - read sli-3 error attention events
7789 * @phba: Pointer to HBA context.
7790 *
7791 * This function is called to read the SLI3 device error attention registers
7792 * for possible error attention events. The caller must hold the hostlock
7793 * with spin_lock_irq().
7794 *
7795 * This fucntion returns 1 when there is Error Attention in the Host Attention
7796 * Register and returns 0 otherwise.
7797 **/
7798static int
7799lpfc_sli_eratt_read(struct lpfc_hba *phba)
7800{
James Smarted957682007-06-17 19:56:37 -05007801 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007802
James Smart3772a992009-05-22 14:50:54 -04007803 /* Read chip Host Attention (HA) register */
7804 ha_copy = readl(phba->HAregaddr);
7805 if (ha_copy & HA_ERATT) {
7806 /* Read host status register to retrieve error event */
7807 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007808
James Smart3772a992009-05-22 14:50:54 -04007809 /* Check if there is a deferred error condition is active */
7810 if ((HS_FFER1 & phba->work_hs) &&
7811 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7812 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007813 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007814 /* Clear all interrupt enable conditions */
7815 writel(0, phba->HCregaddr);
7816 readl(phba->HCregaddr);
7817 }
7818
7819 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007820 phba->work_ha |= HA_ERATT;
7821 /* Indicate polling handles this ERATT */
7822 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007823 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007824 }
James Smart3772a992009-05-22 14:50:54 -04007825 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007826}
7827
James Smarte59058c2008-08-24 21:49:00 -04007828/**
James Smartda0436e2009-05-22 14:51:39 -04007829 * lpfc_sli4_eratt_read - read sli-4 error attention events
7830 * @phba: Pointer to HBA context.
7831 *
7832 * This function is called to read the SLI4 device error attention registers
7833 * for possible error attention events. The caller must hold the hostlock
7834 * with spin_lock_irq().
7835 *
7836 * This fucntion returns 1 when there is Error Attention in the Host Attention
7837 * Register and returns 0 otherwise.
7838 **/
7839static int
7840lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7841{
7842 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007843
7844 /* For now, use the SLI4 device internal unrecoverable error
7845 * registers for error attention. This can be changed later.
7846 */
James Smarta747c9c2009-11-18 15:41:10 -05007847 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7848 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7849 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7850 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7852 "1423 HBA Unrecoverable error: "
7853 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7854 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7855 uerr_sta_lo, uerr_sta_hi,
7856 phba->sli4_hba.ue_mask_lo,
7857 phba->sli4_hba.ue_mask_hi);
7858 phba->work_status[0] = uerr_sta_lo;
7859 phba->work_status[1] = uerr_sta_hi;
7860 /* Set the driver HA work bitmap */
7861 phba->work_ha |= HA_ERATT;
7862 /* Indicate polling handles this ERATT */
7863 phba->hba_flag |= HBA_ERATT_HANDLED;
7864 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007865 }
7866 return 0;
7867}
7868
7869/**
James Smart3621a712009-04-06 18:47:14 -04007870 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007871 * @phba: Pointer to HBA context.
7872 *
James Smart3772a992009-05-22 14:50:54 -04007873 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007874 * error attention register bit for error attention events.
7875 *
7876 * This fucntion returns 1 when there is Error Attention in the Host Attention
7877 * Register and returns 0 otherwise.
7878 **/
7879int
7880lpfc_sli_check_eratt(struct lpfc_hba *phba)
7881{
7882 uint32_t ha_copy;
7883
7884 /* If somebody is waiting to handle an eratt, don't process it
7885 * here. The brdkill function will do this.
7886 */
7887 if (phba->link_flag & LS_IGNORE_ERATT)
7888 return 0;
7889
7890 /* Check if interrupt handler handles this ERATT */
7891 spin_lock_irq(&phba->hbalock);
7892 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7893 /* Interrupt handler has handled ERATT */
7894 spin_unlock_irq(&phba->hbalock);
7895 return 0;
7896 }
7897
James Smarta257bf92009-04-06 18:48:10 -04007898 /*
7899 * If there is deferred error attention, do not check for error
7900 * attention
7901 */
7902 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7903 spin_unlock_irq(&phba->hbalock);
7904 return 0;
7905 }
7906
James Smart3772a992009-05-22 14:50:54 -04007907 /* If PCI channel is offline, don't process it */
7908 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007909 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007910 return 0;
7911 }
7912
7913 switch (phba->sli_rev) {
7914 case LPFC_SLI_REV2:
7915 case LPFC_SLI_REV3:
7916 /* Read chip Host Attention (HA) register */
7917 ha_copy = lpfc_sli_eratt_read(phba);
7918 break;
James Smartda0436e2009-05-22 14:51:39 -04007919 case LPFC_SLI_REV4:
7920 /* Read devcie Uncoverable Error (UERR) registers */
7921 ha_copy = lpfc_sli4_eratt_read(phba);
7922 break;
James Smart3772a992009-05-22 14:50:54 -04007923 default:
7924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7925 "0299 Invalid SLI revision (%d)\n",
7926 phba->sli_rev);
7927 ha_copy = 0;
7928 break;
James Smart93996272008-08-24 21:50:30 -04007929 }
7930 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007931
7932 return ha_copy;
7933}
7934
7935/**
7936 * lpfc_intr_state_check - Check device state for interrupt handling
7937 * @phba: Pointer to HBA context.
7938 *
7939 * This inline routine checks whether a device or its PCI slot is in a state
7940 * that the interrupt should be handled.
7941 *
7942 * This function returns 0 if the device or the PCI slot is in a state that
7943 * interrupt should be handled, otherwise -EIO.
7944 */
7945static inline int
7946lpfc_intr_state_check(struct lpfc_hba *phba)
7947{
7948 /* If the pci channel is offline, ignore all the interrupts */
7949 if (unlikely(pci_channel_offline(phba->pcidev)))
7950 return -EIO;
7951
7952 /* Update device level interrupt statistics */
7953 phba->sli.slistat.sli_intr++;
7954
7955 /* Ignore all interrupts during initialization. */
7956 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7957 return -EIO;
7958
James Smart93996272008-08-24 21:50:30 -04007959 return 0;
7960}
7961
7962/**
James Smart3772a992009-05-22 14:50:54 -04007963 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007964 * @irq: Interrupt number.
7965 * @dev_id: The device context pointer.
7966 *
James Smart93996272008-08-24 21:50:30 -04007967 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007968 * service routine when device with SLI-3 interface spec is enabled with
7969 * MSI-X multi-message interrupt mode and there are slow-path events in
7970 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7971 * interrupt mode, this function is called as part of the device-level
7972 * interrupt handler. When the PCI slot is in error recovery or the HBA
7973 * is undergoing initialization, the interrupt handler will not process
7974 * the interrupt. The link attention and ELS ring attention events are
7975 * handled by the worker thread. The interrupt handler signals the worker
7976 * thread and returns for these events. This function is called without
7977 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007978 * structures.
7979 *
7980 * This function returns IRQ_HANDLED when interrupt is handled else it
7981 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007982 **/
dea31012005-04-17 16:05:31 -05007983irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007984lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007985{
James Smart2e0fef82007-06-17 19:56:36 -05007986 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007987 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007988 uint32_t work_ha_copy;
7989 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007990 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007991 uint32_t control;
7992
James Smart92d7f7b2007-06-17 19:56:38 -05007993 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007994 struct lpfc_vport *vport;
7995 struct lpfc_nodelist *ndlp;
7996 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007997 LPFC_MBOXQ_t *pmb;
7998 int rc;
7999
dea31012005-04-17 16:05:31 -05008000 /*
8001 * Get the driver's phba structure from the dev_id and
8002 * assume the HBA is not interrupting.
8003 */
James Smart93996272008-08-24 21:50:30 -04008004 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008005
8006 if (unlikely(!phba))
8007 return IRQ_NONE;
8008
dea31012005-04-17 16:05:31 -05008009 /*
James Smart93996272008-08-24 21:50:30 -04008010 * Stuff needs to be attented to when this function is invoked as an
8011 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008012 */
James Smart93996272008-08-24 21:50:30 -04008013 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008014 /* Check device state for handling interrupt */
8015 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008016 return IRQ_NONE;
8017 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008018 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008019 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008020 /* If somebody is waiting to handle an eratt don't process it
8021 * here. The brdkill function will do this.
8022 */
8023 if (phba->link_flag & LS_IGNORE_ERATT)
8024 ha_copy &= ~HA_ERATT;
8025 /* Check the need for handling ERATT in interrupt handler */
8026 if (ha_copy & HA_ERATT) {
8027 if (phba->hba_flag & HBA_ERATT_HANDLED)
8028 /* ERATT polling has handled ERATT */
8029 ha_copy &= ~HA_ERATT;
8030 else
8031 /* Indicate interrupt handler handles ERATT */
8032 phba->hba_flag |= HBA_ERATT_HANDLED;
8033 }
James Smarta257bf92009-04-06 18:48:10 -04008034
8035 /*
8036 * If there is deferred error attention, do not check for any
8037 * interrupt.
8038 */
8039 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008040 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008041 return IRQ_NONE;
8042 }
8043
James Smart93996272008-08-24 21:50:30 -04008044 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008045 hc_copy = readl(phba->HCregaddr);
8046 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8047 HC_LAINT_ENA | HC_ERINT_ENA),
8048 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008049 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8050 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008051 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008052 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008053 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008054 } else
8055 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008056
dea31012005-04-17 16:05:31 -05008057 work_ha_copy = ha_copy & phba->work_ha_mask;
8058
James Smart93996272008-08-24 21:50:30 -04008059 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008060 if (work_ha_copy & HA_LATT) {
8061 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8062 /*
8063 * Turn off Link Attention interrupts
8064 * until CLEAR_LA done
8065 */
James Smart5b75da22008-12-04 22:39:35 -05008066 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008067 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8068 control = readl(phba->HCregaddr);
8069 control &= ~HC_LAINT_ENA;
8070 writel(control, phba->HCregaddr);
8071 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008072 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008073 }
8074 else
8075 work_ha_copy &= ~HA_LATT;
8076 }
8077
James Smart93996272008-08-24 21:50:30 -04008078 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008079 /*
8080 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8081 * the only slow ring.
8082 */
8083 status = (work_ha_copy &
8084 (HA_RXMASK << (4*LPFC_ELS_RING)));
8085 status >>= (4*LPFC_ELS_RING);
8086 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008087 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008088 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008089
8090 lpfc_debugfs_slow_ring_trc(phba,
8091 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8092 control, status,
8093 (uint32_t)phba->sli.slistat.sli_intr);
8094
James Smart858c9f62007-06-17 19:56:39 -05008095 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008096 lpfc_debugfs_slow_ring_trc(phba,
8097 "ISR Disable ring:"
8098 "pwork:x%x hawork:x%x wait:x%x",
8099 phba->work_ha, work_ha_copy,
8100 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008101 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008102
James Smart858c9f62007-06-17 19:56:39 -05008103 control &=
8104 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008105 writel(control, phba->HCregaddr);
8106 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008107 }
James Smarta58cbd52007-08-02 11:09:43 -04008108 else {
8109 lpfc_debugfs_slow_ring_trc(phba,
8110 "ISR slow ring: pwork:"
8111 "x%x hawork:x%x wait:x%x",
8112 phba->work_ha, work_ha_copy,
8113 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008114 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008115 }
James Smart5b75da22008-12-04 22:39:35 -05008116 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008117 }
8118 }
James Smart5b75da22008-12-04 22:39:35 -05008119 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008120 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008121 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008122 /*
8123 * Check if there is a deferred error condition
8124 * is active
8125 */
8126 if ((HS_FFER1 & phba->work_hs) &&
8127 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8128 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8129 phba->hba_flag |= DEFER_ERATT;
8130 /* Clear all interrupt enable conditions */
8131 writel(0, phba->HCregaddr);
8132 readl(phba->HCregaddr);
8133 }
8134 }
8135
James Smart93996272008-08-24 21:50:30 -04008136 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008137 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008138 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008139 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008140 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008141
8142 /* First check out the status word */
8143 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8144 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008145 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008146 /*
8147 * Stray Mailbox Interrupt, mbxCommand <cmd>
8148 * mbxStatus <status>
8149 */
James Smart09372822008-01-11 01:52:54 -05008150 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008151 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008152 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008153 "Interrupt mbxCommand x%x "
8154 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008155 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008156 pmbox->mbxCommand,
8157 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008158 /* clear mailbox attention bit */
8159 work_ha_copy &= ~HA_MBATT;
8160 } else {
James Smart97eab632008-04-07 10:16:05 -04008161 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008162 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008163 phba->last_completion_time = jiffies;
8164 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008165 if (pmb->mbox_cmpl) {
8166 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8167 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008168 if (pmb->out_ext_byte_len &&
8169 pmb->context2)
8170 lpfc_sli_pcimem_bcopy(
8171 phba->mbox_ext,
8172 pmb->context2,
8173 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008174 }
James Smart09372822008-01-11 01:52:54 -05008175 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8176 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8177
8178 lpfc_debugfs_disc_trc(vport,
8179 LPFC_DISC_TRC_MBOX_VPORT,
8180 "MBOX dflt rpi: : "
8181 "status:x%x rpi:x%x",
8182 (uint32_t)pmbox->mbxStatus,
8183 pmbox->un.varWords[0], 0);
8184
8185 if (!pmbox->mbxStatus) {
8186 mp = (struct lpfc_dmabuf *)
8187 (pmb->context1);
8188 ndlp = (struct lpfc_nodelist *)
8189 pmb->context2;
8190
8191 /* Reg_LOGIN of dflt RPI was
8192 * successful. new lets get
8193 * rid of the RPI using the
8194 * same mbox buffer.
8195 */
8196 lpfc_unreg_login(phba,
8197 vport->vpi,
8198 pmbox->un.varWords[0],
8199 pmb);
8200 pmb->mbox_cmpl =
8201 lpfc_mbx_cmpl_dflt_rpi;
8202 pmb->context1 = mp;
8203 pmb->context2 = ndlp;
8204 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008205 rc = lpfc_sli_issue_mbox(phba,
8206 pmb,
8207 MBX_NOWAIT);
8208 if (rc != MBX_BUSY)
8209 lpfc_printf_log(phba,
8210 KERN_ERR,
8211 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008212 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008213 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008214 if (rc != MBX_NOT_FINISHED)
8215 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008216 }
8217 }
James Smart5b75da22008-12-04 22:39:35 -05008218 spin_lock_irqsave(
8219 &phba->pport->work_port_lock,
8220 iflag);
James Smart09372822008-01-11 01:52:54 -05008221 phba->pport->work_port_events &=
8222 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008223 spin_unlock_irqrestore(
8224 &phba->pport->work_port_lock,
8225 iflag);
James Smart09372822008-01-11 01:52:54 -05008226 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008227 }
James Smart97eab632008-04-07 10:16:05 -04008228 } else
James Smart5b75da22008-12-04 22:39:35 -05008229 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008230
James Smart92d7f7b2007-06-17 19:56:38 -05008231 if ((work_ha_copy & HA_MBATT) &&
8232 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008233send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008234 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008235 do {
8236 rc = lpfc_sli_issue_mbox(phba, NULL,
8237 MBX_NOWAIT);
8238 } while (rc == MBX_NOT_FINISHED);
8239 if (rc != MBX_SUCCESS)
8240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8241 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008242 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008243 }
8244
James Smart5b75da22008-12-04 22:39:35 -05008245 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008246 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008247 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008248 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008249 }
James Smart93996272008-08-24 21:50:30 -04008250 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008251
James Smart3772a992009-05-22 14:50:54 -04008252} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008253
8254/**
James Smart3772a992009-05-22 14:50:54 -04008255 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008256 * @irq: Interrupt number.
8257 * @dev_id: The device context pointer.
8258 *
8259 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008260 * service routine when device with SLI-3 interface spec is enabled with
8261 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8262 * ring event in the HBA. However, when the device is enabled with either
8263 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8264 * device-level interrupt handler. When the PCI slot is in error recovery
8265 * or the HBA is undergoing initialization, the interrupt handler will not
8266 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8267 * the intrrupt context. This function is called without any lock held.
8268 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008269 *
8270 * This function returns IRQ_HANDLED when interrupt is handled else it
8271 * returns IRQ_NONE.
8272 **/
8273irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008274lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008275{
8276 struct lpfc_hba *phba;
8277 uint32_t ha_copy;
8278 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008279 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008280
8281 /* Get the driver's phba structure from the dev_id and
8282 * assume the HBA is not interrupting.
8283 */
8284 phba = (struct lpfc_hba *) dev_id;
8285
8286 if (unlikely(!phba))
8287 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008288
8289 /*
James Smart93996272008-08-24 21:50:30 -04008290 * Stuff needs to be attented to when this function is invoked as an
8291 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008292 */
James Smart93996272008-08-24 21:50:30 -04008293 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008294 /* Check device state for handling interrupt */
8295 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008296 return IRQ_NONE;
8297 /* Need to read HA REG for FCP ring and other ring events */
8298 ha_copy = readl(phba->HAregaddr);
8299 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008300 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008301 /*
8302 * If there is deferred error attention, do not check for
8303 * any interrupt.
8304 */
8305 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008306 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008307 return IRQ_NONE;
8308 }
James Smart93996272008-08-24 21:50:30 -04008309 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8310 phba->HAregaddr);
8311 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008313 } else
8314 ha_copy = phba->ha_copy;
8315
8316 /*
8317 * Process all events on FCP ring. Take the optimized path for FCP IO.
8318 */
8319 ha_copy &= ~(phba->work_ha_mask);
8320
8321 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008322 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008323 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008324 lpfc_sli_handle_fast_ring_event(phba,
8325 &phba->sli.ring[LPFC_FCP_RING],
8326 status);
James Smarta4bc3372006-12-02 13:34:16 -05008327
8328 if (phba->cfg_multi_ring_support == 2) {
8329 /*
James Smart93996272008-08-24 21:50:30 -04008330 * Process all events on extra ring. Take the optimized path
8331 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008332 */
James Smart93996272008-08-24 21:50:30 -04008333 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008334 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008335 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008336 lpfc_sli_handle_fast_ring_event(phba,
8337 &phba->sli.ring[LPFC_EXTRA_RING],
8338 status);
8339 }
8340 }
dea31012005-04-17 16:05:31 -05008341 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008342} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008343
James Smart93996272008-08-24 21:50:30 -04008344/**
James Smart3772a992009-05-22 14:50:54 -04008345 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008346 * @irq: Interrupt number.
8347 * @dev_id: The device context pointer.
8348 *
James Smart3772a992009-05-22 14:50:54 -04008349 * This function is the HBA device-level interrupt handler to device with
8350 * SLI-3 interface spec, called from the PCI layer when either MSI or
8351 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8352 * requires driver attention. This function invokes the slow-path interrupt
8353 * attention handling function and fast-path interrupt attention handling
8354 * function in turn to process the relevant HBA attention events. This
8355 * function is called without any lock held. It gets the hbalock to access
8356 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008357 *
8358 * This function returns IRQ_HANDLED when interrupt is handled, else it
8359 * returns IRQ_NONE.
8360 **/
8361irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008362lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008363{
8364 struct lpfc_hba *phba;
8365 irqreturn_t sp_irq_rc, fp_irq_rc;
8366 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008367 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008368
8369 /*
8370 * Get the driver's phba structure from the dev_id and
8371 * assume the HBA is not interrupting.
8372 */
8373 phba = (struct lpfc_hba *) dev_id;
8374
8375 if (unlikely(!phba))
8376 return IRQ_NONE;
8377
James Smart3772a992009-05-22 14:50:54 -04008378 /* Check device state for handling interrupt */
8379 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008380 return IRQ_NONE;
8381
8382 spin_lock(&phba->hbalock);
8383 phba->ha_copy = readl(phba->HAregaddr);
8384 if (unlikely(!phba->ha_copy)) {
8385 spin_unlock(&phba->hbalock);
8386 return IRQ_NONE;
8387 } else if (phba->ha_copy & HA_ERATT) {
8388 if (phba->hba_flag & HBA_ERATT_HANDLED)
8389 /* ERATT polling has handled ERATT */
8390 phba->ha_copy &= ~HA_ERATT;
8391 else
8392 /* Indicate interrupt handler handles ERATT */
8393 phba->hba_flag |= HBA_ERATT_HANDLED;
8394 }
8395
James Smarta257bf92009-04-06 18:48:10 -04008396 /*
8397 * If there is deferred error attention, do not check for any interrupt.
8398 */
8399 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8400 spin_unlock_irq(&phba->hbalock);
8401 return IRQ_NONE;
8402 }
8403
James Smart93996272008-08-24 21:50:30 -04008404 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008405 hc_copy = readl(phba->HCregaddr);
8406 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8407 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8408 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008409 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008410 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008411 readl(phba->HAregaddr); /* flush */
8412 spin_unlock(&phba->hbalock);
8413
8414 /*
8415 * Invokes slow-path host attention interrupt handling as appropriate.
8416 */
8417
8418 /* status of events with mailbox and link attention */
8419 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8420
8421 /* status of events with ELS ring */
8422 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8423 status2 >>= (4*LPFC_ELS_RING);
8424
8425 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008426 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008427 else
8428 sp_irq_rc = IRQ_NONE;
8429
8430 /*
8431 * Invoke fast-path host attention interrupt handling as appropriate.
8432 */
8433
8434 /* status of events with FCP ring */
8435 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8436 status1 >>= (4*LPFC_FCP_RING);
8437
8438 /* status of events with extra ring */
8439 if (phba->cfg_multi_ring_support == 2) {
8440 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8441 status2 >>= (4*LPFC_EXTRA_RING);
8442 } else
8443 status2 = 0;
8444
8445 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008446 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008447 else
8448 fp_irq_rc = IRQ_NONE;
8449
8450 /* Return device-level interrupt handling status */
8451 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008452} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008453
8454/**
8455 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8456 * @phba: pointer to lpfc hba data structure.
8457 *
8458 * This routine is invoked by the worker thread to process all the pending
8459 * SLI4 FCP abort XRI events.
8460 **/
8461void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8462{
8463 struct lpfc_cq_event *cq_event;
8464
8465 /* First, declare the fcp xri abort event has been handled */
8466 spin_lock_irq(&phba->hbalock);
8467 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8468 spin_unlock_irq(&phba->hbalock);
8469 /* Now, handle all the fcp xri abort events */
8470 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8471 /* Get the first event from the head of the event queue */
8472 spin_lock_irq(&phba->hbalock);
8473 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8474 cq_event, struct lpfc_cq_event, list);
8475 spin_unlock_irq(&phba->hbalock);
8476 /* Notify aborted XRI for FCP work queue */
8477 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8478 /* Free the event processed back to the free pool */
8479 lpfc_sli4_cq_event_release(phba, cq_event);
8480 }
8481}
8482
8483/**
8484 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8485 * @phba: pointer to lpfc hba data structure.
8486 *
8487 * This routine is invoked by the worker thread to process all the pending
8488 * SLI4 els abort xri events.
8489 **/
8490void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8491{
8492 struct lpfc_cq_event *cq_event;
8493
8494 /* First, declare the els xri abort event has been handled */
8495 spin_lock_irq(&phba->hbalock);
8496 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8497 spin_unlock_irq(&phba->hbalock);
8498 /* Now, handle all the els xri abort events */
8499 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8500 /* Get the first event from the head of the event queue */
8501 spin_lock_irq(&phba->hbalock);
8502 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8503 cq_event, struct lpfc_cq_event, list);
8504 spin_unlock_irq(&phba->hbalock);
8505 /* Notify aborted XRI for ELS work queue */
8506 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8507 /* Free the event processed back to the free pool */
8508 lpfc_sli4_cq_event_release(phba, cq_event);
8509 }
8510}
8511
James Smart341af102010-01-26 23:07:37 -05008512/**
8513 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8514 * @phba: pointer to lpfc hba data structure
8515 * @pIocbIn: pointer to the rspiocbq
8516 * @pIocbOut: pointer to the cmdiocbq
8517 * @wcqe: pointer to the complete wcqe
8518 *
8519 * This routine transfers the fields of a command iocbq to a response iocbq
8520 * by copying all the IOCB fields from command iocbq and transferring the
8521 * completion status information from the complete wcqe.
8522 **/
James Smart4f774512009-05-22 14:52:35 -04008523static void
James Smart341af102010-01-26 23:07:37 -05008524lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8525 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008526 struct lpfc_iocbq *pIocbOut,
8527 struct lpfc_wcqe_complete *wcqe)
8528{
James Smart341af102010-01-26 23:07:37 -05008529 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008530 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8531
8532 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8533 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008534 /* Map WCQE parameters into irspiocb parameters */
8535 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8536 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8537 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8538 pIocbIn->iocb.un.fcpi.fcpi_parm =
8539 pIocbOut->iocb.un.fcpi.fcpi_parm -
8540 wcqe->total_data_placed;
8541 else
8542 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008543 else {
James Smart4f774512009-05-22 14:52:35 -04008544 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008545 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8546 }
James Smart341af102010-01-26 23:07:37 -05008547
8548 /* Pick up HBA exchange busy condition */
8549 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8550 spin_lock_irqsave(&phba->hbalock, iflags);
8551 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8552 spin_unlock_irqrestore(&phba->hbalock, iflags);
8553 }
James Smart4f774512009-05-22 14:52:35 -04008554}
8555
8556/**
James Smart45ed1192009-10-02 15:17:02 -04008557 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8558 * @phba: Pointer to HBA context object.
8559 * @wcqe: Pointer to work-queue completion queue entry.
8560 *
8561 * This routine handles an ELS work-queue completion event and construct
8562 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8563 * discovery engine to handle.
8564 *
8565 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8566 **/
8567static struct lpfc_iocbq *
8568lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8569 struct lpfc_iocbq *irspiocbq)
8570{
8571 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8572 struct lpfc_iocbq *cmdiocbq;
8573 struct lpfc_wcqe_complete *wcqe;
8574 unsigned long iflags;
8575
8576 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8577 spin_lock_irqsave(&phba->hbalock, iflags);
8578 pring->stats.iocb_event++;
8579 /* Look up the ELS command IOCB and create pseudo response IOCB */
8580 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8581 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8582 spin_unlock_irqrestore(&phba->hbalock, iflags);
8583
8584 if (unlikely(!cmdiocbq)) {
8585 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8586 "0386 ELS complete with no corresponding "
8587 "cmdiocb: iotag (%d)\n",
8588 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8589 lpfc_sli_release_iocbq(phba, irspiocbq);
8590 return NULL;
8591 }
8592
8593 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008594 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008595
8596 return irspiocbq;
8597}
8598
8599/**
James Smart04c68492009-05-22 14:52:52 -04008600 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8601 * @phba: Pointer to HBA context object.
8602 * @cqe: Pointer to mailbox completion queue entry.
8603 *
8604 * This routine process a mailbox completion queue entry with asynchrous
8605 * event.
8606 *
8607 * Return: true if work posted to worker thread, otherwise false.
8608 **/
8609static bool
8610lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8611{
8612 struct lpfc_cq_event *cq_event;
8613 unsigned long iflags;
8614
8615 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8616 "0392 Async Event: word0:x%x, word1:x%x, "
8617 "word2:x%x, word3:x%x\n", mcqe->word0,
8618 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8619
8620 /* Allocate a new internal CQ_EVENT entry */
8621 cq_event = lpfc_sli4_cq_event_alloc(phba);
8622 if (!cq_event) {
8623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8624 "0394 Failed to allocate CQ_EVENT entry\n");
8625 return false;
8626 }
8627
8628 /* Move the CQE into an asynchronous event entry */
8629 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8630 spin_lock_irqsave(&phba->hbalock, iflags);
8631 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8632 /* Set the async event flag */
8633 phba->hba_flag |= ASYNC_EVENT;
8634 spin_unlock_irqrestore(&phba->hbalock, iflags);
8635
8636 return true;
8637}
8638
8639/**
8640 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8641 * @phba: Pointer to HBA context object.
8642 * @cqe: Pointer to mailbox completion queue entry.
8643 *
8644 * This routine process a mailbox completion queue entry with mailbox
8645 * completion event.
8646 *
8647 * Return: true if work posted to worker thread, otherwise false.
8648 **/
8649static bool
8650lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8651{
8652 uint32_t mcqe_status;
8653 MAILBOX_t *mbox, *pmbox;
8654 struct lpfc_mqe *mqe;
8655 struct lpfc_vport *vport;
8656 struct lpfc_nodelist *ndlp;
8657 struct lpfc_dmabuf *mp;
8658 unsigned long iflags;
8659 LPFC_MBOXQ_t *pmb;
8660 bool workposted = false;
8661 int rc;
8662
8663 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8664 if (!bf_get(lpfc_trailer_completed, mcqe))
8665 goto out_no_mqe_complete;
8666
8667 /* Get the reference to the active mbox command */
8668 spin_lock_irqsave(&phba->hbalock, iflags);
8669 pmb = phba->sli.mbox_active;
8670 if (unlikely(!pmb)) {
8671 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8672 "1832 No pending MBOX command to handle\n");
8673 spin_unlock_irqrestore(&phba->hbalock, iflags);
8674 goto out_no_mqe_complete;
8675 }
8676 spin_unlock_irqrestore(&phba->hbalock, iflags);
8677 mqe = &pmb->u.mqe;
8678 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8679 mbox = phba->mbox;
8680 vport = pmb->vport;
8681
8682 /* Reset heartbeat timer */
8683 phba->last_completion_time = jiffies;
8684 del_timer(&phba->sli.mbox_tmo);
8685
8686 /* Move mbox data to caller's mailbox region, do endian swapping */
8687 if (pmb->mbox_cmpl && mbox)
8688 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8689 /* Set the mailbox status with SLI4 range 0x4000 */
8690 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8691 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8692 bf_set(lpfc_mqe_status, mqe,
8693 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8694
8695 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8696 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8697 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8698 "MBOX dflt rpi: status:x%x rpi:x%x",
8699 mcqe_status,
8700 pmbox->un.varWords[0], 0);
8701 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8702 mp = (struct lpfc_dmabuf *)(pmb->context1);
8703 ndlp = (struct lpfc_nodelist *)pmb->context2;
8704 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8705 * RID of the PPI using the same mbox buffer.
8706 */
8707 lpfc_unreg_login(phba, vport->vpi,
8708 pmbox->un.varWords[0], pmb);
8709 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8710 pmb->context1 = mp;
8711 pmb->context2 = ndlp;
8712 pmb->vport = vport;
8713 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8714 if (rc != MBX_BUSY)
8715 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8716 LOG_SLI, "0385 rc should "
8717 "have been MBX_BUSY\n");
8718 if (rc != MBX_NOT_FINISHED)
8719 goto send_current_mbox;
8720 }
8721 }
8722 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8723 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8724 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8725
8726 /* There is mailbox completion work to do */
8727 spin_lock_irqsave(&phba->hbalock, iflags);
8728 __lpfc_mbox_cmpl_put(phba, pmb);
8729 phba->work_ha |= HA_MBATT;
8730 spin_unlock_irqrestore(&phba->hbalock, iflags);
8731 workposted = true;
8732
8733send_current_mbox:
8734 spin_lock_irqsave(&phba->hbalock, iflags);
8735 /* Release the mailbox command posting token */
8736 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8737 /* Setting active mailbox pointer need to be in sync to flag clear */
8738 phba->sli.mbox_active = NULL;
8739 spin_unlock_irqrestore(&phba->hbalock, iflags);
8740 /* Wake up worker thread to post the next pending mailbox command */
8741 lpfc_worker_wake_up(phba);
8742out_no_mqe_complete:
8743 if (bf_get(lpfc_trailer_consumed, mcqe))
8744 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8745 return workposted;
8746}
8747
8748/**
8749 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8750 * @phba: Pointer to HBA context object.
8751 * @cqe: Pointer to mailbox completion queue entry.
8752 *
8753 * This routine process a mailbox completion queue entry, it invokes the
8754 * proper mailbox complete handling or asynchrous event handling routine
8755 * according to the MCQE's async bit.
8756 *
8757 * Return: true if work posted to worker thread, otherwise false.
8758 **/
8759static bool
8760lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8761{
8762 struct lpfc_mcqe mcqe;
8763 bool workposted;
8764
8765 /* Copy the mailbox MCQE and convert endian order as needed */
8766 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8767
8768 /* Invoke the proper event handling routine */
8769 if (!bf_get(lpfc_trailer_async, &mcqe))
8770 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8771 else
8772 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8773 return workposted;
8774}
8775
8776/**
James Smart4f774512009-05-22 14:52:35 -04008777 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8778 * @phba: Pointer to HBA context object.
8779 * @wcqe: Pointer to work-queue completion queue entry.
8780 *
8781 * This routine handles an ELS work-queue completion event.
8782 *
8783 * Return: true if work posted to worker thread, otherwise false.
8784 **/
8785static bool
8786lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8787 struct lpfc_wcqe_complete *wcqe)
8788{
James Smart4f774512009-05-22 14:52:35 -04008789 struct lpfc_iocbq *irspiocbq;
8790 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008791
James Smart45ed1192009-10-02 15:17:02 -04008792 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008793 irspiocbq = lpfc_sli_get_iocbq(phba);
8794 if (!irspiocbq) {
8795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8796 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008797 return false;
James Smart4f774512009-05-22 14:52:35 -04008798 }
James Smart4f774512009-05-22 14:52:35 -04008799
James Smart45ed1192009-10-02 15:17:02 -04008800 /* Save off the slow-path queue event for work thread to process */
8801 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008802 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008803 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008804 &phba->sli4_hba.sp_queue_event);
8805 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008806 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008807
James Smart45ed1192009-10-02 15:17:02 -04008808 return true;
James Smart4f774512009-05-22 14:52:35 -04008809}
8810
8811/**
8812 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8813 * @phba: Pointer to HBA context object.
8814 * @wcqe: Pointer to work-queue completion queue entry.
8815 *
8816 * This routine handles slow-path WQ entry comsumed event by invoking the
8817 * proper WQ release routine to the slow-path WQ.
8818 **/
8819static void
8820lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8821 struct lpfc_wcqe_release *wcqe)
8822{
8823 /* Check for the slow-path ELS work queue */
8824 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8825 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8826 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8827 else
8828 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8829 "2579 Slow-path wqe consume event carries "
8830 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8831 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8832 phba->sli4_hba.els_wq->queue_id);
8833}
8834
8835/**
8836 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8837 * @phba: Pointer to HBA context object.
8838 * @cq: Pointer to a WQ completion queue.
8839 * @wcqe: Pointer to work-queue completion queue entry.
8840 *
8841 * This routine handles an XRI abort event.
8842 *
8843 * Return: true if work posted to worker thread, otherwise false.
8844 **/
8845static bool
8846lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8847 struct lpfc_queue *cq,
8848 struct sli4_wcqe_xri_aborted *wcqe)
8849{
8850 bool workposted = false;
8851 struct lpfc_cq_event *cq_event;
8852 unsigned long iflags;
8853
8854 /* Allocate a new internal CQ_EVENT entry */
8855 cq_event = lpfc_sli4_cq_event_alloc(phba);
8856 if (!cq_event) {
8857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8858 "0602 Failed to allocate CQ_EVENT entry\n");
8859 return false;
8860 }
8861
8862 /* Move the CQE into the proper xri abort event list */
8863 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8864 switch (cq->subtype) {
8865 case LPFC_FCP:
8866 spin_lock_irqsave(&phba->hbalock, iflags);
8867 list_add_tail(&cq_event->list,
8868 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8869 /* Set the fcp xri abort event flag */
8870 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8871 spin_unlock_irqrestore(&phba->hbalock, iflags);
8872 workposted = true;
8873 break;
8874 case LPFC_ELS:
8875 spin_lock_irqsave(&phba->hbalock, iflags);
8876 list_add_tail(&cq_event->list,
8877 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8878 /* Set the els xri abort event flag */
8879 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8880 spin_unlock_irqrestore(&phba->hbalock, iflags);
8881 workposted = true;
8882 break;
8883 default:
8884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8885 "0603 Invalid work queue CQE subtype (x%x)\n",
8886 cq->subtype);
8887 workposted = false;
8888 break;
8889 }
8890 return workposted;
8891}
8892
8893/**
James Smart4d9ab992009-10-02 15:16:39 -04008894 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008895 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008896 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008897 *
James Smart4d9ab992009-10-02 15:16:39 -04008898 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008899 *
8900 * Return: true if work posted to worker thread, otherwise false.
8901 **/
8902static bool
James Smart4d9ab992009-10-02 15:16:39 -04008903lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8904{
8905 bool workposted = false;
8906 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8907 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8908 struct hbq_dmabuf *dma_buf;
8909 uint32_t status;
8910 unsigned long iflags;
8911
James Smart4d9ab992009-10-02 15:16:39 -04008912 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8913 goto out;
8914
8915 status = bf_get(lpfc_rcqe_status, rcqe);
8916 switch (status) {
8917 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8919 "2537 Receive Frame Truncated!!\n");
8920 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008921 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008922 spin_lock_irqsave(&phba->hbalock, iflags);
8923 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8924 if (!dma_buf) {
8925 spin_unlock_irqrestore(&phba->hbalock, iflags);
8926 goto out;
8927 }
8928 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8929 /* save off the frame for the word thread to process */
8930 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008931 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008932 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008933 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008934 spin_unlock_irqrestore(&phba->hbalock, iflags);
8935 workposted = true;
8936 break;
8937 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8938 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8939 /* Post more buffers if possible */
8940 spin_lock_irqsave(&phba->hbalock, iflags);
8941 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8942 spin_unlock_irqrestore(&phba->hbalock, iflags);
8943 workposted = true;
8944 break;
8945 }
8946out:
8947 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008948}
8949
8950/**
8951 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8952 * @phba: Pointer to HBA context object.
8953 * @cq: Pointer to the completion queue.
8954 * @wcqe: Pointer to a completion queue entry.
8955 *
8956 * This routine process a slow-path work-queue or recieve queue completion queue
8957 * entry.
8958 *
8959 * Return: true if work posted to worker thread, otherwise false.
8960 **/
8961static bool
8962lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008963 struct lpfc_cqe *cqe)
8964{
James Smart45ed1192009-10-02 15:17:02 -04008965 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008966 bool workposted = false;
8967
8968 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008969 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008970
8971 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008972 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008973 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008974 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008975 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008976 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008977 break;
8978 case CQE_CODE_RELEASE_WQE:
8979 /* Process the WQ release event */
8980 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008981 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008982 break;
8983 case CQE_CODE_XRI_ABORTED:
8984 /* Process the WQ XRI abort event */
8985 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008986 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008987 break;
James Smart4d9ab992009-10-02 15:16:39 -04008988 case CQE_CODE_RECEIVE:
8989 /* Process the RQ event */
8990 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008991 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008992 break;
James Smart4f774512009-05-22 14:52:35 -04008993 default:
8994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8995 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008996 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008997 break;
8998 }
8999 return workposted;
9000}
9001
9002/**
James Smart4f774512009-05-22 14:52:35 -04009003 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9004 * @phba: Pointer to HBA context object.
9005 * @eqe: Pointer to fast-path event queue entry.
9006 *
9007 * This routine process a event queue entry from the slow-path event queue.
9008 * It will check the MajorCode and MinorCode to determine this is for a
9009 * completion event on a completion queue, if not, an error shall be logged
9010 * and just return. Otherwise, it will get to the corresponding completion
9011 * queue and process all the entries on that completion queue, rearm the
9012 * completion queue, and then return.
9013 *
9014 **/
9015static void
9016lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9017{
9018 struct lpfc_queue *cq = NULL, *childq, *speq;
9019 struct lpfc_cqe *cqe;
9020 bool workposted = false;
9021 int ecount = 0;
9022 uint16_t cqid;
9023
James Smartcb5172e2010-03-15 11:25:07 -04009024 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9026 "0359 Not a valid slow-path completion "
9027 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009028 bf_get_le32(lpfc_eqe_major_code, eqe),
9029 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009030 return;
9031 }
9032
9033 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009034 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009035
9036 /* Search for completion queue pointer matching this cqid */
9037 speq = phba->sli4_hba.sp_eq;
9038 list_for_each_entry(childq, &speq->child_list, list) {
9039 if (childq->queue_id == cqid) {
9040 cq = childq;
9041 break;
9042 }
9043 }
9044 if (unlikely(!cq)) {
9045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9046 "0365 Slow-path CQ identifier (%d) does "
9047 "not exist\n", cqid);
9048 return;
9049 }
9050
9051 /* Process all the entries to the CQ */
9052 switch (cq->type) {
9053 case LPFC_MCQ:
9054 while ((cqe = lpfc_sli4_cq_get(cq))) {
9055 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9056 if (!(++ecount % LPFC_GET_QE_REL_INT))
9057 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9058 }
9059 break;
9060 case LPFC_WCQ:
9061 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009062 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009063 if (!(++ecount % LPFC_GET_QE_REL_INT))
9064 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9065 }
9066 break;
9067 default:
9068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9069 "0370 Invalid completion queue type (%d)\n",
9070 cq->type);
9071 return;
9072 }
9073
9074 /* Catch the no cq entry condition, log an error */
9075 if (unlikely(ecount == 0))
9076 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9077 "0371 No entry from the CQ: identifier "
9078 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9079
9080 /* In any case, flash and re-arm the RCQ */
9081 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9082
9083 /* wake up worker thread if there are works to be done */
9084 if (workposted)
9085 lpfc_worker_wake_up(phba);
9086}
9087
9088/**
9089 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9090 * @eqe: Pointer to fast-path completion queue entry.
9091 *
9092 * This routine process a fast-path work queue completion entry from fast-path
9093 * event queue for FCP command response completion.
9094 **/
9095static void
9096lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9097 struct lpfc_wcqe_complete *wcqe)
9098{
9099 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9100 struct lpfc_iocbq *cmdiocbq;
9101 struct lpfc_iocbq irspiocbq;
9102 unsigned long iflags;
9103
9104 spin_lock_irqsave(&phba->hbalock, iflags);
9105 pring->stats.iocb_event++;
9106 spin_unlock_irqrestore(&phba->hbalock, iflags);
9107
9108 /* Check for response status */
9109 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9110 /* If resource errors reported from HBA, reduce queue
9111 * depth of the SCSI device.
9112 */
9113 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9114 IOSTAT_LOCAL_REJECT) &&
9115 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9116 phba->lpfc_rampdown_queue_depth(phba);
9117 }
9118 /* Log the error status */
9119 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9120 "0373 FCP complete error: status=x%x, "
9121 "hw_status=x%x, total_data_specified=%d, "
9122 "parameter=x%x, word3=x%x\n",
9123 bf_get(lpfc_wcqe_c_status, wcqe),
9124 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9125 wcqe->total_data_placed, wcqe->parameter,
9126 wcqe->word3);
9127 }
9128
9129 /* Look up the FCP command IOCB and create pseudo response IOCB */
9130 spin_lock_irqsave(&phba->hbalock, iflags);
9131 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9132 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9133 spin_unlock_irqrestore(&phba->hbalock, iflags);
9134 if (unlikely(!cmdiocbq)) {
9135 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9136 "0374 FCP complete with no corresponding "
9137 "cmdiocb: iotag (%d)\n",
9138 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9139 return;
9140 }
9141 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9142 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9143 "0375 FCP cmdiocb not callback function "
9144 "iotag: (%d)\n",
9145 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9146 return;
9147 }
9148
9149 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009150 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009151
James Smart0f65ff62010-02-26 14:14:23 -05009152 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9153 spin_lock_irqsave(&phba->hbalock, iflags);
9154 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9155 spin_unlock_irqrestore(&phba->hbalock, iflags);
9156 }
9157
James Smart4f774512009-05-22 14:52:35 -04009158 /* Pass the cmd_iocb and the rsp state to the upper layer */
9159 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9160}
9161
9162/**
9163 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9164 * @phba: Pointer to HBA context object.
9165 * @cq: Pointer to completion queue.
9166 * @wcqe: Pointer to work-queue completion queue entry.
9167 *
9168 * This routine handles an fast-path WQ entry comsumed event by invoking the
9169 * proper WQ release routine to the slow-path WQ.
9170 **/
9171static void
9172lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9173 struct lpfc_wcqe_release *wcqe)
9174{
9175 struct lpfc_queue *childwq;
9176 bool wqid_matched = false;
9177 uint16_t fcp_wqid;
9178
9179 /* Check for fast-path FCP work queue release */
9180 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9181 list_for_each_entry(childwq, &cq->child_list, list) {
9182 if (childwq->queue_id == fcp_wqid) {
9183 lpfc_sli4_wq_release(childwq,
9184 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9185 wqid_matched = true;
9186 break;
9187 }
9188 }
9189 /* Report warning log message if no match found */
9190 if (wqid_matched != true)
9191 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9192 "2580 Fast-path wqe consume event carries "
9193 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9194}
9195
9196/**
9197 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9198 * @cq: Pointer to the completion queue.
9199 * @eqe: Pointer to fast-path completion queue entry.
9200 *
9201 * This routine process a fast-path work queue completion entry from fast-path
9202 * event queue for FCP command response completion.
9203 **/
9204static int
9205lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9206 struct lpfc_cqe *cqe)
9207{
9208 struct lpfc_wcqe_release wcqe;
9209 bool workposted = false;
9210
9211 /* Copy the work queue CQE and convert endian order if needed */
9212 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9213
9214 /* Check and process for different type of WCQE and dispatch */
9215 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9216 case CQE_CODE_COMPL_WQE:
9217 /* Process the WQ complete event */
9218 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9219 (struct lpfc_wcqe_complete *)&wcqe);
9220 break;
9221 case CQE_CODE_RELEASE_WQE:
9222 /* Process the WQ release event */
9223 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9224 (struct lpfc_wcqe_release *)&wcqe);
9225 break;
9226 case CQE_CODE_XRI_ABORTED:
9227 /* Process the WQ XRI abort event */
9228 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9229 (struct sli4_wcqe_xri_aborted *)&wcqe);
9230 break;
9231 default:
9232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9233 "0144 Not a valid WCQE code: x%x\n",
9234 bf_get(lpfc_wcqe_c_code, &wcqe));
9235 break;
9236 }
9237 return workposted;
9238}
9239
9240/**
9241 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9242 * @phba: Pointer to HBA context object.
9243 * @eqe: Pointer to fast-path event queue entry.
9244 *
9245 * This routine process a event queue entry from the fast-path event queue.
9246 * It will check the MajorCode and MinorCode to determine this is for a
9247 * completion event on a completion queue, if not, an error shall be logged
9248 * and just return. Otherwise, it will get to the corresponding completion
9249 * queue and process all the entries on the completion queue, rearm the
9250 * completion queue, and then return.
9251 **/
9252static void
9253lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9254 uint32_t fcp_cqidx)
9255{
9256 struct lpfc_queue *cq;
9257 struct lpfc_cqe *cqe;
9258 bool workposted = false;
9259 uint16_t cqid;
9260 int ecount = 0;
9261
James Smartcb5172e2010-03-15 11:25:07 -04009262 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9264 "0366 Not a valid fast-path completion "
9265 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009266 bf_get_le32(lpfc_eqe_major_code, eqe),
9267 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009268 return;
9269 }
9270
9271 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9272 if (unlikely(!cq)) {
9273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9274 "0367 Fast-path completion queue does not "
9275 "exist\n");
9276 return;
9277 }
9278
9279 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009280 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009281 if (unlikely(cqid != cq->queue_id)) {
9282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9283 "0368 Miss-matched fast-path completion "
9284 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9285 cqid, cq->queue_id);
9286 return;
9287 }
9288
9289 /* Process all the entries to the CQ */
9290 while ((cqe = lpfc_sli4_cq_get(cq))) {
9291 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9292 if (!(++ecount % LPFC_GET_QE_REL_INT))
9293 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9294 }
9295
9296 /* Catch the no cq entry condition */
9297 if (unlikely(ecount == 0))
9298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9299 "0369 No entry from fast-path completion "
9300 "queue fcpcqid=%d\n", cq->queue_id);
9301
9302 /* In any case, flash and re-arm the CQ */
9303 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9304
9305 /* wake up worker thread if there are works to be done */
9306 if (workposted)
9307 lpfc_worker_wake_up(phba);
9308}
9309
9310static void
9311lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9312{
9313 struct lpfc_eqe *eqe;
9314
9315 /* walk all the EQ entries and drop on the floor */
9316 while ((eqe = lpfc_sli4_eq_get(eq)))
9317 ;
9318
9319 /* Clear and re-arm the EQ */
9320 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9321}
9322
9323/**
9324 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9325 * @irq: Interrupt number.
9326 * @dev_id: The device context pointer.
9327 *
9328 * This function is directly called from the PCI layer as an interrupt
9329 * service routine when device with SLI-4 interface spec is enabled with
9330 * MSI-X multi-message interrupt mode and there are slow-path events in
9331 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9332 * interrupt mode, this function is called as part of the device-level
9333 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9334 * undergoing initialization, the interrupt handler will not process the
9335 * interrupt. The link attention and ELS ring attention events are handled
9336 * by the worker thread. The interrupt handler signals the worker thread
9337 * and returns for these events. This function is called without any lock
9338 * held. It gets the hbalock to access and update SLI data structures.
9339 *
9340 * This function returns IRQ_HANDLED when interrupt is handled else it
9341 * returns IRQ_NONE.
9342 **/
9343irqreturn_t
9344lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9345{
9346 struct lpfc_hba *phba;
9347 struct lpfc_queue *speq;
9348 struct lpfc_eqe *eqe;
9349 unsigned long iflag;
9350 int ecount = 0;
9351
9352 /*
9353 * Get the driver's phba structure from the dev_id
9354 */
9355 phba = (struct lpfc_hba *)dev_id;
9356
9357 if (unlikely(!phba))
9358 return IRQ_NONE;
9359
9360 /* Get to the EQ struct associated with this vector */
9361 speq = phba->sli4_hba.sp_eq;
9362
9363 /* Check device state for handling interrupt */
9364 if (unlikely(lpfc_intr_state_check(phba))) {
9365 /* Check again for link_state with lock held */
9366 spin_lock_irqsave(&phba->hbalock, iflag);
9367 if (phba->link_state < LPFC_LINK_DOWN)
9368 /* Flush, clear interrupt, and rearm the EQ */
9369 lpfc_sli4_eq_flush(phba, speq);
9370 spin_unlock_irqrestore(&phba->hbalock, iflag);
9371 return IRQ_NONE;
9372 }
9373
9374 /*
9375 * Process all the event on FCP slow-path EQ
9376 */
9377 while ((eqe = lpfc_sli4_eq_get(speq))) {
9378 lpfc_sli4_sp_handle_eqe(phba, eqe);
9379 if (!(++ecount % LPFC_GET_QE_REL_INT))
9380 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9381 }
9382
9383 /* Always clear and re-arm the slow-path EQ */
9384 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9385
9386 /* Catch the no cq entry condition */
9387 if (unlikely(ecount == 0)) {
9388 if (phba->intr_type == MSIX)
9389 /* MSI-X treated interrupt served as no EQ share INT */
9390 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9391 "0357 MSI-X interrupt with no EQE\n");
9392 else
9393 /* Non MSI-X treated on interrupt as EQ share INT */
9394 return IRQ_NONE;
9395 }
9396
9397 return IRQ_HANDLED;
9398} /* lpfc_sli4_sp_intr_handler */
9399
9400/**
9401 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9402 * @irq: Interrupt number.
9403 * @dev_id: The device context pointer.
9404 *
9405 * This function is directly called from the PCI layer as an interrupt
9406 * service routine when device with SLI-4 interface spec is enabled with
9407 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9408 * ring event in the HBA. However, when the device is enabled with either
9409 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9410 * device-level interrupt handler. When the PCI slot is in error recovery
9411 * or the HBA is undergoing initialization, the interrupt handler will not
9412 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9413 * the intrrupt context. This function is called without any lock held.
9414 * It gets the hbalock to access and update SLI data structures. Note that,
9415 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9416 * equal to that of FCP CQ index.
9417 *
9418 * This function returns IRQ_HANDLED when interrupt is handled else it
9419 * returns IRQ_NONE.
9420 **/
9421irqreturn_t
9422lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9423{
9424 struct lpfc_hba *phba;
9425 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9426 struct lpfc_queue *fpeq;
9427 struct lpfc_eqe *eqe;
9428 unsigned long iflag;
9429 int ecount = 0;
9430 uint32_t fcp_eqidx;
9431
9432 /* Get the driver's phba structure from the dev_id */
9433 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9434 phba = fcp_eq_hdl->phba;
9435 fcp_eqidx = fcp_eq_hdl->idx;
9436
9437 if (unlikely(!phba))
9438 return IRQ_NONE;
9439
9440 /* Get to the EQ struct associated with this vector */
9441 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9442
9443 /* Check device state for handling interrupt */
9444 if (unlikely(lpfc_intr_state_check(phba))) {
9445 /* Check again for link_state with lock held */
9446 spin_lock_irqsave(&phba->hbalock, iflag);
9447 if (phba->link_state < LPFC_LINK_DOWN)
9448 /* Flush, clear interrupt, and rearm the EQ */
9449 lpfc_sli4_eq_flush(phba, fpeq);
9450 spin_unlock_irqrestore(&phba->hbalock, iflag);
9451 return IRQ_NONE;
9452 }
9453
9454 /*
9455 * Process all the event on FCP fast-path EQ
9456 */
9457 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9458 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9459 if (!(++ecount % LPFC_GET_QE_REL_INT))
9460 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9461 }
9462
9463 /* Always clear and re-arm the fast-path EQ */
9464 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9465
9466 if (unlikely(ecount == 0)) {
9467 if (phba->intr_type == MSIX)
9468 /* MSI-X treated interrupt served as no EQ share INT */
9469 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9470 "0358 MSI-X interrupt with no EQE\n");
9471 else
9472 /* Non MSI-X treated on interrupt as EQ share INT */
9473 return IRQ_NONE;
9474 }
9475
9476 return IRQ_HANDLED;
9477} /* lpfc_sli4_fp_intr_handler */
9478
9479/**
9480 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9481 * @irq: Interrupt number.
9482 * @dev_id: The device context pointer.
9483 *
9484 * This function is the device-level interrupt handler to device with SLI-4
9485 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9486 * interrupt mode is enabled and there is an event in the HBA which requires
9487 * driver attention. This function invokes the slow-path interrupt attention
9488 * handling function and fast-path interrupt attention handling function in
9489 * turn to process the relevant HBA attention events. This function is called
9490 * without any lock held. It gets the hbalock to access and update SLI data
9491 * structures.
9492 *
9493 * This function returns IRQ_HANDLED when interrupt is handled, else it
9494 * returns IRQ_NONE.
9495 **/
9496irqreturn_t
9497lpfc_sli4_intr_handler(int irq, void *dev_id)
9498{
9499 struct lpfc_hba *phba;
9500 irqreturn_t sp_irq_rc, fp_irq_rc;
9501 bool fp_handled = false;
9502 uint32_t fcp_eqidx;
9503
9504 /* Get the driver's phba structure from the dev_id */
9505 phba = (struct lpfc_hba *)dev_id;
9506
9507 if (unlikely(!phba))
9508 return IRQ_NONE;
9509
9510 /*
9511 * Invokes slow-path host attention interrupt handling as appropriate.
9512 */
9513 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9514
9515 /*
9516 * Invoke fast-path host attention interrupt handling as appropriate.
9517 */
9518 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9519 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9520 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9521 if (fp_irq_rc == IRQ_HANDLED)
9522 fp_handled |= true;
9523 }
9524
9525 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9526} /* lpfc_sli4_intr_handler */
9527
9528/**
9529 * lpfc_sli4_queue_free - free a queue structure and associated memory
9530 * @queue: The queue structure to free.
9531 *
9532 * This function frees a queue structure and the DMAable memeory used for
9533 * the host resident queue. This function must be called after destroying the
9534 * queue on the HBA.
9535 **/
9536void
9537lpfc_sli4_queue_free(struct lpfc_queue *queue)
9538{
9539 struct lpfc_dmabuf *dmabuf;
9540
9541 if (!queue)
9542 return;
9543
9544 while (!list_empty(&queue->page_list)) {
9545 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9546 list);
9547 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9548 dmabuf->virt, dmabuf->phys);
9549 kfree(dmabuf);
9550 }
9551 kfree(queue);
9552 return;
9553}
9554
9555/**
9556 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9557 * @phba: The HBA that this queue is being created on.
9558 * @entry_size: The size of each queue entry for this queue.
9559 * @entry count: The number of entries that this queue will handle.
9560 *
9561 * This function allocates a queue structure and the DMAable memory used for
9562 * the host resident queue. This function must be called before creating the
9563 * queue on the HBA.
9564 **/
9565struct lpfc_queue *
9566lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9567 uint32_t entry_count)
9568{
9569 struct lpfc_queue *queue;
9570 struct lpfc_dmabuf *dmabuf;
9571 int x, total_qe_count;
9572 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009573 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009574
9575
James Smartcb5172e2010-03-15 11:25:07 -04009576 if (!phba->sli4_hba.pc_sli4_params.supported)
9577 hw_page_size = SLI4_PAGE_SIZE;
9578
James Smart4f774512009-05-22 14:52:35 -04009579 queue = kzalloc(sizeof(struct lpfc_queue) +
9580 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9581 if (!queue)
9582 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009583 queue->page_count = (ALIGN(entry_size * entry_count,
9584 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009585 INIT_LIST_HEAD(&queue->list);
9586 INIT_LIST_HEAD(&queue->page_list);
9587 INIT_LIST_HEAD(&queue->child_list);
9588 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9589 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9590 if (!dmabuf)
9591 goto out_fail;
9592 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009593 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009594 GFP_KERNEL);
9595 if (!dmabuf->virt) {
9596 kfree(dmabuf);
9597 goto out_fail;
9598 }
James Smartcb5172e2010-03-15 11:25:07 -04009599 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009600 dmabuf->buffer_tag = x;
9601 list_add_tail(&dmabuf->list, &queue->page_list);
9602 /* initialize queue's entry array */
9603 dma_pointer = dmabuf->virt;
9604 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009605 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009606 total_qe_count++, dma_pointer += entry_size) {
9607 queue->qe[total_qe_count].address = dma_pointer;
9608 }
9609 }
9610 queue->entry_size = entry_size;
9611 queue->entry_count = entry_count;
9612 queue->phba = phba;
9613
9614 return queue;
9615out_fail:
9616 lpfc_sli4_queue_free(queue);
9617 return NULL;
9618}
9619
9620/**
9621 * lpfc_eq_create - Create an Event Queue on the HBA
9622 * @phba: HBA structure that indicates port to create a queue on.
9623 * @eq: The queue structure to use to create the event queue.
9624 * @imax: The maximum interrupt per second limit.
9625 *
9626 * This function creates an event queue, as detailed in @eq, on a port,
9627 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9628 *
9629 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9630 * is used to get the entry count and entry size that are necessary to
9631 * determine the number of pages to allocate and use for this queue. This
9632 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9633 * event queue. This function is asynchronous and will wait for the mailbox
9634 * command to finish before continuing.
9635 *
9636 * On success this function will return a zero. If unable to allocate enough
9637 * memory this function will return ENOMEM. If the queue create mailbox command
9638 * fails this function will return ENXIO.
9639 **/
9640uint32_t
9641lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9642{
9643 struct lpfc_mbx_eq_create *eq_create;
9644 LPFC_MBOXQ_t *mbox;
9645 int rc, length, status = 0;
9646 struct lpfc_dmabuf *dmabuf;
9647 uint32_t shdr_status, shdr_add_status;
9648 union lpfc_sli4_cfg_shdr *shdr;
9649 uint16_t dmult;
9650
9651 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9652 if (!mbox)
9653 return -ENOMEM;
9654 length = (sizeof(struct lpfc_mbx_eq_create) -
9655 sizeof(struct lpfc_sli4_cfg_mhdr));
9656 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9657 LPFC_MBOX_OPCODE_EQ_CREATE,
9658 length, LPFC_SLI4_MBX_EMBED);
9659 eq_create = &mbox->u.mqe.un.eq_create;
9660 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9661 eq->page_count);
9662 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9663 LPFC_EQE_SIZE);
9664 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9665 /* Calculate delay multiper from maximum interrupt per second */
9666 dmult = LPFC_DMULT_CONST/imax - 1;
9667 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9668 dmult);
9669 switch (eq->entry_count) {
9670 default:
9671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9672 "0360 Unsupported EQ count. (%d)\n",
9673 eq->entry_count);
9674 if (eq->entry_count < 256)
9675 return -EINVAL;
9676 /* otherwise default to smallest count (drop through) */
9677 case 256:
9678 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9679 LPFC_EQ_CNT_256);
9680 break;
9681 case 512:
9682 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9683 LPFC_EQ_CNT_512);
9684 break;
9685 case 1024:
9686 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9687 LPFC_EQ_CNT_1024);
9688 break;
9689 case 2048:
9690 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9691 LPFC_EQ_CNT_2048);
9692 break;
9693 case 4096:
9694 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9695 LPFC_EQ_CNT_4096);
9696 break;
9697 }
9698 list_for_each_entry(dmabuf, &eq->page_list, list) {
9699 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9700 putPaddrLow(dmabuf->phys);
9701 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9702 putPaddrHigh(dmabuf->phys);
9703 }
9704 mbox->vport = phba->pport;
9705 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9706 mbox->context1 = NULL;
9707 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9708 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9709 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9710 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9711 if (shdr_status || shdr_add_status || rc) {
9712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9713 "2500 EQ_CREATE mailbox failed with "
9714 "status x%x add_status x%x, mbx status x%x\n",
9715 shdr_status, shdr_add_status, rc);
9716 status = -ENXIO;
9717 }
9718 eq->type = LPFC_EQ;
9719 eq->subtype = LPFC_NONE;
9720 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9721 if (eq->queue_id == 0xFFFF)
9722 status = -ENXIO;
9723 eq->host_index = 0;
9724 eq->hba_index = 0;
9725
James Smart8fa38512009-07-19 10:01:03 -04009726 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009727 return status;
9728}
9729
9730/**
9731 * lpfc_cq_create - Create a Completion Queue on the HBA
9732 * @phba: HBA structure that indicates port to create a queue on.
9733 * @cq: The queue structure to use to create the completion queue.
9734 * @eq: The event queue to bind this completion queue to.
9735 *
9736 * This function creates a completion queue, as detailed in @wq, on a port,
9737 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9738 *
9739 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9740 * is used to get the entry count and entry size that are necessary to
9741 * determine the number of pages to allocate and use for this queue. The @eq
9742 * is used to indicate which event queue to bind this completion queue to. This
9743 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9744 * completion queue. This function is asynchronous and will wait for the mailbox
9745 * command to finish before continuing.
9746 *
9747 * On success this function will return a zero. If unable to allocate enough
9748 * memory this function will return ENOMEM. If the queue create mailbox command
9749 * fails this function will return ENXIO.
9750 **/
9751uint32_t
9752lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9753 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9754{
9755 struct lpfc_mbx_cq_create *cq_create;
9756 struct lpfc_dmabuf *dmabuf;
9757 LPFC_MBOXQ_t *mbox;
9758 int rc, length, status = 0;
9759 uint32_t shdr_status, shdr_add_status;
9760 union lpfc_sli4_cfg_shdr *shdr;
9761
9762 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9763 if (!mbox)
9764 return -ENOMEM;
9765 length = (sizeof(struct lpfc_mbx_cq_create) -
9766 sizeof(struct lpfc_sli4_cfg_mhdr));
9767 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9768 LPFC_MBOX_OPCODE_CQ_CREATE,
9769 length, LPFC_SLI4_MBX_EMBED);
9770 cq_create = &mbox->u.mqe.un.cq_create;
9771 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9772 cq->page_count);
9773 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9774 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9775 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9776 switch (cq->entry_count) {
9777 default:
9778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9779 "0361 Unsupported CQ count. (%d)\n",
9780 cq->entry_count);
9781 if (cq->entry_count < 256)
9782 return -EINVAL;
9783 /* otherwise default to smallest count (drop through) */
9784 case 256:
9785 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9786 LPFC_CQ_CNT_256);
9787 break;
9788 case 512:
9789 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9790 LPFC_CQ_CNT_512);
9791 break;
9792 case 1024:
9793 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9794 LPFC_CQ_CNT_1024);
9795 break;
9796 }
9797 list_for_each_entry(dmabuf, &cq->page_list, list) {
9798 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9799 putPaddrLow(dmabuf->phys);
9800 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9801 putPaddrHigh(dmabuf->phys);
9802 }
9803 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9804
9805 /* The IOCTL status is embedded in the mailbox subheader. */
9806 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9807 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9808 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9809 if (shdr_status || shdr_add_status || rc) {
9810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9811 "2501 CQ_CREATE mailbox failed with "
9812 "status x%x add_status x%x, mbx status x%x\n",
9813 shdr_status, shdr_add_status, rc);
9814 status = -ENXIO;
9815 goto out;
9816 }
9817 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9818 if (cq->queue_id == 0xFFFF) {
9819 status = -ENXIO;
9820 goto out;
9821 }
9822 /* link the cq onto the parent eq child list */
9823 list_add_tail(&cq->list, &eq->child_list);
9824 /* Set up completion queue's type and subtype */
9825 cq->type = type;
9826 cq->subtype = subtype;
9827 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9828 cq->host_index = 0;
9829 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009830
James Smart8fa38512009-07-19 10:01:03 -04009831out:
9832 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009833 return status;
9834}
9835
9836/**
James Smart04c68492009-05-22 14:52:52 -04009837 * lpfc_mq_create - Create a mailbox Queue on the HBA
9838 * @phba: HBA structure that indicates port to create a queue on.
9839 * @mq: The queue structure to use to create the mailbox queue.
9840 *
9841 * This function creates a mailbox queue, as detailed in @mq, on a port,
9842 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9843 *
9844 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9845 * is used to get the entry count and entry size that are necessary to
9846 * determine the number of pages to allocate and use for this queue. This
9847 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9848 * mailbox queue. This function is asynchronous and will wait for the mailbox
9849 * command to finish before continuing.
9850 *
9851 * On success this function will return a zero. If unable to allocate enough
9852 * memory this function will return ENOMEM. If the queue create mailbox command
9853 * fails this function will return ENXIO.
9854 **/
9855uint32_t
9856lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9857 struct lpfc_queue *cq, uint32_t subtype)
9858{
9859 struct lpfc_mbx_mq_create *mq_create;
9860 struct lpfc_dmabuf *dmabuf;
9861 LPFC_MBOXQ_t *mbox;
9862 int rc, length, status = 0;
9863 uint32_t shdr_status, shdr_add_status;
9864 union lpfc_sli4_cfg_shdr *shdr;
9865
9866 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9867 if (!mbox)
9868 return -ENOMEM;
9869 length = (sizeof(struct lpfc_mbx_mq_create) -
9870 sizeof(struct lpfc_sli4_cfg_mhdr));
9871 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9872 LPFC_MBOX_OPCODE_MQ_CREATE,
9873 length, LPFC_SLI4_MBX_EMBED);
9874 mq_create = &mbox->u.mqe.un.mq_create;
9875 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9876 mq->page_count);
9877 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9878 cq->queue_id);
9879 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9880 switch (mq->entry_count) {
9881 default:
9882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9883 "0362 Unsupported MQ count. (%d)\n",
9884 mq->entry_count);
9885 if (mq->entry_count < 16)
9886 return -EINVAL;
9887 /* otherwise default to smallest count (drop through) */
9888 case 16:
9889 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9890 LPFC_MQ_CNT_16);
9891 break;
9892 case 32:
9893 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9894 LPFC_MQ_CNT_32);
9895 break;
9896 case 64:
9897 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9898 LPFC_MQ_CNT_64);
9899 break;
9900 case 128:
9901 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9902 LPFC_MQ_CNT_128);
9903 break;
9904 }
9905 list_for_each_entry(dmabuf, &mq->page_list, list) {
9906 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9907 putPaddrLow(dmabuf->phys);
9908 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9909 putPaddrHigh(dmabuf->phys);
9910 }
9911 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9912 /* The IOCTL status is embedded in the mailbox subheader. */
9913 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9914 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9915 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9916 if (shdr_status || shdr_add_status || rc) {
9917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9918 "2502 MQ_CREATE mailbox failed with "
9919 "status x%x add_status x%x, mbx status x%x\n",
9920 shdr_status, shdr_add_status, rc);
9921 status = -ENXIO;
9922 goto out;
9923 }
9924 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9925 if (mq->queue_id == 0xFFFF) {
9926 status = -ENXIO;
9927 goto out;
9928 }
9929 mq->type = LPFC_MQ;
9930 mq->subtype = subtype;
9931 mq->host_index = 0;
9932 mq->hba_index = 0;
9933
9934 /* link the mq onto the parent cq child list */
9935 list_add_tail(&mq->list, &cq->child_list);
9936out:
James Smart8fa38512009-07-19 10:01:03 -04009937 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009938 return status;
9939}
9940
9941/**
James Smart4f774512009-05-22 14:52:35 -04009942 * lpfc_wq_create - Create a Work Queue on the HBA
9943 * @phba: HBA structure that indicates port to create a queue on.
9944 * @wq: The queue structure to use to create the work queue.
9945 * @cq: The completion queue to bind this work queue to.
9946 * @subtype: The subtype of the work queue indicating its functionality.
9947 *
9948 * This function creates a work queue, as detailed in @wq, on a port, described
9949 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9950 *
9951 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9952 * is used to get the entry count and entry size that are necessary to
9953 * determine the number of pages to allocate and use for this queue. The @cq
9954 * is used to indicate which completion queue to bind this work queue to. This
9955 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9956 * work queue. This function is asynchronous and will wait for the mailbox
9957 * command to finish before continuing.
9958 *
9959 * On success this function will return a zero. If unable to allocate enough
9960 * memory this function will return ENOMEM. If the queue create mailbox command
9961 * fails this function will return ENXIO.
9962 **/
9963uint32_t
9964lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9965 struct lpfc_queue *cq, uint32_t subtype)
9966{
9967 struct lpfc_mbx_wq_create *wq_create;
9968 struct lpfc_dmabuf *dmabuf;
9969 LPFC_MBOXQ_t *mbox;
9970 int rc, length, status = 0;
9971 uint32_t shdr_status, shdr_add_status;
9972 union lpfc_sli4_cfg_shdr *shdr;
9973
9974 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9975 if (!mbox)
9976 return -ENOMEM;
9977 length = (sizeof(struct lpfc_mbx_wq_create) -
9978 sizeof(struct lpfc_sli4_cfg_mhdr));
9979 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9980 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9981 length, LPFC_SLI4_MBX_EMBED);
9982 wq_create = &mbox->u.mqe.un.wq_create;
9983 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9984 wq->page_count);
9985 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9986 cq->queue_id);
9987 list_for_each_entry(dmabuf, &wq->page_list, list) {
9988 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9989 putPaddrLow(dmabuf->phys);
9990 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9991 putPaddrHigh(dmabuf->phys);
9992 }
9993 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9994 /* The IOCTL status is embedded in the mailbox subheader. */
9995 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9996 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9997 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9998 if (shdr_status || shdr_add_status || rc) {
9999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10000 "2503 WQ_CREATE mailbox failed with "
10001 "status x%x add_status x%x, mbx status x%x\n",
10002 shdr_status, shdr_add_status, rc);
10003 status = -ENXIO;
10004 goto out;
10005 }
10006 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10007 if (wq->queue_id == 0xFFFF) {
10008 status = -ENXIO;
10009 goto out;
10010 }
10011 wq->type = LPFC_WQ;
10012 wq->subtype = subtype;
10013 wq->host_index = 0;
10014 wq->hba_index = 0;
10015
10016 /* link the wq onto the parent cq child list */
10017 list_add_tail(&wq->list, &cq->child_list);
10018out:
James Smart8fa38512009-07-19 10:01:03 -040010019 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010020 return status;
10021}
10022
10023/**
10024 * lpfc_rq_create - Create a Receive Queue on the HBA
10025 * @phba: HBA structure that indicates port to create a queue on.
10026 * @hrq: The queue structure to use to create the header receive queue.
10027 * @drq: The queue structure to use to create the data receive queue.
10028 * @cq: The completion queue to bind this work queue to.
10029 *
10030 * This function creates a receive buffer queue pair , as detailed in @hrq and
10031 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10032 * to the HBA.
10033 *
10034 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10035 * struct is used to get the entry count that is necessary to determine the
10036 * number of pages to use for this queue. The @cq is used to indicate which
10037 * completion queue to bind received buffers that are posted to these queues to.
10038 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10039 * receive queue pair. This function is asynchronous and will wait for the
10040 * mailbox command to finish before continuing.
10041 *
10042 * On success this function will return a zero. If unable to allocate enough
10043 * memory this function will return ENOMEM. If the queue create mailbox command
10044 * fails this function will return ENXIO.
10045 **/
10046uint32_t
10047lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10048 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10049{
10050 struct lpfc_mbx_rq_create *rq_create;
10051 struct lpfc_dmabuf *dmabuf;
10052 LPFC_MBOXQ_t *mbox;
10053 int rc, length, status = 0;
10054 uint32_t shdr_status, shdr_add_status;
10055 union lpfc_sli4_cfg_shdr *shdr;
10056
10057 if (hrq->entry_count != drq->entry_count)
10058 return -EINVAL;
10059 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10060 if (!mbox)
10061 return -ENOMEM;
10062 length = (sizeof(struct lpfc_mbx_rq_create) -
10063 sizeof(struct lpfc_sli4_cfg_mhdr));
10064 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10065 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10066 length, LPFC_SLI4_MBX_EMBED);
10067 rq_create = &mbox->u.mqe.un.rq_create;
10068 switch (hrq->entry_count) {
10069 default:
10070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10071 "2535 Unsupported RQ count. (%d)\n",
10072 hrq->entry_count);
10073 if (hrq->entry_count < 512)
10074 return -EINVAL;
10075 /* otherwise default to smallest count (drop through) */
10076 case 512:
10077 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10078 LPFC_RQ_RING_SIZE_512);
10079 break;
10080 case 1024:
10081 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10082 LPFC_RQ_RING_SIZE_1024);
10083 break;
10084 case 2048:
10085 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10086 LPFC_RQ_RING_SIZE_2048);
10087 break;
10088 case 4096:
10089 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10090 LPFC_RQ_RING_SIZE_4096);
10091 break;
10092 }
10093 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10094 cq->queue_id);
10095 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10096 hrq->page_count);
10097 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10098 LPFC_HDR_BUF_SIZE);
10099 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10100 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10101 putPaddrLow(dmabuf->phys);
10102 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10103 putPaddrHigh(dmabuf->phys);
10104 }
10105 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10106 /* The IOCTL status is embedded in the mailbox subheader. */
10107 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10108 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10109 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10110 if (shdr_status || shdr_add_status || rc) {
10111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10112 "2504 RQ_CREATE mailbox failed with "
10113 "status x%x add_status x%x, mbx status x%x\n",
10114 shdr_status, shdr_add_status, rc);
10115 status = -ENXIO;
10116 goto out;
10117 }
10118 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10119 if (hrq->queue_id == 0xFFFF) {
10120 status = -ENXIO;
10121 goto out;
10122 }
10123 hrq->type = LPFC_HRQ;
10124 hrq->subtype = subtype;
10125 hrq->host_index = 0;
10126 hrq->hba_index = 0;
10127
10128 /* now create the data queue */
10129 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10130 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10131 length, LPFC_SLI4_MBX_EMBED);
10132 switch (drq->entry_count) {
10133 default:
10134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10135 "2536 Unsupported RQ count. (%d)\n",
10136 drq->entry_count);
10137 if (drq->entry_count < 512)
10138 return -EINVAL;
10139 /* otherwise default to smallest count (drop through) */
10140 case 512:
10141 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10142 LPFC_RQ_RING_SIZE_512);
10143 break;
10144 case 1024:
10145 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10146 LPFC_RQ_RING_SIZE_1024);
10147 break;
10148 case 2048:
10149 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10150 LPFC_RQ_RING_SIZE_2048);
10151 break;
10152 case 4096:
10153 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10154 LPFC_RQ_RING_SIZE_4096);
10155 break;
10156 }
10157 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10158 cq->queue_id);
10159 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10160 drq->page_count);
10161 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10162 LPFC_DATA_BUF_SIZE);
10163 list_for_each_entry(dmabuf, &drq->page_list, list) {
10164 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10165 putPaddrLow(dmabuf->phys);
10166 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10167 putPaddrHigh(dmabuf->phys);
10168 }
10169 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10170 /* The IOCTL status is embedded in the mailbox subheader. */
10171 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10172 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10173 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10174 if (shdr_status || shdr_add_status || rc) {
10175 status = -ENXIO;
10176 goto out;
10177 }
10178 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10179 if (drq->queue_id == 0xFFFF) {
10180 status = -ENXIO;
10181 goto out;
10182 }
10183 drq->type = LPFC_DRQ;
10184 drq->subtype = subtype;
10185 drq->host_index = 0;
10186 drq->hba_index = 0;
10187
10188 /* link the header and data RQs onto the parent cq child list */
10189 list_add_tail(&hrq->list, &cq->child_list);
10190 list_add_tail(&drq->list, &cq->child_list);
10191
10192out:
James Smart8fa38512009-07-19 10:01:03 -040010193 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010194 return status;
10195}
10196
10197/**
10198 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10199 * @eq: The queue structure associated with the queue to destroy.
10200 *
10201 * This function destroys a queue, as detailed in @eq by sending an mailbox
10202 * command, specific to the type of queue, to the HBA.
10203 *
10204 * The @eq struct is used to get the queue ID of the queue to destroy.
10205 *
10206 * On success this function will return a zero. If the queue destroy mailbox
10207 * command fails this function will return ENXIO.
10208 **/
10209uint32_t
10210lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10211{
10212 LPFC_MBOXQ_t *mbox;
10213 int rc, length, status = 0;
10214 uint32_t shdr_status, shdr_add_status;
10215 union lpfc_sli4_cfg_shdr *shdr;
10216
10217 if (!eq)
10218 return -ENODEV;
10219 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10220 if (!mbox)
10221 return -ENOMEM;
10222 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10223 sizeof(struct lpfc_sli4_cfg_mhdr));
10224 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10225 LPFC_MBOX_OPCODE_EQ_DESTROY,
10226 length, LPFC_SLI4_MBX_EMBED);
10227 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10228 eq->queue_id);
10229 mbox->vport = eq->phba->pport;
10230 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10231
10232 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10233 /* The IOCTL status is embedded in the mailbox subheader. */
10234 shdr = (union lpfc_sli4_cfg_shdr *)
10235 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10236 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10237 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10238 if (shdr_status || shdr_add_status || rc) {
10239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10240 "2505 EQ_DESTROY mailbox failed with "
10241 "status x%x add_status x%x, mbx status x%x\n",
10242 shdr_status, shdr_add_status, rc);
10243 status = -ENXIO;
10244 }
10245
10246 /* Remove eq from any list */
10247 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010248 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010249 return status;
10250}
10251
10252/**
10253 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10254 * @cq: The queue structure associated with the queue to destroy.
10255 *
10256 * This function destroys a queue, as detailed in @cq by sending an mailbox
10257 * command, specific to the type of queue, to the HBA.
10258 *
10259 * The @cq struct is used to get the queue ID of the queue to destroy.
10260 *
10261 * On success this function will return a zero. If the queue destroy mailbox
10262 * command fails this function will return ENXIO.
10263 **/
10264uint32_t
10265lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10266{
10267 LPFC_MBOXQ_t *mbox;
10268 int rc, length, status = 0;
10269 uint32_t shdr_status, shdr_add_status;
10270 union lpfc_sli4_cfg_shdr *shdr;
10271
10272 if (!cq)
10273 return -ENODEV;
10274 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10275 if (!mbox)
10276 return -ENOMEM;
10277 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10278 sizeof(struct lpfc_sli4_cfg_mhdr));
10279 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10280 LPFC_MBOX_OPCODE_CQ_DESTROY,
10281 length, LPFC_SLI4_MBX_EMBED);
10282 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10283 cq->queue_id);
10284 mbox->vport = cq->phba->pport;
10285 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10286 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10287 /* The IOCTL status is embedded in the mailbox subheader. */
10288 shdr = (union lpfc_sli4_cfg_shdr *)
10289 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10292 if (shdr_status || shdr_add_status || rc) {
10293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10294 "2506 CQ_DESTROY mailbox failed with "
10295 "status x%x add_status x%x, mbx status x%x\n",
10296 shdr_status, shdr_add_status, rc);
10297 status = -ENXIO;
10298 }
10299 /* Remove cq from any list */
10300 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010301 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010302 return status;
10303}
10304
10305/**
James Smart04c68492009-05-22 14:52:52 -040010306 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10307 * @qm: The queue structure associated with the queue to destroy.
10308 *
10309 * This function destroys a queue, as detailed in @mq by sending an mailbox
10310 * command, specific to the type of queue, to the HBA.
10311 *
10312 * The @mq struct is used to get the queue ID of the queue to destroy.
10313 *
10314 * On success this function will return a zero. If the queue destroy mailbox
10315 * command fails this function will return ENXIO.
10316 **/
10317uint32_t
10318lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10319{
10320 LPFC_MBOXQ_t *mbox;
10321 int rc, length, status = 0;
10322 uint32_t shdr_status, shdr_add_status;
10323 union lpfc_sli4_cfg_shdr *shdr;
10324
10325 if (!mq)
10326 return -ENODEV;
10327 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10328 if (!mbox)
10329 return -ENOMEM;
10330 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10331 sizeof(struct lpfc_sli4_cfg_mhdr));
10332 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10333 LPFC_MBOX_OPCODE_MQ_DESTROY,
10334 length, LPFC_SLI4_MBX_EMBED);
10335 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10336 mq->queue_id);
10337 mbox->vport = mq->phba->pport;
10338 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10339 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10340 /* The IOCTL status is embedded in the mailbox subheader. */
10341 shdr = (union lpfc_sli4_cfg_shdr *)
10342 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10343 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10344 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10345 if (shdr_status || shdr_add_status || rc) {
10346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10347 "2507 MQ_DESTROY mailbox failed with "
10348 "status x%x add_status x%x, mbx status x%x\n",
10349 shdr_status, shdr_add_status, rc);
10350 status = -ENXIO;
10351 }
10352 /* Remove mq from any list */
10353 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010354 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010355 return status;
10356}
10357
10358/**
James Smart4f774512009-05-22 14:52:35 -040010359 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10360 * @wq: The queue structure associated with the queue to destroy.
10361 *
10362 * This function destroys a queue, as detailed in @wq by sending an mailbox
10363 * command, specific to the type of queue, to the HBA.
10364 *
10365 * The @wq struct is used to get the queue ID of the queue to destroy.
10366 *
10367 * On success this function will return a zero. If the queue destroy mailbox
10368 * command fails this function will return ENXIO.
10369 **/
10370uint32_t
10371lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10372{
10373 LPFC_MBOXQ_t *mbox;
10374 int rc, length, status = 0;
10375 uint32_t shdr_status, shdr_add_status;
10376 union lpfc_sli4_cfg_shdr *shdr;
10377
10378 if (!wq)
10379 return -ENODEV;
10380 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10381 if (!mbox)
10382 return -ENOMEM;
10383 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10384 sizeof(struct lpfc_sli4_cfg_mhdr));
10385 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10386 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10387 length, LPFC_SLI4_MBX_EMBED);
10388 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10389 wq->queue_id);
10390 mbox->vport = wq->phba->pport;
10391 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10392 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10393 shdr = (union lpfc_sli4_cfg_shdr *)
10394 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10395 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10397 if (shdr_status || shdr_add_status || rc) {
10398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10399 "2508 WQ_DESTROY mailbox failed with "
10400 "status x%x add_status x%x, mbx status x%x\n",
10401 shdr_status, shdr_add_status, rc);
10402 status = -ENXIO;
10403 }
10404 /* Remove wq from any list */
10405 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010406 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010407 return status;
10408}
10409
10410/**
10411 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10412 * @rq: The queue structure associated with the queue to destroy.
10413 *
10414 * This function destroys a queue, as detailed in @rq by sending an mailbox
10415 * command, specific to the type of queue, to the HBA.
10416 *
10417 * The @rq struct is used to get the queue ID of the queue to destroy.
10418 *
10419 * On success this function will return a zero. If the queue destroy mailbox
10420 * command fails this function will return ENXIO.
10421 **/
10422uint32_t
10423lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10424 struct lpfc_queue *drq)
10425{
10426 LPFC_MBOXQ_t *mbox;
10427 int rc, length, status = 0;
10428 uint32_t shdr_status, shdr_add_status;
10429 union lpfc_sli4_cfg_shdr *shdr;
10430
10431 if (!hrq || !drq)
10432 return -ENODEV;
10433 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10434 if (!mbox)
10435 return -ENOMEM;
10436 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10437 sizeof(struct mbox_header));
10438 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10439 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10440 length, LPFC_SLI4_MBX_EMBED);
10441 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10442 hrq->queue_id);
10443 mbox->vport = hrq->phba->pport;
10444 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10445 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10446 /* The IOCTL status is embedded in the mailbox subheader. */
10447 shdr = (union lpfc_sli4_cfg_shdr *)
10448 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10449 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10450 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10451 if (shdr_status || shdr_add_status || rc) {
10452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10453 "2509 RQ_DESTROY mailbox failed with "
10454 "status x%x add_status x%x, mbx status x%x\n",
10455 shdr_status, shdr_add_status, rc);
10456 if (rc != MBX_TIMEOUT)
10457 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10458 return -ENXIO;
10459 }
10460 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10461 drq->queue_id);
10462 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10463 shdr = (union lpfc_sli4_cfg_shdr *)
10464 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10465 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10466 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10467 if (shdr_status || shdr_add_status || rc) {
10468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10469 "2510 RQ_DESTROY mailbox failed with "
10470 "status x%x add_status x%x, mbx status x%x\n",
10471 shdr_status, shdr_add_status, rc);
10472 status = -ENXIO;
10473 }
10474 list_del_init(&hrq->list);
10475 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010476 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010477 return status;
10478}
10479
10480/**
10481 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10482 * @phba: The virtual port for which this call being executed.
10483 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10484 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10485 * @xritag: the xritag that ties this io to the SGL pages.
10486 *
10487 * This routine will post the sgl pages for the IO that has the xritag
10488 * that is in the iocbq structure. The xritag is assigned during iocbq
10489 * creation and persists for as long as the driver is loaded.
10490 * if the caller has fewer than 256 scatter gather segments to map then
10491 * pdma_phys_addr1 should be 0.
10492 * If the caller needs to map more than 256 scatter gather segment then
10493 * pdma_phys_addr1 should be a valid physical address.
10494 * physical address for SGLs must be 64 byte aligned.
10495 * If you are going to map 2 SGL's then the first one must have 256 entries
10496 * the second sgl can have between 1 and 256 entries.
10497 *
10498 * Return codes:
10499 * 0 - Success
10500 * -ENXIO, -ENOMEM - Failure
10501 **/
10502int
10503lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10504 dma_addr_t pdma_phys_addr0,
10505 dma_addr_t pdma_phys_addr1,
10506 uint16_t xritag)
10507{
10508 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10509 LPFC_MBOXQ_t *mbox;
10510 int rc;
10511 uint32_t shdr_status, shdr_add_status;
10512 union lpfc_sli4_cfg_shdr *shdr;
10513
10514 if (xritag == NO_XRI) {
10515 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10516 "0364 Invalid param:\n");
10517 return -EINVAL;
10518 }
10519
10520 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10521 if (!mbox)
10522 return -ENOMEM;
10523
10524 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10525 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10526 sizeof(struct lpfc_mbx_post_sgl_pages) -
10527 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10528
10529 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10530 &mbox->u.mqe.un.post_sgl_pages;
10531 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10532 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10533
10534 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10535 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10536 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10537 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10538
10539 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10540 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10541 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10542 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10543 if (!phba->sli4_hba.intr_enable)
10544 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10545 else
10546 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10547 /* The IOCTL status is embedded in the mailbox subheader. */
10548 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10549 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10550 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10551 if (rc != MBX_TIMEOUT)
10552 mempool_free(mbox, phba->mbox_mem_pool);
10553 if (shdr_status || shdr_add_status || rc) {
10554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10555 "2511 POST_SGL mailbox failed with "
10556 "status x%x add_status x%x, mbx status x%x\n",
10557 shdr_status, shdr_add_status, rc);
10558 rc = -ENXIO;
10559 }
10560 return 0;
10561}
10562/**
10563 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10564 * @phba: The virtual port for which this call being executed.
10565 *
10566 * This routine will remove all of the sgl pages registered with the hba.
10567 *
10568 * Return codes:
10569 * 0 - Success
10570 * -ENXIO, -ENOMEM - Failure
10571 **/
10572int
10573lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10574{
10575 LPFC_MBOXQ_t *mbox;
10576 int rc;
10577 uint32_t shdr_status, shdr_add_status;
10578 union lpfc_sli4_cfg_shdr *shdr;
10579
10580 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10581 if (!mbox)
10582 return -ENOMEM;
10583
10584 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10585 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10586 LPFC_SLI4_MBX_EMBED);
10587 if (!phba->sli4_hba.intr_enable)
10588 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10589 else
10590 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10591 /* The IOCTL status is embedded in the mailbox subheader. */
10592 shdr = (union lpfc_sli4_cfg_shdr *)
10593 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10594 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10595 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10596 if (rc != MBX_TIMEOUT)
10597 mempool_free(mbox, phba->mbox_mem_pool);
10598 if (shdr_status || shdr_add_status || rc) {
10599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10600 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10601 "status x%x add_status x%x, mbx status x%x\n",
10602 shdr_status, shdr_add_status, rc);
10603 rc = -ENXIO;
10604 }
10605 return rc;
10606}
10607
10608/**
10609 * lpfc_sli4_next_xritag - Get an xritag for the io
10610 * @phba: Pointer to HBA context object.
10611 *
10612 * This function gets an xritag for the iocb. If there is no unused xritag
10613 * it will return 0xffff.
10614 * The function returns the allocated xritag if successful, else returns zero.
10615 * Zero is not a valid xritag.
10616 * The caller is not required to hold any lock.
10617 **/
10618uint16_t
10619lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10620{
10621 uint16_t xritag;
10622
10623 spin_lock_irq(&phba->hbalock);
10624 xritag = phba->sli4_hba.next_xri;
10625 if ((xritag != (uint16_t) -1) && xritag <
10626 (phba->sli4_hba.max_cfg_param.max_xri
10627 + phba->sli4_hba.max_cfg_param.xri_base)) {
10628 phba->sli4_hba.next_xri++;
10629 phba->sli4_hba.max_cfg_param.xri_used++;
10630 spin_unlock_irq(&phba->hbalock);
10631 return xritag;
10632 }
10633 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010634 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010635 "2004 Failed to allocate XRI.last XRITAG is %d"
10636 " Max XRI is %d, Used XRI is %d\n",
10637 phba->sli4_hba.next_xri,
10638 phba->sli4_hba.max_cfg_param.max_xri,
10639 phba->sli4_hba.max_cfg_param.xri_used);
10640 return -1;
10641}
10642
10643/**
10644 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10645 * @phba: pointer to lpfc hba data structure.
10646 *
10647 * This routine is invoked to post a block of driver's sgl pages to the
10648 * HBA using non-embedded mailbox command. No Lock is held. This routine
10649 * is only called when the driver is loading and after all IO has been
10650 * stopped.
10651 **/
10652int
10653lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10654{
10655 struct lpfc_sglq *sglq_entry;
10656 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10657 struct sgl_page_pairs *sgl_pg_pairs;
10658 void *viraddr;
10659 LPFC_MBOXQ_t *mbox;
10660 uint32_t reqlen, alloclen, pg_pairs;
10661 uint32_t mbox_tmo;
10662 uint16_t xritag_start = 0;
10663 int els_xri_cnt, rc = 0;
10664 uint32_t shdr_status, shdr_add_status;
10665 union lpfc_sli4_cfg_shdr *shdr;
10666
10667 /* The number of sgls to be posted */
10668 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10669
10670 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10671 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10672 if (reqlen > PAGE_SIZE) {
10673 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10674 "2559 Block sgl registration required DMA "
10675 "size (%d) great than a page\n", reqlen);
10676 return -ENOMEM;
10677 }
10678 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10679 if (!mbox) {
10680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10681 "2560 Failed to allocate mbox cmd memory\n");
10682 return -ENOMEM;
10683 }
10684
10685 /* Allocate DMA memory and set up the non-embedded mailbox command */
10686 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10687 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10688 LPFC_SLI4_MBX_NEMBED);
10689
10690 if (alloclen < reqlen) {
10691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10692 "0285 Allocated DMA memory size (%d) is "
10693 "less than the requested DMA memory "
10694 "size (%d)\n", alloclen, reqlen);
10695 lpfc_sli4_mbox_cmd_free(phba, mbox);
10696 return -ENOMEM;
10697 }
James Smart4f774512009-05-22 14:52:35 -040010698 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010699 viraddr = mbox->sge_array->addr[0];
10700
10701 /* Set up the SGL pages in the non-embedded DMA pages */
10702 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10703 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10704
10705 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10706 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10707 /* Set up the sge entry */
10708 sgl_pg_pairs->sgl_pg0_addr_lo =
10709 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10710 sgl_pg_pairs->sgl_pg0_addr_hi =
10711 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10712 sgl_pg_pairs->sgl_pg1_addr_lo =
10713 cpu_to_le32(putPaddrLow(0));
10714 sgl_pg_pairs->sgl_pg1_addr_hi =
10715 cpu_to_le32(putPaddrHigh(0));
10716 /* Keep the first xritag on the list */
10717 if (pg_pairs == 0)
10718 xritag_start = sglq_entry->sli4_xritag;
10719 sgl_pg_pairs++;
10720 }
10721 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010722 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010723 /* Perform endian conversion if necessary */
10724 sgl->word0 = cpu_to_le32(sgl->word0);
10725
10726 if (!phba->sli4_hba.intr_enable)
10727 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10728 else {
10729 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10730 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10731 }
10732 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10733 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10734 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10735 if (rc != MBX_TIMEOUT)
10736 lpfc_sli4_mbox_cmd_free(phba, mbox);
10737 if (shdr_status || shdr_add_status || rc) {
10738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10739 "2513 POST_SGL_BLOCK mailbox command failed "
10740 "status x%x add_status x%x mbx status x%x\n",
10741 shdr_status, shdr_add_status, rc);
10742 rc = -ENXIO;
10743 }
10744 return rc;
10745}
10746
10747/**
10748 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10749 * @phba: pointer to lpfc hba data structure.
10750 * @sblist: pointer to scsi buffer list.
10751 * @count: number of scsi buffers on the list.
10752 *
10753 * This routine is invoked to post a block of @count scsi sgl pages from a
10754 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10755 * No Lock is held.
10756 *
10757 **/
10758int
10759lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10760 int cnt)
10761{
10762 struct lpfc_scsi_buf *psb;
10763 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10764 struct sgl_page_pairs *sgl_pg_pairs;
10765 void *viraddr;
10766 LPFC_MBOXQ_t *mbox;
10767 uint32_t reqlen, alloclen, pg_pairs;
10768 uint32_t mbox_tmo;
10769 uint16_t xritag_start = 0;
10770 int rc = 0;
10771 uint32_t shdr_status, shdr_add_status;
10772 dma_addr_t pdma_phys_bpl1;
10773 union lpfc_sli4_cfg_shdr *shdr;
10774
10775 /* Calculate the requested length of the dma memory */
10776 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10777 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10778 if (reqlen > PAGE_SIZE) {
10779 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10780 "0217 Block sgl registration required DMA "
10781 "size (%d) great than a page\n", reqlen);
10782 return -ENOMEM;
10783 }
10784 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10785 if (!mbox) {
10786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10787 "0283 Failed to allocate mbox cmd memory\n");
10788 return -ENOMEM;
10789 }
10790
10791 /* Allocate DMA memory and set up the non-embedded mailbox command */
10792 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10793 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10794 LPFC_SLI4_MBX_NEMBED);
10795
10796 if (alloclen < reqlen) {
10797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10798 "2561 Allocated DMA memory size (%d) is "
10799 "less than the requested DMA memory "
10800 "size (%d)\n", alloclen, reqlen);
10801 lpfc_sli4_mbox_cmd_free(phba, mbox);
10802 return -ENOMEM;
10803 }
James Smart4f774512009-05-22 14:52:35 -040010804 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010805 viraddr = mbox->sge_array->addr[0];
10806
10807 /* Set up the SGL pages in the non-embedded DMA pages */
10808 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10809 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10810
10811 pg_pairs = 0;
10812 list_for_each_entry(psb, sblist, list) {
10813 /* Set up the sge entry */
10814 sgl_pg_pairs->sgl_pg0_addr_lo =
10815 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10816 sgl_pg_pairs->sgl_pg0_addr_hi =
10817 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10818 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10819 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10820 else
10821 pdma_phys_bpl1 = 0;
10822 sgl_pg_pairs->sgl_pg1_addr_lo =
10823 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10824 sgl_pg_pairs->sgl_pg1_addr_hi =
10825 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10826 /* Keep the first xritag on the list */
10827 if (pg_pairs == 0)
10828 xritag_start = psb->cur_iocbq.sli4_xritag;
10829 sgl_pg_pairs++;
10830 pg_pairs++;
10831 }
10832 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10833 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10834 /* Perform endian conversion if necessary */
10835 sgl->word0 = cpu_to_le32(sgl->word0);
10836
10837 if (!phba->sli4_hba.intr_enable)
10838 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10839 else {
10840 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10841 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10842 }
10843 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10844 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10845 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10846 if (rc != MBX_TIMEOUT)
10847 lpfc_sli4_mbox_cmd_free(phba, mbox);
10848 if (shdr_status || shdr_add_status || rc) {
10849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10850 "2564 POST_SGL_BLOCK mailbox command failed "
10851 "status x%x add_status x%x mbx status x%x\n",
10852 shdr_status, shdr_add_status, rc);
10853 rc = -ENXIO;
10854 }
10855 return rc;
10856}
10857
10858/**
10859 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10860 * @phba: pointer to lpfc_hba struct that the frame was received on
10861 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10862 *
10863 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10864 * valid type of frame that the LPFC driver will handle. This function will
10865 * return a zero if the frame is a valid frame or a non zero value when the
10866 * frame does not pass the check.
10867 **/
10868static int
10869lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10870{
10871 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10872 char *type_names[] = FC_TYPE_NAMES_INIT;
10873 struct fc_vft_header *fc_vft_hdr;
10874
10875 switch (fc_hdr->fh_r_ctl) {
10876 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10877 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10878 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10879 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10880 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10881 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10882 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10883 case FC_RCTL_DD_CMD_STATUS: /* command status */
10884 case FC_RCTL_ELS_REQ: /* extended link services request */
10885 case FC_RCTL_ELS_REP: /* extended link services reply */
10886 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10887 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10888 case FC_RCTL_BA_NOP: /* basic link service NOP */
10889 case FC_RCTL_BA_ABTS: /* basic link service abort */
10890 case FC_RCTL_BA_RMC: /* remove connection */
10891 case FC_RCTL_BA_ACC: /* basic accept */
10892 case FC_RCTL_BA_RJT: /* basic reject */
10893 case FC_RCTL_BA_PRMT:
10894 case FC_RCTL_ACK_1: /* acknowledge_1 */
10895 case FC_RCTL_ACK_0: /* acknowledge_0 */
10896 case FC_RCTL_P_RJT: /* port reject */
10897 case FC_RCTL_F_RJT: /* fabric reject */
10898 case FC_RCTL_P_BSY: /* port busy */
10899 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10900 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10901 case FC_RCTL_LCR: /* link credit reset */
10902 case FC_RCTL_END: /* end */
10903 break;
10904 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10905 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10906 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10907 return lpfc_fc_frame_check(phba, fc_hdr);
10908 default:
10909 goto drop;
10910 }
10911 switch (fc_hdr->fh_type) {
10912 case FC_TYPE_BLS:
10913 case FC_TYPE_ELS:
10914 case FC_TYPE_FCP:
10915 case FC_TYPE_CT:
10916 break;
10917 case FC_TYPE_IP:
10918 case FC_TYPE_ILS:
10919 default:
10920 goto drop;
10921 }
10922 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10923 "2538 Received frame rctl:%s type:%s\n",
10924 rctl_names[fc_hdr->fh_r_ctl],
10925 type_names[fc_hdr->fh_type]);
10926 return 0;
10927drop:
10928 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10929 "2539 Dropped frame rctl:%s type:%s\n",
10930 rctl_names[fc_hdr->fh_r_ctl],
10931 type_names[fc_hdr->fh_type]);
10932 return 1;
10933}
10934
10935/**
10936 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10937 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10938 *
10939 * This function processes the FC header to retrieve the VFI from the VF
10940 * header, if one exists. This function will return the VFI if one exists
10941 * or 0 if no VSAN Header exists.
10942 **/
10943static uint32_t
10944lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10945{
10946 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10947
10948 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10949 return 0;
10950 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10951}
10952
10953/**
10954 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10955 * @phba: Pointer to the HBA structure to search for the vport on
10956 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10957 * @fcfi: The FC Fabric ID that the frame came from
10958 *
10959 * This function searches the @phba for a vport that matches the content of the
10960 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10961 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10962 * returns the matching vport pointer or NULL if unable to match frame to a
10963 * vport.
10964 **/
10965static struct lpfc_vport *
10966lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10967 uint16_t fcfi)
10968{
10969 struct lpfc_vport **vports;
10970 struct lpfc_vport *vport = NULL;
10971 int i;
10972 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10973 fc_hdr->fh_d_id[1] << 8 |
10974 fc_hdr->fh_d_id[2]);
10975
10976 vports = lpfc_create_vport_work_array(phba);
10977 if (vports != NULL)
10978 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10979 if (phba->fcf.fcfi == fcfi &&
10980 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10981 vports[i]->fc_myDID == did) {
10982 vport = vports[i];
10983 break;
10984 }
10985 }
10986 lpfc_destroy_vport_work_array(phba, vports);
10987 return vport;
10988}
10989
10990/**
James Smart45ed1192009-10-02 15:17:02 -040010991 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10992 * @vport: The vport to work on.
10993 *
10994 * This function updates the receive sequence time stamp for this vport. The
10995 * receive sequence time stamp indicates the time that the last frame of the
10996 * the sequence that has been idle for the longest amount of time was received.
10997 * the driver uses this time stamp to indicate if any received sequences have
10998 * timed out.
10999 **/
11000void
11001lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11002{
11003 struct lpfc_dmabuf *h_buf;
11004 struct hbq_dmabuf *dmabuf = NULL;
11005
11006 /* get the oldest sequence on the rcv list */
11007 h_buf = list_get_first(&vport->rcv_buffer_list,
11008 struct lpfc_dmabuf, list);
11009 if (!h_buf)
11010 return;
11011 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11012 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11013}
11014
11015/**
11016 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11017 * @vport: The vport that the received sequences were sent to.
11018 *
11019 * This function cleans up all outstanding received sequences. This is called
11020 * by the driver when a link event or user action invalidates all the received
11021 * sequences.
11022 **/
11023void
11024lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11025{
11026 struct lpfc_dmabuf *h_buf, *hnext;
11027 struct lpfc_dmabuf *d_buf, *dnext;
11028 struct hbq_dmabuf *dmabuf = NULL;
11029
11030 /* start with the oldest sequence on the rcv list */
11031 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11032 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11033 list_del_init(&dmabuf->hbuf.list);
11034 list_for_each_entry_safe(d_buf, dnext,
11035 &dmabuf->dbuf.list, list) {
11036 list_del_init(&d_buf->list);
11037 lpfc_in_buf_free(vport->phba, d_buf);
11038 }
11039 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11040 }
11041}
11042
11043/**
11044 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11045 * @vport: The vport that the received sequences were sent to.
11046 *
11047 * This function determines whether any received sequences have timed out by
11048 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11049 * indicates that there is at least one timed out sequence this routine will
11050 * go through the received sequences one at a time from most inactive to most
11051 * active to determine which ones need to be cleaned up. Once it has determined
11052 * that a sequence needs to be cleaned up it will simply free up the resources
11053 * without sending an abort.
11054 **/
11055void
11056lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11057{
11058 struct lpfc_dmabuf *h_buf, *hnext;
11059 struct lpfc_dmabuf *d_buf, *dnext;
11060 struct hbq_dmabuf *dmabuf = NULL;
11061 unsigned long timeout;
11062 int abort_count = 0;
11063
11064 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11065 vport->rcv_buffer_time_stamp);
11066 if (list_empty(&vport->rcv_buffer_list) ||
11067 time_before(jiffies, timeout))
11068 return;
11069 /* start with the oldest sequence on the rcv list */
11070 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11071 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11072 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11073 dmabuf->time_stamp);
11074 if (time_before(jiffies, timeout))
11075 break;
11076 abort_count++;
11077 list_del_init(&dmabuf->hbuf.list);
11078 list_for_each_entry_safe(d_buf, dnext,
11079 &dmabuf->dbuf.list, list) {
11080 list_del_init(&d_buf->list);
11081 lpfc_in_buf_free(vport->phba, d_buf);
11082 }
11083 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11084 }
11085 if (abort_count)
11086 lpfc_update_rcv_time_stamp(vport);
11087}
11088
11089/**
James Smart4f774512009-05-22 14:52:35 -040011090 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11091 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11092 *
11093 * This function searches through the existing incomplete sequences that have
11094 * been sent to this @vport. If the frame matches one of the incomplete
11095 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11096 * make up that sequence. If no sequence is found that matches this frame then
11097 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11098 * This function returns a pointer to the first dmabuf in the sequence list that
11099 * the frame was linked to.
11100 **/
11101static struct hbq_dmabuf *
11102lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11103{
11104 struct fc_frame_header *new_hdr;
11105 struct fc_frame_header *temp_hdr;
11106 struct lpfc_dmabuf *d_buf;
11107 struct lpfc_dmabuf *h_buf;
11108 struct hbq_dmabuf *seq_dmabuf = NULL;
11109 struct hbq_dmabuf *temp_dmabuf = NULL;
11110
James Smart4d9ab992009-10-02 15:16:39 -040011111 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011112 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011113 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11114 /* Use the hdr_buf to find the sequence that this frame belongs to */
11115 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11116 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11117 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11118 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11119 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11120 continue;
11121 /* found a pending sequence that matches this frame */
11122 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11123 break;
11124 }
11125 if (!seq_dmabuf) {
11126 /*
11127 * This indicates first frame received for this sequence.
11128 * Queue the buffer on the vport's rcv_buffer_list.
11129 */
11130 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011131 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011132 return dmabuf;
11133 }
11134 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011135 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11136 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011137 list_del_init(&seq_dmabuf->hbuf.list);
11138 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11139 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011140 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011141 return dmabuf;
11142 }
James Smart45ed1192009-10-02 15:17:02 -040011143 /* move this sequence to the tail to indicate a young sequence */
11144 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11145 seq_dmabuf->time_stamp = jiffies;
11146 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011147 if (list_empty(&seq_dmabuf->dbuf.list)) {
11148 temp_hdr = dmabuf->hbuf.virt;
11149 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11150 return seq_dmabuf;
11151 }
James Smart4f774512009-05-22 14:52:35 -040011152 /* find the correct place in the sequence to insert this frame */
11153 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11154 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11155 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11156 /*
11157 * If the frame's sequence count is greater than the frame on
11158 * the list then insert the frame right after this frame
11159 */
James Smarteeead812009-12-21 17:01:23 -050011160 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11161 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011162 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11163 return seq_dmabuf;
11164 }
11165 }
11166 return NULL;
11167}
11168
11169/**
James Smart6669f9b2009-10-02 15:16:45 -040011170 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11171 * @vport: pointer to a vitural port
11172 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11173 *
11174 * This function tries to abort from the partially assembed sequence, described
11175 * by the information from basic abbort @dmabuf. It checks to see whether such
11176 * partially assembled sequence held by the driver. If so, it shall free up all
11177 * the frames from the partially assembled sequence.
11178 *
11179 * Return
11180 * true -- if there is matching partially assembled sequence present and all
11181 * the frames freed with the sequence;
11182 * false -- if there is no matching partially assembled sequence present so
11183 * nothing got aborted in the lower layer driver
11184 **/
11185static bool
11186lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11187 struct hbq_dmabuf *dmabuf)
11188{
11189 struct fc_frame_header *new_hdr;
11190 struct fc_frame_header *temp_hdr;
11191 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11192 struct hbq_dmabuf *seq_dmabuf = NULL;
11193
11194 /* Use the hdr_buf to find the sequence that matches this frame */
11195 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11196 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11197 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11198 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11199 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11200 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11201 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11202 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11203 continue;
11204 /* found a pending sequence that matches this frame */
11205 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11206 break;
11207 }
11208
11209 /* Free up all the frames from the partially assembled sequence */
11210 if (seq_dmabuf) {
11211 list_for_each_entry_safe(d_buf, n_buf,
11212 &seq_dmabuf->dbuf.list, list) {
11213 list_del_init(&d_buf->list);
11214 lpfc_in_buf_free(vport->phba, d_buf);
11215 }
11216 return true;
11217 }
11218 return false;
11219}
11220
11221/**
11222 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11223 * @phba: Pointer to HBA context object.
11224 * @cmd_iocbq: pointer to the command iocbq structure.
11225 * @rsp_iocbq: pointer to the response iocbq structure.
11226 *
11227 * This function handles the sequence abort accept iocb command complete
11228 * event. It properly releases the memory allocated to the sequence abort
11229 * accept iocb.
11230 **/
11231static void
11232lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11233 struct lpfc_iocbq *cmd_iocbq,
11234 struct lpfc_iocbq *rsp_iocbq)
11235{
11236 if (cmd_iocbq)
11237 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11238}
11239
11240/**
11241 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11242 * @phba: Pointer to HBA context object.
11243 * @fc_hdr: pointer to a FC frame header.
11244 *
11245 * This function sends a basic accept to a previous unsol sequence abort
11246 * event after aborting the sequence handling.
11247 **/
11248static void
11249lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11250 struct fc_frame_header *fc_hdr)
11251{
11252 struct lpfc_iocbq *ctiocb = NULL;
11253 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011254 uint16_t oxid, rxid;
11255 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011256 IOCB_t *icmd;
11257
11258 if (!lpfc_is_link_up(phba))
11259 return;
11260
11261 sid = sli4_sid_from_fc_hdr(fc_hdr);
11262 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011263 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011264
11265 ndlp = lpfc_findnode_did(phba->pport, sid);
11266 if (!ndlp) {
11267 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11268 "1268 Find ndlp returned NULL for oxid:x%x "
11269 "SID:x%x\n", oxid, sid);
11270 return;
11271 }
11272
11273 /* Allocate buffer for acc iocb */
11274 ctiocb = lpfc_sli_get_iocbq(phba);
11275 if (!ctiocb)
11276 return;
11277
James Smart5ffc2662009-11-18 15:39:44 -050011278 /* Extract the F_CTL field from FC_HDR */
11279 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11280
James Smart6669f9b2009-10-02 15:16:45 -040011281 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011282 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011283 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011284 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11285 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11286 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11287
11288 /* Fill in the rest of iocb fields */
11289 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11290 icmd->ulpBdeCount = 0;
11291 icmd->ulpLe = 1;
11292 icmd->ulpClass = CLASS3;
11293 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011294
James Smart6669f9b2009-10-02 15:16:45 -040011295 ctiocb->iocb_cmpl = NULL;
11296 ctiocb->vport = phba->pport;
11297 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11298
James Smart5ffc2662009-11-18 15:39:44 -050011299 if (fctl & FC_FC_EX_CTX) {
11300 /* ABTS sent by responder to CT exchange, construction
11301 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11302 * field and RX_ID from ABTS for RX_ID field.
11303 */
11304 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11305 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11306 ctiocb->sli4_xritag = oxid;
11307 } else {
11308 /* ABTS sent by initiator to CT exchange, construction
11309 * of BA_ACC will need to allocate a new XRI as for the
11310 * XRI_TAG and RX_ID fields.
11311 */
11312 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11313 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11314 ctiocb->sli4_xritag = NO_XRI;
11315 }
11316 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11317
James Smart6669f9b2009-10-02 15:16:45 -040011318 /* Xmit CT abts accept on exchange <xid> */
11319 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11320 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11321 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11322 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11323}
11324
11325/**
11326 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11327 * @vport: Pointer to the vport on which this sequence was received
11328 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11329 *
11330 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11331 * receive sequence is only partially assembed by the driver, it shall abort
11332 * the partially assembled frames for the sequence. Otherwise, if the
11333 * unsolicited receive sequence has been completely assembled and passed to
11334 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11335 * unsolicited sequence has been aborted. After that, it will issue a basic
11336 * accept to accept the abort.
11337 **/
11338void
11339lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11340 struct hbq_dmabuf *dmabuf)
11341{
11342 struct lpfc_hba *phba = vport->phba;
11343 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011344 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011345 bool abts_par;
11346
James Smart6669f9b2009-10-02 15:16:45 -040011347 /* Make a copy of fc_hdr before the dmabuf being released */
11348 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011349 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011350
James Smart5ffc2662009-11-18 15:39:44 -050011351 if (fctl & FC_FC_EX_CTX) {
11352 /*
11353 * ABTS sent by responder to exchange, just free the buffer
11354 */
James Smart6669f9b2009-10-02 15:16:45 -040011355 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011356 } else {
11357 /*
11358 * ABTS sent by initiator to exchange, need to do cleanup
11359 */
11360 /* Try to abort partially assembled seq */
11361 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11362
11363 /* Send abort to ULP if partially seq abort failed */
11364 if (abts_par == false)
11365 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11366 else
11367 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11368 }
James Smart6669f9b2009-10-02 15:16:45 -040011369 /* Send basic accept (BA_ACC) to the abort requester */
11370 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11371}
11372
11373/**
James Smart4f774512009-05-22 14:52:35 -040011374 * lpfc_seq_complete - Indicates if a sequence is complete
11375 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11376 *
11377 * This function checks the sequence, starting with the frame described by
11378 * @dmabuf, to see if all the frames associated with this sequence are present.
11379 * the frames associated with this sequence are linked to the @dmabuf using the
11380 * dbuf list. This function looks for two major things. 1) That the first frame
11381 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11382 * set. 3) That there are no holes in the sequence count. The function will
11383 * return 1 when the sequence is complete, otherwise it will return 0.
11384 **/
11385static int
11386lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11387{
11388 struct fc_frame_header *hdr;
11389 struct lpfc_dmabuf *d_buf;
11390 struct hbq_dmabuf *seq_dmabuf;
11391 uint32_t fctl;
11392 int seq_count = 0;
11393
11394 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11395 /* make sure first fame of sequence has a sequence count of zero */
11396 if (hdr->fh_seq_cnt != seq_count)
11397 return 0;
11398 fctl = (hdr->fh_f_ctl[0] << 16 |
11399 hdr->fh_f_ctl[1] << 8 |
11400 hdr->fh_f_ctl[2]);
11401 /* If last frame of sequence we can return success. */
11402 if (fctl & FC_FC_END_SEQ)
11403 return 1;
11404 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11405 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11406 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11407 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011408 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011409 return 0;
11410 fctl = (hdr->fh_f_ctl[0] << 16 |
11411 hdr->fh_f_ctl[1] << 8 |
11412 hdr->fh_f_ctl[2]);
11413 /* If last frame of sequence we can return success. */
11414 if (fctl & FC_FC_END_SEQ)
11415 return 1;
11416 }
11417 return 0;
11418}
11419
11420/**
11421 * lpfc_prep_seq - Prep sequence for ULP processing
11422 * @vport: Pointer to the vport on which this sequence was received
11423 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11424 *
11425 * This function takes a sequence, described by a list of frames, and creates
11426 * a list of iocbq structures to describe the sequence. This iocbq list will be
11427 * used to issue to the generic unsolicited sequence handler. This routine
11428 * returns a pointer to the first iocbq in the list. If the function is unable
11429 * to allocate an iocbq then it throw out the received frames that were not
11430 * able to be described and return a pointer to the first iocbq. If unable to
11431 * allocate any iocbqs (including the first) this function will return NULL.
11432 **/
11433static struct lpfc_iocbq *
11434lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11435{
11436 struct lpfc_dmabuf *d_buf, *n_buf;
11437 struct lpfc_iocbq *first_iocbq, *iocbq;
11438 struct fc_frame_header *fc_hdr;
11439 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011440 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011441
11442 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11443 /* remove from receive buffer list */
11444 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011445 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011446 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011447 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011448 /* Get an iocbq struct to fill in. */
11449 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11450 if (first_iocbq) {
11451 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011452 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011453 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11454 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11455 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11456 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11457 vport->vpi + vport->phba->vpi_base;
11458 /* put the first buffer into the first IOCBq */
11459 first_iocbq->context2 = &seq_dmabuf->dbuf;
11460 first_iocbq->context3 = NULL;
11461 first_iocbq->iocb.ulpBdeCount = 1;
11462 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11463 LPFC_DATA_BUF_SIZE;
11464 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011465 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011466 bf_get(lpfc_rcqe_length,
11467 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011468 }
11469 iocbq = first_iocbq;
11470 /*
11471 * Each IOCBq can have two Buffers assigned, so go through the list
11472 * of buffers for this sequence and save two buffers in each IOCBq
11473 */
11474 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11475 if (!iocbq) {
11476 lpfc_in_buf_free(vport->phba, d_buf);
11477 continue;
11478 }
11479 if (!iocbq->context3) {
11480 iocbq->context3 = d_buf;
11481 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011482 pbde = (struct ulp_bde64 *)
11483 &iocbq->iocb.unsli3.sli3Words[4];
11484 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011485 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011486 bf_get(lpfc_rcqe_length,
11487 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011488 } else {
11489 iocbq = lpfc_sli_get_iocbq(vport->phba);
11490 if (!iocbq) {
11491 if (first_iocbq) {
11492 first_iocbq->iocb.ulpStatus =
11493 IOSTAT_FCP_RSP_ERROR;
11494 first_iocbq->iocb.un.ulpWord[4] =
11495 IOERR_NO_RESOURCES;
11496 }
11497 lpfc_in_buf_free(vport->phba, d_buf);
11498 continue;
11499 }
11500 iocbq->context2 = d_buf;
11501 iocbq->context3 = NULL;
11502 iocbq->iocb.ulpBdeCount = 1;
11503 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11504 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011505 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011506 bf_get(lpfc_rcqe_length,
11507 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011508 iocbq->iocb.un.rcvels.remoteID = sid;
11509 list_add_tail(&iocbq->list, &first_iocbq->list);
11510 }
11511 }
11512 return first_iocbq;
11513}
11514
James Smart6669f9b2009-10-02 15:16:45 -040011515static void
11516lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11517 struct hbq_dmabuf *seq_dmabuf)
11518{
11519 struct fc_frame_header *fc_hdr;
11520 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11521 struct lpfc_hba *phba = vport->phba;
11522
11523 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11524 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11525 if (!iocbq) {
11526 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11527 "2707 Ring %d handler: Failed to allocate "
11528 "iocb Rctl x%x Type x%x received\n",
11529 LPFC_ELS_RING,
11530 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11531 return;
11532 }
11533 if (!lpfc_complete_unsol_iocb(phba,
11534 &phba->sli.ring[LPFC_ELS_RING],
11535 iocbq, fc_hdr->fh_r_ctl,
11536 fc_hdr->fh_type))
11537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11538 "2540 Ring %d handler: unexpected Rctl "
11539 "x%x Type x%x received\n",
11540 LPFC_ELS_RING,
11541 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11542
11543 /* Free iocb created in lpfc_prep_seq */
11544 list_for_each_entry_safe(curr_iocb, next_iocb,
11545 &iocbq->list, list) {
11546 list_del_init(&curr_iocb->list);
11547 lpfc_sli_release_iocbq(phba, curr_iocb);
11548 }
11549 lpfc_sli_release_iocbq(phba, iocbq);
11550}
11551
James Smart4f774512009-05-22 14:52:35 -040011552/**
11553 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11554 * @phba: Pointer to HBA context object.
11555 *
11556 * This function is called with no lock held. This function processes all
11557 * the received buffers and gives it to upper layers when a received buffer
11558 * indicates that it is the final frame in the sequence. The interrupt
11559 * service routine processes received buffers at interrupt contexts and adds
11560 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11561 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11562 * appropriate receive function when the final frame in a sequence is received.
11563 **/
James Smart4d9ab992009-10-02 15:16:39 -040011564void
11565lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11566 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011567{
James Smart4d9ab992009-10-02 15:16:39 -040011568 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011569 struct fc_frame_header *fc_hdr;
11570 struct lpfc_vport *vport;
11571 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011572
James Smart4f774512009-05-22 14:52:35 -040011573 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011574 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11575 /* check to see if this a valid type of frame */
11576 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11577 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11578 return;
11579 }
11580 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11581 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011582 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011583 /* throw out the frame */
11584 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11585 return;
11586 }
James Smart6669f9b2009-10-02 15:16:45 -040011587 /* Handle the basic abort sequence (BA_ABTS) event */
11588 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11589 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11590 return;
11591 }
11592
James Smart4d9ab992009-10-02 15:16:39 -040011593 /* Link this frame */
11594 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11595 if (!seq_dmabuf) {
11596 /* unable to add frame to vport - throw it out */
11597 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11598 return;
11599 }
11600 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011601 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011602 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011603
James Smart6669f9b2009-10-02 15:16:45 -040011604 /* Send the complete sequence to the upper layer protocol */
11605 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011606}
James Smart6fb120a2009-05-22 14:52:59 -040011607
11608/**
11609 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11610 * @phba: pointer to lpfc hba data structure.
11611 *
11612 * This routine is invoked to post rpi header templates to the
11613 * HBA consistent with the SLI-4 interface spec. This routine
11614 * posts a PAGE_SIZE memory region to the port to hold up to
11615 * PAGE_SIZE modulo 64 rpi context headers.
11616 *
11617 * This routine does not require any locks. It's usage is expected
11618 * to be driver load or reset recovery when the driver is
11619 * sequential.
11620 *
11621 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011622 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011623 * EIO - The mailbox failed to complete successfully.
11624 * When this error occurs, the driver is not guaranteed
11625 * to have any rpi regions posted to the device and
11626 * must either attempt to repost the regions or take a
11627 * fatal error.
11628 **/
11629int
11630lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11631{
11632 struct lpfc_rpi_hdr *rpi_page;
11633 uint32_t rc = 0;
11634
11635 /* Post all rpi memory regions to the port. */
11636 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11637 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11638 if (rc != MBX_SUCCESS) {
11639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11640 "2008 Error %d posting all rpi "
11641 "headers\n", rc);
11642 rc = -EIO;
11643 break;
11644 }
11645 }
11646
11647 return rc;
11648}
11649
11650/**
11651 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11652 * @phba: pointer to lpfc hba data structure.
11653 * @rpi_page: pointer to the rpi memory region.
11654 *
11655 * This routine is invoked to post a single rpi header to the
11656 * HBA consistent with the SLI-4 interface spec. This memory region
11657 * maps up to 64 rpi context regions.
11658 *
11659 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011660 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011661 * ENOMEM - No available memory
11662 * EIO - The mailbox failed to complete successfully.
11663 **/
11664int
11665lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11666{
11667 LPFC_MBOXQ_t *mboxq;
11668 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11669 uint32_t rc = 0;
11670 uint32_t mbox_tmo;
11671 uint32_t shdr_status, shdr_add_status;
11672 union lpfc_sli4_cfg_shdr *shdr;
11673
11674 /* The port is notified of the header region via a mailbox command. */
11675 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11676 if (!mboxq) {
11677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11678 "2001 Unable to allocate memory for issuing "
11679 "SLI_CONFIG_SPECIAL mailbox command\n");
11680 return -ENOMEM;
11681 }
11682
11683 /* Post all rpi memory regions to the port. */
11684 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11685 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11686 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11687 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11688 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11689 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11690 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11691 hdr_tmpl, rpi_page->page_count);
11692 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11693 rpi_page->start_rpi);
11694 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11695 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011696 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011697 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11698 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11699 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11700 if (rc != MBX_TIMEOUT)
11701 mempool_free(mboxq, phba->mbox_mem_pool);
11702 if (shdr_status || shdr_add_status || rc) {
11703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11704 "2514 POST_RPI_HDR mailbox failed with "
11705 "status x%x add_status x%x, mbx status x%x\n",
11706 shdr_status, shdr_add_status, rc);
11707 rc = -ENXIO;
11708 }
11709 return rc;
11710}
11711
11712/**
11713 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11714 * @phba: pointer to lpfc hba data structure.
11715 *
11716 * This routine is invoked to post rpi header templates to the
11717 * HBA consistent with the SLI-4 interface spec. This routine
11718 * posts a PAGE_SIZE memory region to the port to hold up to
11719 * PAGE_SIZE modulo 64 rpi context headers.
11720 *
11721 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011722 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011723 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11724 **/
11725int
11726lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11727{
11728 int rpi;
11729 uint16_t max_rpi, rpi_base, rpi_limit;
11730 uint16_t rpi_remaining;
11731 struct lpfc_rpi_hdr *rpi_hdr;
11732
11733 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11734 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11735 rpi_limit = phba->sli4_hba.next_rpi;
11736
11737 /*
11738 * The valid rpi range is not guaranteed to be zero-based. Start
11739 * the search at the rpi_base as reported by the port.
11740 */
11741 spin_lock_irq(&phba->hbalock);
11742 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11743 if (rpi >= rpi_limit || rpi < rpi_base)
11744 rpi = LPFC_RPI_ALLOC_ERROR;
11745 else {
11746 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11747 phba->sli4_hba.max_cfg_param.rpi_used++;
11748 phba->sli4_hba.rpi_count++;
11749 }
11750
11751 /*
11752 * Don't try to allocate more rpi header regions if the device limit
11753 * on available rpis max has been exhausted.
11754 */
11755 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11756 (phba->sli4_hba.rpi_count >= max_rpi)) {
11757 spin_unlock_irq(&phba->hbalock);
11758 return rpi;
11759 }
11760
11761 /*
11762 * If the driver is running low on rpi resources, allocate another
11763 * page now. Note that the next_rpi value is used because
11764 * it represents how many are actually in use whereas max_rpi notes
11765 * how many are supported max by the device.
11766 */
11767 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11768 phba->sli4_hba.rpi_count;
11769 spin_unlock_irq(&phba->hbalock);
11770 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11771 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11772 if (!rpi_hdr) {
11773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11774 "2002 Error Could not grow rpi "
11775 "count\n");
11776 } else {
11777 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11778 }
11779 }
11780
11781 return rpi;
11782}
11783
11784/**
11785 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11786 * @phba: pointer to lpfc hba data structure.
11787 *
11788 * This routine is invoked to release an rpi to the pool of
11789 * available rpis maintained by the driver.
11790 **/
11791void
11792lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11793{
11794 spin_lock_irq(&phba->hbalock);
11795 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11796 phba->sli4_hba.rpi_count--;
11797 phba->sli4_hba.max_cfg_param.rpi_used--;
11798 spin_unlock_irq(&phba->hbalock);
11799}
11800
11801/**
11802 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11803 * @phba: pointer to lpfc hba data structure.
11804 *
11805 * This routine is invoked to remove the memory region that
11806 * provided rpi via a bitmask.
11807 **/
11808void
11809lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11810{
11811 kfree(phba->sli4_hba.rpi_bmask);
11812}
11813
11814/**
11815 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11816 * @phba: pointer to lpfc hba data structure.
11817 *
11818 * This routine is invoked to remove the memory region that
11819 * provided rpi via a bitmask.
11820 **/
11821int
11822lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11823{
11824 LPFC_MBOXQ_t *mboxq;
11825 struct lpfc_hba *phba = ndlp->phba;
11826 int rc;
11827
11828 /* The port is notified of the header region via a mailbox command. */
11829 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11830 if (!mboxq)
11831 return -ENOMEM;
11832
11833 /* Post all rpi memory regions to the port. */
11834 lpfc_resume_rpi(mboxq, ndlp);
11835 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11836 if (rc == MBX_NOT_FINISHED) {
11837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11838 "2010 Resume RPI Mailbox failed "
11839 "status %d, mbxStatus x%x\n", rc,
11840 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11841 mempool_free(mboxq, phba->mbox_mem_pool);
11842 return -EIO;
11843 }
11844 return 0;
11845}
11846
11847/**
11848 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11849 * @phba: pointer to lpfc hba data structure.
11850 * @vpi: vpi value to activate with the port.
11851 *
11852 * This routine is invoked to activate a vpi with the
11853 * port when the host intends to use vports with a
11854 * nonzero vpi.
11855 *
11856 * Returns:
11857 * 0 success
11858 * -Evalue otherwise
11859 **/
11860int
11861lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11862{
11863 LPFC_MBOXQ_t *mboxq;
11864 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011865 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011866 uint32_t mbox_tmo;
11867
11868 if (vpi == 0)
11869 return -EINVAL;
11870 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11871 if (!mboxq)
11872 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011873 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011874 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11875 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011876 if (rc != MBX_SUCCESS) {
11877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11878 "2022 INIT VPI Mailbox failed "
11879 "status %d, mbxStatus x%x\n", rc,
11880 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011881 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011882 }
James Smart6a9c52c2009-10-02 15:16:51 -040011883 if (rc != MBX_TIMEOUT)
11884 mempool_free(mboxq, phba->mbox_mem_pool);
11885
11886 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011887}
11888
11889/**
11890 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11891 * @phba: pointer to lpfc hba data structure.
11892 * @mboxq: Pointer to mailbox object.
11893 *
11894 * This routine is invoked to manually add a single FCF record. The caller
11895 * must pass a completely initialized FCF_Record. This routine takes
11896 * care of the nonembedded mailbox operations.
11897 **/
11898static void
11899lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11900{
11901 void *virt_addr;
11902 union lpfc_sli4_cfg_shdr *shdr;
11903 uint32_t shdr_status, shdr_add_status;
11904
11905 virt_addr = mboxq->sge_array->addr[0];
11906 /* The IOCTL status is embedded in the mailbox subheader. */
11907 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11908 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11909 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11910
11911 if ((shdr_status || shdr_add_status) &&
11912 (shdr_status != STATUS_FCF_IN_USE))
11913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 "2558 ADD_FCF_RECORD mailbox failed with "
11915 "status x%x add_status x%x\n",
11916 shdr_status, shdr_add_status);
11917
11918 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11919}
11920
11921/**
11922 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11923 * @phba: pointer to lpfc hba data structure.
11924 * @fcf_record: pointer to the initialized fcf record to add.
11925 *
11926 * This routine is invoked to manually add a single FCF record. The caller
11927 * must pass a completely initialized FCF_Record. This routine takes
11928 * care of the nonembedded mailbox operations.
11929 **/
11930int
11931lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11932{
11933 int rc = 0;
11934 LPFC_MBOXQ_t *mboxq;
11935 uint8_t *bytep;
11936 void *virt_addr;
11937 dma_addr_t phys_addr;
11938 struct lpfc_mbx_sge sge;
11939 uint32_t alloc_len, req_len;
11940 uint32_t fcfindex;
11941
11942 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11943 if (!mboxq) {
11944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11945 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11946 return -ENOMEM;
11947 }
11948
11949 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11950 sizeof(uint32_t);
11951
11952 /* Allocate DMA memory and set up the non-embedded mailbox command */
11953 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11954 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11955 req_len, LPFC_SLI4_MBX_NEMBED);
11956 if (alloc_len < req_len) {
11957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11958 "2523 Allocated DMA memory size (x%x) is "
11959 "less than the requested DMA memory "
11960 "size (x%x)\n", alloc_len, req_len);
11961 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11962 return -ENOMEM;
11963 }
11964
11965 /*
11966 * Get the first SGE entry from the non-embedded DMA memory. This
11967 * routine only uses a single SGE.
11968 */
11969 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11970 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011971 virt_addr = mboxq->sge_array->addr[0];
11972 /*
11973 * Configure the FCF record for FCFI 0. This is the driver's
11974 * hardcoded default and gets used in nonFIP mode.
11975 */
11976 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11977 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11978 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11979
11980 /*
11981 * Copy the fcf_index and the FCF Record Data. The data starts after
11982 * the FCoE header plus word10. The data copy needs to be endian
11983 * correct.
11984 */
11985 bytep += sizeof(uint32_t);
11986 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11987 mboxq->vport = phba->pport;
11988 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11989 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11990 if (rc == MBX_NOT_FINISHED) {
11991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11992 "2515 ADD_FCF_RECORD mailbox failed with "
11993 "status 0x%x\n", rc);
11994 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11995 rc = -EIO;
11996 } else
11997 rc = 0;
11998
11999 return rc;
12000}
12001
12002/**
12003 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12004 * @phba: pointer to lpfc hba data structure.
12005 * @fcf_record: pointer to the fcf record to write the default data.
12006 * @fcf_index: FCF table entry index.
12007 *
12008 * This routine is invoked to build the driver's default FCF record. The
12009 * values used are hardcoded. This routine handles memory initialization.
12010 *
12011 **/
12012void
12013lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12014 struct fcf_record *fcf_record,
12015 uint16_t fcf_index)
12016{
12017 memset(fcf_record, 0, sizeof(struct fcf_record));
12018 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12019 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12020 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12021 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12022 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12023 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12024 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12025 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12026 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12027 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12028 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12029 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12030 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012031 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012032 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12033 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12034 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12035 /* Set the VLAN bit map */
12036 if (phba->valid_vlan) {
12037 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12038 = 1 << (phba->vlan_id % 8);
12039 }
12040}
12041
12042/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012043 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012044 * @phba: pointer to lpfc hba data structure.
12045 * @fcf_index: FCF table entry offset.
12046 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012047 * This routine is invoked to scan the entire FCF table by reading FCF
12048 * record and processing it one at a time starting from the @fcf_index
12049 * for initial FCF discovery or fast FCF failover rediscovery.
12050 *
12051 * Return 0 if the mailbox command is submitted sucessfully, none 0
12052 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012053 **/
12054int
James Smart0c9ab6f2010-02-26 14:15:57 -050012055lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012056{
12057 int rc = 0, error;
12058 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012059
James Smart32b97932009-07-19 10:01:21 -040012060 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012061 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12062 if (!mboxq) {
12063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12064 "2000 Failed to allocate mbox for "
12065 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012066 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012067 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012068 }
James Smartecfd03c2010-02-12 14:41:27 -050012069 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012070 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012071 if (rc) {
12072 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012073 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012074 }
James Smartecfd03c2010-02-12 14:41:27 -050012075 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012076 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012077 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smart6fb120a2009-05-22 14:52:59 -040012078 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012079 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012080 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012081 else {
James Smart32b97932009-07-19 10:01:21 -040012082 spin_lock_irq(&phba->hbalock);
12083 phba->hba_flag |= FCF_DISC_INPROGRESS;
12084 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012085 /* Reset FCF round robin index bmask for new scan */
James Smart999d8132010-03-15 11:24:56 -040012086 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012087 memset(phba->fcf.fcf_rr_bmask, 0,
12088 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart999d8132010-03-15 11:24:56 -040012089 phba->fcf.eligible_fcf_cnt = 0;
12090 }
James Smart6fb120a2009-05-22 14:52:59 -040012091 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012092 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012093fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012094 if (error) {
12095 if (mboxq)
12096 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12097 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12098 spin_lock_irq(&phba->hbalock);
12099 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12100 spin_unlock_irq(&phba->hbalock);
12101 }
James Smart6fb120a2009-05-22 14:52:59 -040012102 return error;
12103}
James Smarta0c87cb2009-07-19 10:01:10 -040012104
12105/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012106 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12107 * @phba: pointer to lpfc hba data structure.
12108 * @fcf_index: FCF table entry offset.
12109 *
12110 * This routine is invoked to read an FCF record indicated by @fcf_index
12111 * and to use it for FLOGI round robin FCF failover.
12112 *
12113 * Return 0 if the mailbox command is submitted sucessfully, none 0
12114 * otherwise.
12115 **/
12116int
12117lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12118{
12119 int rc = 0, error;
12120 LPFC_MBOXQ_t *mboxq;
12121
12122 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12123 if (!mboxq) {
12124 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12125 "2763 Failed to allocate mbox for "
12126 "READ_FCF cmd\n");
12127 error = -ENOMEM;
12128 goto fail_fcf_read;
12129 }
12130 /* Construct the read FCF record mailbox command */
12131 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12132 if (rc) {
12133 error = -EINVAL;
12134 goto fail_fcf_read;
12135 }
12136 /* Issue the mailbox command asynchronously */
12137 mboxq->vport = phba->pport;
12138 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12139 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12140 if (rc == MBX_NOT_FINISHED)
12141 error = -EIO;
12142 else
12143 error = 0;
12144
12145fail_fcf_read:
12146 if (error && mboxq)
12147 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12148 return error;
12149}
12150
12151/**
12152 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12153 * @phba: pointer to lpfc hba data structure.
12154 * @fcf_index: FCF table entry offset.
12155 *
12156 * This routine is invoked to read an FCF record indicated by @fcf_index to
12157 * determine whether it's eligible for FLOGI round robin failover list.
12158 *
12159 * Return 0 if the mailbox command is submitted sucessfully, none 0
12160 * otherwise.
12161 **/
12162int
12163lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12164{
12165 int rc = 0, error;
12166 LPFC_MBOXQ_t *mboxq;
12167
12168 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12169 if (!mboxq) {
12170 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12171 "2758 Failed to allocate mbox for "
12172 "READ_FCF cmd\n");
12173 error = -ENOMEM;
12174 goto fail_fcf_read;
12175 }
12176 /* Construct the read FCF record mailbox command */
12177 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12178 if (rc) {
12179 error = -EINVAL;
12180 goto fail_fcf_read;
12181 }
12182 /* Issue the mailbox command asynchronously */
12183 mboxq->vport = phba->pport;
12184 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12186 if (rc == MBX_NOT_FINISHED)
12187 error = -EIO;
12188 else
12189 error = 0;
12190
12191fail_fcf_read:
12192 if (error && mboxq)
12193 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12194 return error;
12195}
12196
12197/**
12198 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12199 * @phba: pointer to lpfc hba data structure.
12200 *
12201 * This routine is to get the next eligible FCF record index in a round
12202 * robin fashion. If the next eligible FCF record index equals to the
12203 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12204 * shall be returned, otherwise, the next eligible FCF record's index
12205 * shall be returned.
12206 **/
12207uint16_t
12208lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12209{
12210 uint16_t next_fcf_index;
12211
12212 /* Search from the currently registered FCF index */
12213 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12214 LPFC_SLI4_FCF_TBL_INDX_MAX,
12215 phba->fcf.current_rec.fcf_indx);
12216 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12217 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12218 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12219 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12220 /* Round robin failover stop condition */
12221 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12222 return LPFC_FCOE_FCF_NEXT_NONE;
12223
12224 return next_fcf_index;
12225}
12226
12227/**
12228 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12229 * @phba: pointer to lpfc hba data structure.
12230 *
12231 * This routine sets the FCF record index in to the eligible bmask for
12232 * round robin failover search. It checks to make sure that the index
12233 * does not go beyond the range of the driver allocated bmask dimension
12234 * before setting the bit.
12235 *
12236 * Returns 0 if the index bit successfully set, otherwise, it returns
12237 * -EINVAL.
12238 **/
12239int
12240lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12241{
12242 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12243 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12244 "2610 HBA FCF index reached driver's "
12245 "book keeping dimension: fcf_index:%d, "
12246 "driver_bmask_max:%d\n",
12247 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12248 return -EINVAL;
12249 }
12250 /* Set the eligible FCF record index bmask */
12251 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12252
12253 return 0;
12254}
12255
12256/**
12257 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12258 * @phba: pointer to lpfc hba data structure.
12259 *
12260 * This routine clears the FCF record index from the eligible bmask for
12261 * round robin failover search. It checks to make sure that the index
12262 * does not go beyond the range of the driver allocated bmask dimension
12263 * before clearing the bit.
12264 **/
12265void
12266lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12267{
12268 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12269 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12270 "2762 HBA FCF index goes beyond driver's "
12271 "book keeping dimension: fcf_index:%d, "
12272 "driver_bmask_max:%d\n",
12273 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12274 return;
12275 }
12276 /* Clear the eligible FCF record index bmask */
12277 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12278}
12279
12280/**
James Smartecfd03c2010-02-12 14:41:27 -050012281 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12282 * @phba: pointer to lpfc hba data structure.
12283 *
12284 * This routine is the completion routine for the rediscover FCF table mailbox
12285 * command. If the mailbox command returned failure, it will try to stop the
12286 * FCF rediscover wait timer.
12287 **/
12288void
12289lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12290{
12291 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12292 uint32_t shdr_status, shdr_add_status;
12293
12294 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12295
12296 shdr_status = bf_get(lpfc_mbox_hdr_status,
12297 &redisc_fcf->header.cfg_shdr.response);
12298 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12299 &redisc_fcf->header.cfg_shdr.response);
12300 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012301 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012302 "2746 Requesting for FCF rediscovery failed "
12303 "status x%x add_status x%x\n",
12304 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012305 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012306 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012307 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012308 spin_unlock_irq(&phba->hbalock);
12309 /*
12310 * CVL event triggered FCF rediscover request failed,
12311 * last resort to re-try current registered FCF entry.
12312 */
12313 lpfc_retry_pport_discovery(phba);
12314 } else {
12315 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012316 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012317 spin_unlock_irq(&phba->hbalock);
12318 /*
12319 * DEAD FCF event triggered FCF rediscover request
12320 * failed, last resort to fail over as a link down
12321 * to FCF registration.
12322 */
12323 lpfc_sli4_fcf_dead_failthrough(phba);
12324 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012325 } else {
12326 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12327 "2775 Start FCF rediscovery quiescent period "
12328 "wait timer before scaning FCF table\n");
James Smartecfd03c2010-02-12 14:41:27 -050012329 /*
12330 * Start FCF rediscovery wait timer for pending FCF
12331 * before rescan FCF record table.
12332 */
12333 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012334 }
James Smartecfd03c2010-02-12 14:41:27 -050012335
12336 mempool_free(mbox, phba->mbox_mem_pool);
12337}
12338
12339/**
12340 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12341 * @phba: pointer to lpfc hba data structure.
12342 *
12343 * This routine is invoked to request for rediscovery of the entire FCF table
12344 * by the port.
12345 **/
12346int
12347lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12348{
12349 LPFC_MBOXQ_t *mbox;
12350 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12351 int rc, length;
12352
James Smart0c9ab6f2010-02-26 14:15:57 -050012353 /* Cancel retry delay timers to all vports before FCF rediscover */
12354 lpfc_cancel_all_vport_retry_delay_timer(phba);
12355
James Smartecfd03c2010-02-12 14:41:27 -050012356 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12357 if (!mbox) {
12358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12359 "2745 Failed to allocate mbox for "
12360 "requesting FCF rediscover.\n");
12361 return -ENOMEM;
12362 }
12363
12364 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12365 sizeof(struct lpfc_sli4_cfg_mhdr));
12366 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12367 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12368 length, LPFC_SLI4_MBX_EMBED);
12369
12370 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12371 /* Set count to 0 for invalidating the entire FCF database */
12372 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12373
12374 /* Issue the mailbox command asynchronously */
12375 mbox->vport = phba->pport;
12376 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12377 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12378
12379 if (rc == MBX_NOT_FINISHED) {
12380 mempool_free(mbox, phba->mbox_mem_pool);
12381 return -EIO;
12382 }
12383 return 0;
12384}
12385
12386/**
James Smartfc2b9892010-02-26 14:15:29 -050012387 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12388 * @phba: pointer to lpfc hba data structure.
12389 *
12390 * This function is the failover routine as a last resort to the FCF DEAD
12391 * event when driver failed to perform fast FCF failover.
12392 **/
12393void
12394lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12395{
12396 uint32_t link_state;
12397
12398 /*
12399 * Last resort as FCF DEAD event failover will treat this as
12400 * a link down, but save the link state because we don't want
12401 * it to be changed to Link Down unless it is already down.
12402 */
12403 link_state = phba->link_state;
12404 lpfc_linkdown(phba);
12405 phba->link_state = link_state;
12406
12407 /* Unregister FCF if no devices connected to it */
12408 lpfc_unregister_unused_fcf(phba);
12409}
12410
12411/**
James Smarta0c87cb2009-07-19 10:01:10 -040012412 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12413 * @phba: pointer to lpfc hba data structure.
12414 *
12415 * This function read region 23 and parse TLV for port status to
12416 * decide if the user disaled the port. If the TLV indicates the
12417 * port is disabled, the hba_flag is set accordingly.
12418 **/
12419void
12420lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12421{
12422 LPFC_MBOXQ_t *pmb = NULL;
12423 MAILBOX_t *mb;
12424 uint8_t *rgn23_data = NULL;
12425 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12426 int rc;
12427
12428 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12429 if (!pmb) {
12430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12431 "2600 lpfc_sli_read_serdes_param failed to"
12432 " allocate mailbox memory\n");
12433 goto out;
12434 }
12435 mb = &pmb->u.mb;
12436
12437 /* Get adapter Region 23 data */
12438 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12439 if (!rgn23_data)
12440 goto out;
12441
12442 do {
12443 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12444 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12445
12446 if (rc != MBX_SUCCESS) {
12447 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12448 "2601 lpfc_sli_read_link_ste failed to"
12449 " read config region 23 rc 0x%x Status 0x%x\n",
12450 rc, mb->mbxStatus);
12451 mb->un.varDmp.word_cnt = 0;
12452 }
12453 /*
12454 * dump mem may return a zero when finished or we got a
12455 * mailbox error, either way we are done.
12456 */
12457 if (mb->un.varDmp.word_cnt == 0)
12458 break;
12459 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12460 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12461
12462 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12463 rgn23_data + offset,
12464 mb->un.varDmp.word_cnt);
12465 offset += mb->un.varDmp.word_cnt;
12466 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12467
12468 data_size = offset;
12469 offset = 0;
12470
12471 if (!data_size)
12472 goto out;
12473
12474 /* Check the region signature first */
12475 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12477 "2619 Config region 23 has bad signature\n");
12478 goto out;
12479 }
12480 offset += 4;
12481
12482 /* Check the data structure version */
12483 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12485 "2620 Config region 23 has bad version\n");
12486 goto out;
12487 }
12488 offset += 4;
12489
12490 /* Parse TLV entries in the region */
12491 while (offset < data_size) {
12492 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12493 break;
12494 /*
12495 * If the TLV is not driver specific TLV or driver id is
12496 * not linux driver id, skip the record.
12497 */
12498 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12499 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12500 (rgn23_data[offset + 3] != 0)) {
12501 offset += rgn23_data[offset + 1] * 4 + 4;
12502 continue;
12503 }
12504
12505 /* Driver found a driver specific TLV in the config region */
12506 sub_tlv_len = rgn23_data[offset + 1] * 4;
12507 offset += 4;
12508 tlv_offset = 0;
12509
12510 /*
12511 * Search for configured port state sub-TLV.
12512 */
12513 while ((offset < data_size) &&
12514 (tlv_offset < sub_tlv_len)) {
12515 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12516 offset += 4;
12517 tlv_offset += 4;
12518 break;
12519 }
12520 if (rgn23_data[offset] != PORT_STE_TYPE) {
12521 offset += rgn23_data[offset + 1] * 4 + 4;
12522 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12523 continue;
12524 }
12525
12526 /* This HBA contains PORT_STE configured */
12527 if (!rgn23_data[offset + 2])
12528 phba->hba_flag |= LINK_DISABLED;
12529
12530 goto out;
12531 }
12532 }
12533out:
12534 if (pmb)
12535 mempool_free(pmb, phba->mbox_mem_pool);
12536 kfree(rgn23_data);
12537 return;
12538}
James Smart695a8142010-01-26 23:08:03 -050012539
12540/**
12541 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12542 * @vport: pointer to vport data structure.
12543 *
12544 * This function iterate through the mailboxq and clean up all REG_LOGIN
12545 * and REG_VPI mailbox commands associated with the vport. This function
12546 * is called when driver want to restart discovery of the vport due to
12547 * a Clear Virtual Link event.
12548 **/
12549void
12550lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12551{
12552 struct lpfc_hba *phba = vport->phba;
12553 LPFC_MBOXQ_t *mb, *nextmb;
12554 struct lpfc_dmabuf *mp;
12555
12556 spin_lock_irq(&phba->hbalock);
12557 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12558 if (mb->vport != vport)
12559 continue;
12560
12561 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12562 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12563 continue;
12564
12565 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12566 mp = (struct lpfc_dmabuf *) (mb->context1);
12567 if (mp) {
12568 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12569 kfree(mp);
12570 }
12571 }
12572 list_del(&mb->list);
12573 mempool_free(mb, phba->mbox_mem_pool);
12574 }
12575 mb = phba->sli.mbox_active;
12576 if (mb && (mb->vport == vport)) {
12577 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12578 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12579 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12580 }
12581 spin_unlock_irq(&phba->hbalock);
12582}
12583