blob: 7ddf5268227126901fe4dcec627e3b32d9019be7 [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
James Smartd7c47992010-06-08 18:31:54 -04004239 memcpy(vpd, dmabuf->virt, *vpd_size);
4240
James Smartda0436e2009-05-22 14:51:39 -04004241 dma_free_coherent(&phba->pcidev->dev, dma_size,
4242 dmabuf->virt, dmabuf->phys);
4243 kfree(dmabuf);
4244 return 0;
4245}
4246
4247/**
4248 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4249 * @phba: pointer to lpfc hba data structure.
4250 *
4251 * This routine is called to explicitly arm the SLI4 device's completion and
4252 * event queues
4253 **/
4254static void
4255lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4256{
4257 uint8_t fcp_eqidx;
4258
4259 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4260 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004261 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4262 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4263 LPFC_QUEUE_REARM);
4264 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4265 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4266 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4267 LPFC_QUEUE_REARM);
4268}
4269
4270/**
4271 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4272 * @phba: Pointer to HBA context object.
4273 *
4274 * This function is the main SLI4 device intialization PCI function. This
4275 * function is called by the HBA intialization code, HBA reset code and
4276 * HBA error attention handler code. Caller is not required to hold any
4277 * locks.
4278 **/
4279int
4280lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4281{
4282 int rc;
4283 LPFC_MBOXQ_t *mboxq;
4284 struct lpfc_mqe *mqe;
4285 uint8_t *vpd;
4286 uint32_t vpd_size;
4287 uint32_t ftr_rsp = 0;
4288 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4289 struct lpfc_vport *vport = phba->pport;
4290 struct lpfc_dmabuf *mp;
4291
4292 /* Perform a PCI function reset to start from clean */
4293 rc = lpfc_pci_function_reset(phba);
4294 if (unlikely(rc))
4295 return -ENODEV;
4296
4297 /* Check the HBA Host Status Register for readyness */
4298 rc = lpfc_sli4_post_status_check(phba);
4299 if (unlikely(rc))
4300 return -ENODEV;
4301 else {
4302 spin_lock_irq(&phba->hbalock);
4303 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4304 spin_unlock_irq(&phba->hbalock);
4305 }
4306
4307 /*
4308 * Allocate a single mailbox container for initializing the
4309 * port.
4310 */
4311 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4312 if (!mboxq)
4313 return -ENOMEM;
4314
4315 /*
4316 * Continue initialization with default values even if driver failed
4317 * to read FCoE param config regions
4318 */
4319 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4320 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004321 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004322
4323 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004324 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004325 vpd = kzalloc(vpd_size, GFP_KERNEL);
4326 if (!vpd) {
4327 rc = -ENOMEM;
4328 goto out_free_mbox;
4329 }
4330
4331 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4332 if (unlikely(rc))
4333 goto out_free_vpd;
4334
4335 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004336 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4337 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4338 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004339
4340 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4341 LPFC_DCBX_CEE_MODE)
4342 phba->hba_flag |= HBA_FIP_SUPPORT;
4343 else
4344 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4345
James Smartf1126682009-06-10 17:22:44 -04004346 if (phba->sli_rev != LPFC_SLI_REV4 ||
4347 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4349 "0376 READ_REV Error. SLI Level %d "
4350 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004351 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004352 rc = -EIO;
4353 goto out_free_vpd;
4354 }
James Smartda0436e2009-05-22 14:51:39 -04004355 /*
4356 * Evaluate the read rev and vpd data. Populate the driver
4357 * state with the results. If this routine fails, the failure
4358 * is not fatal as the driver will use generic values.
4359 */
4360 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4361 if (unlikely(!rc)) {
4362 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4363 "0377 Error %d parsing vpd. "
4364 "Using defaults.\n", rc);
4365 rc = 0;
4366 }
4367
James Smartf1126682009-06-10 17:22:44 -04004368 /* Save information as VPD data */
4369 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4370 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4371 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4372 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4373 &mqe->un.read_rev);
4374 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4375 &mqe->un.read_rev);
4376 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4377 &mqe->un.read_rev);
4378 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4379 &mqe->un.read_rev);
4380 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4381 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4382 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4383 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4384 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4385 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4386 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4387 "(%d):0380 READ_REV Status x%x "
4388 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4389 mboxq->vport ? mboxq->vport->vpi : 0,
4390 bf_get(lpfc_mqe_status, mqe),
4391 phba->vpd.rev.opFwName,
4392 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4393 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004394
4395 /*
4396 * Discover the port's supported feature set and match it against the
4397 * hosts requests.
4398 */
4399 lpfc_request_features(phba, mboxq);
4400 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4401 if (unlikely(rc)) {
4402 rc = -EIO;
4403 goto out_free_vpd;
4404 }
4405
4406 /*
4407 * The port must support FCP initiator mode as this is the
4408 * only mode running in the host.
4409 */
4410 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4411 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4412 "0378 No support for fcpi mode.\n");
4413 ftr_rsp++;
4414 }
4415
4416 /*
4417 * If the port cannot support the host's requested features
4418 * then turn off the global config parameters to disable the
4419 * feature in the driver. This is not a fatal error.
4420 */
4421 if ((phba->cfg_enable_bg) &&
4422 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4423 ftr_rsp++;
4424
4425 if (phba->max_vpi && phba->cfg_enable_npiv &&
4426 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4427 ftr_rsp++;
4428
4429 if (ftr_rsp) {
4430 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4431 "0379 Feature Mismatch Data: x%08x %08x "
4432 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4433 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4434 phba->cfg_enable_npiv, phba->max_vpi);
4435 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4436 phba->cfg_enable_bg = 0;
4437 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4438 phba->cfg_enable_npiv = 0;
4439 }
4440
4441 /* These SLI3 features are assumed in SLI4 */
4442 spin_lock_irq(&phba->hbalock);
4443 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4444 spin_unlock_irq(&phba->hbalock);
4445
4446 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004447 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4448 if (rc) {
4449 phba->link_state = LPFC_HBA_ERROR;
4450 rc = -ENOMEM;
4451 goto out_free_vpd;
4452 }
4453
James Smartda0436e2009-05-22 14:51:39 -04004454 mboxq->vport = vport;
4455 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4456 mp = (struct lpfc_dmabuf *) mboxq->context1;
4457 if (rc == MBX_SUCCESS) {
4458 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4459 rc = 0;
4460 }
4461
4462 /*
4463 * This memory was allocated by the lpfc_read_sparam routine. Release
4464 * it to the mbuf pool.
4465 */
4466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4467 kfree(mp);
4468 mboxq->context1 = NULL;
4469 if (unlikely(rc)) {
4470 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4471 "0382 READ_SPARAM command failed "
4472 "status %d, mbxStatus x%x\n",
4473 rc, bf_get(lpfc_mqe_status, mqe));
4474 phba->link_state = LPFC_HBA_ERROR;
4475 rc = -EIO;
4476 goto out_free_vpd;
4477 }
4478
4479 if (phba->cfg_soft_wwnn)
4480 u64_to_wwn(phba->cfg_soft_wwnn,
4481 vport->fc_sparam.nodeName.u.wwn);
4482 if (phba->cfg_soft_wwpn)
4483 u64_to_wwn(phba->cfg_soft_wwpn,
4484 vport->fc_sparam.portName.u.wwn);
4485 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4486 sizeof(struct lpfc_name));
4487 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4488 sizeof(struct lpfc_name));
4489
4490 /* Update the fc_host data structures with new wwn. */
4491 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4492 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4493
4494 /* Register SGL pool to the device using non-embedded mailbox command */
4495 rc = lpfc_sli4_post_sgl_list(phba);
4496 if (unlikely(rc)) {
4497 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004498 "0582 Error %d during sgl post operation\n",
4499 rc);
James Smartda0436e2009-05-22 14:51:39 -04004500 rc = -ENODEV;
4501 goto out_free_vpd;
4502 }
4503
4504 /* Register SCSI SGL pool to the device */
4505 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4506 if (unlikely(rc)) {
4507 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004508 "0383 Error %d during scsi sgl post "
4509 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004510 /* Some Scsi buffers were moved to the abort scsi list */
4511 /* A pci function reset will repost them */
4512 rc = -ENODEV;
4513 goto out_free_vpd;
4514 }
4515
4516 /* Post the rpi header region to the device. */
4517 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4518 if (unlikely(rc)) {
4519 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4520 "0393 Error %d during rpi post operation\n",
4521 rc);
4522 rc = -ENODEV;
4523 goto out_free_vpd;
4524 }
James Smartda0436e2009-05-22 14:51:39 -04004525
4526 /* Set up all the queues to the device */
4527 rc = lpfc_sli4_queue_setup(phba);
4528 if (unlikely(rc)) {
4529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4530 "0381 Error %d during queue setup.\n ", rc);
4531 goto out_stop_timers;
4532 }
4533
4534 /* Arm the CQs and then EQs on device */
4535 lpfc_sli4_arm_cqeq_intr(phba);
4536
4537 /* Indicate device interrupt mode */
4538 phba->sli4_hba.intr_enable = 1;
4539
4540 /* Allow asynchronous mailbox command to go through */
4541 spin_lock_irq(&phba->hbalock);
4542 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4543 spin_unlock_irq(&phba->hbalock);
4544
4545 /* Post receive buffers to the device */
4546 lpfc_sli4_rb_setup(phba);
4547
James Smartfc2b9892010-02-26 14:15:29 -05004548 /* Reset HBA FCF states after HBA reset */
4549 phba->fcf.fcf_flag = 0;
4550 phba->fcf.current_rec.flag = 0;
4551
James Smartda0436e2009-05-22 14:51:39 -04004552 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004553 mod_timer(&vport->els_tmofunc,
4554 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004555
4556 /* Start heart beat timer */
4557 mod_timer(&phba->hb_tmofunc,
4558 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4559 phba->hb_outstanding = 0;
4560 phba->last_completion_time = jiffies;
4561
4562 /* Start error attention (ERATT) polling timer */
4563 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4564
James Smart75baf692010-06-08 18:31:21 -04004565 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4566 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4567 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4568 if (!rc) {
4569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570 "2829 This device supports "
4571 "Advanced Error Reporting (AER)\n");
4572 spin_lock_irq(&phba->hbalock);
4573 phba->hba_flag |= HBA_AER_ENABLED;
4574 spin_unlock_irq(&phba->hbalock);
4575 } else {
4576 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4577 "2830 This device does not support "
4578 "Advanced Error Reporting (AER)\n");
4579 phba->cfg_aer_support = 0;
4580 }
4581 }
4582
James Smartda0436e2009-05-22 14:51:39 -04004583 /*
4584 * The port is ready, set the host's link state to LINK_DOWN
4585 * in preparation for link interrupts.
4586 */
4587 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4588 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4589 lpfc_set_loopback_flag(phba);
4590 /* Change driver state to LPFC_LINK_DOWN right before init link */
4591 spin_lock_irq(&phba->hbalock);
4592 phba->link_state = LPFC_LINK_DOWN;
4593 spin_unlock_irq(&phba->hbalock);
4594 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4595 if (unlikely(rc != MBX_NOT_FINISHED)) {
4596 kfree(vpd);
4597 return 0;
4598 } else
4599 rc = -EIO;
4600
4601 /* Unset all the queues set up in this routine when error out */
4602 if (rc)
4603 lpfc_sli4_queue_unset(phba);
4604
4605out_stop_timers:
4606 if (rc)
4607 lpfc_stop_hba_timers(phba);
4608out_free_vpd:
4609 kfree(vpd);
4610out_free_mbox:
4611 mempool_free(mboxq, phba->mbox_mem_pool);
4612 return rc;
4613}
James Smarte59058c2008-08-24 21:49:00 -04004614
4615/**
James Smart3621a712009-04-06 18:47:14 -04004616 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004617 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004618 *
James Smarte59058c2008-08-24 21:49:00 -04004619 * This is the callback function for mailbox timer. The mailbox
4620 * timer is armed when a new mailbox command is issued and the timer
4621 * is deleted when the mailbox complete. The function is called by
4622 * the kernel timer code when a mailbox does not complete within
4623 * expected time. This function wakes up the worker thread to
4624 * process the mailbox timeout and returns. All the processing is
4625 * done by the worker thread function lpfc_mbox_timeout_handler.
4626 **/
dea31012005-04-17 16:05:31 -05004627void
4628lpfc_mbox_timeout(unsigned long ptr)
4629{
James Smart92d7f7b2007-06-17 19:56:38 -05004630 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004631 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004632 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004633
James Smart2e0fef82007-06-17 19:56:36 -05004634 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004635 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004636 if (!tmo_posted)
4637 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4638 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4639
James Smart5e9d9b82008-06-14 22:52:53 -04004640 if (!tmo_posted)
4641 lpfc_worker_wake_up(phba);
4642 return;
dea31012005-04-17 16:05:31 -05004643}
4644
James Smarte59058c2008-08-24 21:49:00 -04004645
4646/**
James Smart3621a712009-04-06 18:47:14 -04004647 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004648 * @phba: Pointer to HBA context object.
4649 *
4650 * This function is called from worker thread when a mailbox command times out.
4651 * The caller is not required to hold any locks. This function will reset the
4652 * HBA and recover all the pending commands.
4653 **/
dea31012005-04-17 16:05:31 -05004654void
4655lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4656{
James Smart2e0fef82007-06-17 19:56:36 -05004657 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004658 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004659 struct lpfc_sli *psli = &phba->sli;
4660 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004661
James Smarta257bf92009-04-06 18:48:10 -04004662 /* Check the pmbox pointer first. There is a race condition
4663 * between the mbox timeout handler getting executed in the
4664 * worklist and the mailbox actually completing. When this
4665 * race condition occurs, the mbox_active will be NULL.
4666 */
4667 spin_lock_irq(&phba->hbalock);
4668 if (pmbox == NULL) {
4669 lpfc_printf_log(phba, KERN_WARNING,
4670 LOG_MBOX | LOG_SLI,
4671 "0353 Active Mailbox cleared - mailbox timeout "
4672 "exiting\n");
4673 spin_unlock_irq(&phba->hbalock);
4674 return;
4675 }
4676
dea31012005-04-17 16:05:31 -05004677 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004679 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004680 mb->mbxCommand,
4681 phba->pport->port_state,
4682 phba->sli.sli_flag,
4683 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004684 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004685
James Smart1dcb58e2007-04-25 09:51:30 -04004686 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4687 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4688 * it to fail all oustanding SCSI IO.
4689 */
James Smart2e0fef82007-06-17 19:56:36 -05004690 spin_lock_irq(&phba->pport->work_port_lock);
4691 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4692 spin_unlock_irq(&phba->pport->work_port_lock);
4693 spin_lock_irq(&phba->hbalock);
4694 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004695 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004696 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004697
4698 pring = &psli->ring[psli->fcp_ring];
4699 lpfc_sli_abort_iocb_ring(phba, pring);
4700
4701 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004702 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004703
4704 /* Reset the HBA device */
4705 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004706}
4707
James Smarte59058c2008-08-24 21:49:00 -04004708/**
James Smart3772a992009-05-22 14:50:54 -04004709 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004710 * @phba: Pointer to HBA context object.
4711 * @pmbox: Pointer to mailbox object.
4712 * @flag: Flag indicating how the mailbox need to be processed.
4713 *
4714 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004715 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4716 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004717 * The mailbox command can be submitted in polling mode, in which case
4718 * this function will wait in a polling loop for the completion of the
4719 * mailbox.
4720 * If the mailbox is submitted in no_wait mode (not polling) the
4721 * function will submit the command and returns immediately without waiting
4722 * for the mailbox completion. The no_wait is supported only when HBA
4723 * is in SLI2/SLI3 mode - interrupts are enabled.
4724 * The SLI interface allows only one mailbox pending at a time. If the
4725 * mailbox is issued in polling mode and there is already a mailbox
4726 * pending, then the function will return an error. If the mailbox is issued
4727 * in NO_WAIT mode and there is a mailbox pending already, the function
4728 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4729 * The sli layer owns the mailbox object until the completion of mailbox
4730 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4731 * return codes the caller owns the mailbox command after the return of
4732 * the function.
4733 **/
James Smart3772a992009-05-22 14:50:54 -04004734static int
4735lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4736 uint32_t flag)
dea31012005-04-17 16:05:31 -05004737{
dea31012005-04-17 16:05:31 -05004738 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004739 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004740 uint32_t status, evtctr;
4741 uint32_t ha_copy;
4742 int i;
James Smart09372822008-01-11 01:52:54 -05004743 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004744 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004745 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004746 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004747 int processing_queue = 0;
4748
4749 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4750 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004751 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004752 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004753 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4754 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4755 return MBX_SUCCESS;
4756 }
James Smart58da1ff2008-04-07 10:15:56 -04004757 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004758 pmbox = lpfc_mbox_get(phba);
4759 if (!pmbox) {
4760 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4761 return MBX_SUCCESS;
4762 }
4763 }
dea31012005-04-17 16:05:31 -05004764
James Smarted957682007-06-17 19:56:37 -05004765 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004766 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004767 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004768 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004769 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004770 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004771 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004772 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004773 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004774 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004775 }
4776 }
4777
Linas Vepstas8d63f372007-02-14 14:28:36 -06004778 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004779 if (unlikely(pci_channel_offline(phba->pcidev))) {
4780 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4781 goto out_not_finished;
4782 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004783
James Smarta257bf92009-04-06 18:48:10 -04004784 /* If HBA has a deferred error attention, fail the iocb. */
4785 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4787 goto out_not_finished;
4788 }
4789
dea31012005-04-17 16:05:31 -05004790 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004791
James Smart3772a992009-05-22 14:50:54 -04004792 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004793 status = MBX_SUCCESS;
4794
James Smart2e0fef82007-06-17 19:56:36 -05004795 if (phba->link_state == LPFC_HBA_ERROR) {
4796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004797
4798 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004799 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4800 "(%d):0311 Mailbox command x%x cannot "
4801 "issue Data: x%x x%x\n",
4802 pmbox->vport ? pmbox->vport->vpi : 0,
4803 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004804 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004805 }
4806
James Smart92908312006-03-07 15:04:13 -05004807 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4808 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004809 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004810 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4811 "(%d):2528 Mailbox command x%x cannot "
4812 "issue Data: x%x x%x\n",
4813 pmbox->vport ? pmbox->vport->vpi : 0,
4814 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004815 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004816 }
4817
dea31012005-04-17 16:05:31 -05004818 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4819 /* Polling for a mbox command when another one is already active
4820 * is not allowed in SLI. Also, the driver must have established
4821 * SLI2 mode to queue and process multiple mbox commands.
4822 */
4823
4824 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004825 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004826
4827 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004828 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4829 "(%d):2529 Mailbox command x%x "
4830 "cannot issue Data: x%x x%x\n",
4831 pmbox->vport ? pmbox->vport->vpi : 0,
4832 pmbox->u.mb.mbxCommand,
4833 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004834 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004835 }
4836
James Smart3772a992009-05-22 14:50:54 -04004837 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004838 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004839 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004840 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4841 "(%d):2530 Mailbox command x%x "
4842 "cannot issue Data: x%x x%x\n",
4843 pmbox->vport ? pmbox->vport->vpi : 0,
4844 pmbox->u.mb.mbxCommand,
4845 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004846 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004847 }
4848
dea31012005-04-17 16:05:31 -05004849 /* Another mailbox command is still being processed, queue this
4850 * command to be processed later.
4851 */
4852 lpfc_mbox_put(phba, pmbox);
4853
4854 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004855 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004856 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004857 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004858 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4859 mb->mbxCommand, phba->pport->port_state,
4860 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004861
4862 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004863 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004864
James Smart858c9f62007-06-17 19:56:39 -05004865 if (pmbox->vport) {
4866 lpfc_debugfs_disc_trc(pmbox->vport,
4867 LPFC_DISC_TRC_MBOX_VPORT,
4868 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4869 (uint32_t)mb->mbxCommand,
4870 mb->un.varWords[0], mb->un.varWords[1]);
4871 }
4872 else {
4873 lpfc_debugfs_disc_trc(phba->pport,
4874 LPFC_DISC_TRC_MBOX,
4875 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4876 (uint32_t)mb->mbxCommand,
4877 mb->un.varWords[0], mb->un.varWords[1]);
4878 }
4879
James Smart2e0fef82007-06-17 19:56:36 -05004880 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004881 }
4882
dea31012005-04-17 16:05:31 -05004883 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4884
4885 /* If we are not polling, we MUST be in SLI2 mode */
4886 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004887 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004888 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004889 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004890 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004891 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4893 "(%d):2531 Mailbox command x%x "
4894 "cannot issue Data: x%x x%x\n",
4895 pmbox->vport ? pmbox->vport->vpi : 0,
4896 pmbox->u.mb.mbxCommand,
4897 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004898 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004899 }
4900 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004901 mod_timer(&psli->mbox_tmo, (jiffies +
4902 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004903 }
4904
4905 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004906 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004907 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004908 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004909 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004910 mb->mbxCommand, phba->pport->port_state,
4911 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004912
James Smart858c9f62007-06-17 19:56:39 -05004913 if (mb->mbxCommand != MBX_HEARTBEAT) {
4914 if (pmbox->vport) {
4915 lpfc_debugfs_disc_trc(pmbox->vport,
4916 LPFC_DISC_TRC_MBOX_VPORT,
4917 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4918 (uint32_t)mb->mbxCommand,
4919 mb->un.varWords[0], mb->un.varWords[1]);
4920 }
4921 else {
4922 lpfc_debugfs_disc_trc(phba->pport,
4923 LPFC_DISC_TRC_MBOX,
4924 "MBOX Send: cmd:x%x mb:x%x x%x",
4925 (uint32_t)mb->mbxCommand,
4926 mb->un.varWords[0], mb->un.varWords[1]);
4927 }
4928 }
4929
dea31012005-04-17 16:05:31 -05004930 psli->slistat.mbox_cmd++;
4931 evtctr = psli->slistat.mbox_event;
4932
4933 /* next set own bit for the adapter and copy over command word */
4934 mb->mbxOwner = OWN_CHIP;
4935
James Smart3772a992009-05-22 14:50:54 -04004936 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004937 /* Populate mbox extension offset word. */
4938 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4939 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4940 = (uint8_t *)phba->mbox_ext
4941 - (uint8_t *)phba->mbox;
4942 }
4943
4944 /* Copy the mailbox extension data */
4945 if (pmbox->in_ext_byte_len && pmbox->context2) {
4946 lpfc_sli_pcimem_bcopy(pmbox->context2,
4947 (uint8_t *)phba->mbox_ext,
4948 pmbox->in_ext_byte_len);
4949 }
4950 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004951 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004952 } else {
James Smart7a470272010-03-15 11:25:20 -04004953 /* Populate mbox extension offset word. */
4954 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4955 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4956 = MAILBOX_HBA_EXT_OFFSET;
4957
4958 /* Copy the mailbox extension data */
4959 if (pmbox->in_ext_byte_len && pmbox->context2) {
4960 lpfc_memcpy_to_slim(phba->MBslimaddr +
4961 MAILBOX_HBA_EXT_OFFSET,
4962 pmbox->context2, pmbox->in_ext_byte_len);
4963
4964 }
James Smart92908312006-03-07 15:04:13 -05004965 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004966 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004967 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004968 }
4969
4970 /* First copy mbox command data to HBA SLIM, skip past first
4971 word */
4972 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4973 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4974 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4975
4976 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004977 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004978 to_slim = phba->MBslimaddr;
4979 writel(ldata, to_slim);
4980 readl(to_slim); /* flush */
4981
4982 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4983 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004984 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004985 }
4986 }
4987
4988 wmb();
dea31012005-04-17 16:05:31 -05004989
4990 switch (flag) {
4991 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004992 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004993 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004994 /* Interrupt board to do it */
4995 writel(CA_MBATT, phba->CAregaddr);
4996 readl(phba->CAregaddr); /* flush */
4997 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004998 break;
4999
5000 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005001 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005002 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005003 /* Interrupt board to do it */
5004 writel(CA_MBATT, phba->CAregaddr);
5005 readl(phba->CAregaddr); /* flush */
5006
James Smart3772a992009-05-22 14:50:54 -04005007 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005008 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005009 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005010 word0 = le32_to_cpu(word0);
5011 } else {
5012 /* First read mbox status word */
5013 word0 = readl(phba->MBslimaddr);
5014 }
5015
5016 /* Read the HBA Host Attention Register */
5017 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005018 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5019 mb->mbxCommand) *
5020 1000) + jiffies;
5021 i = 0;
dea31012005-04-17 16:05:31 -05005022 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005023 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5024 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005025 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005026 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005027 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005028 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005029 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005030 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005031 }
5032
5033 /* Check if we took a mbox interrupt while we were
5034 polling */
5035 if (((word0 & OWN_CHIP) != OWN_CHIP)
5036 && (evtctr != psli->slistat.mbox_event))
5037 break;
5038
James Smart09372822008-01-11 01:52:54 -05005039 if (i++ > 10) {
5040 spin_unlock_irqrestore(&phba->hbalock,
5041 drvr_flag);
5042 msleep(1);
5043 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5044 }
dea31012005-04-17 16:05:31 -05005045
James Smart3772a992009-05-22 14:50:54 -04005046 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005047 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005048 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005049 word0 = le32_to_cpu(word0);
5050 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5051 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005052 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005053 /* Check real SLIM for any errors */
5054 slimword0 = readl(phba->MBslimaddr);
5055 slimmb = (MAILBOX_t *) & slimword0;
5056 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5057 && slimmb->mbxStatus) {
5058 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005059 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005060 word0 = slimword0;
5061 }
5062 }
5063 } else {
5064 /* First copy command data */
5065 word0 = readl(phba->MBslimaddr);
5066 }
5067 /* Read the HBA Host Attention Register */
5068 ha_copy = readl(phba->HAregaddr);
5069 }
5070
James Smart3772a992009-05-22 14:50:54 -04005071 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005072 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005073 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005074 /* Copy the mailbox extension data */
5075 if (pmbox->out_ext_byte_len && pmbox->context2) {
5076 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5077 pmbox->context2,
5078 pmbox->out_ext_byte_len);
5079 }
dea31012005-04-17 16:05:31 -05005080 } else {
5081 /* First copy command data */
5082 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5083 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005084 /* Copy the mailbox extension data */
5085 if (pmbox->out_ext_byte_len && pmbox->context2) {
5086 lpfc_memcpy_from_slim(pmbox->context2,
5087 phba->MBslimaddr +
5088 MAILBOX_HBA_EXT_OFFSET,
5089 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005090 }
5091 }
5092
5093 writel(HA_MBATT, phba->HAregaddr);
5094 readl(phba->HAregaddr); /* flush */
5095
5096 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5097 status = mb->mbxStatus;
5098 }
5099
James Smart2e0fef82007-06-17 19:56:36 -05005100 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5101 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005102
5103out_not_finished:
5104 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005105 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005106 lpfc_mbox_cmpl_put(phba, pmbox);
5107 }
5108 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005109}
5110
James Smarte59058c2008-08-24 21:49:00 -04005111/**
James Smartf1126682009-06-10 17:22:44 -04005112 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5113 * @phba: Pointer to HBA context object.
5114 *
5115 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5116 * the driver internal pending mailbox queue. It will then try to wait out the
5117 * possible outstanding mailbox command before return.
5118 *
5119 * Returns:
5120 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5121 * the outstanding mailbox command timed out.
5122 **/
5123static int
5124lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5125{
5126 struct lpfc_sli *psli = &phba->sli;
5127 uint8_t actcmd = MBX_HEARTBEAT;
5128 int rc = 0;
5129 unsigned long timeout;
5130
5131 /* Mark the asynchronous mailbox command posting as blocked */
5132 spin_lock_irq(&phba->hbalock);
5133 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5134 if (phba->sli.mbox_active)
5135 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5136 spin_unlock_irq(&phba->hbalock);
5137 /* Determine how long we might wait for the active mailbox
5138 * command to be gracefully completed by firmware.
5139 */
5140 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5141 jiffies;
5142 /* Wait for the outstnading mailbox command to complete */
5143 while (phba->sli.mbox_active) {
5144 /* Check active mailbox complete status every 2ms */
5145 msleep(2);
5146 if (time_after(jiffies, timeout)) {
5147 /* Timeout, marked the outstanding cmd not complete */
5148 rc = 1;
5149 break;
5150 }
5151 }
5152
5153 /* Can not cleanly block async mailbox command, fails it */
5154 if (rc) {
5155 spin_lock_irq(&phba->hbalock);
5156 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5157 spin_unlock_irq(&phba->hbalock);
5158 }
5159 return rc;
5160}
5161
5162/**
5163 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5164 * @phba: Pointer to HBA context object.
5165 *
5166 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5167 * commands from the driver internal pending mailbox queue. It makes sure
5168 * that there is no outstanding mailbox command before resuming posting
5169 * asynchronous mailbox commands. If, for any reason, there is outstanding
5170 * mailbox command, it will try to wait it out before resuming asynchronous
5171 * mailbox command posting.
5172 **/
5173static void
5174lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5175{
5176 struct lpfc_sli *psli = &phba->sli;
5177
5178 spin_lock_irq(&phba->hbalock);
5179 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5180 /* Asynchronous mailbox posting is not blocked, do nothing */
5181 spin_unlock_irq(&phba->hbalock);
5182 return;
5183 }
5184
5185 /* Outstanding synchronous mailbox command is guaranteed to be done,
5186 * successful or timeout, after timing-out the outstanding mailbox
5187 * command shall always be removed, so just unblock posting async
5188 * mailbox command and resume
5189 */
5190 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5191 spin_unlock_irq(&phba->hbalock);
5192
5193 /* wake up worker thread to post asynchronlous mailbox command */
5194 lpfc_worker_wake_up(phba);
5195}
5196
5197/**
James Smartda0436e2009-05-22 14:51:39 -04005198 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5199 * @phba: Pointer to HBA context object.
5200 * @mboxq: Pointer to mailbox object.
5201 *
5202 * The function posts a mailbox to the port. The mailbox is expected
5203 * to be comletely filled in and ready for the port to operate on it.
5204 * This routine executes a synchronous completion operation on the
5205 * mailbox by polling for its completion.
5206 *
5207 * The caller must not be holding any locks when calling this routine.
5208 *
5209 * Returns:
5210 * MBX_SUCCESS - mailbox posted successfully
5211 * Any of the MBX error values.
5212 **/
5213static int
5214lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5215{
5216 int rc = MBX_SUCCESS;
5217 unsigned long iflag;
5218 uint32_t db_ready;
5219 uint32_t mcqe_status;
5220 uint32_t mbx_cmnd;
5221 unsigned long timeout;
5222 struct lpfc_sli *psli = &phba->sli;
5223 struct lpfc_mqe *mb = &mboxq->u.mqe;
5224 struct lpfc_bmbx_create *mbox_rgn;
5225 struct dma_address *dma_address;
5226 struct lpfc_register bmbx_reg;
5227
5228 /*
5229 * Only one mailbox can be active to the bootstrap mailbox region
5230 * at a time and there is no queueing provided.
5231 */
5232 spin_lock_irqsave(&phba->hbalock, iflag);
5233 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5234 spin_unlock_irqrestore(&phba->hbalock, iflag);
5235 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5236 "(%d):2532 Mailbox command x%x (x%x) "
5237 "cannot issue Data: x%x x%x\n",
5238 mboxq->vport ? mboxq->vport->vpi : 0,
5239 mboxq->u.mb.mbxCommand,
5240 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5241 psli->sli_flag, MBX_POLL);
5242 return MBXERR_ERROR;
5243 }
5244 /* The server grabs the token and owns it until release */
5245 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5246 phba->sli.mbox_active = mboxq;
5247 spin_unlock_irqrestore(&phba->hbalock, iflag);
5248
5249 /*
5250 * Initialize the bootstrap memory region to avoid stale data areas
5251 * in the mailbox post. Then copy the caller's mailbox contents to
5252 * the bmbx mailbox region.
5253 */
5254 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5255 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5256 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5257 sizeof(struct lpfc_mqe));
5258
5259 /* Post the high mailbox dma address to the port and wait for ready. */
5260 dma_address = &phba->sli4_hba.bmbx.dma_address;
5261 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5262
5263 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5264 * 1000) + jiffies;
5265 do {
5266 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5267 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5268 if (!db_ready)
5269 msleep(2);
5270
5271 if (time_after(jiffies, timeout)) {
5272 rc = MBXERR_ERROR;
5273 goto exit;
5274 }
5275 } while (!db_ready);
5276
5277 /* Post the low mailbox dma address to the port. */
5278 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5279 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5280 * 1000) + jiffies;
5281 do {
5282 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5283 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5284 if (!db_ready)
5285 msleep(2);
5286
5287 if (time_after(jiffies, timeout)) {
5288 rc = MBXERR_ERROR;
5289 goto exit;
5290 }
5291 } while (!db_ready);
5292
5293 /*
5294 * Read the CQ to ensure the mailbox has completed.
5295 * If so, update the mailbox status so that the upper layers
5296 * can complete the request normally.
5297 */
5298 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5299 sizeof(struct lpfc_mqe));
5300 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5301 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5302 sizeof(struct lpfc_mcqe));
5303 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5304
5305 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5306 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5307 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5308 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005309 } else
5310 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005311
5312 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5313 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5314 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5315 " x%x x%x CQ: x%x x%x x%x x%x\n",
5316 mboxq->vport ? mboxq->vport->vpi : 0,
5317 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5318 bf_get(lpfc_mqe_status, mb),
5319 mb->un.mb_words[0], mb->un.mb_words[1],
5320 mb->un.mb_words[2], mb->un.mb_words[3],
5321 mb->un.mb_words[4], mb->un.mb_words[5],
5322 mb->un.mb_words[6], mb->un.mb_words[7],
5323 mb->un.mb_words[8], mb->un.mb_words[9],
5324 mb->un.mb_words[10], mb->un.mb_words[11],
5325 mb->un.mb_words[12], mboxq->mcqe.word0,
5326 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5327 mboxq->mcqe.trailer);
5328exit:
5329 /* We are holding the token, no needed for lock when release */
5330 spin_lock_irqsave(&phba->hbalock, iflag);
5331 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5332 phba->sli.mbox_active = NULL;
5333 spin_unlock_irqrestore(&phba->hbalock, iflag);
5334 return rc;
5335}
5336
5337/**
5338 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5339 * @phba: Pointer to HBA context object.
5340 * @pmbox: Pointer to mailbox object.
5341 * @flag: Flag indicating how the mailbox need to be processed.
5342 *
5343 * This function is called by discovery code and HBA management code to submit
5344 * a mailbox command to firmware with SLI-4 interface spec.
5345 *
5346 * Return codes the caller owns the mailbox command after the return of the
5347 * function.
5348 **/
5349static int
5350lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5351 uint32_t flag)
5352{
5353 struct lpfc_sli *psli = &phba->sli;
5354 unsigned long iflags;
5355 int rc;
5356
James Smart8fa38512009-07-19 10:01:03 -04005357 rc = lpfc_mbox_dev_check(phba);
5358 if (unlikely(rc)) {
5359 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5360 "(%d):2544 Mailbox command x%x (x%x) "
5361 "cannot issue Data: x%x x%x\n",
5362 mboxq->vport ? mboxq->vport->vpi : 0,
5363 mboxq->u.mb.mbxCommand,
5364 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5365 psli->sli_flag, flag);
5366 goto out_not_finished;
5367 }
5368
James Smartda0436e2009-05-22 14:51:39 -04005369 /* Detect polling mode and jump to a handler */
5370 if (!phba->sli4_hba.intr_enable) {
5371 if (flag == MBX_POLL)
5372 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5373 else
5374 rc = -EIO;
5375 if (rc != MBX_SUCCESS)
5376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5377 "(%d):2541 Mailbox command x%x "
5378 "(x%x) cannot issue Data: x%x x%x\n",
5379 mboxq->vport ? mboxq->vport->vpi : 0,
5380 mboxq->u.mb.mbxCommand,
5381 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5382 psli->sli_flag, flag);
5383 return rc;
5384 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005385 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5386 "(%d):2542 Try to issue mailbox command "
5387 "x%x (x%x) synchronously ahead of async"
5388 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005389 mboxq->vport ? mboxq->vport->vpi : 0,
5390 mboxq->u.mb.mbxCommand,
5391 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5392 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005393 /* Try to block the asynchronous mailbox posting */
5394 rc = lpfc_sli4_async_mbox_block(phba);
5395 if (!rc) {
5396 /* Successfully blocked, now issue sync mbox cmd */
5397 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5398 if (rc != MBX_SUCCESS)
5399 lpfc_printf_log(phba, KERN_ERR,
5400 LOG_MBOX | LOG_SLI,
5401 "(%d):2597 Mailbox command "
5402 "x%x (x%x) cannot issue "
5403 "Data: x%x x%x\n",
5404 mboxq->vport ?
5405 mboxq->vport->vpi : 0,
5406 mboxq->u.mb.mbxCommand,
5407 lpfc_sli4_mbox_opcode_get(phba,
5408 mboxq),
5409 psli->sli_flag, flag);
5410 /* Unblock the async mailbox posting afterward */
5411 lpfc_sli4_async_mbox_unblock(phba);
5412 }
5413 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005414 }
5415
5416 /* Now, interrupt mode asynchrous mailbox command */
5417 rc = lpfc_mbox_cmd_check(phba, mboxq);
5418 if (rc) {
5419 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5420 "(%d):2543 Mailbox command x%x (x%x) "
5421 "cannot issue Data: x%x x%x\n",
5422 mboxq->vport ? mboxq->vport->vpi : 0,
5423 mboxq->u.mb.mbxCommand,
5424 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5425 psli->sli_flag, flag);
5426 goto out_not_finished;
5427 }
James Smartda0436e2009-05-22 14:51:39 -04005428
5429 /* Put the mailbox command to the driver internal FIFO */
5430 psli->slistat.mbox_busy++;
5431 spin_lock_irqsave(&phba->hbalock, iflags);
5432 lpfc_mbox_put(phba, mboxq);
5433 spin_unlock_irqrestore(&phba->hbalock, iflags);
5434 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5435 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5436 "x%x (x%x) x%x x%x x%x\n",
5437 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5438 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5439 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5440 phba->pport->port_state,
5441 psli->sli_flag, MBX_NOWAIT);
5442 /* Wake up worker thread to transport mailbox command from head */
5443 lpfc_worker_wake_up(phba);
5444
5445 return MBX_BUSY;
5446
5447out_not_finished:
5448 return MBX_NOT_FINISHED;
5449}
5450
5451/**
5452 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5453 * @phba: Pointer to HBA context object.
5454 *
5455 * This function is called by worker thread to send a mailbox command to
5456 * SLI4 HBA firmware.
5457 *
5458 **/
5459int
5460lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5461{
5462 struct lpfc_sli *psli = &phba->sli;
5463 LPFC_MBOXQ_t *mboxq;
5464 int rc = MBX_SUCCESS;
5465 unsigned long iflags;
5466 struct lpfc_mqe *mqe;
5467 uint32_t mbx_cmnd;
5468
5469 /* Check interrupt mode before post async mailbox command */
5470 if (unlikely(!phba->sli4_hba.intr_enable))
5471 return MBX_NOT_FINISHED;
5472
5473 /* Check for mailbox command service token */
5474 spin_lock_irqsave(&phba->hbalock, iflags);
5475 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5476 spin_unlock_irqrestore(&phba->hbalock, iflags);
5477 return MBX_NOT_FINISHED;
5478 }
5479 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5480 spin_unlock_irqrestore(&phba->hbalock, iflags);
5481 return MBX_NOT_FINISHED;
5482 }
5483 if (unlikely(phba->sli.mbox_active)) {
5484 spin_unlock_irqrestore(&phba->hbalock, iflags);
5485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5486 "0384 There is pending active mailbox cmd\n");
5487 return MBX_NOT_FINISHED;
5488 }
5489 /* Take the mailbox command service token */
5490 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5491
5492 /* Get the next mailbox command from head of queue */
5493 mboxq = lpfc_mbox_get(phba);
5494
5495 /* If no more mailbox command waiting for post, we're done */
5496 if (!mboxq) {
5497 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5498 spin_unlock_irqrestore(&phba->hbalock, iflags);
5499 return MBX_SUCCESS;
5500 }
5501 phba->sli.mbox_active = mboxq;
5502 spin_unlock_irqrestore(&phba->hbalock, iflags);
5503
5504 /* Check device readiness for posting mailbox command */
5505 rc = lpfc_mbox_dev_check(phba);
5506 if (unlikely(rc))
5507 /* Driver clean routine will clean up pending mailbox */
5508 goto out_not_finished;
5509
5510 /* Prepare the mbox command to be posted */
5511 mqe = &mboxq->u.mqe;
5512 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5513
5514 /* Start timer for the mbox_tmo and log some mailbox post messages */
5515 mod_timer(&psli->mbox_tmo, (jiffies +
5516 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5517
5518 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5519 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5520 "x%x x%x\n",
5521 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5522 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5523 phba->pport->port_state, psli->sli_flag);
5524
5525 if (mbx_cmnd != MBX_HEARTBEAT) {
5526 if (mboxq->vport) {
5527 lpfc_debugfs_disc_trc(mboxq->vport,
5528 LPFC_DISC_TRC_MBOX_VPORT,
5529 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5530 mbx_cmnd, mqe->un.mb_words[0],
5531 mqe->un.mb_words[1]);
5532 } else {
5533 lpfc_debugfs_disc_trc(phba->pport,
5534 LPFC_DISC_TRC_MBOX,
5535 "MBOX Send: cmd:x%x mb:x%x x%x",
5536 mbx_cmnd, mqe->un.mb_words[0],
5537 mqe->un.mb_words[1]);
5538 }
5539 }
5540 psli->slistat.mbox_cmd++;
5541
5542 /* Post the mailbox command to the port */
5543 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5544 if (rc != MBX_SUCCESS) {
5545 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5546 "(%d):2533 Mailbox command x%x (x%x) "
5547 "cannot issue Data: x%x x%x\n",
5548 mboxq->vport ? mboxq->vport->vpi : 0,
5549 mboxq->u.mb.mbxCommand,
5550 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5551 psli->sli_flag, MBX_NOWAIT);
5552 goto out_not_finished;
5553 }
5554
5555 return rc;
5556
5557out_not_finished:
5558 spin_lock_irqsave(&phba->hbalock, iflags);
5559 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5560 __lpfc_mbox_cmpl_put(phba, mboxq);
5561 /* Release the token */
5562 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5563 phba->sli.mbox_active = NULL;
5564 spin_unlock_irqrestore(&phba->hbalock, iflags);
5565
5566 return MBX_NOT_FINISHED;
5567}
5568
5569/**
5570 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5571 * @phba: Pointer to HBA context object.
5572 * @pmbox: Pointer to mailbox object.
5573 * @flag: Flag indicating how the mailbox need to be processed.
5574 *
5575 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5576 * the API jump table function pointer from the lpfc_hba struct.
5577 *
5578 * Return codes the caller owns the mailbox command after the return of the
5579 * function.
5580 **/
5581int
5582lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5583{
5584 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5585}
5586
5587/**
5588 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5589 * @phba: The hba struct for which this call is being executed.
5590 * @dev_grp: The HBA PCI-Device group number.
5591 *
5592 * This routine sets up the mbox interface API function jump table in @phba
5593 * struct.
5594 * Returns: 0 - success, -ENODEV - failure.
5595 **/
5596int
5597lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5598{
5599
5600 switch (dev_grp) {
5601 case LPFC_PCI_DEV_LP:
5602 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5603 phba->lpfc_sli_handle_slow_ring_event =
5604 lpfc_sli_handle_slow_ring_event_s3;
5605 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5606 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5607 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5608 break;
5609 case LPFC_PCI_DEV_OC:
5610 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5611 phba->lpfc_sli_handle_slow_ring_event =
5612 lpfc_sli_handle_slow_ring_event_s4;
5613 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5614 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5615 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5616 break;
5617 default:
5618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5619 "1420 Invalid HBA PCI-device group: 0x%x\n",
5620 dev_grp);
5621 return -ENODEV;
5622 break;
5623 }
5624 return 0;
5625}
5626
5627/**
James Smart3621a712009-04-06 18:47:14 -04005628 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005629 * @phba: Pointer to HBA context object.
5630 * @pring: Pointer to driver SLI ring object.
5631 * @piocb: Pointer to address of newly added command iocb.
5632 *
5633 * This function is called with hbalock held to add a command
5634 * iocb to the txq when SLI layer cannot submit the command iocb
5635 * to the ring.
5636 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04005637void
James Smart92d7f7b2007-06-17 19:56:38 -05005638__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005639 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005640{
5641 /* Insert the caller's iocb in the txq tail for later processing. */
5642 list_add_tail(&piocb->list, &pring->txq);
5643 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005644}
5645
James Smarte59058c2008-08-24 21:49:00 -04005646/**
James Smart3621a712009-04-06 18:47:14 -04005647 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005648 * @phba: Pointer to HBA context object.
5649 * @pring: Pointer to driver SLI ring object.
5650 * @piocb: Pointer to address of newly added command iocb.
5651 *
5652 * This function is called with hbalock held before a new
5653 * iocb is submitted to the firmware. This function checks
5654 * txq to flush the iocbs in txq to Firmware before
5655 * submitting new iocbs to the Firmware.
5656 * If there are iocbs in the txq which need to be submitted
5657 * to firmware, lpfc_sli_next_iocb returns the first element
5658 * of the txq after dequeuing it from txq.
5659 * If there is no iocb in the txq then the function will return
5660 * *piocb and *piocb is set to NULL. Caller needs to check
5661 * *piocb to find if there are more commands in the txq.
5662 **/
dea31012005-04-17 16:05:31 -05005663static struct lpfc_iocbq *
5664lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005665 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005666{
5667 struct lpfc_iocbq * nextiocb;
5668
5669 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5670 if (!nextiocb) {
5671 nextiocb = *piocb;
5672 *piocb = NULL;
5673 }
5674
5675 return nextiocb;
5676}
5677
James Smarte59058c2008-08-24 21:49:00 -04005678/**
James Smart3772a992009-05-22 14:50:54 -04005679 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005680 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005681 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005682 * @piocb: Pointer to command iocb.
5683 * @flag: Flag indicating if this command can be put into txq.
5684 *
James Smart3772a992009-05-22 14:50:54 -04005685 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5686 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5687 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5688 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5689 * this function allows only iocbs for posting buffers. This function finds
5690 * next available slot in the command ring and posts the command to the
5691 * available slot and writes the port attention register to request HBA start
5692 * processing new iocb. If there is no slot available in the ring and
5693 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5694 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005695 *
James Smart3772a992009-05-22 14:50:54 -04005696 * This function is called with hbalock held. The function will return success
5697 * after it successfully submit the iocb to firmware or after adding to the
5698 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005699 **/
James Smart98c9ea52007-10-27 13:37:33 -04005700static int
James Smart3772a992009-05-22 14:50:54 -04005701__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005702 struct lpfc_iocbq *piocb, uint32_t flag)
5703{
5704 struct lpfc_iocbq *nextiocb;
5705 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005706 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005707
James Smart92d7f7b2007-06-17 19:56:38 -05005708 if (piocb->iocb_cmpl && (!piocb->vport) &&
5709 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5710 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5711 lpfc_printf_log(phba, KERN_ERR,
5712 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005713 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005714 piocb->iocb.ulpCommand);
5715 dump_stack();
5716 return IOCB_ERROR;
5717 }
5718
5719
Linas Vepstas8d63f372007-02-14 14:28:36 -06005720 /* If the PCI channel is in offline state, do not post iocbs. */
5721 if (unlikely(pci_channel_offline(phba->pcidev)))
5722 return IOCB_ERROR;
5723
James Smarta257bf92009-04-06 18:48:10 -04005724 /* If HBA has a deferred error attention, fail the iocb. */
5725 if (unlikely(phba->hba_flag & DEFER_ERATT))
5726 return IOCB_ERROR;
5727
dea31012005-04-17 16:05:31 -05005728 /*
5729 * We should never get an IOCB if we are in a < LINK_DOWN state
5730 */
James Smart2e0fef82007-06-17 19:56:36 -05005731 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005732 return IOCB_ERROR;
5733
5734 /*
5735 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005736 * outstanding event.
dea31012005-04-17 16:05:31 -05005737 */
James Smart0b727fe2007-10-27 13:37:25 -04005738 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005739 goto iocb_busy;
5740
James Smart2e0fef82007-06-17 19:56:36 -05005741 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005742 /*
James Smart2680eea2007-04-25 09:52:55 -04005743 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005744 * can be issued if the link is not up.
5745 */
5746 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005747 case CMD_GEN_REQUEST64_CR:
5748 case CMD_GEN_REQUEST64_CX:
5749 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5750 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005751 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005752 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5753 MENLO_TRANSPORT_TYPE))
5754
5755 goto iocb_busy;
5756 break;
dea31012005-04-17 16:05:31 -05005757 case CMD_QUE_RING_BUF_CN:
5758 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005759 /*
5760 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5761 * completion, iocb_cmpl MUST be 0.
5762 */
5763 if (piocb->iocb_cmpl)
5764 piocb->iocb_cmpl = NULL;
5765 /*FALLTHROUGH*/
5766 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005767 case CMD_CLOSE_XRI_CN:
5768 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005769 break;
5770 default:
5771 goto iocb_busy;
5772 }
5773
5774 /*
5775 * For FCP commands, we must be in a state where we can process link
5776 * attention events.
5777 */
5778 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005779 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005780 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005781 }
dea31012005-04-17 16:05:31 -05005782
dea31012005-04-17 16:05:31 -05005783 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5784 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5785 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5786
5787 if (iocb)
5788 lpfc_sli_update_ring(phba, pring);
5789 else
5790 lpfc_sli_update_full_ring(phba, pring);
5791
5792 if (!piocb)
5793 return IOCB_SUCCESS;
5794
5795 goto out_busy;
5796
5797 iocb_busy:
5798 pring->stats.iocb_cmd_delay++;
5799
5800 out_busy:
5801
5802 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005803 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005804 return IOCB_SUCCESS;
5805 }
5806
5807 return IOCB_BUSY;
5808}
5809
James Smart3772a992009-05-22 14:50:54 -04005810/**
James Smart4f774512009-05-22 14:52:35 -04005811 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5812 * @phba: Pointer to HBA context object.
5813 * @piocb: Pointer to command iocb.
5814 * @sglq: Pointer to the scatter gather queue object.
5815 *
5816 * This routine converts the bpl or bde that is in the IOCB
5817 * to a sgl list for the sli4 hardware. The physical address
5818 * of the bpl/bde is converted back to a virtual address.
5819 * If the IOCB contains a BPL then the list of BDE's is
5820 * converted to sli4_sge's. If the IOCB contains a single
5821 * BDE then it is converted to a single sli_sge.
5822 * The IOCB is still in cpu endianess so the contents of
5823 * the bpl can be used without byte swapping.
5824 *
5825 * Returns valid XRI = Success, NO_XRI = Failure.
5826**/
5827static uint16_t
5828lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5829 struct lpfc_sglq *sglq)
5830{
5831 uint16_t xritag = NO_XRI;
5832 struct ulp_bde64 *bpl = NULL;
5833 struct ulp_bde64 bde;
5834 struct sli4_sge *sgl = NULL;
5835 IOCB_t *icmd;
5836 int numBdes = 0;
5837 int i = 0;
5838
5839 if (!piocbq || !sglq)
5840 return xritag;
5841
5842 sgl = (struct sli4_sge *)sglq->sgl;
5843 icmd = &piocbq->iocb;
5844 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5845 numBdes = icmd->un.genreq64.bdl.bdeSize /
5846 sizeof(struct ulp_bde64);
5847 /* The addrHigh and addrLow fields within the IOCB
5848 * have not been byteswapped yet so there is no
5849 * need to swap them back.
5850 */
5851 bpl = (struct ulp_bde64 *)
5852 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5853
5854 if (!bpl)
5855 return xritag;
5856
5857 for (i = 0; i < numBdes; i++) {
5858 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005859 sgl->addr_hi = bpl->addrHigh;
5860 sgl->addr_lo = bpl->addrLow;
5861
James Smart4f774512009-05-22 14:52:35 -04005862 if ((i+1) == numBdes)
5863 bf_set(lpfc_sli4_sge_last, sgl, 1);
5864 else
5865 bf_set(lpfc_sli4_sge_last, sgl, 0);
5866 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005867 /* swap the size field back to the cpu so we
5868 * can assign it to the sgl.
5869 */
5870 bde.tus.w = le32_to_cpu(bpl->tus.w);
5871 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005872 bpl++;
5873 sgl++;
5874 }
5875 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5876 /* The addrHigh and addrLow fields of the BDE have not
5877 * been byteswapped yet so they need to be swapped
5878 * before putting them in the sgl.
5879 */
5880 sgl->addr_hi =
5881 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5882 sgl->addr_lo =
5883 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005884 bf_set(lpfc_sli4_sge_last, sgl, 1);
5885 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005886 sgl->sge_len =
5887 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005888 }
5889 return sglq->sli4_xritag;
5890}
5891
5892/**
5893 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5894 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005895 *
5896 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005897 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5898 * held.
James Smart4f774512009-05-22 14:52:35 -04005899 *
5900 * Return: index into SLI4 fast-path FCP queue index.
5901 **/
5902static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005903lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005904{
James Smart8fa38512009-07-19 10:01:03 -04005905 ++phba->fcp_qidx;
5906 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5907 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005908
James Smart8fa38512009-07-19 10:01:03 -04005909 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005910}
5911
5912/**
5913 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5914 * @phba: Pointer to HBA context object.
5915 * @piocb: Pointer to command iocb.
5916 * @wqe: Pointer to the work queue entry.
5917 *
5918 * This routine converts the iocb command to its Work Queue Entry
5919 * equivalent. The wqe pointer should not have any fields set when
5920 * this routine is called because it will memcpy over them.
5921 * This routine does not set the CQ_ID or the WQEC bits in the
5922 * wqe.
5923 *
5924 * Returns: 0 = Success, IOCB_ERROR = Failure.
5925 **/
5926static int
5927lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5928 union lpfc_wqe *wqe)
5929{
James Smart5ffc2662009-11-18 15:39:44 -05005930 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005931 uint8_t ct = 0;
5932 uint32_t fip;
5933 uint32_t abort_tag;
5934 uint8_t command_type = ELS_COMMAND_NON_FIP;
5935 uint8_t cmnd;
5936 uint16_t xritag;
5937 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005938 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005939 int numBdes, i;
5940 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005941
James Smart45ed1192009-10-02 15:17:02 -04005942 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005943 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005944 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005945 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005946 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005947 command_type = ELS_COMMAND_FIP;
5948 else
5949 command_type = ELS_COMMAND_NON_FIP;
5950
James Smart4f774512009-05-22 14:52:35 -04005951 /* Some of the fields are in the right position already */
5952 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5953 abort_tag = (uint32_t) iocbq->iotag;
5954 xritag = iocbq->sli4_xritag;
5955 wqe->words[7] = 0; /* The ct field has moved so reset */
5956 /* words0-2 bpl convert bde */
5957 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005958 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5959 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005960 bpl = (struct ulp_bde64 *)
5961 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5962 if (!bpl)
5963 return IOCB_ERROR;
5964
5965 /* Should already be byte swapped. */
5966 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5967 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5968 /* swap the size field back to the cpu so we
5969 * can assign it to the sgl.
5970 */
5971 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005972 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5973 total_len = 0;
5974 for (i = 0; i < numBdes; i++) {
5975 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5976 total_len += bde.tus.f.bdeSize;
5977 }
James Smart4f774512009-05-22 14:52:35 -04005978 } else
James Smart5ffc2662009-11-18 15:39:44 -05005979 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005980
5981 iocbq->iocb.ulpIoTag = iocbq->iotag;
5982 cmnd = iocbq->iocb.ulpCommand;
5983
5984 switch (iocbq->iocb.ulpCommand) {
5985 case CMD_ELS_REQUEST64_CR:
5986 if (!iocbq->iocb.ulpLe) {
5987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5988 "2007 Only Limited Edition cmd Format"
5989 " supported 0x%x\n",
5990 iocbq->iocb.ulpCommand);
5991 return IOCB_ERROR;
5992 }
James Smart5ffc2662009-11-18 15:39:44 -05005993 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005994 /* Els_reguest64 has a TMO */
5995 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5996 iocbq->iocb.ulpTimeout);
5997 /* Need a VF for word 4 set the vf bit*/
5998 bf_set(els_req64_vf, &wqe->els_req, 0);
5999 /* And a VFID for word 12 */
6000 bf_set(els_req64_vfid, &wqe->els_req, 0);
6001 /*
6002 * Set ct field to 3, indicates that the context_tag field
6003 * contains the FCFI and remote N_Port_ID is
6004 * in word 5.
6005 */
6006
6007 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6008 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6009 iocbq->iocb.ulpContext);
6010
James Smart4f774512009-05-22 14:52:35 -04006011 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6012 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6013 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006014
6015 if (command_type == ELS_COMMAND_FIP) {
6016 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6017 >> LPFC_FIP_ELS_ID_SHIFT);
6018 }
6019 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6020
James Smart4f774512009-05-22 14:52:35 -04006021 break;
James Smart5ffc2662009-11-18 15:39:44 -05006022 case CMD_XMIT_SEQUENCE64_CX:
6023 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6024 iocbq->iocb.un.ulpWord[3]);
6025 wqe->generic.word3 = 0;
6026 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006027 /* The entire sequence is transmitted for this IOCB */
6028 xmit_len = total_len;
6029 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006030 case CMD_XMIT_SEQUENCE64_CR:
6031 /* word3 iocb=io_tag32 wqe=payload_offset */
6032 /* payload offset used for multilpe outstanding
6033 * sequences on the same exchange
6034 */
6035 wqe->words[3] = 0;
6036 /* word4 relative_offset memcpy */
6037 /* word5 r_ctl/df_ctl memcpy */
6038 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006039 wqe->xmit_sequence.xmit_len = xmit_len;
6040 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006041 break;
6042 case CMD_XMIT_BCAST64_CN:
6043 /* word3 iocb=iotag32 wqe=payload_len */
6044 wqe->words[3] = 0; /* no definition for this in wqe */
6045 /* word4 iocb=rsvd wqe=rsvd */
6046 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6047 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6048 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6049 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6050 break;
6051 case CMD_FCP_IWRITE64_CR:
6052 command_type = FCP_COMMAND_DATA_OUT;
6053 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6054 * confusing.
6055 * word3 is payload_len: byte offset to the sgl entry for the
6056 * fcp_command.
6057 * word4 is total xfer len, same as the IOCB->ulpParameter.
6058 * word5 is initial xfer len 0 = wait for xfer-ready
6059 */
6060
6061 /* Always wait for xfer-ready before sending data */
6062 wqe->fcp_iwrite.initial_xfer_len = 0;
6063 /* word 4 (xfer length) should have been set on the memcpy */
6064
6065 /* allow write to fall through to read */
6066 case CMD_FCP_IREAD64_CR:
6067 /* FCP_CMD is always the 1st sgl entry */
6068 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05006069 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04006070
6071 /* word 4 (xfer length) should have been set on the memcpy */
6072
6073 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6074 iocbq->iocb.ulpFCP2Rcvy);
6075 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6076 /* The XC bit and the XS bit are similar. The driver never
6077 * tracked whether or not the exchange was previouslly open.
6078 * XC = Exchange create, 0 is create. 1 is already open.
6079 * XS = link cmd: 1 do not close the exchange after command.
6080 * XS = 0 close exchange when command completes.
6081 * The only time we would not set the XC bit is when the XS bit
6082 * is set and we are sending our 2nd or greater command on
6083 * this exchange.
6084 */
James Smart4f774512009-05-22 14:52:35 -04006085 /* Always open the exchange */
6086 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6087
6088 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6089 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006090 break;
6091 case CMD_FCP_ICMND64_CR:
6092 /* Always open the exchange */
6093 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6094
6095 wqe->words[4] = 0;
6096 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6097 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006098 break;
6099 case CMD_GEN_REQUEST64_CR:
6100 /* word3 command length is described as byte offset to the
6101 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6102 * sgl[0] = cmnd
6103 * sgl[1] = rsp.
6104 *
6105 */
James Smart5ffc2662009-11-18 15:39:44 -05006106 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006107 /* Word4 parameter copied in the memcpy */
6108 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6109 /* word6 context tag copied in memcpy */
6110 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6111 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6113 "2015 Invalid CT %x command 0x%x\n",
6114 ct, iocbq->iocb.ulpCommand);
6115 return IOCB_ERROR;
6116 }
6117 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6118 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6119 iocbq->iocb.ulpTimeout);
6120
6121 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6122 command_type = OTHER_COMMAND;
6123 break;
6124 case CMD_XMIT_ELS_RSP64_CX:
6125 /* words0-2 BDE memcpy */
6126 /* word3 iocb=iotag32 wqe=rsvd */
6127 wqe->words[3] = 0;
6128 /* word4 iocb=did wge=rsvd. */
6129 wqe->words[4] = 0;
6130 /* word5 iocb=rsvd wge=did */
6131 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6132 iocbq->iocb.un.elsreq64.remoteID);
6133
6134 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6135 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6136
6137 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6138 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6139 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6140 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6141 iocbq->vport->vpi + phba->vpi_base);
6142 command_type = OTHER_COMMAND;
6143 break;
6144 case CMD_CLOSE_XRI_CN:
6145 case CMD_ABORT_XRI_CN:
6146 case CMD_ABORT_XRI_CX:
6147 /* words 0-2 memcpy should be 0 rserved */
6148 /* port will send abts */
6149 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6150 /*
6151 * The link is down so the fw does not need to send abts
6152 * on the wire.
6153 */
6154 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6155 else
6156 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6157 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006158 wqe->words[5] = 0;
6159 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6160 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6161 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006162 /*
6163 * The abort handler will send us CMD_ABORT_XRI_CN or
6164 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6165 */
6166 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6167 cmnd = CMD_ABORT_XRI_CX;
6168 command_type = OTHER_COMMAND;
6169 xritag = 0;
6170 break;
James Smart6669f9b2009-10-02 15:16:45 -04006171 case CMD_XMIT_BLS_RSP64_CX:
6172 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6173 * we re-construct this WQE here based on information in
6174 * iocbq from scratch.
6175 */
6176 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006177 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006178 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006179 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6180 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6181 LPFC_ABTS_UNSOL_INT) {
6182 /* ABTS sent by initiator to CT exchange, the
6183 * RX_ID field will be filled with the newly
6184 * allocated responder XRI.
6185 */
6186 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6187 iocbq->sli4_xritag);
6188 } else {
6189 /* ABTS sent by responder to CT exchange, the
6190 * RX_ID field will be filled with the responder
6191 * RX_ID from ABTS.
6192 */
6193 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6194 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6195 }
James Smart6669f9b2009-10-02 15:16:45 -04006196 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6197 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6198 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6199 iocbq->iocb.ulpContext);
6200 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6201 command_type = OTHER_COMMAND;
6202 break;
James Smart4f774512009-05-22 14:52:35 -04006203 case CMD_XRI_ABORTED_CX:
6204 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6205 /* words0-2 are all 0's no bde */
6206 /* word3 and word4 are rsvrd */
6207 wqe->words[3] = 0;
6208 wqe->words[4] = 0;
6209 /* word5 iocb=rsvd wge=did */
6210 /* There is no remote port id in the IOCB? */
6211 /* Let this fall through and fail */
6212 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6213 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6214 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6215 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6216 default:
6217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6218 "2014 Invalid command 0x%x\n",
6219 iocbq->iocb.ulpCommand);
6220 return IOCB_ERROR;
6221 break;
6222
6223 }
6224 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6225 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6226 wqe->generic.abort_tag = abort_tag;
6227 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6228 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6229 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6230 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6231
6232 return 0;
6233}
6234
6235/**
6236 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6237 * @phba: Pointer to HBA context object.
6238 * @ring_number: SLI ring number to issue iocb on.
6239 * @piocb: Pointer to command iocb.
6240 * @flag: Flag indicating if this command can be put into txq.
6241 *
6242 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6243 * an iocb command to an HBA with SLI-4 interface spec.
6244 *
6245 * This function is called with hbalock held. The function will return success
6246 * after it successfully submit the iocb to firmware or after adding to the
6247 * txq.
6248 **/
6249static int
6250__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6251 struct lpfc_iocbq *piocb, uint32_t flag)
6252{
6253 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006254 union lpfc_wqe wqe;
6255 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006256
6257 if (piocb->sli4_xritag == NO_XRI) {
6258 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006259 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006260 sglq = NULL;
6261 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006262 if (pring->txq_cnt) {
6263 if (!(flag & SLI_IOCB_RET_IOCB)) {
6264 __lpfc_sli_ringtx_put(phba,
6265 pring, piocb);
6266 return IOCB_SUCCESS;
6267 } else {
6268 return IOCB_BUSY;
6269 }
6270 } else {
James Smart4f774512009-05-22 14:52:35 -04006271 sglq = __lpfc_sli_get_sglq(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04006272 if (!sglq) {
6273 if (!(flag & SLI_IOCB_RET_IOCB)) {
6274 __lpfc_sli_ringtx_put(phba,
6275 pring,
6276 piocb);
6277 return IOCB_SUCCESS;
6278 } else
6279 return IOCB_BUSY;
6280 }
6281 }
James Smart4f774512009-05-22 14:52:35 -04006282 }
6283 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6284 sglq = NULL; /* These IO's already have an XRI and
6285 * a mapped sgl.
6286 */
6287 } else {
6288 /* This is a continuation of a commandi,(CX) so this
6289 * sglq is on the active list
6290 */
6291 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6292 if (!sglq)
6293 return IOCB_ERROR;
6294 }
6295
6296 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006297 piocb->sli4_xritag = sglq->sli4_xritag;
6298
6299 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006300 return IOCB_ERROR;
6301 }
6302
6303 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6304 return IOCB_ERROR;
6305
James Smart341af102010-01-26 23:07:37 -05006306 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6307 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006308 /*
6309 * For FCP command IOCB, get a new WQ index to distribute
6310 * WQE across the WQsr. On the other hand, for abort IOCB,
6311 * it carries the same WQ index to the original command
6312 * IOCB.
6313 */
James Smart341af102010-01-26 23:07:37 -05006314 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006315 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6316 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6317 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006318 return IOCB_ERROR;
6319 } else {
6320 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6321 return IOCB_ERROR;
6322 }
6323 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6324
6325 return 0;
6326}
6327
6328/**
James Smart3772a992009-05-22 14:50:54 -04006329 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6330 *
6331 * This routine wraps the actual lockless version for issusing IOCB function
6332 * pointer from the lpfc_hba struct.
6333 *
6334 * Return codes:
6335 * IOCB_ERROR - Error
6336 * IOCB_SUCCESS - Success
6337 * IOCB_BUSY - Busy
6338 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006339int
James Smart3772a992009-05-22 14:50:54 -04006340__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6341 struct lpfc_iocbq *piocb, uint32_t flag)
6342{
6343 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6344}
6345
6346/**
6347 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6348 * @phba: The hba struct for which this call is being executed.
6349 * @dev_grp: The HBA PCI-Device group number.
6350 *
6351 * This routine sets up the SLI interface API function jump table in @phba
6352 * struct.
6353 * Returns: 0 - success, -ENODEV - failure.
6354 **/
6355int
6356lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6357{
6358
6359 switch (dev_grp) {
6360 case LPFC_PCI_DEV_LP:
6361 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6362 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6363 break;
James Smart4f774512009-05-22 14:52:35 -04006364 case LPFC_PCI_DEV_OC:
6365 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6366 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6367 break;
James Smart3772a992009-05-22 14:50:54 -04006368 default:
6369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6370 "1419 Invalid HBA PCI-device group: 0x%x\n",
6371 dev_grp);
6372 return -ENODEV;
6373 break;
6374 }
6375 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6376 return 0;
6377}
James Smart92d7f7b2007-06-17 19:56:38 -05006378
James Smarte59058c2008-08-24 21:49:00 -04006379/**
James Smart3621a712009-04-06 18:47:14 -04006380 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006381 * @phba: Pointer to HBA context object.
6382 * @pring: Pointer to driver SLI ring object.
6383 * @piocb: Pointer to command iocb.
6384 * @flag: Flag indicating if this command can be put into txq.
6385 *
6386 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6387 * function. This function gets the hbalock and calls
6388 * __lpfc_sli_issue_iocb function and will return the error returned
6389 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6390 * functions which do not hold hbalock.
6391 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006392int
James Smart3772a992009-05-22 14:50:54 -04006393lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006394 struct lpfc_iocbq *piocb, uint32_t flag)
6395{
6396 unsigned long iflags;
6397 int rc;
6398
6399 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006400 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006401 spin_unlock_irqrestore(&phba->hbalock, iflags);
6402
6403 return rc;
6404}
6405
James Smarte59058c2008-08-24 21:49:00 -04006406/**
James Smart3621a712009-04-06 18:47:14 -04006407 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006408 * @phba: Pointer to HBA context object.
6409 *
6410 * This function is called while driver attaches with the
6411 * HBA to setup the extra ring. The extra ring is used
6412 * only when driver needs to support target mode functionality
6413 * or IP over FC functionalities.
6414 *
6415 * This function is called with no lock held.
6416 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006417static int
6418lpfc_extra_ring_setup( struct lpfc_hba *phba)
6419{
6420 struct lpfc_sli *psli;
6421 struct lpfc_sli_ring *pring;
6422
6423 psli = &phba->sli;
6424
6425 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006426
6427 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006428 pring = &psli->ring[psli->fcp_ring];
6429 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6430 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6431 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6432 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6433
James Smarta4bc3372006-12-02 13:34:16 -05006434 /* and give them to the extra ring */
6435 pring = &psli->ring[psli->extra_ring];
6436
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006437 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6438 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6439 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6440 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6441
6442 /* Setup default profile for this ring */
6443 pring->iotag_max = 4096;
6444 pring->num_mask = 1;
6445 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006446 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6447 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006448 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6449 return 0;
6450}
6451
James Smarte59058c2008-08-24 21:49:00 -04006452/**
James Smart3621a712009-04-06 18:47:14 -04006453 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006454 * @phba: Pointer to HBA context object.
6455 * @pring: Pointer to driver SLI ring object.
6456 * @iocbq: Pointer to iocb object.
6457 *
6458 * This function is called by the slow ring event handler
6459 * function when there is an ASYNC event iocb in the ring.
6460 * This function is called with no lock held.
6461 * Currently this function handles only temperature related
6462 * ASYNC events. The function decodes the temperature sensor
6463 * event message and posts events for the management applications.
6464 **/
James Smart98c9ea52007-10-27 13:37:33 -04006465static void
James Smart57127f12007-10-27 13:37:05 -04006466lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6467 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6468{
6469 IOCB_t *icmd;
6470 uint16_t evt_code;
6471 uint16_t temp;
6472 struct temp_event temp_event_data;
6473 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006474 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006475
6476 icmd = &iocbq->iocb;
6477 evt_code = icmd->un.asyncstat.evt_code;
6478 temp = icmd->ulpContext;
6479
6480 if ((evt_code != ASYNC_TEMP_WARN) &&
6481 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006482 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006483 lpfc_printf_log(phba,
6484 KERN_ERR,
6485 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006486 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006487 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006488 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6489 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6490 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6491 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006492 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006493 icmd->un.asyncstat.evt_code,
6494 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6495 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6496 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6497 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6498
James Smart57127f12007-10-27 13:37:05 -04006499 return;
6500 }
6501 temp_event_data.data = (uint32_t)temp;
6502 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6503 if (evt_code == ASYNC_TEMP_WARN) {
6504 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6505 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006506 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006507 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006508 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006509 "corrective action. temperature : %d Celsius\n",
6510 temp);
6511 }
6512 if (evt_code == ASYNC_TEMP_SAFE) {
6513 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6514 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006515 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006516 LOG_TEMP,
6517 "0340 Adapter temperature is OK now. "
6518 "temperature : %d Celsius\n",
6519 temp);
6520 }
6521
6522 /* Send temperature change event to applications */
6523 shost = lpfc_shost_from_vport(phba->pport);
6524 fc_host_post_vendor_event(shost, fc_get_event_number(),
6525 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006526 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006527
6528}
6529
6530
James Smarte59058c2008-08-24 21:49:00 -04006531/**
James Smart3621a712009-04-06 18:47:14 -04006532 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006533 * @phba: Pointer to HBA context object.
6534 *
6535 * lpfc_sli_setup sets up rings of the SLI interface with
6536 * number of iocbs per ring and iotags. This function is
6537 * called while driver attach to the HBA and before the
6538 * interrupts are enabled. So there is no need for locking.
6539 *
6540 * This function always returns 0.
6541 **/
dea31012005-04-17 16:05:31 -05006542int
6543lpfc_sli_setup(struct lpfc_hba *phba)
6544{
James Smarted957682007-06-17 19:56:37 -05006545 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006546 struct lpfc_sli *psli = &phba->sli;
6547 struct lpfc_sli_ring *pring;
6548
6549 psli->num_rings = MAX_CONFIGURED_RINGS;
6550 psli->sli_flag = 0;
6551 psli->fcp_ring = LPFC_FCP_RING;
6552 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006553 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006554
James Bottomley604a3e32005-10-29 10:28:33 -05006555 psli->iocbq_lookup = NULL;
6556 psli->iocbq_lookup_len = 0;
6557 psli->last_iotag = 0;
6558
dea31012005-04-17 16:05:31 -05006559 for (i = 0; i < psli->num_rings; i++) {
6560 pring = &psli->ring[i];
6561 switch (i) {
6562 case LPFC_FCP_RING: /* ring 0 - FCP */
6563 /* numCiocb and numRiocb are used in config_port */
6564 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6565 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6566 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6567 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6568 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6569 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006570 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006571 SLI3_IOCB_CMD_SIZE :
6572 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006573 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006574 SLI3_IOCB_RSP_SIZE :
6575 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006576 pring->iotag_ctr = 0;
6577 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006578 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006579 pring->fast_iotag = pring->iotag_max;
6580 pring->num_mask = 0;
6581 break;
James Smarta4bc3372006-12-02 13:34:16 -05006582 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006583 /* numCiocb and numRiocb are used in config_port */
6584 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6585 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006586 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006587 SLI3_IOCB_CMD_SIZE :
6588 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006589 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006590 SLI3_IOCB_RSP_SIZE :
6591 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006592 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006593 pring->num_mask = 0;
6594 break;
6595 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6596 /* numCiocb and numRiocb are used in config_port */
6597 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6598 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006599 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006600 SLI3_IOCB_CMD_SIZE :
6601 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006602 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006603 SLI3_IOCB_RSP_SIZE :
6604 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006605 pring->fast_iotag = 0;
6606 pring->iotag_ctr = 0;
6607 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006608 pring->lpfc_sli_rcv_async_status =
6609 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006610 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006611 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006612 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6613 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006614 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006615 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006616 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006617 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6618 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006619 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006620 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006621 pring->prt[2].profile = 0; /* Mask 2 */
6622 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006623 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006624 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006625 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006626 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006627 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006628 pring->prt[3].profile = 0; /* Mask 3 */
6629 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006630 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006631 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006632 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006633 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006634 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006635 /* abort unsolicited sequence */
6636 pring->prt[4].profile = 0; /* Mask 4 */
6637 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6638 pring->prt[4].type = FC_TYPE_BLS;
6639 pring->prt[4].lpfc_sli_rcv_unsol_event =
6640 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006641 break;
6642 }
James Smarted957682007-06-17 19:56:37 -05006643 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006644 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006645 }
James Smarted957682007-06-17 19:56:37 -05006646 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006647 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006648 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6649 "SLI2 SLIM Data: x%x x%lx\n",
6650 phba->brd_no, totiocbsize,
6651 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006652 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006653 if (phba->cfg_multi_ring_support == 2)
6654 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006655
6656 return 0;
6657}
6658
James Smarte59058c2008-08-24 21:49:00 -04006659/**
James Smart3621a712009-04-06 18:47:14 -04006660 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006661 * @phba: Pointer to HBA context object.
6662 *
6663 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6664 * ring. This function also initializes ring indices of each ring.
6665 * This function is called during the initialization of the SLI
6666 * interface of an HBA.
6667 * This function is called with no lock held and always returns
6668 * 1.
6669 **/
dea31012005-04-17 16:05:31 -05006670int
James Smart2e0fef82007-06-17 19:56:36 -05006671lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006672{
6673 struct lpfc_sli *psli;
6674 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006675 int i;
dea31012005-04-17 16:05:31 -05006676
6677 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006678 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006679 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006680 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006681 /* Initialize list headers for txq and txcmplq as double linked lists */
6682 for (i = 0; i < psli->num_rings; i++) {
6683 pring = &psli->ring[i];
6684 pring->ringno = i;
6685 pring->next_cmdidx = 0;
6686 pring->local_getidx = 0;
6687 pring->cmdidx = 0;
6688 INIT_LIST_HEAD(&pring->txq);
6689 INIT_LIST_HEAD(&pring->txcmplq);
6690 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006691 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006692 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006693 }
James Smart2e0fef82007-06-17 19:56:36 -05006694 spin_unlock_irq(&phba->hbalock);
6695 return 1;
dea31012005-04-17 16:05:31 -05006696}
6697
James Smarte59058c2008-08-24 21:49:00 -04006698/**
James Smart04c68492009-05-22 14:52:52 -04006699 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6700 * @phba: Pointer to HBA context object.
6701 *
6702 * This routine flushes the mailbox command subsystem. It will unconditionally
6703 * flush all the mailbox commands in the three possible stages in the mailbox
6704 * command sub-system: pending mailbox command queue; the outstanding mailbox
6705 * command; and completed mailbox command queue. It is caller's responsibility
6706 * to make sure that the driver is in the proper state to flush the mailbox
6707 * command sub-system. Namely, the posting of mailbox commands into the
6708 * pending mailbox command queue from the various clients must be stopped;
6709 * either the HBA is in a state that it will never works on the outstanding
6710 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6711 * mailbox command has been completed.
6712 **/
6713static void
6714lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6715{
6716 LIST_HEAD(completions);
6717 struct lpfc_sli *psli = &phba->sli;
6718 LPFC_MBOXQ_t *pmb;
6719 unsigned long iflag;
6720
6721 /* Flush all the mailbox commands in the mbox system */
6722 spin_lock_irqsave(&phba->hbalock, iflag);
6723 /* The pending mailbox command queue */
6724 list_splice_init(&phba->sli.mboxq, &completions);
6725 /* The outstanding active mailbox command */
6726 if (psli->mbox_active) {
6727 list_add_tail(&psli->mbox_active->list, &completions);
6728 psli->mbox_active = NULL;
6729 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6730 }
6731 /* The completed mailbox command queue */
6732 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6733 spin_unlock_irqrestore(&phba->hbalock, iflag);
6734
6735 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6736 while (!list_empty(&completions)) {
6737 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6738 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6739 if (pmb->mbox_cmpl)
6740 pmb->mbox_cmpl(phba, pmb);
6741 }
6742}
6743
6744/**
James Smart3621a712009-04-06 18:47:14 -04006745 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006746 * @vport: Pointer to virtual port object.
6747 *
6748 * lpfc_sli_host_down is called to clean up the resources
6749 * associated with a vport before destroying virtual
6750 * port data structures.
6751 * This function does following operations:
6752 * - Free discovery resources associated with this virtual
6753 * port.
6754 * - Free iocbs associated with this virtual port in
6755 * the txq.
6756 * - Send abort for all iocb commands associated with this
6757 * vport in txcmplq.
6758 *
6759 * This function is called with no lock held and always returns 1.
6760 **/
dea31012005-04-17 16:05:31 -05006761int
James Smart92d7f7b2007-06-17 19:56:38 -05006762lpfc_sli_host_down(struct lpfc_vport *vport)
6763{
James Smart858c9f62007-06-17 19:56:39 -05006764 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006765 struct lpfc_hba *phba = vport->phba;
6766 struct lpfc_sli *psli = &phba->sli;
6767 struct lpfc_sli_ring *pring;
6768 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006769 int i;
6770 unsigned long flags = 0;
6771 uint16_t prev_pring_flag;
6772
6773 lpfc_cleanup_discovery_resources(vport);
6774
6775 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006776 for (i = 0; i < psli->num_rings; i++) {
6777 pring = &psli->ring[i];
6778 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006779 /* Only slow rings */
6780 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006781 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006782 /* Set the lpfc data pending flag */
6783 set_bit(LPFC_DATA_READY, &phba->data_flags);
6784 }
James Smart92d7f7b2007-06-17 19:56:38 -05006785 /*
6786 * Error everything on the txq since these iocbs have not been
6787 * given to the FW yet.
6788 */
James Smart92d7f7b2007-06-17 19:56:38 -05006789 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6790 if (iocb->vport != vport)
6791 continue;
James Smart858c9f62007-06-17 19:56:39 -05006792 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006793 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006794 }
6795
6796 /* Next issue ABTS for everything on the txcmplq */
6797 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6798 list) {
6799 if (iocb->vport != vport)
6800 continue;
6801 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6802 }
6803
6804 pring->flag = prev_pring_flag;
6805 }
6806
6807 spin_unlock_irqrestore(&phba->hbalock, flags);
6808
James Smarta257bf92009-04-06 18:48:10 -04006809 /* Cancel all the IOCBs from the completions list */
6810 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6811 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006812 return 1;
6813}
6814
James Smarte59058c2008-08-24 21:49:00 -04006815/**
James Smart3621a712009-04-06 18:47:14 -04006816 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006817 * @phba: Pointer to HBA context object.
6818 *
6819 * This function cleans up all iocb, buffers, mailbox commands
6820 * while shutting down the HBA. This function is called with no
6821 * lock held and always returns 1.
6822 * This function does the following to cleanup driver resources:
6823 * - Free discovery resources for each virtual port
6824 * - Cleanup any pending fabric iocbs
6825 * - Iterate through the iocb txq and free each entry
6826 * in the list.
6827 * - Free up any buffer posted to the HBA
6828 * - Free mailbox commands in the mailbox queue.
6829 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006830int
James Smart2e0fef82007-06-17 19:56:36 -05006831lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006832{
James Smart2534ba72007-04-25 09:52:20 -04006833 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006834 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006835 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006836 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006837 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006838 int i;
6839
6840 /* Shutdown the mailbox command sub-system */
6841 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006842
dea31012005-04-17 16:05:31 -05006843 lpfc_hba_down_prep(phba);
6844
James Smart92d7f7b2007-06-17 19:56:38 -05006845 lpfc_fabric_abort_hba(phba);
6846
James Smart2e0fef82007-06-17 19:56:36 -05006847 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006848 for (i = 0; i < psli->num_rings; i++) {
6849 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006850 /* Only slow rings */
6851 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006852 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006853 /* Set the lpfc data pending flag */
6854 set_bit(LPFC_DATA_READY, &phba->data_flags);
6855 }
dea31012005-04-17 16:05:31 -05006856
6857 /*
6858 * Error everything on the txq since these iocbs have not been
6859 * given to the FW yet.
6860 */
James Smart2534ba72007-04-25 09:52:20 -04006861 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006862 pring->txq_cnt = 0;
6863
dea31012005-04-17 16:05:31 -05006864 }
James Smart2e0fef82007-06-17 19:56:36 -05006865 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006866
James Smarta257bf92009-04-06 18:48:10 -04006867 /* Cancel all the IOCBs from the completions list */
6868 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6869 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006870
James Smart0ff10d42008-01-11 01:52:36 -05006871 spin_lock_irqsave(&phba->hbalock, flags);
6872 list_splice_init(&phba->elsbuf, &completions);
6873 phba->elsbuf_cnt = 0;
6874 phba->elsbuf_prev_cnt = 0;
6875 spin_unlock_irqrestore(&phba->hbalock, flags);
6876
6877 while (!list_empty(&completions)) {
6878 list_remove_head(&completions, buf_ptr,
6879 struct lpfc_dmabuf, list);
6880 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6881 kfree(buf_ptr);
6882 }
6883
dea31012005-04-17 16:05:31 -05006884 /* Return any active mbox cmds */
6885 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006886
James Smartda0436e2009-05-22 14:51:39 -04006887 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006888 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006889 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006890
James Smartda0436e2009-05-22 14:51:39 -04006891 return 1;
6892}
James Smart92d7f7b2007-06-17 19:56:38 -05006893
James Smartda0436e2009-05-22 14:51:39 -04006894/**
6895 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6896 * @phba: Pointer to HBA context object.
6897 *
6898 * This function cleans up all queues, iocb, buffers, mailbox commands while
6899 * shutting down the SLI4 HBA FCoE function. This function is called with no
6900 * lock held and always returns 1.
6901 *
6902 * This function does the following to cleanup driver FCoE function resources:
6903 * - Free discovery resources for each virtual port
6904 * - Cleanup any pending fabric iocbs
6905 * - Iterate through the iocb txq and free each entry in the list.
6906 * - Free up any buffer posted to the HBA.
6907 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6908 * - Free mailbox commands in the mailbox queue.
6909 **/
6910int
6911lpfc_sli4_hba_down(struct lpfc_hba *phba)
6912{
6913 /* Stop the SLI4 device port */
6914 lpfc_stop_port(phba);
6915
6916 /* Tear down the queues in the HBA */
6917 lpfc_sli4_queue_unset(phba);
6918
6919 /* unregister default FCFI from the HBA */
6920 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6921
dea31012005-04-17 16:05:31 -05006922 return 1;
6923}
6924
James Smarte59058c2008-08-24 21:49:00 -04006925/**
James Smart3621a712009-04-06 18:47:14 -04006926 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006927 * @srcp: Source memory pointer.
6928 * @destp: Destination memory pointer.
6929 * @cnt: Number of words required to be copied.
6930 *
6931 * This function is used for copying data between driver memory
6932 * and the SLI memory. This function also changes the endianness
6933 * of each word if native endianness is different from SLI
6934 * endianness. This function can be called with or without
6935 * lock.
6936 **/
dea31012005-04-17 16:05:31 -05006937void
6938lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6939{
6940 uint32_t *src = srcp;
6941 uint32_t *dest = destp;
6942 uint32_t ldata;
6943 int i;
6944
6945 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6946 ldata = *src;
6947 ldata = le32_to_cpu(ldata);
6948 *dest = ldata;
6949 src++;
6950 dest++;
6951 }
6952}
6953
James Smarte59058c2008-08-24 21:49:00 -04006954
6955/**
James Smarta0c87cb2009-07-19 10:01:10 -04006956 * lpfc_sli_bemem_bcopy - SLI memory copy function
6957 * @srcp: Source memory pointer.
6958 * @destp: Destination memory pointer.
6959 * @cnt: Number of words required to be copied.
6960 *
6961 * This function is used for copying data between a data structure
6962 * with big endian representation to local endianness.
6963 * This function can be called with or without lock.
6964 **/
6965void
6966lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6967{
6968 uint32_t *src = srcp;
6969 uint32_t *dest = destp;
6970 uint32_t ldata;
6971 int i;
6972
6973 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6974 ldata = *src;
6975 ldata = be32_to_cpu(ldata);
6976 *dest = ldata;
6977 src++;
6978 dest++;
6979 }
6980}
6981
6982/**
James Smart3621a712009-04-06 18:47:14 -04006983 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006984 * @phba: Pointer to HBA context object.
6985 * @pring: Pointer to driver SLI ring object.
6986 * @mp: Pointer to driver buffer object.
6987 *
6988 * This function is called with no lock held.
6989 * It always return zero after adding the buffer to the postbufq
6990 * buffer list.
6991 **/
dea31012005-04-17 16:05:31 -05006992int
James Smart2e0fef82007-06-17 19:56:36 -05006993lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6994 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006995{
6996 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6997 later */
James Smart2e0fef82007-06-17 19:56:36 -05006998 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006999 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007000 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007001 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007002 return 0;
7003}
7004
James Smarte59058c2008-08-24 21:49:00 -04007005/**
James Smart3621a712009-04-06 18:47:14 -04007006 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007007 * @phba: Pointer to HBA context object.
7008 *
7009 * When HBQ is enabled, buffers are searched based on tags. This function
7010 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7011 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7012 * does not conflict with tags of buffer posted for unsolicited events.
7013 * The function returns the allocated tag. The function is called with
7014 * no locks held.
7015 **/
James Smart76bb24e2007-10-27 13:38:00 -04007016uint32_t
7017lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7018{
7019 spin_lock_irq(&phba->hbalock);
7020 phba->buffer_tag_count++;
7021 /*
7022 * Always set the QUE_BUFTAG_BIT to distiguish between
7023 * a tag assigned by HBQ.
7024 */
7025 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7026 spin_unlock_irq(&phba->hbalock);
7027 return phba->buffer_tag_count;
7028}
7029
James Smarte59058c2008-08-24 21:49:00 -04007030/**
James Smart3621a712009-04-06 18:47:14 -04007031 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007032 * @phba: Pointer to HBA context object.
7033 * @pring: Pointer to driver SLI ring object.
7034 * @tag: Buffer tag.
7035 *
7036 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7037 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7038 * iocb is posted to the response ring with the tag of the buffer.
7039 * This function searches the pring->postbufq list using the tag
7040 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7041 * iocb. If the buffer is found then lpfc_dmabuf object of the
7042 * buffer is returned to the caller else NULL is returned.
7043 * This function is called with no lock held.
7044 **/
James Smart76bb24e2007-10-27 13:38:00 -04007045struct lpfc_dmabuf *
7046lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7047 uint32_t tag)
7048{
7049 struct lpfc_dmabuf *mp, *next_mp;
7050 struct list_head *slp = &pring->postbufq;
7051
7052 /* Search postbufq, from the begining, looking for a match on tag */
7053 spin_lock_irq(&phba->hbalock);
7054 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7055 if (mp->buffer_tag == tag) {
7056 list_del_init(&mp->list);
7057 pring->postbufq_cnt--;
7058 spin_unlock_irq(&phba->hbalock);
7059 return mp;
7060 }
7061 }
7062
7063 spin_unlock_irq(&phba->hbalock);
7064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007065 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007066 "ring %d Data x%lx x%p x%p x%x\n",
7067 pring->ringno, (unsigned long) tag,
7068 slp->next, slp->prev, pring->postbufq_cnt);
7069
7070 return NULL;
7071}
dea31012005-04-17 16:05:31 -05007072
James Smarte59058c2008-08-24 21:49:00 -04007073/**
James Smart3621a712009-04-06 18:47:14 -04007074 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007075 * @phba: Pointer to HBA context object.
7076 * @pring: Pointer to driver SLI ring object.
7077 * @phys: DMA address of the buffer.
7078 *
7079 * This function searches the buffer list using the dma_address
7080 * of unsolicited event to find the driver's lpfc_dmabuf object
7081 * corresponding to the dma_address. The function returns the
7082 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7083 * This function is called by the ct and els unsolicited event
7084 * handlers to get the buffer associated with the unsolicited
7085 * event.
7086 *
7087 * This function is called with no lock held.
7088 **/
dea31012005-04-17 16:05:31 -05007089struct lpfc_dmabuf *
7090lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7091 dma_addr_t phys)
7092{
7093 struct lpfc_dmabuf *mp, *next_mp;
7094 struct list_head *slp = &pring->postbufq;
7095
7096 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007097 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007098 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7099 if (mp->phys == phys) {
7100 list_del_init(&mp->list);
7101 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007102 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007103 return mp;
7104 }
7105 }
7106
James Smart2e0fef82007-06-17 19:56:36 -05007107 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007109 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007110 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007111 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007112 slp->next, slp->prev, pring->postbufq_cnt);
7113 return NULL;
7114}
7115
James Smarte59058c2008-08-24 21:49:00 -04007116/**
James Smart3621a712009-04-06 18:47:14 -04007117 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007118 * @phba: Pointer to HBA context object.
7119 * @cmdiocb: Pointer to driver command iocb object.
7120 * @rspiocb: Pointer to driver response iocb object.
7121 *
7122 * This function is the completion handler for the abort iocbs for
7123 * ELS commands. This function is called from the ELS ring event
7124 * handler with no lock held. This function frees memory resources
7125 * associated with the abort iocb.
7126 **/
dea31012005-04-17 16:05:31 -05007127static void
James Smart2e0fef82007-06-17 19:56:36 -05007128lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7129 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007130{
James Smart2e0fef82007-06-17 19:56:36 -05007131 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007132 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007133 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007134 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7135
7136 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007137
7138 if (irsp->ulpStatus) {
7139 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7140 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7141
James Smart2e0fef82007-06-17 19:56:36 -05007142 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007143 if (phba->sli_rev < LPFC_SLI_REV4) {
7144 if (abort_iotag != 0 &&
7145 abort_iotag <= phba->sli.last_iotag)
7146 abort_iocb =
7147 phba->sli.iocbq_lookup[abort_iotag];
7148 } else
7149 /* For sli4 the abort_tag is the XRI,
7150 * so the abort routine puts the iotag of the iocb
7151 * being aborted in the context field of the abort
7152 * IOCB.
7153 */
7154 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007155
James Smart2680eea2007-04-25 09:52:55 -04007156 /*
James Smart58da1ff2008-04-07 10:15:56 -04007157 * If the iocb is not found in Firmware queue the iocb
7158 * might have completed already. Do not free it again.
7159 */
James Smart9b379602008-04-07 10:16:00 -04007160 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007161 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7162 spin_unlock_irq(&phba->hbalock);
7163 lpfc_sli_release_iocbq(phba, cmdiocb);
7164 return;
7165 }
7166 /* For SLI4 the ulpContext field for abort IOCB
7167 * holds the iotag of the IOCB being aborted so
7168 * the local abort_context needs to be reset to
7169 * match the aborted IOCBs ulpContext.
7170 */
7171 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7172 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007173 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007174
7175 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7176 "0327 Cannot abort els iocb %p "
7177 "with tag %x context %x, abort status %x, "
7178 "abort code %x\n",
7179 abort_iocb, abort_iotag, abort_context,
7180 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007181 /*
James Smart2680eea2007-04-25 09:52:55 -04007182 * make sure we have the right iocbq before taking it
7183 * off the txcmplq and try to call completion routine.
7184 */
James Smart2e0fef82007-06-17 19:56:36 -05007185 if (!abort_iocb ||
7186 abort_iocb->iocb.ulpContext != abort_context ||
7187 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7188 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007189 else if (phba->sli_rev < LPFC_SLI_REV4) {
7190 /*
7191 * leave the SLI4 aborted command on the txcmplq
7192 * list and the command complete WCQE's XB bit
7193 * will tell whether the SGL (XRI) can be released
7194 * immediately or to the aborted SGL list for the
7195 * following abort XRI from the HBA.
7196 */
James Smart92d7f7b2007-06-17 19:56:38 -05007197 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007198 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7199 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7200 pring->txcmplq_cnt--;
7201 }
James Smart2680eea2007-04-25 09:52:55 -04007202
James Smart0ff10d42008-01-11 01:52:36 -05007203 /* Firmware could still be in progress of DMAing
7204 * payload, so don't free data buffer till after
7205 * a hbeat.
7206 */
7207 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007208 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007209 spin_unlock_irq(&phba->hbalock);
7210
James Smart92d7f7b2007-06-17 19:56:38 -05007211 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007212 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007213 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007214 } else
7215 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007216 }
7217
James Bottomley604a3e32005-10-29 10:28:33 -05007218 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007219 return;
7220}
7221
James Smarte59058c2008-08-24 21:49:00 -04007222/**
James Smart3621a712009-04-06 18:47:14 -04007223 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007224 * @phba: Pointer to HBA context object.
7225 * @cmdiocb: Pointer to driver command iocb object.
7226 * @rspiocb: Pointer to driver response iocb object.
7227 *
7228 * The function is called from SLI ring event handler with no
7229 * lock held. This function is the completion handler for ELS commands
7230 * which are aborted. The function frees memory resources used for
7231 * the aborted ELS commands.
7232 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007233static void
7234lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7235 struct lpfc_iocbq *rspiocb)
7236{
7237 IOCB_t *irsp = &rspiocb->iocb;
7238
7239 /* ELS cmd tag <ulpIoTag> completes */
7240 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007241 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007242 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007243 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007244 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007245 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7246 lpfc_ct_free_iocb(phba, cmdiocb);
7247 else
7248 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007249 return;
7250}
7251
James Smarte59058c2008-08-24 21:49:00 -04007252/**
James Smart3621a712009-04-06 18:47:14 -04007253 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007254 * @phba: Pointer to HBA context object.
7255 * @pring: Pointer to driver SLI ring object.
7256 * @cmdiocb: Pointer to driver command iocb object.
7257 *
7258 * This function issues an abort iocb for the provided command
7259 * iocb. This function is called with hbalock held.
7260 * The function returns 0 when it fails due to memory allocation
7261 * failure or when the command iocb is an abort request.
7262 **/
dea31012005-04-17 16:05:31 -05007263int
James Smart2e0fef82007-06-17 19:56:36 -05007264lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7265 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007266{
James Smart2e0fef82007-06-17 19:56:36 -05007267 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007268 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007269 IOCB_t *icmd = NULL;
7270 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007271 int retval = IOCB_ERROR;
7272
James Smart92d7f7b2007-06-17 19:56:38 -05007273 /*
7274 * There are certain command types we don't want to abort. And we
7275 * don't want to abort commands that are already in the process of
7276 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007277 */
7278 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007279 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007280 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7281 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007282 return 0;
7283
James Smart858c9f62007-06-17 19:56:39 -05007284 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7285 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007286 */
James Smart858c9f62007-06-17 19:56:39 -05007287 if ((vport->load_flag & FC_UNLOADING) &&
7288 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007289 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7290 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7291 else
7292 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007293 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007294 }
dea31012005-04-17 16:05:31 -05007295
7296 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007297 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007298 if (abtsiocbp == NULL)
7299 return 0;
dea31012005-04-17 16:05:31 -05007300
James Smart07951072007-04-25 09:51:38 -04007301 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007302 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007303 */
7304 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7305
dea31012005-04-17 16:05:31 -05007306 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007307 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7308 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007309 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007310 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007311 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7312 }
James Smartda0436e2009-05-22 14:51:39 -04007313 else
7314 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007315 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007316 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007317
James Smart5ffc2662009-11-18 15:39:44 -05007318 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7319 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007320 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7321 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007322
James Smart2e0fef82007-06-17 19:56:36 -05007323 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007324 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7325 else
7326 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7327
7328 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007329
James Smarte8b62012007-08-02 11:10:09 -04007330 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7331 "0339 Abort xri x%x, original iotag x%x, "
7332 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007333 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007334 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007335 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007336 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007337
James Smartd7c255b2008-08-24 21:50:00 -04007338 if (retval)
7339 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007340abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007341 /*
7342 * Caller to this routine should check for IOCB_ERROR
7343 * and handle it properly. This routine no longer removes
7344 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007345 */
James Smart2e0fef82007-06-17 19:56:36 -05007346 return retval;
dea31012005-04-17 16:05:31 -05007347}
7348
James Smarte59058c2008-08-24 21:49:00 -04007349/**
James Smart3621a712009-04-06 18:47:14 -04007350 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007351 * @iocbq: Pointer to driver iocb object.
7352 * @vport: Pointer to driver virtual port object.
7353 * @tgt_id: SCSI ID of the target.
7354 * @lun_id: LUN ID of the scsi device.
7355 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7356 *
James Smart3621a712009-04-06 18:47:14 -04007357 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007358 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7359 * 0 if the filtering criteria is met for the given iocb and will return
7360 * 1 if the filtering criteria is not met.
7361 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7362 * given iocb is for the SCSI device specified by vport, tgt_id and
7363 * lun_id parameter.
7364 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7365 * given iocb is for the SCSI target specified by vport and tgt_id
7366 * parameters.
7367 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7368 * given iocb is for the SCSI host associated with the given vport.
7369 * This function is called with no locks held.
7370 **/
dea31012005-04-17 16:05:31 -05007371static int
James Smart51ef4c22007-08-02 11:10:31 -04007372lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7373 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007374 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007375{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007376 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007377 int rc = 1;
7378
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007379 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7380 return rc;
7381
James Smart51ef4c22007-08-02 11:10:31 -04007382 if (iocbq->vport != vport)
7383 return rc;
7384
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007385 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007386
James Smart495a7142008-06-14 22:52:59 -04007387 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007388 return rc;
7389
7390 switch (ctx_cmd) {
7391 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007392 if ((lpfc_cmd->rdata->pnode) &&
7393 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7394 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007395 rc = 0;
7396 break;
7397 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007398 if ((lpfc_cmd->rdata->pnode) &&
7399 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007400 rc = 0;
7401 break;
dea31012005-04-17 16:05:31 -05007402 case LPFC_CTX_HOST:
7403 rc = 0;
7404 break;
7405 default:
7406 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007407 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007408 break;
7409 }
7410
7411 return rc;
7412}
7413
James Smarte59058c2008-08-24 21:49:00 -04007414/**
James Smart3621a712009-04-06 18:47:14 -04007415 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007416 * @vport: Pointer to virtual port.
7417 * @tgt_id: SCSI ID of the target.
7418 * @lun_id: LUN ID of the scsi device.
7419 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7420 *
7421 * This function returns number of FCP commands pending for the vport.
7422 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7423 * commands pending on the vport associated with SCSI device specified
7424 * by tgt_id and lun_id parameters.
7425 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7426 * commands pending on the vport associated with SCSI target specified
7427 * by tgt_id parameter.
7428 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7429 * commands pending on the vport.
7430 * This function returns the number of iocbs which satisfy the filter.
7431 * This function is called without any lock held.
7432 **/
dea31012005-04-17 16:05:31 -05007433int
James Smart51ef4c22007-08-02 11:10:31 -04007434lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7435 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007436{
James Smart51ef4c22007-08-02 11:10:31 -04007437 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007438 struct lpfc_iocbq *iocbq;
7439 int sum, i;
dea31012005-04-17 16:05:31 -05007440
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007441 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7442 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007443
James Smart51ef4c22007-08-02 11:10:31 -04007444 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7445 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007446 sum++;
dea31012005-04-17 16:05:31 -05007447 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007448
dea31012005-04-17 16:05:31 -05007449 return sum;
7450}
7451
James Smarte59058c2008-08-24 21:49:00 -04007452/**
James Smart3621a712009-04-06 18:47:14 -04007453 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007454 * @phba: Pointer to HBA context object
7455 * @cmdiocb: Pointer to command iocb object.
7456 * @rspiocb: Pointer to response iocb object.
7457 *
7458 * This function is called when an aborted FCP iocb completes. This
7459 * function is called by the ring event handler with no lock held.
7460 * This function frees the iocb.
7461 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007462void
James Smart2e0fef82007-06-17 19:56:36 -05007463lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7464 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007465{
James Bottomley604a3e32005-10-29 10:28:33 -05007466 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007467 return;
7468}
7469
James Smarte59058c2008-08-24 21:49:00 -04007470/**
James Smart3621a712009-04-06 18:47:14 -04007471 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007472 * @vport: Pointer to virtual port.
7473 * @pring: Pointer to driver SLI ring object.
7474 * @tgt_id: SCSI ID of the target.
7475 * @lun_id: LUN ID of the scsi device.
7476 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7477 *
7478 * This function sends an abort command for every SCSI command
7479 * associated with the given virtual port pending on the ring
7480 * filtered by lpfc_sli_validate_fcp_iocb function.
7481 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7482 * FCP iocbs associated with lun specified by tgt_id and lun_id
7483 * parameters
7484 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7485 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7486 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7487 * FCP iocbs associated with virtual port.
7488 * This function returns number of iocbs it failed to abort.
7489 * This function is called with no locks held.
7490 **/
dea31012005-04-17 16:05:31 -05007491int
James Smart51ef4c22007-08-02 11:10:31 -04007492lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7493 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007494{
James Smart51ef4c22007-08-02 11:10:31 -04007495 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007496 struct lpfc_iocbq *iocbq;
7497 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007498 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007499 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007500 int i;
dea31012005-04-17 16:05:31 -05007501
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007502 for (i = 1; i <= phba->sli.last_iotag; i++) {
7503 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007504
James Smart51ef4c22007-08-02 11:10:31 -04007505 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007506 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007507 continue;
7508
7509 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007510 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007511 if (abtsiocb == NULL) {
7512 errcnt++;
7513 continue;
7514 }
dea31012005-04-17 16:05:31 -05007515
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007516 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007517 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7518 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007519 if (phba->sli_rev == LPFC_SLI_REV4)
7520 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7521 else
7522 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007523 abtsiocb->iocb.ulpLe = 1;
7524 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007525 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007526
James Smart5ffc2662009-11-18 15:39:44 -05007527 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7528 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007529 if (iocbq->iocb_flag & LPFC_IO_FCP)
7530 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007531
James Smart2e0fef82007-06-17 19:56:36 -05007532 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007533 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7534 else
7535 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7536
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007537 /* Setup callback routine and issue the command. */
7538 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007539 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7540 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007541 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007542 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007543 errcnt++;
7544 continue;
7545 }
7546 }
7547
7548 return errcnt;
7549}
7550
James Smarte59058c2008-08-24 21:49:00 -04007551/**
James Smart3621a712009-04-06 18:47:14 -04007552 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007553 * @phba: Pointer to HBA context object.
7554 * @cmdiocbq: Pointer to command iocb.
7555 * @rspiocbq: Pointer to response iocb.
7556 *
7557 * This function is the completion handler for iocbs issued using
7558 * lpfc_sli_issue_iocb_wait function. This function is called by the
7559 * ring event handler function without any lock held. This function
7560 * can be called from both worker thread context and interrupt
7561 * context. This function also can be called from other thread which
7562 * cleans up the SLI layer objects.
7563 * This function copy the contents of the response iocb to the
7564 * response iocb memory object provided by the caller of
7565 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7566 * sleeps for the iocb completion.
7567 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007568static void
7569lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7570 struct lpfc_iocbq *cmdiocbq,
7571 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007572{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007573 wait_queue_head_t *pdone_q;
7574 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007575 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007576
James Smart2e0fef82007-06-17 19:56:36 -05007577 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007578 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7579 if (cmdiocbq->context2 && rspiocbq)
7580 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7581 &rspiocbq->iocb, sizeof(IOCB_t));
7582
James Smart0f65ff62010-02-26 14:14:23 -05007583 /* Set the exchange busy flag for task management commands */
7584 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7585 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7586 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7587 cur_iocbq);
7588 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7589 }
7590
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007591 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007592 if (pdone_q)
7593 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007594 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007595 return;
7596}
7597
James Smarte59058c2008-08-24 21:49:00 -04007598/**
James Smartd11e31d2009-06-10 17:23:06 -04007599 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7600 * @phba: Pointer to HBA context object..
7601 * @piocbq: Pointer to command iocb.
7602 * @flag: Flag to test.
7603 *
7604 * This routine grabs the hbalock and then test the iocb_flag to
7605 * see if the passed in flag is set.
7606 * Returns:
7607 * 1 if flag is set.
7608 * 0 if flag is not set.
7609 **/
7610static int
7611lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7612 struct lpfc_iocbq *piocbq, uint32_t flag)
7613{
7614 unsigned long iflags;
7615 int ret;
7616
7617 spin_lock_irqsave(&phba->hbalock, iflags);
7618 ret = piocbq->iocb_flag & flag;
7619 spin_unlock_irqrestore(&phba->hbalock, iflags);
7620 return ret;
7621
7622}
7623
7624/**
James Smart3621a712009-04-06 18:47:14 -04007625 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007626 * @phba: Pointer to HBA context object..
7627 * @pring: Pointer to sli ring.
7628 * @piocb: Pointer to command iocb.
7629 * @prspiocbq: Pointer to response iocb.
7630 * @timeout: Timeout in number of seconds.
7631 *
7632 * This function issues the iocb to firmware and waits for the
7633 * iocb to complete. If the iocb command is not
7634 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7635 * Caller should not free the iocb resources if this function
7636 * returns IOCB_TIMEDOUT.
7637 * The function waits for the iocb completion using an
7638 * non-interruptible wait.
7639 * This function will sleep while waiting for iocb completion.
7640 * So, this function should not be called from any context which
7641 * does not allow sleeping. Due to the same reason, this function
7642 * cannot be called with interrupt disabled.
7643 * This function assumes that the iocb completions occur while
7644 * this function sleep. So, this function cannot be called from
7645 * the thread which process iocb completion for this ring.
7646 * This function clears the iocb_flag of the iocb object before
7647 * issuing the iocb and the iocb completion handler sets this
7648 * flag and wakes this thread when the iocb completes.
7649 * The contents of the response iocb will be copied to prspiocbq
7650 * by the completion handler when the command completes.
7651 * This function returns IOCB_SUCCESS when success.
7652 * This function is called with no lock held.
7653 **/
dea31012005-04-17 16:05:31 -05007654int
James Smart2e0fef82007-06-17 19:56:36 -05007655lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007656 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007657 struct lpfc_iocbq *piocb,
7658 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007659 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007660{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007661 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007662 long timeleft, timeout_req = 0;
7663 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007664 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04007665 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05007666 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007667 * If the caller has provided a response iocbq buffer, then context2
7668 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007669 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007670 if (prspiocbq) {
7671 if (piocb->context2)
7672 return IOCB_ERROR;
7673 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007674 }
7675
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007676 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7677 piocb->context_un.wait_queue = &done_q;
7678 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007679
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007680 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7681 creg_val = readl(phba->HCregaddr);
7682 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7683 writel(creg_val, phba->HCregaddr);
7684 readl(phba->HCregaddr); /* flush */
7685 }
7686
James Smart2a9bf3d2010-06-07 15:24:45 -04007687 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7688 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007689 if (retval == IOCB_SUCCESS) {
7690 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007691 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007692 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007693 timeout_req);
dea31012005-04-17 16:05:31 -05007694
James Smart7054a602007-04-25 09:52:34 -04007695 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7696 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007697 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007698 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007699 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007700 "0338 IOCB wait timeout error - no "
7701 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007702 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007703 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007705 "0330 IOCB wake NOT set, "
7706 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007707 timeout, (timeleft / jiffies));
7708 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007709 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007710 } else if (retval == IOCB_BUSY) {
7711 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7712 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7713 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7714 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007715 } else {
7716 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007717 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007718 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007719 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007720 }
7721
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007722 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7723 creg_val = readl(phba->HCregaddr);
7724 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7725 writel(creg_val, phba->HCregaddr);
7726 readl(phba->HCregaddr); /* flush */
7727 }
7728
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007729 if (prspiocbq)
7730 piocb->context2 = NULL;
7731
7732 piocb->context_un.wait_queue = NULL;
7733 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007734 return retval;
7735}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007736
James Smarte59058c2008-08-24 21:49:00 -04007737/**
James Smart3621a712009-04-06 18:47:14 -04007738 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007739 * @phba: Pointer to HBA context object.
7740 * @pmboxq: Pointer to driver mailbox object.
7741 * @timeout: Timeout in number of seconds.
7742 *
7743 * This function issues the mailbox to firmware and waits for the
7744 * mailbox command to complete. If the mailbox command is not
7745 * completed within timeout seconds, it returns MBX_TIMEOUT.
7746 * The function waits for the mailbox completion using an
7747 * interruptible wait. If the thread is woken up due to a
7748 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7749 * should not free the mailbox resources, if this function returns
7750 * MBX_TIMEOUT.
7751 * This function will sleep while waiting for mailbox completion.
7752 * So, this function should not be called from any context which
7753 * does not allow sleeping. Due to the same reason, this function
7754 * cannot be called with interrupt disabled.
7755 * This function assumes that the mailbox completion occurs while
7756 * this function sleep. So, this function cannot be called from
7757 * the worker thread which processes mailbox completion.
7758 * This function is called in the context of HBA management
7759 * applications.
7760 * This function returns MBX_SUCCESS when successful.
7761 * This function is called with no lock held.
7762 **/
dea31012005-04-17 16:05:31 -05007763int
James Smart2e0fef82007-06-17 19:56:36 -05007764lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007765 uint32_t timeout)
7766{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007767 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007768 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007769 unsigned long flag;
dea31012005-04-17 16:05:31 -05007770
7771 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007772 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007773 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007774
James Smart495a7142008-06-14 22:52:59 -04007775 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007776 /* setup wake call as IOCB callback */
7777 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7778 /* setup context field to pass wait_queue pointer to wake function */
7779 pmboxq->context1 = &done_q;
7780
dea31012005-04-17 16:05:31 -05007781 /* now issue the command */
7782 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7783
7784 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007785 wait_event_interruptible_timeout(done_q,
7786 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7787 timeout * HZ);
7788
James Smart858c9f62007-06-17 19:56:39 -05007789 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007790 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007791 /*
7792 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7793 * else do not free the resources.
7794 */
James Smartd7c47992010-06-08 18:31:54 -04007795 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05007796 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04007797 lpfc_sli4_swap_str(phba, pmboxq);
7798 } else {
James Smart7054a602007-04-25 09:52:34 -04007799 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007800 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7801 }
7802 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007803 }
7804
dea31012005-04-17 16:05:31 -05007805 return retval;
7806}
7807
James Smarte59058c2008-08-24 21:49:00 -04007808/**
James Smart3772a992009-05-22 14:50:54 -04007809 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007810 * @phba: Pointer to HBA context.
7811 *
James Smart3772a992009-05-22 14:50:54 -04007812 * This function is called to shutdown the driver's mailbox sub-system.
7813 * It first marks the mailbox sub-system is in a block state to prevent
7814 * the asynchronous mailbox command from issued off the pending mailbox
7815 * command queue. If the mailbox command sub-system shutdown is due to
7816 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7817 * the mailbox sub-system flush routine to forcefully bring down the
7818 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7819 * as with offline or HBA function reset), this routine will wait for the
7820 * outstanding mailbox command to complete before invoking the mailbox
7821 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007822 **/
James Smart3772a992009-05-22 14:50:54 -04007823void
7824lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007825{
James Smart3772a992009-05-22 14:50:54 -04007826 struct lpfc_sli *psli = &phba->sli;
7827 uint8_t actcmd = MBX_HEARTBEAT;
7828 unsigned long timeout;
7829
7830 spin_lock_irq(&phba->hbalock);
7831 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7832 spin_unlock_irq(&phba->hbalock);
7833
7834 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7835 spin_lock_irq(&phba->hbalock);
7836 if (phba->sli.mbox_active)
7837 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7838 spin_unlock_irq(&phba->hbalock);
7839 /* Determine how long we might wait for the active mailbox
7840 * command to be gracefully completed by firmware.
7841 */
7842 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7843 1000) + jiffies;
7844 while (phba->sli.mbox_active) {
7845 /* Check active mailbox complete status every 2ms */
7846 msleep(2);
7847 if (time_after(jiffies, timeout))
7848 /* Timeout, let the mailbox flush routine to
7849 * forcefully release active mailbox command
7850 */
7851 break;
7852 }
7853 }
7854 lpfc_sli_mbox_sys_flush(phba);
7855}
7856
7857/**
7858 * lpfc_sli_eratt_read - read sli-3 error attention events
7859 * @phba: Pointer to HBA context.
7860 *
7861 * This function is called to read the SLI3 device error attention registers
7862 * for possible error attention events. The caller must hold the hostlock
7863 * with spin_lock_irq().
7864 *
7865 * This fucntion returns 1 when there is Error Attention in the Host Attention
7866 * Register and returns 0 otherwise.
7867 **/
7868static int
7869lpfc_sli_eratt_read(struct lpfc_hba *phba)
7870{
James Smarted957682007-06-17 19:56:37 -05007871 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007872
James Smart3772a992009-05-22 14:50:54 -04007873 /* Read chip Host Attention (HA) register */
7874 ha_copy = readl(phba->HAregaddr);
7875 if (ha_copy & HA_ERATT) {
7876 /* Read host status register to retrieve error event */
7877 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007878
James Smart3772a992009-05-22 14:50:54 -04007879 /* Check if there is a deferred error condition is active */
7880 if ((HS_FFER1 & phba->work_hs) &&
7881 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7882 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007883 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007884 /* Clear all interrupt enable conditions */
7885 writel(0, phba->HCregaddr);
7886 readl(phba->HCregaddr);
7887 }
7888
7889 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007890 phba->work_ha |= HA_ERATT;
7891 /* Indicate polling handles this ERATT */
7892 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007893 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007894 }
James Smart3772a992009-05-22 14:50:54 -04007895 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007896}
7897
James Smarte59058c2008-08-24 21:49:00 -04007898/**
James Smartda0436e2009-05-22 14:51:39 -04007899 * lpfc_sli4_eratt_read - read sli-4 error attention events
7900 * @phba: Pointer to HBA context.
7901 *
7902 * This function is called to read the SLI4 device error attention registers
7903 * for possible error attention events. The caller must hold the hostlock
7904 * with spin_lock_irq().
7905 *
7906 * This fucntion returns 1 when there is Error Attention in the Host Attention
7907 * Register and returns 0 otherwise.
7908 **/
7909static int
7910lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7911{
7912 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007913
7914 /* For now, use the SLI4 device internal unrecoverable error
7915 * registers for error attention. This can be changed later.
7916 */
James Smarta747c9c2009-11-18 15:41:10 -05007917 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7918 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7919 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7920 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7922 "1423 HBA Unrecoverable error: "
7923 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7924 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7925 uerr_sta_lo, uerr_sta_hi,
7926 phba->sli4_hba.ue_mask_lo,
7927 phba->sli4_hba.ue_mask_hi);
7928 phba->work_status[0] = uerr_sta_lo;
7929 phba->work_status[1] = uerr_sta_hi;
7930 /* Set the driver HA work bitmap */
7931 phba->work_ha |= HA_ERATT;
7932 /* Indicate polling handles this ERATT */
7933 phba->hba_flag |= HBA_ERATT_HANDLED;
7934 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007935 }
7936 return 0;
7937}
7938
7939/**
James Smart3621a712009-04-06 18:47:14 -04007940 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007941 * @phba: Pointer to HBA context.
7942 *
James Smart3772a992009-05-22 14:50:54 -04007943 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007944 * error attention register bit for error attention events.
7945 *
7946 * This fucntion returns 1 when there is Error Attention in the Host Attention
7947 * Register and returns 0 otherwise.
7948 **/
7949int
7950lpfc_sli_check_eratt(struct lpfc_hba *phba)
7951{
7952 uint32_t ha_copy;
7953
7954 /* If somebody is waiting to handle an eratt, don't process it
7955 * here. The brdkill function will do this.
7956 */
7957 if (phba->link_flag & LS_IGNORE_ERATT)
7958 return 0;
7959
7960 /* Check if interrupt handler handles this ERATT */
7961 spin_lock_irq(&phba->hbalock);
7962 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7963 /* Interrupt handler has handled ERATT */
7964 spin_unlock_irq(&phba->hbalock);
7965 return 0;
7966 }
7967
James Smarta257bf92009-04-06 18:48:10 -04007968 /*
7969 * If there is deferred error attention, do not check for error
7970 * attention
7971 */
7972 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7973 spin_unlock_irq(&phba->hbalock);
7974 return 0;
7975 }
7976
James Smart3772a992009-05-22 14:50:54 -04007977 /* If PCI channel is offline, don't process it */
7978 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007979 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007980 return 0;
7981 }
7982
7983 switch (phba->sli_rev) {
7984 case LPFC_SLI_REV2:
7985 case LPFC_SLI_REV3:
7986 /* Read chip Host Attention (HA) register */
7987 ha_copy = lpfc_sli_eratt_read(phba);
7988 break;
James Smartda0436e2009-05-22 14:51:39 -04007989 case LPFC_SLI_REV4:
7990 /* Read devcie Uncoverable Error (UERR) registers */
7991 ha_copy = lpfc_sli4_eratt_read(phba);
7992 break;
James Smart3772a992009-05-22 14:50:54 -04007993 default:
7994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7995 "0299 Invalid SLI revision (%d)\n",
7996 phba->sli_rev);
7997 ha_copy = 0;
7998 break;
James Smart93996272008-08-24 21:50:30 -04007999 }
8000 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008001
8002 return ha_copy;
8003}
8004
8005/**
8006 * lpfc_intr_state_check - Check device state for interrupt handling
8007 * @phba: Pointer to HBA context.
8008 *
8009 * This inline routine checks whether a device or its PCI slot is in a state
8010 * that the interrupt should be handled.
8011 *
8012 * This function returns 0 if the device or the PCI slot is in a state that
8013 * interrupt should be handled, otherwise -EIO.
8014 */
8015static inline int
8016lpfc_intr_state_check(struct lpfc_hba *phba)
8017{
8018 /* If the pci channel is offline, ignore all the interrupts */
8019 if (unlikely(pci_channel_offline(phba->pcidev)))
8020 return -EIO;
8021
8022 /* Update device level interrupt statistics */
8023 phba->sli.slistat.sli_intr++;
8024
8025 /* Ignore all interrupts during initialization. */
8026 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8027 return -EIO;
8028
James Smart93996272008-08-24 21:50:30 -04008029 return 0;
8030}
8031
8032/**
James Smart3772a992009-05-22 14:50:54 -04008033 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008034 * @irq: Interrupt number.
8035 * @dev_id: The device context pointer.
8036 *
James Smart93996272008-08-24 21:50:30 -04008037 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008038 * service routine when device with SLI-3 interface spec is enabled with
8039 * MSI-X multi-message interrupt mode and there are slow-path events in
8040 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8041 * interrupt mode, this function is called as part of the device-level
8042 * interrupt handler. When the PCI slot is in error recovery or the HBA
8043 * is undergoing initialization, the interrupt handler will not process
8044 * the interrupt. The link attention and ELS ring attention events are
8045 * handled by the worker thread. The interrupt handler signals the worker
8046 * thread and returns for these events. This function is called without
8047 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008048 * structures.
8049 *
8050 * This function returns IRQ_HANDLED when interrupt is handled else it
8051 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008052 **/
dea31012005-04-17 16:05:31 -05008053irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008054lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008055{
James Smart2e0fef82007-06-17 19:56:36 -05008056 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008057 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008058 uint32_t work_ha_copy;
8059 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008060 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008061 uint32_t control;
8062
James Smart92d7f7b2007-06-17 19:56:38 -05008063 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008064 struct lpfc_vport *vport;
8065 struct lpfc_nodelist *ndlp;
8066 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008067 LPFC_MBOXQ_t *pmb;
8068 int rc;
8069
dea31012005-04-17 16:05:31 -05008070 /*
8071 * Get the driver's phba structure from the dev_id and
8072 * assume the HBA is not interrupting.
8073 */
James Smart93996272008-08-24 21:50:30 -04008074 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008075
8076 if (unlikely(!phba))
8077 return IRQ_NONE;
8078
dea31012005-04-17 16:05:31 -05008079 /*
James Smart93996272008-08-24 21:50:30 -04008080 * Stuff needs to be attented to when this function is invoked as an
8081 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008082 */
James Smart93996272008-08-24 21:50:30 -04008083 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008084 /* Check device state for handling interrupt */
8085 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008086 return IRQ_NONE;
8087 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008088 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008089 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008090 /* If somebody is waiting to handle an eratt don't process it
8091 * here. The brdkill function will do this.
8092 */
8093 if (phba->link_flag & LS_IGNORE_ERATT)
8094 ha_copy &= ~HA_ERATT;
8095 /* Check the need for handling ERATT in interrupt handler */
8096 if (ha_copy & HA_ERATT) {
8097 if (phba->hba_flag & HBA_ERATT_HANDLED)
8098 /* ERATT polling has handled ERATT */
8099 ha_copy &= ~HA_ERATT;
8100 else
8101 /* Indicate interrupt handler handles ERATT */
8102 phba->hba_flag |= HBA_ERATT_HANDLED;
8103 }
James Smarta257bf92009-04-06 18:48:10 -04008104
8105 /*
8106 * If there is deferred error attention, do not check for any
8107 * interrupt.
8108 */
8109 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008110 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008111 return IRQ_NONE;
8112 }
8113
James Smart93996272008-08-24 21:50:30 -04008114 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008115 hc_copy = readl(phba->HCregaddr);
8116 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8117 HC_LAINT_ENA | HC_ERINT_ENA),
8118 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008119 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8120 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008121 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008122 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008123 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008124 } else
8125 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008126
dea31012005-04-17 16:05:31 -05008127 work_ha_copy = ha_copy & phba->work_ha_mask;
8128
James Smart93996272008-08-24 21:50:30 -04008129 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008130 if (work_ha_copy & HA_LATT) {
8131 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8132 /*
8133 * Turn off Link Attention interrupts
8134 * until CLEAR_LA done
8135 */
James Smart5b75da22008-12-04 22:39:35 -05008136 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008137 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8138 control = readl(phba->HCregaddr);
8139 control &= ~HC_LAINT_ENA;
8140 writel(control, phba->HCregaddr);
8141 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008142 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008143 }
8144 else
8145 work_ha_copy &= ~HA_LATT;
8146 }
8147
James Smart93996272008-08-24 21:50:30 -04008148 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008149 /*
8150 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8151 * the only slow ring.
8152 */
8153 status = (work_ha_copy &
8154 (HA_RXMASK << (4*LPFC_ELS_RING)));
8155 status >>= (4*LPFC_ELS_RING);
8156 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008157 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008158 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008159
8160 lpfc_debugfs_slow_ring_trc(phba,
8161 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8162 control, status,
8163 (uint32_t)phba->sli.slistat.sli_intr);
8164
James Smart858c9f62007-06-17 19:56:39 -05008165 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008166 lpfc_debugfs_slow_ring_trc(phba,
8167 "ISR Disable ring:"
8168 "pwork:x%x hawork:x%x wait:x%x",
8169 phba->work_ha, work_ha_copy,
8170 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008171 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008172
James Smart858c9f62007-06-17 19:56:39 -05008173 control &=
8174 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008175 writel(control, phba->HCregaddr);
8176 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008177 }
James Smarta58cbd52007-08-02 11:09:43 -04008178 else {
8179 lpfc_debugfs_slow_ring_trc(phba,
8180 "ISR slow ring: pwork:"
8181 "x%x hawork:x%x wait:x%x",
8182 phba->work_ha, work_ha_copy,
8183 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008184 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008185 }
James Smart5b75da22008-12-04 22:39:35 -05008186 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008187 }
8188 }
James Smart5b75da22008-12-04 22:39:35 -05008189 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008190 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008191 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008192 /*
8193 * Check if there is a deferred error condition
8194 * is active
8195 */
8196 if ((HS_FFER1 & phba->work_hs) &&
8197 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8198 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8199 phba->hba_flag |= DEFER_ERATT;
8200 /* Clear all interrupt enable conditions */
8201 writel(0, phba->HCregaddr);
8202 readl(phba->HCregaddr);
8203 }
8204 }
8205
James Smart93996272008-08-24 21:50:30 -04008206 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008207 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008208 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008209 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008210 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008211
8212 /* First check out the status word */
8213 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8214 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008215 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008216 /*
8217 * Stray Mailbox Interrupt, mbxCommand <cmd>
8218 * mbxStatus <status>
8219 */
James Smart09372822008-01-11 01:52:54 -05008220 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008221 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008222 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008223 "Interrupt mbxCommand x%x "
8224 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008225 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008226 pmbox->mbxCommand,
8227 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008228 /* clear mailbox attention bit */
8229 work_ha_copy &= ~HA_MBATT;
8230 } else {
James Smart97eab632008-04-07 10:16:05 -04008231 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008232 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008233 phba->last_completion_time = jiffies;
8234 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008235 if (pmb->mbox_cmpl) {
8236 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8237 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008238 if (pmb->out_ext_byte_len &&
8239 pmb->context2)
8240 lpfc_sli_pcimem_bcopy(
8241 phba->mbox_ext,
8242 pmb->context2,
8243 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008244 }
James Smart09372822008-01-11 01:52:54 -05008245 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8246 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8247
8248 lpfc_debugfs_disc_trc(vport,
8249 LPFC_DISC_TRC_MBOX_VPORT,
8250 "MBOX dflt rpi: : "
8251 "status:x%x rpi:x%x",
8252 (uint32_t)pmbox->mbxStatus,
8253 pmbox->un.varWords[0], 0);
8254
8255 if (!pmbox->mbxStatus) {
8256 mp = (struct lpfc_dmabuf *)
8257 (pmb->context1);
8258 ndlp = (struct lpfc_nodelist *)
8259 pmb->context2;
8260
8261 /* Reg_LOGIN of dflt RPI was
8262 * successful. new lets get
8263 * rid of the RPI using the
8264 * same mbox buffer.
8265 */
8266 lpfc_unreg_login(phba,
8267 vport->vpi,
8268 pmbox->un.varWords[0],
8269 pmb);
8270 pmb->mbox_cmpl =
8271 lpfc_mbx_cmpl_dflt_rpi;
8272 pmb->context1 = mp;
8273 pmb->context2 = ndlp;
8274 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008275 rc = lpfc_sli_issue_mbox(phba,
8276 pmb,
8277 MBX_NOWAIT);
8278 if (rc != MBX_BUSY)
8279 lpfc_printf_log(phba,
8280 KERN_ERR,
8281 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008282 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008283 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008284 if (rc != MBX_NOT_FINISHED)
8285 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008286 }
8287 }
James Smart5b75da22008-12-04 22:39:35 -05008288 spin_lock_irqsave(
8289 &phba->pport->work_port_lock,
8290 iflag);
James Smart09372822008-01-11 01:52:54 -05008291 phba->pport->work_port_events &=
8292 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008293 spin_unlock_irqrestore(
8294 &phba->pport->work_port_lock,
8295 iflag);
James Smart09372822008-01-11 01:52:54 -05008296 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008297 }
James Smart97eab632008-04-07 10:16:05 -04008298 } else
James Smart5b75da22008-12-04 22:39:35 -05008299 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008300
James Smart92d7f7b2007-06-17 19:56:38 -05008301 if ((work_ha_copy & HA_MBATT) &&
8302 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008303send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008304 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008305 do {
8306 rc = lpfc_sli_issue_mbox(phba, NULL,
8307 MBX_NOWAIT);
8308 } while (rc == MBX_NOT_FINISHED);
8309 if (rc != MBX_SUCCESS)
8310 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8311 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008312 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008313 }
8314
James Smart5b75da22008-12-04 22:39:35 -05008315 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008316 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008317 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008318 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008319 }
James Smart93996272008-08-24 21:50:30 -04008320 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008321
James Smart3772a992009-05-22 14:50:54 -04008322} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008323
8324/**
James Smart3772a992009-05-22 14:50:54 -04008325 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008326 * @irq: Interrupt number.
8327 * @dev_id: The device context pointer.
8328 *
8329 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008330 * service routine when device with SLI-3 interface spec is enabled with
8331 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8332 * ring event in the HBA. However, when the device is enabled with either
8333 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8334 * device-level interrupt handler. When the PCI slot is in error recovery
8335 * or the HBA is undergoing initialization, the interrupt handler will not
8336 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8337 * the intrrupt context. This function is called without any lock held.
8338 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008339 *
8340 * This function returns IRQ_HANDLED when interrupt is handled else it
8341 * returns IRQ_NONE.
8342 **/
8343irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008344lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008345{
8346 struct lpfc_hba *phba;
8347 uint32_t ha_copy;
8348 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008349 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008350
8351 /* Get the driver's phba structure from the dev_id and
8352 * assume the HBA is not interrupting.
8353 */
8354 phba = (struct lpfc_hba *) dev_id;
8355
8356 if (unlikely(!phba))
8357 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008358
8359 /*
James Smart93996272008-08-24 21:50:30 -04008360 * Stuff needs to be attented to when this function is invoked as an
8361 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008362 */
James Smart93996272008-08-24 21:50:30 -04008363 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008364 /* Check device state for handling interrupt */
8365 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008366 return IRQ_NONE;
8367 /* Need to read HA REG for FCP ring and other ring events */
8368 ha_copy = readl(phba->HAregaddr);
8369 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008370 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008371 /*
8372 * If there is deferred error attention, do not check for
8373 * any interrupt.
8374 */
8375 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008376 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008377 return IRQ_NONE;
8378 }
James Smart93996272008-08-24 21:50:30 -04008379 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8380 phba->HAregaddr);
8381 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008382 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008383 } else
8384 ha_copy = phba->ha_copy;
8385
8386 /*
8387 * Process all events on FCP ring. Take the optimized path for FCP IO.
8388 */
8389 ha_copy &= ~(phba->work_ha_mask);
8390
8391 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008392 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008393 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008394 lpfc_sli_handle_fast_ring_event(phba,
8395 &phba->sli.ring[LPFC_FCP_RING],
8396 status);
James Smarta4bc3372006-12-02 13:34:16 -05008397
8398 if (phba->cfg_multi_ring_support == 2) {
8399 /*
James Smart93996272008-08-24 21:50:30 -04008400 * Process all events on extra ring. Take the optimized path
8401 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008402 */
James Smart93996272008-08-24 21:50:30 -04008403 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008404 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008405 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008406 lpfc_sli_handle_fast_ring_event(phba,
8407 &phba->sli.ring[LPFC_EXTRA_RING],
8408 status);
8409 }
8410 }
dea31012005-04-17 16:05:31 -05008411 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008412} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008413
James Smart93996272008-08-24 21:50:30 -04008414/**
James Smart3772a992009-05-22 14:50:54 -04008415 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008416 * @irq: Interrupt number.
8417 * @dev_id: The device context pointer.
8418 *
James Smart3772a992009-05-22 14:50:54 -04008419 * This function is the HBA device-level interrupt handler to device with
8420 * SLI-3 interface spec, called from the PCI layer when either MSI or
8421 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8422 * requires driver attention. This function invokes the slow-path interrupt
8423 * attention handling function and fast-path interrupt attention handling
8424 * function in turn to process the relevant HBA attention events. This
8425 * function is called without any lock held. It gets the hbalock to access
8426 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008427 *
8428 * This function returns IRQ_HANDLED when interrupt is handled, else it
8429 * returns IRQ_NONE.
8430 **/
8431irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008432lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008433{
8434 struct lpfc_hba *phba;
8435 irqreturn_t sp_irq_rc, fp_irq_rc;
8436 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008437 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008438
8439 /*
8440 * Get the driver's phba structure from the dev_id and
8441 * assume the HBA is not interrupting.
8442 */
8443 phba = (struct lpfc_hba *) dev_id;
8444
8445 if (unlikely(!phba))
8446 return IRQ_NONE;
8447
James Smart3772a992009-05-22 14:50:54 -04008448 /* Check device state for handling interrupt */
8449 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008450 return IRQ_NONE;
8451
8452 spin_lock(&phba->hbalock);
8453 phba->ha_copy = readl(phba->HAregaddr);
8454 if (unlikely(!phba->ha_copy)) {
8455 spin_unlock(&phba->hbalock);
8456 return IRQ_NONE;
8457 } else if (phba->ha_copy & HA_ERATT) {
8458 if (phba->hba_flag & HBA_ERATT_HANDLED)
8459 /* ERATT polling has handled ERATT */
8460 phba->ha_copy &= ~HA_ERATT;
8461 else
8462 /* Indicate interrupt handler handles ERATT */
8463 phba->hba_flag |= HBA_ERATT_HANDLED;
8464 }
8465
James Smarta257bf92009-04-06 18:48:10 -04008466 /*
8467 * If there is deferred error attention, do not check for any interrupt.
8468 */
8469 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8470 spin_unlock_irq(&phba->hbalock);
8471 return IRQ_NONE;
8472 }
8473
James Smart93996272008-08-24 21:50:30 -04008474 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008475 hc_copy = readl(phba->HCregaddr);
8476 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8477 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8478 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008479 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008480 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008481 readl(phba->HAregaddr); /* flush */
8482 spin_unlock(&phba->hbalock);
8483
8484 /*
8485 * Invokes slow-path host attention interrupt handling as appropriate.
8486 */
8487
8488 /* status of events with mailbox and link attention */
8489 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8490
8491 /* status of events with ELS ring */
8492 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8493 status2 >>= (4*LPFC_ELS_RING);
8494
8495 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008496 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008497 else
8498 sp_irq_rc = IRQ_NONE;
8499
8500 /*
8501 * Invoke fast-path host attention interrupt handling as appropriate.
8502 */
8503
8504 /* status of events with FCP ring */
8505 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8506 status1 >>= (4*LPFC_FCP_RING);
8507
8508 /* status of events with extra ring */
8509 if (phba->cfg_multi_ring_support == 2) {
8510 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8511 status2 >>= (4*LPFC_EXTRA_RING);
8512 } else
8513 status2 = 0;
8514
8515 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008516 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008517 else
8518 fp_irq_rc = IRQ_NONE;
8519
8520 /* Return device-level interrupt handling status */
8521 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008522} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008523
8524/**
8525 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8526 * @phba: pointer to lpfc hba data structure.
8527 *
8528 * This routine is invoked by the worker thread to process all the pending
8529 * SLI4 FCP abort XRI events.
8530 **/
8531void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8532{
8533 struct lpfc_cq_event *cq_event;
8534
8535 /* First, declare the fcp xri abort event has been handled */
8536 spin_lock_irq(&phba->hbalock);
8537 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8538 spin_unlock_irq(&phba->hbalock);
8539 /* Now, handle all the fcp xri abort events */
8540 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8541 /* Get the first event from the head of the event queue */
8542 spin_lock_irq(&phba->hbalock);
8543 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8544 cq_event, struct lpfc_cq_event, list);
8545 spin_unlock_irq(&phba->hbalock);
8546 /* Notify aborted XRI for FCP work queue */
8547 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8548 /* Free the event processed back to the free pool */
8549 lpfc_sli4_cq_event_release(phba, cq_event);
8550 }
8551}
8552
8553/**
8554 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8555 * @phba: pointer to lpfc hba data structure.
8556 *
8557 * This routine is invoked by the worker thread to process all the pending
8558 * SLI4 els abort xri events.
8559 **/
8560void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8561{
8562 struct lpfc_cq_event *cq_event;
8563
8564 /* First, declare the els xri abort event has been handled */
8565 spin_lock_irq(&phba->hbalock);
8566 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8567 spin_unlock_irq(&phba->hbalock);
8568 /* Now, handle all the els xri abort events */
8569 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8570 /* Get the first event from the head of the event queue */
8571 spin_lock_irq(&phba->hbalock);
8572 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8573 cq_event, struct lpfc_cq_event, list);
8574 spin_unlock_irq(&phba->hbalock);
8575 /* Notify aborted XRI for ELS work queue */
8576 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8577 /* Free the event processed back to the free pool */
8578 lpfc_sli4_cq_event_release(phba, cq_event);
8579 }
8580}
8581
James Smart341af102010-01-26 23:07:37 -05008582/**
8583 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8584 * @phba: pointer to lpfc hba data structure
8585 * @pIocbIn: pointer to the rspiocbq
8586 * @pIocbOut: pointer to the cmdiocbq
8587 * @wcqe: pointer to the complete wcqe
8588 *
8589 * This routine transfers the fields of a command iocbq to a response iocbq
8590 * by copying all the IOCB fields from command iocbq and transferring the
8591 * completion status information from the complete wcqe.
8592 **/
James Smart4f774512009-05-22 14:52:35 -04008593static void
James Smart341af102010-01-26 23:07:37 -05008594lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8595 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008596 struct lpfc_iocbq *pIocbOut,
8597 struct lpfc_wcqe_complete *wcqe)
8598{
James Smart341af102010-01-26 23:07:37 -05008599 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008600 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8601
8602 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8603 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008604 /* Map WCQE parameters into irspiocb parameters */
8605 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8606 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8607 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8608 pIocbIn->iocb.un.fcpi.fcpi_parm =
8609 pIocbOut->iocb.un.fcpi.fcpi_parm -
8610 wcqe->total_data_placed;
8611 else
8612 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008613 else {
James Smart4f774512009-05-22 14:52:35 -04008614 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008615 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8616 }
James Smart341af102010-01-26 23:07:37 -05008617
8618 /* Pick up HBA exchange busy condition */
8619 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8620 spin_lock_irqsave(&phba->hbalock, iflags);
8621 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8622 spin_unlock_irqrestore(&phba->hbalock, iflags);
8623 }
James Smart4f774512009-05-22 14:52:35 -04008624}
8625
8626/**
James Smart45ed1192009-10-02 15:17:02 -04008627 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8628 * @phba: Pointer to HBA context object.
8629 * @wcqe: Pointer to work-queue completion queue entry.
8630 *
8631 * This routine handles an ELS work-queue completion event and construct
8632 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8633 * discovery engine to handle.
8634 *
8635 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8636 **/
8637static struct lpfc_iocbq *
8638lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8639 struct lpfc_iocbq *irspiocbq)
8640{
8641 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8642 struct lpfc_iocbq *cmdiocbq;
8643 struct lpfc_wcqe_complete *wcqe;
8644 unsigned long iflags;
8645
8646 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8647 spin_lock_irqsave(&phba->hbalock, iflags);
8648 pring->stats.iocb_event++;
8649 /* Look up the ELS command IOCB and create pseudo response IOCB */
8650 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8651 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8652 spin_unlock_irqrestore(&phba->hbalock, iflags);
8653
8654 if (unlikely(!cmdiocbq)) {
8655 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8656 "0386 ELS complete with no corresponding "
8657 "cmdiocb: iotag (%d)\n",
8658 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8659 lpfc_sli_release_iocbq(phba, irspiocbq);
8660 return NULL;
8661 }
8662
8663 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008664 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008665
8666 return irspiocbq;
8667}
8668
8669/**
James Smart04c68492009-05-22 14:52:52 -04008670 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8671 * @phba: Pointer to HBA context object.
8672 * @cqe: Pointer to mailbox completion queue entry.
8673 *
8674 * This routine process a mailbox completion queue entry with asynchrous
8675 * event.
8676 *
8677 * Return: true if work posted to worker thread, otherwise false.
8678 **/
8679static bool
8680lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8681{
8682 struct lpfc_cq_event *cq_event;
8683 unsigned long iflags;
8684
8685 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8686 "0392 Async Event: word0:x%x, word1:x%x, "
8687 "word2:x%x, word3:x%x\n", mcqe->word0,
8688 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8689
8690 /* Allocate a new internal CQ_EVENT entry */
8691 cq_event = lpfc_sli4_cq_event_alloc(phba);
8692 if (!cq_event) {
8693 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8694 "0394 Failed to allocate CQ_EVENT entry\n");
8695 return false;
8696 }
8697
8698 /* Move the CQE into an asynchronous event entry */
8699 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8700 spin_lock_irqsave(&phba->hbalock, iflags);
8701 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8702 /* Set the async event flag */
8703 phba->hba_flag |= ASYNC_EVENT;
8704 spin_unlock_irqrestore(&phba->hbalock, iflags);
8705
8706 return true;
8707}
8708
8709/**
8710 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8711 * @phba: Pointer to HBA context object.
8712 * @cqe: Pointer to mailbox completion queue entry.
8713 *
8714 * This routine process a mailbox completion queue entry with mailbox
8715 * completion event.
8716 *
8717 * Return: true if work posted to worker thread, otherwise false.
8718 **/
8719static bool
8720lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8721{
8722 uint32_t mcqe_status;
8723 MAILBOX_t *mbox, *pmbox;
8724 struct lpfc_mqe *mqe;
8725 struct lpfc_vport *vport;
8726 struct lpfc_nodelist *ndlp;
8727 struct lpfc_dmabuf *mp;
8728 unsigned long iflags;
8729 LPFC_MBOXQ_t *pmb;
8730 bool workposted = false;
8731 int rc;
8732
8733 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8734 if (!bf_get(lpfc_trailer_completed, mcqe))
8735 goto out_no_mqe_complete;
8736
8737 /* Get the reference to the active mbox command */
8738 spin_lock_irqsave(&phba->hbalock, iflags);
8739 pmb = phba->sli.mbox_active;
8740 if (unlikely(!pmb)) {
8741 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8742 "1832 No pending MBOX command to handle\n");
8743 spin_unlock_irqrestore(&phba->hbalock, iflags);
8744 goto out_no_mqe_complete;
8745 }
8746 spin_unlock_irqrestore(&phba->hbalock, iflags);
8747 mqe = &pmb->u.mqe;
8748 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8749 mbox = phba->mbox;
8750 vport = pmb->vport;
8751
8752 /* Reset heartbeat timer */
8753 phba->last_completion_time = jiffies;
8754 del_timer(&phba->sli.mbox_tmo);
8755
8756 /* Move mbox data to caller's mailbox region, do endian swapping */
8757 if (pmb->mbox_cmpl && mbox)
8758 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8759 /* Set the mailbox status with SLI4 range 0x4000 */
8760 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8761 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8762 bf_set(lpfc_mqe_status, mqe,
8763 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8764
8765 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8766 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8767 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8768 "MBOX dflt rpi: status:x%x rpi:x%x",
8769 mcqe_status,
8770 pmbox->un.varWords[0], 0);
8771 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8772 mp = (struct lpfc_dmabuf *)(pmb->context1);
8773 ndlp = (struct lpfc_nodelist *)pmb->context2;
8774 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8775 * RID of the PPI using the same mbox buffer.
8776 */
8777 lpfc_unreg_login(phba, vport->vpi,
8778 pmbox->un.varWords[0], pmb);
8779 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8780 pmb->context1 = mp;
8781 pmb->context2 = ndlp;
8782 pmb->vport = vport;
8783 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8784 if (rc != MBX_BUSY)
8785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8786 LOG_SLI, "0385 rc should "
8787 "have been MBX_BUSY\n");
8788 if (rc != MBX_NOT_FINISHED)
8789 goto send_current_mbox;
8790 }
8791 }
8792 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8793 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8794 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8795
8796 /* There is mailbox completion work to do */
8797 spin_lock_irqsave(&phba->hbalock, iflags);
8798 __lpfc_mbox_cmpl_put(phba, pmb);
8799 phba->work_ha |= HA_MBATT;
8800 spin_unlock_irqrestore(&phba->hbalock, iflags);
8801 workposted = true;
8802
8803send_current_mbox:
8804 spin_lock_irqsave(&phba->hbalock, iflags);
8805 /* Release the mailbox command posting token */
8806 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8807 /* Setting active mailbox pointer need to be in sync to flag clear */
8808 phba->sli.mbox_active = NULL;
8809 spin_unlock_irqrestore(&phba->hbalock, iflags);
8810 /* Wake up worker thread to post the next pending mailbox command */
8811 lpfc_worker_wake_up(phba);
8812out_no_mqe_complete:
8813 if (bf_get(lpfc_trailer_consumed, mcqe))
8814 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8815 return workposted;
8816}
8817
8818/**
8819 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8820 * @phba: Pointer to HBA context object.
8821 * @cqe: Pointer to mailbox completion queue entry.
8822 *
8823 * This routine process a mailbox completion queue entry, it invokes the
8824 * proper mailbox complete handling or asynchrous event handling routine
8825 * according to the MCQE's async bit.
8826 *
8827 * Return: true if work posted to worker thread, otherwise false.
8828 **/
8829static bool
8830lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8831{
8832 struct lpfc_mcqe mcqe;
8833 bool workposted;
8834
8835 /* Copy the mailbox MCQE and convert endian order as needed */
8836 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8837
8838 /* Invoke the proper event handling routine */
8839 if (!bf_get(lpfc_trailer_async, &mcqe))
8840 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8841 else
8842 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8843 return workposted;
8844}
8845
8846/**
James Smart4f774512009-05-22 14:52:35 -04008847 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8848 * @phba: Pointer to HBA context object.
8849 * @wcqe: Pointer to work-queue completion queue entry.
8850 *
8851 * This routine handles an ELS work-queue completion event.
8852 *
8853 * Return: true if work posted to worker thread, otherwise false.
8854 **/
8855static bool
8856lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8857 struct lpfc_wcqe_complete *wcqe)
8858{
James Smart4f774512009-05-22 14:52:35 -04008859 struct lpfc_iocbq *irspiocbq;
8860 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04008861 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04008862
James Smart45ed1192009-10-02 15:17:02 -04008863 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008864 irspiocbq = lpfc_sli_get_iocbq(phba);
8865 if (!irspiocbq) {
8866 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04008867 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8868 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8869 pring->txq_cnt, phba->iocb_cnt,
8870 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8871 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04008872 return false;
James Smart4f774512009-05-22 14:52:35 -04008873 }
James Smart4f774512009-05-22 14:52:35 -04008874
James Smart45ed1192009-10-02 15:17:02 -04008875 /* Save off the slow-path queue event for work thread to process */
8876 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008877 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008878 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008879 &phba->sli4_hba.sp_queue_event);
8880 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008881 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008882
James Smart45ed1192009-10-02 15:17:02 -04008883 return true;
James Smart4f774512009-05-22 14:52:35 -04008884}
8885
8886/**
8887 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8888 * @phba: Pointer to HBA context object.
8889 * @wcqe: Pointer to work-queue completion queue entry.
8890 *
8891 * This routine handles slow-path WQ entry comsumed event by invoking the
8892 * proper WQ release routine to the slow-path WQ.
8893 **/
8894static void
8895lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8896 struct lpfc_wcqe_release *wcqe)
8897{
8898 /* Check for the slow-path ELS work queue */
8899 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8900 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8901 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8902 else
8903 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8904 "2579 Slow-path wqe consume event carries "
8905 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8906 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8907 phba->sli4_hba.els_wq->queue_id);
8908}
8909
8910/**
8911 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8912 * @phba: Pointer to HBA context object.
8913 * @cq: Pointer to a WQ completion queue.
8914 * @wcqe: Pointer to work-queue completion queue entry.
8915 *
8916 * This routine handles an XRI abort event.
8917 *
8918 * Return: true if work posted to worker thread, otherwise false.
8919 **/
8920static bool
8921lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8922 struct lpfc_queue *cq,
8923 struct sli4_wcqe_xri_aborted *wcqe)
8924{
8925 bool workposted = false;
8926 struct lpfc_cq_event *cq_event;
8927 unsigned long iflags;
8928
8929 /* Allocate a new internal CQ_EVENT entry */
8930 cq_event = lpfc_sli4_cq_event_alloc(phba);
8931 if (!cq_event) {
8932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8933 "0602 Failed to allocate CQ_EVENT entry\n");
8934 return false;
8935 }
8936
8937 /* Move the CQE into the proper xri abort event list */
8938 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8939 switch (cq->subtype) {
8940 case LPFC_FCP:
8941 spin_lock_irqsave(&phba->hbalock, iflags);
8942 list_add_tail(&cq_event->list,
8943 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8944 /* Set the fcp xri abort event flag */
8945 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8946 spin_unlock_irqrestore(&phba->hbalock, iflags);
8947 workposted = true;
8948 break;
8949 case LPFC_ELS:
8950 spin_lock_irqsave(&phba->hbalock, iflags);
8951 list_add_tail(&cq_event->list,
8952 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8953 /* Set the els xri abort event flag */
8954 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8955 spin_unlock_irqrestore(&phba->hbalock, iflags);
8956 workposted = true;
8957 break;
8958 default:
8959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8960 "0603 Invalid work queue CQE subtype (x%x)\n",
8961 cq->subtype);
8962 workposted = false;
8963 break;
8964 }
8965 return workposted;
8966}
8967
8968/**
James Smart4d9ab992009-10-02 15:16:39 -04008969 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008970 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008971 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008972 *
James Smart4d9ab992009-10-02 15:16:39 -04008973 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008974 *
8975 * Return: true if work posted to worker thread, otherwise false.
8976 **/
8977static bool
James Smart4d9ab992009-10-02 15:16:39 -04008978lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8979{
8980 bool workposted = false;
8981 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8982 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8983 struct hbq_dmabuf *dma_buf;
8984 uint32_t status;
8985 unsigned long iflags;
8986
James Smart4d9ab992009-10-02 15:16:39 -04008987 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8988 goto out;
8989
8990 status = bf_get(lpfc_rcqe_status, rcqe);
8991 switch (status) {
8992 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8994 "2537 Receive Frame Truncated!!\n");
8995 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008996 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008997 spin_lock_irqsave(&phba->hbalock, iflags);
8998 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8999 if (!dma_buf) {
9000 spin_unlock_irqrestore(&phba->hbalock, iflags);
9001 goto out;
9002 }
9003 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9004 /* save off the frame for the word thread to process */
9005 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009006 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009007 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009008 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009009 spin_unlock_irqrestore(&phba->hbalock, iflags);
9010 workposted = true;
9011 break;
9012 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9013 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9014 /* Post more buffers if possible */
9015 spin_lock_irqsave(&phba->hbalock, iflags);
9016 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9017 spin_unlock_irqrestore(&phba->hbalock, iflags);
9018 workposted = true;
9019 break;
9020 }
9021out:
9022 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009023}
9024
9025/**
9026 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9027 * @phba: Pointer to HBA context object.
9028 * @cq: Pointer to the completion queue.
9029 * @wcqe: Pointer to a completion queue entry.
9030 *
9031 * This routine process a slow-path work-queue or recieve queue completion queue
9032 * entry.
9033 *
9034 * Return: true if work posted to worker thread, otherwise false.
9035 **/
9036static bool
9037lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009038 struct lpfc_cqe *cqe)
9039{
James Smart45ed1192009-10-02 15:17:02 -04009040 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009041 bool workposted = false;
9042
9043 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009044 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009045
9046 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009047 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009048 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009049 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04009050 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009051 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009052 break;
9053 case CQE_CODE_RELEASE_WQE:
9054 /* Process the WQ release event */
9055 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009056 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009057 break;
9058 case CQE_CODE_XRI_ABORTED:
9059 /* Process the WQ XRI abort event */
9060 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009061 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009062 break;
James Smart4d9ab992009-10-02 15:16:39 -04009063 case CQE_CODE_RECEIVE:
9064 /* Process the RQ event */
9065 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009066 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009067 break;
James Smart4f774512009-05-22 14:52:35 -04009068 default:
9069 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9070 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009071 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009072 break;
9073 }
9074 return workposted;
9075}
9076
9077/**
James Smart4f774512009-05-22 14:52:35 -04009078 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9079 * @phba: Pointer to HBA context object.
9080 * @eqe: Pointer to fast-path event queue entry.
9081 *
9082 * This routine process a event queue entry from the slow-path event queue.
9083 * It will check the MajorCode and MinorCode to determine this is for a
9084 * completion event on a completion queue, if not, an error shall be logged
9085 * and just return. Otherwise, it will get to the corresponding completion
9086 * queue and process all the entries on that completion queue, rearm the
9087 * completion queue, and then return.
9088 *
9089 **/
9090static void
9091lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9092{
9093 struct lpfc_queue *cq = NULL, *childq, *speq;
9094 struct lpfc_cqe *cqe;
9095 bool workposted = false;
9096 int ecount = 0;
9097 uint16_t cqid;
9098
James Smartcb5172e2010-03-15 11:25:07 -04009099 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9101 "0359 Not a valid slow-path completion "
9102 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009103 bf_get_le32(lpfc_eqe_major_code, eqe),
9104 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009105 return;
9106 }
9107
9108 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009109 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009110
9111 /* Search for completion queue pointer matching this cqid */
9112 speq = phba->sli4_hba.sp_eq;
9113 list_for_each_entry(childq, &speq->child_list, list) {
9114 if (childq->queue_id == cqid) {
9115 cq = childq;
9116 break;
9117 }
9118 }
9119 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009120 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9121 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9122 "0365 Slow-path CQ identifier "
9123 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009124 return;
9125 }
9126
9127 /* Process all the entries to the CQ */
9128 switch (cq->type) {
9129 case LPFC_MCQ:
9130 while ((cqe = lpfc_sli4_cq_get(cq))) {
9131 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9132 if (!(++ecount % LPFC_GET_QE_REL_INT))
9133 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9134 }
9135 break;
9136 case LPFC_WCQ:
9137 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009138 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009139 if (!(++ecount % LPFC_GET_QE_REL_INT))
9140 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9141 }
9142 break;
9143 default:
9144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9145 "0370 Invalid completion queue type (%d)\n",
9146 cq->type);
9147 return;
9148 }
9149
9150 /* Catch the no cq entry condition, log an error */
9151 if (unlikely(ecount == 0))
9152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9153 "0371 No entry from the CQ: identifier "
9154 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9155
9156 /* In any case, flash and re-arm the RCQ */
9157 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9158
9159 /* wake up worker thread if there are works to be done */
9160 if (workposted)
9161 lpfc_worker_wake_up(phba);
9162}
9163
9164/**
9165 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9166 * @eqe: Pointer to fast-path completion queue entry.
9167 *
9168 * This routine process a fast-path work queue completion entry from fast-path
9169 * event queue for FCP command response completion.
9170 **/
9171static void
9172lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9173 struct lpfc_wcqe_complete *wcqe)
9174{
9175 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9176 struct lpfc_iocbq *cmdiocbq;
9177 struct lpfc_iocbq irspiocbq;
9178 unsigned long iflags;
9179
9180 spin_lock_irqsave(&phba->hbalock, iflags);
9181 pring->stats.iocb_event++;
9182 spin_unlock_irqrestore(&phba->hbalock, iflags);
9183
9184 /* Check for response status */
9185 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9186 /* If resource errors reported from HBA, reduce queue
9187 * depth of the SCSI device.
9188 */
9189 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9190 IOSTAT_LOCAL_REJECT) &&
9191 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9192 phba->lpfc_rampdown_queue_depth(phba);
9193 }
9194 /* Log the error status */
9195 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9196 "0373 FCP complete error: status=x%x, "
9197 "hw_status=x%x, total_data_specified=%d, "
9198 "parameter=x%x, word3=x%x\n",
9199 bf_get(lpfc_wcqe_c_status, wcqe),
9200 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9201 wcqe->total_data_placed, wcqe->parameter,
9202 wcqe->word3);
9203 }
9204
9205 /* Look up the FCP command IOCB and create pseudo response IOCB */
9206 spin_lock_irqsave(&phba->hbalock, iflags);
9207 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9208 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9209 spin_unlock_irqrestore(&phba->hbalock, iflags);
9210 if (unlikely(!cmdiocbq)) {
9211 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9212 "0374 FCP complete with no corresponding "
9213 "cmdiocb: iotag (%d)\n",
9214 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9215 return;
9216 }
9217 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9218 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9219 "0375 FCP cmdiocb not callback function "
9220 "iotag: (%d)\n",
9221 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9222 return;
9223 }
9224
9225 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009226 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009227
James Smart0f65ff62010-02-26 14:14:23 -05009228 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9229 spin_lock_irqsave(&phba->hbalock, iflags);
9230 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9231 spin_unlock_irqrestore(&phba->hbalock, iflags);
9232 }
9233
James Smart4f774512009-05-22 14:52:35 -04009234 /* Pass the cmd_iocb and the rsp state to the upper layer */
9235 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9236}
9237
9238/**
9239 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9240 * @phba: Pointer to HBA context object.
9241 * @cq: Pointer to completion queue.
9242 * @wcqe: Pointer to work-queue completion queue entry.
9243 *
9244 * This routine handles an fast-path WQ entry comsumed event by invoking the
9245 * proper WQ release routine to the slow-path WQ.
9246 **/
9247static void
9248lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9249 struct lpfc_wcqe_release *wcqe)
9250{
9251 struct lpfc_queue *childwq;
9252 bool wqid_matched = false;
9253 uint16_t fcp_wqid;
9254
9255 /* Check for fast-path FCP work queue release */
9256 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9257 list_for_each_entry(childwq, &cq->child_list, list) {
9258 if (childwq->queue_id == fcp_wqid) {
9259 lpfc_sli4_wq_release(childwq,
9260 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9261 wqid_matched = true;
9262 break;
9263 }
9264 }
9265 /* Report warning log message if no match found */
9266 if (wqid_matched != true)
9267 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9268 "2580 Fast-path wqe consume event carries "
9269 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9270}
9271
9272/**
9273 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9274 * @cq: Pointer to the completion queue.
9275 * @eqe: Pointer to fast-path completion queue entry.
9276 *
9277 * This routine process a fast-path work queue completion entry from fast-path
9278 * event queue for FCP command response completion.
9279 **/
9280static int
9281lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9282 struct lpfc_cqe *cqe)
9283{
9284 struct lpfc_wcqe_release wcqe;
9285 bool workposted = false;
James Smart98fc5dd2010-06-07 15:24:29 -04009286 unsigned long iflag;
James Smart4f774512009-05-22 14:52:35 -04009287
9288 /* Copy the work queue CQE and convert endian order if needed */
9289 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9290
9291 /* Check and process for different type of WCQE and dispatch */
9292 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9293 case CQE_CODE_COMPL_WQE:
9294 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009295 spin_lock_irqsave(&phba->hbalock, iflag);
9296 phba->last_completion_time = jiffies;
9297 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4f774512009-05-22 14:52:35 -04009298 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9299 (struct lpfc_wcqe_complete *)&wcqe);
9300 break;
9301 case CQE_CODE_RELEASE_WQE:
9302 /* Process the WQ release event */
9303 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9304 (struct lpfc_wcqe_release *)&wcqe);
9305 break;
9306 case CQE_CODE_XRI_ABORTED:
9307 /* Process the WQ XRI abort event */
9308 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9309 (struct sli4_wcqe_xri_aborted *)&wcqe);
9310 break;
9311 default:
9312 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9313 "0144 Not a valid WCQE code: x%x\n",
9314 bf_get(lpfc_wcqe_c_code, &wcqe));
9315 break;
9316 }
9317 return workposted;
9318}
9319
9320/**
9321 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9322 * @phba: Pointer to HBA context object.
9323 * @eqe: Pointer to fast-path event queue entry.
9324 *
9325 * This routine process a event queue entry from the fast-path event queue.
9326 * It will check the MajorCode and MinorCode to determine this is for a
9327 * completion event on a completion queue, if not, an error shall be logged
9328 * and just return. Otherwise, it will get to the corresponding completion
9329 * queue and process all the entries on the completion queue, rearm the
9330 * completion queue, and then return.
9331 **/
9332static void
9333lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9334 uint32_t fcp_cqidx)
9335{
9336 struct lpfc_queue *cq;
9337 struct lpfc_cqe *cqe;
9338 bool workposted = false;
9339 uint16_t cqid;
9340 int ecount = 0;
9341
James Smartcb5172e2010-03-15 11:25:07 -04009342 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009343 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9344 "0366 Not a valid fast-path completion "
9345 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009346 bf_get_le32(lpfc_eqe_major_code, eqe),
9347 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009348 return;
9349 }
9350
9351 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9352 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009353 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9355 "0367 Fast-path completion queue "
9356 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009357 return;
9358 }
9359
9360 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009361 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009362 if (unlikely(cqid != cq->queue_id)) {
9363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9364 "0368 Miss-matched fast-path completion "
9365 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9366 cqid, cq->queue_id);
9367 return;
9368 }
9369
9370 /* Process all the entries to the CQ */
9371 while ((cqe = lpfc_sli4_cq_get(cq))) {
9372 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9373 if (!(++ecount % LPFC_GET_QE_REL_INT))
9374 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9375 }
9376
9377 /* Catch the no cq entry condition */
9378 if (unlikely(ecount == 0))
9379 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9380 "0369 No entry from fast-path completion "
9381 "queue fcpcqid=%d\n", cq->queue_id);
9382
9383 /* In any case, flash and re-arm the CQ */
9384 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9385
9386 /* wake up worker thread if there are works to be done */
9387 if (workposted)
9388 lpfc_worker_wake_up(phba);
9389}
9390
9391static void
9392lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9393{
9394 struct lpfc_eqe *eqe;
9395
9396 /* walk all the EQ entries and drop on the floor */
9397 while ((eqe = lpfc_sli4_eq_get(eq)))
9398 ;
9399
9400 /* Clear and re-arm the EQ */
9401 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9402}
9403
9404/**
9405 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9406 * @irq: Interrupt number.
9407 * @dev_id: The device context pointer.
9408 *
9409 * This function is directly called from the PCI layer as an interrupt
9410 * service routine when device with SLI-4 interface spec is enabled with
9411 * MSI-X multi-message interrupt mode and there are slow-path events in
9412 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9413 * interrupt mode, this function is called as part of the device-level
9414 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9415 * undergoing initialization, the interrupt handler will not process the
9416 * interrupt. The link attention and ELS ring attention events are handled
9417 * by the worker thread. The interrupt handler signals the worker thread
9418 * and returns for these events. This function is called without any lock
9419 * held. It gets the hbalock to access and update SLI data structures.
9420 *
9421 * This function returns IRQ_HANDLED when interrupt is handled else it
9422 * returns IRQ_NONE.
9423 **/
9424irqreturn_t
9425lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9426{
9427 struct lpfc_hba *phba;
9428 struct lpfc_queue *speq;
9429 struct lpfc_eqe *eqe;
9430 unsigned long iflag;
9431 int ecount = 0;
9432
9433 /*
9434 * Get the driver's phba structure from the dev_id
9435 */
9436 phba = (struct lpfc_hba *)dev_id;
9437
9438 if (unlikely(!phba))
9439 return IRQ_NONE;
9440
9441 /* Get to the EQ struct associated with this vector */
9442 speq = phba->sli4_hba.sp_eq;
9443
9444 /* Check device state for handling interrupt */
9445 if (unlikely(lpfc_intr_state_check(phba))) {
9446 /* Check again for link_state with lock held */
9447 spin_lock_irqsave(&phba->hbalock, iflag);
9448 if (phba->link_state < LPFC_LINK_DOWN)
9449 /* Flush, clear interrupt, and rearm the EQ */
9450 lpfc_sli4_eq_flush(phba, speq);
9451 spin_unlock_irqrestore(&phba->hbalock, iflag);
9452 return IRQ_NONE;
9453 }
9454
9455 /*
9456 * Process all the event on FCP slow-path EQ
9457 */
9458 while ((eqe = lpfc_sli4_eq_get(speq))) {
9459 lpfc_sli4_sp_handle_eqe(phba, eqe);
9460 if (!(++ecount % LPFC_GET_QE_REL_INT))
9461 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9462 }
9463
9464 /* Always clear and re-arm the slow-path EQ */
9465 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9466
9467 /* Catch the no cq entry condition */
9468 if (unlikely(ecount == 0)) {
9469 if (phba->intr_type == MSIX)
9470 /* MSI-X treated interrupt served as no EQ share INT */
9471 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9472 "0357 MSI-X interrupt with no EQE\n");
9473 else
9474 /* Non MSI-X treated on interrupt as EQ share INT */
9475 return IRQ_NONE;
9476 }
9477
9478 return IRQ_HANDLED;
9479} /* lpfc_sli4_sp_intr_handler */
9480
9481/**
9482 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9483 * @irq: Interrupt number.
9484 * @dev_id: The device context pointer.
9485 *
9486 * This function is directly called from the PCI layer as an interrupt
9487 * service routine when device with SLI-4 interface spec is enabled with
9488 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9489 * ring event in the HBA. However, when the device is enabled with either
9490 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9491 * device-level interrupt handler. When the PCI slot is in error recovery
9492 * or the HBA is undergoing initialization, the interrupt handler will not
9493 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9494 * the intrrupt context. This function is called without any lock held.
9495 * It gets the hbalock to access and update SLI data structures. Note that,
9496 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9497 * equal to that of FCP CQ index.
9498 *
9499 * This function returns IRQ_HANDLED when interrupt is handled else it
9500 * returns IRQ_NONE.
9501 **/
9502irqreturn_t
9503lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9504{
9505 struct lpfc_hba *phba;
9506 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9507 struct lpfc_queue *fpeq;
9508 struct lpfc_eqe *eqe;
9509 unsigned long iflag;
9510 int ecount = 0;
9511 uint32_t fcp_eqidx;
9512
9513 /* Get the driver's phba structure from the dev_id */
9514 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9515 phba = fcp_eq_hdl->phba;
9516 fcp_eqidx = fcp_eq_hdl->idx;
9517
9518 if (unlikely(!phba))
9519 return IRQ_NONE;
9520
9521 /* Get to the EQ struct associated with this vector */
9522 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9523
9524 /* Check device state for handling interrupt */
9525 if (unlikely(lpfc_intr_state_check(phba))) {
9526 /* Check again for link_state with lock held */
9527 spin_lock_irqsave(&phba->hbalock, iflag);
9528 if (phba->link_state < LPFC_LINK_DOWN)
9529 /* Flush, clear interrupt, and rearm the EQ */
9530 lpfc_sli4_eq_flush(phba, fpeq);
9531 spin_unlock_irqrestore(&phba->hbalock, iflag);
9532 return IRQ_NONE;
9533 }
9534
9535 /*
9536 * Process all the event on FCP fast-path EQ
9537 */
9538 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9539 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9540 if (!(++ecount % LPFC_GET_QE_REL_INT))
9541 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9542 }
9543
9544 /* Always clear and re-arm the fast-path EQ */
9545 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9546
9547 if (unlikely(ecount == 0)) {
9548 if (phba->intr_type == MSIX)
9549 /* MSI-X treated interrupt served as no EQ share INT */
9550 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9551 "0358 MSI-X interrupt with no EQE\n");
9552 else
9553 /* Non MSI-X treated on interrupt as EQ share INT */
9554 return IRQ_NONE;
9555 }
9556
9557 return IRQ_HANDLED;
9558} /* lpfc_sli4_fp_intr_handler */
9559
9560/**
9561 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9562 * @irq: Interrupt number.
9563 * @dev_id: The device context pointer.
9564 *
9565 * This function is the device-level interrupt handler to device with SLI-4
9566 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9567 * interrupt mode is enabled and there is an event in the HBA which requires
9568 * driver attention. This function invokes the slow-path interrupt attention
9569 * handling function and fast-path interrupt attention handling function in
9570 * turn to process the relevant HBA attention events. This function is called
9571 * without any lock held. It gets the hbalock to access and update SLI data
9572 * structures.
9573 *
9574 * This function returns IRQ_HANDLED when interrupt is handled, else it
9575 * returns IRQ_NONE.
9576 **/
9577irqreturn_t
9578lpfc_sli4_intr_handler(int irq, void *dev_id)
9579{
9580 struct lpfc_hba *phba;
9581 irqreturn_t sp_irq_rc, fp_irq_rc;
9582 bool fp_handled = false;
9583 uint32_t fcp_eqidx;
9584
9585 /* Get the driver's phba structure from the dev_id */
9586 phba = (struct lpfc_hba *)dev_id;
9587
9588 if (unlikely(!phba))
9589 return IRQ_NONE;
9590
9591 /*
9592 * Invokes slow-path host attention interrupt handling as appropriate.
9593 */
9594 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9595
9596 /*
9597 * Invoke fast-path host attention interrupt handling as appropriate.
9598 */
9599 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9600 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9601 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9602 if (fp_irq_rc == IRQ_HANDLED)
9603 fp_handled |= true;
9604 }
9605
9606 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9607} /* lpfc_sli4_intr_handler */
9608
9609/**
9610 * lpfc_sli4_queue_free - free a queue structure and associated memory
9611 * @queue: The queue structure to free.
9612 *
9613 * This function frees a queue structure and the DMAable memeory used for
9614 * the host resident queue. This function must be called after destroying the
9615 * queue on the HBA.
9616 **/
9617void
9618lpfc_sli4_queue_free(struct lpfc_queue *queue)
9619{
9620 struct lpfc_dmabuf *dmabuf;
9621
9622 if (!queue)
9623 return;
9624
9625 while (!list_empty(&queue->page_list)) {
9626 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9627 list);
James Smart49198b32010-04-06 15:04:33 -04009628 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009629 dmabuf->virt, dmabuf->phys);
9630 kfree(dmabuf);
9631 }
9632 kfree(queue);
9633 return;
9634}
9635
9636/**
9637 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9638 * @phba: The HBA that this queue is being created on.
9639 * @entry_size: The size of each queue entry for this queue.
9640 * @entry count: The number of entries that this queue will handle.
9641 *
9642 * This function allocates a queue structure and the DMAable memory used for
9643 * the host resident queue. This function must be called before creating the
9644 * queue on the HBA.
9645 **/
9646struct lpfc_queue *
9647lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9648 uint32_t entry_count)
9649{
9650 struct lpfc_queue *queue;
9651 struct lpfc_dmabuf *dmabuf;
9652 int x, total_qe_count;
9653 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009654 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009655
James Smartcb5172e2010-03-15 11:25:07 -04009656 if (!phba->sli4_hba.pc_sli4_params.supported)
9657 hw_page_size = SLI4_PAGE_SIZE;
9658
James Smart4f774512009-05-22 14:52:35 -04009659 queue = kzalloc(sizeof(struct lpfc_queue) +
9660 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9661 if (!queue)
9662 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009663 queue->page_count = (ALIGN(entry_size * entry_count,
9664 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009665 INIT_LIST_HEAD(&queue->list);
9666 INIT_LIST_HEAD(&queue->page_list);
9667 INIT_LIST_HEAD(&queue->child_list);
9668 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9669 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9670 if (!dmabuf)
9671 goto out_fail;
9672 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009673 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009674 GFP_KERNEL);
9675 if (!dmabuf->virt) {
9676 kfree(dmabuf);
9677 goto out_fail;
9678 }
James Smartcb5172e2010-03-15 11:25:07 -04009679 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009680 dmabuf->buffer_tag = x;
9681 list_add_tail(&dmabuf->list, &queue->page_list);
9682 /* initialize queue's entry array */
9683 dma_pointer = dmabuf->virt;
9684 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009685 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009686 total_qe_count++, dma_pointer += entry_size) {
9687 queue->qe[total_qe_count].address = dma_pointer;
9688 }
9689 }
9690 queue->entry_size = entry_size;
9691 queue->entry_count = entry_count;
9692 queue->phba = phba;
9693
9694 return queue;
9695out_fail:
9696 lpfc_sli4_queue_free(queue);
9697 return NULL;
9698}
9699
9700/**
9701 * lpfc_eq_create - Create an Event Queue on the HBA
9702 * @phba: HBA structure that indicates port to create a queue on.
9703 * @eq: The queue structure to use to create the event queue.
9704 * @imax: The maximum interrupt per second limit.
9705 *
9706 * This function creates an event queue, as detailed in @eq, on a port,
9707 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9708 *
9709 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9710 * is used to get the entry count and entry size that are necessary to
9711 * determine the number of pages to allocate and use for this queue. This
9712 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9713 * event queue. This function is asynchronous and will wait for the mailbox
9714 * command to finish before continuing.
9715 *
9716 * On success this function will return a zero. If unable to allocate enough
9717 * memory this function will return ENOMEM. If the queue create mailbox command
9718 * fails this function will return ENXIO.
9719 **/
9720uint32_t
9721lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9722{
9723 struct lpfc_mbx_eq_create *eq_create;
9724 LPFC_MBOXQ_t *mbox;
9725 int rc, length, status = 0;
9726 struct lpfc_dmabuf *dmabuf;
9727 uint32_t shdr_status, shdr_add_status;
9728 union lpfc_sli4_cfg_shdr *shdr;
9729 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009730 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9731
9732 if (!phba->sli4_hba.pc_sli4_params.supported)
9733 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009734
9735 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9736 if (!mbox)
9737 return -ENOMEM;
9738 length = (sizeof(struct lpfc_mbx_eq_create) -
9739 sizeof(struct lpfc_sli4_cfg_mhdr));
9740 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9741 LPFC_MBOX_OPCODE_EQ_CREATE,
9742 length, LPFC_SLI4_MBX_EMBED);
9743 eq_create = &mbox->u.mqe.un.eq_create;
9744 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9745 eq->page_count);
9746 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9747 LPFC_EQE_SIZE);
9748 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9749 /* Calculate delay multiper from maximum interrupt per second */
9750 dmult = LPFC_DMULT_CONST/imax - 1;
9751 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9752 dmult);
9753 switch (eq->entry_count) {
9754 default:
9755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9756 "0360 Unsupported EQ count. (%d)\n",
9757 eq->entry_count);
9758 if (eq->entry_count < 256)
9759 return -EINVAL;
9760 /* otherwise default to smallest count (drop through) */
9761 case 256:
9762 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9763 LPFC_EQ_CNT_256);
9764 break;
9765 case 512:
9766 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9767 LPFC_EQ_CNT_512);
9768 break;
9769 case 1024:
9770 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9771 LPFC_EQ_CNT_1024);
9772 break;
9773 case 2048:
9774 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9775 LPFC_EQ_CNT_2048);
9776 break;
9777 case 4096:
9778 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9779 LPFC_EQ_CNT_4096);
9780 break;
9781 }
9782 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009783 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009784 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9785 putPaddrLow(dmabuf->phys);
9786 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9787 putPaddrHigh(dmabuf->phys);
9788 }
9789 mbox->vport = phba->pport;
9790 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9791 mbox->context1 = NULL;
9792 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9793 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9794 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9795 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9796 if (shdr_status || shdr_add_status || rc) {
9797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9798 "2500 EQ_CREATE mailbox failed with "
9799 "status x%x add_status x%x, mbx status x%x\n",
9800 shdr_status, shdr_add_status, rc);
9801 status = -ENXIO;
9802 }
9803 eq->type = LPFC_EQ;
9804 eq->subtype = LPFC_NONE;
9805 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9806 if (eq->queue_id == 0xFFFF)
9807 status = -ENXIO;
9808 eq->host_index = 0;
9809 eq->hba_index = 0;
9810
James Smart8fa38512009-07-19 10:01:03 -04009811 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009812 return status;
9813}
9814
9815/**
9816 * lpfc_cq_create - Create a Completion Queue on the HBA
9817 * @phba: HBA structure that indicates port to create a queue on.
9818 * @cq: The queue structure to use to create the completion queue.
9819 * @eq: The event queue to bind this completion queue to.
9820 *
9821 * This function creates a completion queue, as detailed in @wq, on a port,
9822 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9823 *
9824 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9825 * is used to get the entry count and entry size that are necessary to
9826 * determine the number of pages to allocate and use for this queue. The @eq
9827 * is used to indicate which event queue to bind this completion queue to. This
9828 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9829 * completion queue. This function is asynchronous and will wait for the mailbox
9830 * command to finish before continuing.
9831 *
9832 * On success this function will return a zero. If unable to allocate enough
9833 * memory this function will return ENOMEM. If the queue create mailbox command
9834 * fails this function will return ENXIO.
9835 **/
9836uint32_t
9837lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9838 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9839{
9840 struct lpfc_mbx_cq_create *cq_create;
9841 struct lpfc_dmabuf *dmabuf;
9842 LPFC_MBOXQ_t *mbox;
9843 int rc, length, status = 0;
9844 uint32_t shdr_status, shdr_add_status;
9845 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009846 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9847
9848 if (!phba->sli4_hba.pc_sli4_params.supported)
9849 hw_page_size = SLI4_PAGE_SIZE;
9850
James Smart4f774512009-05-22 14:52:35 -04009851
9852 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9853 if (!mbox)
9854 return -ENOMEM;
9855 length = (sizeof(struct lpfc_mbx_cq_create) -
9856 sizeof(struct lpfc_sli4_cfg_mhdr));
9857 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9858 LPFC_MBOX_OPCODE_CQ_CREATE,
9859 length, LPFC_SLI4_MBX_EMBED);
9860 cq_create = &mbox->u.mqe.un.cq_create;
9861 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9862 cq->page_count);
9863 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9864 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9865 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9866 switch (cq->entry_count) {
9867 default:
9868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9869 "0361 Unsupported CQ count. (%d)\n",
9870 cq->entry_count);
9871 if (cq->entry_count < 256)
9872 return -EINVAL;
9873 /* otherwise default to smallest count (drop through) */
9874 case 256:
9875 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9876 LPFC_CQ_CNT_256);
9877 break;
9878 case 512:
9879 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9880 LPFC_CQ_CNT_512);
9881 break;
9882 case 1024:
9883 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9884 LPFC_CQ_CNT_1024);
9885 break;
9886 }
9887 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009888 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009889 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9890 putPaddrLow(dmabuf->phys);
9891 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9892 putPaddrHigh(dmabuf->phys);
9893 }
9894 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9895
9896 /* The IOCTL status is embedded in the mailbox subheader. */
9897 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9898 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9899 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9900 if (shdr_status || shdr_add_status || rc) {
9901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9902 "2501 CQ_CREATE mailbox failed with "
9903 "status x%x add_status x%x, mbx status x%x\n",
9904 shdr_status, shdr_add_status, rc);
9905 status = -ENXIO;
9906 goto out;
9907 }
9908 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9909 if (cq->queue_id == 0xFFFF) {
9910 status = -ENXIO;
9911 goto out;
9912 }
9913 /* link the cq onto the parent eq child list */
9914 list_add_tail(&cq->list, &eq->child_list);
9915 /* Set up completion queue's type and subtype */
9916 cq->type = type;
9917 cq->subtype = subtype;
9918 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9919 cq->host_index = 0;
9920 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009921
James Smart8fa38512009-07-19 10:01:03 -04009922out:
9923 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009924 return status;
9925}
9926
9927/**
James Smartb19a0612010-04-06 14:48:51 -04009928 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -04009929 * @phba: HBA structure that indicates port to create a queue on.
9930 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -04009931 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9932 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -04009933 *
James Smartb19a0612010-04-06 14:48:51 -04009934 * This function provides failback (fb) functionality when the
9935 * mq_create_ext fails on older FW generations. It's purpose is identical
9936 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -04009937 *
James Smartb19a0612010-04-06 14:48:51 -04009938 * This routine cannot fail as all attributes were previously accessed and
9939 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -04009940 **/
James Smartb19a0612010-04-06 14:48:51 -04009941static void
9942lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9943 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -04009944{
9945 struct lpfc_mbx_mq_create *mq_create;
9946 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -04009947 int length;
James Smart04c68492009-05-22 14:52:52 -04009948
James Smart04c68492009-05-22 14:52:52 -04009949 length = (sizeof(struct lpfc_mbx_mq_create) -
9950 sizeof(struct lpfc_sli4_cfg_mhdr));
9951 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9952 LPFC_MBOX_OPCODE_MQ_CREATE,
9953 length, LPFC_SLI4_MBX_EMBED);
9954 mq_create = &mbox->u.mqe.un.mq_create;
9955 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -04009956 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -04009957 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -04009958 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -04009959 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9960 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -04009961 case 16:
9962 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9963 LPFC_MQ_CNT_16);
9964 break;
9965 case 32:
9966 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9967 LPFC_MQ_CNT_32);
9968 break;
9969 case 64:
9970 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9971 LPFC_MQ_CNT_64);
9972 break;
9973 case 128:
9974 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9975 LPFC_MQ_CNT_128);
9976 break;
9977 }
9978 list_for_each_entry(dmabuf, &mq->page_list, list) {
9979 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -04009980 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -04009981 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -04009982 putPaddrHigh(dmabuf->phys);
9983 }
9984}
9985
9986/**
9987 * lpfc_mq_create - Create a mailbox Queue on the HBA
9988 * @phba: HBA structure that indicates port to create a queue on.
9989 * @mq: The queue structure to use to create the mailbox queue.
9990 * @cq: The completion queue to associate with this cq.
9991 * @subtype: The queue's subtype.
9992 *
9993 * This function creates a mailbox queue, as detailed in @mq, on a port,
9994 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9995 *
9996 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9997 * is used to get the entry count and entry size that are necessary to
9998 * determine the number of pages to allocate and use for this queue. This
9999 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10000 * mailbox queue. This function is asynchronous and will wait for the mailbox
10001 * command to finish before continuing.
10002 *
10003 * On success this function will return a zero. If unable to allocate enough
10004 * memory this function will return ENOMEM. If the queue create mailbox command
10005 * fails this function will return ENXIO.
10006 **/
10007int32_t
10008lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10009 struct lpfc_queue *cq, uint32_t subtype)
10010{
10011 struct lpfc_mbx_mq_create *mq_create;
10012 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10013 struct lpfc_dmabuf *dmabuf;
10014 LPFC_MBOXQ_t *mbox;
10015 int rc, length, status = 0;
10016 uint32_t shdr_status, shdr_add_status;
10017 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010018 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010019
James Smart49198b32010-04-06 15:04:33 -040010020 if (!phba->sli4_hba.pc_sli4_params.supported)
10021 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010022
10023 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10024 if (!mbox)
10025 return -ENOMEM;
10026 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10027 sizeof(struct lpfc_sli4_cfg_mhdr));
10028 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10029 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10030 length, LPFC_SLI4_MBX_EMBED);
10031
10032 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10033 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10034 mq->page_count);
10035 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10036 1);
10037 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10038 &mq_create_ext->u.request, 1);
10039 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10040 &mq_create_ext->u.request, 1);
10041 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10042 cq->queue_id);
10043 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10044 switch (mq->entry_count) {
10045 default:
10046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10047 "0362 Unsupported MQ count. (%d)\n",
10048 mq->entry_count);
10049 if (mq->entry_count < 16)
10050 return -EINVAL;
10051 /* otherwise default to smallest count (drop through) */
10052 case 16:
10053 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10054 LPFC_MQ_CNT_16);
10055 break;
10056 case 32:
10057 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10058 LPFC_MQ_CNT_32);
10059 break;
10060 case 64:
10061 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10062 LPFC_MQ_CNT_64);
10063 break;
10064 case 128:
10065 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10066 LPFC_MQ_CNT_128);
10067 break;
10068 }
10069 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010070 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010071 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10072 putPaddrLow(dmabuf->phys);
10073 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010074 putPaddrHigh(dmabuf->phys);
10075 }
10076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010077 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10078 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10079 &mq_create_ext->u.response);
10080 if (rc != MBX_SUCCESS) {
10081 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10082 "2795 MQ_CREATE_EXT failed with "
10083 "status x%x. Failback to MQ_CREATE.\n",
10084 rc);
10085 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10086 mq_create = &mbox->u.mqe.un.mq_create;
10087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10088 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10089 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10090 &mq_create->u.response);
10091 }
10092
James Smart04c68492009-05-22 14:52:52 -040010093 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010094 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10095 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10096 if (shdr_status || shdr_add_status || rc) {
10097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10098 "2502 MQ_CREATE mailbox failed with "
10099 "status x%x add_status x%x, mbx status x%x\n",
10100 shdr_status, shdr_add_status, rc);
10101 status = -ENXIO;
10102 goto out;
10103 }
James Smart04c68492009-05-22 14:52:52 -040010104 if (mq->queue_id == 0xFFFF) {
10105 status = -ENXIO;
10106 goto out;
10107 }
10108 mq->type = LPFC_MQ;
10109 mq->subtype = subtype;
10110 mq->host_index = 0;
10111 mq->hba_index = 0;
10112
10113 /* link the mq onto the parent cq child list */
10114 list_add_tail(&mq->list, &cq->child_list);
10115out:
James Smart8fa38512009-07-19 10:01:03 -040010116 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010117 return status;
10118}
10119
10120/**
James Smart4f774512009-05-22 14:52:35 -040010121 * lpfc_wq_create - Create a Work Queue on the HBA
10122 * @phba: HBA structure that indicates port to create a queue on.
10123 * @wq: The queue structure to use to create the work queue.
10124 * @cq: The completion queue to bind this work queue to.
10125 * @subtype: The subtype of the work queue indicating its functionality.
10126 *
10127 * This function creates a work queue, as detailed in @wq, on a port, described
10128 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10129 *
10130 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10131 * is used to get the entry count and entry size that are necessary to
10132 * determine the number of pages to allocate and use for this queue. The @cq
10133 * is used to indicate which completion queue to bind this work queue to. This
10134 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10135 * work queue. This function is asynchronous and will wait for the mailbox
10136 * command to finish before continuing.
10137 *
10138 * On success this function will return a zero. If unable to allocate enough
10139 * memory this function will return ENOMEM. If the queue create mailbox command
10140 * fails this function will return ENXIO.
10141 **/
10142uint32_t
10143lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10144 struct lpfc_queue *cq, uint32_t subtype)
10145{
10146 struct lpfc_mbx_wq_create *wq_create;
10147 struct lpfc_dmabuf *dmabuf;
10148 LPFC_MBOXQ_t *mbox;
10149 int rc, length, status = 0;
10150 uint32_t shdr_status, shdr_add_status;
10151 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010152 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10153
10154 if (!phba->sli4_hba.pc_sli4_params.supported)
10155 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010156
10157 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10158 if (!mbox)
10159 return -ENOMEM;
10160 length = (sizeof(struct lpfc_mbx_wq_create) -
10161 sizeof(struct lpfc_sli4_cfg_mhdr));
10162 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10163 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10164 length, LPFC_SLI4_MBX_EMBED);
10165 wq_create = &mbox->u.mqe.un.wq_create;
10166 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10167 wq->page_count);
10168 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10169 cq->queue_id);
10170 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010171 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010172 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10173 putPaddrLow(dmabuf->phys);
10174 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10175 putPaddrHigh(dmabuf->phys);
10176 }
10177 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10178 /* The IOCTL status is embedded in the mailbox subheader. */
10179 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10180 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10181 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10182 if (shdr_status || shdr_add_status || rc) {
10183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10184 "2503 WQ_CREATE mailbox failed with "
10185 "status x%x add_status x%x, mbx status x%x\n",
10186 shdr_status, shdr_add_status, rc);
10187 status = -ENXIO;
10188 goto out;
10189 }
10190 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10191 if (wq->queue_id == 0xFFFF) {
10192 status = -ENXIO;
10193 goto out;
10194 }
10195 wq->type = LPFC_WQ;
10196 wq->subtype = subtype;
10197 wq->host_index = 0;
10198 wq->hba_index = 0;
10199
10200 /* link the wq onto the parent cq child list */
10201 list_add_tail(&wq->list, &cq->child_list);
10202out:
James Smart8fa38512009-07-19 10:01:03 -040010203 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010204 return status;
10205}
10206
10207/**
10208 * lpfc_rq_create - Create a Receive Queue on the HBA
10209 * @phba: HBA structure that indicates port to create a queue on.
10210 * @hrq: The queue structure to use to create the header receive queue.
10211 * @drq: The queue structure to use to create the data receive queue.
10212 * @cq: The completion queue to bind this work queue to.
10213 *
10214 * This function creates a receive buffer queue pair , as detailed in @hrq and
10215 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10216 * to the HBA.
10217 *
10218 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10219 * struct is used to get the entry count that is necessary to determine the
10220 * number of pages to use for this queue. The @cq is used to indicate which
10221 * completion queue to bind received buffers that are posted to these queues to.
10222 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10223 * receive queue pair. This function is asynchronous and will wait for the
10224 * mailbox command to finish before continuing.
10225 *
10226 * On success this function will return a zero. If unable to allocate enough
10227 * memory this function will return ENOMEM. If the queue create mailbox command
10228 * fails this function will return ENXIO.
10229 **/
10230uint32_t
10231lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10232 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10233{
10234 struct lpfc_mbx_rq_create *rq_create;
10235 struct lpfc_dmabuf *dmabuf;
10236 LPFC_MBOXQ_t *mbox;
10237 int rc, length, status = 0;
10238 uint32_t shdr_status, shdr_add_status;
10239 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010240 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10241
10242 if (!phba->sli4_hba.pc_sli4_params.supported)
10243 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010244
10245 if (hrq->entry_count != drq->entry_count)
10246 return -EINVAL;
10247 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10248 if (!mbox)
10249 return -ENOMEM;
10250 length = (sizeof(struct lpfc_mbx_rq_create) -
10251 sizeof(struct lpfc_sli4_cfg_mhdr));
10252 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10253 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10254 length, LPFC_SLI4_MBX_EMBED);
10255 rq_create = &mbox->u.mqe.un.rq_create;
10256 switch (hrq->entry_count) {
10257 default:
10258 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10259 "2535 Unsupported RQ count. (%d)\n",
10260 hrq->entry_count);
10261 if (hrq->entry_count < 512)
10262 return -EINVAL;
10263 /* otherwise default to smallest count (drop through) */
10264 case 512:
10265 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10266 LPFC_RQ_RING_SIZE_512);
10267 break;
10268 case 1024:
10269 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10270 LPFC_RQ_RING_SIZE_1024);
10271 break;
10272 case 2048:
10273 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10274 LPFC_RQ_RING_SIZE_2048);
10275 break;
10276 case 4096:
10277 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10278 LPFC_RQ_RING_SIZE_4096);
10279 break;
10280 }
10281 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10282 cq->queue_id);
10283 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10284 hrq->page_count);
10285 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10286 LPFC_HDR_BUF_SIZE);
10287 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010288 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010289 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10290 putPaddrLow(dmabuf->phys);
10291 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10292 putPaddrHigh(dmabuf->phys);
10293 }
10294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10295 /* The IOCTL status is embedded in the mailbox subheader. */
10296 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10297 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10298 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10299 if (shdr_status || shdr_add_status || rc) {
10300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10301 "2504 RQ_CREATE mailbox failed with "
10302 "status x%x add_status x%x, mbx status x%x\n",
10303 shdr_status, shdr_add_status, rc);
10304 status = -ENXIO;
10305 goto out;
10306 }
10307 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10308 if (hrq->queue_id == 0xFFFF) {
10309 status = -ENXIO;
10310 goto out;
10311 }
10312 hrq->type = LPFC_HRQ;
10313 hrq->subtype = subtype;
10314 hrq->host_index = 0;
10315 hrq->hba_index = 0;
10316
10317 /* now create the data queue */
10318 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10319 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10320 length, LPFC_SLI4_MBX_EMBED);
10321 switch (drq->entry_count) {
10322 default:
10323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10324 "2536 Unsupported RQ count. (%d)\n",
10325 drq->entry_count);
10326 if (drq->entry_count < 512)
10327 return -EINVAL;
10328 /* otherwise default to smallest count (drop through) */
10329 case 512:
10330 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10331 LPFC_RQ_RING_SIZE_512);
10332 break;
10333 case 1024:
10334 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10335 LPFC_RQ_RING_SIZE_1024);
10336 break;
10337 case 2048:
10338 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10339 LPFC_RQ_RING_SIZE_2048);
10340 break;
10341 case 4096:
10342 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10343 LPFC_RQ_RING_SIZE_4096);
10344 break;
10345 }
10346 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10347 cq->queue_id);
10348 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10349 drq->page_count);
10350 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10351 LPFC_DATA_BUF_SIZE);
10352 list_for_each_entry(dmabuf, &drq->page_list, list) {
10353 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10354 putPaddrLow(dmabuf->phys);
10355 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10356 putPaddrHigh(dmabuf->phys);
10357 }
10358 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10359 /* The IOCTL status is embedded in the mailbox subheader. */
10360 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10361 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10362 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10363 if (shdr_status || shdr_add_status || rc) {
10364 status = -ENXIO;
10365 goto out;
10366 }
10367 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10368 if (drq->queue_id == 0xFFFF) {
10369 status = -ENXIO;
10370 goto out;
10371 }
10372 drq->type = LPFC_DRQ;
10373 drq->subtype = subtype;
10374 drq->host_index = 0;
10375 drq->hba_index = 0;
10376
10377 /* link the header and data RQs onto the parent cq child list */
10378 list_add_tail(&hrq->list, &cq->child_list);
10379 list_add_tail(&drq->list, &cq->child_list);
10380
10381out:
James Smart8fa38512009-07-19 10:01:03 -040010382 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010383 return status;
10384}
10385
10386/**
10387 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10388 * @eq: The queue structure associated with the queue to destroy.
10389 *
10390 * This function destroys a queue, as detailed in @eq by sending an mailbox
10391 * command, specific to the type of queue, to the HBA.
10392 *
10393 * The @eq struct is used to get the queue ID of the queue to destroy.
10394 *
10395 * On success this function will return a zero. If the queue destroy mailbox
10396 * command fails this function will return ENXIO.
10397 **/
10398uint32_t
10399lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10400{
10401 LPFC_MBOXQ_t *mbox;
10402 int rc, length, status = 0;
10403 uint32_t shdr_status, shdr_add_status;
10404 union lpfc_sli4_cfg_shdr *shdr;
10405
10406 if (!eq)
10407 return -ENODEV;
10408 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10409 if (!mbox)
10410 return -ENOMEM;
10411 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10412 sizeof(struct lpfc_sli4_cfg_mhdr));
10413 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10414 LPFC_MBOX_OPCODE_EQ_DESTROY,
10415 length, LPFC_SLI4_MBX_EMBED);
10416 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10417 eq->queue_id);
10418 mbox->vport = eq->phba->pport;
10419 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10420
10421 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10422 /* The IOCTL status is embedded in the mailbox subheader. */
10423 shdr = (union lpfc_sli4_cfg_shdr *)
10424 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10425 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10426 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10427 if (shdr_status || shdr_add_status || rc) {
10428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10429 "2505 EQ_DESTROY mailbox failed with "
10430 "status x%x add_status x%x, mbx status x%x\n",
10431 shdr_status, shdr_add_status, rc);
10432 status = -ENXIO;
10433 }
10434
10435 /* Remove eq from any list */
10436 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010437 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010438 return status;
10439}
10440
10441/**
10442 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10443 * @cq: The queue structure associated with the queue to destroy.
10444 *
10445 * This function destroys a queue, as detailed in @cq by sending an mailbox
10446 * command, specific to the type of queue, to the HBA.
10447 *
10448 * The @cq struct is used to get the queue ID of the queue to destroy.
10449 *
10450 * On success this function will return a zero. If the queue destroy mailbox
10451 * command fails this function will return ENXIO.
10452 **/
10453uint32_t
10454lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10455{
10456 LPFC_MBOXQ_t *mbox;
10457 int rc, length, status = 0;
10458 uint32_t shdr_status, shdr_add_status;
10459 union lpfc_sli4_cfg_shdr *shdr;
10460
10461 if (!cq)
10462 return -ENODEV;
10463 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10464 if (!mbox)
10465 return -ENOMEM;
10466 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10467 sizeof(struct lpfc_sli4_cfg_mhdr));
10468 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10469 LPFC_MBOX_OPCODE_CQ_DESTROY,
10470 length, LPFC_SLI4_MBX_EMBED);
10471 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10472 cq->queue_id);
10473 mbox->vport = cq->phba->pport;
10474 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10475 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10476 /* The IOCTL status is embedded in the mailbox subheader. */
10477 shdr = (union lpfc_sli4_cfg_shdr *)
10478 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10479 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10480 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10481 if (shdr_status || shdr_add_status || rc) {
10482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10483 "2506 CQ_DESTROY mailbox failed with "
10484 "status x%x add_status x%x, mbx status x%x\n",
10485 shdr_status, shdr_add_status, rc);
10486 status = -ENXIO;
10487 }
10488 /* Remove cq from any list */
10489 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010490 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010491 return status;
10492}
10493
10494/**
James Smart04c68492009-05-22 14:52:52 -040010495 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10496 * @qm: The queue structure associated with the queue to destroy.
10497 *
10498 * This function destroys a queue, as detailed in @mq by sending an mailbox
10499 * command, specific to the type of queue, to the HBA.
10500 *
10501 * The @mq struct is used to get the queue ID of the queue to destroy.
10502 *
10503 * On success this function will return a zero. If the queue destroy mailbox
10504 * command fails this function will return ENXIO.
10505 **/
10506uint32_t
10507lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10508{
10509 LPFC_MBOXQ_t *mbox;
10510 int rc, length, status = 0;
10511 uint32_t shdr_status, shdr_add_status;
10512 union lpfc_sli4_cfg_shdr *shdr;
10513
10514 if (!mq)
10515 return -ENODEV;
10516 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10517 if (!mbox)
10518 return -ENOMEM;
10519 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10520 sizeof(struct lpfc_sli4_cfg_mhdr));
10521 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10522 LPFC_MBOX_OPCODE_MQ_DESTROY,
10523 length, LPFC_SLI4_MBX_EMBED);
10524 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10525 mq->queue_id);
10526 mbox->vport = mq->phba->pport;
10527 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10528 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10529 /* The IOCTL status is embedded in the mailbox subheader. */
10530 shdr = (union lpfc_sli4_cfg_shdr *)
10531 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10532 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10533 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10534 if (shdr_status || shdr_add_status || rc) {
10535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10536 "2507 MQ_DESTROY mailbox failed with "
10537 "status x%x add_status x%x, mbx status x%x\n",
10538 shdr_status, shdr_add_status, rc);
10539 status = -ENXIO;
10540 }
10541 /* Remove mq from any list */
10542 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010543 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010544 return status;
10545}
10546
10547/**
James Smart4f774512009-05-22 14:52:35 -040010548 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10549 * @wq: The queue structure associated with the queue to destroy.
10550 *
10551 * This function destroys a queue, as detailed in @wq by sending an mailbox
10552 * command, specific to the type of queue, to the HBA.
10553 *
10554 * The @wq struct is used to get the queue ID of the queue to destroy.
10555 *
10556 * On success this function will return a zero. If the queue destroy mailbox
10557 * command fails this function will return ENXIO.
10558 **/
10559uint32_t
10560lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10561{
10562 LPFC_MBOXQ_t *mbox;
10563 int rc, length, status = 0;
10564 uint32_t shdr_status, shdr_add_status;
10565 union lpfc_sli4_cfg_shdr *shdr;
10566
10567 if (!wq)
10568 return -ENODEV;
10569 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10570 if (!mbox)
10571 return -ENOMEM;
10572 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10573 sizeof(struct lpfc_sli4_cfg_mhdr));
10574 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10575 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10576 length, LPFC_SLI4_MBX_EMBED);
10577 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10578 wq->queue_id);
10579 mbox->vport = wq->phba->pport;
10580 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10581 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10582 shdr = (union lpfc_sli4_cfg_shdr *)
10583 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10584 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10585 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10586 if (shdr_status || shdr_add_status || rc) {
10587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10588 "2508 WQ_DESTROY mailbox failed with "
10589 "status x%x add_status x%x, mbx status x%x\n",
10590 shdr_status, shdr_add_status, rc);
10591 status = -ENXIO;
10592 }
10593 /* Remove wq from any list */
10594 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010595 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010596 return status;
10597}
10598
10599/**
10600 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10601 * @rq: The queue structure associated with the queue to destroy.
10602 *
10603 * This function destroys a queue, as detailed in @rq by sending an mailbox
10604 * command, specific to the type of queue, to the HBA.
10605 *
10606 * The @rq struct is used to get the queue ID of the queue to destroy.
10607 *
10608 * On success this function will return a zero. If the queue destroy mailbox
10609 * command fails this function will return ENXIO.
10610 **/
10611uint32_t
10612lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10613 struct lpfc_queue *drq)
10614{
10615 LPFC_MBOXQ_t *mbox;
10616 int rc, length, status = 0;
10617 uint32_t shdr_status, shdr_add_status;
10618 union lpfc_sli4_cfg_shdr *shdr;
10619
10620 if (!hrq || !drq)
10621 return -ENODEV;
10622 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10623 if (!mbox)
10624 return -ENOMEM;
10625 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10626 sizeof(struct mbox_header));
10627 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10628 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10629 length, LPFC_SLI4_MBX_EMBED);
10630 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10631 hrq->queue_id);
10632 mbox->vport = hrq->phba->pport;
10633 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10634 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10635 /* The IOCTL status is embedded in the mailbox subheader. */
10636 shdr = (union lpfc_sli4_cfg_shdr *)
10637 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10638 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10639 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10640 if (shdr_status || shdr_add_status || rc) {
10641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10642 "2509 RQ_DESTROY mailbox failed with "
10643 "status x%x add_status x%x, mbx status x%x\n",
10644 shdr_status, shdr_add_status, rc);
10645 if (rc != MBX_TIMEOUT)
10646 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10647 return -ENXIO;
10648 }
10649 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10650 drq->queue_id);
10651 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10652 shdr = (union lpfc_sli4_cfg_shdr *)
10653 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10654 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10655 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10656 if (shdr_status || shdr_add_status || rc) {
10657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10658 "2510 RQ_DESTROY mailbox failed with "
10659 "status x%x add_status x%x, mbx status x%x\n",
10660 shdr_status, shdr_add_status, rc);
10661 status = -ENXIO;
10662 }
10663 list_del_init(&hrq->list);
10664 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010665 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010666 return status;
10667}
10668
10669/**
10670 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10671 * @phba: The virtual port for which this call being executed.
10672 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10673 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10674 * @xritag: the xritag that ties this io to the SGL pages.
10675 *
10676 * This routine will post the sgl pages for the IO that has the xritag
10677 * that is in the iocbq structure. The xritag is assigned during iocbq
10678 * creation and persists for as long as the driver is loaded.
10679 * if the caller has fewer than 256 scatter gather segments to map then
10680 * pdma_phys_addr1 should be 0.
10681 * If the caller needs to map more than 256 scatter gather segment then
10682 * pdma_phys_addr1 should be a valid physical address.
10683 * physical address for SGLs must be 64 byte aligned.
10684 * If you are going to map 2 SGL's then the first one must have 256 entries
10685 * the second sgl can have between 1 and 256 entries.
10686 *
10687 * Return codes:
10688 * 0 - Success
10689 * -ENXIO, -ENOMEM - Failure
10690 **/
10691int
10692lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10693 dma_addr_t pdma_phys_addr0,
10694 dma_addr_t pdma_phys_addr1,
10695 uint16_t xritag)
10696{
10697 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10698 LPFC_MBOXQ_t *mbox;
10699 int rc;
10700 uint32_t shdr_status, shdr_add_status;
10701 union lpfc_sli4_cfg_shdr *shdr;
10702
10703 if (xritag == NO_XRI) {
10704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10705 "0364 Invalid param:\n");
10706 return -EINVAL;
10707 }
10708
10709 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10710 if (!mbox)
10711 return -ENOMEM;
10712
10713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10714 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10715 sizeof(struct lpfc_mbx_post_sgl_pages) -
10716 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10717
10718 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10719 &mbox->u.mqe.un.post_sgl_pages;
10720 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10721 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10722
10723 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10724 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10725 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10726 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10727
10728 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10729 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10730 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10731 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10732 if (!phba->sli4_hba.intr_enable)
10733 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10734 else
10735 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10736 /* The IOCTL status is embedded in the mailbox subheader. */
10737 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10738 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10739 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10740 if (rc != MBX_TIMEOUT)
10741 mempool_free(mbox, phba->mbox_mem_pool);
10742 if (shdr_status || shdr_add_status || rc) {
10743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10744 "2511 POST_SGL mailbox failed with "
10745 "status x%x add_status x%x, mbx status x%x\n",
10746 shdr_status, shdr_add_status, rc);
10747 rc = -ENXIO;
10748 }
10749 return 0;
10750}
10751/**
10752 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10753 * @phba: The virtual port for which this call being executed.
10754 *
10755 * This routine will remove all of the sgl pages registered with the hba.
10756 *
10757 * Return codes:
10758 * 0 - Success
10759 * -ENXIO, -ENOMEM - Failure
10760 **/
10761int
10762lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10763{
10764 LPFC_MBOXQ_t *mbox;
10765 int rc;
10766 uint32_t shdr_status, shdr_add_status;
10767 union lpfc_sli4_cfg_shdr *shdr;
10768
10769 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10770 if (!mbox)
10771 return -ENOMEM;
10772
10773 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10774 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10775 LPFC_SLI4_MBX_EMBED);
10776 if (!phba->sli4_hba.intr_enable)
10777 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10778 else
10779 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10780 /* The IOCTL status is embedded in the mailbox subheader. */
10781 shdr = (union lpfc_sli4_cfg_shdr *)
10782 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10783 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10784 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10785 if (rc != MBX_TIMEOUT)
10786 mempool_free(mbox, phba->mbox_mem_pool);
10787 if (shdr_status || shdr_add_status || rc) {
10788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10789 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10790 "status x%x add_status x%x, mbx status x%x\n",
10791 shdr_status, shdr_add_status, rc);
10792 rc = -ENXIO;
10793 }
10794 return rc;
10795}
10796
10797/**
10798 * lpfc_sli4_next_xritag - Get an xritag for the io
10799 * @phba: Pointer to HBA context object.
10800 *
10801 * This function gets an xritag for the iocb. If there is no unused xritag
10802 * it will return 0xffff.
10803 * The function returns the allocated xritag if successful, else returns zero.
10804 * Zero is not a valid xritag.
10805 * The caller is not required to hold any lock.
10806 **/
10807uint16_t
10808lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10809{
10810 uint16_t xritag;
10811
10812 spin_lock_irq(&phba->hbalock);
10813 xritag = phba->sli4_hba.next_xri;
10814 if ((xritag != (uint16_t) -1) && xritag <
10815 (phba->sli4_hba.max_cfg_param.max_xri
10816 + phba->sli4_hba.max_cfg_param.xri_base)) {
10817 phba->sli4_hba.next_xri++;
10818 phba->sli4_hba.max_cfg_param.xri_used++;
10819 spin_unlock_irq(&phba->hbalock);
10820 return xritag;
10821 }
10822 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010823 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010824 "2004 Failed to allocate XRI.last XRITAG is %d"
10825 " Max XRI is %d, Used XRI is %d\n",
10826 phba->sli4_hba.next_xri,
10827 phba->sli4_hba.max_cfg_param.max_xri,
10828 phba->sli4_hba.max_cfg_param.xri_used);
10829 return -1;
10830}
10831
10832/**
10833 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10834 * @phba: pointer to lpfc hba data structure.
10835 *
10836 * This routine is invoked to post a block of driver's sgl pages to the
10837 * HBA using non-embedded mailbox command. No Lock is held. This routine
10838 * is only called when the driver is loading and after all IO has been
10839 * stopped.
10840 **/
10841int
10842lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10843{
10844 struct lpfc_sglq *sglq_entry;
10845 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10846 struct sgl_page_pairs *sgl_pg_pairs;
10847 void *viraddr;
10848 LPFC_MBOXQ_t *mbox;
10849 uint32_t reqlen, alloclen, pg_pairs;
10850 uint32_t mbox_tmo;
10851 uint16_t xritag_start = 0;
10852 int els_xri_cnt, rc = 0;
10853 uint32_t shdr_status, shdr_add_status;
10854 union lpfc_sli4_cfg_shdr *shdr;
10855
10856 /* The number of sgls to be posted */
10857 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10858
10859 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10860 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010861 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010862 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10863 "2559 Block sgl registration required DMA "
10864 "size (%d) great than a page\n", reqlen);
10865 return -ENOMEM;
10866 }
10867 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10868 if (!mbox) {
10869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10870 "2560 Failed to allocate mbox cmd memory\n");
10871 return -ENOMEM;
10872 }
10873
10874 /* Allocate DMA memory and set up the non-embedded mailbox command */
10875 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10876 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10877 LPFC_SLI4_MBX_NEMBED);
10878
10879 if (alloclen < reqlen) {
10880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10881 "0285 Allocated DMA memory size (%d) is "
10882 "less than the requested DMA memory "
10883 "size (%d)\n", alloclen, reqlen);
10884 lpfc_sli4_mbox_cmd_free(phba, mbox);
10885 return -ENOMEM;
10886 }
James Smart4f774512009-05-22 14:52:35 -040010887 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010888 viraddr = mbox->sge_array->addr[0];
10889
10890 /* Set up the SGL pages in the non-embedded DMA pages */
10891 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10892 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10893
10894 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10895 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10896 /* Set up the sge entry */
10897 sgl_pg_pairs->sgl_pg0_addr_lo =
10898 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10899 sgl_pg_pairs->sgl_pg0_addr_hi =
10900 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10901 sgl_pg_pairs->sgl_pg1_addr_lo =
10902 cpu_to_le32(putPaddrLow(0));
10903 sgl_pg_pairs->sgl_pg1_addr_hi =
10904 cpu_to_le32(putPaddrHigh(0));
10905 /* Keep the first xritag on the list */
10906 if (pg_pairs == 0)
10907 xritag_start = sglq_entry->sli4_xritag;
10908 sgl_pg_pairs++;
10909 }
10910 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010911 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010912 /* Perform endian conversion if necessary */
10913 sgl->word0 = cpu_to_le32(sgl->word0);
10914
10915 if (!phba->sli4_hba.intr_enable)
10916 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10917 else {
10918 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10919 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10920 }
10921 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10922 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10923 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10924 if (rc != MBX_TIMEOUT)
10925 lpfc_sli4_mbox_cmd_free(phba, mbox);
10926 if (shdr_status || shdr_add_status || rc) {
10927 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10928 "2513 POST_SGL_BLOCK mailbox command failed "
10929 "status x%x add_status x%x mbx status x%x\n",
10930 shdr_status, shdr_add_status, rc);
10931 rc = -ENXIO;
10932 }
10933 return rc;
10934}
10935
10936/**
10937 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10938 * @phba: pointer to lpfc hba data structure.
10939 * @sblist: pointer to scsi buffer list.
10940 * @count: number of scsi buffers on the list.
10941 *
10942 * This routine is invoked to post a block of @count scsi sgl pages from a
10943 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10944 * No Lock is held.
10945 *
10946 **/
10947int
10948lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10949 int cnt)
10950{
10951 struct lpfc_scsi_buf *psb;
10952 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10953 struct sgl_page_pairs *sgl_pg_pairs;
10954 void *viraddr;
10955 LPFC_MBOXQ_t *mbox;
10956 uint32_t reqlen, alloclen, pg_pairs;
10957 uint32_t mbox_tmo;
10958 uint16_t xritag_start = 0;
10959 int rc = 0;
10960 uint32_t shdr_status, shdr_add_status;
10961 dma_addr_t pdma_phys_bpl1;
10962 union lpfc_sli4_cfg_shdr *shdr;
10963
10964 /* Calculate the requested length of the dma memory */
10965 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10966 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010967 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010968 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10969 "0217 Block sgl registration required DMA "
10970 "size (%d) great than a page\n", reqlen);
10971 return -ENOMEM;
10972 }
10973 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10974 if (!mbox) {
10975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10976 "0283 Failed to allocate mbox cmd memory\n");
10977 return -ENOMEM;
10978 }
10979
10980 /* Allocate DMA memory and set up the non-embedded mailbox command */
10981 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10982 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10983 LPFC_SLI4_MBX_NEMBED);
10984
10985 if (alloclen < reqlen) {
10986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10987 "2561 Allocated DMA memory size (%d) is "
10988 "less than the requested DMA memory "
10989 "size (%d)\n", alloclen, reqlen);
10990 lpfc_sli4_mbox_cmd_free(phba, mbox);
10991 return -ENOMEM;
10992 }
James Smart4f774512009-05-22 14:52:35 -040010993 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010994 viraddr = mbox->sge_array->addr[0];
10995
10996 /* Set up the SGL pages in the non-embedded DMA pages */
10997 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10998 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10999
11000 pg_pairs = 0;
11001 list_for_each_entry(psb, sblist, list) {
11002 /* Set up the sge entry */
11003 sgl_pg_pairs->sgl_pg0_addr_lo =
11004 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11005 sgl_pg_pairs->sgl_pg0_addr_hi =
11006 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11007 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11008 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11009 else
11010 pdma_phys_bpl1 = 0;
11011 sgl_pg_pairs->sgl_pg1_addr_lo =
11012 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11013 sgl_pg_pairs->sgl_pg1_addr_hi =
11014 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11015 /* Keep the first xritag on the list */
11016 if (pg_pairs == 0)
11017 xritag_start = psb->cur_iocbq.sli4_xritag;
11018 sgl_pg_pairs++;
11019 pg_pairs++;
11020 }
11021 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11022 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11023 /* Perform endian conversion if necessary */
11024 sgl->word0 = cpu_to_le32(sgl->word0);
11025
11026 if (!phba->sli4_hba.intr_enable)
11027 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11028 else {
11029 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11030 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11031 }
11032 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11033 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11034 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11035 if (rc != MBX_TIMEOUT)
11036 lpfc_sli4_mbox_cmd_free(phba, mbox);
11037 if (shdr_status || shdr_add_status || rc) {
11038 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11039 "2564 POST_SGL_BLOCK mailbox command failed "
11040 "status x%x add_status x%x mbx status x%x\n",
11041 shdr_status, shdr_add_status, rc);
11042 rc = -ENXIO;
11043 }
11044 return rc;
11045}
11046
11047/**
11048 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11049 * @phba: pointer to lpfc_hba struct that the frame was received on
11050 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11051 *
11052 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11053 * valid type of frame that the LPFC driver will handle. This function will
11054 * return a zero if the frame is a valid frame or a non zero value when the
11055 * frame does not pass the check.
11056 **/
11057static int
11058lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11059{
11060 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11061 char *type_names[] = FC_TYPE_NAMES_INIT;
11062 struct fc_vft_header *fc_vft_hdr;
11063
11064 switch (fc_hdr->fh_r_ctl) {
11065 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11066 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11067 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11068 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11069 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11070 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11071 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11072 case FC_RCTL_DD_CMD_STATUS: /* command status */
11073 case FC_RCTL_ELS_REQ: /* extended link services request */
11074 case FC_RCTL_ELS_REP: /* extended link services reply */
11075 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11076 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11077 case FC_RCTL_BA_NOP: /* basic link service NOP */
11078 case FC_RCTL_BA_ABTS: /* basic link service abort */
11079 case FC_RCTL_BA_RMC: /* remove connection */
11080 case FC_RCTL_BA_ACC: /* basic accept */
11081 case FC_RCTL_BA_RJT: /* basic reject */
11082 case FC_RCTL_BA_PRMT:
11083 case FC_RCTL_ACK_1: /* acknowledge_1 */
11084 case FC_RCTL_ACK_0: /* acknowledge_0 */
11085 case FC_RCTL_P_RJT: /* port reject */
11086 case FC_RCTL_F_RJT: /* fabric reject */
11087 case FC_RCTL_P_BSY: /* port busy */
11088 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11089 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11090 case FC_RCTL_LCR: /* link credit reset */
11091 case FC_RCTL_END: /* end */
11092 break;
11093 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11094 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11095 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11096 return lpfc_fc_frame_check(phba, fc_hdr);
11097 default:
11098 goto drop;
11099 }
11100 switch (fc_hdr->fh_type) {
11101 case FC_TYPE_BLS:
11102 case FC_TYPE_ELS:
11103 case FC_TYPE_FCP:
11104 case FC_TYPE_CT:
11105 break;
11106 case FC_TYPE_IP:
11107 case FC_TYPE_ILS:
11108 default:
11109 goto drop;
11110 }
11111 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11112 "2538 Received frame rctl:%s type:%s\n",
11113 rctl_names[fc_hdr->fh_r_ctl],
11114 type_names[fc_hdr->fh_type]);
11115 return 0;
11116drop:
11117 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11118 "2539 Dropped frame rctl:%s type:%s\n",
11119 rctl_names[fc_hdr->fh_r_ctl],
11120 type_names[fc_hdr->fh_type]);
11121 return 1;
11122}
11123
11124/**
11125 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11126 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11127 *
11128 * This function processes the FC header to retrieve the VFI from the VF
11129 * header, if one exists. This function will return the VFI if one exists
11130 * or 0 if no VSAN Header exists.
11131 **/
11132static uint32_t
11133lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11134{
11135 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11136
11137 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11138 return 0;
11139 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11140}
11141
11142/**
11143 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11144 * @phba: Pointer to the HBA structure to search for the vport on
11145 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11146 * @fcfi: The FC Fabric ID that the frame came from
11147 *
11148 * This function searches the @phba for a vport that matches the content of the
11149 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11150 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11151 * returns the matching vport pointer or NULL if unable to match frame to a
11152 * vport.
11153 **/
11154static struct lpfc_vport *
11155lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11156 uint16_t fcfi)
11157{
11158 struct lpfc_vport **vports;
11159 struct lpfc_vport *vport = NULL;
11160 int i;
11161 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11162 fc_hdr->fh_d_id[1] << 8 |
11163 fc_hdr->fh_d_id[2]);
11164
11165 vports = lpfc_create_vport_work_array(phba);
11166 if (vports != NULL)
11167 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11168 if (phba->fcf.fcfi == fcfi &&
11169 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11170 vports[i]->fc_myDID == did) {
11171 vport = vports[i];
11172 break;
11173 }
11174 }
11175 lpfc_destroy_vport_work_array(phba, vports);
11176 return vport;
11177}
11178
11179/**
James Smart45ed1192009-10-02 15:17:02 -040011180 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11181 * @vport: The vport to work on.
11182 *
11183 * This function updates the receive sequence time stamp for this vport. The
11184 * receive sequence time stamp indicates the time that the last frame of the
11185 * the sequence that has been idle for the longest amount of time was received.
11186 * the driver uses this time stamp to indicate if any received sequences have
11187 * timed out.
11188 **/
11189void
11190lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11191{
11192 struct lpfc_dmabuf *h_buf;
11193 struct hbq_dmabuf *dmabuf = NULL;
11194
11195 /* get the oldest sequence on the rcv list */
11196 h_buf = list_get_first(&vport->rcv_buffer_list,
11197 struct lpfc_dmabuf, list);
11198 if (!h_buf)
11199 return;
11200 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11201 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11202}
11203
11204/**
11205 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11206 * @vport: The vport that the received sequences were sent to.
11207 *
11208 * This function cleans up all outstanding received sequences. This is called
11209 * by the driver when a link event or user action invalidates all the received
11210 * sequences.
11211 **/
11212void
11213lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11214{
11215 struct lpfc_dmabuf *h_buf, *hnext;
11216 struct lpfc_dmabuf *d_buf, *dnext;
11217 struct hbq_dmabuf *dmabuf = NULL;
11218
11219 /* start with the oldest sequence on the rcv list */
11220 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11221 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11222 list_del_init(&dmabuf->hbuf.list);
11223 list_for_each_entry_safe(d_buf, dnext,
11224 &dmabuf->dbuf.list, list) {
11225 list_del_init(&d_buf->list);
11226 lpfc_in_buf_free(vport->phba, d_buf);
11227 }
11228 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11229 }
11230}
11231
11232/**
11233 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11234 * @vport: The vport that the received sequences were sent to.
11235 *
11236 * This function determines whether any received sequences have timed out by
11237 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11238 * indicates that there is at least one timed out sequence this routine will
11239 * go through the received sequences one at a time from most inactive to most
11240 * active to determine which ones need to be cleaned up. Once it has determined
11241 * that a sequence needs to be cleaned up it will simply free up the resources
11242 * without sending an abort.
11243 **/
11244void
11245lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11246{
11247 struct lpfc_dmabuf *h_buf, *hnext;
11248 struct lpfc_dmabuf *d_buf, *dnext;
11249 struct hbq_dmabuf *dmabuf = NULL;
11250 unsigned long timeout;
11251 int abort_count = 0;
11252
11253 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11254 vport->rcv_buffer_time_stamp);
11255 if (list_empty(&vport->rcv_buffer_list) ||
11256 time_before(jiffies, timeout))
11257 return;
11258 /* start with the oldest sequence on the rcv list */
11259 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11260 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11261 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11262 dmabuf->time_stamp);
11263 if (time_before(jiffies, timeout))
11264 break;
11265 abort_count++;
11266 list_del_init(&dmabuf->hbuf.list);
11267 list_for_each_entry_safe(d_buf, dnext,
11268 &dmabuf->dbuf.list, list) {
11269 list_del_init(&d_buf->list);
11270 lpfc_in_buf_free(vport->phba, d_buf);
11271 }
11272 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11273 }
11274 if (abort_count)
11275 lpfc_update_rcv_time_stamp(vport);
11276}
11277
11278/**
James Smart4f774512009-05-22 14:52:35 -040011279 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11280 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11281 *
11282 * This function searches through the existing incomplete sequences that have
11283 * been sent to this @vport. If the frame matches one of the incomplete
11284 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11285 * make up that sequence. If no sequence is found that matches this frame then
11286 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11287 * This function returns a pointer to the first dmabuf in the sequence list that
11288 * the frame was linked to.
11289 **/
11290static struct hbq_dmabuf *
11291lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11292{
11293 struct fc_frame_header *new_hdr;
11294 struct fc_frame_header *temp_hdr;
11295 struct lpfc_dmabuf *d_buf;
11296 struct lpfc_dmabuf *h_buf;
11297 struct hbq_dmabuf *seq_dmabuf = NULL;
11298 struct hbq_dmabuf *temp_dmabuf = NULL;
11299
James Smart4d9ab992009-10-02 15:16:39 -040011300 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011301 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011302 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11303 /* Use the hdr_buf to find the sequence that this frame belongs to */
11304 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11305 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11306 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11307 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11308 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11309 continue;
11310 /* found a pending sequence that matches this frame */
11311 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11312 break;
11313 }
11314 if (!seq_dmabuf) {
11315 /*
11316 * This indicates first frame received for this sequence.
11317 * Queue the buffer on the vport's rcv_buffer_list.
11318 */
11319 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011320 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011321 return dmabuf;
11322 }
11323 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011324 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11325 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011326 list_del_init(&seq_dmabuf->hbuf.list);
11327 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11328 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011329 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011330 return dmabuf;
11331 }
James Smart45ed1192009-10-02 15:17:02 -040011332 /* move this sequence to the tail to indicate a young sequence */
11333 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11334 seq_dmabuf->time_stamp = jiffies;
11335 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011336 if (list_empty(&seq_dmabuf->dbuf.list)) {
11337 temp_hdr = dmabuf->hbuf.virt;
11338 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11339 return seq_dmabuf;
11340 }
James Smart4f774512009-05-22 14:52:35 -040011341 /* find the correct place in the sequence to insert this frame */
11342 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11343 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11344 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11345 /*
11346 * If the frame's sequence count is greater than the frame on
11347 * the list then insert the frame right after this frame
11348 */
James Smarteeead812009-12-21 17:01:23 -050011349 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11350 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011351 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11352 return seq_dmabuf;
11353 }
11354 }
11355 return NULL;
11356}
11357
11358/**
James Smart6669f9b2009-10-02 15:16:45 -040011359 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11360 * @vport: pointer to a vitural port
11361 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11362 *
11363 * This function tries to abort from the partially assembed sequence, described
11364 * by the information from basic abbort @dmabuf. It checks to see whether such
11365 * partially assembled sequence held by the driver. If so, it shall free up all
11366 * the frames from the partially assembled sequence.
11367 *
11368 * Return
11369 * true -- if there is matching partially assembled sequence present and all
11370 * the frames freed with the sequence;
11371 * false -- if there is no matching partially assembled sequence present so
11372 * nothing got aborted in the lower layer driver
11373 **/
11374static bool
11375lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11376 struct hbq_dmabuf *dmabuf)
11377{
11378 struct fc_frame_header *new_hdr;
11379 struct fc_frame_header *temp_hdr;
11380 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11381 struct hbq_dmabuf *seq_dmabuf = NULL;
11382
11383 /* Use the hdr_buf to find the sequence that matches this frame */
11384 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11385 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11386 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11387 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11388 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11389 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11390 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11391 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11392 continue;
11393 /* found a pending sequence that matches this frame */
11394 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11395 break;
11396 }
11397
11398 /* Free up all the frames from the partially assembled sequence */
11399 if (seq_dmabuf) {
11400 list_for_each_entry_safe(d_buf, n_buf,
11401 &seq_dmabuf->dbuf.list, list) {
11402 list_del_init(&d_buf->list);
11403 lpfc_in_buf_free(vport->phba, d_buf);
11404 }
11405 return true;
11406 }
11407 return false;
11408}
11409
11410/**
11411 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11412 * @phba: Pointer to HBA context object.
11413 * @cmd_iocbq: pointer to the command iocbq structure.
11414 * @rsp_iocbq: pointer to the response iocbq structure.
11415 *
11416 * This function handles the sequence abort accept iocb command complete
11417 * event. It properly releases the memory allocated to the sequence abort
11418 * accept iocb.
11419 **/
11420static void
11421lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11422 struct lpfc_iocbq *cmd_iocbq,
11423 struct lpfc_iocbq *rsp_iocbq)
11424{
11425 if (cmd_iocbq)
11426 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11427}
11428
11429/**
11430 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11431 * @phba: Pointer to HBA context object.
11432 * @fc_hdr: pointer to a FC frame header.
11433 *
11434 * This function sends a basic accept to a previous unsol sequence abort
11435 * event after aborting the sequence handling.
11436 **/
11437static void
11438lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11439 struct fc_frame_header *fc_hdr)
11440{
11441 struct lpfc_iocbq *ctiocb = NULL;
11442 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011443 uint16_t oxid, rxid;
11444 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011445 IOCB_t *icmd;
11446
11447 if (!lpfc_is_link_up(phba))
11448 return;
11449
11450 sid = sli4_sid_from_fc_hdr(fc_hdr);
11451 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011452 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011453
11454 ndlp = lpfc_findnode_did(phba->pport, sid);
11455 if (!ndlp) {
11456 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11457 "1268 Find ndlp returned NULL for oxid:x%x "
11458 "SID:x%x\n", oxid, sid);
11459 return;
11460 }
11461
11462 /* Allocate buffer for acc iocb */
11463 ctiocb = lpfc_sli_get_iocbq(phba);
11464 if (!ctiocb)
11465 return;
11466
James Smart5ffc2662009-11-18 15:39:44 -050011467 /* Extract the F_CTL field from FC_HDR */
11468 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11469
James Smart6669f9b2009-10-02 15:16:45 -040011470 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011471 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011472 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011473 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11474 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11475 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11476
11477 /* Fill in the rest of iocb fields */
11478 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11479 icmd->ulpBdeCount = 0;
11480 icmd->ulpLe = 1;
11481 icmd->ulpClass = CLASS3;
11482 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011483
James Smart6669f9b2009-10-02 15:16:45 -040011484 ctiocb->iocb_cmpl = NULL;
11485 ctiocb->vport = phba->pport;
11486 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11487
James Smart5ffc2662009-11-18 15:39:44 -050011488 if (fctl & FC_FC_EX_CTX) {
11489 /* ABTS sent by responder to CT exchange, construction
11490 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11491 * field and RX_ID from ABTS for RX_ID field.
11492 */
11493 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11494 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11495 ctiocb->sli4_xritag = oxid;
11496 } else {
11497 /* ABTS sent by initiator to CT exchange, construction
11498 * of BA_ACC will need to allocate a new XRI as for the
11499 * XRI_TAG and RX_ID fields.
11500 */
11501 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11502 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11503 ctiocb->sli4_xritag = NO_XRI;
11504 }
11505 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11506
James Smart6669f9b2009-10-02 15:16:45 -040011507 /* Xmit CT abts accept on exchange <xid> */
11508 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11509 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11510 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11511 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11512}
11513
11514/**
11515 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11516 * @vport: Pointer to the vport on which this sequence was received
11517 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11518 *
11519 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11520 * receive sequence is only partially assembed by the driver, it shall abort
11521 * the partially assembled frames for the sequence. Otherwise, if the
11522 * unsolicited receive sequence has been completely assembled and passed to
11523 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11524 * unsolicited sequence has been aborted. After that, it will issue a basic
11525 * accept to accept the abort.
11526 **/
11527void
11528lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11529 struct hbq_dmabuf *dmabuf)
11530{
11531 struct lpfc_hba *phba = vport->phba;
11532 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011533 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011534 bool abts_par;
11535
James Smart6669f9b2009-10-02 15:16:45 -040011536 /* Make a copy of fc_hdr before the dmabuf being released */
11537 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011538 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011539
James Smart5ffc2662009-11-18 15:39:44 -050011540 if (fctl & FC_FC_EX_CTX) {
11541 /*
11542 * ABTS sent by responder to exchange, just free the buffer
11543 */
James Smart6669f9b2009-10-02 15:16:45 -040011544 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011545 } else {
11546 /*
11547 * ABTS sent by initiator to exchange, need to do cleanup
11548 */
11549 /* Try to abort partially assembled seq */
11550 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11551
11552 /* Send abort to ULP if partially seq abort failed */
11553 if (abts_par == false)
11554 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11555 else
11556 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11557 }
James Smart6669f9b2009-10-02 15:16:45 -040011558 /* Send basic accept (BA_ACC) to the abort requester */
11559 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11560}
11561
11562/**
James Smart4f774512009-05-22 14:52:35 -040011563 * lpfc_seq_complete - Indicates if a sequence is complete
11564 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11565 *
11566 * This function checks the sequence, starting with the frame described by
11567 * @dmabuf, to see if all the frames associated with this sequence are present.
11568 * the frames associated with this sequence are linked to the @dmabuf using the
11569 * dbuf list. This function looks for two major things. 1) That the first frame
11570 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11571 * set. 3) That there are no holes in the sequence count. The function will
11572 * return 1 when the sequence is complete, otherwise it will return 0.
11573 **/
11574static int
11575lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11576{
11577 struct fc_frame_header *hdr;
11578 struct lpfc_dmabuf *d_buf;
11579 struct hbq_dmabuf *seq_dmabuf;
11580 uint32_t fctl;
11581 int seq_count = 0;
11582
11583 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11584 /* make sure first fame of sequence has a sequence count of zero */
11585 if (hdr->fh_seq_cnt != seq_count)
11586 return 0;
11587 fctl = (hdr->fh_f_ctl[0] << 16 |
11588 hdr->fh_f_ctl[1] << 8 |
11589 hdr->fh_f_ctl[2]);
11590 /* If last frame of sequence we can return success. */
11591 if (fctl & FC_FC_END_SEQ)
11592 return 1;
11593 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11594 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11595 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11596 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011597 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011598 return 0;
11599 fctl = (hdr->fh_f_ctl[0] << 16 |
11600 hdr->fh_f_ctl[1] << 8 |
11601 hdr->fh_f_ctl[2]);
11602 /* If last frame of sequence we can return success. */
11603 if (fctl & FC_FC_END_SEQ)
11604 return 1;
11605 }
11606 return 0;
11607}
11608
11609/**
11610 * lpfc_prep_seq - Prep sequence for ULP processing
11611 * @vport: Pointer to the vport on which this sequence was received
11612 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11613 *
11614 * This function takes a sequence, described by a list of frames, and creates
11615 * a list of iocbq structures to describe the sequence. This iocbq list will be
11616 * used to issue to the generic unsolicited sequence handler. This routine
11617 * returns a pointer to the first iocbq in the list. If the function is unable
11618 * to allocate an iocbq then it throw out the received frames that were not
11619 * able to be described and return a pointer to the first iocbq. If unable to
11620 * allocate any iocbqs (including the first) this function will return NULL.
11621 **/
11622static struct lpfc_iocbq *
11623lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11624{
11625 struct lpfc_dmabuf *d_buf, *n_buf;
11626 struct lpfc_iocbq *first_iocbq, *iocbq;
11627 struct fc_frame_header *fc_hdr;
11628 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011629 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011630
11631 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11632 /* remove from receive buffer list */
11633 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011634 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011635 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011636 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011637 /* Get an iocbq struct to fill in. */
11638 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11639 if (first_iocbq) {
11640 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011641 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011642 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11643 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11644 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11645 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11646 vport->vpi + vport->phba->vpi_base;
11647 /* put the first buffer into the first IOCBq */
11648 first_iocbq->context2 = &seq_dmabuf->dbuf;
11649 first_iocbq->context3 = NULL;
11650 first_iocbq->iocb.ulpBdeCount = 1;
11651 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11652 LPFC_DATA_BUF_SIZE;
11653 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011654 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011655 bf_get(lpfc_rcqe_length,
11656 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011657 }
11658 iocbq = first_iocbq;
11659 /*
11660 * Each IOCBq can have two Buffers assigned, so go through the list
11661 * of buffers for this sequence and save two buffers in each IOCBq
11662 */
11663 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11664 if (!iocbq) {
11665 lpfc_in_buf_free(vport->phba, d_buf);
11666 continue;
11667 }
11668 if (!iocbq->context3) {
11669 iocbq->context3 = d_buf;
11670 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011671 pbde = (struct ulp_bde64 *)
11672 &iocbq->iocb.unsli3.sli3Words[4];
11673 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011674 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011675 bf_get(lpfc_rcqe_length,
11676 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011677 } else {
11678 iocbq = lpfc_sli_get_iocbq(vport->phba);
11679 if (!iocbq) {
11680 if (first_iocbq) {
11681 first_iocbq->iocb.ulpStatus =
11682 IOSTAT_FCP_RSP_ERROR;
11683 first_iocbq->iocb.un.ulpWord[4] =
11684 IOERR_NO_RESOURCES;
11685 }
11686 lpfc_in_buf_free(vport->phba, d_buf);
11687 continue;
11688 }
11689 iocbq->context2 = d_buf;
11690 iocbq->context3 = NULL;
11691 iocbq->iocb.ulpBdeCount = 1;
11692 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11693 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011694 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011695 bf_get(lpfc_rcqe_length,
11696 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011697 iocbq->iocb.un.rcvels.remoteID = sid;
11698 list_add_tail(&iocbq->list, &first_iocbq->list);
11699 }
11700 }
11701 return first_iocbq;
11702}
11703
James Smart6669f9b2009-10-02 15:16:45 -040011704static void
11705lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11706 struct hbq_dmabuf *seq_dmabuf)
11707{
11708 struct fc_frame_header *fc_hdr;
11709 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11710 struct lpfc_hba *phba = vport->phba;
11711
11712 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11713 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11714 if (!iocbq) {
11715 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11716 "2707 Ring %d handler: Failed to allocate "
11717 "iocb Rctl x%x Type x%x received\n",
11718 LPFC_ELS_RING,
11719 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11720 return;
11721 }
11722 if (!lpfc_complete_unsol_iocb(phba,
11723 &phba->sli.ring[LPFC_ELS_RING],
11724 iocbq, fc_hdr->fh_r_ctl,
11725 fc_hdr->fh_type))
11726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11727 "2540 Ring %d handler: unexpected Rctl "
11728 "x%x Type x%x received\n",
11729 LPFC_ELS_RING,
11730 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11731
11732 /* Free iocb created in lpfc_prep_seq */
11733 list_for_each_entry_safe(curr_iocb, next_iocb,
11734 &iocbq->list, list) {
11735 list_del_init(&curr_iocb->list);
11736 lpfc_sli_release_iocbq(phba, curr_iocb);
11737 }
11738 lpfc_sli_release_iocbq(phba, iocbq);
11739}
11740
James Smart4f774512009-05-22 14:52:35 -040011741/**
11742 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11743 * @phba: Pointer to HBA context object.
11744 *
11745 * This function is called with no lock held. This function processes all
11746 * the received buffers and gives it to upper layers when a received buffer
11747 * indicates that it is the final frame in the sequence. The interrupt
11748 * service routine processes received buffers at interrupt contexts and adds
11749 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11750 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11751 * appropriate receive function when the final frame in a sequence is received.
11752 **/
James Smart4d9ab992009-10-02 15:16:39 -040011753void
11754lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11755 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011756{
James Smart4d9ab992009-10-02 15:16:39 -040011757 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011758 struct fc_frame_header *fc_hdr;
11759 struct lpfc_vport *vport;
11760 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011761
James Smart4f774512009-05-22 14:52:35 -040011762 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011763 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11764 /* check to see if this a valid type of frame */
11765 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11766 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11767 return;
11768 }
11769 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11770 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011771 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011772 /* throw out the frame */
11773 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11774 return;
11775 }
James Smart6669f9b2009-10-02 15:16:45 -040011776 /* Handle the basic abort sequence (BA_ABTS) event */
11777 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11778 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11779 return;
11780 }
11781
James Smart4d9ab992009-10-02 15:16:39 -040011782 /* Link this frame */
11783 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11784 if (!seq_dmabuf) {
11785 /* unable to add frame to vport - throw it out */
11786 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11787 return;
11788 }
11789 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011790 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011791 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011792
James Smart6669f9b2009-10-02 15:16:45 -040011793 /* Send the complete sequence to the upper layer protocol */
11794 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011795}
James Smart6fb120a2009-05-22 14:52:59 -040011796
11797/**
11798 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11799 * @phba: pointer to lpfc hba data structure.
11800 *
11801 * This routine is invoked to post rpi header templates to the
11802 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011803 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11804 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011805 *
11806 * This routine does not require any locks. It's usage is expected
11807 * to be driver load or reset recovery when the driver is
11808 * sequential.
11809 *
11810 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011811 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011812 * EIO - The mailbox failed to complete successfully.
11813 * When this error occurs, the driver is not guaranteed
11814 * to have any rpi regions posted to the device and
11815 * must either attempt to repost the regions or take a
11816 * fatal error.
11817 **/
11818int
11819lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11820{
11821 struct lpfc_rpi_hdr *rpi_page;
11822 uint32_t rc = 0;
11823
11824 /* Post all rpi memory regions to the port. */
11825 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11826 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11827 if (rc != MBX_SUCCESS) {
11828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11829 "2008 Error %d posting all rpi "
11830 "headers\n", rc);
11831 rc = -EIO;
11832 break;
11833 }
11834 }
11835
11836 return rc;
11837}
11838
11839/**
11840 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11841 * @phba: pointer to lpfc hba data structure.
11842 * @rpi_page: pointer to the rpi memory region.
11843 *
11844 * This routine is invoked to post a single rpi header to the
11845 * HBA consistent with the SLI-4 interface spec. This memory region
11846 * maps up to 64 rpi context regions.
11847 *
11848 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011849 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011850 * ENOMEM - No available memory
11851 * EIO - The mailbox failed to complete successfully.
11852 **/
11853int
11854lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11855{
11856 LPFC_MBOXQ_t *mboxq;
11857 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11858 uint32_t rc = 0;
11859 uint32_t mbox_tmo;
11860 uint32_t shdr_status, shdr_add_status;
11861 union lpfc_sli4_cfg_shdr *shdr;
11862
11863 /* The port is notified of the header region via a mailbox command. */
11864 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11865 if (!mboxq) {
11866 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11867 "2001 Unable to allocate memory for issuing "
11868 "SLI_CONFIG_SPECIAL mailbox command\n");
11869 return -ENOMEM;
11870 }
11871
11872 /* Post all rpi memory regions to the port. */
11873 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11874 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11875 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11876 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11877 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11878 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11879 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11880 hdr_tmpl, rpi_page->page_count);
11881 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11882 rpi_page->start_rpi);
11883 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11884 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011885 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011886 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11887 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11889 if (rc != MBX_TIMEOUT)
11890 mempool_free(mboxq, phba->mbox_mem_pool);
11891 if (shdr_status || shdr_add_status || rc) {
11892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11893 "2514 POST_RPI_HDR mailbox failed with "
11894 "status x%x add_status x%x, mbx status x%x\n",
11895 shdr_status, shdr_add_status, rc);
11896 rc = -ENXIO;
11897 }
11898 return rc;
11899}
11900
11901/**
11902 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11903 * @phba: pointer to lpfc hba data structure.
11904 *
11905 * This routine is invoked to post rpi header templates to the
11906 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011907 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11908 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011909 *
11910 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011911 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011912 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11913 **/
11914int
11915lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11916{
11917 int rpi;
11918 uint16_t max_rpi, rpi_base, rpi_limit;
11919 uint16_t rpi_remaining;
11920 struct lpfc_rpi_hdr *rpi_hdr;
11921
11922 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11923 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11924 rpi_limit = phba->sli4_hba.next_rpi;
11925
11926 /*
11927 * The valid rpi range is not guaranteed to be zero-based. Start
11928 * the search at the rpi_base as reported by the port.
11929 */
11930 spin_lock_irq(&phba->hbalock);
11931 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11932 if (rpi >= rpi_limit || rpi < rpi_base)
11933 rpi = LPFC_RPI_ALLOC_ERROR;
11934 else {
11935 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11936 phba->sli4_hba.max_cfg_param.rpi_used++;
11937 phba->sli4_hba.rpi_count++;
11938 }
11939
11940 /*
11941 * Don't try to allocate more rpi header regions if the device limit
11942 * on available rpis max has been exhausted.
11943 */
11944 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11945 (phba->sli4_hba.rpi_count >= max_rpi)) {
11946 spin_unlock_irq(&phba->hbalock);
11947 return rpi;
11948 }
11949
11950 /*
11951 * If the driver is running low on rpi resources, allocate another
11952 * page now. Note that the next_rpi value is used because
11953 * it represents how many are actually in use whereas max_rpi notes
11954 * how many are supported max by the device.
11955 */
11956 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11957 phba->sli4_hba.rpi_count;
11958 spin_unlock_irq(&phba->hbalock);
11959 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11960 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11961 if (!rpi_hdr) {
11962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11963 "2002 Error Could not grow rpi "
11964 "count\n");
11965 } else {
11966 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11967 }
11968 }
11969
11970 return rpi;
11971}
11972
11973/**
11974 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11975 * @phba: pointer to lpfc hba data structure.
11976 *
11977 * This routine is invoked to release an rpi to the pool of
11978 * available rpis maintained by the driver.
11979 **/
11980void
James Smartd7c47992010-06-08 18:31:54 -040011981__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11982{
11983 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
11984 phba->sli4_hba.rpi_count--;
11985 phba->sli4_hba.max_cfg_param.rpi_used--;
11986 }
11987}
11988
11989/**
11990 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11991 * @phba: pointer to lpfc hba data structure.
11992 *
11993 * This routine is invoked to release an rpi to the pool of
11994 * available rpis maintained by the driver.
11995 **/
11996void
James Smart6fb120a2009-05-22 14:52:59 -040011997lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11998{
11999 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012000 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012001 spin_unlock_irq(&phba->hbalock);
12002}
12003
12004/**
12005 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12006 * @phba: pointer to lpfc hba data structure.
12007 *
12008 * This routine is invoked to remove the memory region that
12009 * provided rpi via a bitmask.
12010 **/
12011void
12012lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12013{
12014 kfree(phba->sli4_hba.rpi_bmask);
12015}
12016
12017/**
12018 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12019 * @phba: pointer to lpfc hba data structure.
12020 *
12021 * This routine is invoked to remove the memory region that
12022 * provided rpi via a bitmask.
12023 **/
12024int
12025lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12026{
12027 LPFC_MBOXQ_t *mboxq;
12028 struct lpfc_hba *phba = ndlp->phba;
12029 int rc;
12030
12031 /* The port is notified of the header region via a mailbox command. */
12032 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12033 if (!mboxq)
12034 return -ENOMEM;
12035
12036 /* Post all rpi memory regions to the port. */
12037 lpfc_resume_rpi(mboxq, ndlp);
12038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12039 if (rc == MBX_NOT_FINISHED) {
12040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12041 "2010 Resume RPI Mailbox failed "
12042 "status %d, mbxStatus x%x\n", rc,
12043 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12044 mempool_free(mboxq, phba->mbox_mem_pool);
12045 return -EIO;
12046 }
12047 return 0;
12048}
12049
12050/**
12051 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12052 * @phba: pointer to lpfc hba data structure.
12053 * @vpi: vpi value to activate with the port.
12054 *
12055 * This routine is invoked to activate a vpi with the
12056 * port when the host intends to use vports with a
12057 * nonzero vpi.
12058 *
12059 * Returns:
12060 * 0 success
12061 * -Evalue otherwise
12062 **/
12063int
12064lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12065{
12066 LPFC_MBOXQ_t *mboxq;
12067 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012068 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012069 uint32_t mbox_tmo;
12070
12071 if (vpi == 0)
12072 return -EINVAL;
12073 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12074 if (!mboxq)
12075 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040012076 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012077 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12078 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012079 if (rc != MBX_SUCCESS) {
12080 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12081 "2022 INIT VPI Mailbox failed "
12082 "status %d, mbxStatus x%x\n", rc,
12083 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012084 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012085 }
James Smart6a9c52c2009-10-02 15:16:51 -040012086 if (rc != MBX_TIMEOUT)
12087 mempool_free(mboxq, phba->mbox_mem_pool);
12088
12089 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012090}
12091
12092/**
12093 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12094 * @phba: pointer to lpfc hba data structure.
12095 * @mboxq: Pointer to mailbox object.
12096 *
12097 * This routine is invoked to manually add a single FCF record. The caller
12098 * must pass a completely initialized FCF_Record. This routine takes
12099 * care of the nonembedded mailbox operations.
12100 **/
12101static void
12102lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12103{
12104 void *virt_addr;
12105 union lpfc_sli4_cfg_shdr *shdr;
12106 uint32_t shdr_status, shdr_add_status;
12107
12108 virt_addr = mboxq->sge_array->addr[0];
12109 /* The IOCTL status is embedded in the mailbox subheader. */
12110 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12111 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12112 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12113
12114 if ((shdr_status || shdr_add_status) &&
12115 (shdr_status != STATUS_FCF_IN_USE))
12116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12117 "2558 ADD_FCF_RECORD mailbox failed with "
12118 "status x%x add_status x%x\n",
12119 shdr_status, shdr_add_status);
12120
12121 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12122}
12123
12124/**
12125 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12126 * @phba: pointer to lpfc hba data structure.
12127 * @fcf_record: pointer to the initialized fcf record to add.
12128 *
12129 * This routine is invoked to manually add a single FCF record. The caller
12130 * must pass a completely initialized FCF_Record. This routine takes
12131 * care of the nonembedded mailbox operations.
12132 **/
12133int
12134lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12135{
12136 int rc = 0;
12137 LPFC_MBOXQ_t *mboxq;
12138 uint8_t *bytep;
12139 void *virt_addr;
12140 dma_addr_t phys_addr;
12141 struct lpfc_mbx_sge sge;
12142 uint32_t alloc_len, req_len;
12143 uint32_t fcfindex;
12144
12145 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12146 if (!mboxq) {
12147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12148 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12149 return -ENOMEM;
12150 }
12151
12152 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12153 sizeof(uint32_t);
12154
12155 /* Allocate DMA memory and set up the non-embedded mailbox command */
12156 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12157 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12158 req_len, LPFC_SLI4_MBX_NEMBED);
12159 if (alloc_len < req_len) {
12160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12161 "2523 Allocated DMA memory size (x%x) is "
12162 "less than the requested DMA memory "
12163 "size (x%x)\n", alloc_len, req_len);
12164 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12165 return -ENOMEM;
12166 }
12167
12168 /*
12169 * Get the first SGE entry from the non-embedded DMA memory. This
12170 * routine only uses a single SGE.
12171 */
12172 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12173 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012174 virt_addr = mboxq->sge_array->addr[0];
12175 /*
12176 * Configure the FCF record for FCFI 0. This is the driver's
12177 * hardcoded default and gets used in nonFIP mode.
12178 */
12179 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12180 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12181 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12182
12183 /*
12184 * Copy the fcf_index and the FCF Record Data. The data starts after
12185 * the FCoE header plus word10. The data copy needs to be endian
12186 * correct.
12187 */
12188 bytep += sizeof(uint32_t);
12189 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12190 mboxq->vport = phba->pport;
12191 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12192 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12193 if (rc == MBX_NOT_FINISHED) {
12194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12195 "2515 ADD_FCF_RECORD mailbox failed with "
12196 "status 0x%x\n", rc);
12197 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12198 rc = -EIO;
12199 } else
12200 rc = 0;
12201
12202 return rc;
12203}
12204
12205/**
12206 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12207 * @phba: pointer to lpfc hba data structure.
12208 * @fcf_record: pointer to the fcf record to write the default data.
12209 * @fcf_index: FCF table entry index.
12210 *
12211 * This routine is invoked to build the driver's default FCF record. The
12212 * values used are hardcoded. This routine handles memory initialization.
12213 *
12214 **/
12215void
12216lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12217 struct fcf_record *fcf_record,
12218 uint16_t fcf_index)
12219{
12220 memset(fcf_record, 0, sizeof(struct fcf_record));
12221 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12222 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12223 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12224 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12225 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12226 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12227 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12228 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12229 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12230 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12231 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12232 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12233 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012234 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012235 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12236 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12237 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12238 /* Set the VLAN bit map */
12239 if (phba->valid_vlan) {
12240 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12241 = 1 << (phba->vlan_id % 8);
12242 }
12243}
12244
12245/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012246 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012247 * @phba: pointer to lpfc hba data structure.
12248 * @fcf_index: FCF table entry offset.
12249 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012250 * This routine is invoked to scan the entire FCF table by reading FCF
12251 * record and processing it one at a time starting from the @fcf_index
12252 * for initial FCF discovery or fast FCF failover rediscovery.
12253 *
12254 * Return 0 if the mailbox command is submitted sucessfully, none 0
12255 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012256 **/
12257int
James Smart0c9ab6f2010-02-26 14:15:57 -050012258lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012259{
12260 int rc = 0, error;
12261 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012262
James Smart32b97932009-07-19 10:01:21 -040012263 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012264 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12265 if (!mboxq) {
12266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12267 "2000 Failed to allocate mbox for "
12268 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012269 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012270 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012271 }
James Smartecfd03c2010-02-12 14:41:27 -050012272 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012273 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012274 if (rc) {
12275 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012276 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012277 }
James Smartecfd03c2010-02-12 14:41:27 -050012278 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012279 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012280 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smart6fb120a2009-05-22 14:52:59 -040012281 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012282 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012283 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012284 else {
James Smart32b97932009-07-19 10:01:21 -040012285 spin_lock_irq(&phba->hbalock);
12286 phba->hba_flag |= FCF_DISC_INPROGRESS;
12287 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012288 /* Reset FCF round robin index bmask for new scan */
James Smart999d8132010-03-15 11:24:56 -040012289 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012290 memset(phba->fcf.fcf_rr_bmask, 0,
12291 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart999d8132010-03-15 11:24:56 -040012292 phba->fcf.eligible_fcf_cnt = 0;
12293 }
James Smart6fb120a2009-05-22 14:52:59 -040012294 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012295 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012296fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012297 if (error) {
12298 if (mboxq)
12299 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12300 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12301 spin_lock_irq(&phba->hbalock);
12302 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12303 spin_unlock_irq(&phba->hbalock);
12304 }
James Smart6fb120a2009-05-22 14:52:59 -040012305 return error;
12306}
James Smarta0c87cb2009-07-19 10:01:10 -040012307
12308/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012309 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12310 * @phba: pointer to lpfc hba data structure.
12311 * @fcf_index: FCF table entry offset.
12312 *
12313 * This routine is invoked to read an FCF record indicated by @fcf_index
12314 * and to use it for FLOGI round robin FCF failover.
12315 *
12316 * Return 0 if the mailbox command is submitted sucessfully, none 0
12317 * otherwise.
12318 **/
12319int
12320lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12321{
12322 int rc = 0, error;
12323 LPFC_MBOXQ_t *mboxq;
12324
12325 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12326 if (!mboxq) {
12327 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12328 "2763 Failed to allocate mbox for "
12329 "READ_FCF cmd\n");
12330 error = -ENOMEM;
12331 goto fail_fcf_read;
12332 }
12333 /* Construct the read FCF record mailbox command */
12334 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12335 if (rc) {
12336 error = -EINVAL;
12337 goto fail_fcf_read;
12338 }
12339 /* Issue the mailbox command asynchronously */
12340 mboxq->vport = phba->pport;
12341 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12342 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12343 if (rc == MBX_NOT_FINISHED)
12344 error = -EIO;
12345 else
12346 error = 0;
12347
12348fail_fcf_read:
12349 if (error && mboxq)
12350 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12351 return error;
12352}
12353
12354/**
12355 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12356 * @phba: pointer to lpfc hba data structure.
12357 * @fcf_index: FCF table entry offset.
12358 *
12359 * This routine is invoked to read an FCF record indicated by @fcf_index to
12360 * determine whether it's eligible for FLOGI round robin failover list.
12361 *
12362 * Return 0 if the mailbox command is submitted sucessfully, none 0
12363 * otherwise.
12364 **/
12365int
12366lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12367{
12368 int rc = 0, error;
12369 LPFC_MBOXQ_t *mboxq;
12370
12371 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12372 if (!mboxq) {
12373 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12374 "2758 Failed to allocate mbox for "
12375 "READ_FCF cmd\n");
12376 error = -ENOMEM;
12377 goto fail_fcf_read;
12378 }
12379 /* Construct the read FCF record mailbox command */
12380 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12381 if (rc) {
12382 error = -EINVAL;
12383 goto fail_fcf_read;
12384 }
12385 /* Issue the mailbox command asynchronously */
12386 mboxq->vport = phba->pport;
12387 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12388 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12389 if (rc == MBX_NOT_FINISHED)
12390 error = -EIO;
12391 else
12392 error = 0;
12393
12394fail_fcf_read:
12395 if (error && mboxq)
12396 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12397 return error;
12398}
12399
12400/**
12401 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12402 * @phba: pointer to lpfc hba data structure.
12403 *
12404 * This routine is to get the next eligible FCF record index in a round
12405 * robin fashion. If the next eligible FCF record index equals to the
12406 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12407 * shall be returned, otherwise, the next eligible FCF record's index
12408 * shall be returned.
12409 **/
12410uint16_t
12411lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12412{
12413 uint16_t next_fcf_index;
12414
12415 /* Search from the currently registered FCF index */
12416 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12417 LPFC_SLI4_FCF_TBL_INDX_MAX,
12418 phba->fcf.current_rec.fcf_indx);
12419 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12420 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12421 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12422 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12423 /* Round robin failover stop condition */
James Smartdbb6b3a2010-06-08 18:31:37 -040012424 if ((next_fcf_index == phba->fcf.fcf_rr_init_indx) ||
12425 (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX))
James Smart0c9ab6f2010-02-26 14:15:57 -050012426 return LPFC_FCOE_FCF_NEXT_NONE;
12427
12428 return next_fcf_index;
12429}
12430
12431/**
12432 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12433 * @phba: pointer to lpfc hba data structure.
12434 *
12435 * This routine sets the FCF record index in to the eligible bmask for
12436 * round robin failover search. It checks to make sure that the index
12437 * does not go beyond the range of the driver allocated bmask dimension
12438 * before setting the bit.
12439 *
12440 * Returns 0 if the index bit successfully set, otherwise, it returns
12441 * -EINVAL.
12442 **/
12443int
12444lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12445{
12446 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12447 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12448 "2610 HBA FCF index reached driver's "
12449 "book keeping dimension: fcf_index:%d, "
12450 "driver_bmask_max:%d\n",
12451 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12452 return -EINVAL;
12453 }
12454 /* Set the eligible FCF record index bmask */
12455 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12456
12457 return 0;
12458}
12459
12460/**
12461 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12462 * @phba: pointer to lpfc hba data structure.
12463 *
12464 * This routine clears the FCF record index from the eligible bmask for
12465 * round robin failover search. It checks to make sure that the index
12466 * does not go beyond the range of the driver allocated bmask dimension
12467 * before clearing the bit.
12468 **/
12469void
12470lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12471{
12472 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12473 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12474 "2762 HBA FCF index goes beyond driver's "
12475 "book keeping dimension: fcf_index:%d, "
12476 "driver_bmask_max:%d\n",
12477 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12478 return;
12479 }
12480 /* Clear the eligible FCF record index bmask */
12481 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12482}
12483
12484/**
James Smartecfd03c2010-02-12 14:41:27 -050012485 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12486 * @phba: pointer to lpfc hba data structure.
12487 *
12488 * This routine is the completion routine for the rediscover FCF table mailbox
12489 * command. If the mailbox command returned failure, it will try to stop the
12490 * FCF rediscover wait timer.
12491 **/
12492void
12493lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12494{
12495 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12496 uint32_t shdr_status, shdr_add_status;
12497
12498 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12499
12500 shdr_status = bf_get(lpfc_mbox_hdr_status,
12501 &redisc_fcf->header.cfg_shdr.response);
12502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12503 &redisc_fcf->header.cfg_shdr.response);
12504 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012505 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012506 "2746 Requesting for FCF rediscovery failed "
12507 "status x%x add_status x%x\n",
12508 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012509 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012510 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012511 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012512 spin_unlock_irq(&phba->hbalock);
12513 /*
12514 * CVL event triggered FCF rediscover request failed,
12515 * last resort to re-try current registered FCF entry.
12516 */
12517 lpfc_retry_pport_discovery(phba);
12518 } else {
12519 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012520 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012521 spin_unlock_irq(&phba->hbalock);
12522 /*
12523 * DEAD FCF event triggered FCF rediscover request
12524 * failed, last resort to fail over as a link down
12525 * to FCF registration.
12526 */
12527 lpfc_sli4_fcf_dead_failthrough(phba);
12528 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012529 } else {
12530 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12531 "2775 Start FCF rediscovery quiescent period "
12532 "wait timer before scaning FCF table\n");
James Smartecfd03c2010-02-12 14:41:27 -050012533 /*
12534 * Start FCF rediscovery wait timer for pending FCF
12535 * before rescan FCF record table.
12536 */
12537 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012538 }
James Smartecfd03c2010-02-12 14:41:27 -050012539
12540 mempool_free(mbox, phba->mbox_mem_pool);
12541}
12542
12543/**
12544 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12545 * @phba: pointer to lpfc hba data structure.
12546 *
12547 * This routine is invoked to request for rediscovery of the entire FCF table
12548 * by the port.
12549 **/
12550int
12551lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12552{
12553 LPFC_MBOXQ_t *mbox;
12554 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12555 int rc, length;
12556
James Smart0c9ab6f2010-02-26 14:15:57 -050012557 /* Cancel retry delay timers to all vports before FCF rediscover */
12558 lpfc_cancel_all_vport_retry_delay_timer(phba);
12559
James Smartecfd03c2010-02-12 14:41:27 -050012560 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12561 if (!mbox) {
12562 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12563 "2745 Failed to allocate mbox for "
12564 "requesting FCF rediscover.\n");
12565 return -ENOMEM;
12566 }
12567
12568 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12569 sizeof(struct lpfc_sli4_cfg_mhdr));
12570 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12571 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12572 length, LPFC_SLI4_MBX_EMBED);
12573
12574 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12575 /* Set count to 0 for invalidating the entire FCF database */
12576 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12577
12578 /* Issue the mailbox command asynchronously */
12579 mbox->vport = phba->pport;
12580 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12581 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12582
12583 if (rc == MBX_NOT_FINISHED) {
12584 mempool_free(mbox, phba->mbox_mem_pool);
12585 return -EIO;
12586 }
12587 return 0;
12588}
12589
12590/**
James Smartfc2b9892010-02-26 14:15:29 -050012591 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12592 * @phba: pointer to lpfc hba data structure.
12593 *
12594 * This function is the failover routine as a last resort to the FCF DEAD
12595 * event when driver failed to perform fast FCF failover.
12596 **/
12597void
12598lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12599{
12600 uint32_t link_state;
12601
12602 /*
12603 * Last resort as FCF DEAD event failover will treat this as
12604 * a link down, but save the link state because we don't want
12605 * it to be changed to Link Down unless it is already down.
12606 */
12607 link_state = phba->link_state;
12608 lpfc_linkdown(phba);
12609 phba->link_state = link_state;
12610
12611 /* Unregister FCF if no devices connected to it */
12612 lpfc_unregister_unused_fcf(phba);
12613}
12614
12615/**
James Smarta0c87cb2009-07-19 10:01:10 -040012616 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12617 * @phba: pointer to lpfc hba data structure.
12618 *
12619 * This function read region 23 and parse TLV for port status to
12620 * decide if the user disaled the port. If the TLV indicates the
12621 * port is disabled, the hba_flag is set accordingly.
12622 **/
12623void
12624lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12625{
12626 LPFC_MBOXQ_t *pmb = NULL;
12627 MAILBOX_t *mb;
12628 uint8_t *rgn23_data = NULL;
12629 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12630 int rc;
12631
12632 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12633 if (!pmb) {
12634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12635 "2600 lpfc_sli_read_serdes_param failed to"
12636 " allocate mailbox memory\n");
12637 goto out;
12638 }
12639 mb = &pmb->u.mb;
12640
12641 /* Get adapter Region 23 data */
12642 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12643 if (!rgn23_data)
12644 goto out;
12645
12646 do {
12647 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12648 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12649
12650 if (rc != MBX_SUCCESS) {
12651 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12652 "2601 lpfc_sli_read_link_ste failed to"
12653 " read config region 23 rc 0x%x Status 0x%x\n",
12654 rc, mb->mbxStatus);
12655 mb->un.varDmp.word_cnt = 0;
12656 }
12657 /*
12658 * dump mem may return a zero when finished or we got a
12659 * mailbox error, either way we are done.
12660 */
12661 if (mb->un.varDmp.word_cnt == 0)
12662 break;
12663 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12664 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12665
12666 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12667 rgn23_data + offset,
12668 mb->un.varDmp.word_cnt);
12669 offset += mb->un.varDmp.word_cnt;
12670 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12671
12672 data_size = offset;
12673 offset = 0;
12674
12675 if (!data_size)
12676 goto out;
12677
12678 /* Check the region signature first */
12679 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12681 "2619 Config region 23 has bad signature\n");
12682 goto out;
12683 }
12684 offset += 4;
12685
12686 /* Check the data structure version */
12687 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12689 "2620 Config region 23 has bad version\n");
12690 goto out;
12691 }
12692 offset += 4;
12693
12694 /* Parse TLV entries in the region */
12695 while (offset < data_size) {
12696 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12697 break;
12698 /*
12699 * If the TLV is not driver specific TLV or driver id is
12700 * not linux driver id, skip the record.
12701 */
12702 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12703 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12704 (rgn23_data[offset + 3] != 0)) {
12705 offset += rgn23_data[offset + 1] * 4 + 4;
12706 continue;
12707 }
12708
12709 /* Driver found a driver specific TLV in the config region */
12710 sub_tlv_len = rgn23_data[offset + 1] * 4;
12711 offset += 4;
12712 tlv_offset = 0;
12713
12714 /*
12715 * Search for configured port state sub-TLV.
12716 */
12717 while ((offset < data_size) &&
12718 (tlv_offset < sub_tlv_len)) {
12719 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12720 offset += 4;
12721 tlv_offset += 4;
12722 break;
12723 }
12724 if (rgn23_data[offset] != PORT_STE_TYPE) {
12725 offset += rgn23_data[offset + 1] * 4 + 4;
12726 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12727 continue;
12728 }
12729
12730 /* This HBA contains PORT_STE configured */
12731 if (!rgn23_data[offset + 2])
12732 phba->hba_flag |= LINK_DISABLED;
12733
12734 goto out;
12735 }
12736 }
12737out:
12738 if (pmb)
12739 mempool_free(pmb, phba->mbox_mem_pool);
12740 kfree(rgn23_data);
12741 return;
12742}
James Smart695a8142010-01-26 23:08:03 -050012743
12744/**
12745 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12746 * @vport: pointer to vport data structure.
12747 *
12748 * This function iterate through the mailboxq and clean up all REG_LOGIN
12749 * and REG_VPI mailbox commands associated with the vport. This function
12750 * is called when driver want to restart discovery of the vport due to
12751 * a Clear Virtual Link event.
12752 **/
12753void
12754lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12755{
12756 struct lpfc_hba *phba = vport->phba;
12757 LPFC_MBOXQ_t *mb, *nextmb;
12758 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040012759 struct lpfc_nodelist *ndlp;
James Smart695a8142010-01-26 23:08:03 -050012760
12761 spin_lock_irq(&phba->hbalock);
12762 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12763 if (mb->vport != vport)
12764 continue;
12765
12766 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12767 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12768 continue;
12769
12770 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smartd7c47992010-06-08 18:31:54 -040012771 if (phba->sli_rev == LPFC_SLI_REV4)
12772 __lpfc_sli4_free_rpi(phba,
12773 mb->u.mb.un.varRegLogin.rpi);
James Smart695a8142010-01-26 23:08:03 -050012774 mp = (struct lpfc_dmabuf *) (mb->context1);
12775 if (mp) {
12776 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12777 kfree(mp);
12778 }
James Smart78730cf2010-04-06 15:06:30 -040012779 ndlp = (struct lpfc_nodelist *) mb->context2;
12780 if (ndlp) {
12781 lpfc_nlp_put(ndlp);
12782 mb->context2 = NULL;
12783 }
James Smart695a8142010-01-26 23:08:03 -050012784 }
12785 list_del(&mb->list);
12786 mempool_free(mb, phba->mbox_mem_pool);
12787 }
12788 mb = phba->sli.mbox_active;
12789 if (mb && (mb->vport == vport)) {
12790 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12791 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12792 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart78730cf2010-04-06 15:06:30 -040012793 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12794 ndlp = (struct lpfc_nodelist *) mb->context2;
12795 if (ndlp) {
12796 lpfc_nlp_put(ndlp);
12797 mb->context2 = NULL;
12798 }
12799 /* Unregister the RPI when mailbox complete */
12800 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12801 }
James Smart695a8142010-01-26 23:08:03 -050012802 }
12803 spin_unlock_irq(&phba->hbalock);
12804}
12805
James Smart2a9bf3d2010-06-07 15:24:45 -040012806/**
12807 * lpfc_drain_txq - Drain the txq
12808 * @phba: Pointer to HBA context object.
12809 *
12810 * This function attempt to submit IOCBs on the txq
12811 * to the adapter. For SLI4 adapters, the txq contains
12812 * ELS IOCBs that have been deferred because the there
12813 * are no SGLs. This congestion can occur with large
12814 * vport counts during node discovery.
12815 **/
12816
12817uint32_t
12818lpfc_drain_txq(struct lpfc_hba *phba)
12819{
12820 LIST_HEAD(completions);
12821 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12822 struct lpfc_iocbq *piocbq = 0;
12823 unsigned long iflags = 0;
12824 char *fail_msg = NULL;
12825 struct lpfc_sglq *sglq;
12826 union lpfc_wqe wqe;
12827
12828 spin_lock_irqsave(&phba->hbalock, iflags);
12829 if (pring->txq_cnt > pring->txq_max)
12830 pring->txq_max = pring->txq_cnt;
12831
12832 spin_unlock_irqrestore(&phba->hbalock, iflags);
12833
12834 while (pring->txq_cnt) {
12835 spin_lock_irqsave(&phba->hbalock, iflags);
12836
12837 sglq = __lpfc_sli_get_sglq(phba);
12838 if (!sglq) {
12839 spin_unlock_irqrestore(&phba->hbalock, iflags);
12840 break;
12841 } else {
12842 piocbq = lpfc_sli_ringtx_get(phba, pring);
12843 if (!piocbq) {
12844 /* The txq_cnt out of sync. This should
12845 * never happen
12846 */
12847 sglq = __lpfc_clear_active_sglq(phba,
12848 sglq->sli4_xritag);
12849 spin_unlock_irqrestore(&phba->hbalock, iflags);
12850 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12851 "2823 txq empty and txq_cnt is %d\n ",
12852 pring->txq_cnt);
12853 break;
12854 }
12855 }
12856
12857 /* The xri and iocb resources secured,
12858 * attempt to issue request
12859 */
12860 piocbq->sli4_xritag = sglq->sli4_xritag;
12861 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12862 fail_msg = "to convert bpl to sgl";
12863 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12864 fail_msg = "to convert iocb to wqe";
12865 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12866 fail_msg = " - Wq is full";
12867 else
12868 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12869
12870 if (fail_msg) {
12871 /* Failed means we can't issue and need to cancel */
12872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12873 "2822 IOCB failed %s iotag 0x%x "
12874 "xri 0x%x\n",
12875 fail_msg,
12876 piocbq->iotag, piocbq->sli4_xritag);
12877 list_add_tail(&piocbq->list, &completions);
12878 }
12879 spin_unlock_irqrestore(&phba->hbalock, iflags);
12880 }
12881
12882 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12883 phba->pport->work_port_events &= ~WORKER_SERVICE_TXQ;
12884 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12885
12886 /* Cancel all the IOCBs that cannot be issued */
12887 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12888 IOERR_SLI_ABORTED);
12889
12890 return pring->txq_cnt;
12891}