blob: 9c609546b4efff72a7b38900895679aeb0297d9b [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);
James Smart2a9bf3d2010-06-07 15:24:45 -0400458
459 if (iocbq)
460 phba->iocb_cnt++;
461 if (phba->iocb_cnt > phba->iocb_max)
462 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400463 return iocbq;
464}
465
James Smarte59058c2008-08-24 21:49:00 -0400466/**
James Smartda0436e2009-05-22 14:51:39 -0400467 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468 * @phba: Pointer to HBA context object.
469 * @xritag: XRI value.
470 *
471 * This function clears the sglq pointer from the array of acive
472 * sglq's. The xritag that is passed in is used to index into the
473 * array. Before the xritag can be used it needs to be adjusted
474 * by subtracting the xribase.
475 *
476 * Returns sglq ponter = success, NULL = Failure.
477 **/
478static struct lpfc_sglq *
479__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480{
481 uint16_t adj_xri;
482 struct lpfc_sglq *sglq;
483 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 return NULL;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
James Smart0f65ff62010-02-26 14:14:23 -0500503struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
506 uint16_t adj_xri;
507 struct lpfc_sglq *sglq;
508 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 return NULL;
511 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 return sglq;
513}
514
515/**
516 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
517 * @phba: Pointer to HBA context object.
518 *
519 * This function is called with hbalock held. This function
520 * Gets a new driver sglq object from the sglq list. If the
521 * list is not empty then it is successful, it returns pointer to the newly
522 * allocated sglq object else it returns NULL.
523 **/
524static struct lpfc_sglq *
525__lpfc_sli_get_sglq(struct lpfc_hba *phba)
526{
527 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528 struct lpfc_sglq *sglq = NULL;
529 uint16_t adj_xri;
530 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400531 if (!sglq)
532 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400533 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
James Smart0f65ff62010-02-26 14:14:23 -0500535 sglq->state = SGL_ALLOCATED;
James Smartda0436e2009-05-22 14:51:39 -0400536 return sglq;
537}
538
539/**
James Smart3621a712009-04-06 18:47:14 -0400540 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400541 * @phba: Pointer to HBA context object.
542 *
543 * This function is called with no lock held. This function
544 * allocates a new driver iocb object from the iocb pool. If the
545 * allocation is successful, it returns pointer to the newly
546 * allocated iocb object else it returns NULL.
547 **/
James Smart2e0fef82007-06-17 19:56:36 -0500548struct lpfc_iocbq *
549lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500550{
James Smart2e0fef82007-06-17 19:56:36 -0500551 struct lpfc_iocbq * iocbq = NULL;
552 unsigned long iflags;
553
554 spin_lock_irqsave(&phba->hbalock, iflags);
555 iocbq = __lpfc_sli_get_iocbq(phba);
556 spin_unlock_irqrestore(&phba->hbalock, iflags);
557 return iocbq;
558}
559
James Smarte59058c2008-08-24 21:49:00 -0400560/**
James Smart4f774512009-05-22 14:52:35 -0400561 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562 * @phba: Pointer to HBA context object.
563 * @iocbq: Pointer to driver iocb object.
564 *
565 * This function is called with hbalock held to release driver
566 * iocb object to the iocb pool. The iotag in the iocb object
567 * does not change for each use of the iocb object. This function
568 * clears all other fields of the iocb object when it is freed.
569 * The sqlq structure that holds the xritag and phys and virtual
570 * mappings for the scatter gather list is retrieved from the
571 * active array of sglq. The get of the sglq pointer also clears
572 * the entry in the array. If the status of the IO indiactes that
573 * this IO was aborted then the sglq entry it put on the
574 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575 * IO has good status or fails for any other reason then the sglq
576 * entry is added to the free list (lpfc_sgl_list).
577 **/
578static void
579__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580{
581 struct lpfc_sglq *sglq;
582 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400583 unsigned long iflag = 0;
584 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400585
586 if (iocbq->sli4_xritag == NO_XRI)
587 sglq = NULL;
588 else
589 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500591 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400593 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594 iflag);
595 list_add(&sglq->list,
596 &phba->sli4_hba.lpfc_abts_els_sgl_list);
597 spin_unlock_irqrestore(
598 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500599 } else {
600 sglq->state = SGL_FREED;
James Smart4f774512009-05-22 14:52:35 -0400601 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400602
603 /* Check if TXQ queue needs to be serviced */
604 if (pring->txq_cnt) {
605 spin_lock_irqsave(
606 &phba->pport->work_port_lock, iflag);
607 phba->pport->work_port_events |=
608 WORKER_SERVICE_TXQ;
609 lpfc_worker_wake_up(phba);
610 spin_unlock_irqrestore(
611 &phba->pport->work_port_lock, iflag);
612 }
James Smart0f65ff62010-02-26 14:14:23 -0500613 }
James Smart4f774512009-05-22 14:52:35 -0400614 }
615
616
617 /*
618 * Clean all volatile data fields, preserve iotag and node struct.
619 */
620 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
621 iocbq->sli4_xritag = NO_XRI;
622 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
623}
624
James Smart2a9bf3d2010-06-07 15:24:45 -0400625
James Smart4f774512009-05-22 14:52:35 -0400626/**
James Smart3772a992009-05-22 14:50:54 -0400627 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
628 * @phba: Pointer to HBA context object.
629 * @iocbq: Pointer to driver iocb object.
630 *
631 * This function is called with hbalock held to release driver
632 * iocb object to the iocb pool. The iotag in the iocb object
633 * does not change for each use of the iocb object. This function
634 * clears all other fields of the iocb object when it is freed.
635 **/
636static void
637__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
638{
639 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
640
641 /*
642 * Clean all volatile data fields, preserve iotag and node struct.
643 */
644 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
645 iocbq->sli4_xritag = NO_XRI;
646 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
647}
648
649/**
James Smart3621a712009-04-06 18:47:14 -0400650 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400651 * @phba: Pointer to HBA context object.
652 * @iocbq: Pointer to driver iocb object.
653 *
654 * This function is called with hbalock held to release driver
655 * iocb object to the iocb pool. The iotag in the iocb object
656 * does not change for each use of the iocb object. This function
657 * clears all other fields of the iocb object when it is freed.
658 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100659static void
James Smart2e0fef82007-06-17 19:56:36 -0500660__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
661{
James Smart3772a992009-05-22 14:50:54 -0400662 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400663 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -0500664}
665
James Smarte59058c2008-08-24 21:49:00 -0400666/**
James Smart3621a712009-04-06 18:47:14 -0400667 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400668 * @phba: Pointer to HBA context object.
669 * @iocbq: Pointer to driver iocb object.
670 *
671 * This function is called with no lock held to release the iocb to
672 * iocb pool.
673 **/
James Smart2e0fef82007-06-17 19:56:36 -0500674void
675lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
676{
677 unsigned long iflags;
678
679 /*
680 * Clean all volatile data fields, preserve iotag and node struct.
681 */
682 spin_lock_irqsave(&phba->hbalock, iflags);
683 __lpfc_sli_release_iocbq(phba, iocbq);
684 spin_unlock_irqrestore(&phba->hbalock, iflags);
685}
686
James Smarte59058c2008-08-24 21:49:00 -0400687/**
James Smarta257bf92009-04-06 18:48:10 -0400688 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
689 * @phba: Pointer to HBA context object.
690 * @iocblist: List of IOCBs.
691 * @ulpstatus: ULP status in IOCB command field.
692 * @ulpWord4: ULP word-4 in IOCB command field.
693 *
694 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
695 * on the list by invoking the complete callback function associated with the
696 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
697 * fields.
698 **/
699void
700lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
701 uint32_t ulpstatus, uint32_t ulpWord4)
702{
703 struct lpfc_iocbq *piocb;
704
705 while (!list_empty(iocblist)) {
706 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
707
708 if (!piocb->iocb_cmpl)
709 lpfc_sli_release_iocbq(phba, piocb);
710 else {
711 piocb->iocb.ulpStatus = ulpstatus;
712 piocb->iocb.un.ulpWord[4] = ulpWord4;
713 (piocb->iocb_cmpl) (phba, piocb, piocb);
714 }
715 }
716 return;
717}
718
719/**
James Smart3621a712009-04-06 18:47:14 -0400720 * lpfc_sli_iocb_cmd_type - Get the iocb type
721 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400722 *
723 * This function is called by ring event handler function to get the iocb type.
724 * This function translates the iocb command to an iocb command type used to
725 * decide the final disposition of each completed IOCB.
726 * The function returns
727 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
728 * LPFC_SOL_IOCB if it is a solicited iocb completion
729 * LPFC_ABORT_IOCB if it is an abort iocb
730 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
731 *
732 * The caller is not required to hold any lock.
733 **/
dea31012005-04-17 16:05:31 -0500734static lpfc_iocb_type
735lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
736{
737 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
738
739 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
740 return 0;
741
742 switch (iocb_cmnd) {
743 case CMD_XMIT_SEQUENCE_CR:
744 case CMD_XMIT_SEQUENCE_CX:
745 case CMD_XMIT_BCAST_CN:
746 case CMD_XMIT_BCAST_CX:
747 case CMD_ELS_REQUEST_CR:
748 case CMD_ELS_REQUEST_CX:
749 case CMD_CREATE_XRI_CR:
750 case CMD_CREATE_XRI_CX:
751 case CMD_GET_RPI_CN:
752 case CMD_XMIT_ELS_RSP_CX:
753 case CMD_GET_RPI_CR:
754 case CMD_FCP_IWRITE_CR:
755 case CMD_FCP_IWRITE_CX:
756 case CMD_FCP_IREAD_CR:
757 case CMD_FCP_IREAD_CX:
758 case CMD_FCP_ICMND_CR:
759 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500760 case CMD_FCP_TSEND_CX:
761 case CMD_FCP_TRSP_CX:
762 case CMD_FCP_TRECEIVE_CX:
763 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500764 case CMD_ADAPTER_MSG:
765 case CMD_ADAPTER_DUMP:
766 case CMD_XMIT_SEQUENCE64_CR:
767 case CMD_XMIT_SEQUENCE64_CX:
768 case CMD_XMIT_BCAST64_CN:
769 case CMD_XMIT_BCAST64_CX:
770 case CMD_ELS_REQUEST64_CR:
771 case CMD_ELS_REQUEST64_CX:
772 case CMD_FCP_IWRITE64_CR:
773 case CMD_FCP_IWRITE64_CX:
774 case CMD_FCP_IREAD64_CR:
775 case CMD_FCP_IREAD64_CX:
776 case CMD_FCP_ICMND64_CR:
777 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500778 case CMD_FCP_TSEND64_CX:
779 case CMD_FCP_TRSP64_CX:
780 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500781 case CMD_GEN_REQUEST64_CR:
782 case CMD_GEN_REQUEST64_CX:
783 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400784 case DSSCMD_IWRITE64_CR:
785 case DSSCMD_IWRITE64_CX:
786 case DSSCMD_IREAD64_CR:
787 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500788 type = LPFC_SOL_IOCB;
789 break;
790 case CMD_ABORT_XRI_CN:
791 case CMD_ABORT_XRI_CX:
792 case CMD_CLOSE_XRI_CN:
793 case CMD_CLOSE_XRI_CX:
794 case CMD_XRI_ABORTED_CX:
795 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400796 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500797 type = LPFC_ABORT_IOCB;
798 break;
799 case CMD_RCV_SEQUENCE_CX:
800 case CMD_RCV_ELS_REQ_CX:
801 case CMD_RCV_SEQUENCE64_CX:
802 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400803 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500804 case CMD_IOCB_RCV_SEQ64_CX:
805 case CMD_IOCB_RCV_ELS64_CX:
806 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500807 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500808 type = LPFC_UNSOL_IOCB;
809 break;
James Smart3163f722008-02-08 18:50:25 -0500810 case CMD_IOCB_XMIT_MSEQ64_CR:
811 case CMD_IOCB_XMIT_MSEQ64_CX:
812 case CMD_IOCB_RCV_SEQ_LIST64_CX:
813 case CMD_IOCB_RCV_ELS_LIST64_CX:
814 case CMD_IOCB_CLOSE_EXTENDED_CN:
815 case CMD_IOCB_ABORT_EXTENDED_CN:
816 case CMD_IOCB_RET_HBQE64_CN:
817 case CMD_IOCB_FCP_IBIDIR64_CR:
818 case CMD_IOCB_FCP_IBIDIR64_CX:
819 case CMD_IOCB_FCP_ITASKMGT64_CX:
820 case CMD_IOCB_LOGENTRY_CN:
821 case CMD_IOCB_LOGENTRY_ASYNC_CN:
822 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700823 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500824 type = LPFC_UNKNOWN_IOCB;
825 break;
dea31012005-04-17 16:05:31 -0500826 default:
827 type = LPFC_UNKNOWN_IOCB;
828 break;
829 }
830
831 return type;
832}
833
James Smarte59058c2008-08-24 21:49:00 -0400834/**
James Smart3621a712009-04-06 18:47:14 -0400835 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400836 * @phba: Pointer to HBA context object.
837 *
838 * This function is called from SLI initialization code
839 * to configure every ring of the HBA's SLI interface. The
840 * caller is not required to hold any lock. This function issues
841 * a config_ring mailbox command for each ring.
842 * This function returns zero if successful else returns a negative
843 * error code.
844 **/
dea31012005-04-17 16:05:31 -0500845static int
James Smarted957682007-06-17 19:56:37 -0500846lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500847{
848 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500849 LPFC_MBOXQ_t *pmb;
850 MAILBOX_t *pmbox;
851 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500852
James Smarted957682007-06-17 19:56:37 -0500853 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
854 if (!pmb)
855 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400856 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500857 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500858 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500859 lpfc_config_ring(phba, i, pmb);
860 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
861 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400863 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500864 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
865 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400866 rc, pmbox->mbxCommand,
867 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500868 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500869 ret = -ENXIO;
870 break;
dea31012005-04-17 16:05:31 -0500871 }
872 }
James Smarted957682007-06-17 19:56:37 -0500873 mempool_free(pmb, phba->mbox_mem_pool);
874 return ret;
dea31012005-04-17 16:05:31 -0500875}
876
James Smarte59058c2008-08-24 21:49:00 -0400877/**
James Smart3621a712009-04-06 18:47:14 -0400878 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400879 * @phba: Pointer to HBA context object.
880 * @pring: Pointer to driver SLI ring object.
881 * @piocb: Pointer to the driver iocb object.
882 *
883 * This function is called with hbalock held. The function adds the
884 * new iocb to txcmplq of the given ring. This function always returns
885 * 0. If this function is called for ELS ring, this function checks if
886 * there is a vport associated with the ELS command. This function also
887 * starts els_tmofunc timer if this is an ELS command.
888 **/
dea31012005-04-17 16:05:31 -0500889static int
James Smart2e0fef82007-06-17 19:56:36 -0500890lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
891 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500892{
dea31012005-04-17 16:05:31 -0500893 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400894 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -0500895 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -0400896 if (pring->txcmplq_cnt > pring->txcmplq_max)
897 pring->txcmplq_max = pring->txcmplq_cnt;
898
James Smart92d7f7b2007-06-17 19:56:38 -0500899 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
900 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
901 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
902 if (!piocb->vport)
903 BUG();
904 else
905 mod_timer(&piocb->vport->els_tmofunc,
906 jiffies + HZ * (phba->fc_ratov << 1));
907 }
908
dea31012005-04-17 16:05:31 -0500909
James Smart2e0fef82007-06-17 19:56:36 -0500910 return 0;
dea31012005-04-17 16:05:31 -0500911}
912
James Smarte59058c2008-08-24 21:49:00 -0400913/**
James Smart3621a712009-04-06 18:47:14 -0400914 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400915 * @phba: Pointer to HBA context object.
916 * @pring: Pointer to driver SLI ring object.
917 *
918 * This function is called with hbalock held to get next
919 * iocb in txq of the given ring. If there is any iocb in
920 * the txq, the function returns first iocb in the list after
921 * removing the iocb from the list, else it returns NULL.
922 **/
James Smart2a9bf3d2010-06-07 15:24:45 -0400923struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500924lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500925{
dea31012005-04-17 16:05:31 -0500926 struct lpfc_iocbq *cmd_iocb;
927
James Smart858c9f62007-06-17 19:56:39 -0500928 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
929 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500930 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500931 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500932}
933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
James Smart3621a712009-04-06 18:47:14 -0400935 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400936 * @phba: Pointer to HBA context object.
937 * @pring: Pointer to driver SLI ring object.
938 *
939 * This function is called with hbalock held and the caller must post the
940 * iocb without releasing the lock. If the caller releases the lock,
941 * iocb slot returned by the function is not guaranteed to be available.
942 * The function returns pointer to the next available iocb slot if there
943 * is available slot in the ring, else it returns NULL.
944 * If the get index of the ring is ahead of the put index, the function
945 * will post an error attention event to the worker thread to take the
946 * HBA to offline state.
947 **/
dea31012005-04-17 16:05:31 -0500948static IOCB_t *
949lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
950{
James Smart34b02dc2008-08-24 21:49:55 -0400951 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500952 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500953 if ((pring->next_cmdidx == pring->cmdidx) &&
954 (++pring->next_cmdidx >= max_cmd_idx))
955 pring->next_cmdidx = 0;
956
957 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
958
959 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
960
961 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400963 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200964 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400965 pring->ringno,
dea31012005-04-17 16:05:31 -0500966 pring->local_getidx, max_cmd_idx);
967
James Smart2e0fef82007-06-17 19:56:36 -0500968 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500969 /*
970 * All error attention handlers are posted to
971 * worker thread
972 */
973 phba->work_ha |= HA_ERATT;
974 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500975
James Smart5e9d9b82008-06-14 22:52:53 -0400976 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500977
978 return NULL;
979 }
980
981 if (pring->local_getidx == pring->next_cmdidx)
982 return NULL;
983 }
984
James Smarted957682007-06-17 19:56:37 -0500985 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500986}
987
James Smarte59058c2008-08-24 21:49:00 -0400988/**
James Smart3621a712009-04-06 18:47:14 -0400989 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400990 * @phba: Pointer to HBA context object.
991 * @iocbq: Pointer to driver iocb object.
992 *
993 * This function gets an iotag for the iocb. If there is no unused iotag and
994 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
995 * array and assigns a new iotag.
996 * The function returns the allocated iotag if successful, else returns zero.
997 * Zero is not a valid iotag.
998 * The caller is not required to hold any lock.
999 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001000uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001001lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001002{
James Smart2e0fef82007-06-17 19:56:36 -05001003 struct lpfc_iocbq **new_arr;
1004 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001005 size_t new_len;
1006 struct lpfc_sli *psli = &phba->sli;
1007 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001008
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001010 iotag = psli->last_iotag;
1011 if(++iotag < psli->iocbq_lookup_len) {
1012 psli->last_iotag = iotag;
1013 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001014 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001015 iocbq->iotag = iotag;
1016 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001017 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001018 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1019 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001020 spin_unlock_irq(&phba->hbalock);
1021 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001022 GFP_KERNEL);
1023 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001024 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001025 old_arr = psli->iocbq_lookup;
1026 if (new_len <= psli->iocbq_lookup_len) {
1027 /* highly unprobable case */
1028 kfree(new_arr);
1029 iotag = psli->last_iotag;
1030 if(++iotag < psli->iocbq_lookup_len) {
1031 psli->last_iotag = iotag;
1032 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001033 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001034 iocbq->iotag = iotag;
1035 return iotag;
1036 }
James Smart2e0fef82007-06-17 19:56:36 -05001037 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001038 return 0;
1039 }
1040 if (psli->iocbq_lookup)
1041 memcpy(new_arr, old_arr,
1042 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001043 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001044 psli->iocbq_lookup = new_arr;
1045 psli->iocbq_lookup_len = new_len;
1046 psli->last_iotag = iotag;
1047 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001048 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001049 iocbq->iotag = iotag;
1050 kfree(old_arr);
1051 return iotag;
1052 }
James Smart8f6d98d2006-08-01 07:34:00 -04001053 } else
James Smart2e0fef82007-06-17 19:56:36 -05001054 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001055
James Bottomley604a3e32005-10-29 10:28:33 -05001056 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001057 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1058 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001059
James Bottomley604a3e32005-10-29 10:28:33 -05001060 return 0;
dea31012005-04-17 16:05:31 -05001061}
1062
James Smarte59058c2008-08-24 21:49:00 -04001063/**
James Smart3621a712009-04-06 18:47:14 -04001064 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001065 * @phba: Pointer to HBA context object.
1066 * @pring: Pointer to driver SLI ring object.
1067 * @iocb: Pointer to iocb slot in the ring.
1068 * @nextiocb: Pointer to driver iocb object which need to be
1069 * posted to firmware.
1070 *
1071 * This function is called with hbalock held to post a new iocb to
1072 * the firmware. This function copies the new iocb to ring iocb slot and
1073 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1074 * a completion call back for this iocb else the function will free the
1075 * iocb object.
1076 **/
dea31012005-04-17 16:05:31 -05001077static void
1078lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1079 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1080{
1081 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001082 * Set up an iotag
dea31012005-04-17 16:05:31 -05001083 */
James Bottomley604a3e32005-10-29 10:28:33 -05001084 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001085
James Smarte2a0a9d2008-12-04 22:40:02 -05001086
James Smarta58cbd52007-08-02 11:09:43 -04001087 if (pring->ringno == LPFC_ELS_RING) {
1088 lpfc_debugfs_slow_ring_trc(phba,
1089 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1090 *(((uint32_t *) &nextiocb->iocb) + 4),
1091 *(((uint32_t *) &nextiocb->iocb) + 6),
1092 *(((uint32_t *) &nextiocb->iocb) + 7));
1093 }
1094
dea31012005-04-17 16:05:31 -05001095 /*
1096 * Issue iocb command to adapter
1097 */
James Smart92d7f7b2007-06-17 19:56:38 -05001098 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001099 wmb();
1100 pring->stats.iocb_cmd++;
1101
1102 /*
1103 * If there is no completion routine to call, we can release the
1104 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1105 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1106 */
1107 if (nextiocb->iocb_cmpl)
1108 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001109 else
James Smart2e0fef82007-06-17 19:56:36 -05001110 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001111
1112 /*
1113 * Let the HBA know what IOCB slot will be the next one the
1114 * driver will put a command into.
1115 */
1116 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001117 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001118}
1119
James Smarte59058c2008-08-24 21:49:00 -04001120/**
James Smart3621a712009-04-06 18:47:14 -04001121 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001122 * @phba: Pointer to HBA context object.
1123 * @pring: Pointer to driver SLI ring object.
1124 *
1125 * The caller is not required to hold any lock for calling this function.
1126 * This function updates the chip attention bits for the ring to inform firmware
1127 * that there are pending work to be done for this ring and requests an
1128 * interrupt when there is space available in the ring. This function is
1129 * called when the driver is unable to post more iocbs to the ring due
1130 * to unavailability of space in the ring.
1131 **/
dea31012005-04-17 16:05:31 -05001132static void
James Smart2e0fef82007-06-17 19:56:36 -05001133lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001134{
1135 int ringno = pring->ringno;
1136
1137 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1138
1139 wmb();
1140
1141 /*
1142 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1143 * The HBA will tell us when an IOCB entry is available.
1144 */
1145 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1146 readl(phba->CAregaddr); /* flush */
1147
1148 pring->stats.iocb_cmd_full++;
1149}
1150
James Smarte59058c2008-08-24 21:49:00 -04001151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001153 * @phba: Pointer to HBA context object.
1154 * @pring: Pointer to driver SLI ring object.
1155 *
1156 * This function updates the chip attention register bit for the
1157 * given ring to inform HBA that there is more work to be done
1158 * in this ring. The caller is not required to hold any lock.
1159 **/
dea31012005-04-17 16:05:31 -05001160static void
James Smart2e0fef82007-06-17 19:56:36 -05001161lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001162{
1163 int ringno = pring->ringno;
1164
1165 /*
1166 * Tell the HBA that there is work to do in this ring.
1167 */
James Smart34b02dc2008-08-24 21:49:55 -04001168 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1169 wmb();
1170 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1171 readl(phba->CAregaddr); /* flush */
1172 }
dea31012005-04-17 16:05:31 -05001173}
1174
James Smarte59058c2008-08-24 21:49:00 -04001175/**
James Smart3621a712009-04-06 18:47:14 -04001176 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001177 * @phba: Pointer to HBA context object.
1178 * @pring: Pointer to driver SLI ring object.
1179 *
1180 * This function is called with hbalock held to post pending iocbs
1181 * in the txq to the firmware. This function is called when driver
1182 * detects space available in the ring.
1183 **/
dea31012005-04-17 16:05:31 -05001184static void
James Smart2e0fef82007-06-17 19:56:36 -05001185lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001186{
1187 IOCB_t *iocb;
1188 struct lpfc_iocbq *nextiocb;
1189
1190 /*
1191 * Check to see if:
1192 * (a) there is anything on the txq to send
1193 * (b) link is up
1194 * (c) link attention events can be processed (fcp ring only)
1195 * (d) IOCB processing is not blocked by the outstanding mbox command.
1196 */
1197 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001198 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001199 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001200 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001201
1202 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1203 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1204 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1205
1206 if (iocb)
1207 lpfc_sli_update_ring(phba, pring);
1208 else
1209 lpfc_sli_update_full_ring(phba, pring);
1210 }
1211
1212 return;
1213}
1214
James Smarte59058c2008-08-24 21:49:00 -04001215/**
James Smart3621a712009-04-06 18:47:14 -04001216 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001217 * @phba: Pointer to HBA context object.
1218 * @hbqno: HBQ number.
1219 *
1220 * This function is called with hbalock held to get the next
1221 * available slot for the given HBQ. If there is free slot
1222 * available for the HBQ it will return pointer to the next available
1223 * HBQ entry else it will return NULL.
1224 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001225static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001226lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1227{
1228 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1229
1230 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1231 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1232 hbqp->next_hbqPutIdx = 0;
1233
1234 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001235 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001236 uint32_t getidx = le32_to_cpu(raw_index);
1237
1238 hbqp->local_hbqGetIdx = getidx;
1239
1240 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1241 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001242 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001243 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001244 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001245 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001246 hbqp->entry_count);
1247
1248 phba->link_state = LPFC_HBA_ERROR;
1249 return NULL;
1250 }
1251
1252 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1253 return NULL;
1254 }
1255
James Smart51ef4c22007-08-02 11:10:31 -04001256 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1257 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001258}
1259
James Smarte59058c2008-08-24 21:49:00 -04001260/**
James Smart3621a712009-04-06 18:47:14 -04001261 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001262 * @phba: Pointer to HBA context object.
1263 *
1264 * This function is called with no lock held to free all the
1265 * hbq buffers while uninitializing the SLI interface. It also
1266 * frees the HBQ buffers returned by the firmware but not yet
1267 * processed by the upper layers.
1268 **/
James Smarted957682007-06-17 19:56:37 -05001269void
1270lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1271{
James Smart92d7f7b2007-06-17 19:56:38 -05001272 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1273 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001274 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001275 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001276 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001277
James Smart51ef4c22007-08-02 11:10:31 -04001278 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001279 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001280 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001281 for (i = 0; i < hbq_count; ++i) {
1282 list_for_each_entry_safe(dmabuf, next_dmabuf,
1283 &phba->hbqs[i].hbq_buffer_list, list) {
1284 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1285 list_del(&hbq_buf->dbuf.list);
1286 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1287 }
James Smarta8adb832007-10-27 13:37:53 -04001288 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001289 }
James Smart3163f722008-02-08 18:50:25 -05001290 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001291 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1292 list) {
James Smart3163f722008-02-08 18:50:25 -05001293 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1294 list_del(&hbq_buf->dbuf.list);
1295 if (hbq_buf->tag == -1) {
1296 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1297 (phba, hbq_buf);
1298 } else {
1299 hbqno = hbq_buf->tag >> 16;
1300 if (hbqno >= LPFC_MAX_HBQS)
1301 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1302 (phba, hbq_buf);
1303 else
1304 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1305 hbq_buf);
1306 }
1307 }
1308
1309 /* Mark the HBQs not in use */
1310 phba->hbq_in_use = 0;
1311 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001312}
1313
James Smarte59058c2008-08-24 21:49:00 -04001314/**
James Smart3621a712009-04-06 18:47:14 -04001315 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001316 * @phba: Pointer to HBA context object.
1317 * @hbqno: HBQ number.
1318 * @hbq_buf: Pointer to HBQ buffer.
1319 *
1320 * This function is called with the hbalock held to post a
1321 * hbq buffer to the firmware. If the function finds an empty
1322 * slot in the HBQ, it will post the buffer. The function will return
1323 * pointer to the hbq entry if it successfully post the buffer
1324 * else it will return NULL.
1325 **/
James Smart3772a992009-05-22 14:50:54 -04001326static int
James Smarted957682007-06-17 19:56:37 -05001327lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001328 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001329{
James Smart3772a992009-05-22 14:50:54 -04001330 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1331}
1332
1333/**
1334 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1335 * @phba: Pointer to HBA context object.
1336 * @hbqno: HBQ number.
1337 * @hbq_buf: Pointer to HBQ buffer.
1338 *
1339 * This function is called with the hbalock held to post a hbq buffer to the
1340 * firmware. If the function finds an empty slot in the HBQ, it will post the
1341 * buffer and place it on the hbq_buffer_list. The function will return zero if
1342 * it successfully post the buffer else it will return an error.
1343 **/
1344static int
1345lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1346 struct hbq_dmabuf *hbq_buf)
1347{
James Smarted957682007-06-17 19:56:37 -05001348 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001349 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001350
1351 /* Get next HBQ entry slot to use */
1352 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1353 if (hbqe) {
1354 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1355
James Smart92d7f7b2007-06-17 19:56:38 -05001356 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1357 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001358 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001359 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001360 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1361 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1362 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001363 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1364 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001365 /* flush */
James Smarted957682007-06-17 19:56:37 -05001366 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001367 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001368 return 0;
1369 } else
1370 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001371}
1372
James Smart4f774512009-05-22 14:52:35 -04001373/**
1374 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1375 * @phba: Pointer to HBA context object.
1376 * @hbqno: HBQ number.
1377 * @hbq_buf: Pointer to HBQ buffer.
1378 *
1379 * This function is called with the hbalock held to post an RQE to the SLI4
1380 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1381 * the hbq_buffer_list and return zero, otherwise it will return an error.
1382 **/
1383static int
1384lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1385 struct hbq_dmabuf *hbq_buf)
1386{
1387 int rc;
1388 struct lpfc_rqe hrqe;
1389 struct lpfc_rqe drqe;
1390
1391 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1392 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1393 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1394 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1395 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1396 &hrqe, &drqe);
1397 if (rc < 0)
1398 return rc;
1399 hbq_buf->tag = rc;
1400 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1401 return 0;
1402}
1403
James Smarte59058c2008-08-24 21:49:00 -04001404/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001405static struct lpfc_hbq_init lpfc_els_hbq = {
1406 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001407 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001408 .mask_count = 0,
1409 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001410 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001411 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001412 .init_count = 40,
1413 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001414};
James Smarted957682007-06-17 19:56:37 -05001415
James Smarte59058c2008-08-24 21:49:00 -04001416/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001417static struct lpfc_hbq_init lpfc_extra_hbq = {
1418 .rn = 1,
1419 .entry_count = 200,
1420 .mask_count = 0,
1421 .profile = 0,
1422 .ring_mask = (1 << LPFC_EXTRA_RING),
1423 .buffer_count = 0,
1424 .init_count = 0,
1425 .add_count = 5,
1426};
1427
James Smarte59058c2008-08-24 21:49:00 -04001428/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001429struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001430 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001431 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001432};
1433
James Smarte59058c2008-08-24 21:49:00 -04001434/**
James Smart3621a712009-04-06 18:47:14 -04001435 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001436 * @phba: Pointer to HBA context object.
1437 * @hbqno: HBQ number.
1438 * @count: Number of HBQ buffers to be posted.
1439 *
James Smartd7c255b2008-08-24 21:50:00 -04001440 * This function is called with no lock held to post more hbq buffers to the
1441 * given HBQ. The function returns the number of HBQ buffers successfully
1442 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001443 **/
James Smart311464e2007-08-02 11:10:37 -04001444static int
James Smart92d7f7b2007-06-17 19:56:38 -05001445lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1446{
James Smartd7c255b2008-08-24 21:50:00 -04001447 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001448 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001449 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001450 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001451 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001452 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001453
James Smartd7c255b2008-08-24 21:50:00 -04001454 if ((phba->hbqs[hbqno].buffer_count + count) >
1455 lpfc_hbq_defs[hbqno]->entry_count)
1456 count = lpfc_hbq_defs[hbqno]->entry_count -
1457 phba->hbqs[hbqno].buffer_count;
1458 if (!count)
1459 return 0;
1460 /* Allocate HBQ entries */
1461 for (i = 0; i < count; i++) {
1462 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1463 if (!hbq_buffer)
1464 break;
1465 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1466 }
James Smart3163f722008-02-08 18:50:25 -05001467 /* Check whether HBQ is still in use */
1468 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001469 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001470 goto err;
1471 while (!list_empty(&hbq_buf_list)) {
1472 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1473 dbuf.list);
1474 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1475 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001476 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001477 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001478 posted++;
1479 } else
James Smart51ef4c22007-08-02 11:10:31 -04001480 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001481 }
James Smart3163f722008-02-08 18:50:25 -05001482 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001483 return posted;
1484err:
1485 spin_unlock_irqrestore(&phba->hbalock, flags);
1486 while (!list_empty(&hbq_buf_list)) {
1487 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1488 dbuf.list);
1489 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1490 }
James Smart92d7f7b2007-06-17 19:56:38 -05001491 return 0;
James Smarted957682007-06-17 19:56:37 -05001492}
1493
James Smarte59058c2008-08-24 21:49:00 -04001494/**
James Smart3621a712009-04-06 18:47:14 -04001495 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001496 * @phba: Pointer to HBA context object.
1497 * @qno: HBQ number.
1498 *
1499 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001500 * is called with no lock held. The function returns the number of HBQ entries
1501 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001502 **/
James Smarted957682007-06-17 19:56:37 -05001503int
James Smart92d7f7b2007-06-17 19:56:38 -05001504lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001505{
James Smartdef9c7a2009-12-21 17:02:28 -05001506 if (phba->sli_rev == LPFC_SLI_REV4)
1507 return 0;
1508 else
1509 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1510 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001511}
1512
James Smarte59058c2008-08-24 21:49:00 -04001513/**
James Smart3621a712009-04-06 18:47:14 -04001514 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001515 * @phba: Pointer to HBA context object.
1516 * @qno: HBQ queue number.
1517 *
1518 * This function is called from SLI initialization code path with
1519 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001520 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001521 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001522static int
James Smart92d7f7b2007-06-17 19:56:38 -05001523lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001524{
James Smartdef9c7a2009-12-21 17:02:28 -05001525 if (phba->sli_rev == LPFC_SLI_REV4)
1526 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1527 lpfc_hbq_defs[qno]->entry_count);
1528 else
1529 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1530 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001531}
1532
James Smarte59058c2008-08-24 21:49:00 -04001533/**
James Smart3772a992009-05-22 14:50:54 -04001534 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1535 * @phba: Pointer to HBA context object.
1536 * @hbqno: HBQ number.
1537 *
1538 * This function removes the first hbq buffer on an hbq list and returns a
1539 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1540 **/
1541static struct hbq_dmabuf *
1542lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1543{
1544 struct lpfc_dmabuf *d_buf;
1545
1546 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1547 if (!d_buf)
1548 return NULL;
1549 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1550}
1551
1552/**
James Smart3621a712009-04-06 18:47:14 -04001553 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001554 * @phba: Pointer to HBA context object.
1555 * @tag: Tag of the hbq buffer.
1556 *
1557 * This function is called with hbalock held. This function searches
1558 * for the hbq buffer associated with the given tag in the hbq buffer
1559 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1560 * it returns NULL.
1561 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001562static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001563lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1564{
James Smart92d7f7b2007-06-17 19:56:38 -05001565 struct lpfc_dmabuf *d_buf;
1566 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001567 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001568
James Smart51ef4c22007-08-02 11:10:31 -04001569 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001570 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001571 return NULL;
1572
James Smart3772a992009-05-22 14:50:54 -04001573 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001574 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001575 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001576 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001577 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001578 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001579 }
1580 }
James Smart3772a992009-05-22 14:50:54 -04001581 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001583 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001584 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001585 return NULL;
James Smarted957682007-06-17 19:56:37 -05001586}
1587
James Smarte59058c2008-08-24 21:49:00 -04001588/**
James Smart3621a712009-04-06 18:47:14 -04001589 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001590 * @phba: Pointer to HBA context object.
1591 * @hbq_buffer: Pointer to HBQ buffer.
1592 *
1593 * This function is called with hbalock. This function gives back
1594 * the hbq buffer to firmware. If the HBQ does not have space to
1595 * post the buffer, it will free the buffer.
1596 **/
James Smarted957682007-06-17 19:56:37 -05001597void
James Smart51ef4c22007-08-02 11:10:31 -04001598lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001599{
1600 uint32_t hbqno;
1601
James Smart51ef4c22007-08-02 11:10:31 -04001602 if (hbq_buffer) {
1603 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001604 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001605 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001606 }
1607}
1608
James Smarte59058c2008-08-24 21:49:00 -04001609/**
James Smart3621a712009-04-06 18:47:14 -04001610 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001611 * @mbxCommand: mailbox command code.
1612 *
1613 * This function is called by the mailbox event handler function to verify
1614 * that the completed mailbox command is a legitimate mailbox command. If the
1615 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1616 * and the mailbox event handler will take the HBA offline.
1617 **/
dea31012005-04-17 16:05:31 -05001618static int
1619lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1620{
1621 uint8_t ret;
1622
1623 switch (mbxCommand) {
1624 case MBX_LOAD_SM:
1625 case MBX_READ_NV:
1626 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001627 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001628 case MBX_RUN_BIU_DIAG:
1629 case MBX_INIT_LINK:
1630 case MBX_DOWN_LINK:
1631 case MBX_CONFIG_LINK:
1632 case MBX_CONFIG_RING:
1633 case MBX_RESET_RING:
1634 case MBX_READ_CONFIG:
1635 case MBX_READ_RCONFIG:
1636 case MBX_READ_SPARM:
1637 case MBX_READ_STATUS:
1638 case MBX_READ_RPI:
1639 case MBX_READ_XRI:
1640 case MBX_READ_REV:
1641 case MBX_READ_LNK_STAT:
1642 case MBX_REG_LOGIN:
1643 case MBX_UNREG_LOGIN:
1644 case MBX_READ_LA:
1645 case MBX_CLEAR_LA:
1646 case MBX_DUMP_MEMORY:
1647 case MBX_DUMP_CONTEXT:
1648 case MBX_RUN_DIAGS:
1649 case MBX_RESTART:
1650 case MBX_UPDATE_CFG:
1651 case MBX_DOWN_LOAD:
1652 case MBX_DEL_LD_ENTRY:
1653 case MBX_RUN_PROGRAM:
1654 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001655 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001656 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001657 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001658 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001659 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001660 case MBX_LOAD_AREA:
1661 case MBX_RUN_BIU_DIAG64:
1662 case MBX_CONFIG_PORT:
1663 case MBX_READ_SPARM64:
1664 case MBX_READ_RPI64:
1665 case MBX_REG_LOGIN64:
1666 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001667 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001668 case MBX_SET_DEBUG:
1669 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001670 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001671 case MBX_REG_VPI:
1672 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001673 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001674 case MBX_PORT_CAPABILITIES:
1675 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001676 case MBX_SLI4_CONFIG:
1677 case MBX_SLI4_REQ_FTRS:
1678 case MBX_REG_FCFI:
1679 case MBX_UNREG_FCFI:
1680 case MBX_REG_VFI:
1681 case MBX_UNREG_VFI:
1682 case MBX_INIT_VPI:
1683 case MBX_INIT_VFI:
1684 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04001685 case MBX_READ_EVENT_LOG_STATUS:
1686 case MBX_READ_EVENT_LOG:
dea31012005-04-17 16:05:31 -05001687 ret = mbxCommand;
1688 break;
1689 default:
1690 ret = MBX_SHUTDOWN;
1691 break;
1692 }
James Smart2e0fef82007-06-17 19:56:36 -05001693 return ret;
dea31012005-04-17 16:05:31 -05001694}
James Smarte59058c2008-08-24 21:49:00 -04001695
1696/**
James Smart3621a712009-04-06 18:47:14 -04001697 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001698 * @phba: Pointer to HBA context object.
1699 * @pmboxq: Pointer to mailbox command.
1700 *
1701 * This is completion handler function for mailbox commands issued from
1702 * lpfc_sli_issue_mbox_wait function. This function is called by the
1703 * mailbox event handler function with no lock held. This function
1704 * will wake up thread waiting on the wait queue pointed by context1
1705 * of the mailbox.
1706 **/
James Smart04c68492009-05-22 14:52:52 -04001707void
James Smart2e0fef82007-06-17 19:56:36 -05001708lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001709{
1710 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001711 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001712
1713 /*
1714 * If pdone_q is empty, the driver thread gave up waiting and
1715 * continued running.
1716 */
James Smart7054a602007-04-25 09:52:34 -04001717 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001718 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001719 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1720 if (pdone_q)
1721 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001722 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001723 return;
1724}
1725
James Smarte59058c2008-08-24 21:49:00 -04001726
1727/**
James Smart3621a712009-04-06 18:47:14 -04001728 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001729 * @phba: Pointer to HBA context object.
1730 * @pmb: Pointer to mailbox object.
1731 *
1732 * This function is the default mailbox completion handler. It
1733 * frees the memory resources associated with the completed mailbox
1734 * command. If the completed command is a REG_LOGIN mailbox command,
1735 * this function will issue a UREG_LOGIN to re-claim the RPI.
1736 **/
dea31012005-04-17 16:05:31 -05001737void
James Smart2e0fef82007-06-17 19:56:36 -05001738lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001739{
1740 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001741 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001742 int rc;
James Smart695a8142010-01-26 23:08:03 -05001743 struct lpfc_vport *vport = pmb->vport;
James Smart7054a602007-04-25 09:52:34 -04001744
dea31012005-04-17 16:05:31 -05001745 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001746
dea31012005-04-17 16:05:31 -05001747 if (mp) {
1748 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1749 kfree(mp);
1750 }
James Smart7054a602007-04-25 09:52:34 -04001751
James Smart04c68492009-05-22 14:52:52 -04001752 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1753 (phba->sli_rev == LPFC_SLI_REV4))
1754 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1755
James Smart7054a602007-04-25 09:52:34 -04001756 /*
1757 * If a REG_LOGIN succeeded after node is destroyed or node
1758 * is in re-discovery driver need to cleanup the RPI.
1759 */
James Smart2e0fef82007-06-17 19:56:36 -05001760 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001761 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1762 !pmb->u.mb.mbxStatus) {
1763 rpi = pmb->u.mb.un.varWords[0];
1764 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1765 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001766 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001767 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1768 if (rc != MBX_NOT_FINISHED)
1769 return;
1770 }
1771
James Smart695a8142010-01-26 23:08:03 -05001772 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1773 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1774 !(phba->pport->load_flag & FC_UNLOADING) &&
1775 !pmb->u.mb.mbxStatus) {
1776 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1777 pmb->vport = vport;
1778 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1779 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1780 if (rc != MBX_NOT_FINISHED)
1781 return;
1782 }
1783
James Smart04c68492009-05-22 14:52:52 -04001784 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1785 lpfc_sli4_mbox_cmd_free(phba, pmb);
1786 else
1787 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001788}
1789
James Smarte59058c2008-08-24 21:49:00 -04001790/**
James Smart3621a712009-04-06 18:47:14 -04001791 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001792 * @phba: Pointer to HBA context object.
1793 *
1794 * This function is called with no lock held. This function processes all
1795 * the completed mailbox commands and gives it to upper layers. The interrupt
1796 * service routine processes mailbox completion interrupt and adds completed
1797 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1798 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1799 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1800 * function returns the mailbox commands to the upper layer by calling the
1801 * completion handler function of each mailbox.
1802 **/
dea31012005-04-17 16:05:31 -05001803int
James Smart2e0fef82007-06-17 19:56:36 -05001804lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001805{
James Smart92d7f7b2007-06-17 19:56:38 -05001806 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001807 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001808 int rc;
1809 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001810
1811 phba->sli.slistat.mbox_event++;
1812
James Smart92d7f7b2007-06-17 19:56:38 -05001813 /* Get all completed mailboxe buffers into the cmplq */
1814 spin_lock_irq(&phba->hbalock);
1815 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1816 spin_unlock_irq(&phba->hbalock);
1817
dea31012005-04-17 16:05:31 -05001818 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001819 do {
1820 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1821 if (pmb == NULL)
1822 break;
1823
James Smart04c68492009-05-22 14:52:52 -04001824 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001825
James Smart858c9f62007-06-17 19:56:39 -05001826 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1827 if (pmb->vport) {
1828 lpfc_debugfs_disc_trc(pmb->vport,
1829 LPFC_DISC_TRC_MBOX_VPORT,
1830 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1831 (uint32_t)pmbox->mbxCommand,
1832 pmbox->un.varWords[0],
1833 pmbox->un.varWords[1]);
1834 }
1835 else {
1836 lpfc_debugfs_disc_trc(phba->pport,
1837 LPFC_DISC_TRC_MBOX,
1838 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1839 (uint32_t)pmbox->mbxCommand,
1840 pmbox->un.varWords[0],
1841 pmbox->un.varWords[1]);
1842 }
1843 }
1844
dea31012005-04-17 16:05:31 -05001845 /*
1846 * It is a fatal error if unknown mbox command completion.
1847 */
1848 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1849 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001850 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001852 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001853 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001854 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001855 pmbox->mbxCommand,
1856 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001857 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001858 phba->work_hs = HS_FFER3;
1859 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001860 continue;
dea31012005-04-17 16:05:31 -05001861 }
1862
dea31012005-04-17 16:05:31 -05001863 if (pmbox->mbxStatus) {
1864 phba->sli.slistat.mbox_stat_err++;
1865 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1866 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001867 lpfc_printf_log(phba, KERN_INFO,
1868 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001869 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001870 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001871 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001872 pmb->vport ? pmb->vport->vpi :0,
1873 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001874 lpfc_sli4_mbox_opcode_get(phba,
1875 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001876 pmbox->mbxStatus,
1877 pmbox->un.varWords[0],
1878 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001879 pmbox->mbxStatus = 0;
1880 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001882 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001883 continue;
dea31012005-04-17 16:05:31 -05001884 }
1885 }
1886
1887 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001888 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001889 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001890 "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 -05001891 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001892 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001893 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001894 pmb->mbox_cmpl,
1895 *((uint32_t *) pmbox),
1896 pmbox->un.varWords[0],
1897 pmbox->un.varWords[1],
1898 pmbox->un.varWords[2],
1899 pmbox->un.varWords[3],
1900 pmbox->un.varWords[4],
1901 pmbox->un.varWords[5],
1902 pmbox->un.varWords[6],
1903 pmbox->un.varWords[7]);
1904
James Smart92d7f7b2007-06-17 19:56:38 -05001905 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001906 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001907 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001908 return 0;
dea31012005-04-17 16:05:31 -05001909}
James Smart92d7f7b2007-06-17 19:56:38 -05001910
James Smarte59058c2008-08-24 21:49:00 -04001911/**
James Smart3621a712009-04-06 18:47:14 -04001912 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001913 * @phba: Pointer to HBA context object.
1914 * @pring: Pointer to driver SLI ring object.
1915 * @tag: buffer tag.
1916 *
1917 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1918 * is set in the tag the buffer is posted for a particular exchange,
1919 * the function will return the buffer without replacing the buffer.
1920 * If the buffer is for unsolicited ELS or CT traffic, this function
1921 * returns the buffer and also posts another buffer to the firmware.
1922 **/
James Smart76bb24e2007-10-27 13:38:00 -04001923static struct lpfc_dmabuf *
1924lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001925 struct lpfc_sli_ring *pring,
1926 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001927{
James Smart9f1e1b52008-12-04 22:39:40 -05001928 struct hbq_dmabuf *hbq_entry;
1929
James Smart76bb24e2007-10-27 13:38:00 -04001930 if (tag & QUE_BUFTAG_BIT)
1931 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001932 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1933 if (!hbq_entry)
1934 return NULL;
1935 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001936}
James Smart57127f12007-10-27 13:37:05 -04001937
James Smart3772a992009-05-22 14:50:54 -04001938/**
1939 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1940 * @phba: Pointer to HBA context object.
1941 * @pring: Pointer to driver SLI ring object.
1942 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1943 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1944 * @fch_type: the type for the first frame of the sequence.
1945 *
1946 * This function is called with no lock held. This function uses the r_ctl and
1947 * type of the received sequence to find the correct callback function to call
1948 * to process the sequence.
1949 **/
1950static int
1951lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1952 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1953 uint32_t fch_type)
1954{
1955 int i;
1956
1957 /* unSolicited Responses */
1958 if (pring->prt[0].profile) {
1959 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1960 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1961 saveq);
1962 return 1;
1963 }
1964 /* We must search, based on rctl / type
1965 for the right routine */
1966 for (i = 0; i < pring->num_mask; i++) {
1967 if ((pring->prt[i].rctl == fch_r_ctl) &&
1968 (pring->prt[i].type == fch_type)) {
1969 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1970 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1971 (phba, pring, saveq);
1972 return 1;
1973 }
1974 }
1975 return 0;
1976}
James Smarte59058c2008-08-24 21:49:00 -04001977
1978/**
James Smart3621a712009-04-06 18:47:14 -04001979 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001980 * @phba: Pointer to HBA context object.
1981 * @pring: Pointer to driver SLI ring object.
1982 * @saveq: Pointer to the unsolicited iocb.
1983 *
1984 * This function is called with no lock held by the ring event handler
1985 * when there is an unsolicited iocb posted to the response ring by the
1986 * firmware. This function gets the buffer associated with the iocbs
1987 * and calls the event handler for the ring. This function handles both
1988 * qring buffers and hbq buffers.
1989 * When the function returns 1 the caller can free the iocb object otherwise
1990 * upper layer functions will free the iocb objects.
1991 **/
dea31012005-04-17 16:05:31 -05001992static int
1993lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1994 struct lpfc_iocbq *saveq)
1995{
1996 IOCB_t * irsp;
1997 WORD5 * w5p;
1998 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001999 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002000 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002001 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002002
2003 match = 0;
2004 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002005
2006 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2007 if (pring->lpfc_sli_rcv_async_status)
2008 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2009 else
2010 lpfc_printf_log(phba,
2011 KERN_WARNING,
2012 LOG_SLI,
2013 "0316 Ring %d handler: unexpected "
2014 "ASYNC_STATUS iocb received evt_code "
2015 "0x%x\n",
2016 pring->ringno,
2017 irsp->un.asyncstat.evt_code);
2018 return 1;
2019 }
2020
James Smart3163f722008-02-08 18:50:25 -05002021 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2022 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2023 if (irsp->ulpBdeCount > 0) {
2024 dmzbuf = lpfc_sli_get_buff(phba, pring,
2025 irsp->un.ulpWord[3]);
2026 lpfc_in_buf_free(phba, dmzbuf);
2027 }
2028
2029 if (irsp->ulpBdeCount > 1) {
2030 dmzbuf = lpfc_sli_get_buff(phba, pring,
2031 irsp->unsli3.sli3Words[3]);
2032 lpfc_in_buf_free(phba, dmzbuf);
2033 }
2034
2035 if (irsp->ulpBdeCount > 2) {
2036 dmzbuf = lpfc_sli_get_buff(phba, pring,
2037 irsp->unsli3.sli3Words[7]);
2038 lpfc_in_buf_free(phba, dmzbuf);
2039 }
2040
2041 return 1;
2042 }
2043
James Smart92d7f7b2007-06-17 19:56:38 -05002044 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002045 if (irsp->ulpBdeCount != 0) {
2046 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002047 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002048 if (!saveq->context2)
2049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_SLI,
2052 "0341 Ring %d Cannot find buffer for "
2053 "an unsolicited iocb. tag 0x%x\n",
2054 pring->ringno,
2055 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002056 }
2057 if (irsp->ulpBdeCount == 2) {
2058 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002059 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002060 if (!saveq->context3)
2061 lpfc_printf_log(phba,
2062 KERN_ERR,
2063 LOG_SLI,
2064 "0342 Ring %d Cannot find buffer for an"
2065 " unsolicited iocb. tag 0x%x\n",
2066 pring->ringno,
2067 irsp->unsli3.sli3Words[7]);
2068 }
2069 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002070 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002071 if (irsp->ulpBdeCount != 0) {
2072 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2073 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002074 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002075 lpfc_printf_log(phba,
2076 KERN_ERR,
2077 LOG_SLI,
2078 "0343 Ring %d Cannot find "
2079 "buffer for an unsolicited iocb"
2080 ". tag 0x%x\n", pring->ringno,
2081 irsp->un.ulpWord[3]);
2082 }
2083 if (irsp->ulpBdeCount == 2) {
2084 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2085 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002086 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002087 lpfc_printf_log(phba,
2088 KERN_ERR,
2089 LOG_SLI,
2090 "0344 Ring %d Cannot find "
2091 "buffer for an unsolicited "
2092 "iocb. tag 0x%x\n",
2093 pring->ringno,
2094 irsp->unsli3.sli3Words[7]);
2095 }
2096 }
James Smart92d7f7b2007-06-17 19:56:38 -05002097 }
James Smart9c2face2008-01-11 01:53:18 -05002098 if (irsp->ulpBdeCount != 0 &&
2099 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2100 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2101 int found = 0;
2102
2103 /* search continue save q for same XRI */
2104 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2105 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2106 list_add_tail(&saveq->list, &iocbq->list);
2107 found = 1;
2108 break;
2109 }
2110 }
2111 if (!found)
2112 list_add_tail(&saveq->clist,
2113 &pring->iocb_continue_saveq);
2114 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2115 list_del_init(&iocbq->clist);
2116 saveq = iocbq;
2117 irsp = &(saveq->iocb);
2118 } else
2119 return 0;
2120 }
2121 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2122 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2123 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002124 Rctl = FC_RCTL_ELS_REQ;
2125 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002126 } else {
2127 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2128 Rctl = w5p->hcsw.Rctl;
2129 Type = w5p->hcsw.Type;
2130
2131 /* Firmware Workaround */
2132 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2133 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2134 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002135 Rctl = FC_RCTL_ELS_REQ;
2136 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002137 w5p->hcsw.Rctl = Rctl;
2138 w5p->hcsw.Type = Type;
2139 }
2140 }
James Smart92d7f7b2007-06-17 19:56:38 -05002141
James Smart3772a992009-05-22 14:50:54 -04002142 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002143 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002144 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002145 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002146 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002147
James Smart92d7f7b2007-06-17 19:56:38 -05002148 return 1;
dea31012005-04-17 16:05:31 -05002149}
2150
James Smarte59058c2008-08-24 21:49:00 -04002151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: Pointer to HBA context object.
2154 * @pring: Pointer to driver SLI ring object.
2155 * @prspiocb: Pointer to response iocb object.
2156 *
2157 * This function looks up the iocb_lookup table to get the command iocb
2158 * corresponding to the given response iocb using the iotag of the
2159 * response iocb. This function is called with the hbalock held.
2160 * This function returns the command iocb object if it finds the command
2161 * iocb else returns NULL.
2162 **/
dea31012005-04-17 16:05:31 -05002163static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002164lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2165 struct lpfc_sli_ring *pring,
2166 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002167{
dea31012005-04-17 16:05:31 -05002168 struct lpfc_iocbq *cmd_iocb = NULL;
2169 uint16_t iotag;
2170
James Bottomley604a3e32005-10-29 10:28:33 -05002171 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002172
James Bottomley604a3e32005-10-29 10:28:33 -05002173 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2174 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002175 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002176 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2177 pring->txcmplq_cnt--;
2178 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2179 }
James Bottomley604a3e32005-10-29 10:28:33 -05002180 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002181 }
2182
dea31012005-04-17 16:05:31 -05002183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002184 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002185 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002186 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002187 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002188 return NULL;
2189}
2190
James Smarte59058c2008-08-24 21:49:00 -04002191/**
James Smart3772a992009-05-22 14:50:54 -04002192 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2193 * @phba: Pointer to HBA context object.
2194 * @pring: Pointer to driver SLI ring object.
2195 * @iotag: IOCB tag.
2196 *
2197 * This function looks up the iocb_lookup table to get the command iocb
2198 * corresponding to the given iotag. This function is called with the
2199 * hbalock held.
2200 * This function returns the command iocb object if it finds the command
2201 * iocb else returns NULL.
2202 **/
2203static struct lpfc_iocbq *
2204lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2205 struct lpfc_sli_ring *pring, uint16_t iotag)
2206{
2207 struct lpfc_iocbq *cmd_iocb;
2208
2209 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2210 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2211 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002212 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2213 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2214 pring->txcmplq_cnt--;
2215 }
James Smart3772a992009-05-22 14:50:54 -04002216 return cmd_iocb;
2217 }
2218
2219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2220 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2221 iotag, phba->sli.last_iotag);
2222 return NULL;
2223}
2224
2225/**
James Smart3621a712009-04-06 18:47:14 -04002226 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002227 * @phba: Pointer to HBA context object.
2228 * @pring: Pointer to driver SLI ring object.
2229 * @saveq: Pointer to the response iocb to be processed.
2230 *
2231 * This function is called by the ring event handler for non-fcp
2232 * rings when there is a new response iocb in the response ring.
2233 * The caller is not required to hold any locks. This function
2234 * gets the command iocb associated with the response iocb and
2235 * calls the completion handler for the command iocb. If there
2236 * is no completion handler, the function will free the resources
2237 * associated with command iocb. If the response iocb is for
2238 * an already aborted command iocb, the status of the completion
2239 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2240 * This function always returns 1.
2241 **/
dea31012005-04-17 16:05:31 -05002242static int
James Smart2e0fef82007-06-17 19:56:36 -05002243lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002244 struct lpfc_iocbq *saveq)
2245{
James Smart2e0fef82007-06-17 19:56:36 -05002246 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002247 int rc = 1;
2248 unsigned long iflag;
2249
2250 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002251 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002252 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002253 spin_unlock_irqrestore(&phba->hbalock, iflag);
2254
dea31012005-04-17 16:05:31 -05002255 if (cmdiocbp) {
2256 if (cmdiocbp->iocb_cmpl) {
2257 /*
James Smartea2151b2008-09-07 11:52:10 -04002258 * If an ELS command failed send an event to mgmt
2259 * application.
2260 */
2261 if (saveq->iocb.ulpStatus &&
2262 (pring->ringno == LPFC_ELS_RING) &&
2263 (cmdiocbp->iocb.ulpCommand ==
2264 CMD_ELS_REQUEST64_CR))
2265 lpfc_send_els_failure_event(phba,
2266 cmdiocbp, saveq);
2267
2268 /*
dea31012005-04-17 16:05:31 -05002269 * Post all ELS completions to the worker thread.
2270 * All other are passed to the completion callback.
2271 */
2272 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002273 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2274 (cmdiocbp->iocb_flag &
2275 LPFC_DRIVER_ABORTED)) {
2276 spin_lock_irqsave(&phba->hbalock,
2277 iflag);
James Smart07951072007-04-25 09:51:38 -04002278 cmdiocbp->iocb_flag &=
2279 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002280 spin_unlock_irqrestore(&phba->hbalock,
2281 iflag);
James Smart07951072007-04-25 09:51:38 -04002282 saveq->iocb.ulpStatus =
2283 IOSTAT_LOCAL_REJECT;
2284 saveq->iocb.un.ulpWord[4] =
2285 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002286
2287 /* Firmware could still be in progress
2288 * of DMAing payload, so don't free data
2289 * buffer till after a hbeat.
2290 */
James Smart341af102010-01-26 23:07:37 -05002291 spin_lock_irqsave(&phba->hbalock,
2292 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002293 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002294 spin_unlock_irqrestore(&phba->hbalock,
2295 iflag);
2296 }
James Smart0f65ff62010-02-26 14:14:23 -05002297 if (phba->sli_rev == LPFC_SLI_REV4) {
2298 if (saveq->iocb_flag &
2299 LPFC_EXCHANGE_BUSY) {
2300 /* Set cmdiocb flag for the
2301 * exchange busy so sgl (xri)
2302 * will not be released until
2303 * the abort xri is received
2304 * from hba.
2305 */
2306 spin_lock_irqsave(
2307 &phba->hbalock, iflag);
2308 cmdiocbp->iocb_flag |=
2309 LPFC_EXCHANGE_BUSY;
2310 spin_unlock_irqrestore(
2311 &phba->hbalock, iflag);
2312 }
2313 if (cmdiocbp->iocb_flag &
2314 LPFC_DRIVER_ABORTED) {
2315 /*
2316 * Clear LPFC_DRIVER_ABORTED
2317 * bit in case it was driver
2318 * initiated abort.
2319 */
2320 spin_lock_irqsave(
2321 &phba->hbalock, iflag);
2322 cmdiocbp->iocb_flag &=
2323 ~LPFC_DRIVER_ABORTED;
2324 spin_unlock_irqrestore(
2325 &phba->hbalock, iflag);
2326 cmdiocbp->iocb.ulpStatus =
2327 IOSTAT_LOCAL_REJECT;
2328 cmdiocbp->iocb.un.ulpWord[4] =
2329 IOERR_ABORT_REQUESTED;
2330 /*
2331 * For SLI4, irsiocb contains
2332 * NO_XRI in sli_xritag, it
2333 * shall not affect releasing
2334 * sgl (xri) process.
2335 */
2336 saveq->iocb.ulpStatus =
2337 IOSTAT_LOCAL_REJECT;
2338 saveq->iocb.un.ulpWord[4] =
2339 IOERR_SLI_ABORTED;
2340 spin_lock_irqsave(
2341 &phba->hbalock, iflag);
2342 saveq->iocb_flag |=
2343 LPFC_DELAY_MEM_FREE;
2344 spin_unlock_irqrestore(
2345 &phba->hbalock, iflag);
2346 }
James Smart07951072007-04-25 09:51:38 -04002347 }
dea31012005-04-17 16:05:31 -05002348 }
James Smart2e0fef82007-06-17 19:56:36 -05002349 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002350 } else
2351 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002352 } else {
2353 /*
2354 * Unknown initiating command based on the response iotag.
2355 * This could be the case on the ELS ring because of
2356 * lpfc_els_abort().
2357 */
2358 if (pring->ringno != LPFC_ELS_RING) {
2359 /*
2360 * Ring <ringno> handler: unexpected completion IoTag
2361 * <IoTag>
2362 */
James Smarta257bf92009-04-06 18:48:10 -04002363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002364 "0322 Ring %d handler: "
2365 "unexpected completion IoTag x%x "
2366 "Data: x%x x%x x%x x%x\n",
2367 pring->ringno,
2368 saveq->iocb.ulpIoTag,
2369 saveq->iocb.ulpStatus,
2370 saveq->iocb.un.ulpWord[4],
2371 saveq->iocb.ulpCommand,
2372 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002373 }
2374 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002375
dea31012005-04-17 16:05:31 -05002376 return rc;
2377}
2378
James Smarte59058c2008-08-24 21:49:00 -04002379/**
James Smart3621a712009-04-06 18:47:14 -04002380 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002381 * @phba: Pointer to HBA context object.
2382 * @pring: Pointer to driver SLI ring object.
2383 *
2384 * This function is called from the iocb ring event handlers when
2385 * put pointer is ahead of the get pointer for a ring. This function signal
2386 * an error attention condition to the worker thread and the worker
2387 * thread will transition the HBA to offline state.
2388 **/
James Smart2e0fef82007-06-17 19:56:36 -05002389static void
2390lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002391{
James Smart34b02dc2008-08-24 21:49:55 -04002392 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002393 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002394 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002395 * rsp ring <portRspMax>
2396 */
2397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002398 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002399 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002400 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002401 pring->numRiocb);
2402
James Smart2e0fef82007-06-17 19:56:36 -05002403 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002404
2405 /*
2406 * All error attention handlers are posted to
2407 * worker thread
2408 */
2409 phba->work_ha |= HA_ERATT;
2410 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002411
James Smart5e9d9b82008-06-14 22:52:53 -04002412 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002413
2414 return;
2415}
2416
James Smarte59058c2008-08-24 21:49:00 -04002417/**
James Smart3621a712009-04-06 18:47:14 -04002418 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002419 * @ptr: Pointer to address of HBA context object.
2420 *
2421 * This function is invoked by the Error Attention polling timer when the
2422 * timer times out. It will check the SLI Error Attention register for
2423 * possible attention events. If so, it will post an Error Attention event
2424 * and wake up worker thread to process it. Otherwise, it will set up the
2425 * Error Attention polling timer for the next poll.
2426 **/
2427void lpfc_poll_eratt(unsigned long ptr)
2428{
2429 struct lpfc_hba *phba;
2430 uint32_t eratt = 0;
2431
2432 phba = (struct lpfc_hba *)ptr;
2433
2434 /* Check chip HA register for error event */
2435 eratt = lpfc_sli_check_eratt(phba);
2436
2437 if (eratt)
2438 /* Tell the worker thread there is work to do */
2439 lpfc_worker_wake_up(phba);
2440 else
2441 /* Restart the timer for next eratt poll */
2442 mod_timer(&phba->eratt_poll, jiffies +
2443 HZ * LPFC_ERATT_POLL_INTERVAL);
2444 return;
2445}
2446
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002447
James Smarte59058c2008-08-24 21:49:00 -04002448/**
James Smart3621a712009-04-06 18:47:14 -04002449 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002450 * @phba: Pointer to HBA context object.
2451 * @pring: Pointer to driver SLI ring object.
2452 * @mask: Host attention register mask for this ring.
2453 *
2454 * This function is called from the interrupt context when there is a ring
2455 * event for the fcp ring. The caller does not hold any lock.
2456 * The function processes each response iocb in the response ring until it
2457 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2458 * LE bit set. The function will call the completion handler of the command iocb
2459 * if the response iocb indicates a completion for a command iocb or it is
2460 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2461 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002462 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002463 * to check it explicitly.
2464 */
2465int
James Smart2e0fef82007-06-17 19:56:36 -05002466lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2467 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002468{
James Smart34b02dc2008-08-24 21:49:55 -04002469 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002470 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002471 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002472 struct lpfc_iocbq *cmdiocbq = NULL;
2473 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002474 uint32_t status;
2475 uint32_t portRspPut, portRspMax;
2476 int rc = 1;
2477 lpfc_iocb_type type;
2478 unsigned long iflag;
2479 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002480
James Smart2e0fef82007-06-17 19:56:36 -05002481 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002482 pring->stats.iocb_event++;
2483
dea31012005-04-17 16:05:31 -05002484 /*
2485 * The next available response entry should never exceed the maximum
2486 * entries. If it does, treat it as an adapter hardware error.
2487 */
2488 portRspMax = pring->numRiocb;
2489 portRspPut = le32_to_cpu(pgp->rspPutInx);
2490 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002491 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002492 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002493 return 1;
2494 }
James Smart45ed1192009-10-02 15:17:02 -04002495 if (phba->fcp_ring_in_use) {
2496 spin_unlock_irqrestore(&phba->hbalock, iflag);
2497 return 1;
2498 } else
2499 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002500
2501 rmb();
2502 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002503 /*
2504 * Fetch an entry off the ring and copy it into a local data
2505 * structure. The copy involves a byte-swap since the
2506 * network byte order and pci byte orders are different.
2507 */
James Smarted957682007-06-17 19:56:37 -05002508 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002509 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002510
2511 if (++pring->rspidx >= portRspMax)
2512 pring->rspidx = 0;
2513
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002514 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2515 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002516 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002517 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002518 irsp = &rspiocbq.iocb;
2519
dea31012005-04-17 16:05:31 -05002520 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2521 pring->stats.iocb_rsp++;
2522 rsp_cmpl++;
2523
2524 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002525 /*
2526 * If resource errors reported from HBA, reduce
2527 * queuedepths of the SCSI device.
2528 */
2529 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2530 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2531 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002532 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002533 spin_lock_irqsave(&phba->hbalock, iflag);
2534 }
2535
dea31012005-04-17 16:05:31 -05002536 /* Rsp ring <ringno> error: IOCB */
2537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002538 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002539 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002540 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002541 irsp->un.ulpWord[0],
2542 irsp->un.ulpWord[1],
2543 irsp->un.ulpWord[2],
2544 irsp->un.ulpWord[3],
2545 irsp->un.ulpWord[4],
2546 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002547 *(uint32_t *)&irsp->un1,
2548 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002549 }
2550
2551 switch (type) {
2552 case LPFC_ABORT_IOCB:
2553 case LPFC_SOL_IOCB:
2554 /*
2555 * Idle exchange closed via ABTS from port. No iocb
2556 * resources need to be recovered.
2557 */
2558 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002559 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002560 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002561 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002562 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002563 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002564 break;
2565 }
2566
James Bottomley604a3e32005-10-29 10:28:33 -05002567 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2568 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002569 if (unlikely(!cmdiocbq))
2570 break;
2571 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2572 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2573 if (cmdiocbq->iocb_cmpl) {
2574 spin_unlock_irqrestore(&phba->hbalock, iflag);
2575 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2576 &rspiocbq);
2577 spin_lock_irqsave(&phba->hbalock, iflag);
2578 }
dea31012005-04-17 16:05:31 -05002579 break;
James Smarta4bc3372006-12-02 13:34:16 -05002580 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002581 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002582 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002583 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002584 break;
dea31012005-04-17 16:05:31 -05002585 default:
2586 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2587 char adaptermsg[LPFC_MAX_ADPTMSG];
2588 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2589 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2590 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002591 dev_warn(&((phba->pcidev)->dev),
2592 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002593 phba->brd_no, adaptermsg);
2594 } else {
2595 /* Unknown IOCB command */
2596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002597 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002598 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002599 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002600 irsp->ulpStatus,
2601 irsp->ulpIoTag,
2602 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002603 }
2604 break;
2605 }
2606
2607 /*
2608 * The response IOCB has been processed. Update the ring
2609 * pointer in SLIM. If the port response put pointer has not
2610 * been updated, sync the pgp->rspPutInx and fetch the new port
2611 * response put pointer.
2612 */
James Smarted957682007-06-17 19:56:37 -05002613 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002614
2615 if (pring->rspidx == portRspPut)
2616 portRspPut = le32_to_cpu(pgp->rspPutInx);
2617 }
2618
2619 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2620 pring->stats.iocb_rsp_full++;
2621 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2622 writel(status, phba->CAregaddr);
2623 readl(phba->CAregaddr);
2624 }
2625 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2626 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2627 pring->stats.iocb_cmd_empty++;
2628
2629 /* Force update of the local copy of cmdGetInx */
2630 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2631 lpfc_sli_resume_iocb(phba, pring);
2632
2633 if ((pring->lpfc_sli_cmd_available))
2634 (pring->lpfc_sli_cmd_available) (phba, pring);
2635
2636 }
2637
James Smart45ed1192009-10-02 15:17:02 -04002638 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002639 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002640 return rc;
2641}
2642
James Smarte59058c2008-08-24 21:49:00 -04002643/**
James Smart3772a992009-05-22 14:50:54 -04002644 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2645 * @phba: Pointer to HBA context object.
2646 * @pring: Pointer to driver SLI ring object.
2647 * @rspiocbp: Pointer to driver response IOCB object.
2648 *
2649 * This function is called from the worker thread when there is a slow-path
2650 * response IOCB to process. This function chains all the response iocbs until
2651 * seeing the iocb with the LE bit set. The function will call
2652 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2653 * completion of a command iocb. The function will call the
2654 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2655 * The function frees the resources or calls the completion handler if this
2656 * iocb is an abort completion. The function returns NULL when the response
2657 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2658 * this function shall chain the iocb on to the iocb_continueq and return the
2659 * response iocb passed in.
2660 **/
2661static struct lpfc_iocbq *
2662lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2663 struct lpfc_iocbq *rspiocbp)
2664{
2665 struct lpfc_iocbq *saveq;
2666 struct lpfc_iocbq *cmdiocbp;
2667 struct lpfc_iocbq *next_iocb;
2668 IOCB_t *irsp = NULL;
2669 uint32_t free_saveq;
2670 uint8_t iocb_cmd_type;
2671 lpfc_iocb_type type;
2672 unsigned long iflag;
2673 int rc;
2674
2675 spin_lock_irqsave(&phba->hbalock, iflag);
2676 /* First add the response iocb to the countinueq list */
2677 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2678 pring->iocb_continueq_cnt++;
2679
2680 /* Now, determine whetehr the list is completed for processing */
2681 irsp = &rspiocbp->iocb;
2682 if (irsp->ulpLe) {
2683 /*
2684 * By default, the driver expects to free all resources
2685 * associated with this iocb completion.
2686 */
2687 free_saveq = 1;
2688 saveq = list_get_first(&pring->iocb_continueq,
2689 struct lpfc_iocbq, list);
2690 irsp = &(saveq->iocb);
2691 list_del_init(&pring->iocb_continueq);
2692 pring->iocb_continueq_cnt = 0;
2693
2694 pring->stats.iocb_rsp++;
2695
2696 /*
2697 * If resource errors reported from HBA, reduce
2698 * queuedepths of the SCSI device.
2699 */
2700 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2701 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2702 spin_unlock_irqrestore(&phba->hbalock, iflag);
2703 phba->lpfc_rampdown_queue_depth(phba);
2704 spin_lock_irqsave(&phba->hbalock, iflag);
2705 }
2706
2707 if (irsp->ulpStatus) {
2708 /* Rsp ring <ringno> error: IOCB */
2709 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2710 "0328 Rsp Ring %d error: "
2711 "IOCB Data: "
2712 "x%x x%x x%x x%x "
2713 "x%x x%x x%x x%x "
2714 "x%x x%x x%x x%x "
2715 "x%x x%x x%x x%x\n",
2716 pring->ringno,
2717 irsp->un.ulpWord[0],
2718 irsp->un.ulpWord[1],
2719 irsp->un.ulpWord[2],
2720 irsp->un.ulpWord[3],
2721 irsp->un.ulpWord[4],
2722 irsp->un.ulpWord[5],
2723 *(((uint32_t *) irsp) + 6),
2724 *(((uint32_t *) irsp) + 7),
2725 *(((uint32_t *) irsp) + 8),
2726 *(((uint32_t *) irsp) + 9),
2727 *(((uint32_t *) irsp) + 10),
2728 *(((uint32_t *) irsp) + 11),
2729 *(((uint32_t *) irsp) + 12),
2730 *(((uint32_t *) irsp) + 13),
2731 *(((uint32_t *) irsp) + 14),
2732 *(((uint32_t *) irsp) + 15));
2733 }
2734
2735 /*
2736 * Fetch the IOCB command type and call the correct completion
2737 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2738 * get freed back to the lpfc_iocb_list by the discovery
2739 * kernel thread.
2740 */
2741 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2742 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2743 switch (type) {
2744 case LPFC_SOL_IOCB:
2745 spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2747 spin_lock_irqsave(&phba->hbalock, iflag);
2748 break;
2749
2750 case LPFC_UNSOL_IOCB:
2751 spin_unlock_irqrestore(&phba->hbalock, iflag);
2752 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2753 spin_lock_irqsave(&phba->hbalock, iflag);
2754 if (!rc)
2755 free_saveq = 0;
2756 break;
2757
2758 case LPFC_ABORT_IOCB:
2759 cmdiocbp = NULL;
2760 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2761 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2762 saveq);
2763 if (cmdiocbp) {
2764 /* Call the specified completion routine */
2765 if (cmdiocbp->iocb_cmpl) {
2766 spin_unlock_irqrestore(&phba->hbalock,
2767 iflag);
2768 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2769 saveq);
2770 spin_lock_irqsave(&phba->hbalock,
2771 iflag);
2772 } else
2773 __lpfc_sli_release_iocbq(phba,
2774 cmdiocbp);
2775 }
2776 break;
2777
2778 case LPFC_UNKNOWN_IOCB:
2779 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2780 char adaptermsg[LPFC_MAX_ADPTMSG];
2781 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2782 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2783 MAX_MSG_DATA);
2784 dev_warn(&((phba->pcidev)->dev),
2785 "lpfc%d: %s\n",
2786 phba->brd_no, adaptermsg);
2787 } else {
2788 /* Unknown IOCB command */
2789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2790 "0335 Unknown IOCB "
2791 "command Data: x%x "
2792 "x%x x%x x%x\n",
2793 irsp->ulpCommand,
2794 irsp->ulpStatus,
2795 irsp->ulpIoTag,
2796 irsp->ulpContext);
2797 }
2798 break;
2799 }
2800
2801 if (free_saveq) {
2802 list_for_each_entry_safe(rspiocbp, next_iocb,
2803 &saveq->list, list) {
2804 list_del(&rspiocbp->list);
2805 __lpfc_sli_release_iocbq(phba, rspiocbp);
2806 }
2807 __lpfc_sli_release_iocbq(phba, saveq);
2808 }
2809 rspiocbp = NULL;
2810 }
2811 spin_unlock_irqrestore(&phba->hbalock, iflag);
2812 return rspiocbp;
2813}
2814
2815/**
2816 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002817 * @phba: Pointer to HBA context object.
2818 * @pring: Pointer to driver SLI ring object.
2819 * @mask: Host attention register mask for this ring.
2820 *
James Smart3772a992009-05-22 14:50:54 -04002821 * This routine wraps the actual slow_ring event process routine from the
2822 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002823 **/
James Smart3772a992009-05-22 14:50:54 -04002824void
James Smart2e0fef82007-06-17 19:56:36 -05002825lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2826 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002827{
James Smart3772a992009-05-22 14:50:54 -04002828 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2829}
2830
2831/**
2832 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2833 * @phba: Pointer to HBA context object.
2834 * @pring: Pointer to driver SLI ring object.
2835 * @mask: Host attention register mask for this ring.
2836 *
2837 * This function is called from the worker thread when there is a ring event
2838 * for non-fcp rings. The caller does not hold any lock. The function will
2839 * remove each response iocb in the response ring and calls the handle
2840 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2841 **/
2842static void
2843lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2844 struct lpfc_sli_ring *pring, uint32_t mask)
2845{
James Smart34b02dc2008-08-24 21:49:55 -04002846 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002847 IOCB_t *entry;
2848 IOCB_t *irsp = NULL;
2849 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002850 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002851 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002852 uint32_t status;
dea31012005-04-17 16:05:31 -05002853
James Smart34b02dc2008-08-24 21:49:55 -04002854 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002856 pring->stats.iocb_event++;
2857
dea31012005-04-17 16:05:31 -05002858 /*
2859 * The next available response entry should never exceed the maximum
2860 * entries. If it does, treat it as an adapter hardware error.
2861 */
2862 portRspMax = pring->numRiocb;
2863 portRspPut = le32_to_cpu(pgp->rspPutInx);
2864 if (portRspPut >= portRspMax) {
2865 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002866 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002867 * rsp ring <portRspMax>
2868 */
James Smarted957682007-06-17 19:56:37 -05002869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002870 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002871 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002872 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002873
James Smart2e0fef82007-06-17 19:56:36 -05002874 phba->link_state = LPFC_HBA_ERROR;
2875 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002876
2877 phba->work_hs = HS_FFER3;
2878 lpfc_handle_eratt(phba);
2879
James Smart3772a992009-05-22 14:50:54 -04002880 return;
dea31012005-04-17 16:05:31 -05002881 }
2882
2883 rmb();
dea31012005-04-17 16:05:31 -05002884 while (pring->rspidx != portRspPut) {
2885 /*
2886 * Build a completion list and call the appropriate handler.
2887 * The process is to get the next available response iocb, get
2888 * a free iocb from the list, copy the response data into the
2889 * free iocb, insert to the continuation list, and update the
2890 * next response index to slim. This process makes response
2891 * iocb's in the ring available to DMA as fast as possible but
2892 * pays a penalty for a copy operation. Since the iocb is
2893 * only 32 bytes, this penalty is considered small relative to
2894 * the PCI reads for register values and a slim write. When
2895 * the ulpLe field is set, the entire Command has been
2896 * received.
2897 */
James Smarted957682007-06-17 19:56:37 -05002898 entry = lpfc_resp_iocb(phba, pring);
2899
James Smart858c9f62007-06-17 19:56:39 -05002900 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002901 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002902 if (rspiocbp == NULL) {
2903 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002904 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002905 break;
2906 }
2907
James Smarted957682007-06-17 19:56:37 -05002908 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2909 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002910 irsp = &rspiocbp->iocb;
2911
2912 if (++pring->rspidx >= portRspMax)
2913 pring->rspidx = 0;
2914
James Smarta58cbd52007-08-02 11:09:43 -04002915 if (pring->ringno == LPFC_ELS_RING) {
2916 lpfc_debugfs_slow_ring_trc(phba,
2917 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2918 *(((uint32_t *) irsp) + 4),
2919 *(((uint32_t *) irsp) + 6),
2920 *(((uint32_t *) irsp) + 7));
2921 }
2922
James Smarted957682007-06-17 19:56:37 -05002923 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002924
James Smart3772a992009-05-22 14:50:54 -04002925 spin_unlock_irqrestore(&phba->hbalock, iflag);
2926 /* Handle the response IOCB */
2927 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2928 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002929
2930 /*
2931 * If the port response put pointer has not been updated, sync
2932 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2933 * response put pointer.
2934 */
2935 if (pring->rspidx == portRspPut) {
2936 portRspPut = le32_to_cpu(pgp->rspPutInx);
2937 }
2938 } /* while (pring->rspidx != portRspPut) */
2939
James Smart92d7f7b2007-06-17 19:56:38 -05002940 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002941 /* At least one response entry has been freed */
2942 pring->stats.iocb_rsp_full++;
2943 /* SET RxRE_RSP in Chip Att register */
2944 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2945 writel(status, phba->CAregaddr);
2946 readl(phba->CAregaddr); /* flush */
2947 }
2948 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2949 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2950 pring->stats.iocb_cmd_empty++;
2951
2952 /* Force update of the local copy of cmdGetInx */
2953 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2954 lpfc_sli_resume_iocb(phba, pring);
2955
2956 if ((pring->lpfc_sli_cmd_available))
2957 (pring->lpfc_sli_cmd_available) (phba, pring);
2958
2959 }
2960
James Smart2e0fef82007-06-17 19:56:36 -05002961 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002962 return;
dea31012005-04-17 16:05:31 -05002963}
2964
James Smarte59058c2008-08-24 21:49:00 -04002965/**
James Smart4f774512009-05-22 14:52:35 -04002966 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2967 * @phba: Pointer to HBA context object.
2968 * @pring: Pointer to driver SLI ring object.
2969 * @mask: Host attention register mask for this ring.
2970 *
2971 * This function is called from the worker thread when there is a pending
2972 * ELS response iocb on the driver internal slow-path response iocb worker
2973 * queue. The caller does not hold any lock. The function will remove each
2974 * response iocb from the response worker queue and calls the handle
2975 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2976 **/
2977static void
2978lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2979 struct lpfc_sli_ring *pring, uint32_t mask)
2980{
2981 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002982 struct hbq_dmabuf *dmabuf;
2983 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002984 unsigned long iflag;
2985
James Smart45ed1192009-10-02 15:17:02 -04002986 spin_lock_irqsave(&phba->hbalock, iflag);
2987 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2988 spin_unlock_irqrestore(&phba->hbalock, iflag);
2989 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002990 /* Get the response iocb from the head of work queue */
2991 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002992 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002993 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002994 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002995
2996 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2997 case CQE_CODE_COMPL_WQE:
2998 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2999 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003000 /* Translate ELS WCQE to response IOCBQ */
3001 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3002 irspiocbq);
3003 if (irspiocbq)
3004 lpfc_sli_sp_handle_rspiocb(phba, pring,
3005 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003006 break;
3007 case CQE_CODE_RECEIVE:
3008 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3009 cq_event);
3010 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3011 break;
3012 default:
3013 break;
3014 }
James Smart4f774512009-05-22 14:52:35 -04003015 }
3016}
3017
3018/**
James Smart3621a712009-04-06 18:47:14 -04003019 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003020 * @phba: Pointer to HBA context object.
3021 * @pring: Pointer to driver SLI ring object.
3022 *
3023 * This function aborts all iocbs in the given ring and frees all the iocb
3024 * objects in txq. This function issues an abort iocb for all the iocb commands
3025 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3026 * the return of this function. The caller is not required to hold any locks.
3027 **/
James Smart2e0fef82007-06-17 19:56:36 -05003028void
dea31012005-04-17 16:05:31 -05003029lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3030{
James Smart2534ba72007-04-25 09:52:20 -04003031 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003032 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003033
James Smart92d7f7b2007-06-17 19:56:38 -05003034 if (pring->ringno == LPFC_ELS_RING) {
3035 lpfc_fabric_abort_hba(phba);
3036 }
3037
dea31012005-04-17 16:05:31 -05003038 /* Error everything on txq and txcmplq
3039 * First do the txq.
3040 */
James Smart2e0fef82007-06-17 19:56:36 -05003041 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003042 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003043 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003044
3045 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003046 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3047 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3048
James Smart2e0fef82007-06-17 19:56:36 -05003049 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003050
James Smarta257bf92009-04-06 18:48:10 -04003051 /* Cancel all the IOCBs from the completions list */
3052 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3053 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003054}
3055
James Smarte59058c2008-08-24 21:49:00 -04003056/**
James Smart3621a712009-04-06 18:47:14 -04003057 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003058 * @phba: Pointer to HBA context object.
3059 *
3060 * This function flushes all iocbs in the fcp ring and frees all the iocb
3061 * objects in txq and txcmplq. This function will not issue abort iocbs
3062 * for all the iocb commands in txcmplq, they will just be returned with
3063 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3064 * slot has been permanently disabled.
3065 **/
3066void
3067lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3068{
3069 LIST_HEAD(txq);
3070 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003071 struct lpfc_sli *psli = &phba->sli;
3072 struct lpfc_sli_ring *pring;
3073
3074 /* Currently, only one fcp ring */
3075 pring = &psli->ring[psli->fcp_ring];
3076
3077 spin_lock_irq(&phba->hbalock);
3078 /* Retrieve everything on txq */
3079 list_splice_init(&pring->txq, &txq);
3080 pring->txq_cnt = 0;
3081
3082 /* Retrieve everything on the txcmplq */
3083 list_splice_init(&pring->txcmplq, &txcmplq);
3084 pring->txcmplq_cnt = 0;
3085 spin_unlock_irq(&phba->hbalock);
3086
3087 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003088 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3089 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003090
3091 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003092 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3093 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003094}
3095
3096/**
James Smart3772a992009-05-22 14:50:54 -04003097 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003098 * @phba: Pointer to HBA context object.
3099 * @mask: Bit mask to be checked.
3100 *
3101 * This function reads the host status register and compares
3102 * with the provided bit mask to check if HBA completed
3103 * the restart. This function will wait in a loop for the
3104 * HBA to complete restart. If the HBA does not restart within
3105 * 15 iterations, the function will reset the HBA again. The
3106 * function returns 1 when HBA fail to restart otherwise returns
3107 * zero.
3108 **/
James Smart3772a992009-05-22 14:50:54 -04003109static int
3110lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003111{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003112 uint32_t status;
3113 int i = 0;
3114 int retval = 0;
dea31012005-04-17 16:05:31 -05003115
Jamie Wellnitz41415862006-02-28 19:25:27 -05003116 /* Read the HBA Host Status Register */
3117 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003118
Jamie Wellnitz41415862006-02-28 19:25:27 -05003119 /*
3120 * Check status register every 100ms for 5 retries, then every
3121 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3122 * every 2.5 sec for 4.
3123 * Break our of the loop if errors occurred during init.
3124 */
3125 while (((status & mask) != mask) &&
3126 !(status & HS_FFERM) &&
3127 i++ < 20) {
dea31012005-04-17 16:05:31 -05003128
Jamie Wellnitz41415862006-02-28 19:25:27 -05003129 if (i <= 5)
3130 msleep(10);
3131 else if (i <= 10)
3132 msleep(500);
3133 else
3134 msleep(2500);
dea31012005-04-17 16:05:31 -05003135
Jamie Wellnitz41415862006-02-28 19:25:27 -05003136 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003137 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003138 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003139 lpfc_sli_brdrestart(phba);
3140 }
3141 /* Read the HBA Host Status Register */
3142 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003143 }
dea31012005-04-17 16:05:31 -05003144
Jamie Wellnitz41415862006-02-28 19:25:27 -05003145 /* Check to see if any errors occurred during init */
3146 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3148 "2751 Adapter failed to restart, "
3149 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3150 status,
3151 readl(phba->MBslimaddr + 0xa8),
3152 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003153 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003154 retval = 1;
3155 }
dea31012005-04-17 16:05:31 -05003156
Jamie Wellnitz41415862006-02-28 19:25:27 -05003157 return retval;
dea31012005-04-17 16:05:31 -05003158}
3159
James Smartda0436e2009-05-22 14:51:39 -04003160/**
3161 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3162 * @phba: Pointer to HBA context object.
3163 * @mask: Bit mask to be checked.
3164 *
3165 * This function checks the host status register to check if HBA is
3166 * ready. This function will wait in a loop for the HBA to be ready
3167 * If the HBA is not ready , the function will will reset the HBA PCI
3168 * function again. The function returns 1 when HBA fail to be ready
3169 * otherwise returns zero.
3170 **/
3171static int
3172lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3173{
3174 uint32_t status;
3175 int retval = 0;
3176
3177 /* Read the HBA Host Status Register */
3178 status = lpfc_sli4_post_status_check(phba);
3179
3180 if (status) {
3181 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3182 lpfc_sli_brdrestart(phba);
3183 status = lpfc_sli4_post_status_check(phba);
3184 }
3185
3186 /* Check to see if any errors occurred during init */
3187 if (status) {
3188 phba->link_state = LPFC_HBA_ERROR;
3189 retval = 1;
3190 } else
3191 phba->sli4_hba.intr_enable = 0;
3192
3193 return retval;
3194}
3195
3196/**
3197 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3198 * @phba: Pointer to HBA context object.
3199 * @mask: Bit mask to be checked.
3200 *
3201 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3202 * from the API jump table function pointer from the lpfc_hba struct.
3203 **/
3204int
3205lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3206{
3207 return phba->lpfc_sli_brdready(phba, mask);
3208}
3209
James Smart92908312006-03-07 15:04:13 -05003210#define BARRIER_TEST_PATTERN (0xdeadbeef)
3211
James Smarte59058c2008-08-24 21:49:00 -04003212/**
James Smart3621a712009-04-06 18:47:14 -04003213 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003214 * @phba: Pointer to HBA context object.
3215 *
3216 * This function is called before resetting an HBA. This
3217 * function requests HBA to quiesce DMAs before a reset.
3218 **/
James Smart2e0fef82007-06-17 19:56:36 -05003219void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003220{
James Smart65a29c12006-07-06 15:50:50 -04003221 uint32_t __iomem *resp_buf;
3222 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003223 volatile uint32_t mbox;
3224 uint32_t hc_copy;
3225 int i;
3226 uint8_t hdrtype;
3227
3228 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3229 if (hdrtype != 0x80 ||
3230 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3231 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3232 return;
3233
3234 /*
3235 * Tell the other part of the chip to suspend temporarily all
3236 * its DMA activity.
3237 */
James Smart65a29c12006-07-06 15:50:50 -04003238 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003239
3240 /* Disable the error attention */
3241 hc_copy = readl(phba->HCregaddr);
3242 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3243 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003244 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003245
3246 if (readl(phba->HAregaddr) & HA_ERATT) {
3247 /* Clear Chip error bit */
3248 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003249 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003250 }
3251
3252 mbox = 0;
3253 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3254 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3255
3256 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003257 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003258 writel(mbox, mbox_buf);
3259
3260 for (i = 0;
3261 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3262 mdelay(1);
3263
3264 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003265 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003266 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003267 goto restore_hc;
3268 else
3269 goto clear_errat;
3270 }
3271
3272 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3273 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3274 mdelay(1);
3275
3276clear_errat:
3277
3278 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3279 mdelay(1);
3280
3281 if (readl(phba->HAregaddr) & HA_ERATT) {
3282 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003283 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003284 }
3285
3286restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003287 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003288 writel(hc_copy, phba->HCregaddr);
3289 readl(phba->HCregaddr); /* flush */
3290}
3291
James Smarte59058c2008-08-24 21:49:00 -04003292/**
James Smart3621a712009-04-06 18:47:14 -04003293 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003294 * @phba: Pointer to HBA context object.
3295 *
3296 * This function issues a kill_board mailbox command and waits for
3297 * the error attention interrupt. This function is called for stopping
3298 * the firmware processing. The caller is not required to hold any
3299 * locks. This function calls lpfc_hba_down_post function to free
3300 * any pending commands after the kill. The function will return 1 when it
3301 * fails to kill the board else will return 0.
3302 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003303int
James Smart2e0fef82007-06-17 19:56:36 -05003304lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003305{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003306 struct lpfc_sli *psli;
3307 LPFC_MBOXQ_t *pmb;
3308 uint32_t status;
3309 uint32_t ha_copy;
3310 int retval;
3311 int i = 0;
3312
3313 psli = &phba->sli;
3314
3315 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003316 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003317 "0329 Kill HBA Data: x%x x%x\n",
3318 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003319
James Smart98c9ea52007-10-27 13:37:33 -04003320 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3321 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003322 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003323
3324 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003326 status = readl(phba->HCregaddr);
3327 status &= ~HC_ERINT_ENA;
3328 writel(status, phba->HCregaddr);
3329 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003330 phba->link_flag |= LS_IGNORE_ERATT;
3331 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003332
3333 lpfc_kill_board(phba, pmb);
3334 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3335 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3336
3337 if (retval != MBX_SUCCESS) {
3338 if (retval != MBX_BUSY)
3339 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 "2752 KILL_BOARD command failed retval %d\n",
3342 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003343 spin_lock_irq(&phba->hbalock);
3344 phba->link_flag &= ~LS_IGNORE_ERATT;
3345 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003346 return 1;
3347 }
3348
James Smartf4b4c682009-05-22 14:53:12 -04003349 spin_lock_irq(&phba->hbalock);
3350 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3351 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003352
Jamie Wellnitz41415862006-02-28 19:25:27 -05003353 mempool_free(pmb, phba->mbox_mem_pool);
3354
3355 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3356 * attention every 100ms for 3 seconds. If we don't get ERATT after
3357 * 3 seconds we still set HBA_ERROR state because the status of the
3358 * board is now undefined.
3359 */
3360 ha_copy = readl(phba->HAregaddr);
3361
3362 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3363 mdelay(100);
3364 ha_copy = readl(phba->HAregaddr);
3365 }
3366
3367 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003368 if (ha_copy & HA_ERATT) {
3369 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003370 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003371 }
James Smart2e0fef82007-06-17 19:56:36 -05003372 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003373 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003374 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003375 phba->link_flag &= ~LS_IGNORE_ERATT;
3376 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003377
Jamie Wellnitz41415862006-02-28 19:25:27 -05003378 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003379 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003380
James Smart2e0fef82007-06-17 19:56:36 -05003381 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003382}
3383
James Smarte59058c2008-08-24 21:49:00 -04003384/**
James Smart3772a992009-05-22 14:50:54 -04003385 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003386 * @phba: Pointer to HBA context object.
3387 *
3388 * This function resets the HBA by writing HC_INITFF to the control
3389 * register. After the HBA resets, this function resets all the iocb ring
3390 * indices. This function disables PCI layer parity checking during
3391 * the reset.
3392 * This function returns 0 always.
3393 * The caller is not required to hold any locks.
3394 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003395int
James Smart2e0fef82007-06-17 19:56:36 -05003396lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003397{
3398 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003399 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003400 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003401 int i;
dea31012005-04-17 16:05:31 -05003402
Jamie Wellnitz41415862006-02-28 19:25:27 -05003403 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003404
Jamie Wellnitz41415862006-02-28 19:25:27 -05003405 /* Reset HBA */
3406 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003407 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003408 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003409
3410 /* perform board reset */
3411 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003412 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003413 phba->pport->fc_myDID = 0;
3414 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003415
Jamie Wellnitz41415862006-02-28 19:25:27 -05003416 /* Turn off parity checking and serr during the physical reset */
3417 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3418 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3419 (cfg_value &
3420 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3421
James Smart3772a992009-05-22 14:50:54 -04003422 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3423
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424 /* Now toggle INITFF bit in the Host Control Register */
3425 writel(HC_INITFF, phba->HCregaddr);
3426 mdelay(1);
3427 readl(phba->HCregaddr); /* flush */
3428 writel(0, phba->HCregaddr);
3429 readl(phba->HCregaddr); /* flush */
3430
3431 /* Restore PCI cmd register */
3432 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003433
3434 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003435 for (i = 0; i < psli->num_rings; i++) {
3436 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003437 pring->flag = 0;
3438 pring->rspidx = 0;
3439 pring->next_cmdidx = 0;
3440 pring->local_getidx = 0;
3441 pring->cmdidx = 0;
3442 pring->missbufcnt = 0;
3443 }
dea31012005-04-17 16:05:31 -05003444
James Smart2e0fef82007-06-17 19:56:36 -05003445 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003446 return 0;
3447}
3448
James Smarte59058c2008-08-24 21:49:00 -04003449/**
James Smartda0436e2009-05-22 14:51:39 -04003450 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3451 * @phba: Pointer to HBA context object.
3452 *
3453 * This function resets a SLI4 HBA. This function disables PCI layer parity
3454 * checking during resets the device. The caller is not required to hold
3455 * any locks.
3456 *
3457 * This function returns 0 always.
3458 **/
3459int
3460lpfc_sli4_brdreset(struct lpfc_hba *phba)
3461{
3462 struct lpfc_sli *psli = &phba->sli;
3463 uint16_t cfg_value;
3464 uint8_t qindx;
3465
3466 /* Reset HBA */
3467 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3468 "0295 Reset HBA Data: x%x x%x\n",
3469 phba->pport->port_state, psli->sli_flag);
3470
3471 /* perform board reset */
3472 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003473 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003474 phba->pport->fc_myDID = 0;
3475 phba->pport->fc_prevDID = 0;
3476
3477 /* Turn off parity checking and serr during the physical reset */
3478 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3479 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3480 (cfg_value &
3481 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3482
3483 spin_lock_irq(&phba->hbalock);
3484 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3485 phba->fcf.fcf_flag = 0;
3486 /* Clean up the child queue list for the CQs */
3487 list_del_init(&phba->sli4_hba.mbx_wq->list);
3488 list_del_init(&phba->sli4_hba.els_wq->list);
3489 list_del_init(&phba->sli4_hba.hdr_rq->list);
3490 list_del_init(&phba->sli4_hba.dat_rq->list);
3491 list_del_init(&phba->sli4_hba.mbx_cq->list);
3492 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003493 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3494 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3495 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3496 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3497 spin_unlock_irq(&phba->hbalock);
3498
3499 /* Now physically reset the device */
3500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3501 "0389 Performing PCI function reset!\n");
3502 /* Perform FCoE PCI function reset */
3503 lpfc_pci_function_reset(phba);
3504
3505 return 0;
3506}
3507
3508/**
3509 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003510 * @phba: Pointer to HBA context object.
3511 *
3512 * This function is called in the SLI initialization code path to
3513 * restart the HBA. The caller is not required to hold any lock.
3514 * This function writes MBX_RESTART mailbox command to the SLIM and
3515 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3516 * function to free any pending commands. The function enables
3517 * POST only during the first initialization. The function returns zero.
3518 * The function does not guarantee completion of MBX_RESTART mailbox
3519 * command before the return of this function.
3520 **/
James Smartda0436e2009-05-22 14:51:39 -04003521static int
3522lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003523{
3524 MAILBOX_t *mb;
3525 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003526 volatile uint32_t word0;
3527 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003528 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003529
James Smart2e0fef82007-06-17 19:56:36 -05003530 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003531
James Smart0d878412009-10-02 15:16:56 -04003532 /* Take PCIe device Advanced Error Reporting (AER) state */
3533 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3534
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535 psli = &phba->sli;
3536
3537 /* Restart HBA */
3538 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003539 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003540 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003541
3542 word0 = 0;
3543 mb = (MAILBOX_t *) &word0;
3544 mb->mbxCommand = MBX_RESTART;
3545 mb->mbxHc = 1;
3546
James Smart92908312006-03-07 15:04:13 -05003547 lpfc_reset_barrier(phba);
3548
Jamie Wellnitz41415862006-02-28 19:25:27 -05003549 to_slim = phba->MBslimaddr;
3550 writel(*(uint32_t *) mb, to_slim);
3551 readl(to_slim); /* flush */
3552
3553 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003554 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003555 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003556 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003558 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003559 writel(*(uint32_t *) mb, to_slim);
3560 readl(to_slim); /* flush */
3561
3562 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003563 phba->pport->stopped = 0;
3564 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003565 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003566 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003567
James Smart64ba8812006-08-02 15:24:34 -04003568 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3569 psli->stats_start = get_seconds();
3570
James Smarteaf15d52008-12-04 22:39:29 -05003571 /* Give the INITFF and Post time to settle. */
3572 mdelay(100);
dea31012005-04-17 16:05:31 -05003573
James Smart0d878412009-10-02 15:16:56 -04003574 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3575 if (hba_aer_enabled)
3576 pci_disable_pcie_error_reporting(phba->pcidev);
3577
Jamie Wellnitz41415862006-02-28 19:25:27 -05003578 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003579
3580 return 0;
3581}
3582
James Smarte59058c2008-08-24 21:49:00 -04003583/**
James Smartda0436e2009-05-22 14:51:39 -04003584 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3585 * @phba: Pointer to HBA context object.
3586 *
3587 * This function is called in the SLI initialization code path to restart
3588 * a SLI4 HBA. The caller is not required to hold any lock.
3589 * At the end of the function, it calls lpfc_hba_down_post function to
3590 * free any pending commands.
3591 **/
3592static int
3593lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3594{
3595 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003596 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003597
3598 /* Restart HBA */
3599 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3600 "0296 Restart HBA Data: x%x x%x\n",
3601 phba->pport->port_state, psli->sli_flag);
3602
James Smart75baf692010-06-08 18:31:21 -04003603 /* Take PCIe device Advanced Error Reporting (AER) state */
3604 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3605
James Smartda0436e2009-05-22 14:51:39 -04003606 lpfc_sli4_brdreset(phba);
3607
3608 spin_lock_irq(&phba->hbalock);
3609 phba->pport->stopped = 0;
3610 phba->link_state = LPFC_INIT_START;
3611 phba->hba_flag = 0;
3612 spin_unlock_irq(&phba->hbalock);
3613
3614 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3615 psli->stats_start = get_seconds();
3616
James Smart75baf692010-06-08 18:31:21 -04003617 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3618 if (hba_aer_enabled)
3619 pci_disable_pcie_error_reporting(phba->pcidev);
3620
James Smartda0436e2009-05-22 14:51:39 -04003621 lpfc_hba_down_post(phba);
3622
3623 return 0;
3624}
3625
3626/**
3627 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3628 * @phba: Pointer to HBA context object.
3629 *
3630 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3631 * API jump table function pointer from the lpfc_hba struct.
3632**/
3633int
3634lpfc_sli_brdrestart(struct lpfc_hba *phba)
3635{
3636 return phba->lpfc_sli_brdrestart(phba);
3637}
3638
3639/**
James Smart3621a712009-04-06 18:47:14 -04003640 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003641 * @phba: Pointer to HBA context object.
3642 *
3643 * This function is called after a HBA restart to wait for successful
3644 * restart of the HBA. Successful restart of the HBA is indicated by
3645 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3646 * iteration, the function will restart the HBA again. The function returns
3647 * zero if HBA successfully restarted else returns negative error code.
3648 **/
dea31012005-04-17 16:05:31 -05003649static int
3650lpfc_sli_chipset_init(struct lpfc_hba *phba)
3651{
3652 uint32_t status, i = 0;
3653
3654 /* Read the HBA Host Status Register */
3655 status = readl(phba->HSregaddr);
3656
3657 /* Check status register to see what current state is */
3658 i = 0;
3659 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3660
3661 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3662 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3663 * 4.
3664 */
3665 if (i++ >= 20) {
3666 /* Adapter failed to init, timeout, status reg
3667 <status> */
James Smarted957682007-06-17 19:56:37 -05003668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003669 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003670 "timeout, status reg x%x, "
3671 "FW Data: A8 x%x AC x%x\n", status,
3672 readl(phba->MBslimaddr + 0xa8),
3673 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003674 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003675 return -ETIMEDOUT;
3676 }
3677
3678 /* Check to see if any errors occurred during init */
3679 if (status & HS_FFERM) {
3680 /* ERROR: During chipset initialization */
3681 /* Adapter failed to init, chipset, status reg
3682 <status> */
James Smarted957682007-06-17 19:56:37 -05003683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003684 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003685 "chipset, status reg x%x, "
3686 "FW Data: A8 x%x AC x%x\n", status,
3687 readl(phba->MBslimaddr + 0xa8),
3688 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003689 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003690 return -EIO;
3691 }
3692
3693 if (i <= 5) {
3694 msleep(10);
3695 } else if (i <= 10) {
3696 msleep(500);
3697 } else {
3698 msleep(2500);
3699 }
3700
3701 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003702 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003703 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003704 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003705 }
3706 /* Read the HBA Host Status Register */
3707 status = readl(phba->HSregaddr);
3708 }
3709
3710 /* Check to see if any errors occurred during init */
3711 if (status & HS_FFERM) {
3712 /* ERROR: During chipset initialization */
3713 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003715 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003716 "status reg x%x, "
3717 "FW Data: A8 x%x AC x%x\n", status,
3718 readl(phba->MBslimaddr + 0xa8),
3719 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003720 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003721 return -EIO;
3722 }
3723
3724 /* Clear all interrupt enable conditions */
3725 writel(0, phba->HCregaddr);
3726 readl(phba->HCregaddr); /* flush */
3727
3728 /* setup host attn register */
3729 writel(0xffffffff, phba->HAregaddr);
3730 readl(phba->HAregaddr); /* flush */
3731 return 0;
3732}
3733
James Smarte59058c2008-08-24 21:49:00 -04003734/**
James Smart3621a712009-04-06 18:47:14 -04003735 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003736 *
3737 * This function calculates and returns the number of HBQs required to be
3738 * configured.
3739 **/
James Smart78b2d852007-08-02 11:10:21 -04003740int
James Smarted957682007-06-17 19:56:37 -05003741lpfc_sli_hbq_count(void)
3742{
James Smart92d7f7b2007-06-17 19:56:38 -05003743 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003744}
3745
James Smarte59058c2008-08-24 21:49:00 -04003746/**
James Smart3621a712009-04-06 18:47:14 -04003747 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003748 *
3749 * This function adds the number of hbq entries in every HBQ to get
3750 * the total number of hbq entries required for the HBA and returns
3751 * the total count.
3752 **/
James Smarted957682007-06-17 19:56:37 -05003753static int
3754lpfc_sli_hbq_entry_count(void)
3755{
3756 int hbq_count = lpfc_sli_hbq_count();
3757 int count = 0;
3758 int i;
3759
3760 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003761 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003762 return count;
3763}
3764
James Smarte59058c2008-08-24 21:49:00 -04003765/**
James Smart3621a712009-04-06 18:47:14 -04003766 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003767 *
3768 * This function calculates amount of memory required for all hbq entries
3769 * to be configured and returns the total memory required.
3770 **/
dea31012005-04-17 16:05:31 -05003771int
James Smarted957682007-06-17 19:56:37 -05003772lpfc_sli_hbq_size(void)
3773{
3774 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3775}
3776
James Smarte59058c2008-08-24 21:49:00 -04003777/**
James Smart3621a712009-04-06 18:47:14 -04003778 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003779 * @phba: Pointer to HBA context object.
3780 *
3781 * This function is called during the SLI initialization to configure
3782 * all the HBQs and post buffers to the HBQ. The caller is not
3783 * required to hold any locks. This function will return zero if successful
3784 * else it will return negative error code.
3785 **/
James Smarted957682007-06-17 19:56:37 -05003786static int
3787lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3788{
3789 int hbq_count = lpfc_sli_hbq_count();
3790 LPFC_MBOXQ_t *pmb;
3791 MAILBOX_t *pmbox;
3792 uint32_t hbqno;
3793 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003794
James Smart92d7f7b2007-06-17 19:56:38 -05003795 /* Get a Mailbox buffer to setup mailbox
3796 * commands for HBA initialization
3797 */
James Smarted957682007-06-17 19:56:37 -05003798 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3799
3800 if (!pmb)
3801 return -ENOMEM;
3802
James Smart04c68492009-05-22 14:52:52 -04003803 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003804
3805 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3806 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003807 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003808
3809 hbq_entry_index = 0;
3810 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3811 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3812 phba->hbqs[hbqno].hbqPutIdx = 0;
3813 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3814 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003815 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003816 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3817 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003818 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3819
3820 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3821 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3822 mbxStatus <status>, ring <num> */
3823
3824 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003825 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003826 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003827 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003828 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003829 pmbox->mbxStatus, hbqno);
3830
3831 phba->link_state = LPFC_HBA_ERROR;
3832 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04003833 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003834 }
3835 }
3836 phba->hbq_count = hbq_count;
3837
James Smarted957682007-06-17 19:56:37 -05003838 mempool_free(pmb, phba->mbox_mem_pool);
3839
James Smart92d7f7b2007-06-17 19:56:38 -05003840 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003841 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3842 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003843 return 0;
3844}
3845
James Smarte59058c2008-08-24 21:49:00 -04003846/**
James Smart4f774512009-05-22 14:52:35 -04003847 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3848 * @phba: Pointer to HBA context object.
3849 *
3850 * This function is called during the SLI initialization to configure
3851 * all the HBQs and post buffers to the HBQ. The caller is not
3852 * required to hold any locks. This function will return zero if successful
3853 * else it will return negative error code.
3854 **/
3855static int
3856lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3857{
3858 phba->hbq_in_use = 1;
3859 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3860 phba->hbq_count = 1;
3861 /* Initially populate or replenish the HBQs */
3862 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3863 return 0;
3864}
3865
3866/**
James Smart3621a712009-04-06 18:47:14 -04003867 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003868 * @phba: Pointer to HBA context object.
3869 * @sli_mode: sli mode - 2/3
3870 *
3871 * This function is called by the sli intialization code path
3872 * to issue config_port mailbox command. This function restarts the
3873 * HBA firmware and issues a config_port mailbox command to configure
3874 * the SLI interface in the sli mode specified by sli_mode
3875 * variable. The caller is not required to hold any locks.
3876 * The function returns 0 if successful, else returns negative error
3877 * code.
3878 **/
James Smart93996272008-08-24 21:50:30 -04003879int
3880lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003881{
3882 LPFC_MBOXQ_t *pmb;
3883 uint32_t resetcount = 0, rc = 0, done = 0;
3884
3885 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3886 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003887 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003888 return -ENOMEM;
3889 }
3890
James Smarted957682007-06-17 19:56:37 -05003891 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003892 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003893 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003894 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003895 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003896 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003897 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003898 rc = lpfc_sli_chipset_init(phba);
3899 if (rc)
3900 break;
3901
James Smart2e0fef82007-06-17 19:56:36 -05003902 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003903 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003904 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003905 resetcount++;
3906
James Smarted957682007-06-17 19:56:37 -05003907 /* Call pre CONFIG_PORT mailbox command initialization. A
3908 * value of 0 means the call was successful. Any other
3909 * nonzero value is a failure, but if ERESTART is returned,
3910 * the driver may reset the HBA and try again.
3911 */
dea31012005-04-17 16:05:31 -05003912 rc = lpfc_config_port_prep(phba);
3913 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003914 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003915 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003916 } else if (rc)
dea31012005-04-17 16:05:31 -05003917 break;
James Smart2e0fef82007-06-17 19:56:36 -05003918 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003919 lpfc_config_port(phba, pmb);
3920 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003921 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3922 LPFC_SLI3_HBQ_ENABLED |
3923 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003924 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003925 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003927 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003928 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003929 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003930 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003931 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003932 spin_unlock_irq(&phba->hbalock);
3933 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003934 } else {
3935 /* Allow asynchronous mailbox command to go through */
3936 spin_lock_irq(&phba->hbalock);
3937 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3938 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003939 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003940 }
dea31012005-04-17 16:05:31 -05003941 }
James Smarted957682007-06-17 19:56:37 -05003942 if (!done) {
3943 rc = -EINVAL;
3944 goto do_prep_failed;
3945 }
James Smart04c68492009-05-22 14:52:52 -04003946 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3947 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003948 rc = -ENXIO;
3949 goto do_prep_failed;
3950 }
James Smart04c68492009-05-22 14:52:52 -04003951 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003952 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003953 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3954 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3955 phba->max_vpi : phba->max_vports;
3956
James Smart34b02dc2008-08-24 21:49:55 -04003957 } else
3958 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003959 if (pmb->u.mb.un.varCfgPort.gdss)
3960 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3961 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003962 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003963 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003964 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04003965
3966 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3967 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05003968
3969 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003970 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003971 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3972 else
3973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3974 "0443 Adapter did not grant "
3975 "BlockGuard\n");
3976 }
James Smart34b02dc2008-08-24 21:49:55 -04003977 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003978 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003979 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04003980 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003981 }
James Smart92d7f7b2007-06-17 19:56:38 -05003982do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003983 mempool_free(pmb, phba->mbox_mem_pool);
3984 return rc;
3985}
3986
James Smarte59058c2008-08-24 21:49:00 -04003987
3988/**
James Smart3621a712009-04-06 18:47:14 -04003989 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003990 * @phba: Pointer to HBA context object.
3991 *
3992 * This function is the main SLI intialization function. This function
3993 * is called by the HBA intialization code, HBA reset code and HBA
3994 * error attention handler code. Caller is not required to hold any
3995 * locks. This function issues config_port mailbox command to configure
3996 * the SLI, setup iocb rings and HBQ rings. In the end the function
3997 * calls the config_port_post function to issue init_link mailbox
3998 * command and to start the discovery. The function will return zero
3999 * if successful, else it will return negative error code.
4000 **/
James Smarted957682007-06-17 19:56:37 -05004001int
4002lpfc_sli_hba_setup(struct lpfc_hba *phba)
4003{
4004 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004005 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004006
4007 switch (lpfc_sli_mode) {
4008 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004009 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004011 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004012 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004013 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004014 break;
4015 }
James Smarted957682007-06-17 19:56:37 -05004016 mode = 2;
4017 break;
4018 case 0:
4019 case 3:
4020 break;
4021 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004023 "1819 Unrecognized lpfc_sli_mode "
4024 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004025
4026 break;
4027 }
4028
James Smart93996272008-08-24 21:50:30 -04004029 rc = lpfc_sli_config_port(phba, mode);
4030
James Smarted957682007-06-17 19:56:37 -05004031 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004033 "1820 Unable to select SLI-3. "
4034 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004035 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004036 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004037 if (rc)
dea31012005-04-17 16:05:31 -05004038 goto lpfc_sli_hba_setup_error;
4039
James Smart0d878412009-10-02 15:16:56 -04004040 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4041 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4042 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4043 if (!rc) {
4044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4045 "2709 This device supports "
4046 "Advanced Error Reporting (AER)\n");
4047 spin_lock_irq(&phba->hbalock);
4048 phba->hba_flag |= HBA_AER_ENABLED;
4049 spin_unlock_irq(&phba->hbalock);
4050 } else {
4051 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4052 "2708 This device does not support "
4053 "Advanced Error Reporting (AER)\n");
4054 phba->cfg_aer_support = 0;
4055 }
4056 }
4057
James Smarted957682007-06-17 19:56:37 -05004058 if (phba->sli_rev == 3) {
4059 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4060 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004061 } else {
4062 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4063 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004064 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004065 }
4066
4067 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004068 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4069 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004070 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004071
4072 if (rc)
4073 goto lpfc_sli_hba_setup_error;
4074
James Smart93996272008-08-24 21:50:30 -04004075 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004076 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4077 rc = lpfc_sli_hbq_setup(phba);
4078 if (rc)
4079 goto lpfc_sli_hba_setup_error;
4080 }
James Smart04c68492009-05-22 14:52:52 -04004081 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004082 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004083 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004084
4085 rc = lpfc_config_port_post(phba);
4086 if (rc)
4087 goto lpfc_sli_hba_setup_error;
4088
James Smarted957682007-06-17 19:56:37 -05004089 return rc;
4090
James Smart92d7f7b2007-06-17 19:56:38 -05004091lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004092 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004094 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004095 return rc;
4096}
4097
James Smartda0436e2009-05-22 14:51:39 -04004098/**
4099 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4100 * @phba: Pointer to HBA context object.
4101 * @mboxq: mailbox pointer.
4102 * This function issue a dump mailbox command to read config region
4103 * 23 and parse the records in the region and populate driver
4104 * data structure.
4105 **/
4106static int
4107lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4108 LPFC_MBOXQ_t *mboxq)
4109{
4110 struct lpfc_dmabuf *mp;
4111 struct lpfc_mqe *mqe;
4112 uint32_t data_length;
4113 int rc;
4114
4115 /* Program the default value of vlan_id and fc_map */
4116 phba->valid_vlan = 0;
4117 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4118 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4119 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4120
4121 mqe = &mboxq->u.mqe;
4122 if (lpfc_dump_fcoe_param(phba, mboxq))
4123 return -ENOMEM;
4124
4125 mp = (struct lpfc_dmabuf *) mboxq->context1;
4126 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4127
4128 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4129 "(%d):2571 Mailbox cmd x%x Status x%x "
4130 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4131 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4132 "CQ: x%x x%x x%x x%x\n",
4133 mboxq->vport ? mboxq->vport->vpi : 0,
4134 bf_get(lpfc_mqe_command, mqe),
4135 bf_get(lpfc_mqe_status, mqe),
4136 mqe->un.mb_words[0], mqe->un.mb_words[1],
4137 mqe->un.mb_words[2], mqe->un.mb_words[3],
4138 mqe->un.mb_words[4], mqe->un.mb_words[5],
4139 mqe->un.mb_words[6], mqe->un.mb_words[7],
4140 mqe->un.mb_words[8], mqe->un.mb_words[9],
4141 mqe->un.mb_words[10], mqe->un.mb_words[11],
4142 mqe->un.mb_words[12], mqe->un.mb_words[13],
4143 mqe->un.mb_words[14], mqe->un.mb_words[15],
4144 mqe->un.mb_words[16], mqe->un.mb_words[50],
4145 mboxq->mcqe.word0,
4146 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4147 mboxq->mcqe.trailer);
4148
4149 if (rc) {
4150 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4151 kfree(mp);
4152 return -EIO;
4153 }
4154 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004155 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004156 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4157 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004158 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004159 }
James Smartda0436e2009-05-22 14:51:39 -04004160
4161 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4163 kfree(mp);
4164 return 0;
4165}
4166
4167/**
4168 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4169 * @phba: pointer to lpfc hba data structure.
4170 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4171 * @vpd: pointer to the memory to hold resulting port vpd data.
4172 * @vpd_size: On input, the number of bytes allocated to @vpd.
4173 * On output, the number of data bytes in @vpd.
4174 *
4175 * This routine executes a READ_REV SLI4 mailbox command. In
4176 * addition, this routine gets the port vpd data.
4177 *
4178 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004179 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004180 * ENOMEM - could not allocated memory.
4181 **/
4182static int
4183lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4184 uint8_t *vpd, uint32_t *vpd_size)
4185{
4186 int rc = 0;
4187 uint32_t dma_size;
4188 struct lpfc_dmabuf *dmabuf;
4189 struct lpfc_mqe *mqe;
4190
4191 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4192 if (!dmabuf)
4193 return -ENOMEM;
4194
4195 /*
4196 * Get a DMA buffer for the vpd data resulting from the READ_REV
4197 * mailbox command.
4198 */
4199 dma_size = *vpd_size;
4200 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4201 dma_size,
4202 &dmabuf->phys,
4203 GFP_KERNEL);
4204 if (!dmabuf->virt) {
4205 kfree(dmabuf);
4206 return -ENOMEM;
4207 }
4208 memset(dmabuf->virt, 0, dma_size);
4209
4210 /*
4211 * The SLI4 implementation of READ_REV conflicts at word1,
4212 * bits 31:16 and SLI4 adds vpd functionality not present
4213 * in SLI3. This code corrects the conflicts.
4214 */
4215 lpfc_read_rev(phba, mboxq);
4216 mqe = &mboxq->u.mqe;
4217 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4218 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4219 mqe->un.read_rev.word1 &= 0x0000FFFF;
4220 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4221 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4222
4223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4224 if (rc) {
4225 dma_free_coherent(&phba->pcidev->dev, dma_size,
4226 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004227 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004228 return -EIO;
4229 }
4230
James Smartda0436e2009-05-22 14:51:39 -04004231 /*
4232 * The available vpd length cannot be bigger than the
4233 * DMA buffer passed to the port. Catch the less than
4234 * case and update the caller's size.
4235 */
4236 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4237 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4238
4239 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4240 dma_free_coherent(&phba->pcidev->dev, dma_size,
4241 dmabuf->virt, dmabuf->phys);
4242 kfree(dmabuf);
4243 return 0;
4244}
4245
4246/**
4247 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4248 * @phba: pointer to lpfc hba data structure.
4249 *
4250 * This routine is called to explicitly arm the SLI4 device's completion and
4251 * event queues
4252 **/
4253static void
4254lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4255{
4256 uint8_t fcp_eqidx;
4257
4258 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4259 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004260 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4261 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4262 LPFC_QUEUE_REARM);
4263 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4264 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4265 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4266 LPFC_QUEUE_REARM);
4267}
4268
4269/**
4270 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4271 * @phba: Pointer to HBA context object.
4272 *
4273 * This function is the main SLI4 device intialization PCI function. This
4274 * function is called by the HBA intialization code, HBA reset code and
4275 * HBA error attention handler code. Caller is not required to hold any
4276 * locks.
4277 **/
4278int
4279lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4280{
4281 int rc;
4282 LPFC_MBOXQ_t *mboxq;
4283 struct lpfc_mqe *mqe;
4284 uint8_t *vpd;
4285 uint32_t vpd_size;
4286 uint32_t ftr_rsp = 0;
4287 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4288 struct lpfc_vport *vport = phba->pport;
4289 struct lpfc_dmabuf *mp;
4290
4291 /* Perform a PCI function reset to start from clean */
4292 rc = lpfc_pci_function_reset(phba);
4293 if (unlikely(rc))
4294 return -ENODEV;
4295
4296 /* Check the HBA Host Status Register for readyness */
4297 rc = lpfc_sli4_post_status_check(phba);
4298 if (unlikely(rc))
4299 return -ENODEV;
4300 else {
4301 spin_lock_irq(&phba->hbalock);
4302 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4303 spin_unlock_irq(&phba->hbalock);
4304 }
4305
4306 /*
4307 * Allocate a single mailbox container for initializing the
4308 * port.
4309 */
4310 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4311 if (!mboxq)
4312 return -ENOMEM;
4313
4314 /*
4315 * Continue initialization with default values even if driver failed
4316 * to read FCoE param config regions
4317 */
4318 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4319 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004320 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004321
4322 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004323 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004324 vpd = kzalloc(vpd_size, GFP_KERNEL);
4325 if (!vpd) {
4326 rc = -ENOMEM;
4327 goto out_free_mbox;
4328 }
4329
4330 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4331 if (unlikely(rc))
4332 goto out_free_vpd;
4333
4334 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004335 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4336 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4337 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004338
4339 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4340 LPFC_DCBX_CEE_MODE)
4341 phba->hba_flag |= HBA_FIP_SUPPORT;
4342 else
4343 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4344
James Smartf1126682009-06-10 17:22:44 -04004345 if (phba->sli_rev != LPFC_SLI_REV4 ||
4346 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4348 "0376 READ_REV Error. SLI Level %d "
4349 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004350 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004351 rc = -EIO;
4352 goto out_free_vpd;
4353 }
James Smartda0436e2009-05-22 14:51:39 -04004354 /*
4355 * Evaluate the read rev and vpd data. Populate the driver
4356 * state with the results. If this routine fails, the failure
4357 * is not fatal as the driver will use generic values.
4358 */
4359 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4360 if (unlikely(!rc)) {
4361 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4362 "0377 Error %d parsing vpd. "
4363 "Using defaults.\n", rc);
4364 rc = 0;
4365 }
4366
James Smartf1126682009-06-10 17:22:44 -04004367 /* Save information as VPD data */
4368 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4369 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4370 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4371 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4372 &mqe->un.read_rev);
4373 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4374 &mqe->un.read_rev);
4375 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4376 &mqe->un.read_rev);
4377 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4378 &mqe->un.read_rev);
4379 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4380 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4381 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4382 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4383 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4384 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4385 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4386 "(%d):0380 READ_REV Status x%x "
4387 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4388 mboxq->vport ? mboxq->vport->vpi : 0,
4389 bf_get(lpfc_mqe_status, mqe),
4390 phba->vpd.rev.opFwName,
4391 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4392 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004393
4394 /*
4395 * Discover the port's supported feature set and match it against the
4396 * hosts requests.
4397 */
4398 lpfc_request_features(phba, mboxq);
4399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4400 if (unlikely(rc)) {
4401 rc = -EIO;
4402 goto out_free_vpd;
4403 }
4404
4405 /*
4406 * The port must support FCP initiator mode as this is the
4407 * only mode running in the host.
4408 */
4409 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4410 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4411 "0378 No support for fcpi mode.\n");
4412 ftr_rsp++;
4413 }
4414
4415 /*
4416 * If the port cannot support the host's requested features
4417 * then turn off the global config parameters to disable the
4418 * feature in the driver. This is not a fatal error.
4419 */
4420 if ((phba->cfg_enable_bg) &&
4421 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4422 ftr_rsp++;
4423
4424 if (phba->max_vpi && phba->cfg_enable_npiv &&
4425 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4426 ftr_rsp++;
4427
4428 if (ftr_rsp) {
4429 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4430 "0379 Feature Mismatch Data: x%08x %08x "
4431 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4432 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4433 phba->cfg_enable_npiv, phba->max_vpi);
4434 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4435 phba->cfg_enable_bg = 0;
4436 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4437 phba->cfg_enable_npiv = 0;
4438 }
4439
4440 /* These SLI3 features are assumed in SLI4 */
4441 spin_lock_irq(&phba->hbalock);
4442 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4443 spin_unlock_irq(&phba->hbalock);
4444
4445 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004446 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4447 if (rc) {
4448 phba->link_state = LPFC_HBA_ERROR;
4449 rc = -ENOMEM;
4450 goto out_free_vpd;
4451 }
4452
James Smartda0436e2009-05-22 14:51:39 -04004453 mboxq->vport = vport;
4454 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4455 mp = (struct lpfc_dmabuf *) mboxq->context1;
4456 if (rc == MBX_SUCCESS) {
4457 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4458 rc = 0;
4459 }
4460
4461 /*
4462 * This memory was allocated by the lpfc_read_sparam routine. Release
4463 * it to the mbuf pool.
4464 */
4465 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4466 kfree(mp);
4467 mboxq->context1 = NULL;
4468 if (unlikely(rc)) {
4469 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4470 "0382 READ_SPARAM command failed "
4471 "status %d, mbxStatus x%x\n",
4472 rc, bf_get(lpfc_mqe_status, mqe));
4473 phba->link_state = LPFC_HBA_ERROR;
4474 rc = -EIO;
4475 goto out_free_vpd;
4476 }
4477
4478 if (phba->cfg_soft_wwnn)
4479 u64_to_wwn(phba->cfg_soft_wwnn,
4480 vport->fc_sparam.nodeName.u.wwn);
4481 if (phba->cfg_soft_wwpn)
4482 u64_to_wwn(phba->cfg_soft_wwpn,
4483 vport->fc_sparam.portName.u.wwn);
4484 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4485 sizeof(struct lpfc_name));
4486 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4487 sizeof(struct lpfc_name));
4488
4489 /* Update the fc_host data structures with new wwn. */
4490 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4491 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4492
4493 /* Register SGL pool to the device using non-embedded mailbox command */
4494 rc = lpfc_sli4_post_sgl_list(phba);
4495 if (unlikely(rc)) {
4496 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004497 "0582 Error %d during sgl post operation\n",
4498 rc);
James Smartda0436e2009-05-22 14:51:39 -04004499 rc = -ENODEV;
4500 goto out_free_vpd;
4501 }
4502
4503 /* Register SCSI SGL pool to the device */
4504 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4505 if (unlikely(rc)) {
4506 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004507 "0383 Error %d during scsi sgl post "
4508 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004509 /* Some Scsi buffers were moved to the abort scsi list */
4510 /* A pci function reset will repost them */
4511 rc = -ENODEV;
4512 goto out_free_vpd;
4513 }
4514
4515 /* Post the rpi header region to the device. */
4516 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4517 if (unlikely(rc)) {
4518 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4519 "0393 Error %d during rpi post operation\n",
4520 rc);
4521 rc = -ENODEV;
4522 goto out_free_vpd;
4523 }
James Smartda0436e2009-05-22 14:51:39 -04004524
4525 /* Set up all the queues to the device */
4526 rc = lpfc_sli4_queue_setup(phba);
4527 if (unlikely(rc)) {
4528 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4529 "0381 Error %d during queue setup.\n ", rc);
4530 goto out_stop_timers;
4531 }
4532
4533 /* Arm the CQs and then EQs on device */
4534 lpfc_sli4_arm_cqeq_intr(phba);
4535
4536 /* Indicate device interrupt mode */
4537 phba->sli4_hba.intr_enable = 1;
4538
4539 /* Allow asynchronous mailbox command to go through */
4540 spin_lock_irq(&phba->hbalock);
4541 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4542 spin_unlock_irq(&phba->hbalock);
4543
4544 /* Post receive buffers to the device */
4545 lpfc_sli4_rb_setup(phba);
4546
James Smartfc2b9892010-02-26 14:15:29 -05004547 /* Reset HBA FCF states after HBA reset */
4548 phba->fcf.fcf_flag = 0;
4549 phba->fcf.current_rec.flag = 0;
4550
James Smartda0436e2009-05-22 14:51:39 -04004551 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004552 mod_timer(&vport->els_tmofunc,
4553 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004554
4555 /* Start heart beat timer */
4556 mod_timer(&phba->hb_tmofunc,
4557 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4558 phba->hb_outstanding = 0;
4559 phba->last_completion_time = jiffies;
4560
4561 /* Start error attention (ERATT) polling timer */
4562 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4563
James Smart75baf692010-06-08 18:31:21 -04004564 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4565 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4566 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4567 if (!rc) {
4568 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4569 "2829 This device supports "
4570 "Advanced Error Reporting (AER)\n");
4571 spin_lock_irq(&phba->hbalock);
4572 phba->hba_flag |= HBA_AER_ENABLED;
4573 spin_unlock_irq(&phba->hbalock);
4574 } else {
4575 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4576 "2830 This device does not support "
4577 "Advanced Error Reporting (AER)\n");
4578 phba->cfg_aer_support = 0;
4579 }
4580 }
4581
James Smartda0436e2009-05-22 14:51:39 -04004582 /*
4583 * The port is ready, set the host's link state to LINK_DOWN
4584 * in preparation for link interrupts.
4585 */
4586 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4587 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4588 lpfc_set_loopback_flag(phba);
4589 /* Change driver state to LPFC_LINK_DOWN right before init link */
4590 spin_lock_irq(&phba->hbalock);
4591 phba->link_state = LPFC_LINK_DOWN;
4592 spin_unlock_irq(&phba->hbalock);
4593 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4594 if (unlikely(rc != MBX_NOT_FINISHED)) {
4595 kfree(vpd);
4596 return 0;
4597 } else
4598 rc = -EIO;
4599
4600 /* Unset all the queues set up in this routine when error out */
4601 if (rc)
4602 lpfc_sli4_queue_unset(phba);
4603
4604out_stop_timers:
4605 if (rc)
4606 lpfc_stop_hba_timers(phba);
4607out_free_vpd:
4608 kfree(vpd);
4609out_free_mbox:
4610 mempool_free(mboxq, phba->mbox_mem_pool);
4611 return rc;
4612}
James Smarte59058c2008-08-24 21:49:00 -04004613
4614/**
James Smart3621a712009-04-06 18:47:14 -04004615 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004616 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004617 *
James Smarte59058c2008-08-24 21:49:00 -04004618 * This is the callback function for mailbox timer. The mailbox
4619 * timer is armed when a new mailbox command is issued and the timer
4620 * is deleted when the mailbox complete. The function is called by
4621 * the kernel timer code when a mailbox does not complete within
4622 * expected time. This function wakes up the worker thread to
4623 * process the mailbox timeout and returns. All the processing is
4624 * done by the worker thread function lpfc_mbox_timeout_handler.
4625 **/
dea31012005-04-17 16:05:31 -05004626void
4627lpfc_mbox_timeout(unsigned long ptr)
4628{
James Smart92d7f7b2007-06-17 19:56:38 -05004629 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004630 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004631 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004632
James Smart2e0fef82007-06-17 19:56:36 -05004633 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004634 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004635 if (!tmo_posted)
4636 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4637 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4638
James Smart5e9d9b82008-06-14 22:52:53 -04004639 if (!tmo_posted)
4640 lpfc_worker_wake_up(phba);
4641 return;
dea31012005-04-17 16:05:31 -05004642}
4643
James Smarte59058c2008-08-24 21:49:00 -04004644
4645/**
James Smart3621a712009-04-06 18:47:14 -04004646 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004647 * @phba: Pointer to HBA context object.
4648 *
4649 * This function is called from worker thread when a mailbox command times out.
4650 * The caller is not required to hold any locks. This function will reset the
4651 * HBA and recover all the pending commands.
4652 **/
dea31012005-04-17 16:05:31 -05004653void
4654lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4655{
James Smart2e0fef82007-06-17 19:56:36 -05004656 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004657 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004658 struct lpfc_sli *psli = &phba->sli;
4659 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004660
James Smarta257bf92009-04-06 18:48:10 -04004661 /* Check the pmbox pointer first. There is a race condition
4662 * between the mbox timeout handler getting executed in the
4663 * worklist and the mailbox actually completing. When this
4664 * race condition occurs, the mbox_active will be NULL.
4665 */
4666 spin_lock_irq(&phba->hbalock);
4667 if (pmbox == NULL) {
4668 lpfc_printf_log(phba, KERN_WARNING,
4669 LOG_MBOX | LOG_SLI,
4670 "0353 Active Mailbox cleared - mailbox timeout "
4671 "exiting\n");
4672 spin_unlock_irq(&phba->hbalock);
4673 return;
4674 }
4675
dea31012005-04-17 16:05:31 -05004676 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004677 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004678 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004679 mb->mbxCommand,
4680 phba->pport->port_state,
4681 phba->sli.sli_flag,
4682 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004683 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004684
James Smart1dcb58e2007-04-25 09:51:30 -04004685 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4686 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4687 * it to fail all oustanding SCSI IO.
4688 */
James Smart2e0fef82007-06-17 19:56:36 -05004689 spin_lock_irq(&phba->pport->work_port_lock);
4690 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4691 spin_unlock_irq(&phba->pport->work_port_lock);
4692 spin_lock_irq(&phba->hbalock);
4693 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004694 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004695 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004696
4697 pring = &psli->ring[psli->fcp_ring];
4698 lpfc_sli_abort_iocb_ring(phba, pring);
4699
4700 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004701 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004702
4703 /* Reset the HBA device */
4704 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004705}
4706
James Smarte59058c2008-08-24 21:49:00 -04004707/**
James Smart3772a992009-05-22 14:50:54 -04004708 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004709 * @phba: Pointer to HBA context object.
4710 * @pmbox: Pointer to mailbox object.
4711 * @flag: Flag indicating how the mailbox need to be processed.
4712 *
4713 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004714 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4715 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004716 * The mailbox command can be submitted in polling mode, in which case
4717 * this function will wait in a polling loop for the completion of the
4718 * mailbox.
4719 * If the mailbox is submitted in no_wait mode (not polling) the
4720 * function will submit the command and returns immediately without waiting
4721 * for the mailbox completion. The no_wait is supported only when HBA
4722 * is in SLI2/SLI3 mode - interrupts are enabled.
4723 * The SLI interface allows only one mailbox pending at a time. If the
4724 * mailbox is issued in polling mode and there is already a mailbox
4725 * pending, then the function will return an error. If the mailbox is issued
4726 * in NO_WAIT mode and there is a mailbox pending already, the function
4727 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4728 * The sli layer owns the mailbox object until the completion of mailbox
4729 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4730 * return codes the caller owns the mailbox command after the return of
4731 * the function.
4732 **/
James Smart3772a992009-05-22 14:50:54 -04004733static int
4734lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4735 uint32_t flag)
dea31012005-04-17 16:05:31 -05004736{
dea31012005-04-17 16:05:31 -05004737 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004738 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004739 uint32_t status, evtctr;
4740 uint32_t ha_copy;
4741 int i;
James Smart09372822008-01-11 01:52:54 -05004742 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004743 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004744 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004745 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004746 int processing_queue = 0;
4747
4748 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4749 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004750 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004751 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004752 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4753 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4754 return MBX_SUCCESS;
4755 }
James Smart58da1ff2008-04-07 10:15:56 -04004756 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004757 pmbox = lpfc_mbox_get(phba);
4758 if (!pmbox) {
4759 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4760 return MBX_SUCCESS;
4761 }
4762 }
dea31012005-04-17 16:05:31 -05004763
James Smarted957682007-06-17 19:56:37 -05004764 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004765 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004766 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004767 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004768 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004769 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004770 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004771 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004772 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004773 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004774 }
4775 }
4776
Linas Vepstas8d63f372007-02-14 14:28:36 -06004777 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004778 if (unlikely(pci_channel_offline(phba->pcidev))) {
4779 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4780 goto out_not_finished;
4781 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004782
James Smarta257bf92009-04-06 18:48:10 -04004783 /* If HBA has a deferred error attention, fail the iocb. */
4784 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4785 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4786 goto out_not_finished;
4787 }
4788
dea31012005-04-17 16:05:31 -05004789 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004790
James Smart3772a992009-05-22 14:50:54 -04004791 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004792 status = MBX_SUCCESS;
4793
James Smart2e0fef82007-06-17 19:56:36 -05004794 if (phba->link_state == LPFC_HBA_ERROR) {
4795 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004796
4797 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004798 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4799 "(%d):0311 Mailbox command x%x cannot "
4800 "issue Data: x%x x%x\n",
4801 pmbox->vport ? pmbox->vport->vpi : 0,
4802 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004803 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004804 }
4805
James Smart92908312006-03-07 15:04:13 -05004806 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4807 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004808 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4810 "(%d):2528 Mailbox command x%x cannot "
4811 "issue Data: x%x x%x\n",
4812 pmbox->vport ? pmbox->vport->vpi : 0,
4813 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004814 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004815 }
4816
dea31012005-04-17 16:05:31 -05004817 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4818 /* Polling for a mbox command when another one is already active
4819 * is not allowed in SLI. Also, the driver must have established
4820 * SLI2 mode to queue and process multiple mbox commands.
4821 */
4822
4823 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004825
4826 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4828 "(%d):2529 Mailbox command x%x "
4829 "cannot issue Data: x%x x%x\n",
4830 pmbox->vport ? pmbox->vport->vpi : 0,
4831 pmbox->u.mb.mbxCommand,
4832 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004833 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004834 }
4835
James Smart3772a992009-05-22 14:50:54 -04004836 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004838 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4840 "(%d):2530 Mailbox command x%x "
4841 "cannot issue Data: x%x x%x\n",
4842 pmbox->vport ? pmbox->vport->vpi : 0,
4843 pmbox->u.mb.mbxCommand,
4844 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004845 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004846 }
4847
dea31012005-04-17 16:05:31 -05004848 /* Another mailbox command is still being processed, queue this
4849 * command to be processed later.
4850 */
4851 lpfc_mbox_put(phba, pmbox);
4852
4853 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004854 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004855 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004856 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004857 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4858 mb->mbxCommand, phba->pport->port_state,
4859 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004860
4861 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004862 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004863
James Smart858c9f62007-06-17 19:56:39 -05004864 if (pmbox->vport) {
4865 lpfc_debugfs_disc_trc(pmbox->vport,
4866 LPFC_DISC_TRC_MBOX_VPORT,
4867 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4868 (uint32_t)mb->mbxCommand,
4869 mb->un.varWords[0], mb->un.varWords[1]);
4870 }
4871 else {
4872 lpfc_debugfs_disc_trc(phba->pport,
4873 LPFC_DISC_TRC_MBOX,
4874 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4875 (uint32_t)mb->mbxCommand,
4876 mb->un.varWords[0], mb->un.varWords[1]);
4877 }
4878
James Smart2e0fef82007-06-17 19:56:36 -05004879 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004880 }
4881
dea31012005-04-17 16:05:31 -05004882 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4883
4884 /* If we are not polling, we MUST be in SLI2 mode */
4885 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004886 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004887 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004888 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004889 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004890 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004891 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4892 "(%d):2531 Mailbox command x%x "
4893 "cannot issue Data: x%x x%x\n",
4894 pmbox->vport ? pmbox->vport->vpi : 0,
4895 pmbox->u.mb.mbxCommand,
4896 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004897 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004898 }
4899 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004900 mod_timer(&psli->mbox_tmo, (jiffies +
4901 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004902 }
4903
4904 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004905 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004906 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004907 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004908 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004909 mb->mbxCommand, phba->pport->port_state,
4910 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004911
James Smart858c9f62007-06-17 19:56:39 -05004912 if (mb->mbxCommand != MBX_HEARTBEAT) {
4913 if (pmbox->vport) {
4914 lpfc_debugfs_disc_trc(pmbox->vport,
4915 LPFC_DISC_TRC_MBOX_VPORT,
4916 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4917 (uint32_t)mb->mbxCommand,
4918 mb->un.varWords[0], mb->un.varWords[1]);
4919 }
4920 else {
4921 lpfc_debugfs_disc_trc(phba->pport,
4922 LPFC_DISC_TRC_MBOX,
4923 "MBOX Send: cmd:x%x mb:x%x x%x",
4924 (uint32_t)mb->mbxCommand,
4925 mb->un.varWords[0], mb->un.varWords[1]);
4926 }
4927 }
4928
dea31012005-04-17 16:05:31 -05004929 psli->slistat.mbox_cmd++;
4930 evtctr = psli->slistat.mbox_event;
4931
4932 /* next set own bit for the adapter and copy over command word */
4933 mb->mbxOwner = OWN_CHIP;
4934
James Smart3772a992009-05-22 14:50:54 -04004935 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004936 /* Populate mbox extension offset word. */
4937 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4938 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4939 = (uint8_t *)phba->mbox_ext
4940 - (uint8_t *)phba->mbox;
4941 }
4942
4943 /* Copy the mailbox extension data */
4944 if (pmbox->in_ext_byte_len && pmbox->context2) {
4945 lpfc_sli_pcimem_bcopy(pmbox->context2,
4946 (uint8_t *)phba->mbox_ext,
4947 pmbox->in_ext_byte_len);
4948 }
4949 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004950 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004951 } else {
James Smart7a470272010-03-15 11:25:20 -04004952 /* Populate mbox extension offset word. */
4953 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4954 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4955 = MAILBOX_HBA_EXT_OFFSET;
4956
4957 /* Copy the mailbox extension data */
4958 if (pmbox->in_ext_byte_len && pmbox->context2) {
4959 lpfc_memcpy_to_slim(phba->MBslimaddr +
4960 MAILBOX_HBA_EXT_OFFSET,
4961 pmbox->context2, pmbox->in_ext_byte_len);
4962
4963 }
James Smart92908312006-03-07 15:04:13 -05004964 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004965 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004966 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004967 }
4968
4969 /* First copy mbox command data to HBA SLIM, skip past first
4970 word */
4971 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4972 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4973 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4974
4975 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004976 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004977 to_slim = phba->MBslimaddr;
4978 writel(ldata, to_slim);
4979 readl(to_slim); /* flush */
4980
4981 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4982 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004983 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004984 }
4985 }
4986
4987 wmb();
dea31012005-04-17 16:05:31 -05004988
4989 switch (flag) {
4990 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004991 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004992 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004993 /* Interrupt board to do it */
4994 writel(CA_MBATT, phba->CAregaddr);
4995 readl(phba->CAregaddr); /* flush */
4996 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004997 break;
4998
4999 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005000 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005001 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005002 /* Interrupt board to do it */
5003 writel(CA_MBATT, phba->CAregaddr);
5004 readl(phba->CAregaddr); /* flush */
5005
James Smart3772a992009-05-22 14:50:54 -04005006 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005007 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005008 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005009 word0 = le32_to_cpu(word0);
5010 } else {
5011 /* First read mbox status word */
5012 word0 = readl(phba->MBslimaddr);
5013 }
5014
5015 /* Read the HBA Host Attention Register */
5016 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005017 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5018 mb->mbxCommand) *
5019 1000) + jiffies;
5020 i = 0;
dea31012005-04-17 16:05:31 -05005021 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005022 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5023 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005024 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005025 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005026 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005027 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005028 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005029 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005030 }
5031
5032 /* Check if we took a mbox interrupt while we were
5033 polling */
5034 if (((word0 & OWN_CHIP) != OWN_CHIP)
5035 && (evtctr != psli->slistat.mbox_event))
5036 break;
5037
James Smart09372822008-01-11 01:52:54 -05005038 if (i++ > 10) {
5039 spin_unlock_irqrestore(&phba->hbalock,
5040 drvr_flag);
5041 msleep(1);
5042 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5043 }
dea31012005-04-17 16:05:31 -05005044
James Smart3772a992009-05-22 14:50:54 -04005045 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005046 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005047 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005048 word0 = le32_to_cpu(word0);
5049 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5050 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005051 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005052 /* Check real SLIM for any errors */
5053 slimword0 = readl(phba->MBslimaddr);
5054 slimmb = (MAILBOX_t *) & slimword0;
5055 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5056 && slimmb->mbxStatus) {
5057 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005058 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005059 word0 = slimword0;
5060 }
5061 }
5062 } else {
5063 /* First copy command data */
5064 word0 = readl(phba->MBslimaddr);
5065 }
5066 /* Read the HBA Host Attention Register */
5067 ha_copy = readl(phba->HAregaddr);
5068 }
5069
James Smart3772a992009-05-22 14:50:54 -04005070 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005071 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005072 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005073 /* Copy the mailbox extension data */
5074 if (pmbox->out_ext_byte_len && pmbox->context2) {
5075 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5076 pmbox->context2,
5077 pmbox->out_ext_byte_len);
5078 }
dea31012005-04-17 16:05:31 -05005079 } else {
5080 /* First copy command data */
5081 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5082 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005083 /* Copy the mailbox extension data */
5084 if (pmbox->out_ext_byte_len && pmbox->context2) {
5085 lpfc_memcpy_from_slim(pmbox->context2,
5086 phba->MBslimaddr +
5087 MAILBOX_HBA_EXT_OFFSET,
5088 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005089 }
5090 }
5091
5092 writel(HA_MBATT, phba->HAregaddr);
5093 readl(phba->HAregaddr); /* flush */
5094
5095 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5096 status = mb->mbxStatus;
5097 }
5098
James Smart2e0fef82007-06-17 19:56:36 -05005099 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5100 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005101
5102out_not_finished:
5103 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005104 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005105 lpfc_mbox_cmpl_put(phba, pmbox);
5106 }
5107 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005108}
5109
James Smarte59058c2008-08-24 21:49:00 -04005110/**
James Smartf1126682009-06-10 17:22:44 -04005111 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5112 * @phba: Pointer to HBA context object.
5113 *
5114 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5115 * the driver internal pending mailbox queue. It will then try to wait out the
5116 * possible outstanding mailbox command before return.
5117 *
5118 * Returns:
5119 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5120 * the outstanding mailbox command timed out.
5121 **/
5122static int
5123lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5124{
5125 struct lpfc_sli *psli = &phba->sli;
5126 uint8_t actcmd = MBX_HEARTBEAT;
5127 int rc = 0;
5128 unsigned long timeout;
5129
5130 /* Mark the asynchronous mailbox command posting as blocked */
5131 spin_lock_irq(&phba->hbalock);
5132 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5133 if (phba->sli.mbox_active)
5134 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5135 spin_unlock_irq(&phba->hbalock);
5136 /* Determine how long we might wait for the active mailbox
5137 * command to be gracefully completed by firmware.
5138 */
5139 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5140 jiffies;
5141 /* Wait for the outstnading mailbox command to complete */
5142 while (phba->sli.mbox_active) {
5143 /* Check active mailbox complete status every 2ms */
5144 msleep(2);
5145 if (time_after(jiffies, timeout)) {
5146 /* Timeout, marked the outstanding cmd not complete */
5147 rc = 1;
5148 break;
5149 }
5150 }
5151
5152 /* Can not cleanly block async mailbox command, fails it */
5153 if (rc) {
5154 spin_lock_irq(&phba->hbalock);
5155 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5156 spin_unlock_irq(&phba->hbalock);
5157 }
5158 return rc;
5159}
5160
5161/**
5162 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5163 * @phba: Pointer to HBA context object.
5164 *
5165 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5166 * commands from the driver internal pending mailbox queue. It makes sure
5167 * that there is no outstanding mailbox command before resuming posting
5168 * asynchronous mailbox commands. If, for any reason, there is outstanding
5169 * mailbox command, it will try to wait it out before resuming asynchronous
5170 * mailbox command posting.
5171 **/
5172static void
5173lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5174{
5175 struct lpfc_sli *psli = &phba->sli;
5176
5177 spin_lock_irq(&phba->hbalock);
5178 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5179 /* Asynchronous mailbox posting is not blocked, do nothing */
5180 spin_unlock_irq(&phba->hbalock);
5181 return;
5182 }
5183
5184 /* Outstanding synchronous mailbox command is guaranteed to be done,
5185 * successful or timeout, after timing-out the outstanding mailbox
5186 * command shall always be removed, so just unblock posting async
5187 * mailbox command and resume
5188 */
5189 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5190 spin_unlock_irq(&phba->hbalock);
5191
5192 /* wake up worker thread to post asynchronlous mailbox command */
5193 lpfc_worker_wake_up(phba);
5194}
5195
5196/**
James Smartda0436e2009-05-22 14:51:39 -04005197 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5198 * @phba: Pointer to HBA context object.
5199 * @mboxq: Pointer to mailbox object.
5200 *
5201 * The function posts a mailbox to the port. The mailbox is expected
5202 * to be comletely filled in and ready for the port to operate on it.
5203 * This routine executes a synchronous completion operation on the
5204 * mailbox by polling for its completion.
5205 *
5206 * The caller must not be holding any locks when calling this routine.
5207 *
5208 * Returns:
5209 * MBX_SUCCESS - mailbox posted successfully
5210 * Any of the MBX error values.
5211 **/
5212static int
5213lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5214{
5215 int rc = MBX_SUCCESS;
5216 unsigned long iflag;
5217 uint32_t db_ready;
5218 uint32_t mcqe_status;
5219 uint32_t mbx_cmnd;
5220 unsigned long timeout;
5221 struct lpfc_sli *psli = &phba->sli;
5222 struct lpfc_mqe *mb = &mboxq->u.mqe;
5223 struct lpfc_bmbx_create *mbox_rgn;
5224 struct dma_address *dma_address;
5225 struct lpfc_register bmbx_reg;
5226
5227 /*
5228 * Only one mailbox can be active to the bootstrap mailbox region
5229 * at a time and there is no queueing provided.
5230 */
5231 spin_lock_irqsave(&phba->hbalock, iflag);
5232 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5233 spin_unlock_irqrestore(&phba->hbalock, iflag);
5234 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5235 "(%d):2532 Mailbox command x%x (x%x) "
5236 "cannot issue Data: x%x x%x\n",
5237 mboxq->vport ? mboxq->vport->vpi : 0,
5238 mboxq->u.mb.mbxCommand,
5239 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5240 psli->sli_flag, MBX_POLL);
5241 return MBXERR_ERROR;
5242 }
5243 /* The server grabs the token and owns it until release */
5244 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5245 phba->sli.mbox_active = mboxq;
5246 spin_unlock_irqrestore(&phba->hbalock, iflag);
5247
5248 /*
5249 * Initialize the bootstrap memory region to avoid stale data areas
5250 * in the mailbox post. Then copy the caller's mailbox contents to
5251 * the bmbx mailbox region.
5252 */
5253 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5254 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5255 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5256 sizeof(struct lpfc_mqe));
5257
5258 /* Post the high mailbox dma address to the port and wait for ready. */
5259 dma_address = &phba->sli4_hba.bmbx.dma_address;
5260 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5261
5262 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5263 * 1000) + jiffies;
5264 do {
5265 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5266 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5267 if (!db_ready)
5268 msleep(2);
5269
5270 if (time_after(jiffies, timeout)) {
5271 rc = MBXERR_ERROR;
5272 goto exit;
5273 }
5274 } while (!db_ready);
5275
5276 /* Post the low mailbox dma address to the port. */
5277 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5278 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5279 * 1000) + jiffies;
5280 do {
5281 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5282 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5283 if (!db_ready)
5284 msleep(2);
5285
5286 if (time_after(jiffies, timeout)) {
5287 rc = MBXERR_ERROR;
5288 goto exit;
5289 }
5290 } while (!db_ready);
5291
5292 /*
5293 * Read the CQ to ensure the mailbox has completed.
5294 * If so, update the mailbox status so that the upper layers
5295 * can complete the request normally.
5296 */
5297 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5298 sizeof(struct lpfc_mqe));
5299 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5300 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5301 sizeof(struct lpfc_mcqe));
5302 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5303
5304 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5305 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5306 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5307 rc = MBXERR_ERROR;
5308 }
5309
5310 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5311 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5312 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5313 " x%x x%x CQ: x%x x%x x%x x%x\n",
5314 mboxq->vport ? mboxq->vport->vpi : 0,
5315 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5316 bf_get(lpfc_mqe_status, mb),
5317 mb->un.mb_words[0], mb->un.mb_words[1],
5318 mb->un.mb_words[2], mb->un.mb_words[3],
5319 mb->un.mb_words[4], mb->un.mb_words[5],
5320 mb->un.mb_words[6], mb->un.mb_words[7],
5321 mb->un.mb_words[8], mb->un.mb_words[9],
5322 mb->un.mb_words[10], mb->un.mb_words[11],
5323 mb->un.mb_words[12], mboxq->mcqe.word0,
5324 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5325 mboxq->mcqe.trailer);
5326exit:
5327 /* We are holding the token, no needed for lock when release */
5328 spin_lock_irqsave(&phba->hbalock, iflag);
5329 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5330 phba->sli.mbox_active = NULL;
5331 spin_unlock_irqrestore(&phba->hbalock, iflag);
5332 return rc;
5333}
5334
5335/**
5336 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5337 * @phba: Pointer to HBA context object.
5338 * @pmbox: Pointer to mailbox object.
5339 * @flag: Flag indicating how the mailbox need to be processed.
5340 *
5341 * This function is called by discovery code and HBA management code to submit
5342 * a mailbox command to firmware with SLI-4 interface spec.
5343 *
5344 * Return codes the caller owns the mailbox command after the return of the
5345 * function.
5346 **/
5347static int
5348lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5349 uint32_t flag)
5350{
5351 struct lpfc_sli *psli = &phba->sli;
5352 unsigned long iflags;
5353 int rc;
5354
James Smart8fa38512009-07-19 10:01:03 -04005355 rc = lpfc_mbox_dev_check(phba);
5356 if (unlikely(rc)) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5358 "(%d):2544 Mailbox command x%x (x%x) "
5359 "cannot issue Data: x%x x%x\n",
5360 mboxq->vport ? mboxq->vport->vpi : 0,
5361 mboxq->u.mb.mbxCommand,
5362 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5363 psli->sli_flag, flag);
5364 goto out_not_finished;
5365 }
5366
James Smartda0436e2009-05-22 14:51:39 -04005367 /* Detect polling mode and jump to a handler */
5368 if (!phba->sli4_hba.intr_enable) {
5369 if (flag == MBX_POLL)
5370 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5371 else
5372 rc = -EIO;
5373 if (rc != MBX_SUCCESS)
5374 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5375 "(%d):2541 Mailbox command x%x "
5376 "(x%x) cannot issue Data: x%x x%x\n",
5377 mboxq->vport ? mboxq->vport->vpi : 0,
5378 mboxq->u.mb.mbxCommand,
5379 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5380 psli->sli_flag, flag);
5381 return rc;
5382 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005383 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5384 "(%d):2542 Try to issue mailbox command "
5385 "x%x (x%x) synchronously ahead of async"
5386 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005387 mboxq->vport ? mboxq->vport->vpi : 0,
5388 mboxq->u.mb.mbxCommand,
5389 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5390 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005391 /* Try to block the asynchronous mailbox posting */
5392 rc = lpfc_sli4_async_mbox_block(phba);
5393 if (!rc) {
5394 /* Successfully blocked, now issue sync mbox cmd */
5395 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5396 if (rc != MBX_SUCCESS)
5397 lpfc_printf_log(phba, KERN_ERR,
5398 LOG_MBOX | LOG_SLI,
5399 "(%d):2597 Mailbox command "
5400 "x%x (x%x) cannot issue "
5401 "Data: x%x x%x\n",
5402 mboxq->vport ?
5403 mboxq->vport->vpi : 0,
5404 mboxq->u.mb.mbxCommand,
5405 lpfc_sli4_mbox_opcode_get(phba,
5406 mboxq),
5407 psli->sli_flag, flag);
5408 /* Unblock the async mailbox posting afterward */
5409 lpfc_sli4_async_mbox_unblock(phba);
5410 }
5411 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005412 }
5413
5414 /* Now, interrupt mode asynchrous mailbox command */
5415 rc = lpfc_mbox_cmd_check(phba, mboxq);
5416 if (rc) {
5417 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5418 "(%d):2543 Mailbox command x%x (x%x) "
5419 "cannot issue Data: x%x x%x\n",
5420 mboxq->vport ? mboxq->vport->vpi : 0,
5421 mboxq->u.mb.mbxCommand,
5422 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5423 psli->sli_flag, flag);
5424 goto out_not_finished;
5425 }
James Smartda0436e2009-05-22 14:51:39 -04005426
5427 /* Put the mailbox command to the driver internal FIFO */
5428 psli->slistat.mbox_busy++;
5429 spin_lock_irqsave(&phba->hbalock, iflags);
5430 lpfc_mbox_put(phba, mboxq);
5431 spin_unlock_irqrestore(&phba->hbalock, iflags);
5432 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5433 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5434 "x%x (x%x) x%x x%x x%x\n",
5435 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5436 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5437 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5438 phba->pport->port_state,
5439 psli->sli_flag, MBX_NOWAIT);
5440 /* Wake up worker thread to transport mailbox command from head */
5441 lpfc_worker_wake_up(phba);
5442
5443 return MBX_BUSY;
5444
5445out_not_finished:
5446 return MBX_NOT_FINISHED;
5447}
5448
5449/**
5450 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5451 * @phba: Pointer to HBA context object.
5452 *
5453 * This function is called by worker thread to send a mailbox command to
5454 * SLI4 HBA firmware.
5455 *
5456 **/
5457int
5458lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5459{
5460 struct lpfc_sli *psli = &phba->sli;
5461 LPFC_MBOXQ_t *mboxq;
5462 int rc = MBX_SUCCESS;
5463 unsigned long iflags;
5464 struct lpfc_mqe *mqe;
5465 uint32_t mbx_cmnd;
5466
5467 /* Check interrupt mode before post async mailbox command */
5468 if (unlikely(!phba->sli4_hba.intr_enable))
5469 return MBX_NOT_FINISHED;
5470
5471 /* Check for mailbox command service token */
5472 spin_lock_irqsave(&phba->hbalock, iflags);
5473 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5474 spin_unlock_irqrestore(&phba->hbalock, iflags);
5475 return MBX_NOT_FINISHED;
5476 }
5477 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5478 spin_unlock_irqrestore(&phba->hbalock, iflags);
5479 return MBX_NOT_FINISHED;
5480 }
5481 if (unlikely(phba->sli.mbox_active)) {
5482 spin_unlock_irqrestore(&phba->hbalock, iflags);
5483 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5484 "0384 There is pending active mailbox cmd\n");
5485 return MBX_NOT_FINISHED;
5486 }
5487 /* Take the mailbox command service token */
5488 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5489
5490 /* Get the next mailbox command from head of queue */
5491 mboxq = lpfc_mbox_get(phba);
5492
5493 /* If no more mailbox command waiting for post, we're done */
5494 if (!mboxq) {
5495 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5496 spin_unlock_irqrestore(&phba->hbalock, iflags);
5497 return MBX_SUCCESS;
5498 }
5499 phba->sli.mbox_active = mboxq;
5500 spin_unlock_irqrestore(&phba->hbalock, iflags);
5501
5502 /* Check device readiness for posting mailbox command */
5503 rc = lpfc_mbox_dev_check(phba);
5504 if (unlikely(rc))
5505 /* Driver clean routine will clean up pending mailbox */
5506 goto out_not_finished;
5507
5508 /* Prepare the mbox command to be posted */
5509 mqe = &mboxq->u.mqe;
5510 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5511
5512 /* Start timer for the mbox_tmo and log some mailbox post messages */
5513 mod_timer(&psli->mbox_tmo, (jiffies +
5514 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5515
5516 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5517 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5518 "x%x x%x\n",
5519 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5520 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5521 phba->pport->port_state, psli->sli_flag);
5522
5523 if (mbx_cmnd != MBX_HEARTBEAT) {
5524 if (mboxq->vport) {
5525 lpfc_debugfs_disc_trc(mboxq->vport,
5526 LPFC_DISC_TRC_MBOX_VPORT,
5527 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5528 mbx_cmnd, mqe->un.mb_words[0],
5529 mqe->un.mb_words[1]);
5530 } else {
5531 lpfc_debugfs_disc_trc(phba->pport,
5532 LPFC_DISC_TRC_MBOX,
5533 "MBOX Send: cmd:x%x mb:x%x x%x",
5534 mbx_cmnd, mqe->un.mb_words[0],
5535 mqe->un.mb_words[1]);
5536 }
5537 }
5538 psli->slistat.mbox_cmd++;
5539
5540 /* Post the mailbox command to the port */
5541 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5542 if (rc != MBX_SUCCESS) {
5543 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5544 "(%d):2533 Mailbox command x%x (x%x) "
5545 "cannot issue Data: x%x x%x\n",
5546 mboxq->vport ? mboxq->vport->vpi : 0,
5547 mboxq->u.mb.mbxCommand,
5548 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5549 psli->sli_flag, MBX_NOWAIT);
5550 goto out_not_finished;
5551 }
5552
5553 return rc;
5554
5555out_not_finished:
5556 spin_lock_irqsave(&phba->hbalock, iflags);
5557 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5558 __lpfc_mbox_cmpl_put(phba, mboxq);
5559 /* Release the token */
5560 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5561 phba->sli.mbox_active = NULL;
5562 spin_unlock_irqrestore(&phba->hbalock, iflags);
5563
5564 return MBX_NOT_FINISHED;
5565}
5566
5567/**
5568 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5569 * @phba: Pointer to HBA context object.
5570 * @pmbox: Pointer to mailbox object.
5571 * @flag: Flag indicating how the mailbox need to be processed.
5572 *
5573 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5574 * the API jump table function pointer from the lpfc_hba struct.
5575 *
5576 * Return codes the caller owns the mailbox command after the return of the
5577 * function.
5578 **/
5579int
5580lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5581{
5582 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5583}
5584
5585/**
5586 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5587 * @phba: The hba struct for which this call is being executed.
5588 * @dev_grp: The HBA PCI-Device group number.
5589 *
5590 * This routine sets up the mbox interface API function jump table in @phba
5591 * struct.
5592 * Returns: 0 - success, -ENODEV - failure.
5593 **/
5594int
5595lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5596{
5597
5598 switch (dev_grp) {
5599 case LPFC_PCI_DEV_LP:
5600 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5601 phba->lpfc_sli_handle_slow_ring_event =
5602 lpfc_sli_handle_slow_ring_event_s3;
5603 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5604 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5605 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5606 break;
5607 case LPFC_PCI_DEV_OC:
5608 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5609 phba->lpfc_sli_handle_slow_ring_event =
5610 lpfc_sli_handle_slow_ring_event_s4;
5611 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5612 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5613 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5614 break;
5615 default:
5616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5617 "1420 Invalid HBA PCI-device group: 0x%x\n",
5618 dev_grp);
5619 return -ENODEV;
5620 break;
5621 }
5622 return 0;
5623}
5624
5625/**
James Smart3621a712009-04-06 18:47:14 -04005626 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005627 * @phba: Pointer to HBA context object.
5628 * @pring: Pointer to driver SLI ring object.
5629 * @piocb: Pointer to address of newly added command iocb.
5630 *
5631 * This function is called with hbalock held to add a command
5632 * iocb to the txq when SLI layer cannot submit the command iocb
5633 * to the ring.
5634 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04005635void
James Smart92d7f7b2007-06-17 19:56:38 -05005636__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005637 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005638{
5639 /* Insert the caller's iocb in the txq tail for later processing. */
5640 list_add_tail(&piocb->list, &pring->txq);
5641 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005642}
5643
James Smarte59058c2008-08-24 21:49:00 -04005644/**
James Smart3621a712009-04-06 18:47:14 -04005645 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005646 * @phba: Pointer to HBA context object.
5647 * @pring: Pointer to driver SLI ring object.
5648 * @piocb: Pointer to address of newly added command iocb.
5649 *
5650 * This function is called with hbalock held before a new
5651 * iocb is submitted to the firmware. This function checks
5652 * txq to flush the iocbs in txq to Firmware before
5653 * submitting new iocbs to the Firmware.
5654 * If there are iocbs in the txq which need to be submitted
5655 * to firmware, lpfc_sli_next_iocb returns the first element
5656 * of the txq after dequeuing it from txq.
5657 * If there is no iocb in the txq then the function will return
5658 * *piocb and *piocb is set to NULL. Caller needs to check
5659 * *piocb to find if there are more commands in the txq.
5660 **/
dea31012005-04-17 16:05:31 -05005661static struct lpfc_iocbq *
5662lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005663 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005664{
5665 struct lpfc_iocbq * nextiocb;
5666
5667 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5668 if (!nextiocb) {
5669 nextiocb = *piocb;
5670 *piocb = NULL;
5671 }
5672
5673 return nextiocb;
5674}
5675
James Smarte59058c2008-08-24 21:49:00 -04005676/**
James Smart3772a992009-05-22 14:50:54 -04005677 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005678 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005679 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005680 * @piocb: Pointer to command iocb.
5681 * @flag: Flag indicating if this command can be put into txq.
5682 *
James Smart3772a992009-05-22 14:50:54 -04005683 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5684 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5685 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5686 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5687 * this function allows only iocbs for posting buffers. This function finds
5688 * next available slot in the command ring and posts the command to the
5689 * available slot and writes the port attention register to request HBA start
5690 * processing new iocb. If there is no slot available in the ring and
5691 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5692 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005693 *
James Smart3772a992009-05-22 14:50:54 -04005694 * This function is called with hbalock held. The function will return success
5695 * after it successfully submit the iocb to firmware or after adding to the
5696 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005697 **/
James Smart98c9ea52007-10-27 13:37:33 -04005698static int
James Smart3772a992009-05-22 14:50:54 -04005699__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005700 struct lpfc_iocbq *piocb, uint32_t flag)
5701{
5702 struct lpfc_iocbq *nextiocb;
5703 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005704 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005705
James Smart92d7f7b2007-06-17 19:56:38 -05005706 if (piocb->iocb_cmpl && (!piocb->vport) &&
5707 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5708 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5709 lpfc_printf_log(phba, KERN_ERR,
5710 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005711 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005712 piocb->iocb.ulpCommand);
5713 dump_stack();
5714 return IOCB_ERROR;
5715 }
5716
5717
Linas Vepstas8d63f372007-02-14 14:28:36 -06005718 /* If the PCI channel is in offline state, do not post iocbs. */
5719 if (unlikely(pci_channel_offline(phba->pcidev)))
5720 return IOCB_ERROR;
5721
James Smarta257bf92009-04-06 18:48:10 -04005722 /* If HBA has a deferred error attention, fail the iocb. */
5723 if (unlikely(phba->hba_flag & DEFER_ERATT))
5724 return IOCB_ERROR;
5725
dea31012005-04-17 16:05:31 -05005726 /*
5727 * We should never get an IOCB if we are in a < LINK_DOWN state
5728 */
James Smart2e0fef82007-06-17 19:56:36 -05005729 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005730 return IOCB_ERROR;
5731
5732 /*
5733 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005734 * outstanding event.
dea31012005-04-17 16:05:31 -05005735 */
James Smart0b727fe2007-10-27 13:37:25 -04005736 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005737 goto iocb_busy;
5738
James Smart2e0fef82007-06-17 19:56:36 -05005739 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005740 /*
James Smart2680eea2007-04-25 09:52:55 -04005741 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005742 * can be issued if the link is not up.
5743 */
5744 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005745 case CMD_GEN_REQUEST64_CR:
5746 case CMD_GEN_REQUEST64_CX:
5747 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5748 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005749 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005750 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5751 MENLO_TRANSPORT_TYPE))
5752
5753 goto iocb_busy;
5754 break;
dea31012005-04-17 16:05:31 -05005755 case CMD_QUE_RING_BUF_CN:
5756 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005757 /*
5758 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5759 * completion, iocb_cmpl MUST be 0.
5760 */
5761 if (piocb->iocb_cmpl)
5762 piocb->iocb_cmpl = NULL;
5763 /*FALLTHROUGH*/
5764 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005765 case CMD_CLOSE_XRI_CN:
5766 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005767 break;
5768 default:
5769 goto iocb_busy;
5770 }
5771
5772 /*
5773 * For FCP commands, we must be in a state where we can process link
5774 * attention events.
5775 */
5776 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005777 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005778 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005779 }
dea31012005-04-17 16:05:31 -05005780
dea31012005-04-17 16:05:31 -05005781 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5782 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5783 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5784
5785 if (iocb)
5786 lpfc_sli_update_ring(phba, pring);
5787 else
5788 lpfc_sli_update_full_ring(phba, pring);
5789
5790 if (!piocb)
5791 return IOCB_SUCCESS;
5792
5793 goto out_busy;
5794
5795 iocb_busy:
5796 pring->stats.iocb_cmd_delay++;
5797
5798 out_busy:
5799
5800 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005801 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005802 return IOCB_SUCCESS;
5803 }
5804
5805 return IOCB_BUSY;
5806}
5807
James Smart3772a992009-05-22 14:50:54 -04005808/**
James Smart4f774512009-05-22 14:52:35 -04005809 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5810 * @phba: Pointer to HBA context object.
5811 * @piocb: Pointer to command iocb.
5812 * @sglq: Pointer to the scatter gather queue object.
5813 *
5814 * This routine converts the bpl or bde that is in the IOCB
5815 * to a sgl list for the sli4 hardware. The physical address
5816 * of the bpl/bde is converted back to a virtual address.
5817 * If the IOCB contains a BPL then the list of BDE's is
5818 * converted to sli4_sge's. If the IOCB contains a single
5819 * BDE then it is converted to a single sli_sge.
5820 * The IOCB is still in cpu endianess so the contents of
5821 * the bpl can be used without byte swapping.
5822 *
5823 * Returns valid XRI = Success, NO_XRI = Failure.
5824**/
5825static uint16_t
5826lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5827 struct lpfc_sglq *sglq)
5828{
5829 uint16_t xritag = NO_XRI;
5830 struct ulp_bde64 *bpl = NULL;
5831 struct ulp_bde64 bde;
5832 struct sli4_sge *sgl = NULL;
5833 IOCB_t *icmd;
5834 int numBdes = 0;
5835 int i = 0;
5836
5837 if (!piocbq || !sglq)
5838 return xritag;
5839
5840 sgl = (struct sli4_sge *)sglq->sgl;
5841 icmd = &piocbq->iocb;
5842 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5843 numBdes = icmd->un.genreq64.bdl.bdeSize /
5844 sizeof(struct ulp_bde64);
5845 /* The addrHigh and addrLow fields within the IOCB
5846 * have not been byteswapped yet so there is no
5847 * need to swap them back.
5848 */
5849 bpl = (struct ulp_bde64 *)
5850 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5851
5852 if (!bpl)
5853 return xritag;
5854
5855 for (i = 0; i < numBdes; i++) {
5856 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005857 sgl->addr_hi = bpl->addrHigh;
5858 sgl->addr_lo = bpl->addrLow;
5859
James Smart4f774512009-05-22 14:52:35 -04005860 if ((i+1) == numBdes)
5861 bf_set(lpfc_sli4_sge_last, sgl, 1);
5862 else
5863 bf_set(lpfc_sli4_sge_last, sgl, 0);
5864 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005865 /* swap the size field back to the cpu so we
5866 * can assign it to the sgl.
5867 */
5868 bde.tus.w = le32_to_cpu(bpl->tus.w);
5869 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005870 bpl++;
5871 sgl++;
5872 }
5873 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5874 /* The addrHigh and addrLow fields of the BDE have not
5875 * been byteswapped yet so they need to be swapped
5876 * before putting them in the sgl.
5877 */
5878 sgl->addr_hi =
5879 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5880 sgl->addr_lo =
5881 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005882 bf_set(lpfc_sli4_sge_last, sgl, 1);
5883 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005884 sgl->sge_len =
5885 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005886 }
5887 return sglq->sli4_xritag;
5888}
5889
5890/**
5891 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5892 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005893 *
5894 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005895 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5896 * held.
James Smart4f774512009-05-22 14:52:35 -04005897 *
5898 * Return: index into SLI4 fast-path FCP queue index.
5899 **/
5900static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005901lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005902{
James Smart8fa38512009-07-19 10:01:03 -04005903 ++phba->fcp_qidx;
5904 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5905 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005906
James Smart8fa38512009-07-19 10:01:03 -04005907 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005908}
5909
5910/**
5911 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5912 * @phba: Pointer to HBA context object.
5913 * @piocb: Pointer to command iocb.
5914 * @wqe: Pointer to the work queue entry.
5915 *
5916 * This routine converts the iocb command to its Work Queue Entry
5917 * equivalent. The wqe pointer should not have any fields set when
5918 * this routine is called because it will memcpy over them.
5919 * This routine does not set the CQ_ID or the WQEC bits in the
5920 * wqe.
5921 *
5922 * Returns: 0 = Success, IOCB_ERROR = Failure.
5923 **/
5924static int
5925lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5926 union lpfc_wqe *wqe)
5927{
James Smart5ffc2662009-11-18 15:39:44 -05005928 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005929 uint8_t ct = 0;
5930 uint32_t fip;
5931 uint32_t abort_tag;
5932 uint8_t command_type = ELS_COMMAND_NON_FIP;
5933 uint8_t cmnd;
5934 uint16_t xritag;
5935 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005936 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005937 int numBdes, i;
5938 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005939
James Smart45ed1192009-10-02 15:17:02 -04005940 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005941 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005942 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005943 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005944 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005945 command_type = ELS_COMMAND_FIP;
5946 else
5947 command_type = ELS_COMMAND_NON_FIP;
5948
James Smart4f774512009-05-22 14:52:35 -04005949 /* Some of the fields are in the right position already */
5950 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5951 abort_tag = (uint32_t) iocbq->iotag;
5952 xritag = iocbq->sli4_xritag;
5953 wqe->words[7] = 0; /* The ct field has moved so reset */
5954 /* words0-2 bpl convert bde */
5955 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005956 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5957 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005958 bpl = (struct ulp_bde64 *)
5959 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5960 if (!bpl)
5961 return IOCB_ERROR;
5962
5963 /* Should already be byte swapped. */
5964 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5965 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5966 /* swap the size field back to the cpu so we
5967 * can assign it to the sgl.
5968 */
5969 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005970 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5971 total_len = 0;
5972 for (i = 0; i < numBdes; i++) {
5973 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5974 total_len += bde.tus.f.bdeSize;
5975 }
James Smart4f774512009-05-22 14:52:35 -04005976 } else
James Smart5ffc2662009-11-18 15:39:44 -05005977 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005978
5979 iocbq->iocb.ulpIoTag = iocbq->iotag;
5980 cmnd = iocbq->iocb.ulpCommand;
5981
5982 switch (iocbq->iocb.ulpCommand) {
5983 case CMD_ELS_REQUEST64_CR:
5984 if (!iocbq->iocb.ulpLe) {
5985 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5986 "2007 Only Limited Edition cmd Format"
5987 " supported 0x%x\n",
5988 iocbq->iocb.ulpCommand);
5989 return IOCB_ERROR;
5990 }
James Smart5ffc2662009-11-18 15:39:44 -05005991 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005992 /* Els_reguest64 has a TMO */
5993 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5994 iocbq->iocb.ulpTimeout);
5995 /* Need a VF for word 4 set the vf bit*/
5996 bf_set(els_req64_vf, &wqe->els_req, 0);
5997 /* And a VFID for word 12 */
5998 bf_set(els_req64_vfid, &wqe->els_req, 0);
5999 /*
6000 * Set ct field to 3, indicates that the context_tag field
6001 * contains the FCFI and remote N_Port_ID is
6002 * in word 5.
6003 */
6004
6005 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6006 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6007 iocbq->iocb.ulpContext);
6008
James Smart4f774512009-05-22 14:52:35 -04006009 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6010 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6011 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006012
6013 if (command_type == ELS_COMMAND_FIP) {
6014 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6015 >> LPFC_FIP_ELS_ID_SHIFT);
6016 }
6017 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6018
James Smart4f774512009-05-22 14:52:35 -04006019 break;
James Smart5ffc2662009-11-18 15:39:44 -05006020 case CMD_XMIT_SEQUENCE64_CX:
6021 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6022 iocbq->iocb.un.ulpWord[3]);
6023 wqe->generic.word3 = 0;
6024 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006025 /* The entire sequence is transmitted for this IOCB */
6026 xmit_len = total_len;
6027 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006028 case CMD_XMIT_SEQUENCE64_CR:
6029 /* word3 iocb=io_tag32 wqe=payload_offset */
6030 /* payload offset used for multilpe outstanding
6031 * sequences on the same exchange
6032 */
6033 wqe->words[3] = 0;
6034 /* word4 relative_offset memcpy */
6035 /* word5 r_ctl/df_ctl memcpy */
6036 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006037 wqe->xmit_sequence.xmit_len = xmit_len;
6038 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006039 break;
6040 case CMD_XMIT_BCAST64_CN:
6041 /* word3 iocb=iotag32 wqe=payload_len */
6042 wqe->words[3] = 0; /* no definition for this in wqe */
6043 /* word4 iocb=rsvd wqe=rsvd */
6044 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6045 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6046 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6047 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6048 break;
6049 case CMD_FCP_IWRITE64_CR:
6050 command_type = FCP_COMMAND_DATA_OUT;
6051 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6052 * confusing.
6053 * word3 is payload_len: byte offset to the sgl entry for the
6054 * fcp_command.
6055 * word4 is total xfer len, same as the IOCB->ulpParameter.
6056 * word5 is initial xfer len 0 = wait for xfer-ready
6057 */
6058
6059 /* Always wait for xfer-ready before sending data */
6060 wqe->fcp_iwrite.initial_xfer_len = 0;
6061 /* word 4 (xfer length) should have been set on the memcpy */
6062
6063 /* allow write to fall through to read */
6064 case CMD_FCP_IREAD64_CR:
6065 /* FCP_CMD is always the 1st sgl entry */
6066 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05006067 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04006068
6069 /* word 4 (xfer length) should have been set on the memcpy */
6070
6071 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6072 iocbq->iocb.ulpFCP2Rcvy);
6073 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6074 /* The XC bit and the XS bit are similar. The driver never
6075 * tracked whether or not the exchange was previouslly open.
6076 * XC = Exchange create, 0 is create. 1 is already open.
6077 * XS = link cmd: 1 do not close the exchange after command.
6078 * XS = 0 close exchange when command completes.
6079 * The only time we would not set the XC bit is when the XS bit
6080 * is set and we are sending our 2nd or greater command on
6081 * this exchange.
6082 */
James Smart4f774512009-05-22 14:52:35 -04006083 /* Always open the exchange */
6084 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6085
6086 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6087 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006088 break;
6089 case CMD_FCP_ICMND64_CR:
6090 /* Always open the exchange */
6091 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6092
6093 wqe->words[4] = 0;
6094 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6095 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006096 break;
6097 case CMD_GEN_REQUEST64_CR:
6098 /* word3 command length is described as byte offset to the
6099 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6100 * sgl[0] = cmnd
6101 * sgl[1] = rsp.
6102 *
6103 */
James Smart5ffc2662009-11-18 15:39:44 -05006104 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006105 /* Word4 parameter copied in the memcpy */
6106 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6107 /* word6 context tag copied in memcpy */
6108 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6109 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6111 "2015 Invalid CT %x command 0x%x\n",
6112 ct, iocbq->iocb.ulpCommand);
6113 return IOCB_ERROR;
6114 }
6115 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6116 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6117 iocbq->iocb.ulpTimeout);
6118
6119 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6120 command_type = OTHER_COMMAND;
6121 break;
6122 case CMD_XMIT_ELS_RSP64_CX:
6123 /* words0-2 BDE memcpy */
6124 /* word3 iocb=iotag32 wqe=rsvd */
6125 wqe->words[3] = 0;
6126 /* word4 iocb=did wge=rsvd. */
6127 wqe->words[4] = 0;
6128 /* word5 iocb=rsvd wge=did */
6129 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6130 iocbq->iocb.un.elsreq64.remoteID);
6131
6132 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6133 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6134
6135 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6136 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6137 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6138 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6139 iocbq->vport->vpi + phba->vpi_base);
6140 command_type = OTHER_COMMAND;
6141 break;
6142 case CMD_CLOSE_XRI_CN:
6143 case CMD_ABORT_XRI_CN:
6144 case CMD_ABORT_XRI_CX:
6145 /* words 0-2 memcpy should be 0 rserved */
6146 /* port will send abts */
6147 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6148 /*
6149 * The link is down so the fw does not need to send abts
6150 * on the wire.
6151 */
6152 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6153 else
6154 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6155 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006156 wqe->words[5] = 0;
6157 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6158 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6159 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006160 /*
6161 * The abort handler will send us CMD_ABORT_XRI_CN or
6162 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6163 */
6164 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6165 cmnd = CMD_ABORT_XRI_CX;
6166 command_type = OTHER_COMMAND;
6167 xritag = 0;
6168 break;
James Smart6669f9b2009-10-02 15:16:45 -04006169 case CMD_XMIT_BLS_RSP64_CX:
6170 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6171 * we re-construct this WQE here based on information in
6172 * iocbq from scratch.
6173 */
6174 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006175 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006176 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006177 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6178 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6179 LPFC_ABTS_UNSOL_INT) {
6180 /* ABTS sent by initiator to CT exchange, the
6181 * RX_ID field will be filled with the newly
6182 * allocated responder XRI.
6183 */
6184 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6185 iocbq->sli4_xritag);
6186 } else {
6187 /* ABTS sent by responder to CT exchange, the
6188 * RX_ID field will be filled with the responder
6189 * RX_ID from ABTS.
6190 */
6191 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6192 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6193 }
James Smart6669f9b2009-10-02 15:16:45 -04006194 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6195 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6196 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6197 iocbq->iocb.ulpContext);
6198 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6199 command_type = OTHER_COMMAND;
6200 break;
James Smart4f774512009-05-22 14:52:35 -04006201 case CMD_XRI_ABORTED_CX:
6202 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6203 /* words0-2 are all 0's no bde */
6204 /* word3 and word4 are rsvrd */
6205 wqe->words[3] = 0;
6206 wqe->words[4] = 0;
6207 /* word5 iocb=rsvd wge=did */
6208 /* There is no remote port id in the IOCB? */
6209 /* Let this fall through and fail */
6210 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6211 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6212 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6213 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6214 default:
6215 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6216 "2014 Invalid command 0x%x\n",
6217 iocbq->iocb.ulpCommand);
6218 return IOCB_ERROR;
6219 break;
6220
6221 }
6222 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6223 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6224 wqe->generic.abort_tag = abort_tag;
6225 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6226 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6227 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6228 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6229
6230 return 0;
6231}
6232
6233/**
6234 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6235 * @phba: Pointer to HBA context object.
6236 * @ring_number: SLI ring number to issue iocb on.
6237 * @piocb: Pointer to command iocb.
6238 * @flag: Flag indicating if this command can be put into txq.
6239 *
6240 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6241 * an iocb command to an HBA with SLI-4 interface spec.
6242 *
6243 * This function is called with hbalock held. The function will return success
6244 * after it successfully submit the iocb to firmware or after adding to the
6245 * txq.
6246 **/
6247static int
6248__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6249 struct lpfc_iocbq *piocb, uint32_t flag)
6250{
6251 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006252 union lpfc_wqe wqe;
6253 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006254
6255 if (piocb->sli4_xritag == NO_XRI) {
6256 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006257 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006258 sglq = NULL;
6259 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006260 if (pring->txq_cnt) {
6261 if (!(flag & SLI_IOCB_RET_IOCB)) {
6262 __lpfc_sli_ringtx_put(phba,
6263 pring, piocb);
6264 return IOCB_SUCCESS;
6265 } else {
6266 return IOCB_BUSY;
6267 }
6268 } else {
James Smart4f774512009-05-22 14:52:35 -04006269 sglq = __lpfc_sli_get_sglq(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04006270 if (!sglq) {
6271 if (!(flag & SLI_IOCB_RET_IOCB)) {
6272 __lpfc_sli_ringtx_put(phba,
6273 pring,
6274 piocb);
6275 return IOCB_SUCCESS;
6276 } else
6277 return IOCB_BUSY;
6278 }
6279 }
James Smart4f774512009-05-22 14:52:35 -04006280 }
6281 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6282 sglq = NULL; /* These IO's already have an XRI and
6283 * a mapped sgl.
6284 */
6285 } else {
6286 /* This is a continuation of a commandi,(CX) so this
6287 * sglq is on the active list
6288 */
6289 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6290 if (!sglq)
6291 return IOCB_ERROR;
6292 }
6293
6294 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006295 piocb->sli4_xritag = sglq->sli4_xritag;
6296
6297 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006298 return IOCB_ERROR;
6299 }
6300
6301 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6302 return IOCB_ERROR;
6303
James Smart341af102010-01-26 23:07:37 -05006304 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6305 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006306 /*
6307 * For FCP command IOCB, get a new WQ index to distribute
6308 * WQE across the WQsr. On the other hand, for abort IOCB,
6309 * it carries the same WQ index to the original command
6310 * IOCB.
6311 */
James Smart341af102010-01-26 23:07:37 -05006312 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006313 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6314 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6315 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006316 return IOCB_ERROR;
6317 } else {
6318 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6319 return IOCB_ERROR;
6320 }
6321 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6322
6323 return 0;
6324}
6325
6326/**
James Smart3772a992009-05-22 14:50:54 -04006327 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6328 *
6329 * This routine wraps the actual lockless version for issusing IOCB function
6330 * pointer from the lpfc_hba struct.
6331 *
6332 * Return codes:
6333 * IOCB_ERROR - Error
6334 * IOCB_SUCCESS - Success
6335 * IOCB_BUSY - Busy
6336 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006337int
James Smart3772a992009-05-22 14:50:54 -04006338__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6339 struct lpfc_iocbq *piocb, uint32_t flag)
6340{
6341 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6342}
6343
6344/**
6345 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6346 * @phba: The hba struct for which this call is being executed.
6347 * @dev_grp: The HBA PCI-Device group number.
6348 *
6349 * This routine sets up the SLI interface API function jump table in @phba
6350 * struct.
6351 * Returns: 0 - success, -ENODEV - failure.
6352 **/
6353int
6354lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6355{
6356
6357 switch (dev_grp) {
6358 case LPFC_PCI_DEV_LP:
6359 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6360 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6361 break;
James Smart4f774512009-05-22 14:52:35 -04006362 case LPFC_PCI_DEV_OC:
6363 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6364 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6365 break;
James Smart3772a992009-05-22 14:50:54 -04006366 default:
6367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6368 "1419 Invalid HBA PCI-device group: 0x%x\n",
6369 dev_grp);
6370 return -ENODEV;
6371 break;
6372 }
6373 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6374 return 0;
6375}
James Smart92d7f7b2007-06-17 19:56:38 -05006376
James Smarte59058c2008-08-24 21:49:00 -04006377/**
James Smart3621a712009-04-06 18:47:14 -04006378 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006379 * @phba: Pointer to HBA context object.
6380 * @pring: Pointer to driver SLI ring object.
6381 * @piocb: Pointer to command iocb.
6382 * @flag: Flag indicating if this command can be put into txq.
6383 *
6384 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6385 * function. This function gets the hbalock and calls
6386 * __lpfc_sli_issue_iocb function and will return the error returned
6387 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6388 * functions which do not hold hbalock.
6389 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006390int
James Smart3772a992009-05-22 14:50:54 -04006391lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006392 struct lpfc_iocbq *piocb, uint32_t flag)
6393{
6394 unsigned long iflags;
6395 int rc;
6396
6397 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006398 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006399 spin_unlock_irqrestore(&phba->hbalock, iflags);
6400
6401 return rc;
6402}
6403
James Smarte59058c2008-08-24 21:49:00 -04006404/**
James Smart3621a712009-04-06 18:47:14 -04006405 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006406 * @phba: Pointer to HBA context object.
6407 *
6408 * This function is called while driver attaches with the
6409 * HBA to setup the extra ring. The extra ring is used
6410 * only when driver needs to support target mode functionality
6411 * or IP over FC functionalities.
6412 *
6413 * This function is called with no lock held.
6414 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006415static int
6416lpfc_extra_ring_setup( struct lpfc_hba *phba)
6417{
6418 struct lpfc_sli *psli;
6419 struct lpfc_sli_ring *pring;
6420
6421 psli = &phba->sli;
6422
6423 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006424
6425 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006426 pring = &psli->ring[psli->fcp_ring];
6427 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6428 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6429 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6430 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6431
James Smarta4bc3372006-12-02 13:34:16 -05006432 /* and give them to the extra ring */
6433 pring = &psli->ring[psli->extra_ring];
6434
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006435 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6436 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6437 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6438 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6439
6440 /* Setup default profile for this ring */
6441 pring->iotag_max = 4096;
6442 pring->num_mask = 1;
6443 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006444 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6445 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006446 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6447 return 0;
6448}
6449
James Smarte59058c2008-08-24 21:49:00 -04006450/**
James Smart3621a712009-04-06 18:47:14 -04006451 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006452 * @phba: Pointer to HBA context object.
6453 * @pring: Pointer to driver SLI ring object.
6454 * @iocbq: Pointer to iocb object.
6455 *
6456 * This function is called by the slow ring event handler
6457 * function when there is an ASYNC event iocb in the ring.
6458 * This function is called with no lock held.
6459 * Currently this function handles only temperature related
6460 * ASYNC events. The function decodes the temperature sensor
6461 * event message and posts events for the management applications.
6462 **/
James Smart98c9ea52007-10-27 13:37:33 -04006463static void
James Smart57127f12007-10-27 13:37:05 -04006464lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6465 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6466{
6467 IOCB_t *icmd;
6468 uint16_t evt_code;
6469 uint16_t temp;
6470 struct temp_event temp_event_data;
6471 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006472 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006473
6474 icmd = &iocbq->iocb;
6475 evt_code = icmd->un.asyncstat.evt_code;
6476 temp = icmd->ulpContext;
6477
6478 if ((evt_code != ASYNC_TEMP_WARN) &&
6479 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006480 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006481 lpfc_printf_log(phba,
6482 KERN_ERR,
6483 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006484 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006485 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006486 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6487 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6488 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6489 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006490 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006491 icmd->un.asyncstat.evt_code,
6492 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6493 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6494 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6495 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6496
James Smart57127f12007-10-27 13:37:05 -04006497 return;
6498 }
6499 temp_event_data.data = (uint32_t)temp;
6500 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6501 if (evt_code == ASYNC_TEMP_WARN) {
6502 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6503 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006504 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006505 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006506 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006507 "corrective action. temperature : %d Celsius\n",
6508 temp);
6509 }
6510 if (evt_code == ASYNC_TEMP_SAFE) {
6511 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6512 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006513 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006514 LOG_TEMP,
6515 "0340 Adapter temperature is OK now. "
6516 "temperature : %d Celsius\n",
6517 temp);
6518 }
6519
6520 /* Send temperature change event to applications */
6521 shost = lpfc_shost_from_vport(phba->pport);
6522 fc_host_post_vendor_event(shost, fc_get_event_number(),
6523 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006524 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006525
6526}
6527
6528
James Smarte59058c2008-08-24 21:49:00 -04006529/**
James Smart3621a712009-04-06 18:47:14 -04006530 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006531 * @phba: Pointer to HBA context object.
6532 *
6533 * lpfc_sli_setup sets up rings of the SLI interface with
6534 * number of iocbs per ring and iotags. This function is
6535 * called while driver attach to the HBA and before the
6536 * interrupts are enabled. So there is no need for locking.
6537 *
6538 * This function always returns 0.
6539 **/
dea31012005-04-17 16:05:31 -05006540int
6541lpfc_sli_setup(struct lpfc_hba *phba)
6542{
James Smarted957682007-06-17 19:56:37 -05006543 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006544 struct lpfc_sli *psli = &phba->sli;
6545 struct lpfc_sli_ring *pring;
6546
6547 psli->num_rings = MAX_CONFIGURED_RINGS;
6548 psli->sli_flag = 0;
6549 psli->fcp_ring = LPFC_FCP_RING;
6550 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006551 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006552
James Bottomley604a3e32005-10-29 10:28:33 -05006553 psli->iocbq_lookup = NULL;
6554 psli->iocbq_lookup_len = 0;
6555 psli->last_iotag = 0;
6556
dea31012005-04-17 16:05:31 -05006557 for (i = 0; i < psli->num_rings; i++) {
6558 pring = &psli->ring[i];
6559 switch (i) {
6560 case LPFC_FCP_RING: /* ring 0 - FCP */
6561 /* numCiocb and numRiocb are used in config_port */
6562 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6563 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6564 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6565 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6566 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6567 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006568 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006569 SLI3_IOCB_CMD_SIZE :
6570 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006571 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006572 SLI3_IOCB_RSP_SIZE :
6573 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006574 pring->iotag_ctr = 0;
6575 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006576 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006577 pring->fast_iotag = pring->iotag_max;
6578 pring->num_mask = 0;
6579 break;
James Smarta4bc3372006-12-02 13:34:16 -05006580 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006581 /* numCiocb and numRiocb are used in config_port */
6582 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6583 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006584 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006585 SLI3_IOCB_CMD_SIZE :
6586 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006587 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006588 SLI3_IOCB_RSP_SIZE :
6589 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006590 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006591 pring->num_mask = 0;
6592 break;
6593 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6594 /* numCiocb and numRiocb are used in config_port */
6595 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6596 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006597 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006598 SLI3_IOCB_CMD_SIZE :
6599 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006600 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006601 SLI3_IOCB_RSP_SIZE :
6602 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006603 pring->fast_iotag = 0;
6604 pring->iotag_ctr = 0;
6605 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006606 pring->lpfc_sli_rcv_async_status =
6607 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006608 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006609 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006610 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6611 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006612 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006613 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006614 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006615 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6616 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006617 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006618 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006619 pring->prt[2].profile = 0; /* Mask 2 */
6620 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006621 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006622 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006623 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006624 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006625 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006626 pring->prt[3].profile = 0; /* Mask 3 */
6627 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006628 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006629 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006630 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006631 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006632 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006633 /* abort unsolicited sequence */
6634 pring->prt[4].profile = 0; /* Mask 4 */
6635 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6636 pring->prt[4].type = FC_TYPE_BLS;
6637 pring->prt[4].lpfc_sli_rcv_unsol_event =
6638 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006639 break;
6640 }
James Smarted957682007-06-17 19:56:37 -05006641 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006642 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006643 }
James Smarted957682007-06-17 19:56:37 -05006644 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006645 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006646 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6647 "SLI2 SLIM Data: x%x x%lx\n",
6648 phba->brd_no, totiocbsize,
6649 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006650 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006651 if (phba->cfg_multi_ring_support == 2)
6652 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006653
6654 return 0;
6655}
6656
James Smarte59058c2008-08-24 21:49:00 -04006657/**
James Smart3621a712009-04-06 18:47:14 -04006658 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006659 * @phba: Pointer to HBA context object.
6660 *
6661 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6662 * ring. This function also initializes ring indices of each ring.
6663 * This function is called during the initialization of the SLI
6664 * interface of an HBA.
6665 * This function is called with no lock held and always returns
6666 * 1.
6667 **/
dea31012005-04-17 16:05:31 -05006668int
James Smart2e0fef82007-06-17 19:56:36 -05006669lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006670{
6671 struct lpfc_sli *psli;
6672 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006673 int i;
dea31012005-04-17 16:05:31 -05006674
6675 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006676 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006677 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006678 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006679 /* Initialize list headers for txq and txcmplq as double linked lists */
6680 for (i = 0; i < psli->num_rings; i++) {
6681 pring = &psli->ring[i];
6682 pring->ringno = i;
6683 pring->next_cmdidx = 0;
6684 pring->local_getidx = 0;
6685 pring->cmdidx = 0;
6686 INIT_LIST_HEAD(&pring->txq);
6687 INIT_LIST_HEAD(&pring->txcmplq);
6688 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006689 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006690 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006691 }
James Smart2e0fef82007-06-17 19:56:36 -05006692 spin_unlock_irq(&phba->hbalock);
6693 return 1;
dea31012005-04-17 16:05:31 -05006694}
6695
James Smarte59058c2008-08-24 21:49:00 -04006696/**
James Smart04c68492009-05-22 14:52:52 -04006697 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6698 * @phba: Pointer to HBA context object.
6699 *
6700 * This routine flushes the mailbox command subsystem. It will unconditionally
6701 * flush all the mailbox commands in the three possible stages in the mailbox
6702 * command sub-system: pending mailbox command queue; the outstanding mailbox
6703 * command; and completed mailbox command queue. It is caller's responsibility
6704 * to make sure that the driver is in the proper state to flush the mailbox
6705 * command sub-system. Namely, the posting of mailbox commands into the
6706 * pending mailbox command queue from the various clients must be stopped;
6707 * either the HBA is in a state that it will never works on the outstanding
6708 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6709 * mailbox command has been completed.
6710 **/
6711static void
6712lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6713{
6714 LIST_HEAD(completions);
6715 struct lpfc_sli *psli = &phba->sli;
6716 LPFC_MBOXQ_t *pmb;
6717 unsigned long iflag;
6718
6719 /* Flush all the mailbox commands in the mbox system */
6720 spin_lock_irqsave(&phba->hbalock, iflag);
6721 /* The pending mailbox command queue */
6722 list_splice_init(&phba->sli.mboxq, &completions);
6723 /* The outstanding active mailbox command */
6724 if (psli->mbox_active) {
6725 list_add_tail(&psli->mbox_active->list, &completions);
6726 psli->mbox_active = NULL;
6727 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6728 }
6729 /* The completed mailbox command queue */
6730 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6731 spin_unlock_irqrestore(&phba->hbalock, iflag);
6732
6733 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6734 while (!list_empty(&completions)) {
6735 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6736 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6737 if (pmb->mbox_cmpl)
6738 pmb->mbox_cmpl(phba, pmb);
6739 }
6740}
6741
6742/**
James Smart3621a712009-04-06 18:47:14 -04006743 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006744 * @vport: Pointer to virtual port object.
6745 *
6746 * lpfc_sli_host_down is called to clean up the resources
6747 * associated with a vport before destroying virtual
6748 * port data structures.
6749 * This function does following operations:
6750 * - Free discovery resources associated with this virtual
6751 * port.
6752 * - Free iocbs associated with this virtual port in
6753 * the txq.
6754 * - Send abort for all iocb commands associated with this
6755 * vport in txcmplq.
6756 *
6757 * This function is called with no lock held and always returns 1.
6758 **/
dea31012005-04-17 16:05:31 -05006759int
James Smart92d7f7b2007-06-17 19:56:38 -05006760lpfc_sli_host_down(struct lpfc_vport *vport)
6761{
James Smart858c9f62007-06-17 19:56:39 -05006762 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006763 struct lpfc_hba *phba = vport->phba;
6764 struct lpfc_sli *psli = &phba->sli;
6765 struct lpfc_sli_ring *pring;
6766 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006767 int i;
6768 unsigned long flags = 0;
6769 uint16_t prev_pring_flag;
6770
6771 lpfc_cleanup_discovery_resources(vport);
6772
6773 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006774 for (i = 0; i < psli->num_rings; i++) {
6775 pring = &psli->ring[i];
6776 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006777 /* Only slow rings */
6778 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006779 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006780 /* Set the lpfc data pending flag */
6781 set_bit(LPFC_DATA_READY, &phba->data_flags);
6782 }
James Smart92d7f7b2007-06-17 19:56:38 -05006783 /*
6784 * Error everything on the txq since these iocbs have not been
6785 * given to the FW yet.
6786 */
James Smart92d7f7b2007-06-17 19:56:38 -05006787 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6788 if (iocb->vport != vport)
6789 continue;
James Smart858c9f62007-06-17 19:56:39 -05006790 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006791 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006792 }
6793
6794 /* Next issue ABTS for everything on the txcmplq */
6795 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6796 list) {
6797 if (iocb->vport != vport)
6798 continue;
6799 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6800 }
6801
6802 pring->flag = prev_pring_flag;
6803 }
6804
6805 spin_unlock_irqrestore(&phba->hbalock, flags);
6806
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 Smart92d7f7b2007-06-17 19:56:38 -05006810 return 1;
6811}
6812
James Smarte59058c2008-08-24 21:49:00 -04006813/**
James Smart3621a712009-04-06 18:47:14 -04006814 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006815 * @phba: Pointer to HBA context object.
6816 *
6817 * This function cleans up all iocb, buffers, mailbox commands
6818 * while shutting down the HBA. This function is called with no
6819 * lock held and always returns 1.
6820 * This function does the following to cleanup driver resources:
6821 * - Free discovery resources for each virtual port
6822 * - Cleanup any pending fabric iocbs
6823 * - Iterate through the iocb txq and free each entry
6824 * in the list.
6825 * - Free up any buffer posted to the HBA
6826 * - Free mailbox commands in the mailbox queue.
6827 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006828int
James Smart2e0fef82007-06-17 19:56:36 -05006829lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006830{
James Smart2534ba72007-04-25 09:52:20 -04006831 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006832 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006833 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006834 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006835 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006836 int i;
6837
6838 /* Shutdown the mailbox command sub-system */
6839 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006840
dea31012005-04-17 16:05:31 -05006841 lpfc_hba_down_prep(phba);
6842
James Smart92d7f7b2007-06-17 19:56:38 -05006843 lpfc_fabric_abort_hba(phba);
6844
James Smart2e0fef82007-06-17 19:56:36 -05006845 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006846 for (i = 0; i < psli->num_rings; i++) {
6847 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006848 /* Only slow rings */
6849 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006850 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006851 /* Set the lpfc data pending flag */
6852 set_bit(LPFC_DATA_READY, &phba->data_flags);
6853 }
dea31012005-04-17 16:05:31 -05006854
6855 /*
6856 * Error everything on the txq since these iocbs have not been
6857 * given to the FW yet.
6858 */
James Smart2534ba72007-04-25 09:52:20 -04006859 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006860 pring->txq_cnt = 0;
6861
dea31012005-04-17 16:05:31 -05006862 }
James Smart2e0fef82007-06-17 19:56:36 -05006863 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006864
James Smarta257bf92009-04-06 18:48:10 -04006865 /* Cancel all the IOCBs from the completions list */
6866 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6867 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006868
James Smart0ff10d42008-01-11 01:52:36 -05006869 spin_lock_irqsave(&phba->hbalock, flags);
6870 list_splice_init(&phba->elsbuf, &completions);
6871 phba->elsbuf_cnt = 0;
6872 phba->elsbuf_prev_cnt = 0;
6873 spin_unlock_irqrestore(&phba->hbalock, flags);
6874
6875 while (!list_empty(&completions)) {
6876 list_remove_head(&completions, buf_ptr,
6877 struct lpfc_dmabuf, list);
6878 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6879 kfree(buf_ptr);
6880 }
6881
dea31012005-04-17 16:05:31 -05006882 /* Return any active mbox cmds */
6883 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006884
James Smartda0436e2009-05-22 14:51:39 -04006885 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006886 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006887 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006888
James Smartda0436e2009-05-22 14:51:39 -04006889 return 1;
6890}
James Smart92d7f7b2007-06-17 19:56:38 -05006891
James Smartda0436e2009-05-22 14:51:39 -04006892/**
6893 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6894 * @phba: Pointer to HBA context object.
6895 *
6896 * This function cleans up all queues, iocb, buffers, mailbox commands while
6897 * shutting down the SLI4 HBA FCoE function. This function is called with no
6898 * lock held and always returns 1.
6899 *
6900 * This function does the following to cleanup driver FCoE function resources:
6901 * - Free discovery resources for each virtual port
6902 * - Cleanup any pending fabric iocbs
6903 * - Iterate through the iocb txq and free each entry in the list.
6904 * - Free up any buffer posted to the HBA.
6905 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6906 * - Free mailbox commands in the mailbox queue.
6907 **/
6908int
6909lpfc_sli4_hba_down(struct lpfc_hba *phba)
6910{
6911 /* Stop the SLI4 device port */
6912 lpfc_stop_port(phba);
6913
6914 /* Tear down the queues in the HBA */
6915 lpfc_sli4_queue_unset(phba);
6916
6917 /* unregister default FCFI from the HBA */
6918 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6919
dea31012005-04-17 16:05:31 -05006920 return 1;
6921}
6922
James Smarte59058c2008-08-24 21:49:00 -04006923/**
James Smart3621a712009-04-06 18:47:14 -04006924 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006925 * @srcp: Source memory pointer.
6926 * @destp: Destination memory pointer.
6927 * @cnt: Number of words required to be copied.
6928 *
6929 * This function is used for copying data between driver memory
6930 * and the SLI memory. This function also changes the endianness
6931 * of each word if native endianness is different from SLI
6932 * endianness. This function can be called with or without
6933 * lock.
6934 **/
dea31012005-04-17 16:05:31 -05006935void
6936lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6937{
6938 uint32_t *src = srcp;
6939 uint32_t *dest = destp;
6940 uint32_t ldata;
6941 int i;
6942
6943 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6944 ldata = *src;
6945 ldata = le32_to_cpu(ldata);
6946 *dest = ldata;
6947 src++;
6948 dest++;
6949 }
6950}
6951
James Smarte59058c2008-08-24 21:49:00 -04006952
6953/**
James Smarta0c87cb2009-07-19 10:01:10 -04006954 * lpfc_sli_bemem_bcopy - SLI memory copy function
6955 * @srcp: Source memory pointer.
6956 * @destp: Destination memory pointer.
6957 * @cnt: Number of words required to be copied.
6958 *
6959 * This function is used for copying data between a data structure
6960 * with big endian representation to local endianness.
6961 * This function can be called with or without lock.
6962 **/
6963void
6964lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6965{
6966 uint32_t *src = srcp;
6967 uint32_t *dest = destp;
6968 uint32_t ldata;
6969 int i;
6970
6971 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6972 ldata = *src;
6973 ldata = be32_to_cpu(ldata);
6974 *dest = ldata;
6975 src++;
6976 dest++;
6977 }
6978}
6979
6980/**
James Smart3621a712009-04-06 18:47:14 -04006981 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006982 * @phba: Pointer to HBA context object.
6983 * @pring: Pointer to driver SLI ring object.
6984 * @mp: Pointer to driver buffer object.
6985 *
6986 * This function is called with no lock held.
6987 * It always return zero after adding the buffer to the postbufq
6988 * buffer list.
6989 **/
dea31012005-04-17 16:05:31 -05006990int
James Smart2e0fef82007-06-17 19:56:36 -05006991lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6992 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006993{
6994 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6995 later */
James Smart2e0fef82007-06-17 19:56:36 -05006996 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006997 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006998 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006999 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007000 return 0;
7001}
7002
James Smarte59058c2008-08-24 21:49:00 -04007003/**
James Smart3621a712009-04-06 18:47:14 -04007004 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007005 * @phba: Pointer to HBA context object.
7006 *
7007 * When HBQ is enabled, buffers are searched based on tags. This function
7008 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7009 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7010 * does not conflict with tags of buffer posted for unsolicited events.
7011 * The function returns the allocated tag. The function is called with
7012 * no locks held.
7013 **/
James Smart76bb24e2007-10-27 13:38:00 -04007014uint32_t
7015lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7016{
7017 spin_lock_irq(&phba->hbalock);
7018 phba->buffer_tag_count++;
7019 /*
7020 * Always set the QUE_BUFTAG_BIT to distiguish between
7021 * a tag assigned by HBQ.
7022 */
7023 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7024 spin_unlock_irq(&phba->hbalock);
7025 return phba->buffer_tag_count;
7026}
7027
James Smarte59058c2008-08-24 21:49:00 -04007028/**
James Smart3621a712009-04-06 18:47:14 -04007029 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007030 * @phba: Pointer to HBA context object.
7031 * @pring: Pointer to driver SLI ring object.
7032 * @tag: Buffer tag.
7033 *
7034 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7035 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7036 * iocb is posted to the response ring with the tag of the buffer.
7037 * This function searches the pring->postbufq list using the tag
7038 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7039 * iocb. If the buffer is found then lpfc_dmabuf object of the
7040 * buffer is returned to the caller else NULL is returned.
7041 * This function is called with no lock held.
7042 **/
James Smart76bb24e2007-10-27 13:38:00 -04007043struct lpfc_dmabuf *
7044lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7045 uint32_t tag)
7046{
7047 struct lpfc_dmabuf *mp, *next_mp;
7048 struct list_head *slp = &pring->postbufq;
7049
7050 /* Search postbufq, from the begining, looking for a match on tag */
7051 spin_lock_irq(&phba->hbalock);
7052 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7053 if (mp->buffer_tag == tag) {
7054 list_del_init(&mp->list);
7055 pring->postbufq_cnt--;
7056 spin_unlock_irq(&phba->hbalock);
7057 return mp;
7058 }
7059 }
7060
7061 spin_unlock_irq(&phba->hbalock);
7062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007063 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007064 "ring %d Data x%lx x%p x%p x%x\n",
7065 pring->ringno, (unsigned long) tag,
7066 slp->next, slp->prev, pring->postbufq_cnt);
7067
7068 return NULL;
7069}
dea31012005-04-17 16:05:31 -05007070
James Smarte59058c2008-08-24 21:49:00 -04007071/**
James Smart3621a712009-04-06 18:47:14 -04007072 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007073 * @phba: Pointer to HBA context object.
7074 * @pring: Pointer to driver SLI ring object.
7075 * @phys: DMA address of the buffer.
7076 *
7077 * This function searches the buffer list using the dma_address
7078 * of unsolicited event to find the driver's lpfc_dmabuf object
7079 * corresponding to the dma_address. The function returns the
7080 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7081 * This function is called by the ct and els unsolicited event
7082 * handlers to get the buffer associated with the unsolicited
7083 * event.
7084 *
7085 * This function is called with no lock held.
7086 **/
dea31012005-04-17 16:05:31 -05007087struct lpfc_dmabuf *
7088lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7089 dma_addr_t phys)
7090{
7091 struct lpfc_dmabuf *mp, *next_mp;
7092 struct list_head *slp = &pring->postbufq;
7093
7094 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007096 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7097 if (mp->phys == phys) {
7098 list_del_init(&mp->list);
7099 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007100 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007101 return mp;
7102 }
7103 }
7104
James Smart2e0fef82007-06-17 19:56:36 -05007105 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007107 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007108 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007109 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007110 slp->next, slp->prev, pring->postbufq_cnt);
7111 return NULL;
7112}
7113
James Smarte59058c2008-08-24 21:49:00 -04007114/**
James Smart3621a712009-04-06 18:47:14 -04007115 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007116 * @phba: Pointer to HBA context object.
7117 * @cmdiocb: Pointer to driver command iocb object.
7118 * @rspiocb: Pointer to driver response iocb object.
7119 *
7120 * This function is the completion handler for the abort iocbs for
7121 * ELS commands. This function is called from the ELS ring event
7122 * handler with no lock held. This function frees memory resources
7123 * associated with the abort iocb.
7124 **/
dea31012005-04-17 16:05:31 -05007125static void
James Smart2e0fef82007-06-17 19:56:36 -05007126lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7127 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007128{
James Smart2e0fef82007-06-17 19:56:36 -05007129 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007130 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007131 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007132 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7133
7134 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007135
7136 if (irsp->ulpStatus) {
7137 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7138 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7139
James Smart2e0fef82007-06-17 19:56:36 -05007140 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007141 if (phba->sli_rev < LPFC_SLI_REV4) {
7142 if (abort_iotag != 0 &&
7143 abort_iotag <= phba->sli.last_iotag)
7144 abort_iocb =
7145 phba->sli.iocbq_lookup[abort_iotag];
7146 } else
7147 /* For sli4 the abort_tag is the XRI,
7148 * so the abort routine puts the iotag of the iocb
7149 * being aborted in the context field of the abort
7150 * IOCB.
7151 */
7152 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007153
James Smart2680eea2007-04-25 09:52:55 -04007154 /*
James Smart58da1ff2008-04-07 10:15:56 -04007155 * If the iocb is not found in Firmware queue the iocb
7156 * might have completed already. Do not free it again.
7157 */
James Smart9b379602008-04-07 10:16:00 -04007158 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007159 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7160 spin_unlock_irq(&phba->hbalock);
7161 lpfc_sli_release_iocbq(phba, cmdiocb);
7162 return;
7163 }
7164 /* For SLI4 the ulpContext field for abort IOCB
7165 * holds the iotag of the IOCB being aborted so
7166 * the local abort_context needs to be reset to
7167 * match the aborted IOCBs ulpContext.
7168 */
7169 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7170 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007171 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007172
7173 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7174 "0327 Cannot abort els iocb %p "
7175 "with tag %x context %x, abort status %x, "
7176 "abort code %x\n",
7177 abort_iocb, abort_iotag, abort_context,
7178 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007179 /*
James Smart2680eea2007-04-25 09:52:55 -04007180 * make sure we have the right iocbq before taking it
7181 * off the txcmplq and try to call completion routine.
7182 */
James Smart2e0fef82007-06-17 19:56:36 -05007183 if (!abort_iocb ||
7184 abort_iocb->iocb.ulpContext != abort_context ||
7185 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7186 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007187 else if (phba->sli_rev < LPFC_SLI_REV4) {
7188 /*
7189 * leave the SLI4 aborted command on the txcmplq
7190 * list and the command complete WCQE's XB bit
7191 * will tell whether the SGL (XRI) can be released
7192 * immediately or to the aborted SGL list for the
7193 * following abort XRI from the HBA.
7194 */
James Smart92d7f7b2007-06-17 19:56:38 -05007195 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007196 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7197 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7198 pring->txcmplq_cnt--;
7199 }
James Smart2680eea2007-04-25 09:52:55 -04007200
James Smart0ff10d42008-01-11 01:52:36 -05007201 /* Firmware could still be in progress of DMAing
7202 * payload, so don't free data buffer till after
7203 * a hbeat.
7204 */
7205 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007206 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007207 spin_unlock_irq(&phba->hbalock);
7208
James Smart92d7f7b2007-06-17 19:56:38 -05007209 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007210 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007211 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007212 } else
7213 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007214 }
7215
James Bottomley604a3e32005-10-29 10:28:33 -05007216 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007217 return;
7218}
7219
James Smarte59058c2008-08-24 21:49:00 -04007220/**
James Smart3621a712009-04-06 18:47:14 -04007221 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007222 * @phba: Pointer to HBA context object.
7223 * @cmdiocb: Pointer to driver command iocb object.
7224 * @rspiocb: Pointer to driver response iocb object.
7225 *
7226 * The function is called from SLI ring event handler with no
7227 * lock held. This function is the completion handler for ELS commands
7228 * which are aborted. The function frees memory resources used for
7229 * the aborted ELS commands.
7230 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007231static void
7232lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7233 struct lpfc_iocbq *rspiocb)
7234{
7235 IOCB_t *irsp = &rspiocb->iocb;
7236
7237 /* ELS cmd tag <ulpIoTag> completes */
7238 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007239 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007240 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007241 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007242 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007243 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7244 lpfc_ct_free_iocb(phba, cmdiocb);
7245 else
7246 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007247 return;
7248}
7249
James Smarte59058c2008-08-24 21:49:00 -04007250/**
James Smart3621a712009-04-06 18:47:14 -04007251 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007252 * @phba: Pointer to HBA context object.
7253 * @pring: Pointer to driver SLI ring object.
7254 * @cmdiocb: Pointer to driver command iocb object.
7255 *
7256 * This function issues an abort iocb for the provided command
7257 * iocb. This function is called with hbalock held.
7258 * The function returns 0 when it fails due to memory allocation
7259 * failure or when the command iocb is an abort request.
7260 **/
dea31012005-04-17 16:05:31 -05007261int
James Smart2e0fef82007-06-17 19:56:36 -05007262lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7263 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007264{
James Smart2e0fef82007-06-17 19:56:36 -05007265 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007266 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007267 IOCB_t *icmd = NULL;
7268 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007269 int retval = IOCB_ERROR;
7270
James Smart92d7f7b2007-06-17 19:56:38 -05007271 /*
7272 * There are certain command types we don't want to abort. And we
7273 * don't want to abort commands that are already in the process of
7274 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007275 */
7276 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007277 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007278 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7279 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007280 return 0;
7281
James Smart858c9f62007-06-17 19:56:39 -05007282 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7283 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007284 */
James Smart858c9f62007-06-17 19:56:39 -05007285 if ((vport->load_flag & FC_UNLOADING) &&
7286 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007287 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7288 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7289 else
7290 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007291 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007292 }
dea31012005-04-17 16:05:31 -05007293
7294 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007295 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007296 if (abtsiocbp == NULL)
7297 return 0;
dea31012005-04-17 16:05:31 -05007298
James Smart07951072007-04-25 09:51:38 -04007299 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007300 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007301 */
7302 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7303
dea31012005-04-17 16:05:31 -05007304 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007305 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7306 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007307 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007308 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007309 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7310 }
James Smartda0436e2009-05-22 14:51:39 -04007311 else
7312 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007313 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007314 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007315
James Smart5ffc2662009-11-18 15:39:44 -05007316 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7317 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007318 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7319 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007320
James Smart2e0fef82007-06-17 19:56:36 -05007321 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007322 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7323 else
7324 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7325
7326 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007327
James Smarte8b62012007-08-02 11:10:09 -04007328 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7329 "0339 Abort xri x%x, original iotag x%x, "
7330 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007331 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007332 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007333 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007334 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007335
James Smartd7c255b2008-08-24 21:50:00 -04007336 if (retval)
7337 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007338abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007339 /*
7340 * Caller to this routine should check for IOCB_ERROR
7341 * and handle it properly. This routine no longer removes
7342 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007343 */
James Smart2e0fef82007-06-17 19:56:36 -05007344 return retval;
dea31012005-04-17 16:05:31 -05007345}
7346
James Smarte59058c2008-08-24 21:49:00 -04007347/**
James Smart3621a712009-04-06 18:47:14 -04007348 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007349 * @iocbq: Pointer to driver iocb object.
7350 * @vport: Pointer to driver virtual port object.
7351 * @tgt_id: SCSI ID of the target.
7352 * @lun_id: LUN ID of the scsi device.
7353 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7354 *
James Smart3621a712009-04-06 18:47:14 -04007355 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007356 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7357 * 0 if the filtering criteria is met for the given iocb and will return
7358 * 1 if the filtering criteria is not met.
7359 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7360 * given iocb is for the SCSI device specified by vport, tgt_id and
7361 * lun_id parameter.
7362 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7363 * given iocb is for the SCSI target specified by vport and tgt_id
7364 * parameters.
7365 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7366 * given iocb is for the SCSI host associated with the given vport.
7367 * This function is called with no locks held.
7368 **/
dea31012005-04-17 16:05:31 -05007369static int
James Smart51ef4c22007-08-02 11:10:31 -04007370lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7371 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007372 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007373{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007374 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007375 int rc = 1;
7376
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007377 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7378 return rc;
7379
James Smart51ef4c22007-08-02 11:10:31 -04007380 if (iocbq->vport != vport)
7381 return rc;
7382
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007383 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007384
James Smart495a7142008-06-14 22:52:59 -04007385 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007386 return rc;
7387
7388 switch (ctx_cmd) {
7389 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007390 if ((lpfc_cmd->rdata->pnode) &&
7391 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7392 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007393 rc = 0;
7394 break;
7395 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007396 if ((lpfc_cmd->rdata->pnode) &&
7397 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007398 rc = 0;
7399 break;
dea31012005-04-17 16:05:31 -05007400 case LPFC_CTX_HOST:
7401 rc = 0;
7402 break;
7403 default:
7404 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007405 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007406 break;
7407 }
7408
7409 return rc;
7410}
7411
James Smarte59058c2008-08-24 21:49:00 -04007412/**
James Smart3621a712009-04-06 18:47:14 -04007413 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007414 * @vport: Pointer to virtual port.
7415 * @tgt_id: SCSI ID of the target.
7416 * @lun_id: LUN ID of the scsi device.
7417 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7418 *
7419 * This function returns number of FCP commands pending for the vport.
7420 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7421 * commands pending on the vport associated with SCSI device specified
7422 * by tgt_id and lun_id parameters.
7423 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7424 * commands pending on the vport associated with SCSI target specified
7425 * by tgt_id parameter.
7426 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7427 * commands pending on the vport.
7428 * This function returns the number of iocbs which satisfy the filter.
7429 * This function is called without any lock held.
7430 **/
dea31012005-04-17 16:05:31 -05007431int
James Smart51ef4c22007-08-02 11:10:31 -04007432lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7433 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007434{
James Smart51ef4c22007-08-02 11:10:31 -04007435 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007436 struct lpfc_iocbq *iocbq;
7437 int sum, i;
dea31012005-04-17 16:05:31 -05007438
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007439 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7440 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007441
James Smart51ef4c22007-08-02 11:10:31 -04007442 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7443 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007444 sum++;
dea31012005-04-17 16:05:31 -05007445 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007446
dea31012005-04-17 16:05:31 -05007447 return sum;
7448}
7449
James Smarte59058c2008-08-24 21:49:00 -04007450/**
James Smart3621a712009-04-06 18:47:14 -04007451 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007452 * @phba: Pointer to HBA context object
7453 * @cmdiocb: Pointer to command iocb object.
7454 * @rspiocb: Pointer to response iocb object.
7455 *
7456 * This function is called when an aborted FCP iocb completes. This
7457 * function is called by the ring event handler with no lock held.
7458 * This function frees the iocb.
7459 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007460void
James Smart2e0fef82007-06-17 19:56:36 -05007461lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7462 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007463{
James Bottomley604a3e32005-10-29 10:28:33 -05007464 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007465 return;
7466}
7467
James Smarte59058c2008-08-24 21:49:00 -04007468/**
James Smart3621a712009-04-06 18:47:14 -04007469 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007470 * @vport: Pointer to virtual port.
7471 * @pring: Pointer to driver SLI ring object.
7472 * @tgt_id: SCSI ID of the target.
7473 * @lun_id: LUN ID of the scsi device.
7474 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7475 *
7476 * This function sends an abort command for every SCSI command
7477 * associated with the given virtual port pending on the ring
7478 * filtered by lpfc_sli_validate_fcp_iocb function.
7479 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7480 * FCP iocbs associated with lun specified by tgt_id and lun_id
7481 * parameters
7482 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7483 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7484 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7485 * FCP iocbs associated with virtual port.
7486 * This function returns number of iocbs it failed to abort.
7487 * This function is called with no locks held.
7488 **/
dea31012005-04-17 16:05:31 -05007489int
James Smart51ef4c22007-08-02 11:10:31 -04007490lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7491 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007492{
James Smart51ef4c22007-08-02 11:10:31 -04007493 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007494 struct lpfc_iocbq *iocbq;
7495 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007496 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007497 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007498 int i;
dea31012005-04-17 16:05:31 -05007499
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007500 for (i = 1; i <= phba->sli.last_iotag; i++) {
7501 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007502
James Smart51ef4c22007-08-02 11:10:31 -04007503 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007504 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007505 continue;
7506
7507 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007508 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007509 if (abtsiocb == NULL) {
7510 errcnt++;
7511 continue;
7512 }
dea31012005-04-17 16:05:31 -05007513
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007514 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007515 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7516 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007517 if (phba->sli_rev == LPFC_SLI_REV4)
7518 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7519 else
7520 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007521 abtsiocb->iocb.ulpLe = 1;
7522 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007523 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007524
James Smart5ffc2662009-11-18 15:39:44 -05007525 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7526 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007527 if (iocbq->iocb_flag & LPFC_IO_FCP)
7528 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007529
James Smart2e0fef82007-06-17 19:56:36 -05007530 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007531 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7532 else
7533 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7534
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007535 /* Setup callback routine and issue the command. */
7536 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007537 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7538 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007539 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007540 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007541 errcnt++;
7542 continue;
7543 }
7544 }
7545
7546 return errcnt;
7547}
7548
James Smarte59058c2008-08-24 21:49:00 -04007549/**
James Smart3621a712009-04-06 18:47:14 -04007550 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007551 * @phba: Pointer to HBA context object.
7552 * @cmdiocbq: Pointer to command iocb.
7553 * @rspiocbq: Pointer to response iocb.
7554 *
7555 * This function is the completion handler for iocbs issued using
7556 * lpfc_sli_issue_iocb_wait function. This function is called by the
7557 * ring event handler function without any lock held. This function
7558 * can be called from both worker thread context and interrupt
7559 * context. This function also can be called from other thread which
7560 * cleans up the SLI layer objects.
7561 * This function copy the contents of the response iocb to the
7562 * response iocb memory object provided by the caller of
7563 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7564 * sleeps for the iocb completion.
7565 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007566static void
7567lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7568 struct lpfc_iocbq *cmdiocbq,
7569 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007570{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007571 wait_queue_head_t *pdone_q;
7572 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007573 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007574
James Smart2e0fef82007-06-17 19:56:36 -05007575 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007576 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7577 if (cmdiocbq->context2 && rspiocbq)
7578 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7579 &rspiocbq->iocb, sizeof(IOCB_t));
7580
James Smart0f65ff62010-02-26 14:14:23 -05007581 /* Set the exchange busy flag for task management commands */
7582 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7583 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7584 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7585 cur_iocbq);
7586 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7587 }
7588
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007589 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007590 if (pdone_q)
7591 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007592 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007593 return;
7594}
7595
James Smarte59058c2008-08-24 21:49:00 -04007596/**
James Smartd11e31d2009-06-10 17:23:06 -04007597 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7598 * @phba: Pointer to HBA context object..
7599 * @piocbq: Pointer to command iocb.
7600 * @flag: Flag to test.
7601 *
7602 * This routine grabs the hbalock and then test the iocb_flag to
7603 * see if the passed in flag is set.
7604 * Returns:
7605 * 1 if flag is set.
7606 * 0 if flag is not set.
7607 **/
7608static int
7609lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7610 struct lpfc_iocbq *piocbq, uint32_t flag)
7611{
7612 unsigned long iflags;
7613 int ret;
7614
7615 spin_lock_irqsave(&phba->hbalock, iflags);
7616 ret = piocbq->iocb_flag & flag;
7617 spin_unlock_irqrestore(&phba->hbalock, iflags);
7618 return ret;
7619
7620}
7621
7622/**
James Smart3621a712009-04-06 18:47:14 -04007623 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007624 * @phba: Pointer to HBA context object..
7625 * @pring: Pointer to sli ring.
7626 * @piocb: Pointer to command iocb.
7627 * @prspiocbq: Pointer to response iocb.
7628 * @timeout: Timeout in number of seconds.
7629 *
7630 * This function issues the iocb to firmware and waits for the
7631 * iocb to complete. If the iocb command is not
7632 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7633 * Caller should not free the iocb resources if this function
7634 * returns IOCB_TIMEDOUT.
7635 * The function waits for the iocb completion using an
7636 * non-interruptible wait.
7637 * This function will sleep while waiting for iocb completion.
7638 * So, this function should not be called from any context which
7639 * does not allow sleeping. Due to the same reason, this function
7640 * cannot be called with interrupt disabled.
7641 * This function assumes that the iocb completions occur while
7642 * this function sleep. So, this function cannot be called from
7643 * the thread which process iocb completion for this ring.
7644 * This function clears the iocb_flag of the iocb object before
7645 * issuing the iocb and the iocb completion handler sets this
7646 * flag and wakes this thread when the iocb completes.
7647 * The contents of the response iocb will be copied to prspiocbq
7648 * by the completion handler when the command completes.
7649 * This function returns IOCB_SUCCESS when success.
7650 * This function is called with no lock held.
7651 **/
dea31012005-04-17 16:05:31 -05007652int
James Smart2e0fef82007-06-17 19:56:36 -05007653lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007654 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007655 struct lpfc_iocbq *piocb,
7656 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007657 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007658{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007659 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007660 long timeleft, timeout_req = 0;
7661 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007662 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04007663 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05007664 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007665 * If the caller has provided a response iocbq buffer, then context2
7666 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007667 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007668 if (prspiocbq) {
7669 if (piocb->context2)
7670 return IOCB_ERROR;
7671 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007672 }
7673
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007674 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7675 piocb->context_un.wait_queue = &done_q;
7676 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007677
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007678 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7679 creg_val = readl(phba->HCregaddr);
7680 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7681 writel(creg_val, phba->HCregaddr);
7682 readl(phba->HCregaddr); /* flush */
7683 }
7684
James Smart2a9bf3d2010-06-07 15:24:45 -04007685 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7686 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007687 if (retval == IOCB_SUCCESS) {
7688 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007689 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007690 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007691 timeout_req);
dea31012005-04-17 16:05:31 -05007692
James Smart7054a602007-04-25 09:52:34 -04007693 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7694 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007695 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007696 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007698 "0338 IOCB wait timeout error - no "
7699 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007700 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007701 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007703 "0330 IOCB wake NOT set, "
7704 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007705 timeout, (timeleft / jiffies));
7706 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007707 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007708 } else if (retval == IOCB_BUSY) {
7709 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7710 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7711 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7712 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007713 } else {
7714 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007715 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007716 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007717 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007718 }
7719
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007720 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7721 creg_val = readl(phba->HCregaddr);
7722 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7723 writel(creg_val, phba->HCregaddr);
7724 readl(phba->HCregaddr); /* flush */
7725 }
7726
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007727 if (prspiocbq)
7728 piocb->context2 = NULL;
7729
7730 piocb->context_un.wait_queue = NULL;
7731 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007732 return retval;
7733}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007734
James Smarte59058c2008-08-24 21:49:00 -04007735/**
James Smart3621a712009-04-06 18:47:14 -04007736 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007737 * @phba: Pointer to HBA context object.
7738 * @pmboxq: Pointer to driver mailbox object.
7739 * @timeout: Timeout in number of seconds.
7740 *
7741 * This function issues the mailbox to firmware and waits for the
7742 * mailbox command to complete. If the mailbox command is not
7743 * completed within timeout seconds, it returns MBX_TIMEOUT.
7744 * The function waits for the mailbox completion using an
7745 * interruptible wait. If the thread is woken up due to a
7746 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7747 * should not free the mailbox resources, if this function returns
7748 * MBX_TIMEOUT.
7749 * This function will sleep while waiting for mailbox completion.
7750 * So, this function should not be called from any context which
7751 * does not allow sleeping. Due to the same reason, this function
7752 * cannot be called with interrupt disabled.
7753 * This function assumes that the mailbox completion occurs while
7754 * this function sleep. So, this function cannot be called from
7755 * the worker thread which processes mailbox completion.
7756 * This function is called in the context of HBA management
7757 * applications.
7758 * This function returns MBX_SUCCESS when successful.
7759 * This function is called with no lock held.
7760 **/
dea31012005-04-17 16:05:31 -05007761int
James Smart2e0fef82007-06-17 19:56:36 -05007762lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007763 uint32_t timeout)
7764{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007765 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007766 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007767 unsigned long flag;
dea31012005-04-17 16:05:31 -05007768
7769 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007770 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007771 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007772
James Smart495a7142008-06-14 22:52:59 -04007773 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007774 /* setup wake call as IOCB callback */
7775 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7776 /* setup context field to pass wait_queue pointer to wake function */
7777 pmboxq->context1 = &done_q;
7778
dea31012005-04-17 16:05:31 -05007779 /* now issue the command */
7780 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7781
7782 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007783 wait_event_interruptible_timeout(done_q,
7784 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7785 timeout * HZ);
7786
James Smart858c9f62007-06-17 19:56:39 -05007787 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007788 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007789 /*
7790 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7791 * else do not free the resources.
7792 */
7793 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007794 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007795 else {
James Smart7054a602007-04-25 09:52:34 -04007796 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007797 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7798 }
7799 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007800 }
7801
dea31012005-04-17 16:05:31 -05007802 return retval;
7803}
7804
James Smarte59058c2008-08-24 21:49:00 -04007805/**
James Smart3772a992009-05-22 14:50:54 -04007806 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007807 * @phba: Pointer to HBA context.
7808 *
James Smart3772a992009-05-22 14:50:54 -04007809 * This function is called to shutdown the driver's mailbox sub-system.
7810 * It first marks the mailbox sub-system is in a block state to prevent
7811 * the asynchronous mailbox command from issued off the pending mailbox
7812 * command queue. If the mailbox command sub-system shutdown is due to
7813 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7814 * the mailbox sub-system flush routine to forcefully bring down the
7815 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7816 * as with offline or HBA function reset), this routine will wait for the
7817 * outstanding mailbox command to complete before invoking the mailbox
7818 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007819 **/
James Smart3772a992009-05-22 14:50:54 -04007820void
7821lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007822{
James Smart3772a992009-05-22 14:50:54 -04007823 struct lpfc_sli *psli = &phba->sli;
7824 uint8_t actcmd = MBX_HEARTBEAT;
7825 unsigned long timeout;
7826
7827 spin_lock_irq(&phba->hbalock);
7828 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7829 spin_unlock_irq(&phba->hbalock);
7830
7831 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7832 spin_lock_irq(&phba->hbalock);
7833 if (phba->sli.mbox_active)
7834 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7835 spin_unlock_irq(&phba->hbalock);
7836 /* Determine how long we might wait for the active mailbox
7837 * command to be gracefully completed by firmware.
7838 */
7839 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7840 1000) + jiffies;
7841 while (phba->sli.mbox_active) {
7842 /* Check active mailbox complete status every 2ms */
7843 msleep(2);
7844 if (time_after(jiffies, timeout))
7845 /* Timeout, let the mailbox flush routine to
7846 * forcefully release active mailbox command
7847 */
7848 break;
7849 }
7850 }
7851 lpfc_sli_mbox_sys_flush(phba);
7852}
7853
7854/**
7855 * lpfc_sli_eratt_read - read sli-3 error attention events
7856 * @phba: Pointer to HBA context.
7857 *
7858 * This function is called to read the SLI3 device error attention registers
7859 * for possible error attention events. The caller must hold the hostlock
7860 * with spin_lock_irq().
7861 *
7862 * This fucntion returns 1 when there is Error Attention in the Host Attention
7863 * Register and returns 0 otherwise.
7864 **/
7865static int
7866lpfc_sli_eratt_read(struct lpfc_hba *phba)
7867{
James Smarted957682007-06-17 19:56:37 -05007868 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007869
James Smart3772a992009-05-22 14:50:54 -04007870 /* Read chip Host Attention (HA) register */
7871 ha_copy = readl(phba->HAregaddr);
7872 if (ha_copy & HA_ERATT) {
7873 /* Read host status register to retrieve error event */
7874 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007875
James Smart3772a992009-05-22 14:50:54 -04007876 /* Check if there is a deferred error condition is active */
7877 if ((HS_FFER1 & phba->work_hs) &&
7878 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7879 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007880 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007881 /* Clear all interrupt enable conditions */
7882 writel(0, phba->HCregaddr);
7883 readl(phba->HCregaddr);
7884 }
7885
7886 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007887 phba->work_ha |= HA_ERATT;
7888 /* Indicate polling handles this ERATT */
7889 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007890 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007891 }
James Smart3772a992009-05-22 14:50:54 -04007892 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007893}
7894
James Smarte59058c2008-08-24 21:49:00 -04007895/**
James Smartda0436e2009-05-22 14:51:39 -04007896 * lpfc_sli4_eratt_read - read sli-4 error attention events
7897 * @phba: Pointer to HBA context.
7898 *
7899 * This function is called to read the SLI4 device error attention registers
7900 * for possible error attention events. The caller must hold the hostlock
7901 * with spin_lock_irq().
7902 *
7903 * This fucntion returns 1 when there is Error Attention in the Host Attention
7904 * Register and returns 0 otherwise.
7905 **/
7906static int
7907lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7908{
7909 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007910
7911 /* For now, use the SLI4 device internal unrecoverable error
7912 * registers for error attention. This can be changed later.
7913 */
James Smarta747c9c2009-11-18 15:41:10 -05007914 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7915 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7916 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7917 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7919 "1423 HBA Unrecoverable error: "
7920 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7921 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7922 uerr_sta_lo, uerr_sta_hi,
7923 phba->sli4_hba.ue_mask_lo,
7924 phba->sli4_hba.ue_mask_hi);
7925 phba->work_status[0] = uerr_sta_lo;
7926 phba->work_status[1] = uerr_sta_hi;
7927 /* Set the driver HA work bitmap */
7928 phba->work_ha |= HA_ERATT;
7929 /* Indicate polling handles this ERATT */
7930 phba->hba_flag |= HBA_ERATT_HANDLED;
7931 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007932 }
7933 return 0;
7934}
7935
7936/**
James Smart3621a712009-04-06 18:47:14 -04007937 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007938 * @phba: Pointer to HBA context.
7939 *
James Smart3772a992009-05-22 14:50:54 -04007940 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007941 * error attention register bit for error attention events.
7942 *
7943 * This fucntion returns 1 when there is Error Attention in the Host Attention
7944 * Register and returns 0 otherwise.
7945 **/
7946int
7947lpfc_sli_check_eratt(struct lpfc_hba *phba)
7948{
7949 uint32_t ha_copy;
7950
7951 /* If somebody is waiting to handle an eratt, don't process it
7952 * here. The brdkill function will do this.
7953 */
7954 if (phba->link_flag & LS_IGNORE_ERATT)
7955 return 0;
7956
7957 /* Check if interrupt handler handles this ERATT */
7958 spin_lock_irq(&phba->hbalock);
7959 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7960 /* Interrupt handler has handled ERATT */
7961 spin_unlock_irq(&phba->hbalock);
7962 return 0;
7963 }
7964
James Smarta257bf92009-04-06 18:48:10 -04007965 /*
7966 * If there is deferred error attention, do not check for error
7967 * attention
7968 */
7969 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7970 spin_unlock_irq(&phba->hbalock);
7971 return 0;
7972 }
7973
James Smart3772a992009-05-22 14:50:54 -04007974 /* If PCI channel is offline, don't process it */
7975 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007976 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007977 return 0;
7978 }
7979
7980 switch (phba->sli_rev) {
7981 case LPFC_SLI_REV2:
7982 case LPFC_SLI_REV3:
7983 /* Read chip Host Attention (HA) register */
7984 ha_copy = lpfc_sli_eratt_read(phba);
7985 break;
James Smartda0436e2009-05-22 14:51:39 -04007986 case LPFC_SLI_REV4:
7987 /* Read devcie Uncoverable Error (UERR) registers */
7988 ha_copy = lpfc_sli4_eratt_read(phba);
7989 break;
James Smart3772a992009-05-22 14:50:54 -04007990 default:
7991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7992 "0299 Invalid SLI revision (%d)\n",
7993 phba->sli_rev);
7994 ha_copy = 0;
7995 break;
James Smart93996272008-08-24 21:50:30 -04007996 }
7997 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007998
7999 return ha_copy;
8000}
8001
8002/**
8003 * lpfc_intr_state_check - Check device state for interrupt handling
8004 * @phba: Pointer to HBA context.
8005 *
8006 * This inline routine checks whether a device or its PCI slot is in a state
8007 * that the interrupt should be handled.
8008 *
8009 * This function returns 0 if the device or the PCI slot is in a state that
8010 * interrupt should be handled, otherwise -EIO.
8011 */
8012static inline int
8013lpfc_intr_state_check(struct lpfc_hba *phba)
8014{
8015 /* If the pci channel is offline, ignore all the interrupts */
8016 if (unlikely(pci_channel_offline(phba->pcidev)))
8017 return -EIO;
8018
8019 /* Update device level interrupt statistics */
8020 phba->sli.slistat.sli_intr++;
8021
8022 /* Ignore all interrupts during initialization. */
8023 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8024 return -EIO;
8025
James Smart93996272008-08-24 21:50:30 -04008026 return 0;
8027}
8028
8029/**
James Smart3772a992009-05-22 14:50:54 -04008030 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008031 * @irq: Interrupt number.
8032 * @dev_id: The device context pointer.
8033 *
James Smart93996272008-08-24 21:50:30 -04008034 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008035 * service routine when device with SLI-3 interface spec is enabled with
8036 * MSI-X multi-message interrupt mode and there are slow-path events in
8037 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8038 * interrupt mode, this function is called as part of the device-level
8039 * interrupt handler. When the PCI slot is in error recovery or the HBA
8040 * is undergoing initialization, the interrupt handler will not process
8041 * the interrupt. The link attention and ELS ring attention events are
8042 * handled by the worker thread. The interrupt handler signals the worker
8043 * thread and returns for these events. This function is called without
8044 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008045 * structures.
8046 *
8047 * This function returns IRQ_HANDLED when interrupt is handled else it
8048 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008049 **/
dea31012005-04-17 16:05:31 -05008050irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008051lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008052{
James Smart2e0fef82007-06-17 19:56:36 -05008053 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008054 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008055 uint32_t work_ha_copy;
8056 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008057 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008058 uint32_t control;
8059
James Smart92d7f7b2007-06-17 19:56:38 -05008060 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008061 struct lpfc_vport *vport;
8062 struct lpfc_nodelist *ndlp;
8063 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008064 LPFC_MBOXQ_t *pmb;
8065 int rc;
8066
dea31012005-04-17 16:05:31 -05008067 /*
8068 * Get the driver's phba structure from the dev_id and
8069 * assume the HBA is not interrupting.
8070 */
James Smart93996272008-08-24 21:50:30 -04008071 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008072
8073 if (unlikely(!phba))
8074 return IRQ_NONE;
8075
dea31012005-04-17 16:05:31 -05008076 /*
James Smart93996272008-08-24 21:50:30 -04008077 * Stuff needs to be attented to when this function is invoked as an
8078 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008079 */
James Smart93996272008-08-24 21:50:30 -04008080 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008081 /* Check device state for handling interrupt */
8082 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008083 return IRQ_NONE;
8084 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008085 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008086 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008087 /* If somebody is waiting to handle an eratt don't process it
8088 * here. The brdkill function will do this.
8089 */
8090 if (phba->link_flag & LS_IGNORE_ERATT)
8091 ha_copy &= ~HA_ERATT;
8092 /* Check the need for handling ERATT in interrupt handler */
8093 if (ha_copy & HA_ERATT) {
8094 if (phba->hba_flag & HBA_ERATT_HANDLED)
8095 /* ERATT polling has handled ERATT */
8096 ha_copy &= ~HA_ERATT;
8097 else
8098 /* Indicate interrupt handler handles ERATT */
8099 phba->hba_flag |= HBA_ERATT_HANDLED;
8100 }
James Smarta257bf92009-04-06 18:48:10 -04008101
8102 /*
8103 * If there is deferred error attention, do not check for any
8104 * interrupt.
8105 */
8106 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008107 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008108 return IRQ_NONE;
8109 }
8110
James Smart93996272008-08-24 21:50:30 -04008111 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008112 hc_copy = readl(phba->HCregaddr);
8113 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8114 HC_LAINT_ENA | HC_ERINT_ENA),
8115 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008116 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8117 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008118 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008119 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008120 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008121 } else
8122 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008123
dea31012005-04-17 16:05:31 -05008124 work_ha_copy = ha_copy & phba->work_ha_mask;
8125
James Smart93996272008-08-24 21:50:30 -04008126 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008127 if (work_ha_copy & HA_LATT) {
8128 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8129 /*
8130 * Turn off Link Attention interrupts
8131 * until CLEAR_LA done
8132 */
James Smart5b75da22008-12-04 22:39:35 -05008133 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008134 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8135 control = readl(phba->HCregaddr);
8136 control &= ~HC_LAINT_ENA;
8137 writel(control, phba->HCregaddr);
8138 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008139 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008140 }
8141 else
8142 work_ha_copy &= ~HA_LATT;
8143 }
8144
James Smart93996272008-08-24 21:50:30 -04008145 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008146 /*
8147 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8148 * the only slow ring.
8149 */
8150 status = (work_ha_copy &
8151 (HA_RXMASK << (4*LPFC_ELS_RING)));
8152 status >>= (4*LPFC_ELS_RING);
8153 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008154 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008155 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008156
8157 lpfc_debugfs_slow_ring_trc(phba,
8158 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8159 control, status,
8160 (uint32_t)phba->sli.slistat.sli_intr);
8161
James Smart858c9f62007-06-17 19:56:39 -05008162 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008163 lpfc_debugfs_slow_ring_trc(phba,
8164 "ISR Disable ring:"
8165 "pwork:x%x hawork:x%x wait:x%x",
8166 phba->work_ha, work_ha_copy,
8167 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008168 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008169
James Smart858c9f62007-06-17 19:56:39 -05008170 control &=
8171 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008172 writel(control, phba->HCregaddr);
8173 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008174 }
James Smarta58cbd52007-08-02 11:09:43 -04008175 else {
8176 lpfc_debugfs_slow_ring_trc(phba,
8177 "ISR slow ring: pwork:"
8178 "x%x hawork:x%x wait:x%x",
8179 phba->work_ha, work_ha_copy,
8180 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008181 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008182 }
James Smart5b75da22008-12-04 22:39:35 -05008183 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008184 }
8185 }
James Smart5b75da22008-12-04 22:39:35 -05008186 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008187 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008188 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008189 /*
8190 * Check if there is a deferred error condition
8191 * is active
8192 */
8193 if ((HS_FFER1 & phba->work_hs) &&
8194 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8195 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8196 phba->hba_flag |= DEFER_ERATT;
8197 /* Clear all interrupt enable conditions */
8198 writel(0, phba->HCregaddr);
8199 readl(phba->HCregaddr);
8200 }
8201 }
8202
James Smart93996272008-08-24 21:50:30 -04008203 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008204 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008205 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008206 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008207 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008208
8209 /* First check out the status word */
8210 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8211 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008212 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008213 /*
8214 * Stray Mailbox Interrupt, mbxCommand <cmd>
8215 * mbxStatus <status>
8216 */
James Smart09372822008-01-11 01:52:54 -05008217 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008218 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008219 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008220 "Interrupt mbxCommand x%x "
8221 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008222 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008223 pmbox->mbxCommand,
8224 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008225 /* clear mailbox attention bit */
8226 work_ha_copy &= ~HA_MBATT;
8227 } else {
James Smart97eab632008-04-07 10:16:05 -04008228 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008229 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008230 phba->last_completion_time = jiffies;
8231 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008232 if (pmb->mbox_cmpl) {
8233 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8234 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008235 if (pmb->out_ext_byte_len &&
8236 pmb->context2)
8237 lpfc_sli_pcimem_bcopy(
8238 phba->mbox_ext,
8239 pmb->context2,
8240 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008241 }
James Smart09372822008-01-11 01:52:54 -05008242 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8243 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8244
8245 lpfc_debugfs_disc_trc(vport,
8246 LPFC_DISC_TRC_MBOX_VPORT,
8247 "MBOX dflt rpi: : "
8248 "status:x%x rpi:x%x",
8249 (uint32_t)pmbox->mbxStatus,
8250 pmbox->un.varWords[0], 0);
8251
8252 if (!pmbox->mbxStatus) {
8253 mp = (struct lpfc_dmabuf *)
8254 (pmb->context1);
8255 ndlp = (struct lpfc_nodelist *)
8256 pmb->context2;
8257
8258 /* Reg_LOGIN of dflt RPI was
8259 * successful. new lets get
8260 * rid of the RPI using the
8261 * same mbox buffer.
8262 */
8263 lpfc_unreg_login(phba,
8264 vport->vpi,
8265 pmbox->un.varWords[0],
8266 pmb);
8267 pmb->mbox_cmpl =
8268 lpfc_mbx_cmpl_dflt_rpi;
8269 pmb->context1 = mp;
8270 pmb->context2 = ndlp;
8271 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008272 rc = lpfc_sli_issue_mbox(phba,
8273 pmb,
8274 MBX_NOWAIT);
8275 if (rc != MBX_BUSY)
8276 lpfc_printf_log(phba,
8277 KERN_ERR,
8278 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008279 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008280 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008281 if (rc != MBX_NOT_FINISHED)
8282 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008283 }
8284 }
James Smart5b75da22008-12-04 22:39:35 -05008285 spin_lock_irqsave(
8286 &phba->pport->work_port_lock,
8287 iflag);
James Smart09372822008-01-11 01:52:54 -05008288 phba->pport->work_port_events &=
8289 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008290 spin_unlock_irqrestore(
8291 &phba->pport->work_port_lock,
8292 iflag);
James Smart09372822008-01-11 01:52:54 -05008293 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008294 }
James Smart97eab632008-04-07 10:16:05 -04008295 } else
James Smart5b75da22008-12-04 22:39:35 -05008296 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008297
James Smart92d7f7b2007-06-17 19:56:38 -05008298 if ((work_ha_copy & HA_MBATT) &&
8299 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008300send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008301 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008302 do {
8303 rc = lpfc_sli_issue_mbox(phba, NULL,
8304 MBX_NOWAIT);
8305 } while (rc == MBX_NOT_FINISHED);
8306 if (rc != MBX_SUCCESS)
8307 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8308 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008309 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008310 }
8311
James Smart5b75da22008-12-04 22:39:35 -05008312 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008313 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008314 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008315 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008316 }
James Smart93996272008-08-24 21:50:30 -04008317 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008318
James Smart3772a992009-05-22 14:50:54 -04008319} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008320
8321/**
James Smart3772a992009-05-22 14:50:54 -04008322 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008323 * @irq: Interrupt number.
8324 * @dev_id: The device context pointer.
8325 *
8326 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008327 * service routine when device with SLI-3 interface spec is enabled with
8328 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8329 * ring event in the HBA. However, when the device is enabled with either
8330 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8331 * device-level interrupt handler. When the PCI slot is in error recovery
8332 * or the HBA is undergoing initialization, the interrupt handler will not
8333 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8334 * the intrrupt context. This function is called without any lock held.
8335 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008336 *
8337 * This function returns IRQ_HANDLED when interrupt is handled else it
8338 * returns IRQ_NONE.
8339 **/
8340irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008341lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008342{
8343 struct lpfc_hba *phba;
8344 uint32_t ha_copy;
8345 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008346 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008347
8348 /* Get the driver's phba structure from the dev_id and
8349 * assume the HBA is not interrupting.
8350 */
8351 phba = (struct lpfc_hba *) dev_id;
8352
8353 if (unlikely(!phba))
8354 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008355
8356 /*
James Smart93996272008-08-24 21:50:30 -04008357 * Stuff needs to be attented to when this function is invoked as an
8358 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008359 */
James Smart93996272008-08-24 21:50:30 -04008360 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008361 /* Check device state for handling interrupt */
8362 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008363 return IRQ_NONE;
8364 /* Need to read HA REG for FCP ring and other ring events */
8365 ha_copy = readl(phba->HAregaddr);
8366 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008367 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008368 /*
8369 * If there is deferred error attention, do not check for
8370 * any interrupt.
8371 */
8372 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008373 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008374 return IRQ_NONE;
8375 }
James Smart93996272008-08-24 21:50:30 -04008376 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8377 phba->HAregaddr);
8378 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008379 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008380 } else
8381 ha_copy = phba->ha_copy;
8382
8383 /*
8384 * Process all events on FCP ring. Take the optimized path for FCP IO.
8385 */
8386 ha_copy &= ~(phba->work_ha_mask);
8387
8388 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008389 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008390 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008391 lpfc_sli_handle_fast_ring_event(phba,
8392 &phba->sli.ring[LPFC_FCP_RING],
8393 status);
James Smarta4bc3372006-12-02 13:34:16 -05008394
8395 if (phba->cfg_multi_ring_support == 2) {
8396 /*
James Smart93996272008-08-24 21:50:30 -04008397 * Process all events on extra ring. Take the optimized path
8398 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008399 */
James Smart93996272008-08-24 21:50:30 -04008400 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008401 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008402 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008403 lpfc_sli_handle_fast_ring_event(phba,
8404 &phba->sli.ring[LPFC_EXTRA_RING],
8405 status);
8406 }
8407 }
dea31012005-04-17 16:05:31 -05008408 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008409} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008410
James Smart93996272008-08-24 21:50:30 -04008411/**
James Smart3772a992009-05-22 14:50:54 -04008412 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008413 * @irq: Interrupt number.
8414 * @dev_id: The device context pointer.
8415 *
James Smart3772a992009-05-22 14:50:54 -04008416 * This function is the HBA device-level interrupt handler to device with
8417 * SLI-3 interface spec, called from the PCI layer when either MSI or
8418 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8419 * requires driver attention. This function invokes the slow-path interrupt
8420 * attention handling function and fast-path interrupt attention handling
8421 * function in turn to process the relevant HBA attention events. This
8422 * function is called without any lock held. It gets the hbalock to access
8423 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008424 *
8425 * This function returns IRQ_HANDLED when interrupt is handled, else it
8426 * returns IRQ_NONE.
8427 **/
8428irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008429lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008430{
8431 struct lpfc_hba *phba;
8432 irqreturn_t sp_irq_rc, fp_irq_rc;
8433 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008434 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008435
8436 /*
8437 * Get the driver's phba structure from the dev_id and
8438 * assume the HBA is not interrupting.
8439 */
8440 phba = (struct lpfc_hba *) dev_id;
8441
8442 if (unlikely(!phba))
8443 return IRQ_NONE;
8444
James Smart3772a992009-05-22 14:50:54 -04008445 /* Check device state for handling interrupt */
8446 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008447 return IRQ_NONE;
8448
8449 spin_lock(&phba->hbalock);
8450 phba->ha_copy = readl(phba->HAregaddr);
8451 if (unlikely(!phba->ha_copy)) {
8452 spin_unlock(&phba->hbalock);
8453 return IRQ_NONE;
8454 } else if (phba->ha_copy & HA_ERATT) {
8455 if (phba->hba_flag & HBA_ERATT_HANDLED)
8456 /* ERATT polling has handled ERATT */
8457 phba->ha_copy &= ~HA_ERATT;
8458 else
8459 /* Indicate interrupt handler handles ERATT */
8460 phba->hba_flag |= HBA_ERATT_HANDLED;
8461 }
8462
James Smarta257bf92009-04-06 18:48:10 -04008463 /*
8464 * If there is deferred error attention, do not check for any interrupt.
8465 */
8466 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8467 spin_unlock_irq(&phba->hbalock);
8468 return IRQ_NONE;
8469 }
8470
James Smart93996272008-08-24 21:50:30 -04008471 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008472 hc_copy = readl(phba->HCregaddr);
8473 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8474 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8475 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008476 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008477 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008478 readl(phba->HAregaddr); /* flush */
8479 spin_unlock(&phba->hbalock);
8480
8481 /*
8482 * Invokes slow-path host attention interrupt handling as appropriate.
8483 */
8484
8485 /* status of events with mailbox and link attention */
8486 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8487
8488 /* status of events with ELS ring */
8489 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8490 status2 >>= (4*LPFC_ELS_RING);
8491
8492 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008493 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008494 else
8495 sp_irq_rc = IRQ_NONE;
8496
8497 /*
8498 * Invoke fast-path host attention interrupt handling as appropriate.
8499 */
8500
8501 /* status of events with FCP ring */
8502 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8503 status1 >>= (4*LPFC_FCP_RING);
8504
8505 /* status of events with extra ring */
8506 if (phba->cfg_multi_ring_support == 2) {
8507 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8508 status2 >>= (4*LPFC_EXTRA_RING);
8509 } else
8510 status2 = 0;
8511
8512 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008513 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008514 else
8515 fp_irq_rc = IRQ_NONE;
8516
8517 /* Return device-level interrupt handling status */
8518 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008519} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008520
8521/**
8522 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8523 * @phba: pointer to lpfc hba data structure.
8524 *
8525 * This routine is invoked by the worker thread to process all the pending
8526 * SLI4 FCP abort XRI events.
8527 **/
8528void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8529{
8530 struct lpfc_cq_event *cq_event;
8531
8532 /* First, declare the fcp xri abort event has been handled */
8533 spin_lock_irq(&phba->hbalock);
8534 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8535 spin_unlock_irq(&phba->hbalock);
8536 /* Now, handle all the fcp xri abort events */
8537 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8538 /* Get the first event from the head of the event queue */
8539 spin_lock_irq(&phba->hbalock);
8540 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8541 cq_event, struct lpfc_cq_event, list);
8542 spin_unlock_irq(&phba->hbalock);
8543 /* Notify aborted XRI for FCP work queue */
8544 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8545 /* Free the event processed back to the free pool */
8546 lpfc_sli4_cq_event_release(phba, cq_event);
8547 }
8548}
8549
8550/**
8551 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8552 * @phba: pointer to lpfc hba data structure.
8553 *
8554 * This routine is invoked by the worker thread to process all the pending
8555 * SLI4 els abort xri events.
8556 **/
8557void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8558{
8559 struct lpfc_cq_event *cq_event;
8560
8561 /* First, declare the els xri abort event has been handled */
8562 spin_lock_irq(&phba->hbalock);
8563 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8564 spin_unlock_irq(&phba->hbalock);
8565 /* Now, handle all the els xri abort events */
8566 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8567 /* Get the first event from the head of the event queue */
8568 spin_lock_irq(&phba->hbalock);
8569 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8570 cq_event, struct lpfc_cq_event, list);
8571 spin_unlock_irq(&phba->hbalock);
8572 /* Notify aborted XRI for ELS work queue */
8573 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8574 /* Free the event processed back to the free pool */
8575 lpfc_sli4_cq_event_release(phba, cq_event);
8576 }
8577}
8578
James Smart341af102010-01-26 23:07:37 -05008579/**
8580 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8581 * @phba: pointer to lpfc hba data structure
8582 * @pIocbIn: pointer to the rspiocbq
8583 * @pIocbOut: pointer to the cmdiocbq
8584 * @wcqe: pointer to the complete wcqe
8585 *
8586 * This routine transfers the fields of a command iocbq to a response iocbq
8587 * by copying all the IOCB fields from command iocbq and transferring the
8588 * completion status information from the complete wcqe.
8589 **/
James Smart4f774512009-05-22 14:52:35 -04008590static void
James Smart341af102010-01-26 23:07:37 -05008591lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8592 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008593 struct lpfc_iocbq *pIocbOut,
8594 struct lpfc_wcqe_complete *wcqe)
8595{
James Smart341af102010-01-26 23:07:37 -05008596 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008597 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8598
8599 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8600 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008601 /* Map WCQE parameters into irspiocb parameters */
8602 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8603 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8604 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8605 pIocbIn->iocb.un.fcpi.fcpi_parm =
8606 pIocbOut->iocb.un.fcpi.fcpi_parm -
8607 wcqe->total_data_placed;
8608 else
8609 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008610 else {
James Smart4f774512009-05-22 14:52:35 -04008611 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008612 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8613 }
James Smart341af102010-01-26 23:07:37 -05008614
8615 /* Pick up HBA exchange busy condition */
8616 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8617 spin_lock_irqsave(&phba->hbalock, iflags);
8618 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8619 spin_unlock_irqrestore(&phba->hbalock, iflags);
8620 }
James Smart4f774512009-05-22 14:52:35 -04008621}
8622
8623/**
James Smart45ed1192009-10-02 15:17:02 -04008624 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8625 * @phba: Pointer to HBA context object.
8626 * @wcqe: Pointer to work-queue completion queue entry.
8627 *
8628 * This routine handles an ELS work-queue completion event and construct
8629 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8630 * discovery engine to handle.
8631 *
8632 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8633 **/
8634static struct lpfc_iocbq *
8635lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8636 struct lpfc_iocbq *irspiocbq)
8637{
8638 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8639 struct lpfc_iocbq *cmdiocbq;
8640 struct lpfc_wcqe_complete *wcqe;
8641 unsigned long iflags;
8642
8643 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8644 spin_lock_irqsave(&phba->hbalock, iflags);
8645 pring->stats.iocb_event++;
8646 /* Look up the ELS command IOCB and create pseudo response IOCB */
8647 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8648 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8649 spin_unlock_irqrestore(&phba->hbalock, iflags);
8650
8651 if (unlikely(!cmdiocbq)) {
8652 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8653 "0386 ELS complete with no corresponding "
8654 "cmdiocb: iotag (%d)\n",
8655 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8656 lpfc_sli_release_iocbq(phba, irspiocbq);
8657 return NULL;
8658 }
8659
8660 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008661 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008662
8663 return irspiocbq;
8664}
8665
8666/**
James Smart04c68492009-05-22 14:52:52 -04008667 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8668 * @phba: Pointer to HBA context object.
8669 * @cqe: Pointer to mailbox completion queue entry.
8670 *
8671 * This routine process a mailbox completion queue entry with asynchrous
8672 * event.
8673 *
8674 * Return: true if work posted to worker thread, otherwise false.
8675 **/
8676static bool
8677lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8678{
8679 struct lpfc_cq_event *cq_event;
8680 unsigned long iflags;
8681
8682 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8683 "0392 Async Event: word0:x%x, word1:x%x, "
8684 "word2:x%x, word3:x%x\n", mcqe->word0,
8685 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8686
8687 /* Allocate a new internal CQ_EVENT entry */
8688 cq_event = lpfc_sli4_cq_event_alloc(phba);
8689 if (!cq_event) {
8690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8691 "0394 Failed to allocate CQ_EVENT entry\n");
8692 return false;
8693 }
8694
8695 /* Move the CQE into an asynchronous event entry */
8696 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8697 spin_lock_irqsave(&phba->hbalock, iflags);
8698 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8699 /* Set the async event flag */
8700 phba->hba_flag |= ASYNC_EVENT;
8701 spin_unlock_irqrestore(&phba->hbalock, iflags);
8702
8703 return true;
8704}
8705
8706/**
8707 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8708 * @phba: Pointer to HBA context object.
8709 * @cqe: Pointer to mailbox completion queue entry.
8710 *
8711 * This routine process a mailbox completion queue entry with mailbox
8712 * completion event.
8713 *
8714 * Return: true if work posted to worker thread, otherwise false.
8715 **/
8716static bool
8717lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8718{
8719 uint32_t mcqe_status;
8720 MAILBOX_t *mbox, *pmbox;
8721 struct lpfc_mqe *mqe;
8722 struct lpfc_vport *vport;
8723 struct lpfc_nodelist *ndlp;
8724 struct lpfc_dmabuf *mp;
8725 unsigned long iflags;
8726 LPFC_MBOXQ_t *pmb;
8727 bool workposted = false;
8728 int rc;
8729
8730 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8731 if (!bf_get(lpfc_trailer_completed, mcqe))
8732 goto out_no_mqe_complete;
8733
8734 /* Get the reference to the active mbox command */
8735 spin_lock_irqsave(&phba->hbalock, iflags);
8736 pmb = phba->sli.mbox_active;
8737 if (unlikely(!pmb)) {
8738 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8739 "1832 No pending MBOX command to handle\n");
8740 spin_unlock_irqrestore(&phba->hbalock, iflags);
8741 goto out_no_mqe_complete;
8742 }
8743 spin_unlock_irqrestore(&phba->hbalock, iflags);
8744 mqe = &pmb->u.mqe;
8745 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8746 mbox = phba->mbox;
8747 vport = pmb->vport;
8748
8749 /* Reset heartbeat timer */
8750 phba->last_completion_time = jiffies;
8751 del_timer(&phba->sli.mbox_tmo);
8752
8753 /* Move mbox data to caller's mailbox region, do endian swapping */
8754 if (pmb->mbox_cmpl && mbox)
8755 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8756 /* Set the mailbox status with SLI4 range 0x4000 */
8757 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8758 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8759 bf_set(lpfc_mqe_status, mqe,
8760 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8761
8762 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8763 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8764 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8765 "MBOX dflt rpi: status:x%x rpi:x%x",
8766 mcqe_status,
8767 pmbox->un.varWords[0], 0);
8768 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8769 mp = (struct lpfc_dmabuf *)(pmb->context1);
8770 ndlp = (struct lpfc_nodelist *)pmb->context2;
8771 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8772 * RID of the PPI using the same mbox buffer.
8773 */
8774 lpfc_unreg_login(phba, vport->vpi,
8775 pmbox->un.varWords[0], pmb);
8776 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8777 pmb->context1 = mp;
8778 pmb->context2 = ndlp;
8779 pmb->vport = vport;
8780 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8781 if (rc != MBX_BUSY)
8782 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8783 LOG_SLI, "0385 rc should "
8784 "have been MBX_BUSY\n");
8785 if (rc != MBX_NOT_FINISHED)
8786 goto send_current_mbox;
8787 }
8788 }
8789 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8790 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8791 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8792
8793 /* There is mailbox completion work to do */
8794 spin_lock_irqsave(&phba->hbalock, iflags);
8795 __lpfc_mbox_cmpl_put(phba, pmb);
8796 phba->work_ha |= HA_MBATT;
8797 spin_unlock_irqrestore(&phba->hbalock, iflags);
8798 workposted = true;
8799
8800send_current_mbox:
8801 spin_lock_irqsave(&phba->hbalock, iflags);
8802 /* Release the mailbox command posting token */
8803 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8804 /* Setting active mailbox pointer need to be in sync to flag clear */
8805 phba->sli.mbox_active = NULL;
8806 spin_unlock_irqrestore(&phba->hbalock, iflags);
8807 /* Wake up worker thread to post the next pending mailbox command */
8808 lpfc_worker_wake_up(phba);
8809out_no_mqe_complete:
8810 if (bf_get(lpfc_trailer_consumed, mcqe))
8811 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8812 return workposted;
8813}
8814
8815/**
8816 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8817 * @phba: Pointer to HBA context object.
8818 * @cqe: Pointer to mailbox completion queue entry.
8819 *
8820 * This routine process a mailbox completion queue entry, it invokes the
8821 * proper mailbox complete handling or asynchrous event handling routine
8822 * according to the MCQE's async bit.
8823 *
8824 * Return: true if work posted to worker thread, otherwise false.
8825 **/
8826static bool
8827lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8828{
8829 struct lpfc_mcqe mcqe;
8830 bool workposted;
8831
8832 /* Copy the mailbox MCQE and convert endian order as needed */
8833 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8834
8835 /* Invoke the proper event handling routine */
8836 if (!bf_get(lpfc_trailer_async, &mcqe))
8837 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8838 else
8839 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8840 return workposted;
8841}
8842
8843/**
James Smart4f774512009-05-22 14:52:35 -04008844 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8845 * @phba: Pointer to HBA context object.
8846 * @wcqe: Pointer to work-queue completion queue entry.
8847 *
8848 * This routine handles an ELS work-queue completion event.
8849 *
8850 * Return: true if work posted to worker thread, otherwise false.
8851 **/
8852static bool
8853lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8854 struct lpfc_wcqe_complete *wcqe)
8855{
James Smart4f774512009-05-22 14:52:35 -04008856 struct lpfc_iocbq *irspiocbq;
8857 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04008858 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04008859
James Smart45ed1192009-10-02 15:17:02 -04008860 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008861 irspiocbq = lpfc_sli_get_iocbq(phba);
8862 if (!irspiocbq) {
8863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04008864 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8865 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8866 pring->txq_cnt, phba->iocb_cnt,
8867 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8868 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04008869 return false;
James Smart4f774512009-05-22 14:52:35 -04008870 }
James Smart4f774512009-05-22 14:52:35 -04008871
James Smart45ed1192009-10-02 15:17:02 -04008872 /* Save off the slow-path queue event for work thread to process */
8873 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008874 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008875 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008876 &phba->sli4_hba.sp_queue_event);
8877 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008878 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008879
James Smart45ed1192009-10-02 15:17:02 -04008880 return true;
James Smart4f774512009-05-22 14:52:35 -04008881}
8882
8883/**
8884 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8885 * @phba: Pointer to HBA context object.
8886 * @wcqe: Pointer to work-queue completion queue entry.
8887 *
8888 * This routine handles slow-path WQ entry comsumed event by invoking the
8889 * proper WQ release routine to the slow-path WQ.
8890 **/
8891static void
8892lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8893 struct lpfc_wcqe_release *wcqe)
8894{
8895 /* Check for the slow-path ELS work queue */
8896 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8897 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8898 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8899 else
8900 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8901 "2579 Slow-path wqe consume event carries "
8902 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8903 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8904 phba->sli4_hba.els_wq->queue_id);
8905}
8906
8907/**
8908 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8909 * @phba: Pointer to HBA context object.
8910 * @cq: Pointer to a WQ completion queue.
8911 * @wcqe: Pointer to work-queue completion queue entry.
8912 *
8913 * This routine handles an XRI abort event.
8914 *
8915 * Return: true if work posted to worker thread, otherwise false.
8916 **/
8917static bool
8918lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8919 struct lpfc_queue *cq,
8920 struct sli4_wcqe_xri_aborted *wcqe)
8921{
8922 bool workposted = false;
8923 struct lpfc_cq_event *cq_event;
8924 unsigned long iflags;
8925
8926 /* Allocate a new internal CQ_EVENT entry */
8927 cq_event = lpfc_sli4_cq_event_alloc(phba);
8928 if (!cq_event) {
8929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8930 "0602 Failed to allocate CQ_EVENT entry\n");
8931 return false;
8932 }
8933
8934 /* Move the CQE into the proper xri abort event list */
8935 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8936 switch (cq->subtype) {
8937 case LPFC_FCP:
8938 spin_lock_irqsave(&phba->hbalock, iflags);
8939 list_add_tail(&cq_event->list,
8940 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8941 /* Set the fcp xri abort event flag */
8942 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8943 spin_unlock_irqrestore(&phba->hbalock, iflags);
8944 workposted = true;
8945 break;
8946 case LPFC_ELS:
8947 spin_lock_irqsave(&phba->hbalock, iflags);
8948 list_add_tail(&cq_event->list,
8949 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8950 /* Set the els xri abort event flag */
8951 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8952 spin_unlock_irqrestore(&phba->hbalock, iflags);
8953 workposted = true;
8954 break;
8955 default:
8956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8957 "0603 Invalid work queue CQE subtype (x%x)\n",
8958 cq->subtype);
8959 workposted = false;
8960 break;
8961 }
8962 return workposted;
8963}
8964
8965/**
James Smart4d9ab992009-10-02 15:16:39 -04008966 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008967 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008968 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008969 *
James Smart4d9ab992009-10-02 15:16:39 -04008970 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008971 *
8972 * Return: true if work posted to worker thread, otherwise false.
8973 **/
8974static bool
James Smart4d9ab992009-10-02 15:16:39 -04008975lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8976{
8977 bool workposted = false;
8978 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8979 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8980 struct hbq_dmabuf *dma_buf;
8981 uint32_t status;
8982 unsigned long iflags;
8983
James Smart4d9ab992009-10-02 15:16:39 -04008984 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8985 goto out;
8986
8987 status = bf_get(lpfc_rcqe_status, rcqe);
8988 switch (status) {
8989 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8991 "2537 Receive Frame Truncated!!\n");
8992 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008993 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008994 spin_lock_irqsave(&phba->hbalock, iflags);
8995 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8996 if (!dma_buf) {
8997 spin_unlock_irqrestore(&phba->hbalock, iflags);
8998 goto out;
8999 }
9000 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9001 /* save off the frame for the word thread to process */
9002 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009003 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009004 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009005 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009006 spin_unlock_irqrestore(&phba->hbalock, iflags);
9007 workposted = true;
9008 break;
9009 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9010 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9011 /* Post more buffers if possible */
9012 spin_lock_irqsave(&phba->hbalock, iflags);
9013 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9014 spin_unlock_irqrestore(&phba->hbalock, iflags);
9015 workposted = true;
9016 break;
9017 }
9018out:
9019 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009020}
9021
9022/**
9023 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9024 * @phba: Pointer to HBA context object.
9025 * @cq: Pointer to the completion queue.
9026 * @wcqe: Pointer to a completion queue entry.
9027 *
9028 * This routine process a slow-path work-queue or recieve queue completion queue
9029 * entry.
9030 *
9031 * Return: true if work posted to worker thread, otherwise false.
9032 **/
9033static bool
9034lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009035 struct lpfc_cqe *cqe)
9036{
James Smart45ed1192009-10-02 15:17:02 -04009037 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009038 bool workposted = false;
9039
9040 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009041 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009042
9043 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009044 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009045 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009046 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04009047 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009048 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009049 break;
9050 case CQE_CODE_RELEASE_WQE:
9051 /* Process the WQ release event */
9052 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009053 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009054 break;
9055 case CQE_CODE_XRI_ABORTED:
9056 /* Process the WQ XRI abort event */
9057 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009058 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009059 break;
James Smart4d9ab992009-10-02 15:16:39 -04009060 case CQE_CODE_RECEIVE:
9061 /* Process the RQ event */
9062 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009063 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009064 break;
James Smart4f774512009-05-22 14:52:35 -04009065 default:
9066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9067 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009068 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009069 break;
9070 }
9071 return workposted;
9072}
9073
9074/**
James Smart4f774512009-05-22 14:52:35 -04009075 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9076 * @phba: Pointer to HBA context object.
9077 * @eqe: Pointer to fast-path event queue entry.
9078 *
9079 * This routine process a event queue entry from the slow-path event queue.
9080 * It will check the MajorCode and MinorCode to determine this is for a
9081 * completion event on a completion queue, if not, an error shall be logged
9082 * and just return. Otherwise, it will get to the corresponding completion
9083 * queue and process all the entries on that completion queue, rearm the
9084 * completion queue, and then return.
9085 *
9086 **/
9087static void
9088lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9089{
9090 struct lpfc_queue *cq = NULL, *childq, *speq;
9091 struct lpfc_cqe *cqe;
9092 bool workposted = false;
9093 int ecount = 0;
9094 uint16_t cqid;
9095
James Smartcb5172e2010-03-15 11:25:07 -04009096 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009097 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9098 "0359 Not a valid slow-path completion "
9099 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009100 bf_get_le32(lpfc_eqe_major_code, eqe),
9101 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009102 return;
9103 }
9104
9105 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009106 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009107
9108 /* Search for completion queue pointer matching this cqid */
9109 speq = phba->sli4_hba.sp_eq;
9110 list_for_each_entry(childq, &speq->child_list, list) {
9111 if (childq->queue_id == cqid) {
9112 cq = childq;
9113 break;
9114 }
9115 }
9116 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009117 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9119 "0365 Slow-path CQ identifier "
9120 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009121 return;
9122 }
9123
9124 /* Process all the entries to the CQ */
9125 switch (cq->type) {
9126 case LPFC_MCQ:
9127 while ((cqe = lpfc_sli4_cq_get(cq))) {
9128 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9129 if (!(++ecount % LPFC_GET_QE_REL_INT))
9130 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9131 }
9132 break;
9133 case LPFC_WCQ:
9134 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009135 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009136 if (!(++ecount % LPFC_GET_QE_REL_INT))
9137 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9138 }
9139 break;
9140 default:
9141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9142 "0370 Invalid completion queue type (%d)\n",
9143 cq->type);
9144 return;
9145 }
9146
9147 /* Catch the no cq entry condition, log an error */
9148 if (unlikely(ecount == 0))
9149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9150 "0371 No entry from the CQ: identifier "
9151 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9152
9153 /* In any case, flash and re-arm the RCQ */
9154 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9155
9156 /* wake up worker thread if there are works to be done */
9157 if (workposted)
9158 lpfc_worker_wake_up(phba);
9159}
9160
9161/**
9162 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9163 * @eqe: Pointer to fast-path completion queue entry.
9164 *
9165 * This routine process a fast-path work queue completion entry from fast-path
9166 * event queue for FCP command response completion.
9167 **/
9168static void
9169lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9170 struct lpfc_wcqe_complete *wcqe)
9171{
9172 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9173 struct lpfc_iocbq *cmdiocbq;
9174 struct lpfc_iocbq irspiocbq;
9175 unsigned long iflags;
9176
9177 spin_lock_irqsave(&phba->hbalock, iflags);
9178 pring->stats.iocb_event++;
9179 spin_unlock_irqrestore(&phba->hbalock, iflags);
9180
9181 /* Check for response status */
9182 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9183 /* If resource errors reported from HBA, reduce queue
9184 * depth of the SCSI device.
9185 */
9186 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9187 IOSTAT_LOCAL_REJECT) &&
9188 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9189 phba->lpfc_rampdown_queue_depth(phba);
9190 }
9191 /* Log the error status */
9192 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9193 "0373 FCP complete error: status=x%x, "
9194 "hw_status=x%x, total_data_specified=%d, "
9195 "parameter=x%x, word3=x%x\n",
9196 bf_get(lpfc_wcqe_c_status, wcqe),
9197 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9198 wcqe->total_data_placed, wcqe->parameter,
9199 wcqe->word3);
9200 }
9201
9202 /* Look up the FCP command IOCB and create pseudo response IOCB */
9203 spin_lock_irqsave(&phba->hbalock, iflags);
9204 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9205 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9206 spin_unlock_irqrestore(&phba->hbalock, iflags);
9207 if (unlikely(!cmdiocbq)) {
9208 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9209 "0374 FCP complete with no corresponding "
9210 "cmdiocb: iotag (%d)\n",
9211 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9212 return;
9213 }
9214 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9215 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9216 "0375 FCP cmdiocb not callback function "
9217 "iotag: (%d)\n",
9218 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9219 return;
9220 }
9221
9222 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009223 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009224
James Smart0f65ff62010-02-26 14:14:23 -05009225 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9226 spin_lock_irqsave(&phba->hbalock, iflags);
9227 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9228 spin_unlock_irqrestore(&phba->hbalock, iflags);
9229 }
9230
James Smart4f774512009-05-22 14:52:35 -04009231 /* Pass the cmd_iocb and the rsp state to the upper layer */
9232 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9233}
9234
9235/**
9236 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9237 * @phba: Pointer to HBA context object.
9238 * @cq: Pointer to completion queue.
9239 * @wcqe: Pointer to work-queue completion queue entry.
9240 *
9241 * This routine handles an fast-path WQ entry comsumed event by invoking the
9242 * proper WQ release routine to the slow-path WQ.
9243 **/
9244static void
9245lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9246 struct lpfc_wcqe_release *wcqe)
9247{
9248 struct lpfc_queue *childwq;
9249 bool wqid_matched = false;
9250 uint16_t fcp_wqid;
9251
9252 /* Check for fast-path FCP work queue release */
9253 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9254 list_for_each_entry(childwq, &cq->child_list, list) {
9255 if (childwq->queue_id == fcp_wqid) {
9256 lpfc_sli4_wq_release(childwq,
9257 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9258 wqid_matched = true;
9259 break;
9260 }
9261 }
9262 /* Report warning log message if no match found */
9263 if (wqid_matched != true)
9264 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9265 "2580 Fast-path wqe consume event carries "
9266 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9267}
9268
9269/**
9270 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9271 * @cq: Pointer to the completion queue.
9272 * @eqe: Pointer to fast-path completion queue entry.
9273 *
9274 * This routine process a fast-path work queue completion entry from fast-path
9275 * event queue for FCP command response completion.
9276 **/
9277static int
9278lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9279 struct lpfc_cqe *cqe)
9280{
9281 struct lpfc_wcqe_release wcqe;
9282 bool workposted = false;
James Smart98fc5dd2010-06-07 15:24:29 -04009283 unsigned long iflag;
James Smart4f774512009-05-22 14:52:35 -04009284
9285 /* Copy the work queue CQE and convert endian order if needed */
9286 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9287
9288 /* Check and process for different type of WCQE and dispatch */
9289 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9290 case CQE_CODE_COMPL_WQE:
9291 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009292 spin_lock_irqsave(&phba->hbalock, iflag);
9293 phba->last_completion_time = jiffies;
9294 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4f774512009-05-22 14:52:35 -04009295 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9296 (struct lpfc_wcqe_complete *)&wcqe);
9297 break;
9298 case CQE_CODE_RELEASE_WQE:
9299 /* Process the WQ release event */
9300 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9301 (struct lpfc_wcqe_release *)&wcqe);
9302 break;
9303 case CQE_CODE_XRI_ABORTED:
9304 /* Process the WQ XRI abort event */
9305 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9306 (struct sli4_wcqe_xri_aborted *)&wcqe);
9307 break;
9308 default:
9309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9310 "0144 Not a valid WCQE code: x%x\n",
9311 bf_get(lpfc_wcqe_c_code, &wcqe));
9312 break;
9313 }
9314 return workposted;
9315}
9316
9317/**
9318 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9319 * @phba: Pointer to HBA context object.
9320 * @eqe: Pointer to fast-path event queue entry.
9321 *
9322 * This routine process a event queue entry from the fast-path event queue.
9323 * It will check the MajorCode and MinorCode to determine this is for a
9324 * completion event on a completion queue, if not, an error shall be logged
9325 * and just return. Otherwise, it will get to the corresponding completion
9326 * queue and process all the entries on the completion queue, rearm the
9327 * completion queue, and then return.
9328 **/
9329static void
9330lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9331 uint32_t fcp_cqidx)
9332{
9333 struct lpfc_queue *cq;
9334 struct lpfc_cqe *cqe;
9335 bool workposted = false;
9336 uint16_t cqid;
9337 int ecount = 0;
9338
James Smartcb5172e2010-03-15 11:25:07 -04009339 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9341 "0366 Not a valid fast-path completion "
9342 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009343 bf_get_le32(lpfc_eqe_major_code, eqe),
9344 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009345 return;
9346 }
9347
9348 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9349 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009350 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9352 "0367 Fast-path completion queue "
9353 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009354 return;
9355 }
9356
9357 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009358 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009359 if (unlikely(cqid != cq->queue_id)) {
9360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9361 "0368 Miss-matched fast-path completion "
9362 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9363 cqid, cq->queue_id);
9364 return;
9365 }
9366
9367 /* Process all the entries to the CQ */
9368 while ((cqe = lpfc_sli4_cq_get(cq))) {
9369 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9370 if (!(++ecount % LPFC_GET_QE_REL_INT))
9371 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9372 }
9373
9374 /* Catch the no cq entry condition */
9375 if (unlikely(ecount == 0))
9376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9377 "0369 No entry from fast-path completion "
9378 "queue fcpcqid=%d\n", cq->queue_id);
9379
9380 /* In any case, flash and re-arm the CQ */
9381 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9382
9383 /* wake up worker thread if there are works to be done */
9384 if (workposted)
9385 lpfc_worker_wake_up(phba);
9386}
9387
9388static void
9389lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9390{
9391 struct lpfc_eqe *eqe;
9392
9393 /* walk all the EQ entries and drop on the floor */
9394 while ((eqe = lpfc_sli4_eq_get(eq)))
9395 ;
9396
9397 /* Clear and re-arm the EQ */
9398 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9399}
9400
9401/**
9402 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9403 * @irq: Interrupt number.
9404 * @dev_id: The device context pointer.
9405 *
9406 * This function is directly called from the PCI layer as an interrupt
9407 * service routine when device with SLI-4 interface spec is enabled with
9408 * MSI-X multi-message interrupt mode and there are slow-path events in
9409 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9410 * interrupt mode, this function is called as part of the device-level
9411 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9412 * undergoing initialization, the interrupt handler will not process the
9413 * interrupt. The link attention and ELS ring attention events are handled
9414 * by the worker thread. The interrupt handler signals the worker thread
9415 * and returns for these events. This function is called without any lock
9416 * held. It gets the hbalock to access and update SLI data structures.
9417 *
9418 * This function returns IRQ_HANDLED when interrupt is handled else it
9419 * returns IRQ_NONE.
9420 **/
9421irqreturn_t
9422lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9423{
9424 struct lpfc_hba *phba;
9425 struct lpfc_queue *speq;
9426 struct lpfc_eqe *eqe;
9427 unsigned long iflag;
9428 int ecount = 0;
9429
9430 /*
9431 * Get the driver's phba structure from the dev_id
9432 */
9433 phba = (struct lpfc_hba *)dev_id;
9434
9435 if (unlikely(!phba))
9436 return IRQ_NONE;
9437
9438 /* Get to the EQ struct associated with this vector */
9439 speq = phba->sli4_hba.sp_eq;
9440
9441 /* Check device state for handling interrupt */
9442 if (unlikely(lpfc_intr_state_check(phba))) {
9443 /* Check again for link_state with lock held */
9444 spin_lock_irqsave(&phba->hbalock, iflag);
9445 if (phba->link_state < LPFC_LINK_DOWN)
9446 /* Flush, clear interrupt, and rearm the EQ */
9447 lpfc_sli4_eq_flush(phba, speq);
9448 spin_unlock_irqrestore(&phba->hbalock, iflag);
9449 return IRQ_NONE;
9450 }
9451
9452 /*
9453 * Process all the event on FCP slow-path EQ
9454 */
9455 while ((eqe = lpfc_sli4_eq_get(speq))) {
9456 lpfc_sli4_sp_handle_eqe(phba, eqe);
9457 if (!(++ecount % LPFC_GET_QE_REL_INT))
9458 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9459 }
9460
9461 /* Always clear and re-arm the slow-path EQ */
9462 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9463
9464 /* Catch the no cq entry condition */
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 "0357 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_sp_intr_handler */
9477
9478/**
9479 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9480 * @irq: Interrupt number.
9481 * @dev_id: The device context pointer.
9482 *
9483 * This function is directly called from the PCI layer as an interrupt
9484 * service routine when device with SLI-4 interface spec is enabled with
9485 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9486 * ring event in the HBA. However, when the device is enabled with either
9487 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9488 * device-level interrupt handler. When the PCI slot is in error recovery
9489 * or the HBA is undergoing initialization, the interrupt handler will not
9490 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9491 * the intrrupt context. This function is called without any lock held.
9492 * It gets the hbalock to access and update SLI data structures. Note that,
9493 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9494 * equal to that of FCP CQ index.
9495 *
9496 * This function returns IRQ_HANDLED when interrupt is handled else it
9497 * returns IRQ_NONE.
9498 **/
9499irqreturn_t
9500lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9501{
9502 struct lpfc_hba *phba;
9503 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9504 struct lpfc_queue *fpeq;
9505 struct lpfc_eqe *eqe;
9506 unsigned long iflag;
9507 int ecount = 0;
9508 uint32_t fcp_eqidx;
9509
9510 /* Get the driver's phba structure from the dev_id */
9511 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9512 phba = fcp_eq_hdl->phba;
9513 fcp_eqidx = fcp_eq_hdl->idx;
9514
9515 if (unlikely(!phba))
9516 return IRQ_NONE;
9517
9518 /* Get to the EQ struct associated with this vector */
9519 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9520
9521 /* Check device state for handling interrupt */
9522 if (unlikely(lpfc_intr_state_check(phba))) {
9523 /* Check again for link_state with lock held */
9524 spin_lock_irqsave(&phba->hbalock, iflag);
9525 if (phba->link_state < LPFC_LINK_DOWN)
9526 /* Flush, clear interrupt, and rearm the EQ */
9527 lpfc_sli4_eq_flush(phba, fpeq);
9528 spin_unlock_irqrestore(&phba->hbalock, iflag);
9529 return IRQ_NONE;
9530 }
9531
9532 /*
9533 * Process all the event on FCP fast-path EQ
9534 */
9535 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9536 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9537 if (!(++ecount % LPFC_GET_QE_REL_INT))
9538 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9539 }
9540
9541 /* Always clear and re-arm the fast-path EQ */
9542 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9543
9544 if (unlikely(ecount == 0)) {
9545 if (phba->intr_type == MSIX)
9546 /* MSI-X treated interrupt served as no EQ share INT */
9547 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9548 "0358 MSI-X interrupt with no EQE\n");
9549 else
9550 /* Non MSI-X treated on interrupt as EQ share INT */
9551 return IRQ_NONE;
9552 }
9553
9554 return IRQ_HANDLED;
9555} /* lpfc_sli4_fp_intr_handler */
9556
9557/**
9558 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9559 * @irq: Interrupt number.
9560 * @dev_id: The device context pointer.
9561 *
9562 * This function is the device-level interrupt handler to device with SLI-4
9563 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9564 * interrupt mode is enabled and there is an event in the HBA which requires
9565 * driver attention. This function invokes the slow-path interrupt attention
9566 * handling function and fast-path interrupt attention handling function in
9567 * turn to process the relevant HBA attention events. This function is called
9568 * without any lock held. It gets the hbalock to access and update SLI data
9569 * structures.
9570 *
9571 * This function returns IRQ_HANDLED when interrupt is handled, else it
9572 * returns IRQ_NONE.
9573 **/
9574irqreturn_t
9575lpfc_sli4_intr_handler(int irq, void *dev_id)
9576{
9577 struct lpfc_hba *phba;
9578 irqreturn_t sp_irq_rc, fp_irq_rc;
9579 bool fp_handled = false;
9580 uint32_t fcp_eqidx;
9581
9582 /* Get the driver's phba structure from the dev_id */
9583 phba = (struct lpfc_hba *)dev_id;
9584
9585 if (unlikely(!phba))
9586 return IRQ_NONE;
9587
9588 /*
9589 * Invokes slow-path host attention interrupt handling as appropriate.
9590 */
9591 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9592
9593 /*
9594 * Invoke fast-path host attention interrupt handling as appropriate.
9595 */
9596 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9597 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9598 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9599 if (fp_irq_rc == IRQ_HANDLED)
9600 fp_handled |= true;
9601 }
9602
9603 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9604} /* lpfc_sli4_intr_handler */
9605
9606/**
9607 * lpfc_sli4_queue_free - free a queue structure and associated memory
9608 * @queue: The queue structure to free.
9609 *
9610 * This function frees a queue structure and the DMAable memeory used for
9611 * the host resident queue. This function must be called after destroying the
9612 * queue on the HBA.
9613 **/
9614void
9615lpfc_sli4_queue_free(struct lpfc_queue *queue)
9616{
9617 struct lpfc_dmabuf *dmabuf;
9618
9619 if (!queue)
9620 return;
9621
9622 while (!list_empty(&queue->page_list)) {
9623 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9624 list);
James Smart49198b32010-04-06 15:04:33 -04009625 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009626 dmabuf->virt, dmabuf->phys);
9627 kfree(dmabuf);
9628 }
9629 kfree(queue);
9630 return;
9631}
9632
9633/**
9634 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9635 * @phba: The HBA that this queue is being created on.
9636 * @entry_size: The size of each queue entry for this queue.
9637 * @entry count: The number of entries that this queue will handle.
9638 *
9639 * This function allocates a queue structure and the DMAable memory used for
9640 * the host resident queue. This function must be called before creating the
9641 * queue on the HBA.
9642 **/
9643struct lpfc_queue *
9644lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9645 uint32_t entry_count)
9646{
9647 struct lpfc_queue *queue;
9648 struct lpfc_dmabuf *dmabuf;
9649 int x, total_qe_count;
9650 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009651 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009652
James Smartcb5172e2010-03-15 11:25:07 -04009653 if (!phba->sli4_hba.pc_sli4_params.supported)
9654 hw_page_size = SLI4_PAGE_SIZE;
9655
James Smart4f774512009-05-22 14:52:35 -04009656 queue = kzalloc(sizeof(struct lpfc_queue) +
9657 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9658 if (!queue)
9659 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009660 queue->page_count = (ALIGN(entry_size * entry_count,
9661 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009662 INIT_LIST_HEAD(&queue->list);
9663 INIT_LIST_HEAD(&queue->page_list);
9664 INIT_LIST_HEAD(&queue->child_list);
9665 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9666 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9667 if (!dmabuf)
9668 goto out_fail;
9669 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009670 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009671 GFP_KERNEL);
9672 if (!dmabuf->virt) {
9673 kfree(dmabuf);
9674 goto out_fail;
9675 }
James Smartcb5172e2010-03-15 11:25:07 -04009676 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009677 dmabuf->buffer_tag = x;
9678 list_add_tail(&dmabuf->list, &queue->page_list);
9679 /* initialize queue's entry array */
9680 dma_pointer = dmabuf->virt;
9681 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009682 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009683 total_qe_count++, dma_pointer += entry_size) {
9684 queue->qe[total_qe_count].address = dma_pointer;
9685 }
9686 }
9687 queue->entry_size = entry_size;
9688 queue->entry_count = entry_count;
9689 queue->phba = phba;
9690
9691 return queue;
9692out_fail:
9693 lpfc_sli4_queue_free(queue);
9694 return NULL;
9695}
9696
9697/**
9698 * lpfc_eq_create - Create an Event Queue on the HBA
9699 * @phba: HBA structure that indicates port to create a queue on.
9700 * @eq: The queue structure to use to create the event queue.
9701 * @imax: The maximum interrupt per second limit.
9702 *
9703 * This function creates an event queue, as detailed in @eq, on a port,
9704 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9705 *
9706 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9707 * is used to get the entry count and entry size that are necessary to
9708 * determine the number of pages to allocate and use for this queue. This
9709 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9710 * event queue. This function is asynchronous and will wait for the mailbox
9711 * command to finish before continuing.
9712 *
9713 * On success this function will return a zero. If unable to allocate enough
9714 * memory this function will return ENOMEM. If the queue create mailbox command
9715 * fails this function will return ENXIO.
9716 **/
9717uint32_t
9718lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9719{
9720 struct lpfc_mbx_eq_create *eq_create;
9721 LPFC_MBOXQ_t *mbox;
9722 int rc, length, status = 0;
9723 struct lpfc_dmabuf *dmabuf;
9724 uint32_t shdr_status, shdr_add_status;
9725 union lpfc_sli4_cfg_shdr *shdr;
9726 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009727 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9728
9729 if (!phba->sli4_hba.pc_sli4_params.supported)
9730 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009731
9732 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9733 if (!mbox)
9734 return -ENOMEM;
9735 length = (sizeof(struct lpfc_mbx_eq_create) -
9736 sizeof(struct lpfc_sli4_cfg_mhdr));
9737 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9738 LPFC_MBOX_OPCODE_EQ_CREATE,
9739 length, LPFC_SLI4_MBX_EMBED);
9740 eq_create = &mbox->u.mqe.un.eq_create;
9741 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9742 eq->page_count);
9743 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9744 LPFC_EQE_SIZE);
9745 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9746 /* Calculate delay multiper from maximum interrupt per second */
9747 dmult = LPFC_DMULT_CONST/imax - 1;
9748 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9749 dmult);
9750 switch (eq->entry_count) {
9751 default:
9752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9753 "0360 Unsupported EQ count. (%d)\n",
9754 eq->entry_count);
9755 if (eq->entry_count < 256)
9756 return -EINVAL;
9757 /* otherwise default to smallest count (drop through) */
9758 case 256:
9759 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9760 LPFC_EQ_CNT_256);
9761 break;
9762 case 512:
9763 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9764 LPFC_EQ_CNT_512);
9765 break;
9766 case 1024:
9767 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9768 LPFC_EQ_CNT_1024);
9769 break;
9770 case 2048:
9771 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9772 LPFC_EQ_CNT_2048);
9773 break;
9774 case 4096:
9775 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9776 LPFC_EQ_CNT_4096);
9777 break;
9778 }
9779 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009780 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009781 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9782 putPaddrLow(dmabuf->phys);
9783 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9784 putPaddrHigh(dmabuf->phys);
9785 }
9786 mbox->vport = phba->pport;
9787 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9788 mbox->context1 = NULL;
9789 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9790 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9791 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9792 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9793 if (shdr_status || shdr_add_status || rc) {
9794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9795 "2500 EQ_CREATE mailbox failed with "
9796 "status x%x add_status x%x, mbx status x%x\n",
9797 shdr_status, shdr_add_status, rc);
9798 status = -ENXIO;
9799 }
9800 eq->type = LPFC_EQ;
9801 eq->subtype = LPFC_NONE;
9802 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9803 if (eq->queue_id == 0xFFFF)
9804 status = -ENXIO;
9805 eq->host_index = 0;
9806 eq->hba_index = 0;
9807
James Smart8fa38512009-07-19 10:01:03 -04009808 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009809 return status;
9810}
9811
9812/**
9813 * lpfc_cq_create - Create a Completion Queue on the HBA
9814 * @phba: HBA structure that indicates port to create a queue on.
9815 * @cq: The queue structure to use to create the completion queue.
9816 * @eq: The event queue to bind this completion queue to.
9817 *
9818 * This function creates a completion queue, as detailed in @wq, on a port,
9819 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9820 *
9821 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9822 * is used to get the entry count and entry size that are necessary to
9823 * determine the number of pages to allocate and use for this queue. The @eq
9824 * is used to indicate which event queue to bind this completion queue to. This
9825 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9826 * completion queue. This function is asynchronous and will wait for the mailbox
9827 * command to finish before continuing.
9828 *
9829 * On success this function will return a zero. If unable to allocate enough
9830 * memory this function will return ENOMEM. If the queue create mailbox command
9831 * fails this function will return ENXIO.
9832 **/
9833uint32_t
9834lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9835 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9836{
9837 struct lpfc_mbx_cq_create *cq_create;
9838 struct lpfc_dmabuf *dmabuf;
9839 LPFC_MBOXQ_t *mbox;
9840 int rc, length, status = 0;
9841 uint32_t shdr_status, shdr_add_status;
9842 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009843 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9844
9845 if (!phba->sli4_hba.pc_sli4_params.supported)
9846 hw_page_size = SLI4_PAGE_SIZE;
9847
James Smart4f774512009-05-22 14:52:35 -04009848
9849 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9850 if (!mbox)
9851 return -ENOMEM;
9852 length = (sizeof(struct lpfc_mbx_cq_create) -
9853 sizeof(struct lpfc_sli4_cfg_mhdr));
9854 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9855 LPFC_MBOX_OPCODE_CQ_CREATE,
9856 length, LPFC_SLI4_MBX_EMBED);
9857 cq_create = &mbox->u.mqe.un.cq_create;
9858 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9859 cq->page_count);
9860 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9861 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9862 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9863 switch (cq->entry_count) {
9864 default:
9865 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9866 "0361 Unsupported CQ count. (%d)\n",
9867 cq->entry_count);
9868 if (cq->entry_count < 256)
9869 return -EINVAL;
9870 /* otherwise default to smallest count (drop through) */
9871 case 256:
9872 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9873 LPFC_CQ_CNT_256);
9874 break;
9875 case 512:
9876 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9877 LPFC_CQ_CNT_512);
9878 break;
9879 case 1024:
9880 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9881 LPFC_CQ_CNT_1024);
9882 break;
9883 }
9884 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009885 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009886 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9887 putPaddrLow(dmabuf->phys);
9888 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9889 putPaddrHigh(dmabuf->phys);
9890 }
9891 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9892
9893 /* The IOCTL status is embedded in the mailbox subheader. */
9894 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9895 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9896 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9897 if (shdr_status || shdr_add_status || rc) {
9898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9899 "2501 CQ_CREATE mailbox failed with "
9900 "status x%x add_status x%x, mbx status x%x\n",
9901 shdr_status, shdr_add_status, rc);
9902 status = -ENXIO;
9903 goto out;
9904 }
9905 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9906 if (cq->queue_id == 0xFFFF) {
9907 status = -ENXIO;
9908 goto out;
9909 }
9910 /* link the cq onto the parent eq child list */
9911 list_add_tail(&cq->list, &eq->child_list);
9912 /* Set up completion queue's type and subtype */
9913 cq->type = type;
9914 cq->subtype = subtype;
9915 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9916 cq->host_index = 0;
9917 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009918
James Smart8fa38512009-07-19 10:01:03 -04009919out:
9920 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009921 return status;
9922}
9923
9924/**
James Smartb19a0612010-04-06 14:48:51 -04009925 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -04009926 * @phba: HBA structure that indicates port to create a queue on.
9927 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -04009928 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9929 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -04009930 *
James Smartb19a0612010-04-06 14:48:51 -04009931 * This function provides failback (fb) functionality when the
9932 * mq_create_ext fails on older FW generations. It's purpose is identical
9933 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -04009934 *
James Smartb19a0612010-04-06 14:48:51 -04009935 * This routine cannot fail as all attributes were previously accessed and
9936 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -04009937 **/
James Smartb19a0612010-04-06 14:48:51 -04009938static void
9939lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9940 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -04009941{
9942 struct lpfc_mbx_mq_create *mq_create;
9943 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -04009944 int length;
James Smart04c68492009-05-22 14:52:52 -04009945
James Smart04c68492009-05-22 14:52:52 -04009946 length = (sizeof(struct lpfc_mbx_mq_create) -
9947 sizeof(struct lpfc_sli4_cfg_mhdr));
9948 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9949 LPFC_MBOX_OPCODE_MQ_CREATE,
9950 length, LPFC_SLI4_MBX_EMBED);
9951 mq_create = &mbox->u.mqe.un.mq_create;
9952 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -04009953 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -04009954 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -04009955 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -04009956 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9957 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -04009958 case 16:
9959 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9960 LPFC_MQ_CNT_16);
9961 break;
9962 case 32:
9963 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9964 LPFC_MQ_CNT_32);
9965 break;
9966 case 64:
9967 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9968 LPFC_MQ_CNT_64);
9969 break;
9970 case 128:
9971 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9972 LPFC_MQ_CNT_128);
9973 break;
9974 }
9975 list_for_each_entry(dmabuf, &mq->page_list, list) {
9976 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -04009977 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -04009978 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -04009979 putPaddrHigh(dmabuf->phys);
9980 }
9981}
9982
9983/**
9984 * lpfc_mq_create - Create a mailbox Queue on the HBA
9985 * @phba: HBA structure that indicates port to create a queue on.
9986 * @mq: The queue structure to use to create the mailbox queue.
9987 * @cq: The completion queue to associate with this cq.
9988 * @subtype: The queue's subtype.
9989 *
9990 * This function creates a mailbox queue, as detailed in @mq, on a port,
9991 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9992 *
9993 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9994 * is used to get the entry count and entry size that are necessary to
9995 * determine the number of pages to allocate and use for this queue. This
9996 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9997 * mailbox queue. This function is asynchronous and will wait for the mailbox
9998 * command to finish before continuing.
9999 *
10000 * On success this function will return a zero. If unable to allocate enough
10001 * memory this function will return ENOMEM. If the queue create mailbox command
10002 * fails this function will return ENXIO.
10003 **/
10004int32_t
10005lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10006 struct lpfc_queue *cq, uint32_t subtype)
10007{
10008 struct lpfc_mbx_mq_create *mq_create;
10009 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10010 struct lpfc_dmabuf *dmabuf;
10011 LPFC_MBOXQ_t *mbox;
10012 int rc, length, status = 0;
10013 uint32_t shdr_status, shdr_add_status;
10014 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010015 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010016
James Smart49198b32010-04-06 15:04:33 -040010017 if (!phba->sli4_hba.pc_sli4_params.supported)
10018 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010019
10020 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10021 if (!mbox)
10022 return -ENOMEM;
10023 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10024 sizeof(struct lpfc_sli4_cfg_mhdr));
10025 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10026 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10027 length, LPFC_SLI4_MBX_EMBED);
10028
10029 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10030 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10031 mq->page_count);
10032 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10033 1);
10034 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10035 &mq_create_ext->u.request, 1);
10036 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10037 &mq_create_ext->u.request, 1);
10038 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10039 cq->queue_id);
10040 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10041 switch (mq->entry_count) {
10042 default:
10043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10044 "0362 Unsupported MQ count. (%d)\n",
10045 mq->entry_count);
10046 if (mq->entry_count < 16)
10047 return -EINVAL;
10048 /* otherwise default to smallest count (drop through) */
10049 case 16:
10050 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10051 LPFC_MQ_CNT_16);
10052 break;
10053 case 32:
10054 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10055 LPFC_MQ_CNT_32);
10056 break;
10057 case 64:
10058 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10059 LPFC_MQ_CNT_64);
10060 break;
10061 case 128:
10062 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10063 LPFC_MQ_CNT_128);
10064 break;
10065 }
10066 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010067 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010068 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10069 putPaddrLow(dmabuf->phys);
10070 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010071 putPaddrHigh(dmabuf->phys);
10072 }
10073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010074 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10075 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10076 &mq_create_ext->u.response);
10077 if (rc != MBX_SUCCESS) {
10078 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10079 "2795 MQ_CREATE_EXT failed with "
10080 "status x%x. Failback to MQ_CREATE.\n",
10081 rc);
10082 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10083 mq_create = &mbox->u.mqe.un.mq_create;
10084 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10085 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10086 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10087 &mq_create->u.response);
10088 }
10089
James Smart04c68492009-05-22 14:52:52 -040010090 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010091 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10092 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10093 if (shdr_status || shdr_add_status || rc) {
10094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10095 "2502 MQ_CREATE mailbox failed with "
10096 "status x%x add_status x%x, mbx status x%x\n",
10097 shdr_status, shdr_add_status, rc);
10098 status = -ENXIO;
10099 goto out;
10100 }
James Smart04c68492009-05-22 14:52:52 -040010101 if (mq->queue_id == 0xFFFF) {
10102 status = -ENXIO;
10103 goto out;
10104 }
10105 mq->type = LPFC_MQ;
10106 mq->subtype = subtype;
10107 mq->host_index = 0;
10108 mq->hba_index = 0;
10109
10110 /* link the mq onto the parent cq child list */
10111 list_add_tail(&mq->list, &cq->child_list);
10112out:
James Smart8fa38512009-07-19 10:01:03 -040010113 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010114 return status;
10115}
10116
10117/**
James Smart4f774512009-05-22 14:52:35 -040010118 * lpfc_wq_create - Create a Work Queue on the HBA
10119 * @phba: HBA structure that indicates port to create a queue on.
10120 * @wq: The queue structure to use to create the work queue.
10121 * @cq: The completion queue to bind this work queue to.
10122 * @subtype: The subtype of the work queue indicating its functionality.
10123 *
10124 * This function creates a work queue, as detailed in @wq, on a port, described
10125 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10126 *
10127 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10128 * is used to get the entry count and entry size that are necessary to
10129 * determine the number of pages to allocate and use for this queue. The @cq
10130 * is used to indicate which completion queue to bind this work queue to. This
10131 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10132 * work queue. This function is asynchronous and will wait for the mailbox
10133 * command to finish before continuing.
10134 *
10135 * On success this function will return a zero. If unable to allocate enough
10136 * memory this function will return ENOMEM. If the queue create mailbox command
10137 * fails this function will return ENXIO.
10138 **/
10139uint32_t
10140lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10141 struct lpfc_queue *cq, uint32_t subtype)
10142{
10143 struct lpfc_mbx_wq_create *wq_create;
10144 struct lpfc_dmabuf *dmabuf;
10145 LPFC_MBOXQ_t *mbox;
10146 int rc, length, status = 0;
10147 uint32_t shdr_status, shdr_add_status;
10148 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010149 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10150
10151 if (!phba->sli4_hba.pc_sli4_params.supported)
10152 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010153
10154 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10155 if (!mbox)
10156 return -ENOMEM;
10157 length = (sizeof(struct lpfc_mbx_wq_create) -
10158 sizeof(struct lpfc_sli4_cfg_mhdr));
10159 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10160 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10161 length, LPFC_SLI4_MBX_EMBED);
10162 wq_create = &mbox->u.mqe.un.wq_create;
10163 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10164 wq->page_count);
10165 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10166 cq->queue_id);
10167 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010168 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010169 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10170 putPaddrLow(dmabuf->phys);
10171 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10172 putPaddrHigh(dmabuf->phys);
10173 }
10174 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10175 /* The IOCTL status is embedded in the mailbox subheader. */
10176 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10177 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10178 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10179 if (shdr_status || shdr_add_status || rc) {
10180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10181 "2503 WQ_CREATE mailbox failed with "
10182 "status x%x add_status x%x, mbx status x%x\n",
10183 shdr_status, shdr_add_status, rc);
10184 status = -ENXIO;
10185 goto out;
10186 }
10187 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10188 if (wq->queue_id == 0xFFFF) {
10189 status = -ENXIO;
10190 goto out;
10191 }
10192 wq->type = LPFC_WQ;
10193 wq->subtype = subtype;
10194 wq->host_index = 0;
10195 wq->hba_index = 0;
10196
10197 /* link the wq onto the parent cq child list */
10198 list_add_tail(&wq->list, &cq->child_list);
10199out:
James Smart8fa38512009-07-19 10:01:03 -040010200 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010201 return status;
10202}
10203
10204/**
10205 * lpfc_rq_create - Create a Receive Queue on the HBA
10206 * @phba: HBA structure that indicates port to create a queue on.
10207 * @hrq: The queue structure to use to create the header receive queue.
10208 * @drq: The queue structure to use to create the data receive queue.
10209 * @cq: The completion queue to bind this work queue to.
10210 *
10211 * This function creates a receive buffer queue pair , as detailed in @hrq and
10212 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10213 * to the HBA.
10214 *
10215 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10216 * struct is used to get the entry count that is necessary to determine the
10217 * number of pages to use for this queue. The @cq is used to indicate which
10218 * completion queue to bind received buffers that are posted to these queues to.
10219 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10220 * receive queue pair. This function is asynchronous and will wait for the
10221 * mailbox command to finish before continuing.
10222 *
10223 * On success this function will return a zero. If unable to allocate enough
10224 * memory this function will return ENOMEM. If the queue create mailbox command
10225 * fails this function will return ENXIO.
10226 **/
10227uint32_t
10228lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10229 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10230{
10231 struct lpfc_mbx_rq_create *rq_create;
10232 struct lpfc_dmabuf *dmabuf;
10233 LPFC_MBOXQ_t *mbox;
10234 int rc, length, status = 0;
10235 uint32_t shdr_status, shdr_add_status;
10236 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010237 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10238
10239 if (!phba->sli4_hba.pc_sli4_params.supported)
10240 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010241
10242 if (hrq->entry_count != drq->entry_count)
10243 return -EINVAL;
10244 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10245 if (!mbox)
10246 return -ENOMEM;
10247 length = (sizeof(struct lpfc_mbx_rq_create) -
10248 sizeof(struct lpfc_sli4_cfg_mhdr));
10249 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10250 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10251 length, LPFC_SLI4_MBX_EMBED);
10252 rq_create = &mbox->u.mqe.un.rq_create;
10253 switch (hrq->entry_count) {
10254 default:
10255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10256 "2535 Unsupported RQ count. (%d)\n",
10257 hrq->entry_count);
10258 if (hrq->entry_count < 512)
10259 return -EINVAL;
10260 /* otherwise default to smallest count (drop through) */
10261 case 512:
10262 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10263 LPFC_RQ_RING_SIZE_512);
10264 break;
10265 case 1024:
10266 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10267 LPFC_RQ_RING_SIZE_1024);
10268 break;
10269 case 2048:
10270 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10271 LPFC_RQ_RING_SIZE_2048);
10272 break;
10273 case 4096:
10274 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10275 LPFC_RQ_RING_SIZE_4096);
10276 break;
10277 }
10278 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10279 cq->queue_id);
10280 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10281 hrq->page_count);
10282 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10283 LPFC_HDR_BUF_SIZE);
10284 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010285 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010286 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10287 putPaddrLow(dmabuf->phys);
10288 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10289 putPaddrHigh(dmabuf->phys);
10290 }
10291 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10292 /* The IOCTL status is embedded in the mailbox subheader. */
10293 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10296 if (shdr_status || shdr_add_status || rc) {
10297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10298 "2504 RQ_CREATE mailbox failed with "
10299 "status x%x add_status x%x, mbx status x%x\n",
10300 shdr_status, shdr_add_status, rc);
10301 status = -ENXIO;
10302 goto out;
10303 }
10304 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10305 if (hrq->queue_id == 0xFFFF) {
10306 status = -ENXIO;
10307 goto out;
10308 }
10309 hrq->type = LPFC_HRQ;
10310 hrq->subtype = subtype;
10311 hrq->host_index = 0;
10312 hrq->hba_index = 0;
10313
10314 /* now create the data queue */
10315 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10316 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10317 length, LPFC_SLI4_MBX_EMBED);
10318 switch (drq->entry_count) {
10319 default:
10320 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10321 "2536 Unsupported RQ count. (%d)\n",
10322 drq->entry_count);
10323 if (drq->entry_count < 512)
10324 return -EINVAL;
10325 /* otherwise default to smallest count (drop through) */
10326 case 512:
10327 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10328 LPFC_RQ_RING_SIZE_512);
10329 break;
10330 case 1024:
10331 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10332 LPFC_RQ_RING_SIZE_1024);
10333 break;
10334 case 2048:
10335 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10336 LPFC_RQ_RING_SIZE_2048);
10337 break;
10338 case 4096:
10339 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10340 LPFC_RQ_RING_SIZE_4096);
10341 break;
10342 }
10343 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10344 cq->queue_id);
10345 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10346 drq->page_count);
10347 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10348 LPFC_DATA_BUF_SIZE);
10349 list_for_each_entry(dmabuf, &drq->page_list, list) {
10350 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10351 putPaddrLow(dmabuf->phys);
10352 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10353 putPaddrHigh(dmabuf->phys);
10354 }
10355 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10356 /* The IOCTL status is embedded in the mailbox subheader. */
10357 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10358 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10359 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10360 if (shdr_status || shdr_add_status || rc) {
10361 status = -ENXIO;
10362 goto out;
10363 }
10364 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10365 if (drq->queue_id == 0xFFFF) {
10366 status = -ENXIO;
10367 goto out;
10368 }
10369 drq->type = LPFC_DRQ;
10370 drq->subtype = subtype;
10371 drq->host_index = 0;
10372 drq->hba_index = 0;
10373
10374 /* link the header and data RQs onto the parent cq child list */
10375 list_add_tail(&hrq->list, &cq->child_list);
10376 list_add_tail(&drq->list, &cq->child_list);
10377
10378out:
James Smart8fa38512009-07-19 10:01:03 -040010379 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010380 return status;
10381}
10382
10383/**
10384 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10385 * @eq: The queue structure associated with the queue to destroy.
10386 *
10387 * This function destroys a queue, as detailed in @eq by sending an mailbox
10388 * command, specific to the type of queue, to the HBA.
10389 *
10390 * The @eq struct is used to get the queue ID of the queue to destroy.
10391 *
10392 * On success this function will return a zero. If the queue destroy mailbox
10393 * command fails this function will return ENXIO.
10394 **/
10395uint32_t
10396lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10397{
10398 LPFC_MBOXQ_t *mbox;
10399 int rc, length, status = 0;
10400 uint32_t shdr_status, shdr_add_status;
10401 union lpfc_sli4_cfg_shdr *shdr;
10402
10403 if (!eq)
10404 return -ENODEV;
10405 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10406 if (!mbox)
10407 return -ENOMEM;
10408 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10409 sizeof(struct lpfc_sli4_cfg_mhdr));
10410 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10411 LPFC_MBOX_OPCODE_EQ_DESTROY,
10412 length, LPFC_SLI4_MBX_EMBED);
10413 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10414 eq->queue_id);
10415 mbox->vport = eq->phba->pport;
10416 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10417
10418 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10419 /* The IOCTL status is embedded in the mailbox subheader. */
10420 shdr = (union lpfc_sli4_cfg_shdr *)
10421 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10422 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10423 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10424 if (shdr_status || shdr_add_status || rc) {
10425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10426 "2505 EQ_DESTROY mailbox failed with "
10427 "status x%x add_status x%x, mbx status x%x\n",
10428 shdr_status, shdr_add_status, rc);
10429 status = -ENXIO;
10430 }
10431
10432 /* Remove eq from any list */
10433 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010434 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010435 return status;
10436}
10437
10438/**
10439 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10440 * @cq: The queue structure associated with the queue to destroy.
10441 *
10442 * This function destroys a queue, as detailed in @cq by sending an mailbox
10443 * command, specific to the type of queue, to the HBA.
10444 *
10445 * The @cq struct is used to get the queue ID of the queue to destroy.
10446 *
10447 * On success this function will return a zero. If the queue destroy mailbox
10448 * command fails this function will return ENXIO.
10449 **/
10450uint32_t
10451lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10452{
10453 LPFC_MBOXQ_t *mbox;
10454 int rc, length, status = 0;
10455 uint32_t shdr_status, shdr_add_status;
10456 union lpfc_sli4_cfg_shdr *shdr;
10457
10458 if (!cq)
10459 return -ENODEV;
10460 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10461 if (!mbox)
10462 return -ENOMEM;
10463 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10464 sizeof(struct lpfc_sli4_cfg_mhdr));
10465 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10466 LPFC_MBOX_OPCODE_CQ_DESTROY,
10467 length, LPFC_SLI4_MBX_EMBED);
10468 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10469 cq->queue_id);
10470 mbox->vport = cq->phba->pport;
10471 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10472 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10473 /* The IOCTL status is embedded in the mailbox subheader. */
10474 shdr = (union lpfc_sli4_cfg_shdr *)
10475 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10476 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10477 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10478 if (shdr_status || shdr_add_status || rc) {
10479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10480 "2506 CQ_DESTROY mailbox failed with "
10481 "status x%x add_status x%x, mbx status x%x\n",
10482 shdr_status, shdr_add_status, rc);
10483 status = -ENXIO;
10484 }
10485 /* Remove cq from any list */
10486 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010487 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010488 return status;
10489}
10490
10491/**
James Smart04c68492009-05-22 14:52:52 -040010492 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10493 * @qm: The queue structure associated with the queue to destroy.
10494 *
10495 * This function destroys a queue, as detailed in @mq by sending an mailbox
10496 * command, specific to the type of queue, to the HBA.
10497 *
10498 * The @mq struct is used to get the queue ID of the queue to destroy.
10499 *
10500 * On success this function will return a zero. If the queue destroy mailbox
10501 * command fails this function will return ENXIO.
10502 **/
10503uint32_t
10504lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10505{
10506 LPFC_MBOXQ_t *mbox;
10507 int rc, length, status = 0;
10508 uint32_t shdr_status, shdr_add_status;
10509 union lpfc_sli4_cfg_shdr *shdr;
10510
10511 if (!mq)
10512 return -ENODEV;
10513 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10514 if (!mbox)
10515 return -ENOMEM;
10516 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10517 sizeof(struct lpfc_sli4_cfg_mhdr));
10518 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10519 LPFC_MBOX_OPCODE_MQ_DESTROY,
10520 length, LPFC_SLI4_MBX_EMBED);
10521 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10522 mq->queue_id);
10523 mbox->vport = mq->phba->pport;
10524 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10525 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10526 /* The IOCTL status is embedded in the mailbox subheader. */
10527 shdr = (union lpfc_sli4_cfg_shdr *)
10528 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10529 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10530 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10531 if (shdr_status || shdr_add_status || rc) {
10532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10533 "2507 MQ_DESTROY mailbox failed with "
10534 "status x%x add_status x%x, mbx status x%x\n",
10535 shdr_status, shdr_add_status, rc);
10536 status = -ENXIO;
10537 }
10538 /* Remove mq from any list */
10539 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010540 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010541 return status;
10542}
10543
10544/**
James Smart4f774512009-05-22 14:52:35 -040010545 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10546 * @wq: The queue structure associated with the queue to destroy.
10547 *
10548 * This function destroys a queue, as detailed in @wq by sending an mailbox
10549 * command, specific to the type of queue, to the HBA.
10550 *
10551 * The @wq struct is used to get the queue ID of the queue to destroy.
10552 *
10553 * On success this function will return a zero. If the queue destroy mailbox
10554 * command fails this function will return ENXIO.
10555 **/
10556uint32_t
10557lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10558{
10559 LPFC_MBOXQ_t *mbox;
10560 int rc, length, status = 0;
10561 uint32_t shdr_status, shdr_add_status;
10562 union lpfc_sli4_cfg_shdr *shdr;
10563
10564 if (!wq)
10565 return -ENODEV;
10566 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10567 if (!mbox)
10568 return -ENOMEM;
10569 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10570 sizeof(struct lpfc_sli4_cfg_mhdr));
10571 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10572 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10573 length, LPFC_SLI4_MBX_EMBED);
10574 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10575 wq->queue_id);
10576 mbox->vport = wq->phba->pport;
10577 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10578 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10579 shdr = (union lpfc_sli4_cfg_shdr *)
10580 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10581 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10582 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10583 if (shdr_status || shdr_add_status || rc) {
10584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10585 "2508 WQ_DESTROY mailbox failed with "
10586 "status x%x add_status x%x, mbx status x%x\n",
10587 shdr_status, shdr_add_status, rc);
10588 status = -ENXIO;
10589 }
10590 /* Remove wq from any list */
10591 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010592 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010593 return status;
10594}
10595
10596/**
10597 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10598 * @rq: The queue structure associated with the queue to destroy.
10599 *
10600 * This function destroys a queue, as detailed in @rq by sending an mailbox
10601 * command, specific to the type of queue, to the HBA.
10602 *
10603 * The @rq struct is used to get the queue ID of the queue to destroy.
10604 *
10605 * On success this function will return a zero. If the queue destroy mailbox
10606 * command fails this function will return ENXIO.
10607 **/
10608uint32_t
10609lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10610 struct lpfc_queue *drq)
10611{
10612 LPFC_MBOXQ_t *mbox;
10613 int rc, length, status = 0;
10614 uint32_t shdr_status, shdr_add_status;
10615 union lpfc_sli4_cfg_shdr *shdr;
10616
10617 if (!hrq || !drq)
10618 return -ENODEV;
10619 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10620 if (!mbox)
10621 return -ENOMEM;
10622 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10623 sizeof(struct mbox_header));
10624 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10625 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10626 length, LPFC_SLI4_MBX_EMBED);
10627 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10628 hrq->queue_id);
10629 mbox->vport = hrq->phba->pport;
10630 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10631 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10632 /* The IOCTL status is embedded in the mailbox subheader. */
10633 shdr = (union lpfc_sli4_cfg_shdr *)
10634 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10635 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10636 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10637 if (shdr_status || shdr_add_status || rc) {
10638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10639 "2509 RQ_DESTROY mailbox failed with "
10640 "status x%x add_status x%x, mbx status x%x\n",
10641 shdr_status, shdr_add_status, rc);
10642 if (rc != MBX_TIMEOUT)
10643 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10644 return -ENXIO;
10645 }
10646 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10647 drq->queue_id);
10648 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10649 shdr = (union lpfc_sli4_cfg_shdr *)
10650 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10651 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10652 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10653 if (shdr_status || shdr_add_status || rc) {
10654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10655 "2510 RQ_DESTROY mailbox failed with "
10656 "status x%x add_status x%x, mbx status x%x\n",
10657 shdr_status, shdr_add_status, rc);
10658 status = -ENXIO;
10659 }
10660 list_del_init(&hrq->list);
10661 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010662 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010663 return status;
10664}
10665
10666/**
10667 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10668 * @phba: The virtual port for which this call being executed.
10669 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10670 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10671 * @xritag: the xritag that ties this io to the SGL pages.
10672 *
10673 * This routine will post the sgl pages for the IO that has the xritag
10674 * that is in the iocbq structure. The xritag is assigned during iocbq
10675 * creation and persists for as long as the driver is loaded.
10676 * if the caller has fewer than 256 scatter gather segments to map then
10677 * pdma_phys_addr1 should be 0.
10678 * If the caller needs to map more than 256 scatter gather segment then
10679 * pdma_phys_addr1 should be a valid physical address.
10680 * physical address for SGLs must be 64 byte aligned.
10681 * If you are going to map 2 SGL's then the first one must have 256 entries
10682 * the second sgl can have between 1 and 256 entries.
10683 *
10684 * Return codes:
10685 * 0 - Success
10686 * -ENXIO, -ENOMEM - Failure
10687 **/
10688int
10689lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10690 dma_addr_t pdma_phys_addr0,
10691 dma_addr_t pdma_phys_addr1,
10692 uint16_t xritag)
10693{
10694 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10695 LPFC_MBOXQ_t *mbox;
10696 int rc;
10697 uint32_t shdr_status, shdr_add_status;
10698 union lpfc_sli4_cfg_shdr *shdr;
10699
10700 if (xritag == NO_XRI) {
10701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10702 "0364 Invalid param:\n");
10703 return -EINVAL;
10704 }
10705
10706 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10707 if (!mbox)
10708 return -ENOMEM;
10709
10710 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10711 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10712 sizeof(struct lpfc_mbx_post_sgl_pages) -
10713 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10714
10715 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10716 &mbox->u.mqe.un.post_sgl_pages;
10717 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10718 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10719
10720 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10721 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10722 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10723 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10724
10725 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10726 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10727 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10728 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10729 if (!phba->sli4_hba.intr_enable)
10730 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10731 else
10732 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10733 /* The IOCTL status is embedded in the mailbox subheader. */
10734 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10735 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10736 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10737 if (rc != MBX_TIMEOUT)
10738 mempool_free(mbox, phba->mbox_mem_pool);
10739 if (shdr_status || shdr_add_status || rc) {
10740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10741 "2511 POST_SGL mailbox failed with "
10742 "status x%x add_status x%x, mbx status x%x\n",
10743 shdr_status, shdr_add_status, rc);
10744 rc = -ENXIO;
10745 }
10746 return 0;
10747}
10748/**
10749 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10750 * @phba: The virtual port for which this call being executed.
10751 *
10752 * This routine will remove all of the sgl pages registered with the hba.
10753 *
10754 * Return codes:
10755 * 0 - Success
10756 * -ENXIO, -ENOMEM - Failure
10757 **/
10758int
10759lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10760{
10761 LPFC_MBOXQ_t *mbox;
10762 int rc;
10763 uint32_t shdr_status, shdr_add_status;
10764 union lpfc_sli4_cfg_shdr *shdr;
10765
10766 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10767 if (!mbox)
10768 return -ENOMEM;
10769
10770 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10771 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10772 LPFC_SLI4_MBX_EMBED);
10773 if (!phba->sli4_hba.intr_enable)
10774 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10775 else
10776 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10777 /* The IOCTL status is embedded in the mailbox subheader. */
10778 shdr = (union lpfc_sli4_cfg_shdr *)
10779 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10780 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10781 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10782 if (rc != MBX_TIMEOUT)
10783 mempool_free(mbox, phba->mbox_mem_pool);
10784 if (shdr_status || shdr_add_status || rc) {
10785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10786 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10787 "status x%x add_status x%x, mbx status x%x\n",
10788 shdr_status, shdr_add_status, rc);
10789 rc = -ENXIO;
10790 }
10791 return rc;
10792}
10793
10794/**
10795 * lpfc_sli4_next_xritag - Get an xritag for the io
10796 * @phba: Pointer to HBA context object.
10797 *
10798 * This function gets an xritag for the iocb. If there is no unused xritag
10799 * it will return 0xffff.
10800 * The function returns the allocated xritag if successful, else returns zero.
10801 * Zero is not a valid xritag.
10802 * The caller is not required to hold any lock.
10803 **/
10804uint16_t
10805lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10806{
10807 uint16_t xritag;
10808
10809 spin_lock_irq(&phba->hbalock);
10810 xritag = phba->sli4_hba.next_xri;
10811 if ((xritag != (uint16_t) -1) && xritag <
10812 (phba->sli4_hba.max_cfg_param.max_xri
10813 + phba->sli4_hba.max_cfg_param.xri_base)) {
10814 phba->sli4_hba.next_xri++;
10815 phba->sli4_hba.max_cfg_param.xri_used++;
10816 spin_unlock_irq(&phba->hbalock);
10817 return xritag;
10818 }
10819 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010820 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010821 "2004 Failed to allocate XRI.last XRITAG is %d"
10822 " Max XRI is %d, Used XRI is %d\n",
10823 phba->sli4_hba.next_xri,
10824 phba->sli4_hba.max_cfg_param.max_xri,
10825 phba->sli4_hba.max_cfg_param.xri_used);
10826 return -1;
10827}
10828
10829/**
10830 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10831 * @phba: pointer to lpfc hba data structure.
10832 *
10833 * This routine is invoked to post a block of driver's sgl pages to the
10834 * HBA using non-embedded mailbox command. No Lock is held. This routine
10835 * is only called when the driver is loading and after all IO has been
10836 * stopped.
10837 **/
10838int
10839lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10840{
10841 struct lpfc_sglq *sglq_entry;
10842 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10843 struct sgl_page_pairs *sgl_pg_pairs;
10844 void *viraddr;
10845 LPFC_MBOXQ_t *mbox;
10846 uint32_t reqlen, alloclen, pg_pairs;
10847 uint32_t mbox_tmo;
10848 uint16_t xritag_start = 0;
10849 int els_xri_cnt, rc = 0;
10850 uint32_t shdr_status, shdr_add_status;
10851 union lpfc_sli4_cfg_shdr *shdr;
10852
10853 /* The number of sgls to be posted */
10854 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10855
10856 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10857 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010858 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010859 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10860 "2559 Block sgl registration required DMA "
10861 "size (%d) great than a page\n", reqlen);
10862 return -ENOMEM;
10863 }
10864 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10865 if (!mbox) {
10866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10867 "2560 Failed to allocate mbox cmd memory\n");
10868 return -ENOMEM;
10869 }
10870
10871 /* Allocate DMA memory and set up the non-embedded mailbox command */
10872 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10873 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10874 LPFC_SLI4_MBX_NEMBED);
10875
10876 if (alloclen < reqlen) {
10877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10878 "0285 Allocated DMA memory size (%d) is "
10879 "less than the requested DMA memory "
10880 "size (%d)\n", alloclen, reqlen);
10881 lpfc_sli4_mbox_cmd_free(phba, mbox);
10882 return -ENOMEM;
10883 }
James Smart4f774512009-05-22 14:52:35 -040010884 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010885 viraddr = mbox->sge_array->addr[0];
10886
10887 /* Set up the SGL pages in the non-embedded DMA pages */
10888 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10889 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10890
10891 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10892 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10893 /* Set up the sge entry */
10894 sgl_pg_pairs->sgl_pg0_addr_lo =
10895 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10896 sgl_pg_pairs->sgl_pg0_addr_hi =
10897 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10898 sgl_pg_pairs->sgl_pg1_addr_lo =
10899 cpu_to_le32(putPaddrLow(0));
10900 sgl_pg_pairs->sgl_pg1_addr_hi =
10901 cpu_to_le32(putPaddrHigh(0));
10902 /* Keep the first xritag on the list */
10903 if (pg_pairs == 0)
10904 xritag_start = sglq_entry->sli4_xritag;
10905 sgl_pg_pairs++;
10906 }
10907 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010908 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010909 /* Perform endian conversion if necessary */
10910 sgl->word0 = cpu_to_le32(sgl->word0);
10911
10912 if (!phba->sli4_hba.intr_enable)
10913 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10914 else {
10915 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10916 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10917 }
10918 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10919 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10920 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10921 if (rc != MBX_TIMEOUT)
10922 lpfc_sli4_mbox_cmd_free(phba, mbox);
10923 if (shdr_status || shdr_add_status || rc) {
10924 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10925 "2513 POST_SGL_BLOCK mailbox command failed "
10926 "status x%x add_status x%x mbx status x%x\n",
10927 shdr_status, shdr_add_status, rc);
10928 rc = -ENXIO;
10929 }
10930 return rc;
10931}
10932
10933/**
10934 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10935 * @phba: pointer to lpfc hba data structure.
10936 * @sblist: pointer to scsi buffer list.
10937 * @count: number of scsi buffers on the list.
10938 *
10939 * This routine is invoked to post a block of @count scsi sgl pages from a
10940 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10941 * No Lock is held.
10942 *
10943 **/
10944int
10945lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10946 int cnt)
10947{
10948 struct lpfc_scsi_buf *psb;
10949 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10950 struct sgl_page_pairs *sgl_pg_pairs;
10951 void *viraddr;
10952 LPFC_MBOXQ_t *mbox;
10953 uint32_t reqlen, alloclen, pg_pairs;
10954 uint32_t mbox_tmo;
10955 uint16_t xritag_start = 0;
10956 int rc = 0;
10957 uint32_t shdr_status, shdr_add_status;
10958 dma_addr_t pdma_phys_bpl1;
10959 union lpfc_sli4_cfg_shdr *shdr;
10960
10961 /* Calculate the requested length of the dma memory */
10962 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10963 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010964 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010965 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10966 "0217 Block sgl registration required DMA "
10967 "size (%d) great than a page\n", reqlen);
10968 return -ENOMEM;
10969 }
10970 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10971 if (!mbox) {
10972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10973 "0283 Failed to allocate mbox cmd memory\n");
10974 return -ENOMEM;
10975 }
10976
10977 /* Allocate DMA memory and set up the non-embedded mailbox command */
10978 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10979 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10980 LPFC_SLI4_MBX_NEMBED);
10981
10982 if (alloclen < reqlen) {
10983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10984 "2561 Allocated DMA memory size (%d) is "
10985 "less than the requested DMA memory "
10986 "size (%d)\n", alloclen, reqlen);
10987 lpfc_sli4_mbox_cmd_free(phba, mbox);
10988 return -ENOMEM;
10989 }
James Smart4f774512009-05-22 14:52:35 -040010990 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010991 viraddr = mbox->sge_array->addr[0];
10992
10993 /* Set up the SGL pages in the non-embedded DMA pages */
10994 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10995 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10996
10997 pg_pairs = 0;
10998 list_for_each_entry(psb, sblist, list) {
10999 /* Set up the sge entry */
11000 sgl_pg_pairs->sgl_pg0_addr_lo =
11001 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11002 sgl_pg_pairs->sgl_pg0_addr_hi =
11003 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11004 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11005 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11006 else
11007 pdma_phys_bpl1 = 0;
11008 sgl_pg_pairs->sgl_pg1_addr_lo =
11009 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11010 sgl_pg_pairs->sgl_pg1_addr_hi =
11011 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11012 /* Keep the first xritag on the list */
11013 if (pg_pairs == 0)
11014 xritag_start = psb->cur_iocbq.sli4_xritag;
11015 sgl_pg_pairs++;
11016 pg_pairs++;
11017 }
11018 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11019 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11020 /* Perform endian conversion if necessary */
11021 sgl->word0 = cpu_to_le32(sgl->word0);
11022
11023 if (!phba->sli4_hba.intr_enable)
11024 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11025 else {
11026 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11027 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11028 }
11029 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11030 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11031 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11032 if (rc != MBX_TIMEOUT)
11033 lpfc_sli4_mbox_cmd_free(phba, mbox);
11034 if (shdr_status || shdr_add_status || rc) {
11035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11036 "2564 POST_SGL_BLOCK mailbox command failed "
11037 "status x%x add_status x%x mbx status x%x\n",
11038 shdr_status, shdr_add_status, rc);
11039 rc = -ENXIO;
11040 }
11041 return rc;
11042}
11043
11044/**
11045 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11046 * @phba: pointer to lpfc_hba struct that the frame was received on
11047 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11048 *
11049 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11050 * valid type of frame that the LPFC driver will handle. This function will
11051 * return a zero if the frame is a valid frame or a non zero value when the
11052 * frame does not pass the check.
11053 **/
11054static int
11055lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11056{
11057 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11058 char *type_names[] = FC_TYPE_NAMES_INIT;
11059 struct fc_vft_header *fc_vft_hdr;
11060
11061 switch (fc_hdr->fh_r_ctl) {
11062 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11063 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11064 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11065 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11066 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11067 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11068 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11069 case FC_RCTL_DD_CMD_STATUS: /* command status */
11070 case FC_RCTL_ELS_REQ: /* extended link services request */
11071 case FC_RCTL_ELS_REP: /* extended link services reply */
11072 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11073 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11074 case FC_RCTL_BA_NOP: /* basic link service NOP */
11075 case FC_RCTL_BA_ABTS: /* basic link service abort */
11076 case FC_RCTL_BA_RMC: /* remove connection */
11077 case FC_RCTL_BA_ACC: /* basic accept */
11078 case FC_RCTL_BA_RJT: /* basic reject */
11079 case FC_RCTL_BA_PRMT:
11080 case FC_RCTL_ACK_1: /* acknowledge_1 */
11081 case FC_RCTL_ACK_0: /* acknowledge_0 */
11082 case FC_RCTL_P_RJT: /* port reject */
11083 case FC_RCTL_F_RJT: /* fabric reject */
11084 case FC_RCTL_P_BSY: /* port busy */
11085 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11086 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11087 case FC_RCTL_LCR: /* link credit reset */
11088 case FC_RCTL_END: /* end */
11089 break;
11090 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11091 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11092 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11093 return lpfc_fc_frame_check(phba, fc_hdr);
11094 default:
11095 goto drop;
11096 }
11097 switch (fc_hdr->fh_type) {
11098 case FC_TYPE_BLS:
11099 case FC_TYPE_ELS:
11100 case FC_TYPE_FCP:
11101 case FC_TYPE_CT:
11102 break;
11103 case FC_TYPE_IP:
11104 case FC_TYPE_ILS:
11105 default:
11106 goto drop;
11107 }
11108 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11109 "2538 Received frame rctl:%s type:%s\n",
11110 rctl_names[fc_hdr->fh_r_ctl],
11111 type_names[fc_hdr->fh_type]);
11112 return 0;
11113drop:
11114 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11115 "2539 Dropped frame rctl:%s type:%s\n",
11116 rctl_names[fc_hdr->fh_r_ctl],
11117 type_names[fc_hdr->fh_type]);
11118 return 1;
11119}
11120
11121/**
11122 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11123 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11124 *
11125 * This function processes the FC header to retrieve the VFI from the VF
11126 * header, if one exists. This function will return the VFI if one exists
11127 * or 0 if no VSAN Header exists.
11128 **/
11129static uint32_t
11130lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11131{
11132 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11133
11134 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11135 return 0;
11136 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11137}
11138
11139/**
11140 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11141 * @phba: Pointer to the HBA structure to search for the vport on
11142 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11143 * @fcfi: The FC Fabric ID that the frame came from
11144 *
11145 * This function searches the @phba for a vport that matches the content of the
11146 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11147 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11148 * returns the matching vport pointer or NULL if unable to match frame to a
11149 * vport.
11150 **/
11151static struct lpfc_vport *
11152lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11153 uint16_t fcfi)
11154{
11155 struct lpfc_vport **vports;
11156 struct lpfc_vport *vport = NULL;
11157 int i;
11158 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11159 fc_hdr->fh_d_id[1] << 8 |
11160 fc_hdr->fh_d_id[2]);
11161
11162 vports = lpfc_create_vport_work_array(phba);
11163 if (vports != NULL)
11164 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11165 if (phba->fcf.fcfi == fcfi &&
11166 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11167 vports[i]->fc_myDID == did) {
11168 vport = vports[i];
11169 break;
11170 }
11171 }
11172 lpfc_destroy_vport_work_array(phba, vports);
11173 return vport;
11174}
11175
11176/**
James Smart45ed1192009-10-02 15:17:02 -040011177 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11178 * @vport: The vport to work on.
11179 *
11180 * This function updates the receive sequence time stamp for this vport. The
11181 * receive sequence time stamp indicates the time that the last frame of the
11182 * the sequence that has been idle for the longest amount of time was received.
11183 * the driver uses this time stamp to indicate if any received sequences have
11184 * timed out.
11185 **/
11186void
11187lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11188{
11189 struct lpfc_dmabuf *h_buf;
11190 struct hbq_dmabuf *dmabuf = NULL;
11191
11192 /* get the oldest sequence on the rcv list */
11193 h_buf = list_get_first(&vport->rcv_buffer_list,
11194 struct lpfc_dmabuf, list);
11195 if (!h_buf)
11196 return;
11197 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11198 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11199}
11200
11201/**
11202 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11203 * @vport: The vport that the received sequences were sent to.
11204 *
11205 * This function cleans up all outstanding received sequences. This is called
11206 * by the driver when a link event or user action invalidates all the received
11207 * sequences.
11208 **/
11209void
11210lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11211{
11212 struct lpfc_dmabuf *h_buf, *hnext;
11213 struct lpfc_dmabuf *d_buf, *dnext;
11214 struct hbq_dmabuf *dmabuf = NULL;
11215
11216 /* start with the oldest sequence on the rcv list */
11217 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11218 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11219 list_del_init(&dmabuf->hbuf.list);
11220 list_for_each_entry_safe(d_buf, dnext,
11221 &dmabuf->dbuf.list, list) {
11222 list_del_init(&d_buf->list);
11223 lpfc_in_buf_free(vport->phba, d_buf);
11224 }
11225 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11226 }
11227}
11228
11229/**
11230 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11231 * @vport: The vport that the received sequences were sent to.
11232 *
11233 * This function determines whether any received sequences have timed out by
11234 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11235 * indicates that there is at least one timed out sequence this routine will
11236 * go through the received sequences one at a time from most inactive to most
11237 * active to determine which ones need to be cleaned up. Once it has determined
11238 * that a sequence needs to be cleaned up it will simply free up the resources
11239 * without sending an abort.
11240 **/
11241void
11242lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11243{
11244 struct lpfc_dmabuf *h_buf, *hnext;
11245 struct lpfc_dmabuf *d_buf, *dnext;
11246 struct hbq_dmabuf *dmabuf = NULL;
11247 unsigned long timeout;
11248 int abort_count = 0;
11249
11250 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11251 vport->rcv_buffer_time_stamp);
11252 if (list_empty(&vport->rcv_buffer_list) ||
11253 time_before(jiffies, timeout))
11254 return;
11255 /* start with the oldest sequence on the rcv list */
11256 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11257 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11258 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11259 dmabuf->time_stamp);
11260 if (time_before(jiffies, timeout))
11261 break;
11262 abort_count++;
11263 list_del_init(&dmabuf->hbuf.list);
11264 list_for_each_entry_safe(d_buf, dnext,
11265 &dmabuf->dbuf.list, list) {
11266 list_del_init(&d_buf->list);
11267 lpfc_in_buf_free(vport->phba, d_buf);
11268 }
11269 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11270 }
11271 if (abort_count)
11272 lpfc_update_rcv_time_stamp(vport);
11273}
11274
11275/**
James Smart4f774512009-05-22 14:52:35 -040011276 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11277 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11278 *
11279 * This function searches through the existing incomplete sequences that have
11280 * been sent to this @vport. If the frame matches one of the incomplete
11281 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11282 * make up that sequence. If no sequence is found that matches this frame then
11283 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11284 * This function returns a pointer to the first dmabuf in the sequence list that
11285 * the frame was linked to.
11286 **/
11287static struct hbq_dmabuf *
11288lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11289{
11290 struct fc_frame_header *new_hdr;
11291 struct fc_frame_header *temp_hdr;
11292 struct lpfc_dmabuf *d_buf;
11293 struct lpfc_dmabuf *h_buf;
11294 struct hbq_dmabuf *seq_dmabuf = NULL;
11295 struct hbq_dmabuf *temp_dmabuf = NULL;
11296
James Smart4d9ab992009-10-02 15:16:39 -040011297 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011298 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011299 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11300 /* Use the hdr_buf to find the sequence that this frame belongs to */
11301 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11302 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11303 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11304 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11305 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11306 continue;
11307 /* found a pending sequence that matches this frame */
11308 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11309 break;
11310 }
11311 if (!seq_dmabuf) {
11312 /*
11313 * This indicates first frame received for this sequence.
11314 * Queue the buffer on the vport's rcv_buffer_list.
11315 */
11316 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011317 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011318 return dmabuf;
11319 }
11320 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011321 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11322 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011323 list_del_init(&seq_dmabuf->hbuf.list);
11324 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11325 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011326 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011327 return dmabuf;
11328 }
James Smart45ed1192009-10-02 15:17:02 -040011329 /* move this sequence to the tail to indicate a young sequence */
11330 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11331 seq_dmabuf->time_stamp = jiffies;
11332 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011333 if (list_empty(&seq_dmabuf->dbuf.list)) {
11334 temp_hdr = dmabuf->hbuf.virt;
11335 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11336 return seq_dmabuf;
11337 }
James Smart4f774512009-05-22 14:52:35 -040011338 /* find the correct place in the sequence to insert this frame */
11339 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11340 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11341 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11342 /*
11343 * If the frame's sequence count is greater than the frame on
11344 * the list then insert the frame right after this frame
11345 */
James Smarteeead812009-12-21 17:01:23 -050011346 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11347 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011348 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11349 return seq_dmabuf;
11350 }
11351 }
11352 return NULL;
11353}
11354
11355/**
James Smart6669f9b2009-10-02 15:16:45 -040011356 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11357 * @vport: pointer to a vitural port
11358 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11359 *
11360 * This function tries to abort from the partially assembed sequence, described
11361 * by the information from basic abbort @dmabuf. It checks to see whether such
11362 * partially assembled sequence held by the driver. If so, it shall free up all
11363 * the frames from the partially assembled sequence.
11364 *
11365 * Return
11366 * true -- if there is matching partially assembled sequence present and all
11367 * the frames freed with the sequence;
11368 * false -- if there is no matching partially assembled sequence present so
11369 * nothing got aborted in the lower layer driver
11370 **/
11371static bool
11372lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11373 struct hbq_dmabuf *dmabuf)
11374{
11375 struct fc_frame_header *new_hdr;
11376 struct fc_frame_header *temp_hdr;
11377 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11378 struct hbq_dmabuf *seq_dmabuf = NULL;
11379
11380 /* Use the hdr_buf to find the sequence that matches this frame */
11381 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11382 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11383 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11384 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11385 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11386 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11387 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11388 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11389 continue;
11390 /* found a pending sequence that matches this frame */
11391 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11392 break;
11393 }
11394
11395 /* Free up all the frames from the partially assembled sequence */
11396 if (seq_dmabuf) {
11397 list_for_each_entry_safe(d_buf, n_buf,
11398 &seq_dmabuf->dbuf.list, list) {
11399 list_del_init(&d_buf->list);
11400 lpfc_in_buf_free(vport->phba, d_buf);
11401 }
11402 return true;
11403 }
11404 return false;
11405}
11406
11407/**
11408 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11409 * @phba: Pointer to HBA context object.
11410 * @cmd_iocbq: pointer to the command iocbq structure.
11411 * @rsp_iocbq: pointer to the response iocbq structure.
11412 *
11413 * This function handles the sequence abort accept iocb command complete
11414 * event. It properly releases the memory allocated to the sequence abort
11415 * accept iocb.
11416 **/
11417static void
11418lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11419 struct lpfc_iocbq *cmd_iocbq,
11420 struct lpfc_iocbq *rsp_iocbq)
11421{
11422 if (cmd_iocbq)
11423 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11424}
11425
11426/**
11427 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11428 * @phba: Pointer to HBA context object.
11429 * @fc_hdr: pointer to a FC frame header.
11430 *
11431 * This function sends a basic accept to a previous unsol sequence abort
11432 * event after aborting the sequence handling.
11433 **/
11434static void
11435lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11436 struct fc_frame_header *fc_hdr)
11437{
11438 struct lpfc_iocbq *ctiocb = NULL;
11439 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011440 uint16_t oxid, rxid;
11441 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011442 IOCB_t *icmd;
11443
11444 if (!lpfc_is_link_up(phba))
11445 return;
11446
11447 sid = sli4_sid_from_fc_hdr(fc_hdr);
11448 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011449 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011450
11451 ndlp = lpfc_findnode_did(phba->pport, sid);
11452 if (!ndlp) {
11453 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11454 "1268 Find ndlp returned NULL for oxid:x%x "
11455 "SID:x%x\n", oxid, sid);
11456 return;
11457 }
11458
11459 /* Allocate buffer for acc iocb */
11460 ctiocb = lpfc_sli_get_iocbq(phba);
11461 if (!ctiocb)
11462 return;
11463
James Smart5ffc2662009-11-18 15:39:44 -050011464 /* Extract the F_CTL field from FC_HDR */
11465 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11466
James Smart6669f9b2009-10-02 15:16:45 -040011467 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011468 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011469 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011470 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11471 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11472 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11473
11474 /* Fill in the rest of iocb fields */
11475 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11476 icmd->ulpBdeCount = 0;
11477 icmd->ulpLe = 1;
11478 icmd->ulpClass = CLASS3;
11479 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011480
James Smart6669f9b2009-10-02 15:16:45 -040011481 ctiocb->iocb_cmpl = NULL;
11482 ctiocb->vport = phba->pport;
11483 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11484
James Smart5ffc2662009-11-18 15:39:44 -050011485 if (fctl & FC_FC_EX_CTX) {
11486 /* ABTS sent by responder to CT exchange, construction
11487 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11488 * field and RX_ID from ABTS for RX_ID field.
11489 */
11490 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11491 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11492 ctiocb->sli4_xritag = oxid;
11493 } else {
11494 /* ABTS sent by initiator to CT exchange, construction
11495 * of BA_ACC will need to allocate a new XRI as for the
11496 * XRI_TAG and RX_ID fields.
11497 */
11498 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11499 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11500 ctiocb->sli4_xritag = NO_XRI;
11501 }
11502 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11503
James Smart6669f9b2009-10-02 15:16:45 -040011504 /* Xmit CT abts accept on exchange <xid> */
11505 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11506 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11507 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11508 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11509}
11510
11511/**
11512 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11513 * @vport: Pointer to the vport on which this sequence was received
11514 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11515 *
11516 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11517 * receive sequence is only partially assembed by the driver, it shall abort
11518 * the partially assembled frames for the sequence. Otherwise, if the
11519 * unsolicited receive sequence has been completely assembled and passed to
11520 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11521 * unsolicited sequence has been aborted. After that, it will issue a basic
11522 * accept to accept the abort.
11523 **/
11524void
11525lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11526 struct hbq_dmabuf *dmabuf)
11527{
11528 struct lpfc_hba *phba = vport->phba;
11529 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011530 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011531 bool abts_par;
11532
James Smart6669f9b2009-10-02 15:16:45 -040011533 /* Make a copy of fc_hdr before the dmabuf being released */
11534 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011535 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011536
James Smart5ffc2662009-11-18 15:39:44 -050011537 if (fctl & FC_FC_EX_CTX) {
11538 /*
11539 * ABTS sent by responder to exchange, just free the buffer
11540 */
James Smart6669f9b2009-10-02 15:16:45 -040011541 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011542 } else {
11543 /*
11544 * ABTS sent by initiator to exchange, need to do cleanup
11545 */
11546 /* Try to abort partially assembled seq */
11547 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11548
11549 /* Send abort to ULP if partially seq abort failed */
11550 if (abts_par == false)
11551 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11552 else
11553 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11554 }
James Smart6669f9b2009-10-02 15:16:45 -040011555 /* Send basic accept (BA_ACC) to the abort requester */
11556 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11557}
11558
11559/**
James Smart4f774512009-05-22 14:52:35 -040011560 * lpfc_seq_complete - Indicates if a sequence is complete
11561 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11562 *
11563 * This function checks the sequence, starting with the frame described by
11564 * @dmabuf, to see if all the frames associated with this sequence are present.
11565 * the frames associated with this sequence are linked to the @dmabuf using the
11566 * dbuf list. This function looks for two major things. 1) That the first frame
11567 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11568 * set. 3) That there are no holes in the sequence count. The function will
11569 * return 1 when the sequence is complete, otherwise it will return 0.
11570 **/
11571static int
11572lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11573{
11574 struct fc_frame_header *hdr;
11575 struct lpfc_dmabuf *d_buf;
11576 struct hbq_dmabuf *seq_dmabuf;
11577 uint32_t fctl;
11578 int seq_count = 0;
11579
11580 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11581 /* make sure first fame of sequence has a sequence count of zero */
11582 if (hdr->fh_seq_cnt != seq_count)
11583 return 0;
11584 fctl = (hdr->fh_f_ctl[0] << 16 |
11585 hdr->fh_f_ctl[1] << 8 |
11586 hdr->fh_f_ctl[2]);
11587 /* If last frame of sequence we can return success. */
11588 if (fctl & FC_FC_END_SEQ)
11589 return 1;
11590 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11591 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11592 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11593 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011594 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011595 return 0;
11596 fctl = (hdr->fh_f_ctl[0] << 16 |
11597 hdr->fh_f_ctl[1] << 8 |
11598 hdr->fh_f_ctl[2]);
11599 /* If last frame of sequence we can return success. */
11600 if (fctl & FC_FC_END_SEQ)
11601 return 1;
11602 }
11603 return 0;
11604}
11605
11606/**
11607 * lpfc_prep_seq - Prep sequence for ULP processing
11608 * @vport: Pointer to the vport on which this sequence was received
11609 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11610 *
11611 * This function takes a sequence, described by a list of frames, and creates
11612 * a list of iocbq structures to describe the sequence. This iocbq list will be
11613 * used to issue to the generic unsolicited sequence handler. This routine
11614 * returns a pointer to the first iocbq in the list. If the function is unable
11615 * to allocate an iocbq then it throw out the received frames that were not
11616 * able to be described and return a pointer to the first iocbq. If unable to
11617 * allocate any iocbqs (including the first) this function will return NULL.
11618 **/
11619static struct lpfc_iocbq *
11620lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11621{
11622 struct lpfc_dmabuf *d_buf, *n_buf;
11623 struct lpfc_iocbq *first_iocbq, *iocbq;
11624 struct fc_frame_header *fc_hdr;
11625 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011626 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011627
11628 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11629 /* remove from receive buffer list */
11630 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011631 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011632 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011633 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011634 /* Get an iocbq struct to fill in. */
11635 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11636 if (first_iocbq) {
11637 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011638 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011639 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11640 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11641 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11642 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11643 vport->vpi + vport->phba->vpi_base;
11644 /* put the first buffer into the first IOCBq */
11645 first_iocbq->context2 = &seq_dmabuf->dbuf;
11646 first_iocbq->context3 = NULL;
11647 first_iocbq->iocb.ulpBdeCount = 1;
11648 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11649 LPFC_DATA_BUF_SIZE;
11650 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011651 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011652 bf_get(lpfc_rcqe_length,
11653 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011654 }
11655 iocbq = first_iocbq;
11656 /*
11657 * Each IOCBq can have two Buffers assigned, so go through the list
11658 * of buffers for this sequence and save two buffers in each IOCBq
11659 */
11660 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11661 if (!iocbq) {
11662 lpfc_in_buf_free(vport->phba, d_buf);
11663 continue;
11664 }
11665 if (!iocbq->context3) {
11666 iocbq->context3 = d_buf;
11667 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011668 pbde = (struct ulp_bde64 *)
11669 &iocbq->iocb.unsli3.sli3Words[4];
11670 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011671 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011672 bf_get(lpfc_rcqe_length,
11673 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011674 } else {
11675 iocbq = lpfc_sli_get_iocbq(vport->phba);
11676 if (!iocbq) {
11677 if (first_iocbq) {
11678 first_iocbq->iocb.ulpStatus =
11679 IOSTAT_FCP_RSP_ERROR;
11680 first_iocbq->iocb.un.ulpWord[4] =
11681 IOERR_NO_RESOURCES;
11682 }
11683 lpfc_in_buf_free(vport->phba, d_buf);
11684 continue;
11685 }
11686 iocbq->context2 = d_buf;
11687 iocbq->context3 = NULL;
11688 iocbq->iocb.ulpBdeCount = 1;
11689 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11690 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011691 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011692 bf_get(lpfc_rcqe_length,
11693 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011694 iocbq->iocb.un.rcvels.remoteID = sid;
11695 list_add_tail(&iocbq->list, &first_iocbq->list);
11696 }
11697 }
11698 return first_iocbq;
11699}
11700
James Smart6669f9b2009-10-02 15:16:45 -040011701static void
11702lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11703 struct hbq_dmabuf *seq_dmabuf)
11704{
11705 struct fc_frame_header *fc_hdr;
11706 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11707 struct lpfc_hba *phba = vport->phba;
11708
11709 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11710 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11711 if (!iocbq) {
11712 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11713 "2707 Ring %d handler: Failed to allocate "
11714 "iocb Rctl x%x Type x%x received\n",
11715 LPFC_ELS_RING,
11716 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11717 return;
11718 }
11719 if (!lpfc_complete_unsol_iocb(phba,
11720 &phba->sli.ring[LPFC_ELS_RING],
11721 iocbq, fc_hdr->fh_r_ctl,
11722 fc_hdr->fh_type))
11723 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11724 "2540 Ring %d handler: unexpected Rctl "
11725 "x%x Type x%x received\n",
11726 LPFC_ELS_RING,
11727 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11728
11729 /* Free iocb created in lpfc_prep_seq */
11730 list_for_each_entry_safe(curr_iocb, next_iocb,
11731 &iocbq->list, list) {
11732 list_del_init(&curr_iocb->list);
11733 lpfc_sli_release_iocbq(phba, curr_iocb);
11734 }
11735 lpfc_sli_release_iocbq(phba, iocbq);
11736}
11737
James Smart4f774512009-05-22 14:52:35 -040011738/**
11739 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11740 * @phba: Pointer to HBA context object.
11741 *
11742 * This function is called with no lock held. This function processes all
11743 * the received buffers and gives it to upper layers when a received buffer
11744 * indicates that it is the final frame in the sequence. The interrupt
11745 * service routine processes received buffers at interrupt contexts and adds
11746 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11747 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11748 * appropriate receive function when the final frame in a sequence is received.
11749 **/
James Smart4d9ab992009-10-02 15:16:39 -040011750void
11751lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11752 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011753{
James Smart4d9ab992009-10-02 15:16:39 -040011754 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011755 struct fc_frame_header *fc_hdr;
11756 struct lpfc_vport *vport;
11757 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011758
James Smart4f774512009-05-22 14:52:35 -040011759 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011760 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11761 /* check to see if this a valid type of frame */
11762 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11763 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11764 return;
11765 }
11766 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11767 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011768 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011769 /* throw out the frame */
11770 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11771 return;
11772 }
James Smart6669f9b2009-10-02 15:16:45 -040011773 /* Handle the basic abort sequence (BA_ABTS) event */
11774 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11775 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11776 return;
11777 }
11778
James Smart4d9ab992009-10-02 15:16:39 -040011779 /* Link this frame */
11780 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11781 if (!seq_dmabuf) {
11782 /* unable to add frame to vport - throw it out */
11783 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11784 return;
11785 }
11786 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011787 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011788 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011789
James Smart6669f9b2009-10-02 15:16:45 -040011790 /* Send the complete sequence to the upper layer protocol */
11791 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011792}
James Smart6fb120a2009-05-22 14:52:59 -040011793
11794/**
11795 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11796 * @phba: pointer to lpfc hba data structure.
11797 *
11798 * This routine is invoked to post rpi header templates to the
11799 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011800 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11801 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011802 *
11803 * This routine does not require any locks. It's usage is expected
11804 * to be driver load or reset recovery when the driver is
11805 * sequential.
11806 *
11807 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011808 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011809 * EIO - The mailbox failed to complete successfully.
11810 * When this error occurs, the driver is not guaranteed
11811 * to have any rpi regions posted to the device and
11812 * must either attempt to repost the regions or take a
11813 * fatal error.
11814 **/
11815int
11816lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11817{
11818 struct lpfc_rpi_hdr *rpi_page;
11819 uint32_t rc = 0;
11820
11821 /* Post all rpi memory regions to the port. */
11822 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11823 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11824 if (rc != MBX_SUCCESS) {
11825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11826 "2008 Error %d posting all rpi "
11827 "headers\n", rc);
11828 rc = -EIO;
11829 break;
11830 }
11831 }
11832
11833 return rc;
11834}
11835
11836/**
11837 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11838 * @phba: pointer to lpfc hba data structure.
11839 * @rpi_page: pointer to the rpi memory region.
11840 *
11841 * This routine is invoked to post a single rpi header to the
11842 * HBA consistent with the SLI-4 interface spec. This memory region
11843 * maps up to 64 rpi context regions.
11844 *
11845 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011846 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011847 * ENOMEM - No available memory
11848 * EIO - The mailbox failed to complete successfully.
11849 **/
11850int
11851lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11852{
11853 LPFC_MBOXQ_t *mboxq;
11854 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11855 uint32_t rc = 0;
11856 uint32_t mbox_tmo;
11857 uint32_t shdr_status, shdr_add_status;
11858 union lpfc_sli4_cfg_shdr *shdr;
11859
11860 /* The port is notified of the header region via a mailbox command. */
11861 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11862 if (!mboxq) {
11863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11864 "2001 Unable to allocate memory for issuing "
11865 "SLI_CONFIG_SPECIAL mailbox command\n");
11866 return -ENOMEM;
11867 }
11868
11869 /* Post all rpi memory regions to the port. */
11870 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11871 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11872 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11873 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11874 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11875 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11876 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11877 hdr_tmpl, rpi_page->page_count);
11878 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11879 rpi_page->start_rpi);
11880 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11881 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011882 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011883 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11884 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11885 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11886 if (rc != MBX_TIMEOUT)
11887 mempool_free(mboxq, phba->mbox_mem_pool);
11888 if (shdr_status || shdr_add_status || rc) {
11889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11890 "2514 POST_RPI_HDR mailbox failed with "
11891 "status x%x add_status x%x, mbx status x%x\n",
11892 shdr_status, shdr_add_status, rc);
11893 rc = -ENXIO;
11894 }
11895 return rc;
11896}
11897
11898/**
11899 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11900 * @phba: pointer to lpfc hba data structure.
11901 *
11902 * This routine is invoked to post rpi header templates to the
11903 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011904 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11905 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011906 *
11907 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011908 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011909 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11910 **/
11911int
11912lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11913{
11914 int rpi;
11915 uint16_t max_rpi, rpi_base, rpi_limit;
11916 uint16_t rpi_remaining;
11917 struct lpfc_rpi_hdr *rpi_hdr;
11918
11919 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11920 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11921 rpi_limit = phba->sli4_hba.next_rpi;
11922
11923 /*
11924 * The valid rpi range is not guaranteed to be zero-based. Start
11925 * the search at the rpi_base as reported by the port.
11926 */
11927 spin_lock_irq(&phba->hbalock);
11928 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11929 if (rpi >= rpi_limit || rpi < rpi_base)
11930 rpi = LPFC_RPI_ALLOC_ERROR;
11931 else {
11932 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11933 phba->sli4_hba.max_cfg_param.rpi_used++;
11934 phba->sli4_hba.rpi_count++;
11935 }
11936
11937 /*
11938 * Don't try to allocate more rpi header regions if the device limit
11939 * on available rpis max has been exhausted.
11940 */
11941 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11942 (phba->sli4_hba.rpi_count >= max_rpi)) {
11943 spin_unlock_irq(&phba->hbalock);
11944 return rpi;
11945 }
11946
11947 /*
11948 * If the driver is running low on rpi resources, allocate another
11949 * page now. Note that the next_rpi value is used because
11950 * it represents how many are actually in use whereas max_rpi notes
11951 * how many are supported max by the device.
11952 */
11953 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11954 phba->sli4_hba.rpi_count;
11955 spin_unlock_irq(&phba->hbalock);
11956 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11957 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11958 if (!rpi_hdr) {
11959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11960 "2002 Error Could not grow rpi "
11961 "count\n");
11962 } else {
11963 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11964 }
11965 }
11966
11967 return rpi;
11968}
11969
11970/**
11971 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11972 * @phba: pointer to lpfc hba data structure.
11973 *
11974 * This routine is invoked to release an rpi to the pool of
11975 * available rpis maintained by the driver.
11976 **/
11977void
11978lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11979{
11980 spin_lock_irq(&phba->hbalock);
11981 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11982 phba->sli4_hba.rpi_count--;
11983 phba->sli4_hba.max_cfg_param.rpi_used--;
11984 spin_unlock_irq(&phba->hbalock);
11985}
11986
11987/**
11988 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11989 * @phba: pointer to lpfc hba data structure.
11990 *
11991 * This routine is invoked to remove the memory region that
11992 * provided rpi via a bitmask.
11993 **/
11994void
11995lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11996{
11997 kfree(phba->sli4_hba.rpi_bmask);
11998}
11999
12000/**
12001 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12002 * @phba: pointer to lpfc hba data structure.
12003 *
12004 * This routine is invoked to remove the memory region that
12005 * provided rpi via a bitmask.
12006 **/
12007int
12008lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12009{
12010 LPFC_MBOXQ_t *mboxq;
12011 struct lpfc_hba *phba = ndlp->phba;
12012 int rc;
12013
12014 /* The port is notified of the header region via a mailbox command. */
12015 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12016 if (!mboxq)
12017 return -ENOMEM;
12018
12019 /* Post all rpi memory regions to the port. */
12020 lpfc_resume_rpi(mboxq, ndlp);
12021 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12022 if (rc == MBX_NOT_FINISHED) {
12023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12024 "2010 Resume RPI Mailbox failed "
12025 "status %d, mbxStatus x%x\n", rc,
12026 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12027 mempool_free(mboxq, phba->mbox_mem_pool);
12028 return -EIO;
12029 }
12030 return 0;
12031}
12032
12033/**
12034 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12035 * @phba: pointer to lpfc hba data structure.
12036 * @vpi: vpi value to activate with the port.
12037 *
12038 * This routine is invoked to activate a vpi with the
12039 * port when the host intends to use vports with a
12040 * nonzero vpi.
12041 *
12042 * Returns:
12043 * 0 success
12044 * -Evalue otherwise
12045 **/
12046int
12047lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12048{
12049 LPFC_MBOXQ_t *mboxq;
12050 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012051 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012052 uint32_t mbox_tmo;
12053
12054 if (vpi == 0)
12055 return -EINVAL;
12056 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12057 if (!mboxq)
12058 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040012059 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012060 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12061 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012062 if (rc != MBX_SUCCESS) {
12063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12064 "2022 INIT VPI Mailbox failed "
12065 "status %d, mbxStatus x%x\n", rc,
12066 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012067 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012068 }
James Smart6a9c52c2009-10-02 15:16:51 -040012069 if (rc != MBX_TIMEOUT)
12070 mempool_free(mboxq, phba->mbox_mem_pool);
12071
12072 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012073}
12074
12075/**
12076 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12077 * @phba: pointer to lpfc hba data structure.
12078 * @mboxq: Pointer to mailbox object.
12079 *
12080 * This routine is invoked to manually add a single FCF record. The caller
12081 * must pass a completely initialized FCF_Record. This routine takes
12082 * care of the nonembedded mailbox operations.
12083 **/
12084static void
12085lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12086{
12087 void *virt_addr;
12088 union lpfc_sli4_cfg_shdr *shdr;
12089 uint32_t shdr_status, shdr_add_status;
12090
12091 virt_addr = mboxq->sge_array->addr[0];
12092 /* The IOCTL status is embedded in the mailbox subheader. */
12093 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12094 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12095 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12096
12097 if ((shdr_status || shdr_add_status) &&
12098 (shdr_status != STATUS_FCF_IN_USE))
12099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12100 "2558 ADD_FCF_RECORD mailbox failed with "
12101 "status x%x add_status x%x\n",
12102 shdr_status, shdr_add_status);
12103
12104 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12105}
12106
12107/**
12108 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12109 * @phba: pointer to lpfc hba data structure.
12110 * @fcf_record: pointer to the initialized fcf record to add.
12111 *
12112 * This routine is invoked to manually add a single FCF record. The caller
12113 * must pass a completely initialized FCF_Record. This routine takes
12114 * care of the nonembedded mailbox operations.
12115 **/
12116int
12117lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12118{
12119 int rc = 0;
12120 LPFC_MBOXQ_t *mboxq;
12121 uint8_t *bytep;
12122 void *virt_addr;
12123 dma_addr_t phys_addr;
12124 struct lpfc_mbx_sge sge;
12125 uint32_t alloc_len, req_len;
12126 uint32_t fcfindex;
12127
12128 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12129 if (!mboxq) {
12130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12131 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12132 return -ENOMEM;
12133 }
12134
12135 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12136 sizeof(uint32_t);
12137
12138 /* Allocate DMA memory and set up the non-embedded mailbox command */
12139 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12140 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12141 req_len, LPFC_SLI4_MBX_NEMBED);
12142 if (alloc_len < req_len) {
12143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12144 "2523 Allocated DMA memory size (x%x) is "
12145 "less than the requested DMA memory "
12146 "size (x%x)\n", alloc_len, req_len);
12147 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12148 return -ENOMEM;
12149 }
12150
12151 /*
12152 * Get the first SGE entry from the non-embedded DMA memory. This
12153 * routine only uses a single SGE.
12154 */
12155 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12156 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012157 virt_addr = mboxq->sge_array->addr[0];
12158 /*
12159 * Configure the FCF record for FCFI 0. This is the driver's
12160 * hardcoded default and gets used in nonFIP mode.
12161 */
12162 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12163 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12164 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12165
12166 /*
12167 * Copy the fcf_index and the FCF Record Data. The data starts after
12168 * the FCoE header plus word10. The data copy needs to be endian
12169 * correct.
12170 */
12171 bytep += sizeof(uint32_t);
12172 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12173 mboxq->vport = phba->pport;
12174 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12175 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12176 if (rc == MBX_NOT_FINISHED) {
12177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12178 "2515 ADD_FCF_RECORD mailbox failed with "
12179 "status 0x%x\n", rc);
12180 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12181 rc = -EIO;
12182 } else
12183 rc = 0;
12184
12185 return rc;
12186}
12187
12188/**
12189 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12190 * @phba: pointer to lpfc hba data structure.
12191 * @fcf_record: pointer to the fcf record to write the default data.
12192 * @fcf_index: FCF table entry index.
12193 *
12194 * This routine is invoked to build the driver's default FCF record. The
12195 * values used are hardcoded. This routine handles memory initialization.
12196 *
12197 **/
12198void
12199lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12200 struct fcf_record *fcf_record,
12201 uint16_t fcf_index)
12202{
12203 memset(fcf_record, 0, sizeof(struct fcf_record));
12204 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12205 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12206 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12207 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12208 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12209 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12210 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12211 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12212 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12213 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12214 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12215 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12216 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012217 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012218 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12219 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12220 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12221 /* Set the VLAN bit map */
12222 if (phba->valid_vlan) {
12223 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12224 = 1 << (phba->vlan_id % 8);
12225 }
12226}
12227
12228/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012229 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012230 * @phba: pointer to lpfc hba data structure.
12231 * @fcf_index: FCF table entry offset.
12232 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012233 * This routine is invoked to scan the entire FCF table by reading FCF
12234 * record and processing it one at a time starting from the @fcf_index
12235 * for initial FCF discovery or fast FCF failover rediscovery.
12236 *
12237 * Return 0 if the mailbox command is submitted sucessfully, none 0
12238 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012239 **/
12240int
James Smart0c9ab6f2010-02-26 14:15:57 -050012241lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012242{
12243 int rc = 0, error;
12244 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012245
James Smart32b97932009-07-19 10:01:21 -040012246 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012247 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12248 if (!mboxq) {
12249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12250 "2000 Failed to allocate mbox for "
12251 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012252 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012253 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012254 }
James Smartecfd03c2010-02-12 14:41:27 -050012255 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012256 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012257 if (rc) {
12258 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012259 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012260 }
James Smartecfd03c2010-02-12 14:41:27 -050012261 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012262 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012263 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smart6fb120a2009-05-22 14:52:59 -040012264 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012265 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012266 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012267 else {
James Smart32b97932009-07-19 10:01:21 -040012268 spin_lock_irq(&phba->hbalock);
12269 phba->hba_flag |= FCF_DISC_INPROGRESS;
12270 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012271 /* Reset FCF round robin index bmask for new scan */
James Smart999d8132010-03-15 11:24:56 -040012272 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012273 memset(phba->fcf.fcf_rr_bmask, 0,
12274 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart999d8132010-03-15 11:24:56 -040012275 phba->fcf.eligible_fcf_cnt = 0;
12276 }
James Smart6fb120a2009-05-22 14:52:59 -040012277 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012278 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012279fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012280 if (error) {
12281 if (mboxq)
12282 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12283 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12284 spin_lock_irq(&phba->hbalock);
12285 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12286 spin_unlock_irq(&phba->hbalock);
12287 }
James Smart6fb120a2009-05-22 14:52:59 -040012288 return error;
12289}
James Smarta0c87cb2009-07-19 10:01:10 -040012290
12291/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012292 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12293 * @phba: pointer to lpfc hba data structure.
12294 * @fcf_index: FCF table entry offset.
12295 *
12296 * This routine is invoked to read an FCF record indicated by @fcf_index
12297 * and to use it for FLOGI round robin FCF failover.
12298 *
12299 * Return 0 if the mailbox command is submitted sucessfully, none 0
12300 * otherwise.
12301 **/
12302int
12303lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12304{
12305 int rc = 0, error;
12306 LPFC_MBOXQ_t *mboxq;
12307
12308 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12309 if (!mboxq) {
12310 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12311 "2763 Failed to allocate mbox for "
12312 "READ_FCF cmd\n");
12313 error = -ENOMEM;
12314 goto fail_fcf_read;
12315 }
12316 /* Construct the read FCF record mailbox command */
12317 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12318 if (rc) {
12319 error = -EINVAL;
12320 goto fail_fcf_read;
12321 }
12322 /* Issue the mailbox command asynchronously */
12323 mboxq->vport = phba->pport;
12324 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12325 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12326 if (rc == MBX_NOT_FINISHED)
12327 error = -EIO;
12328 else
12329 error = 0;
12330
12331fail_fcf_read:
12332 if (error && mboxq)
12333 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12334 return error;
12335}
12336
12337/**
12338 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12339 * @phba: pointer to lpfc hba data structure.
12340 * @fcf_index: FCF table entry offset.
12341 *
12342 * This routine is invoked to read an FCF record indicated by @fcf_index to
12343 * determine whether it's eligible for FLOGI round robin failover list.
12344 *
12345 * Return 0 if the mailbox command is submitted sucessfully, none 0
12346 * otherwise.
12347 **/
12348int
12349lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12350{
12351 int rc = 0, error;
12352 LPFC_MBOXQ_t *mboxq;
12353
12354 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12355 if (!mboxq) {
12356 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12357 "2758 Failed to allocate mbox for "
12358 "READ_FCF cmd\n");
12359 error = -ENOMEM;
12360 goto fail_fcf_read;
12361 }
12362 /* Construct the read FCF record mailbox command */
12363 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12364 if (rc) {
12365 error = -EINVAL;
12366 goto fail_fcf_read;
12367 }
12368 /* Issue the mailbox command asynchronously */
12369 mboxq->vport = phba->pport;
12370 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12371 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12372 if (rc == MBX_NOT_FINISHED)
12373 error = -EIO;
12374 else
12375 error = 0;
12376
12377fail_fcf_read:
12378 if (error && mboxq)
12379 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12380 return error;
12381}
12382
12383/**
12384 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12385 * @phba: pointer to lpfc hba data structure.
12386 *
12387 * This routine is to get the next eligible FCF record index in a round
12388 * robin fashion. If the next eligible FCF record index equals to the
12389 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12390 * shall be returned, otherwise, the next eligible FCF record's index
12391 * shall be returned.
12392 **/
12393uint16_t
12394lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12395{
12396 uint16_t next_fcf_index;
12397
12398 /* Search from the currently registered FCF index */
12399 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12400 LPFC_SLI4_FCF_TBL_INDX_MAX,
12401 phba->fcf.current_rec.fcf_indx);
12402 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12403 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12404 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12405 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12406 /* Round robin failover stop condition */
12407 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12408 return LPFC_FCOE_FCF_NEXT_NONE;
12409
12410 return next_fcf_index;
12411}
12412
12413/**
12414 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12415 * @phba: pointer to lpfc hba data structure.
12416 *
12417 * This routine sets the FCF record index in to the eligible bmask for
12418 * round robin failover search. It checks to make sure that the index
12419 * does not go beyond the range of the driver allocated bmask dimension
12420 * before setting the bit.
12421 *
12422 * Returns 0 if the index bit successfully set, otherwise, it returns
12423 * -EINVAL.
12424 **/
12425int
12426lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12427{
12428 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12429 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12430 "2610 HBA FCF index reached driver's "
12431 "book keeping dimension: fcf_index:%d, "
12432 "driver_bmask_max:%d\n",
12433 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12434 return -EINVAL;
12435 }
12436 /* Set the eligible FCF record index bmask */
12437 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12438
12439 return 0;
12440}
12441
12442/**
12443 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12444 * @phba: pointer to lpfc hba data structure.
12445 *
12446 * This routine clears the FCF record index from the eligible bmask for
12447 * round robin failover search. It checks to make sure that the index
12448 * does not go beyond the range of the driver allocated bmask dimension
12449 * before clearing the bit.
12450 **/
12451void
12452lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12453{
12454 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12455 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12456 "2762 HBA FCF index goes beyond driver's "
12457 "book keeping dimension: fcf_index:%d, "
12458 "driver_bmask_max:%d\n",
12459 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12460 return;
12461 }
12462 /* Clear the eligible FCF record index bmask */
12463 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12464}
12465
12466/**
James Smartecfd03c2010-02-12 14:41:27 -050012467 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12468 * @phba: pointer to lpfc hba data structure.
12469 *
12470 * This routine is the completion routine for the rediscover FCF table mailbox
12471 * command. If the mailbox command returned failure, it will try to stop the
12472 * FCF rediscover wait timer.
12473 **/
12474void
12475lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12476{
12477 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12478 uint32_t shdr_status, shdr_add_status;
12479
12480 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12481
12482 shdr_status = bf_get(lpfc_mbox_hdr_status,
12483 &redisc_fcf->header.cfg_shdr.response);
12484 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12485 &redisc_fcf->header.cfg_shdr.response);
12486 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012487 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012488 "2746 Requesting for FCF rediscovery failed "
12489 "status x%x add_status x%x\n",
12490 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012491 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012492 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012493 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012494 spin_unlock_irq(&phba->hbalock);
12495 /*
12496 * CVL event triggered FCF rediscover request failed,
12497 * last resort to re-try current registered FCF entry.
12498 */
12499 lpfc_retry_pport_discovery(phba);
12500 } else {
12501 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012502 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012503 spin_unlock_irq(&phba->hbalock);
12504 /*
12505 * DEAD FCF event triggered FCF rediscover request
12506 * failed, last resort to fail over as a link down
12507 * to FCF registration.
12508 */
12509 lpfc_sli4_fcf_dead_failthrough(phba);
12510 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012511 } else {
12512 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12513 "2775 Start FCF rediscovery quiescent period "
12514 "wait timer before scaning FCF table\n");
James Smartecfd03c2010-02-12 14:41:27 -050012515 /*
12516 * Start FCF rediscovery wait timer for pending FCF
12517 * before rescan FCF record table.
12518 */
12519 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012520 }
James Smartecfd03c2010-02-12 14:41:27 -050012521
12522 mempool_free(mbox, phba->mbox_mem_pool);
12523}
12524
12525/**
12526 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12527 * @phba: pointer to lpfc hba data structure.
12528 *
12529 * This routine is invoked to request for rediscovery of the entire FCF table
12530 * by the port.
12531 **/
12532int
12533lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12534{
12535 LPFC_MBOXQ_t *mbox;
12536 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12537 int rc, length;
12538
James Smart0c9ab6f2010-02-26 14:15:57 -050012539 /* Cancel retry delay timers to all vports before FCF rediscover */
12540 lpfc_cancel_all_vport_retry_delay_timer(phba);
12541
James Smartecfd03c2010-02-12 14:41:27 -050012542 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12543 if (!mbox) {
12544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12545 "2745 Failed to allocate mbox for "
12546 "requesting FCF rediscover.\n");
12547 return -ENOMEM;
12548 }
12549
12550 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12551 sizeof(struct lpfc_sli4_cfg_mhdr));
12552 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12553 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12554 length, LPFC_SLI4_MBX_EMBED);
12555
12556 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12557 /* Set count to 0 for invalidating the entire FCF database */
12558 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12559
12560 /* Issue the mailbox command asynchronously */
12561 mbox->vport = phba->pport;
12562 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12563 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12564
12565 if (rc == MBX_NOT_FINISHED) {
12566 mempool_free(mbox, phba->mbox_mem_pool);
12567 return -EIO;
12568 }
12569 return 0;
12570}
12571
12572/**
James Smartfc2b9892010-02-26 14:15:29 -050012573 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12574 * @phba: pointer to lpfc hba data structure.
12575 *
12576 * This function is the failover routine as a last resort to the FCF DEAD
12577 * event when driver failed to perform fast FCF failover.
12578 **/
12579void
12580lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12581{
12582 uint32_t link_state;
12583
12584 /*
12585 * Last resort as FCF DEAD event failover will treat this as
12586 * a link down, but save the link state because we don't want
12587 * it to be changed to Link Down unless it is already down.
12588 */
12589 link_state = phba->link_state;
12590 lpfc_linkdown(phba);
12591 phba->link_state = link_state;
12592
12593 /* Unregister FCF if no devices connected to it */
12594 lpfc_unregister_unused_fcf(phba);
12595}
12596
12597/**
James Smarta0c87cb2009-07-19 10:01:10 -040012598 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12599 * @phba: pointer to lpfc hba data structure.
12600 *
12601 * This function read region 23 and parse TLV for port status to
12602 * decide if the user disaled the port. If the TLV indicates the
12603 * port is disabled, the hba_flag is set accordingly.
12604 **/
12605void
12606lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12607{
12608 LPFC_MBOXQ_t *pmb = NULL;
12609 MAILBOX_t *mb;
12610 uint8_t *rgn23_data = NULL;
12611 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12612 int rc;
12613
12614 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12615 if (!pmb) {
12616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12617 "2600 lpfc_sli_read_serdes_param failed to"
12618 " allocate mailbox memory\n");
12619 goto out;
12620 }
12621 mb = &pmb->u.mb;
12622
12623 /* Get adapter Region 23 data */
12624 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12625 if (!rgn23_data)
12626 goto out;
12627
12628 do {
12629 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12630 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12631
12632 if (rc != MBX_SUCCESS) {
12633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12634 "2601 lpfc_sli_read_link_ste failed to"
12635 " read config region 23 rc 0x%x Status 0x%x\n",
12636 rc, mb->mbxStatus);
12637 mb->un.varDmp.word_cnt = 0;
12638 }
12639 /*
12640 * dump mem may return a zero when finished or we got a
12641 * mailbox error, either way we are done.
12642 */
12643 if (mb->un.varDmp.word_cnt == 0)
12644 break;
12645 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12646 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12647
12648 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12649 rgn23_data + offset,
12650 mb->un.varDmp.word_cnt);
12651 offset += mb->un.varDmp.word_cnt;
12652 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12653
12654 data_size = offset;
12655 offset = 0;
12656
12657 if (!data_size)
12658 goto out;
12659
12660 /* Check the region signature first */
12661 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12663 "2619 Config region 23 has bad signature\n");
12664 goto out;
12665 }
12666 offset += 4;
12667
12668 /* Check the data structure version */
12669 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12671 "2620 Config region 23 has bad version\n");
12672 goto out;
12673 }
12674 offset += 4;
12675
12676 /* Parse TLV entries in the region */
12677 while (offset < data_size) {
12678 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12679 break;
12680 /*
12681 * If the TLV is not driver specific TLV or driver id is
12682 * not linux driver id, skip the record.
12683 */
12684 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12685 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12686 (rgn23_data[offset + 3] != 0)) {
12687 offset += rgn23_data[offset + 1] * 4 + 4;
12688 continue;
12689 }
12690
12691 /* Driver found a driver specific TLV in the config region */
12692 sub_tlv_len = rgn23_data[offset + 1] * 4;
12693 offset += 4;
12694 tlv_offset = 0;
12695
12696 /*
12697 * Search for configured port state sub-TLV.
12698 */
12699 while ((offset < data_size) &&
12700 (tlv_offset < sub_tlv_len)) {
12701 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12702 offset += 4;
12703 tlv_offset += 4;
12704 break;
12705 }
12706 if (rgn23_data[offset] != PORT_STE_TYPE) {
12707 offset += rgn23_data[offset + 1] * 4 + 4;
12708 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12709 continue;
12710 }
12711
12712 /* This HBA contains PORT_STE configured */
12713 if (!rgn23_data[offset + 2])
12714 phba->hba_flag |= LINK_DISABLED;
12715
12716 goto out;
12717 }
12718 }
12719out:
12720 if (pmb)
12721 mempool_free(pmb, phba->mbox_mem_pool);
12722 kfree(rgn23_data);
12723 return;
12724}
James Smart695a8142010-01-26 23:08:03 -050012725
12726/**
12727 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12728 * @vport: pointer to vport data structure.
12729 *
12730 * This function iterate through the mailboxq and clean up all REG_LOGIN
12731 * and REG_VPI mailbox commands associated with the vport. This function
12732 * is called when driver want to restart discovery of the vport due to
12733 * a Clear Virtual Link event.
12734 **/
12735void
12736lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12737{
12738 struct lpfc_hba *phba = vport->phba;
12739 LPFC_MBOXQ_t *mb, *nextmb;
12740 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040012741 struct lpfc_nodelist *ndlp;
James Smart695a8142010-01-26 23:08:03 -050012742
12743 spin_lock_irq(&phba->hbalock);
12744 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12745 if (mb->vport != vport)
12746 continue;
12747
12748 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12749 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12750 continue;
12751
12752 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12753 mp = (struct lpfc_dmabuf *) (mb->context1);
12754 if (mp) {
12755 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12756 kfree(mp);
12757 }
James Smart78730cf2010-04-06 15:06:30 -040012758 ndlp = (struct lpfc_nodelist *) mb->context2;
12759 if (ndlp) {
12760 lpfc_nlp_put(ndlp);
12761 mb->context2 = NULL;
12762 }
James Smart695a8142010-01-26 23:08:03 -050012763 }
12764 list_del(&mb->list);
12765 mempool_free(mb, phba->mbox_mem_pool);
12766 }
12767 mb = phba->sli.mbox_active;
12768 if (mb && (mb->vport == vport)) {
12769 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12770 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12771 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart78730cf2010-04-06 15:06:30 -040012772 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12773 ndlp = (struct lpfc_nodelist *) mb->context2;
12774 if (ndlp) {
12775 lpfc_nlp_put(ndlp);
12776 mb->context2 = NULL;
12777 }
12778 /* Unregister the RPI when mailbox complete */
12779 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12780 }
James Smart695a8142010-01-26 23:08:03 -050012781 }
12782 spin_unlock_irq(&phba->hbalock);
12783}
12784
James Smart2a9bf3d2010-06-07 15:24:45 -040012785/**
12786 * lpfc_drain_txq - Drain the txq
12787 * @phba: Pointer to HBA context object.
12788 *
12789 * This function attempt to submit IOCBs on the txq
12790 * to the adapter. For SLI4 adapters, the txq contains
12791 * ELS IOCBs that have been deferred because the there
12792 * are no SGLs. This congestion can occur with large
12793 * vport counts during node discovery.
12794 **/
12795
12796uint32_t
12797lpfc_drain_txq(struct lpfc_hba *phba)
12798{
12799 LIST_HEAD(completions);
12800 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12801 struct lpfc_iocbq *piocbq = 0;
12802 unsigned long iflags = 0;
12803 char *fail_msg = NULL;
12804 struct lpfc_sglq *sglq;
12805 union lpfc_wqe wqe;
12806
12807 spin_lock_irqsave(&phba->hbalock, iflags);
12808 if (pring->txq_cnt > pring->txq_max)
12809 pring->txq_max = pring->txq_cnt;
12810
12811 spin_unlock_irqrestore(&phba->hbalock, iflags);
12812
12813 while (pring->txq_cnt) {
12814 spin_lock_irqsave(&phba->hbalock, iflags);
12815
12816 sglq = __lpfc_sli_get_sglq(phba);
12817 if (!sglq) {
12818 spin_unlock_irqrestore(&phba->hbalock, iflags);
12819 break;
12820 } else {
12821 piocbq = lpfc_sli_ringtx_get(phba, pring);
12822 if (!piocbq) {
12823 /* The txq_cnt out of sync. This should
12824 * never happen
12825 */
12826 sglq = __lpfc_clear_active_sglq(phba,
12827 sglq->sli4_xritag);
12828 spin_unlock_irqrestore(&phba->hbalock, iflags);
12829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12830 "2823 txq empty and txq_cnt is %d\n ",
12831 pring->txq_cnt);
12832 break;
12833 }
12834 }
12835
12836 /* The xri and iocb resources secured,
12837 * attempt to issue request
12838 */
12839 piocbq->sli4_xritag = sglq->sli4_xritag;
12840 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12841 fail_msg = "to convert bpl to sgl";
12842 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12843 fail_msg = "to convert iocb to wqe";
12844 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12845 fail_msg = " - Wq is full";
12846 else
12847 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12848
12849 if (fail_msg) {
12850 /* Failed means we can't issue and need to cancel */
12851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12852 "2822 IOCB failed %s iotag 0x%x "
12853 "xri 0x%x\n",
12854 fail_msg,
12855 piocbq->iotag, piocbq->sli4_xritag);
12856 list_add_tail(&piocbq->list, &completions);
12857 }
12858 spin_unlock_irqrestore(&phba->hbalock, iflags);
12859 }
12860
12861 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12862 phba->pport->work_port_events &= ~WORKER_SERVICE_TXQ;
12863 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12864
12865 /* Cancel all the IOCBs that cannot be issued */
12866 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12867 IOERR_SLI_ABORTED);
12868
12869 return pring->txq_cnt;
12870}