blob: 73259bca1d143aa8bc201047979f3c2d5234b917 [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. */
James Smart49198b32010-04-06 15:04:33 -04004299 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004300 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--;
7139
James Smart0ff10d42008-01-11 01:52:36 -05007140 /* Firmware could still be in progress of DMAing
7141 * payload, so don't free data buffer till after
7142 * a hbeat.
7143 */
7144 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007145 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007146 spin_unlock_irq(&phba->hbalock);
7147
James Smart92d7f7b2007-06-17 19:56:38 -05007148 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007149 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007150 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007151 } else
7152 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007153 }
7154
James Bottomley604a3e32005-10-29 10:28:33 -05007155 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007156 return;
7157}
7158
James Smarte59058c2008-08-24 21:49:00 -04007159/**
James Smart3621a712009-04-06 18:47:14 -04007160 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007161 * @phba: Pointer to HBA context object.
7162 * @cmdiocb: Pointer to driver command iocb object.
7163 * @rspiocb: Pointer to driver response iocb object.
7164 *
7165 * The function is called from SLI ring event handler with no
7166 * lock held. This function is the completion handler for ELS commands
7167 * which are aborted. The function frees memory resources used for
7168 * the aborted ELS commands.
7169 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007170static void
7171lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7172 struct lpfc_iocbq *rspiocb)
7173{
7174 IOCB_t *irsp = &rspiocb->iocb;
7175
7176 /* ELS cmd tag <ulpIoTag> completes */
7177 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007178 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007179 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007180 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007181 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007182 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7183 lpfc_ct_free_iocb(phba, cmdiocb);
7184 else
7185 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007186 return;
7187}
7188
James Smarte59058c2008-08-24 21:49:00 -04007189/**
James Smart3621a712009-04-06 18:47:14 -04007190 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007191 * @phba: Pointer to HBA context object.
7192 * @pring: Pointer to driver SLI ring object.
7193 * @cmdiocb: Pointer to driver command iocb object.
7194 *
7195 * This function issues an abort iocb for the provided command
7196 * iocb. This function is called with hbalock held.
7197 * The function returns 0 when it fails due to memory allocation
7198 * failure or when the command iocb is an abort request.
7199 **/
dea31012005-04-17 16:05:31 -05007200int
James Smart2e0fef82007-06-17 19:56:36 -05007201lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7202 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007203{
James Smart2e0fef82007-06-17 19:56:36 -05007204 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007205 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007206 IOCB_t *icmd = NULL;
7207 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007208 int retval = IOCB_ERROR;
7209
James Smart92d7f7b2007-06-17 19:56:38 -05007210 /*
7211 * There are certain command types we don't want to abort. And we
7212 * don't want to abort commands that are already in the process of
7213 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007214 */
7215 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007216 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007217 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7218 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007219 return 0;
7220
James Smart858c9f62007-06-17 19:56:39 -05007221 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7222 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007223 */
James Smart858c9f62007-06-17 19:56:39 -05007224 if ((vport->load_flag & FC_UNLOADING) &&
7225 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007226 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7227 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7228 else
7229 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007230 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007231 }
dea31012005-04-17 16:05:31 -05007232
7233 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007234 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007235 if (abtsiocbp == NULL)
7236 return 0;
dea31012005-04-17 16:05:31 -05007237
James Smart07951072007-04-25 09:51:38 -04007238 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007239 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007240 */
7241 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7242
dea31012005-04-17 16:05:31 -05007243 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007244 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7245 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007246 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007247 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007248 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7249 }
James Smartda0436e2009-05-22 14:51:39 -04007250 else
7251 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007252 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007253 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007254
James Smart5ffc2662009-11-18 15:39:44 -05007255 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7256 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007257 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7258 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007259
James Smart2e0fef82007-06-17 19:56:36 -05007260 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007261 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7262 else
7263 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7264
7265 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007266
James Smarte8b62012007-08-02 11:10:09 -04007267 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7268 "0339 Abort xri x%x, original iotag x%x, "
7269 "abort cmd iotag x%x\n",
7270 iabt->un.acxri.abortContextTag,
7271 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007272 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007273
James Smartd7c255b2008-08-24 21:50:00 -04007274 if (retval)
7275 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007276abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007277 /*
7278 * Caller to this routine should check for IOCB_ERROR
7279 * and handle it properly. This routine no longer removes
7280 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007281 */
James Smart2e0fef82007-06-17 19:56:36 -05007282 return retval;
dea31012005-04-17 16:05:31 -05007283}
7284
James Smarte59058c2008-08-24 21:49:00 -04007285/**
James Smart3621a712009-04-06 18:47:14 -04007286 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007287 * @iocbq: Pointer to driver iocb object.
7288 * @vport: Pointer to driver virtual port object.
7289 * @tgt_id: SCSI ID of the target.
7290 * @lun_id: LUN ID of the scsi device.
7291 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7292 *
James Smart3621a712009-04-06 18:47:14 -04007293 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007294 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7295 * 0 if the filtering criteria is met for the given iocb and will return
7296 * 1 if the filtering criteria is not met.
7297 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7298 * given iocb is for the SCSI device specified by vport, tgt_id and
7299 * lun_id parameter.
7300 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7301 * given iocb is for the SCSI target specified by vport and tgt_id
7302 * parameters.
7303 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7304 * given iocb is for the SCSI host associated with the given vport.
7305 * This function is called with no locks held.
7306 **/
dea31012005-04-17 16:05:31 -05007307static int
James Smart51ef4c22007-08-02 11:10:31 -04007308lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7309 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007310 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007311{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007312 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007313 int rc = 1;
7314
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007315 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7316 return rc;
7317
James Smart51ef4c22007-08-02 11:10:31 -04007318 if (iocbq->vport != vport)
7319 return rc;
7320
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007321 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007322
James Smart495a7142008-06-14 22:52:59 -04007323 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007324 return rc;
7325
7326 switch (ctx_cmd) {
7327 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007328 if ((lpfc_cmd->rdata->pnode) &&
7329 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7330 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007331 rc = 0;
7332 break;
7333 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007334 if ((lpfc_cmd->rdata->pnode) &&
7335 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007336 rc = 0;
7337 break;
dea31012005-04-17 16:05:31 -05007338 case LPFC_CTX_HOST:
7339 rc = 0;
7340 break;
7341 default:
7342 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007343 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007344 break;
7345 }
7346
7347 return rc;
7348}
7349
James Smarte59058c2008-08-24 21:49:00 -04007350/**
James Smart3621a712009-04-06 18:47:14 -04007351 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007352 * @vport: Pointer to virtual port.
7353 * @tgt_id: SCSI ID of the target.
7354 * @lun_id: LUN ID of the scsi device.
7355 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7356 *
7357 * This function returns number of FCP commands pending for the vport.
7358 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7359 * commands pending on the vport associated with SCSI device specified
7360 * by tgt_id and lun_id parameters.
7361 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7362 * commands pending on the vport associated with SCSI target specified
7363 * by tgt_id parameter.
7364 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7365 * commands pending on the vport.
7366 * This function returns the number of iocbs which satisfy the filter.
7367 * This function is called without any lock held.
7368 **/
dea31012005-04-17 16:05:31 -05007369int
James Smart51ef4c22007-08-02 11:10:31 -04007370lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7371 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007372{
James Smart51ef4c22007-08-02 11:10:31 -04007373 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007374 struct lpfc_iocbq *iocbq;
7375 int sum, i;
dea31012005-04-17 16:05:31 -05007376
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007377 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7378 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007379
James Smart51ef4c22007-08-02 11:10:31 -04007380 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7381 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007382 sum++;
dea31012005-04-17 16:05:31 -05007383 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007384
dea31012005-04-17 16:05:31 -05007385 return sum;
7386}
7387
James Smarte59058c2008-08-24 21:49:00 -04007388/**
James Smart3621a712009-04-06 18:47:14 -04007389 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007390 * @phba: Pointer to HBA context object
7391 * @cmdiocb: Pointer to command iocb object.
7392 * @rspiocb: Pointer to response iocb object.
7393 *
7394 * This function is called when an aborted FCP iocb completes. This
7395 * function is called by the ring event handler with no lock held.
7396 * This function frees the iocb.
7397 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007398void
James Smart2e0fef82007-06-17 19:56:36 -05007399lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7400 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007401{
James Bottomley604a3e32005-10-29 10:28:33 -05007402 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007403 return;
7404}
7405
James Smarte59058c2008-08-24 21:49:00 -04007406/**
James Smart3621a712009-04-06 18:47:14 -04007407 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007408 * @vport: Pointer to virtual port.
7409 * @pring: Pointer to driver SLI ring object.
7410 * @tgt_id: SCSI ID of the target.
7411 * @lun_id: LUN ID of the scsi device.
7412 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7413 *
7414 * This function sends an abort command for every SCSI command
7415 * associated with the given virtual port pending on the ring
7416 * filtered by lpfc_sli_validate_fcp_iocb function.
7417 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7418 * FCP iocbs associated with lun specified by tgt_id and lun_id
7419 * parameters
7420 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7421 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7422 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7423 * FCP iocbs associated with virtual port.
7424 * This function returns number of iocbs it failed to abort.
7425 * This function is called with no locks held.
7426 **/
dea31012005-04-17 16:05:31 -05007427int
James Smart51ef4c22007-08-02 11:10:31 -04007428lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7429 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007430{
James Smart51ef4c22007-08-02 11:10:31 -04007431 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007432 struct lpfc_iocbq *iocbq;
7433 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007434 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007435 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007436 int i;
dea31012005-04-17 16:05:31 -05007437
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007438 for (i = 1; i <= phba->sli.last_iotag; i++) {
7439 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007440
James Smart51ef4c22007-08-02 11:10:31 -04007441 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007442 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007443 continue;
7444
7445 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007446 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007447 if (abtsiocb == NULL) {
7448 errcnt++;
7449 continue;
7450 }
dea31012005-04-17 16:05:31 -05007451
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007452 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007453 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7454 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007455 if (phba->sli_rev == LPFC_SLI_REV4)
7456 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7457 else
7458 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007459 abtsiocb->iocb.ulpLe = 1;
7460 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007461 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007462
James Smart5ffc2662009-11-18 15:39:44 -05007463 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7464 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007465 if (iocbq->iocb_flag & LPFC_IO_FCP)
7466 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007467
James Smart2e0fef82007-06-17 19:56:36 -05007468 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007469 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7470 else
7471 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7472
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007473 /* Setup callback routine and issue the command. */
7474 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007475 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7476 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007477 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007478 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007479 errcnt++;
7480 continue;
7481 }
7482 }
7483
7484 return errcnt;
7485}
7486
James Smarte59058c2008-08-24 21:49:00 -04007487/**
James Smart3621a712009-04-06 18:47:14 -04007488 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007489 * @phba: Pointer to HBA context object.
7490 * @cmdiocbq: Pointer to command iocb.
7491 * @rspiocbq: Pointer to response iocb.
7492 *
7493 * This function is the completion handler for iocbs issued using
7494 * lpfc_sli_issue_iocb_wait function. This function is called by the
7495 * ring event handler function without any lock held. This function
7496 * can be called from both worker thread context and interrupt
7497 * context. This function also can be called from other thread which
7498 * cleans up the SLI layer objects.
7499 * This function copy the contents of the response iocb to the
7500 * response iocb memory object provided by the caller of
7501 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7502 * sleeps for the iocb completion.
7503 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007504static void
7505lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7506 struct lpfc_iocbq *cmdiocbq,
7507 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007508{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007509 wait_queue_head_t *pdone_q;
7510 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007511 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007512
James Smart2e0fef82007-06-17 19:56:36 -05007513 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007514 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7515 if (cmdiocbq->context2 && rspiocbq)
7516 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7517 &rspiocbq->iocb, sizeof(IOCB_t));
7518
James Smart0f65ff62010-02-26 14:14:23 -05007519 /* Set the exchange busy flag for task management commands */
7520 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7521 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7522 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7523 cur_iocbq);
7524 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7525 }
7526
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007527 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007528 if (pdone_q)
7529 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007530 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007531 return;
7532}
7533
James Smarte59058c2008-08-24 21:49:00 -04007534/**
James Smartd11e31d2009-06-10 17:23:06 -04007535 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7536 * @phba: Pointer to HBA context object..
7537 * @piocbq: Pointer to command iocb.
7538 * @flag: Flag to test.
7539 *
7540 * This routine grabs the hbalock and then test the iocb_flag to
7541 * see if the passed in flag is set.
7542 * Returns:
7543 * 1 if flag is set.
7544 * 0 if flag is not set.
7545 **/
7546static int
7547lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7548 struct lpfc_iocbq *piocbq, uint32_t flag)
7549{
7550 unsigned long iflags;
7551 int ret;
7552
7553 spin_lock_irqsave(&phba->hbalock, iflags);
7554 ret = piocbq->iocb_flag & flag;
7555 spin_unlock_irqrestore(&phba->hbalock, iflags);
7556 return ret;
7557
7558}
7559
7560/**
James Smart3621a712009-04-06 18:47:14 -04007561 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007562 * @phba: Pointer to HBA context object..
7563 * @pring: Pointer to sli ring.
7564 * @piocb: Pointer to command iocb.
7565 * @prspiocbq: Pointer to response iocb.
7566 * @timeout: Timeout in number of seconds.
7567 *
7568 * This function issues the iocb to firmware and waits for the
7569 * iocb to complete. If the iocb command is not
7570 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7571 * Caller should not free the iocb resources if this function
7572 * returns IOCB_TIMEDOUT.
7573 * The function waits for the iocb completion using an
7574 * non-interruptible wait.
7575 * This function will sleep while waiting for iocb completion.
7576 * So, this function should not be called from any context which
7577 * does not allow sleeping. Due to the same reason, this function
7578 * cannot be called with interrupt disabled.
7579 * This function assumes that the iocb completions occur while
7580 * this function sleep. So, this function cannot be called from
7581 * the thread which process iocb completion for this ring.
7582 * This function clears the iocb_flag of the iocb object before
7583 * issuing the iocb and the iocb completion handler sets this
7584 * flag and wakes this thread when the iocb completes.
7585 * The contents of the response iocb will be copied to prspiocbq
7586 * by the completion handler when the command completes.
7587 * This function returns IOCB_SUCCESS when success.
7588 * This function is called with no lock held.
7589 **/
dea31012005-04-17 16:05:31 -05007590int
James Smart2e0fef82007-06-17 19:56:36 -05007591lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007592 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007593 struct lpfc_iocbq *piocb,
7594 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007595 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007596{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007597 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007598 long timeleft, timeout_req = 0;
7599 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007600 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007601
7602 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007603 * If the caller has provided a response iocbq buffer, then context2
7604 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007605 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007606 if (prspiocbq) {
7607 if (piocb->context2)
7608 return IOCB_ERROR;
7609 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007610 }
7611
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007612 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7613 piocb->context_un.wait_queue = &done_q;
7614 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007615
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007616 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7617 creg_val = readl(phba->HCregaddr);
7618 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7619 writel(creg_val, phba->HCregaddr);
7620 readl(phba->HCregaddr); /* flush */
7621 }
7622
James Smartda0436e2009-05-22 14:51:39 -04007623 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007624 if (retval == IOCB_SUCCESS) {
7625 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007626 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007627 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007628 timeout_req);
dea31012005-04-17 16:05:31 -05007629
James Smart7054a602007-04-25 09:52:34 -04007630 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7631 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007632 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007633 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007635 "0338 IOCB wait timeout error - no "
7636 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007637 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007638 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007640 "0330 IOCB wake NOT set, "
7641 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007642 timeout, (timeleft / jiffies));
7643 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007644 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007645 } else {
7646 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007647 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007648 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007649 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007650 }
7651
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007652 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7653 creg_val = readl(phba->HCregaddr);
7654 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7655 writel(creg_val, phba->HCregaddr);
7656 readl(phba->HCregaddr); /* flush */
7657 }
7658
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007659 if (prspiocbq)
7660 piocb->context2 = NULL;
7661
7662 piocb->context_un.wait_queue = NULL;
7663 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007664 return retval;
7665}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007666
James Smarte59058c2008-08-24 21:49:00 -04007667/**
James Smart3621a712009-04-06 18:47:14 -04007668 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007669 * @phba: Pointer to HBA context object.
7670 * @pmboxq: Pointer to driver mailbox object.
7671 * @timeout: Timeout in number of seconds.
7672 *
7673 * This function issues the mailbox to firmware and waits for the
7674 * mailbox command to complete. If the mailbox command is not
7675 * completed within timeout seconds, it returns MBX_TIMEOUT.
7676 * The function waits for the mailbox completion using an
7677 * interruptible wait. If the thread is woken up due to a
7678 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7679 * should not free the mailbox resources, if this function returns
7680 * MBX_TIMEOUT.
7681 * This function will sleep while waiting for mailbox completion.
7682 * So, this function should not be called from any context which
7683 * does not allow sleeping. Due to the same reason, this function
7684 * cannot be called with interrupt disabled.
7685 * This function assumes that the mailbox completion occurs while
7686 * this function sleep. So, this function cannot be called from
7687 * the worker thread which processes mailbox completion.
7688 * This function is called in the context of HBA management
7689 * applications.
7690 * This function returns MBX_SUCCESS when successful.
7691 * This function is called with no lock held.
7692 **/
dea31012005-04-17 16:05:31 -05007693int
James Smart2e0fef82007-06-17 19:56:36 -05007694lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007695 uint32_t timeout)
7696{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007697 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007698 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007699 unsigned long flag;
dea31012005-04-17 16:05:31 -05007700
7701 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007702 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007703 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007704
James Smart495a7142008-06-14 22:52:59 -04007705 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007706 /* setup wake call as IOCB callback */
7707 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7708 /* setup context field to pass wait_queue pointer to wake function */
7709 pmboxq->context1 = &done_q;
7710
dea31012005-04-17 16:05:31 -05007711 /* now issue the command */
7712 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7713
7714 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007715 wait_event_interruptible_timeout(done_q,
7716 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7717 timeout * HZ);
7718
James Smart858c9f62007-06-17 19:56:39 -05007719 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007720 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007721 /*
7722 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7723 * else do not free the resources.
7724 */
7725 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007726 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007727 else {
James Smart7054a602007-04-25 09:52:34 -04007728 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007729 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7730 }
7731 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007732 }
7733
dea31012005-04-17 16:05:31 -05007734 return retval;
7735}
7736
James Smarte59058c2008-08-24 21:49:00 -04007737/**
James Smart3772a992009-05-22 14:50:54 -04007738 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007739 * @phba: Pointer to HBA context.
7740 *
James Smart3772a992009-05-22 14:50:54 -04007741 * This function is called to shutdown the driver's mailbox sub-system.
7742 * It first marks the mailbox sub-system is in a block state to prevent
7743 * the asynchronous mailbox command from issued off the pending mailbox
7744 * command queue. If the mailbox command sub-system shutdown is due to
7745 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7746 * the mailbox sub-system flush routine to forcefully bring down the
7747 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7748 * as with offline or HBA function reset), this routine will wait for the
7749 * outstanding mailbox command to complete before invoking the mailbox
7750 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007751 **/
James Smart3772a992009-05-22 14:50:54 -04007752void
7753lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007754{
James Smart3772a992009-05-22 14:50:54 -04007755 struct lpfc_sli *psli = &phba->sli;
7756 uint8_t actcmd = MBX_HEARTBEAT;
7757 unsigned long timeout;
7758
7759 spin_lock_irq(&phba->hbalock);
7760 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7761 spin_unlock_irq(&phba->hbalock);
7762
7763 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7764 spin_lock_irq(&phba->hbalock);
7765 if (phba->sli.mbox_active)
7766 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7767 spin_unlock_irq(&phba->hbalock);
7768 /* Determine how long we might wait for the active mailbox
7769 * command to be gracefully completed by firmware.
7770 */
7771 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7772 1000) + jiffies;
7773 while (phba->sli.mbox_active) {
7774 /* Check active mailbox complete status every 2ms */
7775 msleep(2);
7776 if (time_after(jiffies, timeout))
7777 /* Timeout, let the mailbox flush routine to
7778 * forcefully release active mailbox command
7779 */
7780 break;
7781 }
7782 }
7783 lpfc_sli_mbox_sys_flush(phba);
7784}
7785
7786/**
7787 * lpfc_sli_eratt_read - read sli-3 error attention events
7788 * @phba: Pointer to HBA context.
7789 *
7790 * This function is called to read the SLI3 device error attention registers
7791 * for possible error attention events. The caller must hold the hostlock
7792 * with spin_lock_irq().
7793 *
7794 * This fucntion returns 1 when there is Error Attention in the Host Attention
7795 * Register and returns 0 otherwise.
7796 **/
7797static int
7798lpfc_sli_eratt_read(struct lpfc_hba *phba)
7799{
James Smarted957682007-06-17 19:56:37 -05007800 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007801
James Smart3772a992009-05-22 14:50:54 -04007802 /* Read chip Host Attention (HA) register */
7803 ha_copy = readl(phba->HAregaddr);
7804 if (ha_copy & HA_ERATT) {
7805 /* Read host status register to retrieve error event */
7806 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007807
James Smart3772a992009-05-22 14:50:54 -04007808 /* Check if there is a deferred error condition is active */
7809 if ((HS_FFER1 & phba->work_hs) &&
7810 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7811 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007812 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007813 /* Clear all interrupt enable conditions */
7814 writel(0, phba->HCregaddr);
7815 readl(phba->HCregaddr);
7816 }
7817
7818 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007819 phba->work_ha |= HA_ERATT;
7820 /* Indicate polling handles this ERATT */
7821 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007822 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007823 }
James Smart3772a992009-05-22 14:50:54 -04007824 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007825}
7826
James Smarte59058c2008-08-24 21:49:00 -04007827/**
James Smartda0436e2009-05-22 14:51:39 -04007828 * lpfc_sli4_eratt_read - read sli-4 error attention events
7829 * @phba: Pointer to HBA context.
7830 *
7831 * This function is called to read the SLI4 device error attention registers
7832 * for possible error attention events. The caller must hold the hostlock
7833 * with spin_lock_irq().
7834 *
7835 * This fucntion returns 1 when there is Error Attention in the Host Attention
7836 * Register and returns 0 otherwise.
7837 **/
7838static int
7839lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7840{
7841 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007842
7843 /* For now, use the SLI4 device internal unrecoverable error
7844 * registers for error attention. This can be changed later.
7845 */
James Smarta747c9c2009-11-18 15:41:10 -05007846 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7847 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7848 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7849 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7851 "1423 HBA Unrecoverable error: "
7852 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7853 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7854 uerr_sta_lo, uerr_sta_hi,
7855 phba->sli4_hba.ue_mask_lo,
7856 phba->sli4_hba.ue_mask_hi);
7857 phba->work_status[0] = uerr_sta_lo;
7858 phba->work_status[1] = uerr_sta_hi;
7859 /* Set the driver HA work bitmap */
7860 phba->work_ha |= HA_ERATT;
7861 /* Indicate polling handles this ERATT */
7862 phba->hba_flag |= HBA_ERATT_HANDLED;
7863 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007864 }
7865 return 0;
7866}
7867
7868/**
James Smart3621a712009-04-06 18:47:14 -04007869 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007870 * @phba: Pointer to HBA context.
7871 *
James Smart3772a992009-05-22 14:50:54 -04007872 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007873 * error attention register bit for error attention events.
7874 *
7875 * This fucntion returns 1 when there is Error Attention in the Host Attention
7876 * Register and returns 0 otherwise.
7877 **/
7878int
7879lpfc_sli_check_eratt(struct lpfc_hba *phba)
7880{
7881 uint32_t ha_copy;
7882
7883 /* If somebody is waiting to handle an eratt, don't process it
7884 * here. The brdkill function will do this.
7885 */
7886 if (phba->link_flag & LS_IGNORE_ERATT)
7887 return 0;
7888
7889 /* Check if interrupt handler handles this ERATT */
7890 spin_lock_irq(&phba->hbalock);
7891 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7892 /* Interrupt handler has handled ERATT */
7893 spin_unlock_irq(&phba->hbalock);
7894 return 0;
7895 }
7896
James Smarta257bf92009-04-06 18:48:10 -04007897 /*
7898 * If there is deferred error attention, do not check for error
7899 * attention
7900 */
7901 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7902 spin_unlock_irq(&phba->hbalock);
7903 return 0;
7904 }
7905
James Smart3772a992009-05-22 14:50:54 -04007906 /* If PCI channel is offline, don't process it */
7907 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007908 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007909 return 0;
7910 }
7911
7912 switch (phba->sli_rev) {
7913 case LPFC_SLI_REV2:
7914 case LPFC_SLI_REV3:
7915 /* Read chip Host Attention (HA) register */
7916 ha_copy = lpfc_sli_eratt_read(phba);
7917 break;
James Smartda0436e2009-05-22 14:51:39 -04007918 case LPFC_SLI_REV4:
7919 /* Read devcie Uncoverable Error (UERR) registers */
7920 ha_copy = lpfc_sli4_eratt_read(phba);
7921 break;
James Smart3772a992009-05-22 14:50:54 -04007922 default:
7923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7924 "0299 Invalid SLI revision (%d)\n",
7925 phba->sli_rev);
7926 ha_copy = 0;
7927 break;
James Smart93996272008-08-24 21:50:30 -04007928 }
7929 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007930
7931 return ha_copy;
7932}
7933
7934/**
7935 * lpfc_intr_state_check - Check device state for interrupt handling
7936 * @phba: Pointer to HBA context.
7937 *
7938 * This inline routine checks whether a device or its PCI slot is in a state
7939 * that the interrupt should be handled.
7940 *
7941 * This function returns 0 if the device or the PCI slot is in a state that
7942 * interrupt should be handled, otherwise -EIO.
7943 */
7944static inline int
7945lpfc_intr_state_check(struct lpfc_hba *phba)
7946{
7947 /* If the pci channel is offline, ignore all the interrupts */
7948 if (unlikely(pci_channel_offline(phba->pcidev)))
7949 return -EIO;
7950
7951 /* Update device level interrupt statistics */
7952 phba->sli.slistat.sli_intr++;
7953
7954 /* Ignore all interrupts during initialization. */
7955 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7956 return -EIO;
7957
James Smart93996272008-08-24 21:50:30 -04007958 return 0;
7959}
7960
7961/**
James Smart3772a992009-05-22 14:50:54 -04007962 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007963 * @irq: Interrupt number.
7964 * @dev_id: The device context pointer.
7965 *
James Smart93996272008-08-24 21:50:30 -04007966 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007967 * service routine when device with SLI-3 interface spec is enabled with
7968 * MSI-X multi-message interrupt mode and there are slow-path events in
7969 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7970 * interrupt mode, this function is called as part of the device-level
7971 * interrupt handler. When the PCI slot is in error recovery or the HBA
7972 * is undergoing initialization, the interrupt handler will not process
7973 * the interrupt. The link attention and ELS ring attention events are
7974 * handled by the worker thread. The interrupt handler signals the worker
7975 * thread and returns for these events. This function is called without
7976 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007977 * structures.
7978 *
7979 * This function returns IRQ_HANDLED when interrupt is handled else it
7980 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007981 **/
dea31012005-04-17 16:05:31 -05007982irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007983lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007984{
James Smart2e0fef82007-06-17 19:56:36 -05007985 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007986 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007987 uint32_t work_ha_copy;
7988 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007989 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007990 uint32_t control;
7991
James Smart92d7f7b2007-06-17 19:56:38 -05007992 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007993 struct lpfc_vport *vport;
7994 struct lpfc_nodelist *ndlp;
7995 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007996 LPFC_MBOXQ_t *pmb;
7997 int rc;
7998
dea31012005-04-17 16:05:31 -05007999 /*
8000 * Get the driver's phba structure from the dev_id and
8001 * assume the HBA is not interrupting.
8002 */
James Smart93996272008-08-24 21:50:30 -04008003 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008004
8005 if (unlikely(!phba))
8006 return IRQ_NONE;
8007
dea31012005-04-17 16:05:31 -05008008 /*
James Smart93996272008-08-24 21:50:30 -04008009 * Stuff needs to be attented to when this function is invoked as an
8010 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008011 */
James Smart93996272008-08-24 21:50:30 -04008012 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008013 /* Check device state for handling interrupt */
8014 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008015 return IRQ_NONE;
8016 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008017 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008018 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008019 /* If somebody is waiting to handle an eratt don't process it
8020 * here. The brdkill function will do this.
8021 */
8022 if (phba->link_flag & LS_IGNORE_ERATT)
8023 ha_copy &= ~HA_ERATT;
8024 /* Check the need for handling ERATT in interrupt handler */
8025 if (ha_copy & HA_ERATT) {
8026 if (phba->hba_flag & HBA_ERATT_HANDLED)
8027 /* ERATT polling has handled ERATT */
8028 ha_copy &= ~HA_ERATT;
8029 else
8030 /* Indicate interrupt handler handles ERATT */
8031 phba->hba_flag |= HBA_ERATT_HANDLED;
8032 }
James Smarta257bf92009-04-06 18:48:10 -04008033
8034 /*
8035 * If there is deferred error attention, do not check for any
8036 * interrupt.
8037 */
8038 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008039 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008040 return IRQ_NONE;
8041 }
8042
James Smart93996272008-08-24 21:50:30 -04008043 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008044 hc_copy = readl(phba->HCregaddr);
8045 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8046 HC_LAINT_ENA | HC_ERINT_ENA),
8047 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008048 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8049 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008050 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008051 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008052 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008053 } else
8054 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008055
dea31012005-04-17 16:05:31 -05008056 work_ha_copy = ha_copy & phba->work_ha_mask;
8057
James Smart93996272008-08-24 21:50:30 -04008058 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008059 if (work_ha_copy & HA_LATT) {
8060 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8061 /*
8062 * Turn off Link Attention interrupts
8063 * until CLEAR_LA done
8064 */
James Smart5b75da22008-12-04 22:39:35 -05008065 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008066 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8067 control = readl(phba->HCregaddr);
8068 control &= ~HC_LAINT_ENA;
8069 writel(control, phba->HCregaddr);
8070 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008071 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008072 }
8073 else
8074 work_ha_copy &= ~HA_LATT;
8075 }
8076
James Smart93996272008-08-24 21:50:30 -04008077 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008078 /*
8079 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8080 * the only slow ring.
8081 */
8082 status = (work_ha_copy &
8083 (HA_RXMASK << (4*LPFC_ELS_RING)));
8084 status >>= (4*LPFC_ELS_RING);
8085 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008086 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008087 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008088
8089 lpfc_debugfs_slow_ring_trc(phba,
8090 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8091 control, status,
8092 (uint32_t)phba->sli.slistat.sli_intr);
8093
James Smart858c9f62007-06-17 19:56:39 -05008094 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008095 lpfc_debugfs_slow_ring_trc(phba,
8096 "ISR Disable ring:"
8097 "pwork:x%x hawork:x%x wait:x%x",
8098 phba->work_ha, work_ha_copy,
8099 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008100 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008101
James Smart858c9f62007-06-17 19:56:39 -05008102 control &=
8103 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008104 writel(control, phba->HCregaddr);
8105 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008106 }
James Smarta58cbd52007-08-02 11:09:43 -04008107 else {
8108 lpfc_debugfs_slow_ring_trc(phba,
8109 "ISR slow ring: pwork:"
8110 "x%x hawork:x%x wait:x%x",
8111 phba->work_ha, work_ha_copy,
8112 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008113 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008114 }
James Smart5b75da22008-12-04 22:39:35 -05008115 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008116 }
8117 }
James Smart5b75da22008-12-04 22:39:35 -05008118 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008119 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008120 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008121 /*
8122 * Check if there is a deferred error condition
8123 * is active
8124 */
8125 if ((HS_FFER1 & phba->work_hs) &&
8126 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8127 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8128 phba->hba_flag |= DEFER_ERATT;
8129 /* Clear all interrupt enable conditions */
8130 writel(0, phba->HCregaddr);
8131 readl(phba->HCregaddr);
8132 }
8133 }
8134
James Smart93996272008-08-24 21:50:30 -04008135 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008136 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008137 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008138 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008139 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008140
8141 /* First check out the status word */
8142 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8143 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008144 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008145 /*
8146 * Stray Mailbox Interrupt, mbxCommand <cmd>
8147 * mbxStatus <status>
8148 */
James Smart09372822008-01-11 01:52:54 -05008149 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008150 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008151 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008152 "Interrupt mbxCommand x%x "
8153 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008154 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008155 pmbox->mbxCommand,
8156 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008157 /* clear mailbox attention bit */
8158 work_ha_copy &= ~HA_MBATT;
8159 } else {
James Smart97eab632008-04-07 10:16:05 -04008160 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008161 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008162 phba->last_completion_time = jiffies;
8163 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008164 if (pmb->mbox_cmpl) {
8165 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8166 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008167 if (pmb->out_ext_byte_len &&
8168 pmb->context2)
8169 lpfc_sli_pcimem_bcopy(
8170 phba->mbox_ext,
8171 pmb->context2,
8172 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008173 }
James Smart09372822008-01-11 01:52:54 -05008174 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8175 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8176
8177 lpfc_debugfs_disc_trc(vport,
8178 LPFC_DISC_TRC_MBOX_VPORT,
8179 "MBOX dflt rpi: : "
8180 "status:x%x rpi:x%x",
8181 (uint32_t)pmbox->mbxStatus,
8182 pmbox->un.varWords[0], 0);
8183
8184 if (!pmbox->mbxStatus) {
8185 mp = (struct lpfc_dmabuf *)
8186 (pmb->context1);
8187 ndlp = (struct lpfc_nodelist *)
8188 pmb->context2;
8189
8190 /* Reg_LOGIN of dflt RPI was
8191 * successful. new lets get
8192 * rid of the RPI using the
8193 * same mbox buffer.
8194 */
8195 lpfc_unreg_login(phba,
8196 vport->vpi,
8197 pmbox->un.varWords[0],
8198 pmb);
8199 pmb->mbox_cmpl =
8200 lpfc_mbx_cmpl_dflt_rpi;
8201 pmb->context1 = mp;
8202 pmb->context2 = ndlp;
8203 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008204 rc = lpfc_sli_issue_mbox(phba,
8205 pmb,
8206 MBX_NOWAIT);
8207 if (rc != MBX_BUSY)
8208 lpfc_printf_log(phba,
8209 KERN_ERR,
8210 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008211 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008212 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008213 if (rc != MBX_NOT_FINISHED)
8214 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008215 }
8216 }
James Smart5b75da22008-12-04 22:39:35 -05008217 spin_lock_irqsave(
8218 &phba->pport->work_port_lock,
8219 iflag);
James Smart09372822008-01-11 01:52:54 -05008220 phba->pport->work_port_events &=
8221 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008222 spin_unlock_irqrestore(
8223 &phba->pport->work_port_lock,
8224 iflag);
James Smart09372822008-01-11 01:52:54 -05008225 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008226 }
James Smart97eab632008-04-07 10:16:05 -04008227 } else
James Smart5b75da22008-12-04 22:39:35 -05008228 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008229
James Smart92d7f7b2007-06-17 19:56:38 -05008230 if ((work_ha_copy & HA_MBATT) &&
8231 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008232send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008233 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008234 do {
8235 rc = lpfc_sli_issue_mbox(phba, NULL,
8236 MBX_NOWAIT);
8237 } while (rc == MBX_NOT_FINISHED);
8238 if (rc != MBX_SUCCESS)
8239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8240 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008241 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008242 }
8243
James Smart5b75da22008-12-04 22:39:35 -05008244 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008245 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008246 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008247 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008248 }
James Smart93996272008-08-24 21:50:30 -04008249 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008250
James Smart3772a992009-05-22 14:50:54 -04008251} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008252
8253/**
James Smart3772a992009-05-22 14:50:54 -04008254 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008255 * @irq: Interrupt number.
8256 * @dev_id: The device context pointer.
8257 *
8258 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008259 * service routine when device with SLI-3 interface spec is enabled with
8260 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8261 * ring event in the HBA. However, when the device is enabled with either
8262 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8263 * device-level interrupt handler. When the PCI slot is in error recovery
8264 * or the HBA is undergoing initialization, the interrupt handler will not
8265 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8266 * the intrrupt context. This function is called without any lock held.
8267 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008268 *
8269 * This function returns IRQ_HANDLED when interrupt is handled else it
8270 * returns IRQ_NONE.
8271 **/
8272irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008273lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008274{
8275 struct lpfc_hba *phba;
8276 uint32_t ha_copy;
8277 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008278 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008279
8280 /* Get the driver's phba structure from the dev_id and
8281 * assume the HBA is not interrupting.
8282 */
8283 phba = (struct lpfc_hba *) dev_id;
8284
8285 if (unlikely(!phba))
8286 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008287
8288 /*
James Smart93996272008-08-24 21:50:30 -04008289 * Stuff needs to be attented to when this function is invoked as an
8290 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008291 */
James Smart93996272008-08-24 21:50:30 -04008292 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008293 /* Check device state for handling interrupt */
8294 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008295 return IRQ_NONE;
8296 /* Need to read HA REG for FCP ring and other ring events */
8297 ha_copy = readl(phba->HAregaddr);
8298 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008299 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008300 /*
8301 * If there is deferred error attention, do not check for
8302 * any interrupt.
8303 */
8304 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008305 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008306 return IRQ_NONE;
8307 }
James Smart93996272008-08-24 21:50:30 -04008308 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8309 phba->HAregaddr);
8310 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008311 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008312 } else
8313 ha_copy = phba->ha_copy;
8314
8315 /*
8316 * Process all events on FCP ring. Take the optimized path for FCP IO.
8317 */
8318 ha_copy &= ~(phba->work_ha_mask);
8319
8320 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008321 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008322 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008323 lpfc_sli_handle_fast_ring_event(phba,
8324 &phba->sli.ring[LPFC_FCP_RING],
8325 status);
James Smarta4bc3372006-12-02 13:34:16 -05008326
8327 if (phba->cfg_multi_ring_support == 2) {
8328 /*
James Smart93996272008-08-24 21:50:30 -04008329 * Process all events on extra ring. Take the optimized path
8330 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008331 */
James Smart93996272008-08-24 21:50:30 -04008332 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008333 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008334 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008335 lpfc_sli_handle_fast_ring_event(phba,
8336 &phba->sli.ring[LPFC_EXTRA_RING],
8337 status);
8338 }
8339 }
dea31012005-04-17 16:05:31 -05008340 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008341} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008342
James Smart93996272008-08-24 21:50:30 -04008343/**
James Smart3772a992009-05-22 14:50:54 -04008344 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008345 * @irq: Interrupt number.
8346 * @dev_id: The device context pointer.
8347 *
James Smart3772a992009-05-22 14:50:54 -04008348 * This function is the HBA device-level interrupt handler to device with
8349 * SLI-3 interface spec, called from the PCI layer when either MSI or
8350 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8351 * requires driver attention. This function invokes the slow-path interrupt
8352 * attention handling function and fast-path interrupt attention handling
8353 * function in turn to process the relevant HBA attention events. This
8354 * function is called without any lock held. It gets the hbalock to access
8355 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008356 *
8357 * This function returns IRQ_HANDLED when interrupt is handled, else it
8358 * returns IRQ_NONE.
8359 **/
8360irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008361lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008362{
8363 struct lpfc_hba *phba;
8364 irqreturn_t sp_irq_rc, fp_irq_rc;
8365 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008366 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008367
8368 /*
8369 * Get the driver's phba structure from the dev_id and
8370 * assume the HBA is not interrupting.
8371 */
8372 phba = (struct lpfc_hba *) dev_id;
8373
8374 if (unlikely(!phba))
8375 return IRQ_NONE;
8376
James Smart3772a992009-05-22 14:50:54 -04008377 /* Check device state for handling interrupt */
8378 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008379 return IRQ_NONE;
8380
8381 spin_lock(&phba->hbalock);
8382 phba->ha_copy = readl(phba->HAregaddr);
8383 if (unlikely(!phba->ha_copy)) {
8384 spin_unlock(&phba->hbalock);
8385 return IRQ_NONE;
8386 } else if (phba->ha_copy & HA_ERATT) {
8387 if (phba->hba_flag & HBA_ERATT_HANDLED)
8388 /* ERATT polling has handled ERATT */
8389 phba->ha_copy &= ~HA_ERATT;
8390 else
8391 /* Indicate interrupt handler handles ERATT */
8392 phba->hba_flag |= HBA_ERATT_HANDLED;
8393 }
8394
James Smarta257bf92009-04-06 18:48:10 -04008395 /*
8396 * If there is deferred error attention, do not check for any interrupt.
8397 */
8398 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8399 spin_unlock_irq(&phba->hbalock);
8400 return IRQ_NONE;
8401 }
8402
James Smart93996272008-08-24 21:50:30 -04008403 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008404 hc_copy = readl(phba->HCregaddr);
8405 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8406 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8407 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008408 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008409 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008410 readl(phba->HAregaddr); /* flush */
8411 spin_unlock(&phba->hbalock);
8412
8413 /*
8414 * Invokes slow-path host attention interrupt handling as appropriate.
8415 */
8416
8417 /* status of events with mailbox and link attention */
8418 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8419
8420 /* status of events with ELS ring */
8421 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8422 status2 >>= (4*LPFC_ELS_RING);
8423
8424 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008425 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008426 else
8427 sp_irq_rc = IRQ_NONE;
8428
8429 /*
8430 * Invoke fast-path host attention interrupt handling as appropriate.
8431 */
8432
8433 /* status of events with FCP ring */
8434 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8435 status1 >>= (4*LPFC_FCP_RING);
8436
8437 /* status of events with extra ring */
8438 if (phba->cfg_multi_ring_support == 2) {
8439 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8440 status2 >>= (4*LPFC_EXTRA_RING);
8441 } else
8442 status2 = 0;
8443
8444 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008445 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008446 else
8447 fp_irq_rc = IRQ_NONE;
8448
8449 /* Return device-level interrupt handling status */
8450 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008451} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008452
8453/**
8454 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8455 * @phba: pointer to lpfc hba data structure.
8456 *
8457 * This routine is invoked by the worker thread to process all the pending
8458 * SLI4 FCP abort XRI events.
8459 **/
8460void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8461{
8462 struct lpfc_cq_event *cq_event;
8463
8464 /* First, declare the fcp xri abort event has been handled */
8465 spin_lock_irq(&phba->hbalock);
8466 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8467 spin_unlock_irq(&phba->hbalock);
8468 /* Now, handle all the fcp xri abort events */
8469 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8470 /* Get the first event from the head of the event queue */
8471 spin_lock_irq(&phba->hbalock);
8472 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8473 cq_event, struct lpfc_cq_event, list);
8474 spin_unlock_irq(&phba->hbalock);
8475 /* Notify aborted XRI for FCP work queue */
8476 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8477 /* Free the event processed back to the free pool */
8478 lpfc_sli4_cq_event_release(phba, cq_event);
8479 }
8480}
8481
8482/**
8483 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8484 * @phba: pointer to lpfc hba data structure.
8485 *
8486 * This routine is invoked by the worker thread to process all the pending
8487 * SLI4 els abort xri events.
8488 **/
8489void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8490{
8491 struct lpfc_cq_event *cq_event;
8492
8493 /* First, declare the els xri abort event has been handled */
8494 spin_lock_irq(&phba->hbalock);
8495 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8496 spin_unlock_irq(&phba->hbalock);
8497 /* Now, handle all the els xri abort events */
8498 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8499 /* Get the first event from the head of the event queue */
8500 spin_lock_irq(&phba->hbalock);
8501 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8502 cq_event, struct lpfc_cq_event, list);
8503 spin_unlock_irq(&phba->hbalock);
8504 /* Notify aborted XRI for ELS work queue */
8505 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8506 /* Free the event processed back to the free pool */
8507 lpfc_sli4_cq_event_release(phba, cq_event);
8508 }
8509}
8510
James Smart341af102010-01-26 23:07:37 -05008511/**
8512 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8513 * @phba: pointer to lpfc hba data structure
8514 * @pIocbIn: pointer to the rspiocbq
8515 * @pIocbOut: pointer to the cmdiocbq
8516 * @wcqe: pointer to the complete wcqe
8517 *
8518 * This routine transfers the fields of a command iocbq to a response iocbq
8519 * by copying all the IOCB fields from command iocbq and transferring the
8520 * completion status information from the complete wcqe.
8521 **/
James Smart4f774512009-05-22 14:52:35 -04008522static void
James Smart341af102010-01-26 23:07:37 -05008523lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8524 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008525 struct lpfc_iocbq *pIocbOut,
8526 struct lpfc_wcqe_complete *wcqe)
8527{
James Smart341af102010-01-26 23:07:37 -05008528 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008529 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8530
8531 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8532 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008533 /* Map WCQE parameters into irspiocb parameters */
8534 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8535 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8536 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8537 pIocbIn->iocb.un.fcpi.fcpi_parm =
8538 pIocbOut->iocb.un.fcpi.fcpi_parm -
8539 wcqe->total_data_placed;
8540 else
8541 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008542 else {
James Smart4f774512009-05-22 14:52:35 -04008543 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008544 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8545 }
James Smart341af102010-01-26 23:07:37 -05008546
8547 /* Pick up HBA exchange busy condition */
8548 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8549 spin_lock_irqsave(&phba->hbalock, iflags);
8550 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8551 spin_unlock_irqrestore(&phba->hbalock, iflags);
8552 }
James Smart4f774512009-05-22 14:52:35 -04008553}
8554
8555/**
James Smart45ed1192009-10-02 15:17:02 -04008556 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8557 * @phba: Pointer to HBA context object.
8558 * @wcqe: Pointer to work-queue completion queue entry.
8559 *
8560 * This routine handles an ELS work-queue completion event and construct
8561 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8562 * discovery engine to handle.
8563 *
8564 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8565 **/
8566static struct lpfc_iocbq *
8567lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8568 struct lpfc_iocbq *irspiocbq)
8569{
8570 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8571 struct lpfc_iocbq *cmdiocbq;
8572 struct lpfc_wcqe_complete *wcqe;
8573 unsigned long iflags;
8574
8575 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8576 spin_lock_irqsave(&phba->hbalock, iflags);
8577 pring->stats.iocb_event++;
8578 /* Look up the ELS command IOCB and create pseudo response IOCB */
8579 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8580 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8581 spin_unlock_irqrestore(&phba->hbalock, iflags);
8582
8583 if (unlikely(!cmdiocbq)) {
8584 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8585 "0386 ELS complete with no corresponding "
8586 "cmdiocb: iotag (%d)\n",
8587 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8588 lpfc_sli_release_iocbq(phba, irspiocbq);
8589 return NULL;
8590 }
8591
8592 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008593 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008594
8595 return irspiocbq;
8596}
8597
8598/**
James Smart04c68492009-05-22 14:52:52 -04008599 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8600 * @phba: Pointer to HBA context object.
8601 * @cqe: Pointer to mailbox completion queue entry.
8602 *
8603 * This routine process a mailbox completion queue entry with asynchrous
8604 * event.
8605 *
8606 * Return: true if work posted to worker thread, otherwise false.
8607 **/
8608static bool
8609lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8610{
8611 struct lpfc_cq_event *cq_event;
8612 unsigned long iflags;
8613
8614 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8615 "0392 Async Event: word0:x%x, word1:x%x, "
8616 "word2:x%x, word3:x%x\n", mcqe->word0,
8617 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8618
8619 /* Allocate a new internal CQ_EVENT entry */
8620 cq_event = lpfc_sli4_cq_event_alloc(phba);
8621 if (!cq_event) {
8622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8623 "0394 Failed to allocate CQ_EVENT entry\n");
8624 return false;
8625 }
8626
8627 /* Move the CQE into an asynchronous event entry */
8628 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8629 spin_lock_irqsave(&phba->hbalock, iflags);
8630 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8631 /* Set the async event flag */
8632 phba->hba_flag |= ASYNC_EVENT;
8633 spin_unlock_irqrestore(&phba->hbalock, iflags);
8634
8635 return true;
8636}
8637
8638/**
8639 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8640 * @phba: Pointer to HBA context object.
8641 * @cqe: Pointer to mailbox completion queue entry.
8642 *
8643 * This routine process a mailbox completion queue entry with mailbox
8644 * completion event.
8645 *
8646 * Return: true if work posted to worker thread, otherwise false.
8647 **/
8648static bool
8649lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8650{
8651 uint32_t mcqe_status;
8652 MAILBOX_t *mbox, *pmbox;
8653 struct lpfc_mqe *mqe;
8654 struct lpfc_vport *vport;
8655 struct lpfc_nodelist *ndlp;
8656 struct lpfc_dmabuf *mp;
8657 unsigned long iflags;
8658 LPFC_MBOXQ_t *pmb;
8659 bool workposted = false;
8660 int rc;
8661
8662 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8663 if (!bf_get(lpfc_trailer_completed, mcqe))
8664 goto out_no_mqe_complete;
8665
8666 /* Get the reference to the active mbox command */
8667 spin_lock_irqsave(&phba->hbalock, iflags);
8668 pmb = phba->sli.mbox_active;
8669 if (unlikely(!pmb)) {
8670 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8671 "1832 No pending MBOX command to handle\n");
8672 spin_unlock_irqrestore(&phba->hbalock, iflags);
8673 goto out_no_mqe_complete;
8674 }
8675 spin_unlock_irqrestore(&phba->hbalock, iflags);
8676 mqe = &pmb->u.mqe;
8677 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8678 mbox = phba->mbox;
8679 vport = pmb->vport;
8680
8681 /* Reset heartbeat timer */
8682 phba->last_completion_time = jiffies;
8683 del_timer(&phba->sli.mbox_tmo);
8684
8685 /* Move mbox data to caller's mailbox region, do endian swapping */
8686 if (pmb->mbox_cmpl && mbox)
8687 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8688 /* Set the mailbox status with SLI4 range 0x4000 */
8689 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8690 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8691 bf_set(lpfc_mqe_status, mqe,
8692 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8693
8694 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8695 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8696 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8697 "MBOX dflt rpi: status:x%x rpi:x%x",
8698 mcqe_status,
8699 pmbox->un.varWords[0], 0);
8700 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8701 mp = (struct lpfc_dmabuf *)(pmb->context1);
8702 ndlp = (struct lpfc_nodelist *)pmb->context2;
8703 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8704 * RID of the PPI using the same mbox buffer.
8705 */
8706 lpfc_unreg_login(phba, vport->vpi,
8707 pmbox->un.varWords[0], pmb);
8708 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8709 pmb->context1 = mp;
8710 pmb->context2 = ndlp;
8711 pmb->vport = vport;
8712 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8713 if (rc != MBX_BUSY)
8714 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8715 LOG_SLI, "0385 rc should "
8716 "have been MBX_BUSY\n");
8717 if (rc != MBX_NOT_FINISHED)
8718 goto send_current_mbox;
8719 }
8720 }
8721 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8722 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8723 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8724
8725 /* There is mailbox completion work to do */
8726 spin_lock_irqsave(&phba->hbalock, iflags);
8727 __lpfc_mbox_cmpl_put(phba, pmb);
8728 phba->work_ha |= HA_MBATT;
8729 spin_unlock_irqrestore(&phba->hbalock, iflags);
8730 workposted = true;
8731
8732send_current_mbox:
8733 spin_lock_irqsave(&phba->hbalock, iflags);
8734 /* Release the mailbox command posting token */
8735 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8736 /* Setting active mailbox pointer need to be in sync to flag clear */
8737 phba->sli.mbox_active = NULL;
8738 spin_unlock_irqrestore(&phba->hbalock, iflags);
8739 /* Wake up worker thread to post the next pending mailbox command */
8740 lpfc_worker_wake_up(phba);
8741out_no_mqe_complete:
8742 if (bf_get(lpfc_trailer_consumed, mcqe))
8743 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8744 return workposted;
8745}
8746
8747/**
8748 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8749 * @phba: Pointer to HBA context object.
8750 * @cqe: Pointer to mailbox completion queue entry.
8751 *
8752 * This routine process a mailbox completion queue entry, it invokes the
8753 * proper mailbox complete handling or asynchrous event handling routine
8754 * according to the MCQE's async bit.
8755 *
8756 * Return: true if work posted to worker thread, otherwise false.
8757 **/
8758static bool
8759lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8760{
8761 struct lpfc_mcqe mcqe;
8762 bool workposted;
8763
8764 /* Copy the mailbox MCQE and convert endian order as needed */
8765 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8766
8767 /* Invoke the proper event handling routine */
8768 if (!bf_get(lpfc_trailer_async, &mcqe))
8769 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8770 else
8771 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8772 return workposted;
8773}
8774
8775/**
James Smart4f774512009-05-22 14:52:35 -04008776 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8777 * @phba: Pointer to HBA context object.
8778 * @wcqe: Pointer to work-queue completion queue entry.
8779 *
8780 * This routine handles an ELS work-queue completion event.
8781 *
8782 * Return: true if work posted to worker thread, otherwise false.
8783 **/
8784static bool
8785lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8786 struct lpfc_wcqe_complete *wcqe)
8787{
James Smart4f774512009-05-22 14:52:35 -04008788 struct lpfc_iocbq *irspiocbq;
8789 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008790
James Smart45ed1192009-10-02 15:17:02 -04008791 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008792 irspiocbq = lpfc_sli_get_iocbq(phba);
8793 if (!irspiocbq) {
8794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8795 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008796 return false;
James Smart4f774512009-05-22 14:52:35 -04008797 }
James Smart4f774512009-05-22 14:52:35 -04008798
James Smart45ed1192009-10-02 15:17:02 -04008799 /* Save off the slow-path queue event for work thread to process */
8800 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008801 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008802 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008803 &phba->sli4_hba.sp_queue_event);
8804 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008805 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008806
James Smart45ed1192009-10-02 15:17:02 -04008807 return true;
James Smart4f774512009-05-22 14:52:35 -04008808}
8809
8810/**
8811 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8812 * @phba: Pointer to HBA context object.
8813 * @wcqe: Pointer to work-queue completion queue entry.
8814 *
8815 * This routine handles slow-path WQ entry comsumed event by invoking the
8816 * proper WQ release routine to the slow-path WQ.
8817 **/
8818static void
8819lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8820 struct lpfc_wcqe_release *wcqe)
8821{
8822 /* Check for the slow-path ELS work queue */
8823 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8824 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8825 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8826 else
8827 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8828 "2579 Slow-path wqe consume event carries "
8829 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8830 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8831 phba->sli4_hba.els_wq->queue_id);
8832}
8833
8834/**
8835 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8836 * @phba: Pointer to HBA context object.
8837 * @cq: Pointer to a WQ completion queue.
8838 * @wcqe: Pointer to work-queue completion queue entry.
8839 *
8840 * This routine handles an XRI abort event.
8841 *
8842 * Return: true if work posted to worker thread, otherwise false.
8843 **/
8844static bool
8845lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8846 struct lpfc_queue *cq,
8847 struct sli4_wcqe_xri_aborted *wcqe)
8848{
8849 bool workposted = false;
8850 struct lpfc_cq_event *cq_event;
8851 unsigned long iflags;
8852
8853 /* Allocate a new internal CQ_EVENT entry */
8854 cq_event = lpfc_sli4_cq_event_alloc(phba);
8855 if (!cq_event) {
8856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8857 "0602 Failed to allocate CQ_EVENT entry\n");
8858 return false;
8859 }
8860
8861 /* Move the CQE into the proper xri abort event list */
8862 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8863 switch (cq->subtype) {
8864 case LPFC_FCP:
8865 spin_lock_irqsave(&phba->hbalock, iflags);
8866 list_add_tail(&cq_event->list,
8867 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8868 /* Set the fcp xri abort event flag */
8869 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8870 spin_unlock_irqrestore(&phba->hbalock, iflags);
8871 workposted = true;
8872 break;
8873 case LPFC_ELS:
8874 spin_lock_irqsave(&phba->hbalock, iflags);
8875 list_add_tail(&cq_event->list,
8876 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8877 /* Set the els xri abort event flag */
8878 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8879 spin_unlock_irqrestore(&phba->hbalock, iflags);
8880 workposted = true;
8881 break;
8882 default:
8883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8884 "0603 Invalid work queue CQE subtype (x%x)\n",
8885 cq->subtype);
8886 workposted = false;
8887 break;
8888 }
8889 return workposted;
8890}
8891
8892/**
James Smart4d9ab992009-10-02 15:16:39 -04008893 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008894 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008895 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008896 *
James Smart4d9ab992009-10-02 15:16:39 -04008897 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008898 *
8899 * Return: true if work posted to worker thread, otherwise false.
8900 **/
8901static bool
James Smart4d9ab992009-10-02 15:16:39 -04008902lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8903{
8904 bool workposted = false;
8905 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8906 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8907 struct hbq_dmabuf *dma_buf;
8908 uint32_t status;
8909 unsigned long iflags;
8910
James Smart4d9ab992009-10-02 15:16:39 -04008911 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8912 goto out;
8913
8914 status = bf_get(lpfc_rcqe_status, rcqe);
8915 switch (status) {
8916 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8918 "2537 Receive Frame Truncated!!\n");
8919 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008920 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008921 spin_lock_irqsave(&phba->hbalock, iflags);
8922 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8923 if (!dma_buf) {
8924 spin_unlock_irqrestore(&phba->hbalock, iflags);
8925 goto out;
8926 }
8927 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8928 /* save off the frame for the word thread to process */
8929 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008930 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008931 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008932 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008933 spin_unlock_irqrestore(&phba->hbalock, iflags);
8934 workposted = true;
8935 break;
8936 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8937 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8938 /* Post more buffers if possible */
8939 spin_lock_irqsave(&phba->hbalock, iflags);
8940 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8941 spin_unlock_irqrestore(&phba->hbalock, iflags);
8942 workposted = true;
8943 break;
8944 }
8945out:
8946 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008947}
8948
8949/**
8950 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8951 * @phba: Pointer to HBA context object.
8952 * @cq: Pointer to the completion queue.
8953 * @wcqe: Pointer to a completion queue entry.
8954 *
8955 * This routine process a slow-path work-queue or recieve queue completion queue
8956 * entry.
8957 *
8958 * Return: true if work posted to worker thread, otherwise false.
8959 **/
8960static bool
8961lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008962 struct lpfc_cqe *cqe)
8963{
James Smart45ed1192009-10-02 15:17:02 -04008964 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008965 bool workposted = false;
8966
8967 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008968 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008969
8970 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008971 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008972 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008973 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008974 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008975 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008976 break;
8977 case CQE_CODE_RELEASE_WQE:
8978 /* Process the WQ release event */
8979 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008980 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008981 break;
8982 case CQE_CODE_XRI_ABORTED:
8983 /* Process the WQ XRI abort event */
8984 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008985 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008986 break;
James Smart4d9ab992009-10-02 15:16:39 -04008987 case CQE_CODE_RECEIVE:
8988 /* Process the RQ event */
8989 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008990 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008991 break;
James Smart4f774512009-05-22 14:52:35 -04008992 default:
8993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8994 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008995 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008996 break;
8997 }
8998 return workposted;
8999}
9000
9001/**
James Smart4f774512009-05-22 14:52:35 -04009002 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9003 * @phba: Pointer to HBA context object.
9004 * @eqe: Pointer to fast-path event queue entry.
9005 *
9006 * This routine process a event queue entry from the slow-path event queue.
9007 * It will check the MajorCode and MinorCode to determine this is for a
9008 * completion event on a completion queue, if not, an error shall be logged
9009 * and just return. Otherwise, it will get to the corresponding completion
9010 * queue and process all the entries on that completion queue, rearm the
9011 * completion queue, and then return.
9012 *
9013 **/
9014static void
9015lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9016{
9017 struct lpfc_queue *cq = NULL, *childq, *speq;
9018 struct lpfc_cqe *cqe;
9019 bool workposted = false;
9020 int ecount = 0;
9021 uint16_t cqid;
9022
James Smartcb5172e2010-03-15 11:25:07 -04009023 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9025 "0359 Not a valid slow-path completion "
9026 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009027 bf_get_le32(lpfc_eqe_major_code, eqe),
9028 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009029 return;
9030 }
9031
9032 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009033 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009034
9035 /* Search for completion queue pointer matching this cqid */
9036 speq = phba->sli4_hba.sp_eq;
9037 list_for_each_entry(childq, &speq->child_list, list) {
9038 if (childq->queue_id == cqid) {
9039 cq = childq;
9040 break;
9041 }
9042 }
9043 if (unlikely(!cq)) {
9044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9045 "0365 Slow-path CQ identifier (%d) does "
9046 "not exist\n", cqid);
9047 return;
9048 }
9049
9050 /* Process all the entries to the CQ */
9051 switch (cq->type) {
9052 case LPFC_MCQ:
9053 while ((cqe = lpfc_sli4_cq_get(cq))) {
9054 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9055 if (!(++ecount % LPFC_GET_QE_REL_INT))
9056 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9057 }
9058 break;
9059 case LPFC_WCQ:
9060 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009061 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009062 if (!(++ecount % LPFC_GET_QE_REL_INT))
9063 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9064 }
9065 break;
9066 default:
9067 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9068 "0370 Invalid completion queue type (%d)\n",
9069 cq->type);
9070 return;
9071 }
9072
9073 /* Catch the no cq entry condition, log an error */
9074 if (unlikely(ecount == 0))
9075 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9076 "0371 No entry from the CQ: identifier "
9077 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9078
9079 /* In any case, flash and re-arm the RCQ */
9080 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9081
9082 /* wake up worker thread if there are works to be done */
9083 if (workposted)
9084 lpfc_worker_wake_up(phba);
9085}
9086
9087/**
9088 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9089 * @eqe: Pointer to fast-path completion queue entry.
9090 *
9091 * This routine process a fast-path work queue completion entry from fast-path
9092 * event queue for FCP command response completion.
9093 **/
9094static void
9095lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9096 struct lpfc_wcqe_complete *wcqe)
9097{
9098 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9099 struct lpfc_iocbq *cmdiocbq;
9100 struct lpfc_iocbq irspiocbq;
9101 unsigned long iflags;
9102
9103 spin_lock_irqsave(&phba->hbalock, iflags);
9104 pring->stats.iocb_event++;
9105 spin_unlock_irqrestore(&phba->hbalock, iflags);
9106
9107 /* Check for response status */
9108 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9109 /* If resource errors reported from HBA, reduce queue
9110 * depth of the SCSI device.
9111 */
9112 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9113 IOSTAT_LOCAL_REJECT) &&
9114 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9115 phba->lpfc_rampdown_queue_depth(phba);
9116 }
9117 /* Log the error status */
9118 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9119 "0373 FCP complete error: status=x%x, "
9120 "hw_status=x%x, total_data_specified=%d, "
9121 "parameter=x%x, word3=x%x\n",
9122 bf_get(lpfc_wcqe_c_status, wcqe),
9123 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9124 wcqe->total_data_placed, wcqe->parameter,
9125 wcqe->word3);
9126 }
9127
9128 /* Look up the FCP command IOCB and create pseudo response IOCB */
9129 spin_lock_irqsave(&phba->hbalock, iflags);
9130 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9131 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9132 spin_unlock_irqrestore(&phba->hbalock, iflags);
9133 if (unlikely(!cmdiocbq)) {
9134 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9135 "0374 FCP complete with no corresponding "
9136 "cmdiocb: iotag (%d)\n",
9137 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9138 return;
9139 }
9140 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9141 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9142 "0375 FCP cmdiocb not callback function "
9143 "iotag: (%d)\n",
9144 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9145 return;
9146 }
9147
9148 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009149 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009150
James Smart0f65ff62010-02-26 14:14:23 -05009151 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9152 spin_lock_irqsave(&phba->hbalock, iflags);
9153 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9154 spin_unlock_irqrestore(&phba->hbalock, iflags);
9155 }
9156
James Smart4f774512009-05-22 14:52:35 -04009157 /* Pass the cmd_iocb and the rsp state to the upper layer */
9158 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9159}
9160
9161/**
9162 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9163 * @phba: Pointer to HBA context object.
9164 * @cq: Pointer to completion queue.
9165 * @wcqe: Pointer to work-queue completion queue entry.
9166 *
9167 * This routine handles an fast-path WQ entry comsumed event by invoking the
9168 * proper WQ release routine to the slow-path WQ.
9169 **/
9170static void
9171lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9172 struct lpfc_wcqe_release *wcqe)
9173{
9174 struct lpfc_queue *childwq;
9175 bool wqid_matched = false;
9176 uint16_t fcp_wqid;
9177
9178 /* Check for fast-path FCP work queue release */
9179 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9180 list_for_each_entry(childwq, &cq->child_list, list) {
9181 if (childwq->queue_id == fcp_wqid) {
9182 lpfc_sli4_wq_release(childwq,
9183 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9184 wqid_matched = true;
9185 break;
9186 }
9187 }
9188 /* Report warning log message if no match found */
9189 if (wqid_matched != true)
9190 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9191 "2580 Fast-path wqe consume event carries "
9192 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9193}
9194
9195/**
9196 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9197 * @cq: Pointer to the completion queue.
9198 * @eqe: Pointer to fast-path completion queue entry.
9199 *
9200 * This routine process a fast-path work queue completion entry from fast-path
9201 * event queue for FCP command response completion.
9202 **/
9203static int
9204lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9205 struct lpfc_cqe *cqe)
9206{
9207 struct lpfc_wcqe_release wcqe;
9208 bool workposted = false;
9209
9210 /* Copy the work queue CQE and convert endian order if needed */
9211 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9212
9213 /* Check and process for different type of WCQE and dispatch */
9214 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9215 case CQE_CODE_COMPL_WQE:
9216 /* Process the WQ complete event */
9217 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9218 (struct lpfc_wcqe_complete *)&wcqe);
9219 break;
9220 case CQE_CODE_RELEASE_WQE:
9221 /* Process the WQ release event */
9222 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9223 (struct lpfc_wcqe_release *)&wcqe);
9224 break;
9225 case CQE_CODE_XRI_ABORTED:
9226 /* Process the WQ XRI abort event */
9227 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9228 (struct sli4_wcqe_xri_aborted *)&wcqe);
9229 break;
9230 default:
9231 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9232 "0144 Not a valid WCQE code: x%x\n",
9233 bf_get(lpfc_wcqe_c_code, &wcqe));
9234 break;
9235 }
9236 return workposted;
9237}
9238
9239/**
9240 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9241 * @phba: Pointer to HBA context object.
9242 * @eqe: Pointer to fast-path event queue entry.
9243 *
9244 * This routine process a event queue entry from the fast-path event queue.
9245 * It will check the MajorCode and MinorCode to determine this is for a
9246 * completion event on a completion queue, if not, an error shall be logged
9247 * and just return. Otherwise, it will get to the corresponding completion
9248 * queue and process all the entries on the completion queue, rearm the
9249 * completion queue, and then return.
9250 **/
9251static void
9252lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9253 uint32_t fcp_cqidx)
9254{
9255 struct lpfc_queue *cq;
9256 struct lpfc_cqe *cqe;
9257 bool workposted = false;
9258 uint16_t cqid;
9259 int ecount = 0;
9260
James Smartcb5172e2010-03-15 11:25:07 -04009261 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9263 "0366 Not a valid fast-path completion "
9264 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009265 bf_get_le32(lpfc_eqe_major_code, eqe),
9266 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009267 return;
9268 }
9269
9270 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9271 if (unlikely(!cq)) {
9272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9273 "0367 Fast-path completion queue does not "
9274 "exist\n");
9275 return;
9276 }
9277
9278 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009279 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009280 if (unlikely(cqid != cq->queue_id)) {
9281 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9282 "0368 Miss-matched fast-path completion "
9283 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9284 cqid, cq->queue_id);
9285 return;
9286 }
9287
9288 /* Process all the entries to the CQ */
9289 while ((cqe = lpfc_sli4_cq_get(cq))) {
9290 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9291 if (!(++ecount % LPFC_GET_QE_REL_INT))
9292 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9293 }
9294
9295 /* Catch the no cq entry condition */
9296 if (unlikely(ecount == 0))
9297 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9298 "0369 No entry from fast-path completion "
9299 "queue fcpcqid=%d\n", cq->queue_id);
9300
9301 /* In any case, flash and re-arm the CQ */
9302 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9303
9304 /* wake up worker thread if there are works to be done */
9305 if (workposted)
9306 lpfc_worker_wake_up(phba);
9307}
9308
9309static void
9310lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9311{
9312 struct lpfc_eqe *eqe;
9313
9314 /* walk all the EQ entries and drop on the floor */
9315 while ((eqe = lpfc_sli4_eq_get(eq)))
9316 ;
9317
9318 /* Clear and re-arm the EQ */
9319 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9320}
9321
9322/**
9323 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9324 * @irq: Interrupt number.
9325 * @dev_id: The device context pointer.
9326 *
9327 * This function is directly called from the PCI layer as an interrupt
9328 * service routine when device with SLI-4 interface spec is enabled with
9329 * MSI-X multi-message interrupt mode and there are slow-path events in
9330 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9331 * interrupt mode, this function is called as part of the device-level
9332 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9333 * undergoing initialization, the interrupt handler will not process the
9334 * interrupt. The link attention and ELS ring attention events are handled
9335 * by the worker thread. The interrupt handler signals the worker thread
9336 * and returns for these events. This function is called without any lock
9337 * held. It gets the hbalock to access and update SLI data structures.
9338 *
9339 * This function returns IRQ_HANDLED when interrupt is handled else it
9340 * returns IRQ_NONE.
9341 **/
9342irqreturn_t
9343lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9344{
9345 struct lpfc_hba *phba;
9346 struct lpfc_queue *speq;
9347 struct lpfc_eqe *eqe;
9348 unsigned long iflag;
9349 int ecount = 0;
9350
9351 /*
9352 * Get the driver's phba structure from the dev_id
9353 */
9354 phba = (struct lpfc_hba *)dev_id;
9355
9356 if (unlikely(!phba))
9357 return IRQ_NONE;
9358
9359 /* Get to the EQ struct associated with this vector */
9360 speq = phba->sli4_hba.sp_eq;
9361
9362 /* Check device state for handling interrupt */
9363 if (unlikely(lpfc_intr_state_check(phba))) {
9364 /* Check again for link_state with lock held */
9365 spin_lock_irqsave(&phba->hbalock, iflag);
9366 if (phba->link_state < LPFC_LINK_DOWN)
9367 /* Flush, clear interrupt, and rearm the EQ */
9368 lpfc_sli4_eq_flush(phba, speq);
9369 spin_unlock_irqrestore(&phba->hbalock, iflag);
9370 return IRQ_NONE;
9371 }
9372
9373 /*
9374 * Process all the event on FCP slow-path EQ
9375 */
9376 while ((eqe = lpfc_sli4_eq_get(speq))) {
9377 lpfc_sli4_sp_handle_eqe(phba, eqe);
9378 if (!(++ecount % LPFC_GET_QE_REL_INT))
9379 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9380 }
9381
9382 /* Always clear and re-arm the slow-path EQ */
9383 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9384
9385 /* Catch the no cq entry condition */
9386 if (unlikely(ecount == 0)) {
9387 if (phba->intr_type == MSIX)
9388 /* MSI-X treated interrupt served as no EQ share INT */
9389 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9390 "0357 MSI-X interrupt with no EQE\n");
9391 else
9392 /* Non MSI-X treated on interrupt as EQ share INT */
9393 return IRQ_NONE;
9394 }
9395
9396 return IRQ_HANDLED;
9397} /* lpfc_sli4_sp_intr_handler */
9398
9399/**
9400 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9401 * @irq: Interrupt number.
9402 * @dev_id: The device context pointer.
9403 *
9404 * This function is directly called from the PCI layer as an interrupt
9405 * service routine when device with SLI-4 interface spec is enabled with
9406 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9407 * ring event in the HBA. However, when the device is enabled with either
9408 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9409 * device-level interrupt handler. When the PCI slot is in error recovery
9410 * or the HBA is undergoing initialization, the interrupt handler will not
9411 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9412 * the intrrupt context. This function is called without any lock held.
9413 * It gets the hbalock to access and update SLI data structures. Note that,
9414 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9415 * equal to that of FCP CQ index.
9416 *
9417 * This function returns IRQ_HANDLED when interrupt is handled else it
9418 * returns IRQ_NONE.
9419 **/
9420irqreturn_t
9421lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9422{
9423 struct lpfc_hba *phba;
9424 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9425 struct lpfc_queue *fpeq;
9426 struct lpfc_eqe *eqe;
9427 unsigned long iflag;
9428 int ecount = 0;
9429 uint32_t fcp_eqidx;
9430
9431 /* Get the driver's phba structure from the dev_id */
9432 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9433 phba = fcp_eq_hdl->phba;
9434 fcp_eqidx = fcp_eq_hdl->idx;
9435
9436 if (unlikely(!phba))
9437 return IRQ_NONE;
9438
9439 /* Get to the EQ struct associated with this vector */
9440 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9441
9442 /* Check device state for handling interrupt */
9443 if (unlikely(lpfc_intr_state_check(phba))) {
9444 /* Check again for link_state with lock held */
9445 spin_lock_irqsave(&phba->hbalock, iflag);
9446 if (phba->link_state < LPFC_LINK_DOWN)
9447 /* Flush, clear interrupt, and rearm the EQ */
9448 lpfc_sli4_eq_flush(phba, fpeq);
9449 spin_unlock_irqrestore(&phba->hbalock, iflag);
9450 return IRQ_NONE;
9451 }
9452
9453 /*
9454 * Process all the event on FCP fast-path EQ
9455 */
9456 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9457 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9458 if (!(++ecount % LPFC_GET_QE_REL_INT))
9459 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9460 }
9461
9462 /* Always clear and re-arm the fast-path EQ */
9463 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9464
9465 if (unlikely(ecount == 0)) {
9466 if (phba->intr_type == MSIX)
9467 /* MSI-X treated interrupt served as no EQ share INT */
9468 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9469 "0358 MSI-X interrupt with no EQE\n");
9470 else
9471 /* Non MSI-X treated on interrupt as EQ share INT */
9472 return IRQ_NONE;
9473 }
9474
9475 return IRQ_HANDLED;
9476} /* lpfc_sli4_fp_intr_handler */
9477
9478/**
9479 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9480 * @irq: Interrupt number.
9481 * @dev_id: The device context pointer.
9482 *
9483 * This function is the device-level interrupt handler to device with SLI-4
9484 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9485 * interrupt mode is enabled and there is an event in the HBA which requires
9486 * driver attention. This function invokes the slow-path interrupt attention
9487 * handling function and fast-path interrupt attention handling function in
9488 * turn to process the relevant HBA attention events. This function is called
9489 * without any lock held. It gets the hbalock to access and update SLI data
9490 * structures.
9491 *
9492 * This function returns IRQ_HANDLED when interrupt is handled, else it
9493 * returns IRQ_NONE.
9494 **/
9495irqreturn_t
9496lpfc_sli4_intr_handler(int irq, void *dev_id)
9497{
9498 struct lpfc_hba *phba;
9499 irqreturn_t sp_irq_rc, fp_irq_rc;
9500 bool fp_handled = false;
9501 uint32_t fcp_eqidx;
9502
9503 /* Get the driver's phba structure from the dev_id */
9504 phba = (struct lpfc_hba *)dev_id;
9505
9506 if (unlikely(!phba))
9507 return IRQ_NONE;
9508
9509 /*
9510 * Invokes slow-path host attention interrupt handling as appropriate.
9511 */
9512 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9513
9514 /*
9515 * Invoke fast-path host attention interrupt handling as appropriate.
9516 */
9517 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9518 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9519 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9520 if (fp_irq_rc == IRQ_HANDLED)
9521 fp_handled |= true;
9522 }
9523
9524 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9525} /* lpfc_sli4_intr_handler */
9526
9527/**
9528 * lpfc_sli4_queue_free - free a queue structure and associated memory
9529 * @queue: The queue structure to free.
9530 *
9531 * This function frees a queue structure and the DMAable memeory used for
9532 * the host resident queue. This function must be called after destroying the
9533 * queue on the HBA.
9534 **/
9535void
9536lpfc_sli4_queue_free(struct lpfc_queue *queue)
9537{
9538 struct lpfc_dmabuf *dmabuf;
9539
9540 if (!queue)
9541 return;
9542
9543 while (!list_empty(&queue->page_list)) {
9544 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9545 list);
James Smart49198b32010-04-06 15:04:33 -04009546 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009547 dmabuf->virt, dmabuf->phys);
9548 kfree(dmabuf);
9549 }
9550 kfree(queue);
9551 return;
9552}
9553
9554/**
9555 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9556 * @phba: The HBA that this queue is being created on.
9557 * @entry_size: The size of each queue entry for this queue.
9558 * @entry count: The number of entries that this queue will handle.
9559 *
9560 * This function allocates a queue structure and the DMAable memory used for
9561 * the host resident queue. This function must be called before creating the
9562 * queue on the HBA.
9563 **/
9564struct lpfc_queue *
9565lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9566 uint32_t entry_count)
9567{
9568 struct lpfc_queue *queue;
9569 struct lpfc_dmabuf *dmabuf;
9570 int x, total_qe_count;
9571 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009572 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009573
James Smartcb5172e2010-03-15 11:25:07 -04009574 if (!phba->sli4_hba.pc_sli4_params.supported)
9575 hw_page_size = SLI4_PAGE_SIZE;
9576
James Smart4f774512009-05-22 14:52:35 -04009577 queue = kzalloc(sizeof(struct lpfc_queue) +
9578 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9579 if (!queue)
9580 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009581 queue->page_count = (ALIGN(entry_size * entry_count,
9582 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009583 INIT_LIST_HEAD(&queue->list);
9584 INIT_LIST_HEAD(&queue->page_list);
9585 INIT_LIST_HEAD(&queue->child_list);
9586 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9587 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9588 if (!dmabuf)
9589 goto out_fail;
9590 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009591 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009592 GFP_KERNEL);
9593 if (!dmabuf->virt) {
9594 kfree(dmabuf);
9595 goto out_fail;
9596 }
James Smartcb5172e2010-03-15 11:25:07 -04009597 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009598 dmabuf->buffer_tag = x;
9599 list_add_tail(&dmabuf->list, &queue->page_list);
9600 /* initialize queue's entry array */
9601 dma_pointer = dmabuf->virt;
9602 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009603 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009604 total_qe_count++, dma_pointer += entry_size) {
9605 queue->qe[total_qe_count].address = dma_pointer;
9606 }
9607 }
9608 queue->entry_size = entry_size;
9609 queue->entry_count = entry_count;
9610 queue->phba = phba;
9611
9612 return queue;
9613out_fail:
9614 lpfc_sli4_queue_free(queue);
9615 return NULL;
9616}
9617
9618/**
9619 * lpfc_eq_create - Create an Event Queue on the HBA
9620 * @phba: HBA structure that indicates port to create a queue on.
9621 * @eq: The queue structure to use to create the event queue.
9622 * @imax: The maximum interrupt per second limit.
9623 *
9624 * This function creates an event queue, as detailed in @eq, on a port,
9625 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9626 *
9627 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9628 * is used to get the entry count and entry size that are necessary to
9629 * determine the number of pages to allocate and use for this queue. This
9630 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9631 * event queue. This function is asynchronous and will wait for the mailbox
9632 * command to finish before continuing.
9633 *
9634 * On success this function will return a zero. If unable to allocate enough
9635 * memory this function will return ENOMEM. If the queue create mailbox command
9636 * fails this function will return ENXIO.
9637 **/
9638uint32_t
9639lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9640{
9641 struct lpfc_mbx_eq_create *eq_create;
9642 LPFC_MBOXQ_t *mbox;
9643 int rc, length, status = 0;
9644 struct lpfc_dmabuf *dmabuf;
9645 uint32_t shdr_status, shdr_add_status;
9646 union lpfc_sli4_cfg_shdr *shdr;
9647 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009648 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9649
9650 if (!phba->sli4_hba.pc_sli4_params.supported)
9651 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009652
9653 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9654 if (!mbox)
9655 return -ENOMEM;
9656 length = (sizeof(struct lpfc_mbx_eq_create) -
9657 sizeof(struct lpfc_sli4_cfg_mhdr));
9658 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9659 LPFC_MBOX_OPCODE_EQ_CREATE,
9660 length, LPFC_SLI4_MBX_EMBED);
9661 eq_create = &mbox->u.mqe.un.eq_create;
9662 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9663 eq->page_count);
9664 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9665 LPFC_EQE_SIZE);
9666 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9667 /* Calculate delay multiper from maximum interrupt per second */
9668 dmult = LPFC_DMULT_CONST/imax - 1;
9669 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9670 dmult);
9671 switch (eq->entry_count) {
9672 default:
9673 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9674 "0360 Unsupported EQ count. (%d)\n",
9675 eq->entry_count);
9676 if (eq->entry_count < 256)
9677 return -EINVAL;
9678 /* otherwise default to smallest count (drop through) */
9679 case 256:
9680 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9681 LPFC_EQ_CNT_256);
9682 break;
9683 case 512:
9684 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9685 LPFC_EQ_CNT_512);
9686 break;
9687 case 1024:
9688 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9689 LPFC_EQ_CNT_1024);
9690 break;
9691 case 2048:
9692 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9693 LPFC_EQ_CNT_2048);
9694 break;
9695 case 4096:
9696 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9697 LPFC_EQ_CNT_4096);
9698 break;
9699 }
9700 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009701 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009702 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9703 putPaddrLow(dmabuf->phys);
9704 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9705 putPaddrHigh(dmabuf->phys);
9706 }
9707 mbox->vport = phba->pport;
9708 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9709 mbox->context1 = NULL;
9710 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9711 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9712 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9713 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9714 if (shdr_status || shdr_add_status || rc) {
9715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9716 "2500 EQ_CREATE mailbox failed with "
9717 "status x%x add_status x%x, mbx status x%x\n",
9718 shdr_status, shdr_add_status, rc);
9719 status = -ENXIO;
9720 }
9721 eq->type = LPFC_EQ;
9722 eq->subtype = LPFC_NONE;
9723 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9724 if (eq->queue_id == 0xFFFF)
9725 status = -ENXIO;
9726 eq->host_index = 0;
9727 eq->hba_index = 0;
9728
James Smart8fa38512009-07-19 10:01:03 -04009729 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009730 return status;
9731}
9732
9733/**
9734 * lpfc_cq_create - Create a Completion Queue on the HBA
9735 * @phba: HBA structure that indicates port to create a queue on.
9736 * @cq: The queue structure to use to create the completion queue.
9737 * @eq: The event queue to bind this completion queue to.
9738 *
9739 * This function creates a completion queue, as detailed in @wq, on a port,
9740 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9741 *
9742 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9743 * is used to get the entry count and entry size that are necessary to
9744 * determine the number of pages to allocate and use for this queue. The @eq
9745 * is used to indicate which event queue to bind this completion queue to. This
9746 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9747 * completion queue. This function is asynchronous and will wait for the mailbox
9748 * command to finish before continuing.
9749 *
9750 * On success this function will return a zero. If unable to allocate enough
9751 * memory this function will return ENOMEM. If the queue create mailbox command
9752 * fails this function will return ENXIO.
9753 **/
9754uint32_t
9755lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9756 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9757{
9758 struct lpfc_mbx_cq_create *cq_create;
9759 struct lpfc_dmabuf *dmabuf;
9760 LPFC_MBOXQ_t *mbox;
9761 int rc, length, status = 0;
9762 uint32_t shdr_status, shdr_add_status;
9763 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009764 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9765
9766 if (!phba->sli4_hba.pc_sli4_params.supported)
9767 hw_page_size = SLI4_PAGE_SIZE;
9768
James Smart4f774512009-05-22 14:52:35 -04009769
9770 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9771 if (!mbox)
9772 return -ENOMEM;
9773 length = (sizeof(struct lpfc_mbx_cq_create) -
9774 sizeof(struct lpfc_sli4_cfg_mhdr));
9775 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9776 LPFC_MBOX_OPCODE_CQ_CREATE,
9777 length, LPFC_SLI4_MBX_EMBED);
9778 cq_create = &mbox->u.mqe.un.cq_create;
9779 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9780 cq->page_count);
9781 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9782 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9783 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9784 switch (cq->entry_count) {
9785 default:
9786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9787 "0361 Unsupported CQ count. (%d)\n",
9788 cq->entry_count);
9789 if (cq->entry_count < 256)
9790 return -EINVAL;
9791 /* otherwise default to smallest count (drop through) */
9792 case 256:
9793 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9794 LPFC_CQ_CNT_256);
9795 break;
9796 case 512:
9797 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9798 LPFC_CQ_CNT_512);
9799 break;
9800 case 1024:
9801 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9802 LPFC_CQ_CNT_1024);
9803 break;
9804 }
9805 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009806 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009807 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9808 putPaddrLow(dmabuf->phys);
9809 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9810 putPaddrHigh(dmabuf->phys);
9811 }
9812 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9813
9814 /* The IOCTL status is embedded in the mailbox subheader. */
9815 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9816 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9817 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9818 if (shdr_status || shdr_add_status || rc) {
9819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9820 "2501 CQ_CREATE mailbox failed with "
9821 "status x%x add_status x%x, mbx status x%x\n",
9822 shdr_status, shdr_add_status, rc);
9823 status = -ENXIO;
9824 goto out;
9825 }
9826 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9827 if (cq->queue_id == 0xFFFF) {
9828 status = -ENXIO;
9829 goto out;
9830 }
9831 /* link the cq onto the parent eq child list */
9832 list_add_tail(&cq->list, &eq->child_list);
9833 /* Set up completion queue's type and subtype */
9834 cq->type = type;
9835 cq->subtype = subtype;
9836 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9837 cq->host_index = 0;
9838 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009839
James Smart8fa38512009-07-19 10:01:03 -04009840out:
9841 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009842 return status;
9843}
9844
9845/**
James Smartb19a0612010-04-06 14:48:51 -04009846 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -04009847 * @phba: HBA structure that indicates port to create a queue on.
9848 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -04009849 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9850 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -04009851 *
James Smartb19a0612010-04-06 14:48:51 -04009852 * This function provides failback (fb) functionality when the
9853 * mq_create_ext fails on older FW generations. It's purpose is identical
9854 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -04009855 *
James Smartb19a0612010-04-06 14:48:51 -04009856 * This routine cannot fail as all attributes were previously accessed and
9857 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -04009858 **/
James Smartb19a0612010-04-06 14:48:51 -04009859static void
9860lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9861 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -04009862{
9863 struct lpfc_mbx_mq_create *mq_create;
9864 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -04009865 int length;
James Smart04c68492009-05-22 14:52:52 -04009866
James Smart04c68492009-05-22 14:52:52 -04009867 length = (sizeof(struct lpfc_mbx_mq_create) -
9868 sizeof(struct lpfc_sli4_cfg_mhdr));
9869 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9870 LPFC_MBOX_OPCODE_MQ_CREATE,
9871 length, LPFC_SLI4_MBX_EMBED);
9872 mq_create = &mbox->u.mqe.un.mq_create;
9873 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -04009874 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -04009875 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -04009876 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -04009877 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9878 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -04009879 case 16:
9880 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9881 LPFC_MQ_CNT_16);
9882 break;
9883 case 32:
9884 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9885 LPFC_MQ_CNT_32);
9886 break;
9887 case 64:
9888 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9889 LPFC_MQ_CNT_64);
9890 break;
9891 case 128:
9892 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9893 LPFC_MQ_CNT_128);
9894 break;
9895 }
9896 list_for_each_entry(dmabuf, &mq->page_list, list) {
9897 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -04009898 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -04009899 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -04009900 putPaddrHigh(dmabuf->phys);
9901 }
9902}
9903
9904/**
9905 * lpfc_mq_create - Create a mailbox Queue on the HBA
9906 * @phba: HBA structure that indicates port to create a queue on.
9907 * @mq: The queue structure to use to create the mailbox queue.
9908 * @cq: The completion queue to associate with this cq.
9909 * @subtype: The queue's subtype.
9910 *
9911 * This function creates a mailbox queue, as detailed in @mq, on a port,
9912 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9913 *
9914 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9915 * is used to get the entry count and entry size that are necessary to
9916 * determine the number of pages to allocate and use for this queue. This
9917 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9918 * mailbox queue. This function is asynchronous and will wait for the mailbox
9919 * command to finish before continuing.
9920 *
9921 * On success this function will return a zero. If unable to allocate enough
9922 * memory this function will return ENOMEM. If the queue create mailbox command
9923 * fails this function will return ENXIO.
9924 **/
9925int32_t
9926lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9927 struct lpfc_queue *cq, uint32_t subtype)
9928{
9929 struct lpfc_mbx_mq_create *mq_create;
9930 struct lpfc_mbx_mq_create_ext *mq_create_ext;
9931 struct lpfc_dmabuf *dmabuf;
9932 LPFC_MBOXQ_t *mbox;
9933 int rc, length, status = 0;
9934 uint32_t shdr_status, shdr_add_status;
9935 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009936 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -04009937
James Smart49198b32010-04-06 15:04:33 -04009938 if (!phba->sli4_hba.pc_sli4_params.supported)
9939 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -04009940
9941 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9942 if (!mbox)
9943 return -ENOMEM;
9944 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
9945 sizeof(struct lpfc_sli4_cfg_mhdr));
9946 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9947 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
9948 length, LPFC_SLI4_MBX_EMBED);
9949
9950 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
9951 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
9952 mq->page_count);
9953 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
9954 1);
9955 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
9956 &mq_create_ext->u.request, 1);
9957 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
9958 &mq_create_ext->u.request, 1);
9959 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
9960 cq->queue_id);
9961 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
9962 switch (mq->entry_count) {
9963 default:
9964 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9965 "0362 Unsupported MQ count. (%d)\n",
9966 mq->entry_count);
9967 if (mq->entry_count < 16)
9968 return -EINVAL;
9969 /* otherwise default to smallest count (drop through) */
9970 case 16:
9971 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9972 LPFC_MQ_CNT_16);
9973 break;
9974 case 32:
9975 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9976 LPFC_MQ_CNT_32);
9977 break;
9978 case 64:
9979 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9980 LPFC_MQ_CNT_64);
9981 break;
9982 case 128:
9983 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9984 LPFC_MQ_CNT_128);
9985 break;
9986 }
9987 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009988 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -04009989 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
9990 putPaddrLow(dmabuf->phys);
9991 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -04009992 putPaddrHigh(dmabuf->phys);
9993 }
9994 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -04009995 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
9996 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9997 &mq_create_ext->u.response);
9998 if (rc != MBX_SUCCESS) {
9999 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10000 "2795 MQ_CREATE_EXT failed with "
10001 "status x%x. Failback to MQ_CREATE.\n",
10002 rc);
10003 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10004 mq_create = &mbox->u.mqe.un.mq_create;
10005 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10006 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10007 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10008 &mq_create->u.response);
10009 }
10010
James Smart04c68492009-05-22 14:52:52 -040010011 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010012 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10013 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10014 if (shdr_status || shdr_add_status || rc) {
10015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10016 "2502 MQ_CREATE mailbox failed with "
10017 "status x%x add_status x%x, mbx status x%x\n",
10018 shdr_status, shdr_add_status, rc);
10019 status = -ENXIO;
10020 goto out;
10021 }
James Smart04c68492009-05-22 14:52:52 -040010022 if (mq->queue_id == 0xFFFF) {
10023 status = -ENXIO;
10024 goto out;
10025 }
10026 mq->type = LPFC_MQ;
10027 mq->subtype = subtype;
10028 mq->host_index = 0;
10029 mq->hba_index = 0;
10030
10031 /* link the mq onto the parent cq child list */
10032 list_add_tail(&mq->list, &cq->child_list);
10033out:
James Smart8fa38512009-07-19 10:01:03 -040010034 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010035 return status;
10036}
10037
10038/**
James Smart4f774512009-05-22 14:52:35 -040010039 * lpfc_wq_create - Create a Work Queue on the HBA
10040 * @phba: HBA structure that indicates port to create a queue on.
10041 * @wq: The queue structure to use to create the work queue.
10042 * @cq: The completion queue to bind this work queue to.
10043 * @subtype: The subtype of the work queue indicating its functionality.
10044 *
10045 * This function creates a work queue, as detailed in @wq, on a port, described
10046 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10047 *
10048 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10049 * is used to get the entry count and entry size that are necessary to
10050 * determine the number of pages to allocate and use for this queue. The @cq
10051 * is used to indicate which completion queue to bind this work queue to. This
10052 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10053 * work queue. This function is asynchronous and will wait for the mailbox
10054 * command to finish before continuing.
10055 *
10056 * On success this function will return a zero. If unable to allocate enough
10057 * memory this function will return ENOMEM. If the queue create mailbox command
10058 * fails this function will return ENXIO.
10059 **/
10060uint32_t
10061lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10062 struct lpfc_queue *cq, uint32_t subtype)
10063{
10064 struct lpfc_mbx_wq_create *wq_create;
10065 struct lpfc_dmabuf *dmabuf;
10066 LPFC_MBOXQ_t *mbox;
10067 int rc, length, status = 0;
10068 uint32_t shdr_status, shdr_add_status;
10069 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010070 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10071
10072 if (!phba->sli4_hba.pc_sli4_params.supported)
10073 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010074
10075 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10076 if (!mbox)
10077 return -ENOMEM;
10078 length = (sizeof(struct lpfc_mbx_wq_create) -
10079 sizeof(struct lpfc_sli4_cfg_mhdr));
10080 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10081 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10082 length, LPFC_SLI4_MBX_EMBED);
10083 wq_create = &mbox->u.mqe.un.wq_create;
10084 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10085 wq->page_count);
10086 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10087 cq->queue_id);
10088 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010089 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010090 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10091 putPaddrLow(dmabuf->phys);
10092 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10093 putPaddrHigh(dmabuf->phys);
10094 }
10095 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10096 /* The IOCTL status is embedded in the mailbox subheader. */
10097 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10098 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10099 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10100 if (shdr_status || shdr_add_status || rc) {
10101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10102 "2503 WQ_CREATE mailbox failed with "
10103 "status x%x add_status x%x, mbx status x%x\n",
10104 shdr_status, shdr_add_status, rc);
10105 status = -ENXIO;
10106 goto out;
10107 }
10108 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10109 if (wq->queue_id == 0xFFFF) {
10110 status = -ENXIO;
10111 goto out;
10112 }
10113 wq->type = LPFC_WQ;
10114 wq->subtype = subtype;
10115 wq->host_index = 0;
10116 wq->hba_index = 0;
10117
10118 /* link the wq onto the parent cq child list */
10119 list_add_tail(&wq->list, &cq->child_list);
10120out:
James Smart8fa38512009-07-19 10:01:03 -040010121 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010122 return status;
10123}
10124
10125/**
10126 * lpfc_rq_create - Create a Receive Queue on the HBA
10127 * @phba: HBA structure that indicates port to create a queue on.
10128 * @hrq: The queue structure to use to create the header receive queue.
10129 * @drq: The queue structure to use to create the data receive queue.
10130 * @cq: The completion queue to bind this work queue to.
10131 *
10132 * This function creates a receive buffer queue pair , as detailed in @hrq and
10133 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10134 * to the HBA.
10135 *
10136 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10137 * struct is used to get the entry count that is necessary to determine the
10138 * number of pages to use for this queue. The @cq is used to indicate which
10139 * completion queue to bind received buffers that are posted to these queues to.
10140 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10141 * receive queue pair. This function is asynchronous and will wait for the
10142 * mailbox command to finish before continuing.
10143 *
10144 * On success this function will return a zero. If unable to allocate enough
10145 * memory this function will return ENOMEM. If the queue create mailbox command
10146 * fails this function will return ENXIO.
10147 **/
10148uint32_t
10149lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10150 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10151{
10152 struct lpfc_mbx_rq_create *rq_create;
10153 struct lpfc_dmabuf *dmabuf;
10154 LPFC_MBOXQ_t *mbox;
10155 int rc, length, status = 0;
10156 uint32_t shdr_status, shdr_add_status;
10157 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010158 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10159
10160 if (!phba->sli4_hba.pc_sli4_params.supported)
10161 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010162
10163 if (hrq->entry_count != drq->entry_count)
10164 return -EINVAL;
10165 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10166 if (!mbox)
10167 return -ENOMEM;
10168 length = (sizeof(struct lpfc_mbx_rq_create) -
10169 sizeof(struct lpfc_sli4_cfg_mhdr));
10170 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10171 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10172 length, LPFC_SLI4_MBX_EMBED);
10173 rq_create = &mbox->u.mqe.un.rq_create;
10174 switch (hrq->entry_count) {
10175 default:
10176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10177 "2535 Unsupported RQ count. (%d)\n",
10178 hrq->entry_count);
10179 if (hrq->entry_count < 512)
10180 return -EINVAL;
10181 /* otherwise default to smallest count (drop through) */
10182 case 512:
10183 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10184 LPFC_RQ_RING_SIZE_512);
10185 break;
10186 case 1024:
10187 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10188 LPFC_RQ_RING_SIZE_1024);
10189 break;
10190 case 2048:
10191 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10192 LPFC_RQ_RING_SIZE_2048);
10193 break;
10194 case 4096:
10195 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10196 LPFC_RQ_RING_SIZE_4096);
10197 break;
10198 }
10199 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10200 cq->queue_id);
10201 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10202 hrq->page_count);
10203 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10204 LPFC_HDR_BUF_SIZE);
10205 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010206 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010207 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10208 putPaddrLow(dmabuf->phys);
10209 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10210 putPaddrHigh(dmabuf->phys);
10211 }
10212 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10213 /* The IOCTL status is embedded in the mailbox subheader. */
10214 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10215 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10216 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10217 if (shdr_status || shdr_add_status || rc) {
10218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10219 "2504 RQ_CREATE mailbox failed with "
10220 "status x%x add_status x%x, mbx status x%x\n",
10221 shdr_status, shdr_add_status, rc);
10222 status = -ENXIO;
10223 goto out;
10224 }
10225 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10226 if (hrq->queue_id == 0xFFFF) {
10227 status = -ENXIO;
10228 goto out;
10229 }
10230 hrq->type = LPFC_HRQ;
10231 hrq->subtype = subtype;
10232 hrq->host_index = 0;
10233 hrq->hba_index = 0;
10234
10235 /* now create the data queue */
10236 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10237 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10238 length, LPFC_SLI4_MBX_EMBED);
10239 switch (drq->entry_count) {
10240 default:
10241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10242 "2536 Unsupported RQ count. (%d)\n",
10243 drq->entry_count);
10244 if (drq->entry_count < 512)
10245 return -EINVAL;
10246 /* otherwise default to smallest count (drop through) */
10247 case 512:
10248 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10249 LPFC_RQ_RING_SIZE_512);
10250 break;
10251 case 1024:
10252 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10253 LPFC_RQ_RING_SIZE_1024);
10254 break;
10255 case 2048:
10256 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10257 LPFC_RQ_RING_SIZE_2048);
10258 break;
10259 case 4096:
10260 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10261 LPFC_RQ_RING_SIZE_4096);
10262 break;
10263 }
10264 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10265 cq->queue_id);
10266 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10267 drq->page_count);
10268 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10269 LPFC_DATA_BUF_SIZE);
10270 list_for_each_entry(dmabuf, &drq->page_list, list) {
10271 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10272 putPaddrLow(dmabuf->phys);
10273 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10274 putPaddrHigh(dmabuf->phys);
10275 }
10276 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10277 /* The IOCTL status is embedded in the mailbox subheader. */
10278 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10279 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10280 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10281 if (shdr_status || shdr_add_status || rc) {
10282 status = -ENXIO;
10283 goto out;
10284 }
10285 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10286 if (drq->queue_id == 0xFFFF) {
10287 status = -ENXIO;
10288 goto out;
10289 }
10290 drq->type = LPFC_DRQ;
10291 drq->subtype = subtype;
10292 drq->host_index = 0;
10293 drq->hba_index = 0;
10294
10295 /* link the header and data RQs onto the parent cq child list */
10296 list_add_tail(&hrq->list, &cq->child_list);
10297 list_add_tail(&drq->list, &cq->child_list);
10298
10299out:
James Smart8fa38512009-07-19 10:01:03 -040010300 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010301 return status;
10302}
10303
10304/**
10305 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10306 * @eq: The queue structure associated with the queue to destroy.
10307 *
10308 * This function destroys a queue, as detailed in @eq by sending an mailbox
10309 * command, specific to the type of queue, to the HBA.
10310 *
10311 * The @eq struct is used to get the queue ID of the queue to destroy.
10312 *
10313 * On success this function will return a zero. If the queue destroy mailbox
10314 * command fails this function will return ENXIO.
10315 **/
10316uint32_t
10317lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10318{
10319 LPFC_MBOXQ_t *mbox;
10320 int rc, length, status = 0;
10321 uint32_t shdr_status, shdr_add_status;
10322 union lpfc_sli4_cfg_shdr *shdr;
10323
10324 if (!eq)
10325 return -ENODEV;
10326 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10327 if (!mbox)
10328 return -ENOMEM;
10329 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10330 sizeof(struct lpfc_sli4_cfg_mhdr));
10331 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10332 LPFC_MBOX_OPCODE_EQ_DESTROY,
10333 length, LPFC_SLI4_MBX_EMBED);
10334 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10335 eq->queue_id);
10336 mbox->vport = eq->phba->pport;
10337 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10338
10339 rc = lpfc_sli_issue_mbox(eq->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.eq_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 "2505 EQ_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
10353 /* Remove eq from any list */
10354 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010355 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010356 return status;
10357}
10358
10359/**
10360 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10361 * @cq: The queue structure associated with the queue to destroy.
10362 *
10363 * This function destroys a queue, as detailed in @cq by sending an mailbox
10364 * command, specific to the type of queue, to the HBA.
10365 *
10366 * The @cq struct is used to get the queue ID of the queue to destroy.
10367 *
10368 * On success this function will return a zero. If the queue destroy mailbox
10369 * command fails this function will return ENXIO.
10370 **/
10371uint32_t
10372lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10373{
10374 LPFC_MBOXQ_t *mbox;
10375 int rc, length, status = 0;
10376 uint32_t shdr_status, shdr_add_status;
10377 union lpfc_sli4_cfg_shdr *shdr;
10378
10379 if (!cq)
10380 return -ENODEV;
10381 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10382 if (!mbox)
10383 return -ENOMEM;
10384 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10385 sizeof(struct lpfc_sli4_cfg_mhdr));
10386 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10387 LPFC_MBOX_OPCODE_CQ_DESTROY,
10388 length, LPFC_SLI4_MBX_EMBED);
10389 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10390 cq->queue_id);
10391 mbox->vport = cq->phba->pport;
10392 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10393 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10394 /* The IOCTL status is embedded in the mailbox subheader. */
10395 shdr = (union lpfc_sli4_cfg_shdr *)
10396 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10397 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10398 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10399 if (shdr_status || shdr_add_status || rc) {
10400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10401 "2506 CQ_DESTROY mailbox failed with "
10402 "status x%x add_status x%x, mbx status x%x\n",
10403 shdr_status, shdr_add_status, rc);
10404 status = -ENXIO;
10405 }
10406 /* Remove cq from any list */
10407 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010408 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010409 return status;
10410}
10411
10412/**
James Smart04c68492009-05-22 14:52:52 -040010413 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10414 * @qm: The queue structure associated with the queue to destroy.
10415 *
10416 * This function destroys a queue, as detailed in @mq by sending an mailbox
10417 * command, specific to the type of queue, to the HBA.
10418 *
10419 * The @mq struct is used to get the queue ID of the queue to destroy.
10420 *
10421 * On success this function will return a zero. If the queue destroy mailbox
10422 * command fails this function will return ENXIO.
10423 **/
10424uint32_t
10425lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10426{
10427 LPFC_MBOXQ_t *mbox;
10428 int rc, length, status = 0;
10429 uint32_t shdr_status, shdr_add_status;
10430 union lpfc_sli4_cfg_shdr *shdr;
10431
10432 if (!mq)
10433 return -ENODEV;
10434 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10435 if (!mbox)
10436 return -ENOMEM;
10437 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10438 sizeof(struct lpfc_sli4_cfg_mhdr));
10439 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10440 LPFC_MBOX_OPCODE_MQ_DESTROY,
10441 length, LPFC_SLI4_MBX_EMBED);
10442 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10443 mq->queue_id);
10444 mbox->vport = mq->phba->pport;
10445 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10446 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10447 /* The IOCTL status is embedded in the mailbox subheader. */
10448 shdr = (union lpfc_sli4_cfg_shdr *)
10449 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10450 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10451 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10452 if (shdr_status || shdr_add_status || rc) {
10453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10454 "2507 MQ_DESTROY mailbox failed with "
10455 "status x%x add_status x%x, mbx status x%x\n",
10456 shdr_status, shdr_add_status, rc);
10457 status = -ENXIO;
10458 }
10459 /* Remove mq from any list */
10460 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010461 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010462 return status;
10463}
10464
10465/**
James Smart4f774512009-05-22 14:52:35 -040010466 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10467 * @wq: The queue structure associated with the queue to destroy.
10468 *
10469 * This function destroys a queue, as detailed in @wq by sending an mailbox
10470 * command, specific to the type of queue, to the HBA.
10471 *
10472 * The @wq struct is used to get the queue ID of the queue to destroy.
10473 *
10474 * On success this function will return a zero. If the queue destroy mailbox
10475 * command fails this function will return ENXIO.
10476 **/
10477uint32_t
10478lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10479{
10480 LPFC_MBOXQ_t *mbox;
10481 int rc, length, status = 0;
10482 uint32_t shdr_status, shdr_add_status;
10483 union lpfc_sli4_cfg_shdr *shdr;
10484
10485 if (!wq)
10486 return -ENODEV;
10487 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10488 if (!mbox)
10489 return -ENOMEM;
10490 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10491 sizeof(struct lpfc_sli4_cfg_mhdr));
10492 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10493 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10494 length, LPFC_SLI4_MBX_EMBED);
10495 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10496 wq->queue_id);
10497 mbox->vport = wq->phba->pport;
10498 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10499 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10500 shdr = (union lpfc_sli4_cfg_shdr *)
10501 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10504 if (shdr_status || shdr_add_status || rc) {
10505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10506 "2508 WQ_DESTROY mailbox failed with "
10507 "status x%x add_status x%x, mbx status x%x\n",
10508 shdr_status, shdr_add_status, rc);
10509 status = -ENXIO;
10510 }
10511 /* Remove wq from any list */
10512 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010513 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010514 return status;
10515}
10516
10517/**
10518 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10519 * @rq: The queue structure associated with the queue to destroy.
10520 *
10521 * This function destroys a queue, as detailed in @rq by sending an mailbox
10522 * command, specific to the type of queue, to the HBA.
10523 *
10524 * The @rq struct is used to get the queue ID of the queue to destroy.
10525 *
10526 * On success this function will return a zero. If the queue destroy mailbox
10527 * command fails this function will return ENXIO.
10528 **/
10529uint32_t
10530lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10531 struct lpfc_queue *drq)
10532{
10533 LPFC_MBOXQ_t *mbox;
10534 int rc, length, status = 0;
10535 uint32_t shdr_status, shdr_add_status;
10536 union lpfc_sli4_cfg_shdr *shdr;
10537
10538 if (!hrq || !drq)
10539 return -ENODEV;
10540 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10541 if (!mbox)
10542 return -ENOMEM;
10543 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10544 sizeof(struct mbox_header));
10545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10546 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10547 length, LPFC_SLI4_MBX_EMBED);
10548 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10549 hrq->queue_id);
10550 mbox->vport = hrq->phba->pport;
10551 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10552 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10553 /* The IOCTL status is embedded in the mailbox subheader. */
10554 shdr = (union lpfc_sli4_cfg_shdr *)
10555 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10556 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10557 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10558 if (shdr_status || shdr_add_status || rc) {
10559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10560 "2509 RQ_DESTROY mailbox failed with "
10561 "status x%x add_status x%x, mbx status x%x\n",
10562 shdr_status, shdr_add_status, rc);
10563 if (rc != MBX_TIMEOUT)
10564 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10565 return -ENXIO;
10566 }
10567 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10568 drq->queue_id);
10569 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10570 shdr = (union lpfc_sli4_cfg_shdr *)
10571 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10572 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10573 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10574 if (shdr_status || shdr_add_status || rc) {
10575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10576 "2510 RQ_DESTROY mailbox failed with "
10577 "status x%x add_status x%x, mbx status x%x\n",
10578 shdr_status, shdr_add_status, rc);
10579 status = -ENXIO;
10580 }
10581 list_del_init(&hrq->list);
10582 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010583 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010584 return status;
10585}
10586
10587/**
10588 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10589 * @phba: The virtual port for which this call being executed.
10590 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10591 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10592 * @xritag: the xritag that ties this io to the SGL pages.
10593 *
10594 * This routine will post the sgl pages for the IO that has the xritag
10595 * that is in the iocbq structure. The xritag is assigned during iocbq
10596 * creation and persists for as long as the driver is loaded.
10597 * if the caller has fewer than 256 scatter gather segments to map then
10598 * pdma_phys_addr1 should be 0.
10599 * If the caller needs to map more than 256 scatter gather segment then
10600 * pdma_phys_addr1 should be a valid physical address.
10601 * physical address for SGLs must be 64 byte aligned.
10602 * If you are going to map 2 SGL's then the first one must have 256 entries
10603 * the second sgl can have between 1 and 256 entries.
10604 *
10605 * Return codes:
10606 * 0 - Success
10607 * -ENXIO, -ENOMEM - Failure
10608 **/
10609int
10610lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10611 dma_addr_t pdma_phys_addr0,
10612 dma_addr_t pdma_phys_addr1,
10613 uint16_t xritag)
10614{
10615 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10616 LPFC_MBOXQ_t *mbox;
10617 int rc;
10618 uint32_t shdr_status, shdr_add_status;
10619 union lpfc_sli4_cfg_shdr *shdr;
10620
10621 if (xritag == NO_XRI) {
10622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10623 "0364 Invalid param:\n");
10624 return -EINVAL;
10625 }
10626
10627 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10628 if (!mbox)
10629 return -ENOMEM;
10630
10631 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10632 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10633 sizeof(struct lpfc_mbx_post_sgl_pages) -
10634 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10635
10636 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10637 &mbox->u.mqe.un.post_sgl_pages;
10638 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10639 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10640
10641 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10642 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10643 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10644 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10645
10646 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10647 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10648 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10649 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10650 if (!phba->sli4_hba.intr_enable)
10651 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10652 else
10653 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10654 /* The IOCTL status is embedded in the mailbox subheader. */
10655 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10656 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10657 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10658 if (rc != MBX_TIMEOUT)
10659 mempool_free(mbox, phba->mbox_mem_pool);
10660 if (shdr_status || shdr_add_status || rc) {
10661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10662 "2511 POST_SGL mailbox failed with "
10663 "status x%x add_status x%x, mbx status x%x\n",
10664 shdr_status, shdr_add_status, rc);
10665 rc = -ENXIO;
10666 }
10667 return 0;
10668}
10669/**
10670 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10671 * @phba: The virtual port for which this call being executed.
10672 *
10673 * This routine will remove all of the sgl pages registered with the hba.
10674 *
10675 * Return codes:
10676 * 0 - Success
10677 * -ENXIO, -ENOMEM - Failure
10678 **/
10679int
10680lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10681{
10682 LPFC_MBOXQ_t *mbox;
10683 int rc;
10684 uint32_t shdr_status, shdr_add_status;
10685 union lpfc_sli4_cfg_shdr *shdr;
10686
10687 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10688 if (!mbox)
10689 return -ENOMEM;
10690
10691 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10692 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10693 LPFC_SLI4_MBX_EMBED);
10694 if (!phba->sli4_hba.intr_enable)
10695 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10696 else
10697 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10698 /* The IOCTL status is embedded in the mailbox subheader. */
10699 shdr = (union lpfc_sli4_cfg_shdr *)
10700 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10701 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10702 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10703 if (rc != MBX_TIMEOUT)
10704 mempool_free(mbox, phba->mbox_mem_pool);
10705 if (shdr_status || shdr_add_status || rc) {
10706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10707 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10708 "status x%x add_status x%x, mbx status x%x\n",
10709 shdr_status, shdr_add_status, rc);
10710 rc = -ENXIO;
10711 }
10712 return rc;
10713}
10714
10715/**
10716 * lpfc_sli4_next_xritag - Get an xritag for the io
10717 * @phba: Pointer to HBA context object.
10718 *
10719 * This function gets an xritag for the iocb. If there is no unused xritag
10720 * it will return 0xffff.
10721 * The function returns the allocated xritag if successful, else returns zero.
10722 * Zero is not a valid xritag.
10723 * The caller is not required to hold any lock.
10724 **/
10725uint16_t
10726lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10727{
10728 uint16_t xritag;
10729
10730 spin_lock_irq(&phba->hbalock);
10731 xritag = phba->sli4_hba.next_xri;
10732 if ((xritag != (uint16_t) -1) && xritag <
10733 (phba->sli4_hba.max_cfg_param.max_xri
10734 + phba->sli4_hba.max_cfg_param.xri_base)) {
10735 phba->sli4_hba.next_xri++;
10736 phba->sli4_hba.max_cfg_param.xri_used++;
10737 spin_unlock_irq(&phba->hbalock);
10738 return xritag;
10739 }
10740 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010741 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010742 "2004 Failed to allocate XRI.last XRITAG is %d"
10743 " Max XRI is %d, Used XRI is %d\n",
10744 phba->sli4_hba.next_xri,
10745 phba->sli4_hba.max_cfg_param.max_xri,
10746 phba->sli4_hba.max_cfg_param.xri_used);
10747 return -1;
10748}
10749
10750/**
10751 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10752 * @phba: pointer to lpfc hba data structure.
10753 *
10754 * This routine is invoked to post a block of driver's sgl pages to the
10755 * HBA using non-embedded mailbox command. No Lock is held. This routine
10756 * is only called when the driver is loading and after all IO has been
10757 * stopped.
10758 **/
10759int
10760lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10761{
10762 struct lpfc_sglq *sglq_entry;
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 els_xri_cnt, rc = 0;
10771 uint32_t shdr_status, shdr_add_status;
10772 union lpfc_sli4_cfg_shdr *shdr;
10773
10774 /* The number of sgls to be posted */
10775 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10776
10777 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10778 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010779 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010780 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10781 "2559 Block sgl registration required DMA "
10782 "size (%d) great than a page\n", reqlen);
10783 return -ENOMEM;
10784 }
10785 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10786 if (!mbox) {
10787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10788 "2560 Failed to allocate mbox cmd memory\n");
10789 return -ENOMEM;
10790 }
10791
10792 /* Allocate DMA memory and set up the non-embedded mailbox command */
10793 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10794 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10795 LPFC_SLI4_MBX_NEMBED);
10796
10797 if (alloclen < reqlen) {
10798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10799 "0285 Allocated DMA memory size (%d) is "
10800 "less than the requested DMA memory "
10801 "size (%d)\n", alloclen, reqlen);
10802 lpfc_sli4_mbox_cmd_free(phba, mbox);
10803 return -ENOMEM;
10804 }
James Smart4f774512009-05-22 14:52:35 -040010805 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010806 viraddr = mbox->sge_array->addr[0];
10807
10808 /* Set up the SGL pages in the non-embedded DMA pages */
10809 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10810 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10811
10812 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10813 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10814 /* Set up the sge entry */
10815 sgl_pg_pairs->sgl_pg0_addr_lo =
10816 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10817 sgl_pg_pairs->sgl_pg0_addr_hi =
10818 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10819 sgl_pg_pairs->sgl_pg1_addr_lo =
10820 cpu_to_le32(putPaddrLow(0));
10821 sgl_pg_pairs->sgl_pg1_addr_hi =
10822 cpu_to_le32(putPaddrHigh(0));
10823 /* Keep the first xritag on the list */
10824 if (pg_pairs == 0)
10825 xritag_start = sglq_entry->sli4_xritag;
10826 sgl_pg_pairs++;
10827 }
10828 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010829 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010830 /* Perform endian conversion if necessary */
10831 sgl->word0 = cpu_to_le32(sgl->word0);
10832
10833 if (!phba->sli4_hba.intr_enable)
10834 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10835 else {
10836 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10837 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10838 }
10839 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10840 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10841 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10842 if (rc != MBX_TIMEOUT)
10843 lpfc_sli4_mbox_cmd_free(phba, mbox);
10844 if (shdr_status || shdr_add_status || rc) {
10845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10846 "2513 POST_SGL_BLOCK mailbox command failed "
10847 "status x%x add_status x%x mbx status x%x\n",
10848 shdr_status, shdr_add_status, rc);
10849 rc = -ENXIO;
10850 }
10851 return rc;
10852}
10853
10854/**
10855 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10856 * @phba: pointer to lpfc hba data structure.
10857 * @sblist: pointer to scsi buffer list.
10858 * @count: number of scsi buffers on the list.
10859 *
10860 * This routine is invoked to post a block of @count scsi sgl pages from a
10861 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10862 * No Lock is held.
10863 *
10864 **/
10865int
10866lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10867 int cnt)
10868{
10869 struct lpfc_scsi_buf *psb;
10870 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10871 struct sgl_page_pairs *sgl_pg_pairs;
10872 void *viraddr;
10873 LPFC_MBOXQ_t *mbox;
10874 uint32_t reqlen, alloclen, pg_pairs;
10875 uint32_t mbox_tmo;
10876 uint16_t xritag_start = 0;
10877 int rc = 0;
10878 uint32_t shdr_status, shdr_add_status;
10879 dma_addr_t pdma_phys_bpl1;
10880 union lpfc_sli4_cfg_shdr *shdr;
10881
10882 /* Calculate the requested length of the dma memory */
10883 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10884 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010885 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010886 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10887 "0217 Block sgl registration required DMA "
10888 "size (%d) great than a page\n", reqlen);
10889 return -ENOMEM;
10890 }
10891 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10892 if (!mbox) {
10893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10894 "0283 Failed to allocate mbox cmd memory\n");
10895 return -ENOMEM;
10896 }
10897
10898 /* Allocate DMA memory and set up the non-embedded mailbox command */
10899 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10900 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10901 LPFC_SLI4_MBX_NEMBED);
10902
10903 if (alloclen < reqlen) {
10904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10905 "2561 Allocated DMA memory size (%d) is "
10906 "less than the requested DMA memory "
10907 "size (%d)\n", alloclen, reqlen);
10908 lpfc_sli4_mbox_cmd_free(phba, mbox);
10909 return -ENOMEM;
10910 }
James Smart4f774512009-05-22 14:52:35 -040010911 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010912 viraddr = mbox->sge_array->addr[0];
10913
10914 /* Set up the SGL pages in the non-embedded DMA pages */
10915 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10916 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10917
10918 pg_pairs = 0;
10919 list_for_each_entry(psb, sblist, list) {
10920 /* Set up the sge entry */
10921 sgl_pg_pairs->sgl_pg0_addr_lo =
10922 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10923 sgl_pg_pairs->sgl_pg0_addr_hi =
10924 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10925 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10926 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10927 else
10928 pdma_phys_bpl1 = 0;
10929 sgl_pg_pairs->sgl_pg1_addr_lo =
10930 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10931 sgl_pg_pairs->sgl_pg1_addr_hi =
10932 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10933 /* Keep the first xritag on the list */
10934 if (pg_pairs == 0)
10935 xritag_start = psb->cur_iocbq.sli4_xritag;
10936 sgl_pg_pairs++;
10937 pg_pairs++;
10938 }
10939 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10940 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10941 /* Perform endian conversion if necessary */
10942 sgl->word0 = cpu_to_le32(sgl->word0);
10943
10944 if (!phba->sli4_hba.intr_enable)
10945 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10946 else {
10947 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10948 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10949 }
10950 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10951 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10952 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10953 if (rc != MBX_TIMEOUT)
10954 lpfc_sli4_mbox_cmd_free(phba, mbox);
10955 if (shdr_status || shdr_add_status || rc) {
10956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10957 "2564 POST_SGL_BLOCK mailbox command failed "
10958 "status x%x add_status x%x mbx status x%x\n",
10959 shdr_status, shdr_add_status, rc);
10960 rc = -ENXIO;
10961 }
10962 return rc;
10963}
10964
10965/**
10966 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10967 * @phba: pointer to lpfc_hba struct that the frame was received on
10968 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10969 *
10970 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10971 * valid type of frame that the LPFC driver will handle. This function will
10972 * return a zero if the frame is a valid frame or a non zero value when the
10973 * frame does not pass the check.
10974 **/
10975static int
10976lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10977{
10978 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10979 char *type_names[] = FC_TYPE_NAMES_INIT;
10980 struct fc_vft_header *fc_vft_hdr;
10981
10982 switch (fc_hdr->fh_r_ctl) {
10983 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10984 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10985 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10986 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10987 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10988 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10989 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10990 case FC_RCTL_DD_CMD_STATUS: /* command status */
10991 case FC_RCTL_ELS_REQ: /* extended link services request */
10992 case FC_RCTL_ELS_REP: /* extended link services reply */
10993 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10994 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10995 case FC_RCTL_BA_NOP: /* basic link service NOP */
10996 case FC_RCTL_BA_ABTS: /* basic link service abort */
10997 case FC_RCTL_BA_RMC: /* remove connection */
10998 case FC_RCTL_BA_ACC: /* basic accept */
10999 case FC_RCTL_BA_RJT: /* basic reject */
11000 case FC_RCTL_BA_PRMT:
11001 case FC_RCTL_ACK_1: /* acknowledge_1 */
11002 case FC_RCTL_ACK_0: /* acknowledge_0 */
11003 case FC_RCTL_P_RJT: /* port reject */
11004 case FC_RCTL_F_RJT: /* fabric reject */
11005 case FC_RCTL_P_BSY: /* port busy */
11006 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11007 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11008 case FC_RCTL_LCR: /* link credit reset */
11009 case FC_RCTL_END: /* end */
11010 break;
11011 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11012 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11013 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11014 return lpfc_fc_frame_check(phba, fc_hdr);
11015 default:
11016 goto drop;
11017 }
11018 switch (fc_hdr->fh_type) {
11019 case FC_TYPE_BLS:
11020 case FC_TYPE_ELS:
11021 case FC_TYPE_FCP:
11022 case FC_TYPE_CT:
11023 break;
11024 case FC_TYPE_IP:
11025 case FC_TYPE_ILS:
11026 default:
11027 goto drop;
11028 }
11029 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11030 "2538 Received frame rctl:%s type:%s\n",
11031 rctl_names[fc_hdr->fh_r_ctl],
11032 type_names[fc_hdr->fh_type]);
11033 return 0;
11034drop:
11035 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11036 "2539 Dropped frame rctl:%s type:%s\n",
11037 rctl_names[fc_hdr->fh_r_ctl],
11038 type_names[fc_hdr->fh_type]);
11039 return 1;
11040}
11041
11042/**
11043 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11044 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11045 *
11046 * This function processes the FC header to retrieve the VFI from the VF
11047 * header, if one exists. This function will return the VFI if one exists
11048 * or 0 if no VSAN Header exists.
11049 **/
11050static uint32_t
11051lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11052{
11053 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11054
11055 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11056 return 0;
11057 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11058}
11059
11060/**
11061 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11062 * @phba: Pointer to the HBA structure to search for the vport on
11063 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11064 * @fcfi: The FC Fabric ID that the frame came from
11065 *
11066 * This function searches the @phba for a vport that matches the content of the
11067 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11068 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11069 * returns the matching vport pointer or NULL if unable to match frame to a
11070 * vport.
11071 **/
11072static struct lpfc_vport *
11073lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11074 uint16_t fcfi)
11075{
11076 struct lpfc_vport **vports;
11077 struct lpfc_vport *vport = NULL;
11078 int i;
11079 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11080 fc_hdr->fh_d_id[1] << 8 |
11081 fc_hdr->fh_d_id[2]);
11082
11083 vports = lpfc_create_vport_work_array(phba);
11084 if (vports != NULL)
11085 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11086 if (phba->fcf.fcfi == fcfi &&
11087 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11088 vports[i]->fc_myDID == did) {
11089 vport = vports[i];
11090 break;
11091 }
11092 }
11093 lpfc_destroy_vport_work_array(phba, vports);
11094 return vport;
11095}
11096
11097/**
James Smart45ed1192009-10-02 15:17:02 -040011098 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11099 * @vport: The vport to work on.
11100 *
11101 * This function updates the receive sequence time stamp for this vport. The
11102 * receive sequence time stamp indicates the time that the last frame of the
11103 * the sequence that has been idle for the longest amount of time was received.
11104 * the driver uses this time stamp to indicate if any received sequences have
11105 * timed out.
11106 **/
11107void
11108lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11109{
11110 struct lpfc_dmabuf *h_buf;
11111 struct hbq_dmabuf *dmabuf = NULL;
11112
11113 /* get the oldest sequence on the rcv list */
11114 h_buf = list_get_first(&vport->rcv_buffer_list,
11115 struct lpfc_dmabuf, list);
11116 if (!h_buf)
11117 return;
11118 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11119 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11120}
11121
11122/**
11123 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11124 * @vport: The vport that the received sequences were sent to.
11125 *
11126 * This function cleans up all outstanding received sequences. This is called
11127 * by the driver when a link event or user action invalidates all the received
11128 * sequences.
11129 **/
11130void
11131lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11132{
11133 struct lpfc_dmabuf *h_buf, *hnext;
11134 struct lpfc_dmabuf *d_buf, *dnext;
11135 struct hbq_dmabuf *dmabuf = NULL;
11136
11137 /* start with the oldest sequence on the rcv list */
11138 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11139 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11140 list_del_init(&dmabuf->hbuf.list);
11141 list_for_each_entry_safe(d_buf, dnext,
11142 &dmabuf->dbuf.list, list) {
11143 list_del_init(&d_buf->list);
11144 lpfc_in_buf_free(vport->phba, d_buf);
11145 }
11146 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11147 }
11148}
11149
11150/**
11151 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11152 * @vport: The vport that the received sequences were sent to.
11153 *
11154 * This function determines whether any received sequences have timed out by
11155 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11156 * indicates that there is at least one timed out sequence this routine will
11157 * go through the received sequences one at a time from most inactive to most
11158 * active to determine which ones need to be cleaned up. Once it has determined
11159 * that a sequence needs to be cleaned up it will simply free up the resources
11160 * without sending an abort.
11161 **/
11162void
11163lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11164{
11165 struct lpfc_dmabuf *h_buf, *hnext;
11166 struct lpfc_dmabuf *d_buf, *dnext;
11167 struct hbq_dmabuf *dmabuf = NULL;
11168 unsigned long timeout;
11169 int abort_count = 0;
11170
11171 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11172 vport->rcv_buffer_time_stamp);
11173 if (list_empty(&vport->rcv_buffer_list) ||
11174 time_before(jiffies, timeout))
11175 return;
11176 /* start with the oldest sequence on the rcv list */
11177 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11178 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11179 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11180 dmabuf->time_stamp);
11181 if (time_before(jiffies, timeout))
11182 break;
11183 abort_count++;
11184 list_del_init(&dmabuf->hbuf.list);
11185 list_for_each_entry_safe(d_buf, dnext,
11186 &dmabuf->dbuf.list, list) {
11187 list_del_init(&d_buf->list);
11188 lpfc_in_buf_free(vport->phba, d_buf);
11189 }
11190 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11191 }
11192 if (abort_count)
11193 lpfc_update_rcv_time_stamp(vport);
11194}
11195
11196/**
James Smart4f774512009-05-22 14:52:35 -040011197 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11198 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11199 *
11200 * This function searches through the existing incomplete sequences that have
11201 * been sent to this @vport. If the frame matches one of the incomplete
11202 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11203 * make up that sequence. If no sequence is found that matches this frame then
11204 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11205 * This function returns a pointer to the first dmabuf in the sequence list that
11206 * the frame was linked to.
11207 **/
11208static struct hbq_dmabuf *
11209lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11210{
11211 struct fc_frame_header *new_hdr;
11212 struct fc_frame_header *temp_hdr;
11213 struct lpfc_dmabuf *d_buf;
11214 struct lpfc_dmabuf *h_buf;
11215 struct hbq_dmabuf *seq_dmabuf = NULL;
11216 struct hbq_dmabuf *temp_dmabuf = NULL;
11217
James Smart4d9ab992009-10-02 15:16:39 -040011218 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011219 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011220 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11221 /* Use the hdr_buf to find the sequence that this frame belongs to */
11222 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11223 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11224 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11225 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11226 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11227 continue;
11228 /* found a pending sequence that matches this frame */
11229 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11230 break;
11231 }
11232 if (!seq_dmabuf) {
11233 /*
11234 * This indicates first frame received for this sequence.
11235 * Queue the buffer on the vport's rcv_buffer_list.
11236 */
11237 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011238 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011239 return dmabuf;
11240 }
11241 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011242 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11243 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011244 list_del_init(&seq_dmabuf->hbuf.list);
11245 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11246 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011247 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011248 return dmabuf;
11249 }
James Smart45ed1192009-10-02 15:17:02 -040011250 /* move this sequence to the tail to indicate a young sequence */
11251 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11252 seq_dmabuf->time_stamp = jiffies;
11253 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011254 if (list_empty(&seq_dmabuf->dbuf.list)) {
11255 temp_hdr = dmabuf->hbuf.virt;
11256 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11257 return seq_dmabuf;
11258 }
James Smart4f774512009-05-22 14:52:35 -040011259 /* find the correct place in the sequence to insert this frame */
11260 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11261 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11262 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11263 /*
11264 * If the frame's sequence count is greater than the frame on
11265 * the list then insert the frame right after this frame
11266 */
James Smarteeead812009-12-21 17:01:23 -050011267 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11268 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011269 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11270 return seq_dmabuf;
11271 }
11272 }
11273 return NULL;
11274}
11275
11276/**
James Smart6669f9b2009-10-02 15:16:45 -040011277 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11278 * @vport: pointer to a vitural port
11279 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11280 *
11281 * This function tries to abort from the partially assembed sequence, described
11282 * by the information from basic abbort @dmabuf. It checks to see whether such
11283 * partially assembled sequence held by the driver. If so, it shall free up all
11284 * the frames from the partially assembled sequence.
11285 *
11286 * Return
11287 * true -- if there is matching partially assembled sequence present and all
11288 * the frames freed with the sequence;
11289 * false -- if there is no matching partially assembled sequence present so
11290 * nothing got aborted in the lower layer driver
11291 **/
11292static bool
11293lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11294 struct hbq_dmabuf *dmabuf)
11295{
11296 struct fc_frame_header *new_hdr;
11297 struct fc_frame_header *temp_hdr;
11298 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11299 struct hbq_dmabuf *seq_dmabuf = NULL;
11300
11301 /* Use the hdr_buf to find the sequence that matches this frame */
11302 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11303 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11304 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11305 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11306 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11307 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11308 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11309 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11310 continue;
11311 /* found a pending sequence that matches this frame */
11312 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11313 break;
11314 }
11315
11316 /* Free up all the frames from the partially assembled sequence */
11317 if (seq_dmabuf) {
11318 list_for_each_entry_safe(d_buf, n_buf,
11319 &seq_dmabuf->dbuf.list, list) {
11320 list_del_init(&d_buf->list);
11321 lpfc_in_buf_free(vport->phba, d_buf);
11322 }
11323 return true;
11324 }
11325 return false;
11326}
11327
11328/**
11329 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11330 * @phba: Pointer to HBA context object.
11331 * @cmd_iocbq: pointer to the command iocbq structure.
11332 * @rsp_iocbq: pointer to the response iocbq structure.
11333 *
11334 * This function handles the sequence abort accept iocb command complete
11335 * event. It properly releases the memory allocated to the sequence abort
11336 * accept iocb.
11337 **/
11338static void
11339lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11340 struct lpfc_iocbq *cmd_iocbq,
11341 struct lpfc_iocbq *rsp_iocbq)
11342{
11343 if (cmd_iocbq)
11344 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11345}
11346
11347/**
11348 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11349 * @phba: Pointer to HBA context object.
11350 * @fc_hdr: pointer to a FC frame header.
11351 *
11352 * This function sends a basic accept to a previous unsol sequence abort
11353 * event after aborting the sequence handling.
11354 **/
11355static void
11356lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11357 struct fc_frame_header *fc_hdr)
11358{
11359 struct lpfc_iocbq *ctiocb = NULL;
11360 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011361 uint16_t oxid, rxid;
11362 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011363 IOCB_t *icmd;
11364
11365 if (!lpfc_is_link_up(phba))
11366 return;
11367
11368 sid = sli4_sid_from_fc_hdr(fc_hdr);
11369 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011370 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011371
11372 ndlp = lpfc_findnode_did(phba->pport, sid);
11373 if (!ndlp) {
11374 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11375 "1268 Find ndlp returned NULL for oxid:x%x "
11376 "SID:x%x\n", oxid, sid);
11377 return;
11378 }
11379
11380 /* Allocate buffer for acc iocb */
11381 ctiocb = lpfc_sli_get_iocbq(phba);
11382 if (!ctiocb)
11383 return;
11384
James Smart5ffc2662009-11-18 15:39:44 -050011385 /* Extract the F_CTL field from FC_HDR */
11386 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11387
James Smart6669f9b2009-10-02 15:16:45 -040011388 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011389 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011390 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011391 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11392 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11393 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11394
11395 /* Fill in the rest of iocb fields */
11396 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11397 icmd->ulpBdeCount = 0;
11398 icmd->ulpLe = 1;
11399 icmd->ulpClass = CLASS3;
11400 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011401
James Smart6669f9b2009-10-02 15:16:45 -040011402 ctiocb->iocb_cmpl = NULL;
11403 ctiocb->vport = phba->pport;
11404 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11405
James Smart5ffc2662009-11-18 15:39:44 -050011406 if (fctl & FC_FC_EX_CTX) {
11407 /* ABTS sent by responder to CT exchange, construction
11408 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11409 * field and RX_ID from ABTS for RX_ID field.
11410 */
11411 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11412 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11413 ctiocb->sli4_xritag = oxid;
11414 } else {
11415 /* ABTS sent by initiator to CT exchange, construction
11416 * of BA_ACC will need to allocate a new XRI as for the
11417 * XRI_TAG and RX_ID fields.
11418 */
11419 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11420 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11421 ctiocb->sli4_xritag = NO_XRI;
11422 }
11423 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11424
James Smart6669f9b2009-10-02 15:16:45 -040011425 /* Xmit CT abts accept on exchange <xid> */
11426 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11427 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11428 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11429 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11430}
11431
11432/**
11433 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11434 * @vport: Pointer to the vport on which this sequence was received
11435 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11436 *
11437 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11438 * receive sequence is only partially assembed by the driver, it shall abort
11439 * the partially assembled frames for the sequence. Otherwise, if the
11440 * unsolicited receive sequence has been completely assembled and passed to
11441 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11442 * unsolicited sequence has been aborted. After that, it will issue a basic
11443 * accept to accept the abort.
11444 **/
11445void
11446lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11447 struct hbq_dmabuf *dmabuf)
11448{
11449 struct lpfc_hba *phba = vport->phba;
11450 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011451 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011452 bool abts_par;
11453
James Smart6669f9b2009-10-02 15:16:45 -040011454 /* Make a copy of fc_hdr before the dmabuf being released */
11455 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011456 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011457
James Smart5ffc2662009-11-18 15:39:44 -050011458 if (fctl & FC_FC_EX_CTX) {
11459 /*
11460 * ABTS sent by responder to exchange, just free the buffer
11461 */
James Smart6669f9b2009-10-02 15:16:45 -040011462 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011463 } else {
11464 /*
11465 * ABTS sent by initiator to exchange, need to do cleanup
11466 */
11467 /* Try to abort partially assembled seq */
11468 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11469
11470 /* Send abort to ULP if partially seq abort failed */
11471 if (abts_par == false)
11472 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11473 else
11474 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11475 }
James Smart6669f9b2009-10-02 15:16:45 -040011476 /* Send basic accept (BA_ACC) to the abort requester */
11477 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11478}
11479
11480/**
James Smart4f774512009-05-22 14:52:35 -040011481 * lpfc_seq_complete - Indicates if a sequence is complete
11482 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11483 *
11484 * This function checks the sequence, starting with the frame described by
11485 * @dmabuf, to see if all the frames associated with this sequence are present.
11486 * the frames associated with this sequence are linked to the @dmabuf using the
11487 * dbuf list. This function looks for two major things. 1) That the first frame
11488 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11489 * set. 3) That there are no holes in the sequence count. The function will
11490 * return 1 when the sequence is complete, otherwise it will return 0.
11491 **/
11492static int
11493lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11494{
11495 struct fc_frame_header *hdr;
11496 struct lpfc_dmabuf *d_buf;
11497 struct hbq_dmabuf *seq_dmabuf;
11498 uint32_t fctl;
11499 int seq_count = 0;
11500
11501 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11502 /* make sure first fame of sequence has a sequence count of zero */
11503 if (hdr->fh_seq_cnt != seq_count)
11504 return 0;
11505 fctl = (hdr->fh_f_ctl[0] << 16 |
11506 hdr->fh_f_ctl[1] << 8 |
11507 hdr->fh_f_ctl[2]);
11508 /* If last frame of sequence we can return success. */
11509 if (fctl & FC_FC_END_SEQ)
11510 return 1;
11511 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11512 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11513 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11514 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011515 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011516 return 0;
11517 fctl = (hdr->fh_f_ctl[0] << 16 |
11518 hdr->fh_f_ctl[1] << 8 |
11519 hdr->fh_f_ctl[2]);
11520 /* If last frame of sequence we can return success. */
11521 if (fctl & FC_FC_END_SEQ)
11522 return 1;
11523 }
11524 return 0;
11525}
11526
11527/**
11528 * lpfc_prep_seq - Prep sequence for ULP processing
11529 * @vport: Pointer to the vport on which this sequence was received
11530 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11531 *
11532 * This function takes a sequence, described by a list of frames, and creates
11533 * a list of iocbq structures to describe the sequence. This iocbq list will be
11534 * used to issue to the generic unsolicited sequence handler. This routine
11535 * returns a pointer to the first iocbq in the list. If the function is unable
11536 * to allocate an iocbq then it throw out the received frames that were not
11537 * able to be described and return a pointer to the first iocbq. If unable to
11538 * allocate any iocbqs (including the first) this function will return NULL.
11539 **/
11540static struct lpfc_iocbq *
11541lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11542{
11543 struct lpfc_dmabuf *d_buf, *n_buf;
11544 struct lpfc_iocbq *first_iocbq, *iocbq;
11545 struct fc_frame_header *fc_hdr;
11546 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011547 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011548
11549 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11550 /* remove from receive buffer list */
11551 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011552 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011553 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011554 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011555 /* Get an iocbq struct to fill in. */
11556 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11557 if (first_iocbq) {
11558 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011559 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011560 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11561 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11562 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11563 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11564 vport->vpi + vport->phba->vpi_base;
11565 /* put the first buffer into the first IOCBq */
11566 first_iocbq->context2 = &seq_dmabuf->dbuf;
11567 first_iocbq->context3 = NULL;
11568 first_iocbq->iocb.ulpBdeCount = 1;
11569 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11570 LPFC_DATA_BUF_SIZE;
11571 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011572 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011573 bf_get(lpfc_rcqe_length,
11574 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011575 }
11576 iocbq = first_iocbq;
11577 /*
11578 * Each IOCBq can have two Buffers assigned, so go through the list
11579 * of buffers for this sequence and save two buffers in each IOCBq
11580 */
11581 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11582 if (!iocbq) {
11583 lpfc_in_buf_free(vport->phba, d_buf);
11584 continue;
11585 }
11586 if (!iocbq->context3) {
11587 iocbq->context3 = d_buf;
11588 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011589 pbde = (struct ulp_bde64 *)
11590 &iocbq->iocb.unsli3.sli3Words[4];
11591 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011592 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011593 bf_get(lpfc_rcqe_length,
11594 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011595 } else {
11596 iocbq = lpfc_sli_get_iocbq(vport->phba);
11597 if (!iocbq) {
11598 if (first_iocbq) {
11599 first_iocbq->iocb.ulpStatus =
11600 IOSTAT_FCP_RSP_ERROR;
11601 first_iocbq->iocb.un.ulpWord[4] =
11602 IOERR_NO_RESOURCES;
11603 }
11604 lpfc_in_buf_free(vport->phba, d_buf);
11605 continue;
11606 }
11607 iocbq->context2 = d_buf;
11608 iocbq->context3 = NULL;
11609 iocbq->iocb.ulpBdeCount = 1;
11610 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11611 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011612 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011613 bf_get(lpfc_rcqe_length,
11614 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011615 iocbq->iocb.un.rcvels.remoteID = sid;
11616 list_add_tail(&iocbq->list, &first_iocbq->list);
11617 }
11618 }
11619 return first_iocbq;
11620}
11621
James Smart6669f9b2009-10-02 15:16:45 -040011622static void
11623lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11624 struct hbq_dmabuf *seq_dmabuf)
11625{
11626 struct fc_frame_header *fc_hdr;
11627 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11628 struct lpfc_hba *phba = vport->phba;
11629
11630 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11631 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11632 if (!iocbq) {
11633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11634 "2707 Ring %d handler: Failed to allocate "
11635 "iocb Rctl x%x Type x%x received\n",
11636 LPFC_ELS_RING,
11637 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11638 return;
11639 }
11640 if (!lpfc_complete_unsol_iocb(phba,
11641 &phba->sli.ring[LPFC_ELS_RING],
11642 iocbq, fc_hdr->fh_r_ctl,
11643 fc_hdr->fh_type))
11644 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11645 "2540 Ring %d handler: unexpected Rctl "
11646 "x%x Type x%x received\n",
11647 LPFC_ELS_RING,
11648 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11649
11650 /* Free iocb created in lpfc_prep_seq */
11651 list_for_each_entry_safe(curr_iocb, next_iocb,
11652 &iocbq->list, list) {
11653 list_del_init(&curr_iocb->list);
11654 lpfc_sli_release_iocbq(phba, curr_iocb);
11655 }
11656 lpfc_sli_release_iocbq(phba, iocbq);
11657}
11658
James Smart4f774512009-05-22 14:52:35 -040011659/**
11660 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11661 * @phba: Pointer to HBA context object.
11662 *
11663 * This function is called with no lock held. This function processes all
11664 * the received buffers and gives it to upper layers when a received buffer
11665 * indicates that it is the final frame in the sequence. The interrupt
11666 * service routine processes received buffers at interrupt contexts and adds
11667 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11668 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11669 * appropriate receive function when the final frame in a sequence is received.
11670 **/
James Smart4d9ab992009-10-02 15:16:39 -040011671void
11672lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11673 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011674{
James Smart4d9ab992009-10-02 15:16:39 -040011675 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011676 struct fc_frame_header *fc_hdr;
11677 struct lpfc_vport *vport;
11678 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011679
James Smart4f774512009-05-22 14:52:35 -040011680 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011681 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11682 /* check to see if this a valid type of frame */
11683 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11684 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11685 return;
11686 }
11687 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11688 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011689 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011690 /* throw out the frame */
11691 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11692 return;
11693 }
James Smart6669f9b2009-10-02 15:16:45 -040011694 /* Handle the basic abort sequence (BA_ABTS) event */
11695 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11696 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11697 return;
11698 }
11699
James Smart4d9ab992009-10-02 15:16:39 -040011700 /* Link this frame */
11701 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11702 if (!seq_dmabuf) {
11703 /* unable to add frame to vport - throw it out */
11704 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11705 return;
11706 }
11707 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011708 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011709 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011710
James Smart6669f9b2009-10-02 15:16:45 -040011711 /* Send the complete sequence to the upper layer protocol */
11712 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011713}
James Smart6fb120a2009-05-22 14:52:59 -040011714
11715/**
11716 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11717 * @phba: pointer to lpfc hba data structure.
11718 *
11719 * This routine is invoked to post rpi header templates to the
11720 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011721 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11722 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011723 *
11724 * This routine does not require any locks. It's usage is expected
11725 * to be driver load or reset recovery when the driver is
11726 * sequential.
11727 *
11728 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011729 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011730 * EIO - The mailbox failed to complete successfully.
11731 * When this error occurs, the driver is not guaranteed
11732 * to have any rpi regions posted to the device and
11733 * must either attempt to repost the regions or take a
11734 * fatal error.
11735 **/
11736int
11737lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11738{
11739 struct lpfc_rpi_hdr *rpi_page;
11740 uint32_t rc = 0;
11741
11742 /* Post all rpi memory regions to the port. */
11743 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11744 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11745 if (rc != MBX_SUCCESS) {
11746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11747 "2008 Error %d posting all rpi "
11748 "headers\n", rc);
11749 rc = -EIO;
11750 break;
11751 }
11752 }
11753
11754 return rc;
11755}
11756
11757/**
11758 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11759 * @phba: pointer to lpfc hba data structure.
11760 * @rpi_page: pointer to the rpi memory region.
11761 *
11762 * This routine is invoked to post a single rpi header to the
11763 * HBA consistent with the SLI-4 interface spec. This memory region
11764 * maps up to 64 rpi context regions.
11765 *
11766 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011767 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011768 * ENOMEM - No available memory
11769 * EIO - The mailbox failed to complete successfully.
11770 **/
11771int
11772lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11773{
11774 LPFC_MBOXQ_t *mboxq;
11775 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11776 uint32_t rc = 0;
11777 uint32_t mbox_tmo;
11778 uint32_t shdr_status, shdr_add_status;
11779 union lpfc_sli4_cfg_shdr *shdr;
11780
11781 /* The port is notified of the header region via a mailbox command. */
11782 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11783 if (!mboxq) {
11784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11785 "2001 Unable to allocate memory for issuing "
11786 "SLI_CONFIG_SPECIAL mailbox command\n");
11787 return -ENOMEM;
11788 }
11789
11790 /* Post all rpi memory regions to the port. */
11791 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11792 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11793 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11794 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11795 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11796 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11797 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11798 hdr_tmpl, rpi_page->page_count);
11799 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11800 rpi_page->start_rpi);
11801 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11802 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011803 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011804 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11805 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11806 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11807 if (rc != MBX_TIMEOUT)
11808 mempool_free(mboxq, phba->mbox_mem_pool);
11809 if (shdr_status || shdr_add_status || rc) {
11810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11811 "2514 POST_RPI_HDR mailbox failed with "
11812 "status x%x add_status x%x, mbx status x%x\n",
11813 shdr_status, shdr_add_status, rc);
11814 rc = -ENXIO;
11815 }
11816 return rc;
11817}
11818
11819/**
11820 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11821 * @phba: pointer to lpfc hba data structure.
11822 *
11823 * This routine is invoked to post rpi header templates to the
11824 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011825 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11826 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011827 *
11828 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011829 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011830 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11831 **/
11832int
11833lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11834{
11835 int rpi;
11836 uint16_t max_rpi, rpi_base, rpi_limit;
11837 uint16_t rpi_remaining;
11838 struct lpfc_rpi_hdr *rpi_hdr;
11839
11840 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11841 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11842 rpi_limit = phba->sli4_hba.next_rpi;
11843
11844 /*
11845 * The valid rpi range is not guaranteed to be zero-based. Start
11846 * the search at the rpi_base as reported by the port.
11847 */
11848 spin_lock_irq(&phba->hbalock);
11849 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11850 if (rpi >= rpi_limit || rpi < rpi_base)
11851 rpi = LPFC_RPI_ALLOC_ERROR;
11852 else {
11853 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11854 phba->sli4_hba.max_cfg_param.rpi_used++;
11855 phba->sli4_hba.rpi_count++;
11856 }
11857
11858 /*
11859 * Don't try to allocate more rpi header regions if the device limit
11860 * on available rpis max has been exhausted.
11861 */
11862 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11863 (phba->sli4_hba.rpi_count >= max_rpi)) {
11864 spin_unlock_irq(&phba->hbalock);
11865 return rpi;
11866 }
11867
11868 /*
11869 * If the driver is running low on rpi resources, allocate another
11870 * page now. Note that the next_rpi value is used because
11871 * it represents how many are actually in use whereas max_rpi notes
11872 * how many are supported max by the device.
11873 */
11874 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11875 phba->sli4_hba.rpi_count;
11876 spin_unlock_irq(&phba->hbalock);
11877 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11878 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11879 if (!rpi_hdr) {
11880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11881 "2002 Error Could not grow rpi "
11882 "count\n");
11883 } else {
11884 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11885 }
11886 }
11887
11888 return rpi;
11889}
11890
11891/**
11892 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11893 * @phba: pointer to lpfc hba data structure.
11894 *
11895 * This routine is invoked to release an rpi to the pool of
11896 * available rpis maintained by the driver.
11897 **/
11898void
11899lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11900{
11901 spin_lock_irq(&phba->hbalock);
11902 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11903 phba->sli4_hba.rpi_count--;
11904 phba->sli4_hba.max_cfg_param.rpi_used--;
11905 spin_unlock_irq(&phba->hbalock);
11906}
11907
11908/**
11909 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11910 * @phba: pointer to lpfc hba data structure.
11911 *
11912 * This routine is invoked to remove the memory region that
11913 * provided rpi via a bitmask.
11914 **/
11915void
11916lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11917{
11918 kfree(phba->sli4_hba.rpi_bmask);
11919}
11920
11921/**
11922 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11923 * @phba: pointer to lpfc hba data structure.
11924 *
11925 * This routine is invoked to remove the memory region that
11926 * provided rpi via a bitmask.
11927 **/
11928int
11929lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11930{
11931 LPFC_MBOXQ_t *mboxq;
11932 struct lpfc_hba *phba = ndlp->phba;
11933 int rc;
11934
11935 /* The port is notified of the header region via a mailbox command. */
11936 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11937 if (!mboxq)
11938 return -ENOMEM;
11939
11940 /* Post all rpi memory regions to the port. */
11941 lpfc_resume_rpi(mboxq, ndlp);
11942 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11943 if (rc == MBX_NOT_FINISHED) {
11944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11945 "2010 Resume RPI Mailbox failed "
11946 "status %d, mbxStatus x%x\n", rc,
11947 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11948 mempool_free(mboxq, phba->mbox_mem_pool);
11949 return -EIO;
11950 }
11951 return 0;
11952}
11953
11954/**
11955 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11956 * @phba: pointer to lpfc hba data structure.
11957 * @vpi: vpi value to activate with the port.
11958 *
11959 * This routine is invoked to activate a vpi with the
11960 * port when the host intends to use vports with a
11961 * nonzero vpi.
11962 *
11963 * Returns:
11964 * 0 success
11965 * -Evalue otherwise
11966 **/
11967int
11968lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11969{
11970 LPFC_MBOXQ_t *mboxq;
11971 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011972 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011973 uint32_t mbox_tmo;
11974
11975 if (vpi == 0)
11976 return -EINVAL;
11977 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11978 if (!mboxq)
11979 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011980 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011981 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11982 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011983 if (rc != MBX_SUCCESS) {
11984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11985 "2022 INIT VPI Mailbox failed "
11986 "status %d, mbxStatus x%x\n", rc,
11987 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011988 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011989 }
James Smart6a9c52c2009-10-02 15:16:51 -040011990 if (rc != MBX_TIMEOUT)
11991 mempool_free(mboxq, phba->mbox_mem_pool);
11992
11993 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011994}
11995
11996/**
11997 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11998 * @phba: pointer to lpfc hba data structure.
11999 * @mboxq: Pointer to mailbox object.
12000 *
12001 * This routine is invoked to manually add a single FCF record. The caller
12002 * must pass a completely initialized FCF_Record. This routine takes
12003 * care of the nonembedded mailbox operations.
12004 **/
12005static void
12006lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12007{
12008 void *virt_addr;
12009 union lpfc_sli4_cfg_shdr *shdr;
12010 uint32_t shdr_status, shdr_add_status;
12011
12012 virt_addr = mboxq->sge_array->addr[0];
12013 /* The IOCTL status is embedded in the mailbox subheader. */
12014 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12015 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12016 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12017
12018 if ((shdr_status || shdr_add_status) &&
12019 (shdr_status != STATUS_FCF_IN_USE))
12020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12021 "2558 ADD_FCF_RECORD mailbox failed with "
12022 "status x%x add_status x%x\n",
12023 shdr_status, shdr_add_status);
12024
12025 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12026}
12027
12028/**
12029 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12030 * @phba: pointer to lpfc hba data structure.
12031 * @fcf_record: pointer to the initialized fcf record to add.
12032 *
12033 * This routine is invoked to manually add a single FCF record. The caller
12034 * must pass a completely initialized FCF_Record. This routine takes
12035 * care of the nonembedded mailbox operations.
12036 **/
12037int
12038lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12039{
12040 int rc = 0;
12041 LPFC_MBOXQ_t *mboxq;
12042 uint8_t *bytep;
12043 void *virt_addr;
12044 dma_addr_t phys_addr;
12045 struct lpfc_mbx_sge sge;
12046 uint32_t alloc_len, req_len;
12047 uint32_t fcfindex;
12048
12049 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12050 if (!mboxq) {
12051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12052 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12053 return -ENOMEM;
12054 }
12055
12056 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12057 sizeof(uint32_t);
12058
12059 /* Allocate DMA memory and set up the non-embedded mailbox command */
12060 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12061 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12062 req_len, LPFC_SLI4_MBX_NEMBED);
12063 if (alloc_len < req_len) {
12064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12065 "2523 Allocated DMA memory size (x%x) is "
12066 "less than the requested DMA memory "
12067 "size (x%x)\n", alloc_len, req_len);
12068 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12069 return -ENOMEM;
12070 }
12071
12072 /*
12073 * Get the first SGE entry from the non-embedded DMA memory. This
12074 * routine only uses a single SGE.
12075 */
12076 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12077 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012078 virt_addr = mboxq->sge_array->addr[0];
12079 /*
12080 * Configure the FCF record for FCFI 0. This is the driver's
12081 * hardcoded default and gets used in nonFIP mode.
12082 */
12083 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12084 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12085 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12086
12087 /*
12088 * Copy the fcf_index and the FCF Record Data. The data starts after
12089 * the FCoE header plus word10. The data copy needs to be endian
12090 * correct.
12091 */
12092 bytep += sizeof(uint32_t);
12093 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12094 mboxq->vport = phba->pport;
12095 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12096 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12097 if (rc == MBX_NOT_FINISHED) {
12098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12099 "2515 ADD_FCF_RECORD mailbox failed with "
12100 "status 0x%x\n", rc);
12101 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12102 rc = -EIO;
12103 } else
12104 rc = 0;
12105
12106 return rc;
12107}
12108
12109/**
12110 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12111 * @phba: pointer to lpfc hba data structure.
12112 * @fcf_record: pointer to the fcf record to write the default data.
12113 * @fcf_index: FCF table entry index.
12114 *
12115 * This routine is invoked to build the driver's default FCF record. The
12116 * values used are hardcoded. This routine handles memory initialization.
12117 *
12118 **/
12119void
12120lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12121 struct fcf_record *fcf_record,
12122 uint16_t fcf_index)
12123{
12124 memset(fcf_record, 0, sizeof(struct fcf_record));
12125 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12126 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12127 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12128 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12129 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12130 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12131 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12132 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12133 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12134 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12135 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12136 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12137 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012138 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012139 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12140 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12141 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12142 /* Set the VLAN bit map */
12143 if (phba->valid_vlan) {
12144 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12145 = 1 << (phba->vlan_id % 8);
12146 }
12147}
12148
12149/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012150 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012151 * @phba: pointer to lpfc hba data structure.
12152 * @fcf_index: FCF table entry offset.
12153 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012154 * This routine is invoked to scan the entire FCF table by reading FCF
12155 * record and processing it one at a time starting from the @fcf_index
12156 * for initial FCF discovery or fast FCF failover rediscovery.
12157 *
12158 * Return 0 if the mailbox command is submitted sucessfully, none 0
12159 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012160 **/
12161int
James Smart0c9ab6f2010-02-26 14:15:57 -050012162lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012163{
12164 int rc = 0, error;
12165 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012166
James Smart32b97932009-07-19 10:01:21 -040012167 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012168 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12169 if (!mboxq) {
12170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12171 "2000 Failed to allocate mbox for "
12172 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012173 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012174 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012175 }
James Smartecfd03c2010-02-12 14:41:27 -050012176 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012177 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012178 if (rc) {
12179 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012180 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012181 }
James Smartecfd03c2010-02-12 14:41:27 -050012182 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012183 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012184 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smart6fb120a2009-05-22 14:52:59 -040012185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012186 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012187 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012188 else {
James Smart32b97932009-07-19 10:01:21 -040012189 spin_lock_irq(&phba->hbalock);
12190 phba->hba_flag |= FCF_DISC_INPROGRESS;
12191 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012192 /* Reset FCF round robin index bmask for new scan */
James Smart999d8132010-03-15 11:24:56 -040012193 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012194 memset(phba->fcf.fcf_rr_bmask, 0,
12195 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart999d8132010-03-15 11:24:56 -040012196 phba->fcf.eligible_fcf_cnt = 0;
12197 }
James Smart6fb120a2009-05-22 14:52:59 -040012198 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012199 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012200fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012201 if (error) {
12202 if (mboxq)
12203 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12204 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12205 spin_lock_irq(&phba->hbalock);
12206 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12207 spin_unlock_irq(&phba->hbalock);
12208 }
James Smart6fb120a2009-05-22 14:52:59 -040012209 return error;
12210}
James Smarta0c87cb2009-07-19 10:01:10 -040012211
12212/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012213 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12214 * @phba: pointer to lpfc hba data structure.
12215 * @fcf_index: FCF table entry offset.
12216 *
12217 * This routine is invoked to read an FCF record indicated by @fcf_index
12218 * and to use it for FLOGI round robin FCF failover.
12219 *
12220 * Return 0 if the mailbox command is submitted sucessfully, none 0
12221 * otherwise.
12222 **/
12223int
12224lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12225{
12226 int rc = 0, error;
12227 LPFC_MBOXQ_t *mboxq;
12228
12229 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12230 if (!mboxq) {
12231 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12232 "2763 Failed to allocate mbox for "
12233 "READ_FCF cmd\n");
12234 error = -ENOMEM;
12235 goto fail_fcf_read;
12236 }
12237 /* Construct the read FCF record mailbox command */
12238 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12239 if (rc) {
12240 error = -EINVAL;
12241 goto fail_fcf_read;
12242 }
12243 /* Issue the mailbox command asynchronously */
12244 mboxq->vport = phba->pport;
12245 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12246 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12247 if (rc == MBX_NOT_FINISHED)
12248 error = -EIO;
12249 else
12250 error = 0;
12251
12252fail_fcf_read:
12253 if (error && mboxq)
12254 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12255 return error;
12256}
12257
12258/**
12259 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12260 * @phba: pointer to lpfc hba data structure.
12261 * @fcf_index: FCF table entry offset.
12262 *
12263 * This routine is invoked to read an FCF record indicated by @fcf_index to
12264 * determine whether it's eligible for FLOGI round robin failover list.
12265 *
12266 * Return 0 if the mailbox command is submitted sucessfully, none 0
12267 * otherwise.
12268 **/
12269int
12270lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12271{
12272 int rc = 0, error;
12273 LPFC_MBOXQ_t *mboxq;
12274
12275 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12276 if (!mboxq) {
12277 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12278 "2758 Failed to allocate mbox for "
12279 "READ_FCF cmd\n");
12280 error = -ENOMEM;
12281 goto fail_fcf_read;
12282 }
12283 /* Construct the read FCF record mailbox command */
12284 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12285 if (rc) {
12286 error = -EINVAL;
12287 goto fail_fcf_read;
12288 }
12289 /* Issue the mailbox command asynchronously */
12290 mboxq->vport = phba->pport;
12291 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12292 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12293 if (rc == MBX_NOT_FINISHED)
12294 error = -EIO;
12295 else
12296 error = 0;
12297
12298fail_fcf_read:
12299 if (error && mboxq)
12300 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12301 return error;
12302}
12303
12304/**
12305 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12306 * @phba: pointer to lpfc hba data structure.
12307 *
12308 * This routine is to get the next eligible FCF record index in a round
12309 * robin fashion. If the next eligible FCF record index equals to the
12310 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12311 * shall be returned, otherwise, the next eligible FCF record's index
12312 * shall be returned.
12313 **/
12314uint16_t
12315lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12316{
12317 uint16_t next_fcf_index;
12318
12319 /* Search from the currently registered FCF index */
12320 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12321 LPFC_SLI4_FCF_TBL_INDX_MAX,
12322 phba->fcf.current_rec.fcf_indx);
12323 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12324 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12325 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12326 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12327 /* Round robin failover stop condition */
12328 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12329 return LPFC_FCOE_FCF_NEXT_NONE;
12330
12331 return next_fcf_index;
12332}
12333
12334/**
12335 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12336 * @phba: pointer to lpfc hba data structure.
12337 *
12338 * This routine sets the FCF record index in to the eligible bmask for
12339 * round robin failover search. It checks to make sure that the index
12340 * does not go beyond the range of the driver allocated bmask dimension
12341 * before setting the bit.
12342 *
12343 * Returns 0 if the index bit successfully set, otherwise, it returns
12344 * -EINVAL.
12345 **/
12346int
12347lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12348{
12349 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12350 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12351 "2610 HBA FCF index reached driver's "
12352 "book keeping dimension: fcf_index:%d, "
12353 "driver_bmask_max:%d\n",
12354 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12355 return -EINVAL;
12356 }
12357 /* Set the eligible FCF record index bmask */
12358 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12359
12360 return 0;
12361}
12362
12363/**
12364 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12365 * @phba: pointer to lpfc hba data structure.
12366 *
12367 * This routine clears the FCF record index from the eligible bmask for
12368 * round robin failover search. It checks to make sure that the index
12369 * does not go beyond the range of the driver allocated bmask dimension
12370 * before clearing the bit.
12371 **/
12372void
12373lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12374{
12375 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12376 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12377 "2762 HBA FCF index goes beyond driver's "
12378 "book keeping dimension: fcf_index:%d, "
12379 "driver_bmask_max:%d\n",
12380 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12381 return;
12382 }
12383 /* Clear the eligible FCF record index bmask */
12384 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12385}
12386
12387/**
James Smartecfd03c2010-02-12 14:41:27 -050012388 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12389 * @phba: pointer to lpfc hba data structure.
12390 *
12391 * This routine is the completion routine for the rediscover FCF table mailbox
12392 * command. If the mailbox command returned failure, it will try to stop the
12393 * FCF rediscover wait timer.
12394 **/
12395void
12396lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12397{
12398 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12399 uint32_t shdr_status, shdr_add_status;
12400
12401 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12402
12403 shdr_status = bf_get(lpfc_mbox_hdr_status,
12404 &redisc_fcf->header.cfg_shdr.response);
12405 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12406 &redisc_fcf->header.cfg_shdr.response);
12407 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012408 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012409 "2746 Requesting for FCF rediscovery failed "
12410 "status x%x add_status x%x\n",
12411 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012412 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012413 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012414 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012415 spin_unlock_irq(&phba->hbalock);
12416 /*
12417 * CVL event triggered FCF rediscover request failed,
12418 * last resort to re-try current registered FCF entry.
12419 */
12420 lpfc_retry_pport_discovery(phba);
12421 } else {
12422 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012423 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012424 spin_unlock_irq(&phba->hbalock);
12425 /*
12426 * DEAD FCF event triggered FCF rediscover request
12427 * failed, last resort to fail over as a link down
12428 * to FCF registration.
12429 */
12430 lpfc_sli4_fcf_dead_failthrough(phba);
12431 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012432 } else {
12433 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12434 "2775 Start FCF rediscovery quiescent period "
12435 "wait timer before scaning FCF table\n");
James Smartecfd03c2010-02-12 14:41:27 -050012436 /*
12437 * Start FCF rediscovery wait timer for pending FCF
12438 * before rescan FCF record table.
12439 */
12440 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012441 }
James Smartecfd03c2010-02-12 14:41:27 -050012442
12443 mempool_free(mbox, phba->mbox_mem_pool);
12444}
12445
12446/**
12447 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12448 * @phba: pointer to lpfc hba data structure.
12449 *
12450 * This routine is invoked to request for rediscovery of the entire FCF table
12451 * by the port.
12452 **/
12453int
12454lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12455{
12456 LPFC_MBOXQ_t *mbox;
12457 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12458 int rc, length;
12459
James Smart0c9ab6f2010-02-26 14:15:57 -050012460 /* Cancel retry delay timers to all vports before FCF rediscover */
12461 lpfc_cancel_all_vport_retry_delay_timer(phba);
12462
James Smartecfd03c2010-02-12 14:41:27 -050012463 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12464 if (!mbox) {
12465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12466 "2745 Failed to allocate mbox for "
12467 "requesting FCF rediscover.\n");
12468 return -ENOMEM;
12469 }
12470
12471 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12472 sizeof(struct lpfc_sli4_cfg_mhdr));
12473 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12474 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12475 length, LPFC_SLI4_MBX_EMBED);
12476
12477 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12478 /* Set count to 0 for invalidating the entire FCF database */
12479 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12480
12481 /* Issue the mailbox command asynchronously */
12482 mbox->vport = phba->pport;
12483 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12484 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12485
12486 if (rc == MBX_NOT_FINISHED) {
12487 mempool_free(mbox, phba->mbox_mem_pool);
12488 return -EIO;
12489 }
12490 return 0;
12491}
12492
12493/**
James Smartfc2b9892010-02-26 14:15:29 -050012494 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12495 * @phba: pointer to lpfc hba data structure.
12496 *
12497 * This function is the failover routine as a last resort to the FCF DEAD
12498 * event when driver failed to perform fast FCF failover.
12499 **/
12500void
12501lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12502{
12503 uint32_t link_state;
12504
12505 /*
12506 * Last resort as FCF DEAD event failover will treat this as
12507 * a link down, but save the link state because we don't want
12508 * it to be changed to Link Down unless it is already down.
12509 */
12510 link_state = phba->link_state;
12511 lpfc_linkdown(phba);
12512 phba->link_state = link_state;
12513
12514 /* Unregister FCF if no devices connected to it */
12515 lpfc_unregister_unused_fcf(phba);
12516}
12517
12518/**
James Smarta0c87cb2009-07-19 10:01:10 -040012519 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12520 * @phba: pointer to lpfc hba data structure.
12521 *
12522 * This function read region 23 and parse TLV for port status to
12523 * decide if the user disaled the port. If the TLV indicates the
12524 * port is disabled, the hba_flag is set accordingly.
12525 **/
12526void
12527lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12528{
12529 LPFC_MBOXQ_t *pmb = NULL;
12530 MAILBOX_t *mb;
12531 uint8_t *rgn23_data = NULL;
12532 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12533 int rc;
12534
12535 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12536 if (!pmb) {
12537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12538 "2600 lpfc_sli_read_serdes_param failed to"
12539 " allocate mailbox memory\n");
12540 goto out;
12541 }
12542 mb = &pmb->u.mb;
12543
12544 /* Get adapter Region 23 data */
12545 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12546 if (!rgn23_data)
12547 goto out;
12548
12549 do {
12550 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12552
12553 if (rc != MBX_SUCCESS) {
12554 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12555 "2601 lpfc_sli_read_link_ste failed to"
12556 " read config region 23 rc 0x%x Status 0x%x\n",
12557 rc, mb->mbxStatus);
12558 mb->un.varDmp.word_cnt = 0;
12559 }
12560 /*
12561 * dump mem may return a zero when finished or we got a
12562 * mailbox error, either way we are done.
12563 */
12564 if (mb->un.varDmp.word_cnt == 0)
12565 break;
12566 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12567 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12568
12569 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12570 rgn23_data + offset,
12571 mb->un.varDmp.word_cnt);
12572 offset += mb->un.varDmp.word_cnt;
12573 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12574
12575 data_size = offset;
12576 offset = 0;
12577
12578 if (!data_size)
12579 goto out;
12580
12581 /* Check the region signature first */
12582 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12584 "2619 Config region 23 has bad signature\n");
12585 goto out;
12586 }
12587 offset += 4;
12588
12589 /* Check the data structure version */
12590 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12592 "2620 Config region 23 has bad version\n");
12593 goto out;
12594 }
12595 offset += 4;
12596
12597 /* Parse TLV entries in the region */
12598 while (offset < data_size) {
12599 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12600 break;
12601 /*
12602 * If the TLV is not driver specific TLV or driver id is
12603 * not linux driver id, skip the record.
12604 */
12605 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12606 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12607 (rgn23_data[offset + 3] != 0)) {
12608 offset += rgn23_data[offset + 1] * 4 + 4;
12609 continue;
12610 }
12611
12612 /* Driver found a driver specific TLV in the config region */
12613 sub_tlv_len = rgn23_data[offset + 1] * 4;
12614 offset += 4;
12615 tlv_offset = 0;
12616
12617 /*
12618 * Search for configured port state sub-TLV.
12619 */
12620 while ((offset < data_size) &&
12621 (tlv_offset < sub_tlv_len)) {
12622 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12623 offset += 4;
12624 tlv_offset += 4;
12625 break;
12626 }
12627 if (rgn23_data[offset] != PORT_STE_TYPE) {
12628 offset += rgn23_data[offset + 1] * 4 + 4;
12629 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12630 continue;
12631 }
12632
12633 /* This HBA contains PORT_STE configured */
12634 if (!rgn23_data[offset + 2])
12635 phba->hba_flag |= LINK_DISABLED;
12636
12637 goto out;
12638 }
12639 }
12640out:
12641 if (pmb)
12642 mempool_free(pmb, phba->mbox_mem_pool);
12643 kfree(rgn23_data);
12644 return;
12645}
James Smart695a8142010-01-26 23:08:03 -050012646
12647/**
12648 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12649 * @vport: pointer to vport data structure.
12650 *
12651 * This function iterate through the mailboxq and clean up all REG_LOGIN
12652 * and REG_VPI mailbox commands associated with the vport. This function
12653 * is called when driver want to restart discovery of the vport due to
12654 * a Clear Virtual Link event.
12655 **/
12656void
12657lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12658{
12659 struct lpfc_hba *phba = vport->phba;
12660 LPFC_MBOXQ_t *mb, *nextmb;
12661 struct lpfc_dmabuf *mp;
12662
12663 spin_lock_irq(&phba->hbalock);
12664 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12665 if (mb->vport != vport)
12666 continue;
12667
12668 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12669 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12670 continue;
12671
12672 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12673 mp = (struct lpfc_dmabuf *) (mb->context1);
12674 if (mp) {
12675 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12676 kfree(mp);
12677 }
12678 }
12679 list_del(&mb->list);
12680 mempool_free(mb, phba->mbox_mem_pool);
12681 }
12682 mb = phba->sli.mbox_active;
12683 if (mb && (mb->vport == vport)) {
12684 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12685 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12686 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12687 }
12688 spin_unlock_irq(&phba->hbalock);
12689}
12690