blob: bedaa4e7cbf8737c98f65f0d3e7dba47db649c77 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040068static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
James Smartf0d9bcc2010-10-22 11:07:09 -040098 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart4f774512009-05-22 14:52:35 -040099
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 */
James Smart589a52d2010-07-14 15:30:54 -0400604 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400605 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500606 }
James Smart4f774512009-05-22 14:52:35 -0400607 }
608
609
610 /*
611 * Clean all volatile data fields, preserve iotag and node struct.
612 */
613 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
614 iocbq->sli4_xritag = NO_XRI;
615 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
616}
617
James Smart2a9bf3d2010-06-07 15:24:45 -0400618
James Smart4f774512009-05-22 14:52:35 -0400619/**
James Smart3772a992009-05-22 14:50:54 -0400620 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
621 * @phba: Pointer to HBA context object.
622 * @iocbq: Pointer to driver iocb object.
623 *
624 * This function is called with hbalock held to release driver
625 * iocb object to the iocb pool. The iotag in the iocb object
626 * does not change for each use of the iocb object. This function
627 * clears all other fields of the iocb object when it is freed.
628 **/
629static void
630__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
631{
632 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
633
634 /*
635 * Clean all volatile data fields, preserve iotag and node struct.
636 */
637 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
638 iocbq->sli4_xritag = NO_XRI;
639 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
640}
641
642/**
James Smart3621a712009-04-06 18:47:14 -0400643 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400644 * @phba: Pointer to HBA context object.
645 * @iocbq: Pointer to driver iocb object.
646 *
647 * This function is called with hbalock held to release driver
648 * iocb object to the iocb pool. The iotag in the iocb object
649 * does not change for each use of the iocb object. This function
650 * clears all other fields of the iocb object when it is freed.
651 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100652static void
James Smart2e0fef82007-06-17 19:56:36 -0500653__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
654{
James Smart3772a992009-05-22 14:50:54 -0400655 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400656 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -0500657}
658
James Smarte59058c2008-08-24 21:49:00 -0400659/**
James Smart3621a712009-04-06 18:47:14 -0400660 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400661 * @phba: Pointer to HBA context object.
662 * @iocbq: Pointer to driver iocb object.
663 *
664 * This function is called with no lock held to release the iocb to
665 * iocb pool.
666 **/
James Smart2e0fef82007-06-17 19:56:36 -0500667void
668lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
669{
670 unsigned long iflags;
671
672 /*
673 * Clean all volatile data fields, preserve iotag and node struct.
674 */
675 spin_lock_irqsave(&phba->hbalock, iflags);
676 __lpfc_sli_release_iocbq(phba, iocbq);
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678}
679
James Smarte59058c2008-08-24 21:49:00 -0400680/**
James Smarta257bf92009-04-06 18:48:10 -0400681 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
682 * @phba: Pointer to HBA context object.
683 * @iocblist: List of IOCBs.
684 * @ulpstatus: ULP status in IOCB command field.
685 * @ulpWord4: ULP word-4 in IOCB command field.
686 *
687 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
688 * on the list by invoking the complete callback function associated with the
689 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
690 * fields.
691 **/
692void
693lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
694 uint32_t ulpstatus, uint32_t ulpWord4)
695{
696 struct lpfc_iocbq *piocb;
697
698 while (!list_empty(iocblist)) {
699 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
700
701 if (!piocb->iocb_cmpl)
702 lpfc_sli_release_iocbq(phba, piocb);
703 else {
704 piocb->iocb.ulpStatus = ulpstatus;
705 piocb->iocb.un.ulpWord[4] = ulpWord4;
706 (piocb->iocb_cmpl) (phba, piocb, piocb);
707 }
708 }
709 return;
710}
711
712/**
James Smart3621a712009-04-06 18:47:14 -0400713 * lpfc_sli_iocb_cmd_type - Get the iocb type
714 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400715 *
716 * This function is called by ring event handler function to get the iocb type.
717 * This function translates the iocb command to an iocb command type used to
718 * decide the final disposition of each completed IOCB.
719 * The function returns
720 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
721 * LPFC_SOL_IOCB if it is a solicited iocb completion
722 * LPFC_ABORT_IOCB if it is an abort iocb
723 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
724 *
725 * The caller is not required to hold any lock.
726 **/
dea31012005-04-17 16:05:31 -0500727static lpfc_iocb_type
728lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
729{
730 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
731
732 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
733 return 0;
734
735 switch (iocb_cmnd) {
736 case CMD_XMIT_SEQUENCE_CR:
737 case CMD_XMIT_SEQUENCE_CX:
738 case CMD_XMIT_BCAST_CN:
739 case CMD_XMIT_BCAST_CX:
740 case CMD_ELS_REQUEST_CR:
741 case CMD_ELS_REQUEST_CX:
742 case CMD_CREATE_XRI_CR:
743 case CMD_CREATE_XRI_CX:
744 case CMD_GET_RPI_CN:
745 case CMD_XMIT_ELS_RSP_CX:
746 case CMD_GET_RPI_CR:
747 case CMD_FCP_IWRITE_CR:
748 case CMD_FCP_IWRITE_CX:
749 case CMD_FCP_IREAD_CR:
750 case CMD_FCP_IREAD_CX:
751 case CMD_FCP_ICMND_CR:
752 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500753 case CMD_FCP_TSEND_CX:
754 case CMD_FCP_TRSP_CX:
755 case CMD_FCP_TRECEIVE_CX:
756 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500757 case CMD_ADAPTER_MSG:
758 case CMD_ADAPTER_DUMP:
759 case CMD_XMIT_SEQUENCE64_CR:
760 case CMD_XMIT_SEQUENCE64_CX:
761 case CMD_XMIT_BCAST64_CN:
762 case CMD_XMIT_BCAST64_CX:
763 case CMD_ELS_REQUEST64_CR:
764 case CMD_ELS_REQUEST64_CX:
765 case CMD_FCP_IWRITE64_CR:
766 case CMD_FCP_IWRITE64_CX:
767 case CMD_FCP_IREAD64_CR:
768 case CMD_FCP_IREAD64_CX:
769 case CMD_FCP_ICMND64_CR:
770 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500771 case CMD_FCP_TSEND64_CX:
772 case CMD_FCP_TRSP64_CX:
773 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500774 case CMD_GEN_REQUEST64_CR:
775 case CMD_GEN_REQUEST64_CX:
776 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400777 case DSSCMD_IWRITE64_CR:
778 case DSSCMD_IWRITE64_CX:
779 case DSSCMD_IREAD64_CR:
780 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500781 type = LPFC_SOL_IOCB;
782 break;
783 case CMD_ABORT_XRI_CN:
784 case CMD_ABORT_XRI_CX:
785 case CMD_CLOSE_XRI_CN:
786 case CMD_CLOSE_XRI_CX:
787 case CMD_XRI_ABORTED_CX:
788 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400789 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500790 type = LPFC_ABORT_IOCB;
791 break;
792 case CMD_RCV_SEQUENCE_CX:
793 case CMD_RCV_ELS_REQ_CX:
794 case CMD_RCV_SEQUENCE64_CX:
795 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400796 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500797 case CMD_IOCB_RCV_SEQ64_CX:
798 case CMD_IOCB_RCV_ELS64_CX:
799 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500800 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500801 type = LPFC_UNSOL_IOCB;
802 break;
James Smart3163f722008-02-08 18:50:25 -0500803 case CMD_IOCB_XMIT_MSEQ64_CR:
804 case CMD_IOCB_XMIT_MSEQ64_CX:
805 case CMD_IOCB_RCV_SEQ_LIST64_CX:
806 case CMD_IOCB_RCV_ELS_LIST64_CX:
807 case CMD_IOCB_CLOSE_EXTENDED_CN:
808 case CMD_IOCB_ABORT_EXTENDED_CN:
809 case CMD_IOCB_RET_HBQE64_CN:
810 case CMD_IOCB_FCP_IBIDIR64_CR:
811 case CMD_IOCB_FCP_IBIDIR64_CX:
812 case CMD_IOCB_FCP_ITASKMGT64_CX:
813 case CMD_IOCB_LOGENTRY_CN:
814 case CMD_IOCB_LOGENTRY_ASYNC_CN:
815 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700816 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500817 type = LPFC_UNKNOWN_IOCB;
818 break;
dea31012005-04-17 16:05:31 -0500819 default:
820 type = LPFC_UNKNOWN_IOCB;
821 break;
822 }
823
824 return type;
825}
826
James Smarte59058c2008-08-24 21:49:00 -0400827/**
James Smart3621a712009-04-06 18:47:14 -0400828 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400829 * @phba: Pointer to HBA context object.
830 *
831 * This function is called from SLI initialization code
832 * to configure every ring of the HBA's SLI interface. The
833 * caller is not required to hold any lock. This function issues
834 * a config_ring mailbox command for each ring.
835 * This function returns zero if successful else returns a negative
836 * error code.
837 **/
dea31012005-04-17 16:05:31 -0500838static int
James Smarted957682007-06-17 19:56:37 -0500839lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500840{
841 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500842 LPFC_MBOXQ_t *pmb;
843 MAILBOX_t *pmbox;
844 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500845
James Smarted957682007-06-17 19:56:37 -0500846 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
847 if (!pmb)
848 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400849 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500850 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500851 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500852 lpfc_config_ring(phba, i, pmb);
853 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
854 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400856 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500857 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
858 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400859 rc, pmbox->mbxCommand,
860 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500861 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500862 ret = -ENXIO;
863 break;
dea31012005-04-17 16:05:31 -0500864 }
865 }
James Smarted957682007-06-17 19:56:37 -0500866 mempool_free(pmb, phba->mbox_mem_pool);
867 return ret;
dea31012005-04-17 16:05:31 -0500868}
869
James Smarte59058c2008-08-24 21:49:00 -0400870/**
James Smart3621a712009-04-06 18:47:14 -0400871 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400872 * @phba: Pointer to HBA context object.
873 * @pring: Pointer to driver SLI ring object.
874 * @piocb: Pointer to the driver iocb object.
875 *
876 * This function is called with hbalock held. The function adds the
877 * new iocb to txcmplq of the given ring. This function always returns
878 * 0. If this function is called for ELS ring, this function checks if
879 * there is a vport associated with the ELS command. This function also
880 * starts els_tmofunc timer if this is an ELS command.
881 **/
dea31012005-04-17 16:05:31 -0500882static int
James Smart2e0fef82007-06-17 19:56:36 -0500883lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
884 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500885{
dea31012005-04-17 16:05:31 -0500886 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400887 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -0500888 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -0400889 if (pring->txcmplq_cnt > pring->txcmplq_max)
890 pring->txcmplq_max = pring->txcmplq_cnt;
891
James Smart92d7f7b2007-06-17 19:56:38 -0500892 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
893 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
894 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
895 if (!piocb->vport)
896 BUG();
897 else
898 mod_timer(&piocb->vport->els_tmofunc,
899 jiffies + HZ * (phba->fc_ratov << 1));
900 }
901
dea31012005-04-17 16:05:31 -0500902
James Smart2e0fef82007-06-17 19:56:36 -0500903 return 0;
dea31012005-04-17 16:05:31 -0500904}
905
James Smarte59058c2008-08-24 21:49:00 -0400906/**
James Smart3621a712009-04-06 18:47:14 -0400907 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400908 * @phba: Pointer to HBA context object.
909 * @pring: Pointer to driver SLI ring object.
910 *
911 * This function is called with hbalock held to get next
912 * iocb in txq of the given ring. If there is any iocb in
913 * the txq, the function returns first iocb in the list after
914 * removing the iocb from the list, else it returns NULL.
915 **/
James Smart2a9bf3d2010-06-07 15:24:45 -0400916struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500917lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500918{
dea31012005-04-17 16:05:31 -0500919 struct lpfc_iocbq *cmd_iocb;
920
James Smart858c9f62007-06-17 19:56:39 -0500921 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
922 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500923 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500924 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500925}
926
James Smarte59058c2008-08-24 21:49:00 -0400927/**
James Smart3621a712009-04-06 18:47:14 -0400928 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400929 * @phba: Pointer to HBA context object.
930 * @pring: Pointer to driver SLI ring object.
931 *
932 * This function is called with hbalock held and the caller must post the
933 * iocb without releasing the lock. If the caller releases the lock,
934 * iocb slot returned by the function is not guaranteed to be available.
935 * The function returns pointer to the next available iocb slot if there
936 * is available slot in the ring, else it returns NULL.
937 * If the get index of the ring is ahead of the put index, the function
938 * will post an error attention event to the worker thread to take the
939 * HBA to offline state.
940 **/
dea31012005-04-17 16:05:31 -0500941static IOCB_t *
942lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
943{
James Smart34b02dc2008-08-24 21:49:55 -0400944 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500945 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500946 if ((pring->next_cmdidx == pring->cmdidx) &&
947 (++pring->next_cmdidx >= max_cmd_idx))
948 pring->next_cmdidx = 0;
949
950 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
951
952 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
953
954 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400956 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200957 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400958 pring->ringno,
dea31012005-04-17 16:05:31 -0500959 pring->local_getidx, max_cmd_idx);
960
James Smart2e0fef82007-06-17 19:56:36 -0500961 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500962 /*
963 * All error attention handlers are posted to
964 * worker thread
965 */
966 phba->work_ha |= HA_ERATT;
967 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500968
James Smart5e9d9b82008-06-14 22:52:53 -0400969 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500970
971 return NULL;
972 }
973
974 if (pring->local_getidx == pring->next_cmdidx)
975 return NULL;
976 }
977
James Smarted957682007-06-17 19:56:37 -0500978 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500979}
980
James Smarte59058c2008-08-24 21:49:00 -0400981/**
James Smart3621a712009-04-06 18:47:14 -0400982 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400983 * @phba: Pointer to HBA context object.
984 * @iocbq: Pointer to driver iocb object.
985 *
986 * This function gets an iotag for the iocb. If there is no unused iotag and
987 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
988 * array and assigns a new iotag.
989 * The function returns the allocated iotag if successful, else returns zero.
990 * Zero is not a valid iotag.
991 * The caller is not required to hold any lock.
992 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500993uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500994lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500995{
James Smart2e0fef82007-06-17 19:56:36 -0500996 struct lpfc_iocbq **new_arr;
997 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500998 size_t new_len;
999 struct lpfc_sli *psli = &phba->sli;
1000 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001001
James Smart2e0fef82007-06-17 19:56:36 -05001002 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001003 iotag = psli->last_iotag;
1004 if(++iotag < psli->iocbq_lookup_len) {
1005 psli->last_iotag = iotag;
1006 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001007 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001008 iocbq->iotag = iotag;
1009 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001010 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001011 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1012 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001013 spin_unlock_irq(&phba->hbalock);
1014 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001015 GFP_KERNEL);
1016 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001017 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001018 old_arr = psli->iocbq_lookup;
1019 if (new_len <= psli->iocbq_lookup_len) {
1020 /* highly unprobable case */
1021 kfree(new_arr);
1022 iotag = psli->last_iotag;
1023 if(++iotag < psli->iocbq_lookup_len) {
1024 psli->last_iotag = iotag;
1025 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001026 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001027 iocbq->iotag = iotag;
1028 return iotag;
1029 }
James Smart2e0fef82007-06-17 19:56:36 -05001030 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001031 return 0;
1032 }
1033 if (psli->iocbq_lookup)
1034 memcpy(new_arr, old_arr,
1035 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001036 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001037 psli->iocbq_lookup = new_arr;
1038 psli->iocbq_lookup_len = new_len;
1039 psli->last_iotag = iotag;
1040 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001041 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001042 iocbq->iotag = iotag;
1043 kfree(old_arr);
1044 return iotag;
1045 }
James Smart8f6d98d2006-08-01 07:34:00 -04001046 } else
James Smart2e0fef82007-06-17 19:56:36 -05001047 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001048
James Smartbc739052010-08-04 16:11:18 -04001049 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001050 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1051 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001052
James Bottomley604a3e32005-10-29 10:28:33 -05001053 return 0;
dea31012005-04-17 16:05:31 -05001054}
1055
James Smarte59058c2008-08-24 21:49:00 -04001056/**
James Smart3621a712009-04-06 18:47:14 -04001057 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001058 * @phba: Pointer to HBA context object.
1059 * @pring: Pointer to driver SLI ring object.
1060 * @iocb: Pointer to iocb slot in the ring.
1061 * @nextiocb: Pointer to driver iocb object which need to be
1062 * posted to firmware.
1063 *
1064 * This function is called with hbalock held to post a new iocb to
1065 * the firmware. This function copies the new iocb to ring iocb slot and
1066 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1067 * a completion call back for this iocb else the function will free the
1068 * iocb object.
1069 **/
dea31012005-04-17 16:05:31 -05001070static void
1071lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1072 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1073{
1074 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001075 * Set up an iotag
dea31012005-04-17 16:05:31 -05001076 */
James Bottomley604a3e32005-10-29 10:28:33 -05001077 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001078
James Smarte2a0a9d2008-12-04 22:40:02 -05001079
James Smarta58cbd52007-08-02 11:09:43 -04001080 if (pring->ringno == LPFC_ELS_RING) {
1081 lpfc_debugfs_slow_ring_trc(phba,
1082 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1083 *(((uint32_t *) &nextiocb->iocb) + 4),
1084 *(((uint32_t *) &nextiocb->iocb) + 6),
1085 *(((uint32_t *) &nextiocb->iocb) + 7));
1086 }
1087
dea31012005-04-17 16:05:31 -05001088 /*
1089 * Issue iocb command to adapter
1090 */
James Smart92d7f7b2007-06-17 19:56:38 -05001091 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001092 wmb();
1093 pring->stats.iocb_cmd++;
1094
1095 /*
1096 * If there is no completion routine to call, we can release the
1097 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1098 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1099 */
1100 if (nextiocb->iocb_cmpl)
1101 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001102 else
James Smart2e0fef82007-06-17 19:56:36 -05001103 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001104
1105 /*
1106 * Let the HBA know what IOCB slot will be the next one the
1107 * driver will put a command into.
1108 */
1109 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001110 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001111}
1112
James Smarte59058c2008-08-24 21:49:00 -04001113/**
James Smart3621a712009-04-06 18:47:14 -04001114 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001115 * @phba: Pointer to HBA context object.
1116 * @pring: Pointer to driver SLI ring object.
1117 *
1118 * The caller is not required to hold any lock for calling this function.
1119 * This function updates the chip attention bits for the ring to inform firmware
1120 * that there are pending work to be done for this ring and requests an
1121 * interrupt when there is space available in the ring. This function is
1122 * called when the driver is unable to post more iocbs to the ring due
1123 * to unavailability of space in the ring.
1124 **/
dea31012005-04-17 16:05:31 -05001125static void
James Smart2e0fef82007-06-17 19:56:36 -05001126lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001127{
1128 int ringno = pring->ringno;
1129
1130 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1131
1132 wmb();
1133
1134 /*
1135 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1136 * The HBA will tell us when an IOCB entry is available.
1137 */
1138 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1139 readl(phba->CAregaddr); /* flush */
1140
1141 pring->stats.iocb_cmd_full++;
1142}
1143
James Smarte59058c2008-08-24 21:49:00 -04001144/**
James Smart3621a712009-04-06 18:47:14 -04001145 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001146 * @phba: Pointer to HBA context object.
1147 * @pring: Pointer to driver SLI ring object.
1148 *
1149 * This function updates the chip attention register bit for the
1150 * given ring to inform HBA that there is more work to be done
1151 * in this ring. The caller is not required to hold any lock.
1152 **/
dea31012005-04-17 16:05:31 -05001153static void
James Smart2e0fef82007-06-17 19:56:36 -05001154lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001155{
1156 int ringno = pring->ringno;
1157
1158 /*
1159 * Tell the HBA that there is work to do in this ring.
1160 */
James Smart34b02dc2008-08-24 21:49:55 -04001161 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1162 wmb();
1163 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1164 readl(phba->CAregaddr); /* flush */
1165 }
dea31012005-04-17 16:05:31 -05001166}
1167
James Smarte59058c2008-08-24 21:49:00 -04001168/**
James Smart3621a712009-04-06 18:47:14 -04001169 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001170 * @phba: Pointer to HBA context object.
1171 * @pring: Pointer to driver SLI ring object.
1172 *
1173 * This function is called with hbalock held to post pending iocbs
1174 * in the txq to the firmware. This function is called when driver
1175 * detects space available in the ring.
1176 **/
dea31012005-04-17 16:05:31 -05001177static void
James Smart2e0fef82007-06-17 19:56:36 -05001178lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001179{
1180 IOCB_t *iocb;
1181 struct lpfc_iocbq *nextiocb;
1182
1183 /*
1184 * Check to see if:
1185 * (a) there is anything on the txq to send
1186 * (b) link is up
1187 * (c) link attention events can be processed (fcp ring only)
1188 * (d) IOCB processing is not blocked by the outstanding mbox command.
1189 */
1190 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001191 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001192 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001193 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001194
1195 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1196 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1197 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1198
1199 if (iocb)
1200 lpfc_sli_update_ring(phba, pring);
1201 else
1202 lpfc_sli_update_full_ring(phba, pring);
1203 }
1204
1205 return;
1206}
1207
James Smarte59058c2008-08-24 21:49:00 -04001208/**
James Smart3621a712009-04-06 18:47:14 -04001209 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001210 * @phba: Pointer to HBA context object.
1211 * @hbqno: HBQ number.
1212 *
1213 * This function is called with hbalock held to get the next
1214 * available slot for the given HBQ. If there is free slot
1215 * available for the HBQ it will return pointer to the next available
1216 * HBQ entry else it will return NULL.
1217 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001218static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001219lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1220{
1221 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1222
1223 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1224 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1225 hbqp->next_hbqPutIdx = 0;
1226
1227 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001228 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001229 uint32_t getidx = le32_to_cpu(raw_index);
1230
1231 hbqp->local_hbqGetIdx = getidx;
1232
1233 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1234 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001235 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001236 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001237 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001238 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001239 hbqp->entry_count);
1240
1241 phba->link_state = LPFC_HBA_ERROR;
1242 return NULL;
1243 }
1244
1245 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1246 return NULL;
1247 }
1248
James Smart51ef4c22007-08-02 11:10:31 -04001249 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1250 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001251}
1252
James Smarte59058c2008-08-24 21:49:00 -04001253/**
James Smart3621a712009-04-06 18:47:14 -04001254 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001255 * @phba: Pointer to HBA context object.
1256 *
1257 * This function is called with no lock held to free all the
1258 * hbq buffers while uninitializing the SLI interface. It also
1259 * frees the HBQ buffers returned by the firmware but not yet
1260 * processed by the upper layers.
1261 **/
James Smarted957682007-06-17 19:56:37 -05001262void
1263lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1264{
James Smart92d7f7b2007-06-17 19:56:38 -05001265 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1266 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001267 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001268 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001269 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001270
James Smart51ef4c22007-08-02 11:10:31 -04001271 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001272 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001273 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001274 for (i = 0; i < hbq_count; ++i) {
1275 list_for_each_entry_safe(dmabuf, next_dmabuf,
1276 &phba->hbqs[i].hbq_buffer_list, list) {
1277 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1278 list_del(&hbq_buf->dbuf.list);
1279 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1280 }
James Smarta8adb832007-10-27 13:37:53 -04001281 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001282 }
James Smart3163f722008-02-08 18:50:25 -05001283 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001284 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1285 list) {
James Smart3163f722008-02-08 18:50:25 -05001286 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1287 list_del(&hbq_buf->dbuf.list);
1288 if (hbq_buf->tag == -1) {
1289 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1290 (phba, hbq_buf);
1291 } else {
1292 hbqno = hbq_buf->tag >> 16;
1293 if (hbqno >= LPFC_MAX_HBQS)
1294 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1295 (phba, hbq_buf);
1296 else
1297 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1298 hbq_buf);
1299 }
1300 }
1301
1302 /* Mark the HBQs not in use */
1303 phba->hbq_in_use = 0;
1304 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001305}
1306
James Smarte59058c2008-08-24 21:49:00 -04001307/**
James Smart3621a712009-04-06 18:47:14 -04001308 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001309 * @phba: Pointer to HBA context object.
1310 * @hbqno: HBQ number.
1311 * @hbq_buf: Pointer to HBQ buffer.
1312 *
1313 * This function is called with the hbalock held to post a
1314 * hbq buffer to the firmware. If the function finds an empty
1315 * slot in the HBQ, it will post the buffer. The function will return
1316 * pointer to the hbq entry if it successfully post the buffer
1317 * else it will return NULL.
1318 **/
James Smart3772a992009-05-22 14:50:54 -04001319static int
James Smarted957682007-06-17 19:56:37 -05001320lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001321 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001322{
James Smart3772a992009-05-22 14:50:54 -04001323 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1324}
1325
1326/**
1327 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1328 * @phba: Pointer to HBA context object.
1329 * @hbqno: HBQ number.
1330 * @hbq_buf: Pointer to HBQ buffer.
1331 *
1332 * This function is called with the hbalock held to post a hbq buffer to the
1333 * firmware. If the function finds an empty slot in the HBQ, it will post the
1334 * buffer and place it on the hbq_buffer_list. The function will return zero if
1335 * it successfully post the buffer else it will return an error.
1336 **/
1337static int
1338lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1339 struct hbq_dmabuf *hbq_buf)
1340{
James Smarted957682007-06-17 19:56:37 -05001341 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001342 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001343
1344 /* Get next HBQ entry slot to use */
1345 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1346 if (hbqe) {
1347 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1348
James Smart92d7f7b2007-06-17 19:56:38 -05001349 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1350 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001351 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001352 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001353 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1354 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1355 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001356 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1357 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001358 /* flush */
James Smarted957682007-06-17 19:56:37 -05001359 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001360 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001361 return 0;
1362 } else
1363 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001364}
1365
James Smart4f774512009-05-22 14:52:35 -04001366/**
1367 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1368 * @phba: Pointer to HBA context object.
1369 * @hbqno: HBQ number.
1370 * @hbq_buf: Pointer to HBQ buffer.
1371 *
1372 * This function is called with the hbalock held to post an RQE to the SLI4
1373 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1374 * the hbq_buffer_list and return zero, otherwise it will return an error.
1375 **/
1376static int
1377lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1378 struct hbq_dmabuf *hbq_buf)
1379{
1380 int rc;
1381 struct lpfc_rqe hrqe;
1382 struct lpfc_rqe drqe;
1383
1384 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1385 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1386 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1387 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1388 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1389 &hrqe, &drqe);
1390 if (rc < 0)
1391 return rc;
1392 hbq_buf->tag = rc;
1393 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1394 return 0;
1395}
1396
James Smarte59058c2008-08-24 21:49:00 -04001397/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001398static struct lpfc_hbq_init lpfc_els_hbq = {
1399 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001400 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001401 .mask_count = 0,
1402 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001403 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001404 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001405 .init_count = 40,
1406 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001407};
James Smarted957682007-06-17 19:56:37 -05001408
James Smarte59058c2008-08-24 21:49:00 -04001409/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001410static struct lpfc_hbq_init lpfc_extra_hbq = {
1411 .rn = 1,
1412 .entry_count = 200,
1413 .mask_count = 0,
1414 .profile = 0,
1415 .ring_mask = (1 << LPFC_EXTRA_RING),
1416 .buffer_count = 0,
1417 .init_count = 0,
1418 .add_count = 5,
1419};
1420
James Smarte59058c2008-08-24 21:49:00 -04001421/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001422struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001423 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001424 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001425};
1426
James Smarte59058c2008-08-24 21:49:00 -04001427/**
James Smart3621a712009-04-06 18:47:14 -04001428 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001429 * @phba: Pointer to HBA context object.
1430 * @hbqno: HBQ number.
1431 * @count: Number of HBQ buffers to be posted.
1432 *
James Smartd7c255b2008-08-24 21:50:00 -04001433 * This function is called with no lock held to post more hbq buffers to the
1434 * given HBQ. The function returns the number of HBQ buffers successfully
1435 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001436 **/
James Smart311464e2007-08-02 11:10:37 -04001437static int
James Smart92d7f7b2007-06-17 19:56:38 -05001438lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1439{
James Smartd7c255b2008-08-24 21:50:00 -04001440 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001441 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001442 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001443 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001444 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001445 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001446
James Smartd7c255b2008-08-24 21:50:00 -04001447 if ((phba->hbqs[hbqno].buffer_count + count) >
1448 lpfc_hbq_defs[hbqno]->entry_count)
1449 count = lpfc_hbq_defs[hbqno]->entry_count -
1450 phba->hbqs[hbqno].buffer_count;
1451 if (!count)
1452 return 0;
1453 /* Allocate HBQ entries */
1454 for (i = 0; i < count; i++) {
1455 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1456 if (!hbq_buffer)
1457 break;
1458 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1459 }
James Smart3163f722008-02-08 18:50:25 -05001460 /* Check whether HBQ is still in use */
1461 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001462 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001463 goto err;
1464 while (!list_empty(&hbq_buf_list)) {
1465 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1466 dbuf.list);
1467 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1468 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001469 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001470 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001471 posted++;
1472 } else
James Smart51ef4c22007-08-02 11:10:31 -04001473 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001474 }
James Smart3163f722008-02-08 18:50:25 -05001475 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001476 return posted;
1477err:
1478 spin_unlock_irqrestore(&phba->hbalock, flags);
1479 while (!list_empty(&hbq_buf_list)) {
1480 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1481 dbuf.list);
1482 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1483 }
James Smart92d7f7b2007-06-17 19:56:38 -05001484 return 0;
James Smarted957682007-06-17 19:56:37 -05001485}
1486
James Smarte59058c2008-08-24 21:49:00 -04001487/**
James Smart3621a712009-04-06 18:47:14 -04001488 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001489 * @phba: Pointer to HBA context object.
1490 * @qno: HBQ number.
1491 *
1492 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001493 * is called with no lock held. The function returns the number of HBQ entries
1494 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001495 **/
James Smarted957682007-06-17 19:56:37 -05001496int
James Smart92d7f7b2007-06-17 19:56:38 -05001497lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001498{
James Smartdef9c7a2009-12-21 17:02:28 -05001499 if (phba->sli_rev == LPFC_SLI_REV4)
1500 return 0;
1501 else
1502 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001504}
1505
James Smarte59058c2008-08-24 21:49:00 -04001506/**
James Smart3621a712009-04-06 18:47:14 -04001507 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001508 * @phba: Pointer to HBA context object.
1509 * @qno: HBQ queue number.
1510 *
1511 * This function is called from SLI initialization code path with
1512 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001513 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001514 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001515static int
James Smart92d7f7b2007-06-17 19:56:38 -05001516lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001517{
James Smartdef9c7a2009-12-21 17:02:28 -05001518 if (phba->sli_rev == LPFC_SLI_REV4)
1519 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1520 lpfc_hbq_defs[qno]->entry_count);
1521 else
1522 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1523 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001524}
1525
James Smarte59058c2008-08-24 21:49:00 -04001526/**
James Smart3772a992009-05-22 14:50:54 -04001527 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1528 * @phba: Pointer to HBA context object.
1529 * @hbqno: HBQ number.
1530 *
1531 * This function removes the first hbq buffer on an hbq list and returns a
1532 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1533 **/
1534static struct hbq_dmabuf *
1535lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1536{
1537 struct lpfc_dmabuf *d_buf;
1538
1539 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1540 if (!d_buf)
1541 return NULL;
1542 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1543}
1544
1545/**
James Smart3621a712009-04-06 18:47:14 -04001546 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001547 * @phba: Pointer to HBA context object.
1548 * @tag: Tag of the hbq buffer.
1549 *
1550 * This function is called with hbalock held. This function searches
1551 * for the hbq buffer associated with the given tag in the hbq buffer
1552 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1553 * it returns NULL.
1554 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001555static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001556lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1557{
James Smart92d7f7b2007-06-17 19:56:38 -05001558 struct lpfc_dmabuf *d_buf;
1559 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001560 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001561
James Smart51ef4c22007-08-02 11:10:31 -04001562 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001563 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001564 return NULL;
1565
James Smart3772a992009-05-22 14:50:54 -04001566 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001567 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001568 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001569 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001570 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001571 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001572 }
1573 }
James Smart3772a992009-05-22 14:50:54 -04001574 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001576 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001577 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001578 return NULL;
James Smarted957682007-06-17 19:56:37 -05001579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001583 * @phba: Pointer to HBA context object.
1584 * @hbq_buffer: Pointer to HBQ buffer.
1585 *
1586 * This function is called with hbalock. This function gives back
1587 * the hbq buffer to firmware. If the HBQ does not have space to
1588 * post the buffer, it will free the buffer.
1589 **/
James Smarted957682007-06-17 19:56:37 -05001590void
James Smart51ef4c22007-08-02 11:10:31 -04001591lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001592{
1593 uint32_t hbqno;
1594
James Smart51ef4c22007-08-02 11:10:31 -04001595 if (hbq_buffer) {
1596 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001597 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001598 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001599 }
1600}
1601
James Smarte59058c2008-08-24 21:49:00 -04001602/**
James Smart3621a712009-04-06 18:47:14 -04001603 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001604 * @mbxCommand: mailbox command code.
1605 *
1606 * This function is called by the mailbox event handler function to verify
1607 * that the completed mailbox command is a legitimate mailbox command. If the
1608 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1609 * and the mailbox event handler will take the HBA offline.
1610 **/
dea31012005-04-17 16:05:31 -05001611static int
1612lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1613{
1614 uint8_t ret;
1615
1616 switch (mbxCommand) {
1617 case MBX_LOAD_SM:
1618 case MBX_READ_NV:
1619 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001620 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001621 case MBX_RUN_BIU_DIAG:
1622 case MBX_INIT_LINK:
1623 case MBX_DOWN_LINK:
1624 case MBX_CONFIG_LINK:
1625 case MBX_CONFIG_RING:
1626 case MBX_RESET_RING:
1627 case MBX_READ_CONFIG:
1628 case MBX_READ_RCONFIG:
1629 case MBX_READ_SPARM:
1630 case MBX_READ_STATUS:
1631 case MBX_READ_RPI:
1632 case MBX_READ_XRI:
1633 case MBX_READ_REV:
1634 case MBX_READ_LNK_STAT:
1635 case MBX_REG_LOGIN:
1636 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05001637 case MBX_CLEAR_LA:
1638 case MBX_DUMP_MEMORY:
1639 case MBX_DUMP_CONTEXT:
1640 case MBX_RUN_DIAGS:
1641 case MBX_RESTART:
1642 case MBX_UPDATE_CFG:
1643 case MBX_DOWN_LOAD:
1644 case MBX_DEL_LD_ENTRY:
1645 case MBX_RUN_PROGRAM:
1646 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001647 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001648 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001649 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001650 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001651 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001652 case MBX_LOAD_AREA:
1653 case MBX_RUN_BIU_DIAG64:
1654 case MBX_CONFIG_PORT:
1655 case MBX_READ_SPARM64:
1656 case MBX_READ_RPI64:
1657 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05001658 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05001659 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001660 case MBX_SET_DEBUG:
1661 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001662 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001663 case MBX_REG_VPI:
1664 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001665 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001666 case MBX_PORT_CAPABILITIES:
1667 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001668 case MBX_SLI4_CONFIG:
1669 case MBX_SLI4_REQ_FTRS:
1670 case MBX_REG_FCFI:
1671 case MBX_UNREG_FCFI:
1672 case MBX_REG_VFI:
1673 case MBX_UNREG_VFI:
1674 case MBX_INIT_VPI:
1675 case MBX_INIT_VFI:
1676 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04001677 case MBX_READ_EVENT_LOG_STATUS:
1678 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04001679 case MBX_SECURITY_MGMT:
1680 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05001681 ret = mbxCommand;
1682 break;
1683 default:
1684 ret = MBX_SHUTDOWN;
1685 break;
1686 }
James Smart2e0fef82007-06-17 19:56:36 -05001687 return ret;
dea31012005-04-17 16:05:31 -05001688}
James Smarte59058c2008-08-24 21:49:00 -04001689
1690/**
James Smart3621a712009-04-06 18:47:14 -04001691 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001692 * @phba: Pointer to HBA context object.
1693 * @pmboxq: Pointer to mailbox command.
1694 *
1695 * This is completion handler function for mailbox commands issued from
1696 * lpfc_sli_issue_mbox_wait function. This function is called by the
1697 * mailbox event handler function with no lock held. This function
1698 * will wake up thread waiting on the wait queue pointed by context1
1699 * of the mailbox.
1700 **/
James Smart04c68492009-05-22 14:52:52 -04001701void
James Smart2e0fef82007-06-17 19:56:36 -05001702lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001703{
1704 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001705 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001706
1707 /*
1708 * If pdone_q is empty, the driver thread gave up waiting and
1709 * continued running.
1710 */
James Smart7054a602007-04-25 09:52:34 -04001711 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001712 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001713 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1714 if (pdone_q)
1715 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001716 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001717 return;
1718}
1719
James Smarte59058c2008-08-24 21:49:00 -04001720
1721/**
James Smart3621a712009-04-06 18:47:14 -04001722 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001723 * @phba: Pointer to HBA context object.
1724 * @pmb: Pointer to mailbox object.
1725 *
1726 * This function is the default mailbox completion handler. It
1727 * frees the memory resources associated with the completed mailbox
1728 * command. If the completed command is a REG_LOGIN mailbox command,
1729 * this function will issue a UREG_LOGIN to re-claim the RPI.
1730 **/
dea31012005-04-17 16:05:31 -05001731void
James Smart2e0fef82007-06-17 19:56:36 -05001732lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001733{
James Smartd439d282010-09-29 11:18:45 -04001734 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001735 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04001736 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04001737 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04001738 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001739 int rc;
1740
dea31012005-04-17 16:05:31 -05001741 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001742
dea31012005-04-17 16:05:31 -05001743 if (mp) {
1744 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1745 kfree(mp);
1746 }
James Smart7054a602007-04-25 09:52:34 -04001747
James Smart04c68492009-05-22 14:52:52 -04001748 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
James Smart5af5eee2010-10-22 11:06:38 -04001749 (phba->sli_rev == LPFC_SLI_REV4) &&
1750 (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
James Smart04c68492009-05-22 14:52:52 -04001751 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1752
James Smart7054a602007-04-25 09:52:34 -04001753 /*
1754 * If a REG_LOGIN succeeded after node is destroyed or node
1755 * is in re-discovery driver need to cleanup the RPI.
1756 */
James Smart2e0fef82007-06-17 19:56:36 -05001757 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001758 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1759 !pmb->u.mb.mbxStatus) {
1760 rpi = pmb->u.mb.un.varWords[0];
1761 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1762 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001763 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001764 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1765 if (rc != MBX_NOT_FINISHED)
1766 return;
1767 }
1768
James Smart695a8142010-01-26 23:08:03 -05001769 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1770 !(phba->pport->load_flag & FC_UNLOADING) &&
1771 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04001772 shost = lpfc_shost_from_vport(vport);
1773 spin_lock_irq(shost->host_lock);
1774 vport->vpi_state |= LPFC_VPI_REGISTERED;
1775 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
1776 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05001777 }
1778
James Smartd439d282010-09-29 11:18:45 -04001779 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
1780 ndlp = (struct lpfc_nodelist *)pmb->context2;
1781 lpfc_nlp_put(ndlp);
1782 pmb->context2 = NULL;
1783 }
1784
James Smartdcf2a4e2010-09-29 11:18:53 -04001785 /* Check security permission status on INIT_LINK mailbox command */
1786 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
1787 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
1788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1789 "2860 SLI authentication is required "
1790 "for INIT_LINK but has not done yet\n");
1791
James Smart04c68492009-05-22 14:52:52 -04001792 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1793 lpfc_sli4_mbox_cmd_free(phba, pmb);
1794 else
1795 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001796}
1797
James Smarte59058c2008-08-24 21:49:00 -04001798/**
James Smart3621a712009-04-06 18:47:14 -04001799 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001800 * @phba: Pointer to HBA context object.
1801 *
1802 * This function is called with no lock held. This function processes all
1803 * the completed mailbox commands and gives it to upper layers. The interrupt
1804 * service routine processes mailbox completion interrupt and adds completed
1805 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1806 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1807 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1808 * function returns the mailbox commands to the upper layer by calling the
1809 * completion handler function of each mailbox.
1810 **/
dea31012005-04-17 16:05:31 -05001811int
James Smart2e0fef82007-06-17 19:56:36 -05001812lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001813{
James Smart92d7f7b2007-06-17 19:56:38 -05001814 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001815 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001816 int rc;
1817 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001818
1819 phba->sli.slistat.mbox_event++;
1820
James Smart92d7f7b2007-06-17 19:56:38 -05001821 /* Get all completed mailboxe buffers into the cmplq */
1822 spin_lock_irq(&phba->hbalock);
1823 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1824 spin_unlock_irq(&phba->hbalock);
1825
dea31012005-04-17 16:05:31 -05001826 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001827 do {
1828 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1829 if (pmb == NULL)
1830 break;
1831
James Smart04c68492009-05-22 14:52:52 -04001832 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001833
James Smart858c9f62007-06-17 19:56:39 -05001834 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1835 if (pmb->vport) {
1836 lpfc_debugfs_disc_trc(pmb->vport,
1837 LPFC_DISC_TRC_MBOX_VPORT,
1838 "MBOX cmpl vport: 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 else {
1844 lpfc_debugfs_disc_trc(phba->pport,
1845 LPFC_DISC_TRC_MBOX,
1846 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1847 (uint32_t)pmbox->mbxCommand,
1848 pmbox->un.varWords[0],
1849 pmbox->un.varWords[1]);
1850 }
1851 }
1852
dea31012005-04-17 16:05:31 -05001853 /*
1854 * It is a fatal error if unknown mbox command completion.
1855 */
1856 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1857 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001858 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001859 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001860 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001861 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001862 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001863 pmbox->mbxCommand,
1864 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001865 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001866 phba->work_hs = HS_FFER3;
1867 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001868 continue;
dea31012005-04-17 16:05:31 -05001869 }
1870
dea31012005-04-17 16:05:31 -05001871 if (pmbox->mbxStatus) {
1872 phba->sli.slistat.mbox_stat_err++;
1873 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1874 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001875 lpfc_printf_log(phba, KERN_INFO,
1876 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001877 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001878 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001879 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001880 pmb->vport ? pmb->vport->vpi :0,
1881 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001882 lpfc_sli4_mbox_opcode_get(phba,
1883 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001884 pmbox->mbxStatus,
1885 pmbox->un.varWords[0],
1886 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001887 pmbox->mbxStatus = 0;
1888 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001889 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001890 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001891 continue;
dea31012005-04-17 16:05:31 -05001892 }
1893 }
1894
1895 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001896 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001897 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001898 "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 -05001899 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001900 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001901 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001902 pmb->mbox_cmpl,
1903 *((uint32_t *) pmbox),
1904 pmbox->un.varWords[0],
1905 pmbox->un.varWords[1],
1906 pmbox->un.varWords[2],
1907 pmbox->un.varWords[3],
1908 pmbox->un.varWords[4],
1909 pmbox->un.varWords[5],
1910 pmbox->un.varWords[6],
1911 pmbox->un.varWords[7]);
1912
James Smart92d7f7b2007-06-17 19:56:38 -05001913 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001914 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001915 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001916 return 0;
dea31012005-04-17 16:05:31 -05001917}
James Smart92d7f7b2007-06-17 19:56:38 -05001918
James Smarte59058c2008-08-24 21:49:00 -04001919/**
James Smart3621a712009-04-06 18:47:14 -04001920 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001921 * @phba: Pointer to HBA context object.
1922 * @pring: Pointer to driver SLI ring object.
1923 * @tag: buffer tag.
1924 *
1925 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1926 * is set in the tag the buffer is posted for a particular exchange,
1927 * the function will return the buffer without replacing the buffer.
1928 * If the buffer is for unsolicited ELS or CT traffic, this function
1929 * returns the buffer and also posts another buffer to the firmware.
1930 **/
James Smart76bb24e2007-10-27 13:38:00 -04001931static struct lpfc_dmabuf *
1932lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001933 struct lpfc_sli_ring *pring,
1934 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001935{
James Smart9f1e1b52008-12-04 22:39:40 -05001936 struct hbq_dmabuf *hbq_entry;
1937
James Smart76bb24e2007-10-27 13:38:00 -04001938 if (tag & QUE_BUFTAG_BIT)
1939 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001940 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1941 if (!hbq_entry)
1942 return NULL;
1943 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001944}
James Smart57127f12007-10-27 13:37:05 -04001945
James Smart3772a992009-05-22 14:50:54 -04001946/**
1947 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1948 * @phba: Pointer to HBA context object.
1949 * @pring: Pointer to driver SLI ring object.
1950 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1951 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1952 * @fch_type: the type for the first frame of the sequence.
1953 *
1954 * This function is called with no lock held. This function uses the r_ctl and
1955 * type of the received sequence to find the correct callback function to call
1956 * to process the sequence.
1957 **/
1958static int
1959lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1960 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1961 uint32_t fch_type)
1962{
1963 int i;
1964
1965 /* unSolicited Responses */
1966 if (pring->prt[0].profile) {
1967 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1968 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1969 saveq);
1970 return 1;
1971 }
1972 /* We must search, based on rctl / type
1973 for the right routine */
1974 for (i = 0; i < pring->num_mask; i++) {
1975 if ((pring->prt[i].rctl == fch_r_ctl) &&
1976 (pring->prt[i].type == fch_type)) {
1977 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1978 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1979 (phba, pring, saveq);
1980 return 1;
1981 }
1982 }
1983 return 0;
1984}
James Smarte59058c2008-08-24 21:49:00 -04001985
1986/**
James Smart3621a712009-04-06 18:47:14 -04001987 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001988 * @phba: Pointer to HBA context object.
1989 * @pring: Pointer to driver SLI ring object.
1990 * @saveq: Pointer to the unsolicited iocb.
1991 *
1992 * This function is called with no lock held by the ring event handler
1993 * when there is an unsolicited iocb posted to the response ring by the
1994 * firmware. This function gets the buffer associated with the iocbs
1995 * and calls the event handler for the ring. This function handles both
1996 * qring buffers and hbq buffers.
1997 * When the function returns 1 the caller can free the iocb object otherwise
1998 * upper layer functions will free the iocb objects.
1999 **/
dea31012005-04-17 16:05:31 -05002000static int
2001lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2002 struct lpfc_iocbq *saveq)
2003{
2004 IOCB_t * irsp;
2005 WORD5 * w5p;
2006 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002007 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002008 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002009 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002010
2011 match = 0;
2012 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002013
2014 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2015 if (pring->lpfc_sli_rcv_async_status)
2016 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2017 else
2018 lpfc_printf_log(phba,
2019 KERN_WARNING,
2020 LOG_SLI,
2021 "0316 Ring %d handler: unexpected "
2022 "ASYNC_STATUS iocb received evt_code "
2023 "0x%x\n",
2024 pring->ringno,
2025 irsp->un.asyncstat.evt_code);
2026 return 1;
2027 }
2028
James Smart3163f722008-02-08 18:50:25 -05002029 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2030 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2031 if (irsp->ulpBdeCount > 0) {
2032 dmzbuf = lpfc_sli_get_buff(phba, pring,
2033 irsp->un.ulpWord[3]);
2034 lpfc_in_buf_free(phba, dmzbuf);
2035 }
2036
2037 if (irsp->ulpBdeCount > 1) {
2038 dmzbuf = lpfc_sli_get_buff(phba, pring,
2039 irsp->unsli3.sli3Words[3]);
2040 lpfc_in_buf_free(phba, dmzbuf);
2041 }
2042
2043 if (irsp->ulpBdeCount > 2) {
2044 dmzbuf = lpfc_sli_get_buff(phba, pring,
2045 irsp->unsli3.sli3Words[7]);
2046 lpfc_in_buf_free(phba, dmzbuf);
2047 }
2048
2049 return 1;
2050 }
2051
James Smart92d7f7b2007-06-17 19:56:38 -05002052 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002053 if (irsp->ulpBdeCount != 0) {
2054 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002055 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002056 if (!saveq->context2)
2057 lpfc_printf_log(phba,
2058 KERN_ERR,
2059 LOG_SLI,
2060 "0341 Ring %d Cannot find buffer for "
2061 "an unsolicited iocb. tag 0x%x\n",
2062 pring->ringno,
2063 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002064 }
2065 if (irsp->ulpBdeCount == 2) {
2066 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002067 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002068 if (!saveq->context3)
2069 lpfc_printf_log(phba,
2070 KERN_ERR,
2071 LOG_SLI,
2072 "0342 Ring %d Cannot find buffer for an"
2073 " unsolicited iocb. tag 0x%x\n",
2074 pring->ringno,
2075 irsp->unsli3.sli3Words[7]);
2076 }
2077 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002078 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002079 if (irsp->ulpBdeCount != 0) {
2080 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2081 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002082 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002083 lpfc_printf_log(phba,
2084 KERN_ERR,
2085 LOG_SLI,
2086 "0343 Ring %d Cannot find "
2087 "buffer for an unsolicited iocb"
2088 ". tag 0x%x\n", pring->ringno,
2089 irsp->un.ulpWord[3]);
2090 }
2091 if (irsp->ulpBdeCount == 2) {
2092 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2093 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002094 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002095 lpfc_printf_log(phba,
2096 KERN_ERR,
2097 LOG_SLI,
2098 "0344 Ring %d Cannot find "
2099 "buffer for an unsolicited "
2100 "iocb. tag 0x%x\n",
2101 pring->ringno,
2102 irsp->unsli3.sli3Words[7]);
2103 }
2104 }
James Smart92d7f7b2007-06-17 19:56:38 -05002105 }
James Smart9c2face2008-01-11 01:53:18 -05002106 if (irsp->ulpBdeCount != 0 &&
2107 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2108 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2109 int found = 0;
2110
2111 /* search continue save q for same XRI */
2112 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2113 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2114 list_add_tail(&saveq->list, &iocbq->list);
2115 found = 1;
2116 break;
2117 }
2118 }
2119 if (!found)
2120 list_add_tail(&saveq->clist,
2121 &pring->iocb_continue_saveq);
2122 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2123 list_del_init(&iocbq->clist);
2124 saveq = iocbq;
2125 irsp = &(saveq->iocb);
2126 } else
2127 return 0;
2128 }
2129 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2130 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2131 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002132 Rctl = FC_RCTL_ELS_REQ;
2133 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002134 } else {
2135 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2136 Rctl = w5p->hcsw.Rctl;
2137 Type = w5p->hcsw.Type;
2138
2139 /* Firmware Workaround */
2140 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2141 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2142 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002143 Rctl = FC_RCTL_ELS_REQ;
2144 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002145 w5p->hcsw.Rctl = Rctl;
2146 w5p->hcsw.Type = Type;
2147 }
2148 }
James Smart92d7f7b2007-06-17 19:56:38 -05002149
James Smart3772a992009-05-22 14:50:54 -04002150 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002151 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002152 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002153 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002154 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002155
James Smart92d7f7b2007-06-17 19:56:38 -05002156 return 1;
dea31012005-04-17 16:05:31 -05002157}
2158
James Smarte59058c2008-08-24 21:49:00 -04002159/**
James Smart3621a712009-04-06 18:47:14 -04002160 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002161 * @phba: Pointer to HBA context object.
2162 * @pring: Pointer to driver SLI ring object.
2163 * @prspiocb: Pointer to response iocb object.
2164 *
2165 * This function looks up the iocb_lookup table to get the command iocb
2166 * corresponding to the given response iocb using the iotag of the
2167 * response iocb. This function is called with the hbalock held.
2168 * This function returns the command iocb object if it finds the command
2169 * iocb else returns NULL.
2170 **/
dea31012005-04-17 16:05:31 -05002171static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002172lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2173 struct lpfc_sli_ring *pring,
2174 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002175{
dea31012005-04-17 16:05:31 -05002176 struct lpfc_iocbq *cmd_iocb = NULL;
2177 uint16_t iotag;
2178
James Bottomley604a3e32005-10-29 10:28:33 -05002179 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002180
James Bottomley604a3e32005-10-29 10:28:33 -05002181 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2182 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002183 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002184 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2185 pring->txcmplq_cnt--;
2186 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2187 }
James Bottomley604a3e32005-10-29 10:28:33 -05002188 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002189 }
2190
dea31012005-04-17 16:05:31 -05002191 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002192 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002193 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002194 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002195 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002196 return NULL;
2197}
2198
James Smarte59058c2008-08-24 21:49:00 -04002199/**
James Smart3772a992009-05-22 14:50:54 -04002200 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2201 * @phba: Pointer to HBA context object.
2202 * @pring: Pointer to driver SLI ring object.
2203 * @iotag: IOCB tag.
2204 *
2205 * This function looks up the iocb_lookup table to get the command iocb
2206 * corresponding to the given iotag. This function is called with the
2207 * hbalock held.
2208 * This function returns the command iocb object if it finds the command
2209 * iocb else returns NULL.
2210 **/
2211static struct lpfc_iocbq *
2212lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2213 struct lpfc_sli_ring *pring, uint16_t iotag)
2214{
2215 struct lpfc_iocbq *cmd_iocb;
2216
2217 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2218 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2219 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002220 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2221 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2222 pring->txcmplq_cnt--;
2223 }
James Smart3772a992009-05-22 14:50:54 -04002224 return cmd_iocb;
2225 }
2226
2227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2228 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2229 iotag, phba->sli.last_iotag);
2230 return NULL;
2231}
2232
2233/**
James Smart3621a712009-04-06 18:47:14 -04002234 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002235 * @phba: Pointer to HBA context object.
2236 * @pring: Pointer to driver SLI ring object.
2237 * @saveq: Pointer to the response iocb to be processed.
2238 *
2239 * This function is called by the ring event handler for non-fcp
2240 * rings when there is a new response iocb in the response ring.
2241 * The caller is not required to hold any locks. This function
2242 * gets the command iocb associated with the response iocb and
2243 * calls the completion handler for the command iocb. If there
2244 * is no completion handler, the function will free the resources
2245 * associated with command iocb. If the response iocb is for
2246 * an already aborted command iocb, the status of the completion
2247 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2248 * This function always returns 1.
2249 **/
dea31012005-04-17 16:05:31 -05002250static int
James Smart2e0fef82007-06-17 19:56:36 -05002251lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002252 struct lpfc_iocbq *saveq)
2253{
James Smart2e0fef82007-06-17 19:56:36 -05002254 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002255 int rc = 1;
2256 unsigned long iflag;
2257
2258 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002259 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002260 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002261 spin_unlock_irqrestore(&phba->hbalock, iflag);
2262
dea31012005-04-17 16:05:31 -05002263 if (cmdiocbp) {
2264 if (cmdiocbp->iocb_cmpl) {
2265 /*
James Smartea2151b2008-09-07 11:52:10 -04002266 * If an ELS command failed send an event to mgmt
2267 * application.
2268 */
2269 if (saveq->iocb.ulpStatus &&
2270 (pring->ringno == LPFC_ELS_RING) &&
2271 (cmdiocbp->iocb.ulpCommand ==
2272 CMD_ELS_REQUEST64_CR))
2273 lpfc_send_els_failure_event(phba,
2274 cmdiocbp, saveq);
2275
2276 /*
dea31012005-04-17 16:05:31 -05002277 * Post all ELS completions to the worker thread.
2278 * All other are passed to the completion callback.
2279 */
2280 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002281 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2282 (cmdiocbp->iocb_flag &
2283 LPFC_DRIVER_ABORTED)) {
2284 spin_lock_irqsave(&phba->hbalock,
2285 iflag);
James Smart07951072007-04-25 09:51:38 -04002286 cmdiocbp->iocb_flag &=
2287 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002288 spin_unlock_irqrestore(&phba->hbalock,
2289 iflag);
James Smart07951072007-04-25 09:51:38 -04002290 saveq->iocb.ulpStatus =
2291 IOSTAT_LOCAL_REJECT;
2292 saveq->iocb.un.ulpWord[4] =
2293 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002294
2295 /* Firmware could still be in progress
2296 * of DMAing payload, so don't free data
2297 * buffer till after a hbeat.
2298 */
James Smart341af102010-01-26 23:07:37 -05002299 spin_lock_irqsave(&phba->hbalock,
2300 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002301 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002302 spin_unlock_irqrestore(&phba->hbalock,
2303 iflag);
2304 }
James Smart0f65ff62010-02-26 14:14:23 -05002305 if (phba->sli_rev == LPFC_SLI_REV4) {
2306 if (saveq->iocb_flag &
2307 LPFC_EXCHANGE_BUSY) {
2308 /* Set cmdiocb flag for the
2309 * exchange busy so sgl (xri)
2310 * will not be released until
2311 * the abort xri is received
2312 * from hba.
2313 */
2314 spin_lock_irqsave(
2315 &phba->hbalock, iflag);
2316 cmdiocbp->iocb_flag |=
2317 LPFC_EXCHANGE_BUSY;
2318 spin_unlock_irqrestore(
2319 &phba->hbalock, iflag);
2320 }
2321 if (cmdiocbp->iocb_flag &
2322 LPFC_DRIVER_ABORTED) {
2323 /*
2324 * Clear LPFC_DRIVER_ABORTED
2325 * bit in case it was driver
2326 * initiated abort.
2327 */
2328 spin_lock_irqsave(
2329 &phba->hbalock, iflag);
2330 cmdiocbp->iocb_flag &=
2331 ~LPFC_DRIVER_ABORTED;
2332 spin_unlock_irqrestore(
2333 &phba->hbalock, iflag);
2334 cmdiocbp->iocb.ulpStatus =
2335 IOSTAT_LOCAL_REJECT;
2336 cmdiocbp->iocb.un.ulpWord[4] =
2337 IOERR_ABORT_REQUESTED;
2338 /*
2339 * For SLI4, irsiocb contains
2340 * NO_XRI in sli_xritag, it
2341 * shall not affect releasing
2342 * sgl (xri) process.
2343 */
2344 saveq->iocb.ulpStatus =
2345 IOSTAT_LOCAL_REJECT;
2346 saveq->iocb.un.ulpWord[4] =
2347 IOERR_SLI_ABORTED;
2348 spin_lock_irqsave(
2349 &phba->hbalock, iflag);
2350 saveq->iocb_flag |=
2351 LPFC_DELAY_MEM_FREE;
2352 spin_unlock_irqrestore(
2353 &phba->hbalock, iflag);
2354 }
James Smart07951072007-04-25 09:51:38 -04002355 }
dea31012005-04-17 16:05:31 -05002356 }
James Smart2e0fef82007-06-17 19:56:36 -05002357 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002358 } else
2359 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002360 } else {
2361 /*
2362 * Unknown initiating command based on the response iotag.
2363 * This could be the case on the ELS ring because of
2364 * lpfc_els_abort().
2365 */
2366 if (pring->ringno != LPFC_ELS_RING) {
2367 /*
2368 * Ring <ringno> handler: unexpected completion IoTag
2369 * <IoTag>
2370 */
James Smarta257bf92009-04-06 18:48:10 -04002371 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002372 "0322 Ring %d handler: "
2373 "unexpected completion IoTag x%x "
2374 "Data: x%x x%x x%x x%x\n",
2375 pring->ringno,
2376 saveq->iocb.ulpIoTag,
2377 saveq->iocb.ulpStatus,
2378 saveq->iocb.un.ulpWord[4],
2379 saveq->iocb.ulpCommand,
2380 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002381 }
2382 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002383
dea31012005-04-17 16:05:31 -05002384 return rc;
2385}
2386
James Smarte59058c2008-08-24 21:49:00 -04002387/**
James Smart3621a712009-04-06 18:47:14 -04002388 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002389 * @phba: Pointer to HBA context object.
2390 * @pring: Pointer to driver SLI ring object.
2391 *
2392 * This function is called from the iocb ring event handlers when
2393 * put pointer is ahead of the get pointer for a ring. This function signal
2394 * an error attention condition to the worker thread and the worker
2395 * thread will transition the HBA to offline state.
2396 **/
James Smart2e0fef82007-06-17 19:56:36 -05002397static void
2398lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002399{
James Smart34b02dc2008-08-24 21:49:55 -04002400 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002401 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002402 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002403 * rsp ring <portRspMax>
2404 */
2405 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002406 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002407 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002408 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002409 pring->numRiocb);
2410
James Smart2e0fef82007-06-17 19:56:36 -05002411 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002412
2413 /*
2414 * All error attention handlers are posted to
2415 * worker thread
2416 */
2417 phba->work_ha |= HA_ERATT;
2418 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002419
James Smart5e9d9b82008-06-14 22:52:53 -04002420 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002421
2422 return;
2423}
2424
James Smarte59058c2008-08-24 21:49:00 -04002425/**
James Smart3621a712009-04-06 18:47:14 -04002426 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002427 * @ptr: Pointer to address of HBA context object.
2428 *
2429 * This function is invoked by the Error Attention polling timer when the
2430 * timer times out. It will check the SLI Error Attention register for
2431 * possible attention events. If so, it will post an Error Attention event
2432 * and wake up worker thread to process it. Otherwise, it will set up the
2433 * Error Attention polling timer for the next poll.
2434 **/
2435void lpfc_poll_eratt(unsigned long ptr)
2436{
2437 struct lpfc_hba *phba;
2438 uint32_t eratt = 0;
2439
2440 phba = (struct lpfc_hba *)ptr;
2441
2442 /* Check chip HA register for error event */
2443 eratt = lpfc_sli_check_eratt(phba);
2444
2445 if (eratt)
2446 /* Tell the worker thread there is work to do */
2447 lpfc_worker_wake_up(phba);
2448 else
2449 /* Restart the timer for next eratt poll */
2450 mod_timer(&phba->eratt_poll, jiffies +
2451 HZ * LPFC_ERATT_POLL_INTERVAL);
2452 return;
2453}
2454
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002455
James Smarte59058c2008-08-24 21:49:00 -04002456/**
James Smart3621a712009-04-06 18:47:14 -04002457 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002458 * @phba: Pointer to HBA context object.
2459 * @pring: Pointer to driver SLI ring object.
2460 * @mask: Host attention register mask for this ring.
2461 *
2462 * This function is called from the interrupt context when there is a ring
2463 * event for the fcp ring. The caller does not hold any lock.
2464 * The function processes each response iocb in the response ring until it
2465 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2466 * LE bit set. The function will call the completion handler of the command iocb
2467 * if the response iocb indicates a completion for a command iocb or it is
2468 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2469 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002470 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002471 * to check it explicitly.
2472 */
2473int
James Smart2e0fef82007-06-17 19:56:36 -05002474lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2475 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002476{
James Smart34b02dc2008-08-24 21:49:55 -04002477 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002478 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002479 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002480 struct lpfc_iocbq *cmdiocbq = NULL;
2481 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002482 uint32_t status;
2483 uint32_t portRspPut, portRspMax;
2484 int rc = 1;
2485 lpfc_iocb_type type;
2486 unsigned long iflag;
2487 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002488
James Smart2e0fef82007-06-17 19:56:36 -05002489 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002490 pring->stats.iocb_event++;
2491
dea31012005-04-17 16:05:31 -05002492 /*
2493 * The next available response entry should never exceed the maximum
2494 * entries. If it does, treat it as an adapter hardware error.
2495 */
2496 portRspMax = pring->numRiocb;
2497 portRspPut = le32_to_cpu(pgp->rspPutInx);
2498 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002499 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002500 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002501 return 1;
2502 }
James Smart45ed1192009-10-02 15:17:02 -04002503 if (phba->fcp_ring_in_use) {
2504 spin_unlock_irqrestore(&phba->hbalock, iflag);
2505 return 1;
2506 } else
2507 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002508
2509 rmb();
2510 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002511 /*
2512 * Fetch an entry off the ring and copy it into a local data
2513 * structure. The copy involves a byte-swap since the
2514 * network byte order and pci byte orders are different.
2515 */
James Smarted957682007-06-17 19:56:37 -05002516 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002517 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002518
2519 if (++pring->rspidx >= portRspMax)
2520 pring->rspidx = 0;
2521
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002522 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2523 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002524 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002525 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002526 irsp = &rspiocbq.iocb;
2527
dea31012005-04-17 16:05:31 -05002528 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2529 pring->stats.iocb_rsp++;
2530 rsp_cmpl++;
2531
2532 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002533 /*
2534 * If resource errors reported from HBA, reduce
2535 * queuedepths of the SCSI device.
2536 */
2537 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2538 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2539 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002540 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002541 spin_lock_irqsave(&phba->hbalock, iflag);
2542 }
2543
dea31012005-04-17 16:05:31 -05002544 /* Rsp ring <ringno> error: IOCB */
2545 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002546 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002547 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002548 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002549 irsp->un.ulpWord[0],
2550 irsp->un.ulpWord[1],
2551 irsp->un.ulpWord[2],
2552 irsp->un.ulpWord[3],
2553 irsp->un.ulpWord[4],
2554 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002555 *(uint32_t *)&irsp->un1,
2556 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002557 }
2558
2559 switch (type) {
2560 case LPFC_ABORT_IOCB:
2561 case LPFC_SOL_IOCB:
2562 /*
2563 * Idle exchange closed via ABTS from port. No iocb
2564 * resources need to be recovered.
2565 */
2566 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002567 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002568 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002569 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002570 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002571 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002572 break;
2573 }
2574
James Bottomley604a3e32005-10-29 10:28:33 -05002575 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2576 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002577 if (unlikely(!cmdiocbq))
2578 break;
2579 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2580 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2581 if (cmdiocbq->iocb_cmpl) {
2582 spin_unlock_irqrestore(&phba->hbalock, iflag);
2583 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2584 &rspiocbq);
2585 spin_lock_irqsave(&phba->hbalock, iflag);
2586 }
dea31012005-04-17 16:05:31 -05002587 break;
James Smarta4bc3372006-12-02 13:34:16 -05002588 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002589 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002590 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002591 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002592 break;
dea31012005-04-17 16:05:31 -05002593 default:
2594 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2595 char adaptermsg[LPFC_MAX_ADPTMSG];
2596 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2597 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2598 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002599 dev_warn(&((phba->pcidev)->dev),
2600 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002601 phba->brd_no, adaptermsg);
2602 } else {
2603 /* Unknown IOCB command */
2604 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002605 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002606 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002607 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002608 irsp->ulpStatus,
2609 irsp->ulpIoTag,
2610 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002611 }
2612 break;
2613 }
2614
2615 /*
2616 * The response IOCB has been processed. Update the ring
2617 * pointer in SLIM. If the port response put pointer has not
2618 * been updated, sync the pgp->rspPutInx and fetch the new port
2619 * response put pointer.
2620 */
James Smarted957682007-06-17 19:56:37 -05002621 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002622
2623 if (pring->rspidx == portRspPut)
2624 portRspPut = le32_to_cpu(pgp->rspPutInx);
2625 }
2626
2627 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2628 pring->stats.iocb_rsp_full++;
2629 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2630 writel(status, phba->CAregaddr);
2631 readl(phba->CAregaddr);
2632 }
2633 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2634 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2635 pring->stats.iocb_cmd_empty++;
2636
2637 /* Force update of the local copy of cmdGetInx */
2638 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2639 lpfc_sli_resume_iocb(phba, pring);
2640
2641 if ((pring->lpfc_sli_cmd_available))
2642 (pring->lpfc_sli_cmd_available) (phba, pring);
2643
2644 }
2645
James Smart45ed1192009-10-02 15:17:02 -04002646 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002647 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002648 return rc;
2649}
2650
James Smarte59058c2008-08-24 21:49:00 -04002651/**
James Smart3772a992009-05-22 14:50:54 -04002652 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2653 * @phba: Pointer to HBA context object.
2654 * @pring: Pointer to driver SLI ring object.
2655 * @rspiocbp: Pointer to driver response IOCB object.
2656 *
2657 * This function is called from the worker thread when there is a slow-path
2658 * response IOCB to process. This function chains all the response iocbs until
2659 * seeing the iocb with the LE bit set. The function will call
2660 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2661 * completion of a command iocb. The function will call the
2662 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2663 * The function frees the resources or calls the completion handler if this
2664 * iocb is an abort completion. The function returns NULL when the response
2665 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2666 * this function shall chain the iocb on to the iocb_continueq and return the
2667 * response iocb passed in.
2668 **/
2669static struct lpfc_iocbq *
2670lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2671 struct lpfc_iocbq *rspiocbp)
2672{
2673 struct lpfc_iocbq *saveq;
2674 struct lpfc_iocbq *cmdiocbp;
2675 struct lpfc_iocbq *next_iocb;
2676 IOCB_t *irsp = NULL;
2677 uint32_t free_saveq;
2678 uint8_t iocb_cmd_type;
2679 lpfc_iocb_type type;
2680 unsigned long iflag;
2681 int rc;
2682
2683 spin_lock_irqsave(&phba->hbalock, iflag);
2684 /* First add the response iocb to the countinueq list */
2685 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2686 pring->iocb_continueq_cnt++;
2687
2688 /* Now, determine whetehr the list is completed for processing */
2689 irsp = &rspiocbp->iocb;
2690 if (irsp->ulpLe) {
2691 /*
2692 * By default, the driver expects to free all resources
2693 * associated with this iocb completion.
2694 */
2695 free_saveq = 1;
2696 saveq = list_get_first(&pring->iocb_continueq,
2697 struct lpfc_iocbq, list);
2698 irsp = &(saveq->iocb);
2699 list_del_init(&pring->iocb_continueq);
2700 pring->iocb_continueq_cnt = 0;
2701
2702 pring->stats.iocb_rsp++;
2703
2704 /*
2705 * If resource errors reported from HBA, reduce
2706 * queuedepths of the SCSI device.
2707 */
2708 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2709 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2710 spin_unlock_irqrestore(&phba->hbalock, iflag);
2711 phba->lpfc_rampdown_queue_depth(phba);
2712 spin_lock_irqsave(&phba->hbalock, iflag);
2713 }
2714
2715 if (irsp->ulpStatus) {
2716 /* Rsp ring <ringno> error: IOCB */
2717 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2718 "0328 Rsp Ring %d error: "
2719 "IOCB Data: "
2720 "x%x x%x x%x x%x "
2721 "x%x x%x x%x x%x "
2722 "x%x x%x x%x x%x "
2723 "x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 irsp->un.ulpWord[0],
2726 irsp->un.ulpWord[1],
2727 irsp->un.ulpWord[2],
2728 irsp->un.ulpWord[3],
2729 irsp->un.ulpWord[4],
2730 irsp->un.ulpWord[5],
2731 *(((uint32_t *) irsp) + 6),
2732 *(((uint32_t *) irsp) + 7),
2733 *(((uint32_t *) irsp) + 8),
2734 *(((uint32_t *) irsp) + 9),
2735 *(((uint32_t *) irsp) + 10),
2736 *(((uint32_t *) irsp) + 11),
2737 *(((uint32_t *) irsp) + 12),
2738 *(((uint32_t *) irsp) + 13),
2739 *(((uint32_t *) irsp) + 14),
2740 *(((uint32_t *) irsp) + 15));
2741 }
2742
2743 /*
2744 * Fetch the IOCB command type and call the correct completion
2745 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2746 * get freed back to the lpfc_iocb_list by the discovery
2747 * kernel thread.
2748 */
2749 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2750 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2751 switch (type) {
2752 case LPFC_SOL_IOCB:
2753 spin_unlock_irqrestore(&phba->hbalock, iflag);
2754 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2755 spin_lock_irqsave(&phba->hbalock, iflag);
2756 break;
2757
2758 case LPFC_UNSOL_IOCB:
2759 spin_unlock_irqrestore(&phba->hbalock, iflag);
2760 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2761 spin_lock_irqsave(&phba->hbalock, iflag);
2762 if (!rc)
2763 free_saveq = 0;
2764 break;
2765
2766 case LPFC_ABORT_IOCB:
2767 cmdiocbp = NULL;
2768 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2769 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2770 saveq);
2771 if (cmdiocbp) {
2772 /* Call the specified completion routine */
2773 if (cmdiocbp->iocb_cmpl) {
2774 spin_unlock_irqrestore(&phba->hbalock,
2775 iflag);
2776 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2777 saveq);
2778 spin_lock_irqsave(&phba->hbalock,
2779 iflag);
2780 } else
2781 __lpfc_sli_release_iocbq(phba,
2782 cmdiocbp);
2783 }
2784 break;
2785
2786 case LPFC_UNKNOWN_IOCB:
2787 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2788 char adaptermsg[LPFC_MAX_ADPTMSG];
2789 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2790 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2791 MAX_MSG_DATA);
2792 dev_warn(&((phba->pcidev)->dev),
2793 "lpfc%d: %s\n",
2794 phba->brd_no, adaptermsg);
2795 } else {
2796 /* Unknown IOCB command */
2797 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2798 "0335 Unknown IOCB "
2799 "command Data: x%x "
2800 "x%x x%x x%x\n",
2801 irsp->ulpCommand,
2802 irsp->ulpStatus,
2803 irsp->ulpIoTag,
2804 irsp->ulpContext);
2805 }
2806 break;
2807 }
2808
2809 if (free_saveq) {
2810 list_for_each_entry_safe(rspiocbp, next_iocb,
2811 &saveq->list, list) {
2812 list_del(&rspiocbp->list);
2813 __lpfc_sli_release_iocbq(phba, rspiocbp);
2814 }
2815 __lpfc_sli_release_iocbq(phba, saveq);
2816 }
2817 rspiocbp = NULL;
2818 }
2819 spin_unlock_irqrestore(&phba->hbalock, iflag);
2820 return rspiocbp;
2821}
2822
2823/**
2824 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002825 * @phba: Pointer to HBA context object.
2826 * @pring: Pointer to driver SLI ring object.
2827 * @mask: Host attention register mask for this ring.
2828 *
James Smart3772a992009-05-22 14:50:54 -04002829 * This routine wraps the actual slow_ring event process routine from the
2830 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002831 **/
James Smart3772a992009-05-22 14:50:54 -04002832void
James Smart2e0fef82007-06-17 19:56:36 -05002833lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2834 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002835{
James Smart3772a992009-05-22 14:50:54 -04002836 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2837}
2838
2839/**
2840 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2841 * @phba: Pointer to HBA context object.
2842 * @pring: Pointer to driver SLI ring object.
2843 * @mask: Host attention register mask for this ring.
2844 *
2845 * This function is called from the worker thread when there is a ring event
2846 * for non-fcp rings. The caller does not hold any lock. The function will
2847 * remove each response iocb in the response ring and calls the handle
2848 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2849 **/
2850static void
2851lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2852 struct lpfc_sli_ring *pring, uint32_t mask)
2853{
James Smart34b02dc2008-08-24 21:49:55 -04002854 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002855 IOCB_t *entry;
2856 IOCB_t *irsp = NULL;
2857 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002858 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002859 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002860 uint32_t status;
dea31012005-04-17 16:05:31 -05002861
James Smart34b02dc2008-08-24 21:49:55 -04002862 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002863 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002864 pring->stats.iocb_event++;
2865
dea31012005-04-17 16:05:31 -05002866 /*
2867 * The next available response entry should never exceed the maximum
2868 * entries. If it does, treat it as an adapter hardware error.
2869 */
2870 portRspMax = pring->numRiocb;
2871 portRspPut = le32_to_cpu(pgp->rspPutInx);
2872 if (portRspPut >= portRspMax) {
2873 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002874 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002875 * rsp ring <portRspMax>
2876 */
James Smarted957682007-06-17 19:56:37 -05002877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002878 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002879 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002880 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002881
James Smart2e0fef82007-06-17 19:56:36 -05002882 phba->link_state = LPFC_HBA_ERROR;
2883 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002884
2885 phba->work_hs = HS_FFER3;
2886 lpfc_handle_eratt(phba);
2887
James Smart3772a992009-05-22 14:50:54 -04002888 return;
dea31012005-04-17 16:05:31 -05002889 }
2890
2891 rmb();
dea31012005-04-17 16:05:31 -05002892 while (pring->rspidx != portRspPut) {
2893 /*
2894 * Build a completion list and call the appropriate handler.
2895 * The process is to get the next available response iocb, get
2896 * a free iocb from the list, copy the response data into the
2897 * free iocb, insert to the continuation list, and update the
2898 * next response index to slim. This process makes response
2899 * iocb's in the ring available to DMA as fast as possible but
2900 * pays a penalty for a copy operation. Since the iocb is
2901 * only 32 bytes, this penalty is considered small relative to
2902 * the PCI reads for register values and a slim write. When
2903 * the ulpLe field is set, the entire Command has been
2904 * received.
2905 */
James Smarted957682007-06-17 19:56:37 -05002906 entry = lpfc_resp_iocb(phba, pring);
2907
James Smart858c9f62007-06-17 19:56:39 -05002908 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002909 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002910 if (rspiocbp == NULL) {
2911 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002912 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002913 break;
2914 }
2915
James Smarted957682007-06-17 19:56:37 -05002916 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2917 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002918 irsp = &rspiocbp->iocb;
2919
2920 if (++pring->rspidx >= portRspMax)
2921 pring->rspidx = 0;
2922
James Smarta58cbd52007-08-02 11:09:43 -04002923 if (pring->ringno == LPFC_ELS_RING) {
2924 lpfc_debugfs_slow_ring_trc(phba,
2925 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2926 *(((uint32_t *) irsp) + 4),
2927 *(((uint32_t *) irsp) + 6),
2928 *(((uint32_t *) irsp) + 7));
2929 }
2930
James Smarted957682007-06-17 19:56:37 -05002931 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002932
James Smart3772a992009-05-22 14:50:54 -04002933 spin_unlock_irqrestore(&phba->hbalock, iflag);
2934 /* Handle the response IOCB */
2935 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2936 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002937
2938 /*
2939 * If the port response put pointer has not been updated, sync
2940 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2941 * response put pointer.
2942 */
2943 if (pring->rspidx == portRspPut) {
2944 portRspPut = le32_to_cpu(pgp->rspPutInx);
2945 }
2946 } /* while (pring->rspidx != portRspPut) */
2947
James Smart92d7f7b2007-06-17 19:56:38 -05002948 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002949 /* At least one response entry has been freed */
2950 pring->stats.iocb_rsp_full++;
2951 /* SET RxRE_RSP in Chip Att register */
2952 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2953 writel(status, phba->CAregaddr);
2954 readl(phba->CAregaddr); /* flush */
2955 }
2956 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2957 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2958 pring->stats.iocb_cmd_empty++;
2959
2960 /* Force update of the local copy of cmdGetInx */
2961 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2962 lpfc_sli_resume_iocb(phba, pring);
2963
2964 if ((pring->lpfc_sli_cmd_available))
2965 (pring->lpfc_sli_cmd_available) (phba, pring);
2966
2967 }
2968
James Smart2e0fef82007-06-17 19:56:36 -05002969 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002970 return;
dea31012005-04-17 16:05:31 -05002971}
2972
James Smarte59058c2008-08-24 21:49:00 -04002973/**
James Smart4f774512009-05-22 14:52:35 -04002974 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2975 * @phba: Pointer to HBA context object.
2976 * @pring: Pointer to driver SLI ring object.
2977 * @mask: Host attention register mask for this ring.
2978 *
2979 * This function is called from the worker thread when there is a pending
2980 * ELS response iocb on the driver internal slow-path response iocb worker
2981 * queue. The caller does not hold any lock. The function will remove each
2982 * response iocb from the response worker queue and calls the handle
2983 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2984 **/
2985static void
2986lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2987 struct lpfc_sli_ring *pring, uint32_t mask)
2988{
2989 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002990 struct hbq_dmabuf *dmabuf;
2991 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002992 unsigned long iflag;
2993
James Smart45ed1192009-10-02 15:17:02 -04002994 spin_lock_irqsave(&phba->hbalock, iflag);
2995 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2996 spin_unlock_irqrestore(&phba->hbalock, iflag);
2997 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002998 /* Get the response iocb from the head of work queue */
2999 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003000 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003001 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003002 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003003
3004 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3005 case CQE_CODE_COMPL_WQE:
3006 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3007 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003008 /* Translate ELS WCQE to response IOCBQ */
3009 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3010 irspiocbq);
3011 if (irspiocbq)
3012 lpfc_sli_sp_handle_rspiocb(phba, pring,
3013 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003014 break;
3015 case CQE_CODE_RECEIVE:
3016 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3017 cq_event);
3018 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3019 break;
3020 default:
3021 break;
3022 }
James Smart4f774512009-05-22 14:52:35 -04003023 }
3024}
3025
3026/**
James Smart3621a712009-04-06 18:47:14 -04003027 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003028 * @phba: Pointer to HBA context object.
3029 * @pring: Pointer to driver SLI ring object.
3030 *
3031 * This function aborts all iocbs in the given ring and frees all the iocb
3032 * objects in txq. This function issues an abort iocb for all the iocb commands
3033 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3034 * the return of this function. The caller is not required to hold any locks.
3035 **/
James Smart2e0fef82007-06-17 19:56:36 -05003036void
dea31012005-04-17 16:05:31 -05003037lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3038{
James Smart2534ba72007-04-25 09:52:20 -04003039 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003040 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003041
James Smart92d7f7b2007-06-17 19:56:38 -05003042 if (pring->ringno == LPFC_ELS_RING) {
3043 lpfc_fabric_abort_hba(phba);
3044 }
3045
dea31012005-04-17 16:05:31 -05003046 /* Error everything on txq and txcmplq
3047 * First do the txq.
3048 */
James Smart2e0fef82007-06-17 19:56:36 -05003049 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003050 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003051 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003052
3053 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003054 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3055 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3056
James Smart2e0fef82007-06-17 19:56:36 -05003057 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003058
James Smarta257bf92009-04-06 18:48:10 -04003059 /* Cancel all the IOCBs from the completions list */
3060 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3061 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003062}
3063
James Smarte59058c2008-08-24 21:49:00 -04003064/**
James Smart3621a712009-04-06 18:47:14 -04003065 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003066 * @phba: Pointer to HBA context object.
3067 *
3068 * This function flushes all iocbs in the fcp ring and frees all the iocb
3069 * objects in txq and txcmplq. This function will not issue abort iocbs
3070 * for all the iocb commands in txcmplq, they will just be returned with
3071 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3072 * slot has been permanently disabled.
3073 **/
3074void
3075lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3076{
3077 LIST_HEAD(txq);
3078 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003079 struct lpfc_sli *psli = &phba->sli;
3080 struct lpfc_sli_ring *pring;
3081
3082 /* Currently, only one fcp ring */
3083 pring = &psli->ring[psli->fcp_ring];
3084
3085 spin_lock_irq(&phba->hbalock);
3086 /* Retrieve everything on txq */
3087 list_splice_init(&pring->txq, &txq);
3088 pring->txq_cnt = 0;
3089
3090 /* Retrieve everything on the txcmplq */
3091 list_splice_init(&pring->txcmplq, &txcmplq);
3092 pring->txcmplq_cnt = 0;
3093 spin_unlock_irq(&phba->hbalock);
3094
3095 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003096 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3097 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003098
3099 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003100 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3101 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003102}
3103
3104/**
James Smart3772a992009-05-22 14:50:54 -04003105 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003106 * @phba: Pointer to HBA context object.
3107 * @mask: Bit mask to be checked.
3108 *
3109 * This function reads the host status register and compares
3110 * with the provided bit mask to check if HBA completed
3111 * the restart. This function will wait in a loop for the
3112 * HBA to complete restart. If the HBA does not restart within
3113 * 15 iterations, the function will reset the HBA again. The
3114 * function returns 1 when HBA fail to restart otherwise returns
3115 * zero.
3116 **/
James Smart3772a992009-05-22 14:50:54 -04003117static int
3118lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003119{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003120 uint32_t status;
3121 int i = 0;
3122 int retval = 0;
dea31012005-04-17 16:05:31 -05003123
Jamie Wellnitz41415862006-02-28 19:25:27 -05003124 /* Read the HBA Host Status Register */
3125 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003126
Jamie Wellnitz41415862006-02-28 19:25:27 -05003127 /*
3128 * Check status register every 100ms for 5 retries, then every
3129 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3130 * every 2.5 sec for 4.
3131 * Break our of the loop if errors occurred during init.
3132 */
3133 while (((status & mask) != mask) &&
3134 !(status & HS_FFERM) &&
3135 i++ < 20) {
dea31012005-04-17 16:05:31 -05003136
Jamie Wellnitz41415862006-02-28 19:25:27 -05003137 if (i <= 5)
3138 msleep(10);
3139 else if (i <= 10)
3140 msleep(500);
3141 else
3142 msleep(2500);
dea31012005-04-17 16:05:31 -05003143
Jamie Wellnitz41415862006-02-28 19:25:27 -05003144 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003145 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003146 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003147 lpfc_sli_brdrestart(phba);
3148 }
3149 /* Read the HBA Host Status Register */
3150 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003151 }
dea31012005-04-17 16:05:31 -05003152
Jamie Wellnitz41415862006-02-28 19:25:27 -05003153 /* Check to see if any errors occurred during init */
3154 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3156 "2751 Adapter failed to restart, "
3157 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3158 status,
3159 readl(phba->MBslimaddr + 0xa8),
3160 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003161 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003162 retval = 1;
3163 }
dea31012005-04-17 16:05:31 -05003164
Jamie Wellnitz41415862006-02-28 19:25:27 -05003165 return retval;
dea31012005-04-17 16:05:31 -05003166}
3167
James Smartda0436e2009-05-22 14:51:39 -04003168/**
3169 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3170 * @phba: Pointer to HBA context object.
3171 * @mask: Bit mask to be checked.
3172 *
3173 * This function checks the host status register to check if HBA is
3174 * ready. This function will wait in a loop for the HBA to be ready
3175 * If the HBA is not ready , the function will will reset the HBA PCI
3176 * function again. The function returns 1 when HBA fail to be ready
3177 * otherwise returns zero.
3178 **/
3179static int
3180lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3181{
3182 uint32_t status;
3183 int retval = 0;
3184
3185 /* Read the HBA Host Status Register */
3186 status = lpfc_sli4_post_status_check(phba);
3187
3188 if (status) {
3189 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3190 lpfc_sli_brdrestart(phba);
3191 status = lpfc_sli4_post_status_check(phba);
3192 }
3193
3194 /* Check to see if any errors occurred during init */
3195 if (status) {
3196 phba->link_state = LPFC_HBA_ERROR;
3197 retval = 1;
3198 } else
3199 phba->sli4_hba.intr_enable = 0;
3200
3201 return retval;
3202}
3203
3204/**
3205 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3206 * @phba: Pointer to HBA context object.
3207 * @mask: Bit mask to be checked.
3208 *
3209 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3210 * from the API jump table function pointer from the lpfc_hba struct.
3211 **/
3212int
3213lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3214{
3215 return phba->lpfc_sli_brdready(phba, mask);
3216}
3217
James Smart92908312006-03-07 15:04:13 -05003218#define BARRIER_TEST_PATTERN (0xdeadbeef)
3219
James Smarte59058c2008-08-24 21:49:00 -04003220/**
James Smart3621a712009-04-06 18:47:14 -04003221 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003222 * @phba: Pointer to HBA context object.
3223 *
3224 * This function is called before resetting an HBA. This
3225 * function requests HBA to quiesce DMAs before a reset.
3226 **/
James Smart2e0fef82007-06-17 19:56:36 -05003227void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003228{
James Smart65a29c12006-07-06 15:50:50 -04003229 uint32_t __iomem *resp_buf;
3230 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003231 volatile uint32_t mbox;
3232 uint32_t hc_copy;
3233 int i;
3234 uint8_t hdrtype;
3235
3236 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3237 if (hdrtype != 0x80 ||
3238 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3239 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3240 return;
3241
3242 /*
3243 * Tell the other part of the chip to suspend temporarily all
3244 * its DMA activity.
3245 */
James Smart65a29c12006-07-06 15:50:50 -04003246 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003247
3248 /* Disable the error attention */
3249 hc_copy = readl(phba->HCregaddr);
3250 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3251 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003252 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003253
3254 if (readl(phba->HAregaddr) & HA_ERATT) {
3255 /* Clear Chip error bit */
3256 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003257 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003258 }
3259
3260 mbox = 0;
3261 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3262 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3263
3264 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003265 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003266 writel(mbox, mbox_buf);
3267
3268 for (i = 0;
3269 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3270 mdelay(1);
3271
3272 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003273 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003274 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003275 goto restore_hc;
3276 else
3277 goto clear_errat;
3278 }
3279
3280 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3281 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3282 mdelay(1);
3283
3284clear_errat:
3285
3286 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3287 mdelay(1);
3288
3289 if (readl(phba->HAregaddr) & HA_ERATT) {
3290 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003291 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003292 }
3293
3294restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003295 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003296 writel(hc_copy, phba->HCregaddr);
3297 readl(phba->HCregaddr); /* flush */
3298}
3299
James Smarte59058c2008-08-24 21:49:00 -04003300/**
James Smart3621a712009-04-06 18:47:14 -04003301 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003302 * @phba: Pointer to HBA context object.
3303 *
3304 * This function issues a kill_board mailbox command and waits for
3305 * the error attention interrupt. This function is called for stopping
3306 * the firmware processing. The caller is not required to hold any
3307 * locks. This function calls lpfc_hba_down_post function to free
3308 * any pending commands after the kill. The function will return 1 when it
3309 * fails to kill the board else will return 0.
3310 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003311int
James Smart2e0fef82007-06-17 19:56:36 -05003312lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003313{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003314 struct lpfc_sli *psli;
3315 LPFC_MBOXQ_t *pmb;
3316 uint32_t status;
3317 uint32_t ha_copy;
3318 int retval;
3319 int i = 0;
3320
3321 psli = &phba->sli;
3322
3323 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003324 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003325 "0329 Kill HBA Data: x%x x%x\n",
3326 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003327
James Smart98c9ea52007-10-27 13:37:33 -04003328 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3329 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003330 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003331
3332 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003333 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003334 status = readl(phba->HCregaddr);
3335 status &= ~HC_ERINT_ENA;
3336 writel(status, phba->HCregaddr);
3337 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003338 phba->link_flag |= LS_IGNORE_ERATT;
3339 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003340
3341 lpfc_kill_board(phba, pmb);
3342 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3343 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3344
3345 if (retval != MBX_SUCCESS) {
3346 if (retval != MBX_BUSY)
3347 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003348 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3349 "2752 KILL_BOARD command failed retval %d\n",
3350 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003351 spin_lock_irq(&phba->hbalock);
3352 phba->link_flag &= ~LS_IGNORE_ERATT;
3353 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003354 return 1;
3355 }
3356
James Smartf4b4c682009-05-22 14:53:12 -04003357 spin_lock_irq(&phba->hbalock);
3358 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3359 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003360
Jamie Wellnitz41415862006-02-28 19:25:27 -05003361 mempool_free(pmb, phba->mbox_mem_pool);
3362
3363 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3364 * attention every 100ms for 3 seconds. If we don't get ERATT after
3365 * 3 seconds we still set HBA_ERROR state because the status of the
3366 * board is now undefined.
3367 */
3368 ha_copy = readl(phba->HAregaddr);
3369
3370 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3371 mdelay(100);
3372 ha_copy = readl(phba->HAregaddr);
3373 }
3374
3375 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003376 if (ha_copy & HA_ERATT) {
3377 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003378 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003379 }
James Smart2e0fef82007-06-17 19:56:36 -05003380 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003381 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003382 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003383 phba->link_flag &= ~LS_IGNORE_ERATT;
3384 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003385
Jamie Wellnitz41415862006-02-28 19:25:27 -05003386 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003387 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003388
James Smart2e0fef82007-06-17 19:56:36 -05003389 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003390}
3391
James Smarte59058c2008-08-24 21:49:00 -04003392/**
James Smart3772a992009-05-22 14:50:54 -04003393 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003394 * @phba: Pointer to HBA context object.
3395 *
3396 * This function resets the HBA by writing HC_INITFF to the control
3397 * register. After the HBA resets, this function resets all the iocb ring
3398 * indices. This function disables PCI layer parity checking during
3399 * the reset.
3400 * This function returns 0 always.
3401 * The caller is not required to hold any locks.
3402 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003403int
James Smart2e0fef82007-06-17 19:56:36 -05003404lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003405{
3406 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003407 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003408 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003409 int i;
dea31012005-04-17 16:05:31 -05003410
Jamie Wellnitz41415862006-02-28 19:25:27 -05003411 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003412
Jamie Wellnitz41415862006-02-28 19:25:27 -05003413 /* Reset HBA */
3414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003415 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003416 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003417
3418 /* perform board reset */
3419 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003420 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003421 phba->pport->fc_myDID = 0;
3422 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003423
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424 /* Turn off parity checking and serr during the physical reset */
3425 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3426 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3427 (cfg_value &
3428 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3429
James Smart3772a992009-05-22 14:50:54 -04003430 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3431
Jamie Wellnitz41415862006-02-28 19:25:27 -05003432 /* Now toggle INITFF bit in the Host Control Register */
3433 writel(HC_INITFF, phba->HCregaddr);
3434 mdelay(1);
3435 readl(phba->HCregaddr); /* flush */
3436 writel(0, phba->HCregaddr);
3437 readl(phba->HCregaddr); /* flush */
3438
3439 /* Restore PCI cmd register */
3440 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003441
3442 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003443 for (i = 0; i < psli->num_rings; i++) {
3444 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003445 pring->flag = 0;
3446 pring->rspidx = 0;
3447 pring->next_cmdidx = 0;
3448 pring->local_getidx = 0;
3449 pring->cmdidx = 0;
3450 pring->missbufcnt = 0;
3451 }
dea31012005-04-17 16:05:31 -05003452
James Smart2e0fef82007-06-17 19:56:36 -05003453 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003454 return 0;
3455}
3456
James Smarte59058c2008-08-24 21:49:00 -04003457/**
James Smartda0436e2009-05-22 14:51:39 -04003458 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3459 * @phba: Pointer to HBA context object.
3460 *
3461 * This function resets a SLI4 HBA. This function disables PCI layer parity
3462 * checking during resets the device. The caller is not required to hold
3463 * any locks.
3464 *
3465 * This function returns 0 always.
3466 **/
3467int
3468lpfc_sli4_brdreset(struct lpfc_hba *phba)
3469{
3470 struct lpfc_sli *psli = &phba->sli;
3471 uint16_t cfg_value;
3472 uint8_t qindx;
3473
3474 /* Reset HBA */
3475 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3476 "0295 Reset HBA Data: x%x x%x\n",
3477 phba->pport->port_state, psli->sli_flag);
3478
3479 /* perform board reset */
3480 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003481 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003482 phba->pport->fc_myDID = 0;
3483 phba->pport->fc_prevDID = 0;
3484
3485 /* Turn off parity checking and serr during the physical reset */
3486 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3487 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3488 (cfg_value &
3489 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3490
3491 spin_lock_irq(&phba->hbalock);
3492 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3493 phba->fcf.fcf_flag = 0;
3494 /* Clean up the child queue list for the CQs */
3495 list_del_init(&phba->sli4_hba.mbx_wq->list);
3496 list_del_init(&phba->sli4_hba.els_wq->list);
3497 list_del_init(&phba->sli4_hba.hdr_rq->list);
3498 list_del_init(&phba->sli4_hba.dat_rq->list);
3499 list_del_init(&phba->sli4_hba.mbx_cq->list);
3500 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003501 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3502 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3503 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3504 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3505 spin_unlock_irq(&phba->hbalock);
3506
3507 /* Now physically reset the device */
3508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3509 "0389 Performing PCI function reset!\n");
3510 /* Perform FCoE PCI function reset */
3511 lpfc_pci_function_reset(phba);
3512
3513 return 0;
3514}
3515
3516/**
3517 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003518 * @phba: Pointer to HBA context object.
3519 *
3520 * This function is called in the SLI initialization code path to
3521 * restart the HBA. The caller is not required to hold any lock.
3522 * This function writes MBX_RESTART mailbox command to the SLIM and
3523 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3524 * function to free any pending commands. The function enables
3525 * POST only during the first initialization. The function returns zero.
3526 * The function does not guarantee completion of MBX_RESTART mailbox
3527 * command before the return of this function.
3528 **/
James Smartda0436e2009-05-22 14:51:39 -04003529static int
3530lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003531{
3532 MAILBOX_t *mb;
3533 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003534 volatile uint32_t word0;
3535 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003536 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003537
James Smart2e0fef82007-06-17 19:56:36 -05003538 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003539
James Smart0d878412009-10-02 15:16:56 -04003540 /* Take PCIe device Advanced Error Reporting (AER) state */
3541 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3542
Jamie Wellnitz41415862006-02-28 19:25:27 -05003543 psli = &phba->sli;
3544
3545 /* Restart HBA */
3546 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003547 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003548 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003549
3550 word0 = 0;
3551 mb = (MAILBOX_t *) &word0;
3552 mb->mbxCommand = MBX_RESTART;
3553 mb->mbxHc = 1;
3554
James Smart92908312006-03-07 15:04:13 -05003555 lpfc_reset_barrier(phba);
3556
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 to_slim = phba->MBslimaddr;
3558 writel(*(uint32_t *) mb, to_slim);
3559 readl(to_slim); /* flush */
3560
3561 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003562 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003563 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003564 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003565 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003566 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003567 writel(*(uint32_t *) mb, to_slim);
3568 readl(to_slim); /* flush */
3569
3570 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003571 phba->pport->stopped = 0;
3572 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003573 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003574 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003575
James Smart64ba8812006-08-02 15:24:34 -04003576 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3577 psli->stats_start = get_seconds();
3578
James Smarteaf15d52008-12-04 22:39:29 -05003579 /* Give the INITFF and Post time to settle. */
3580 mdelay(100);
dea31012005-04-17 16:05:31 -05003581
James Smart0d878412009-10-02 15:16:56 -04003582 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3583 if (hba_aer_enabled)
3584 pci_disable_pcie_error_reporting(phba->pcidev);
3585
Jamie Wellnitz41415862006-02-28 19:25:27 -05003586 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003587
3588 return 0;
3589}
3590
James Smarte59058c2008-08-24 21:49:00 -04003591/**
James Smartda0436e2009-05-22 14:51:39 -04003592 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3593 * @phba: Pointer to HBA context object.
3594 *
3595 * This function is called in the SLI initialization code path to restart
3596 * a SLI4 HBA. The caller is not required to hold any lock.
3597 * At the end of the function, it calls lpfc_hba_down_post function to
3598 * free any pending commands.
3599 **/
3600static int
3601lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3602{
3603 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003604 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003605
3606 /* Restart HBA */
3607 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3608 "0296 Restart HBA Data: x%x x%x\n",
3609 phba->pport->port_state, psli->sli_flag);
3610
James Smart75baf692010-06-08 18:31:21 -04003611 /* Take PCIe device Advanced Error Reporting (AER) state */
3612 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3613
James Smartda0436e2009-05-22 14:51:39 -04003614 lpfc_sli4_brdreset(phba);
3615
3616 spin_lock_irq(&phba->hbalock);
3617 phba->pport->stopped = 0;
3618 phba->link_state = LPFC_INIT_START;
3619 phba->hba_flag = 0;
3620 spin_unlock_irq(&phba->hbalock);
3621
3622 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3623 psli->stats_start = get_seconds();
3624
James Smart75baf692010-06-08 18:31:21 -04003625 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3626 if (hba_aer_enabled)
3627 pci_disable_pcie_error_reporting(phba->pcidev);
3628
James Smartda0436e2009-05-22 14:51:39 -04003629 lpfc_hba_down_post(phba);
3630
3631 return 0;
3632}
3633
3634/**
3635 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3636 * @phba: Pointer to HBA context object.
3637 *
3638 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3639 * API jump table function pointer from the lpfc_hba struct.
3640**/
3641int
3642lpfc_sli_brdrestart(struct lpfc_hba *phba)
3643{
3644 return phba->lpfc_sli_brdrestart(phba);
3645}
3646
3647/**
James Smart3621a712009-04-06 18:47:14 -04003648 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003649 * @phba: Pointer to HBA context object.
3650 *
3651 * This function is called after a HBA restart to wait for successful
3652 * restart of the HBA. Successful restart of the HBA is indicated by
3653 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3654 * iteration, the function will restart the HBA again. The function returns
3655 * zero if HBA successfully restarted else returns negative error code.
3656 **/
dea31012005-04-17 16:05:31 -05003657static int
3658lpfc_sli_chipset_init(struct lpfc_hba *phba)
3659{
3660 uint32_t status, i = 0;
3661
3662 /* Read the HBA Host Status Register */
3663 status = readl(phba->HSregaddr);
3664
3665 /* Check status register to see what current state is */
3666 i = 0;
3667 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3668
James Smartdcf2a4e2010-09-29 11:18:53 -04003669 /* Check every 10ms for 10 retries, then every 100ms for 90
3670 * retries, then every 1 sec for 50 retires for a total of
3671 * ~60 seconds before reset the board again and check every
3672 * 1 sec for 50 retries. The up to 60 seconds before the
3673 * board ready is required by the Falcon FIPS zeroization
3674 * complete, and any reset the board in between shall cause
3675 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05003676 */
James Smartdcf2a4e2010-09-29 11:18:53 -04003677 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05003678 /* Adapter failed to init, timeout, status reg
3679 <status> */
James Smarted957682007-06-17 19:56:37 -05003680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003681 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003682 "timeout, status reg x%x, "
3683 "FW Data: A8 x%x AC x%x\n", status,
3684 readl(phba->MBslimaddr + 0xa8),
3685 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003686 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003687 return -ETIMEDOUT;
3688 }
3689
3690 /* Check to see if any errors occurred during init */
3691 if (status & HS_FFERM) {
3692 /* ERROR: During chipset initialization */
3693 /* Adapter failed to init, chipset, status reg
3694 <status> */
James Smarted957682007-06-17 19:56:37 -05003695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003696 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003697 "chipset, status reg x%x, "
3698 "FW Data: A8 x%x AC x%x\n", status,
3699 readl(phba->MBslimaddr + 0xa8),
3700 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003701 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003702 return -EIO;
3703 }
3704
James Smartdcf2a4e2010-09-29 11:18:53 -04003705 if (i <= 10)
dea31012005-04-17 16:05:31 -05003706 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04003707 else if (i <= 100)
3708 msleep(100);
3709 else
3710 msleep(1000);
dea31012005-04-17 16:05:31 -05003711
James Smartdcf2a4e2010-09-29 11:18:53 -04003712 if (i == 150) {
3713 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003714 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003715 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003716 }
3717 /* Read the HBA Host Status Register */
3718 status = readl(phba->HSregaddr);
3719 }
3720
3721 /* Check to see if any errors occurred during init */
3722 if (status & HS_FFERM) {
3723 /* ERROR: During chipset initialization */
3724 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003726 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003727 "status reg x%x, "
3728 "FW Data: A8 x%x AC x%x\n", status,
3729 readl(phba->MBslimaddr + 0xa8),
3730 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003731 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003732 return -EIO;
3733 }
3734
3735 /* Clear all interrupt enable conditions */
3736 writel(0, phba->HCregaddr);
3737 readl(phba->HCregaddr); /* flush */
3738
3739 /* setup host attn register */
3740 writel(0xffffffff, phba->HAregaddr);
3741 readl(phba->HAregaddr); /* flush */
3742 return 0;
3743}
3744
James Smarte59058c2008-08-24 21:49:00 -04003745/**
James Smart3621a712009-04-06 18:47:14 -04003746 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003747 *
3748 * This function calculates and returns the number of HBQs required to be
3749 * configured.
3750 **/
James Smart78b2d852007-08-02 11:10:21 -04003751int
James Smarted957682007-06-17 19:56:37 -05003752lpfc_sli_hbq_count(void)
3753{
James Smart92d7f7b2007-06-17 19:56:38 -05003754 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003755}
3756
James Smarte59058c2008-08-24 21:49:00 -04003757/**
James Smart3621a712009-04-06 18:47:14 -04003758 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003759 *
3760 * This function adds the number of hbq entries in every HBQ to get
3761 * the total number of hbq entries required for the HBA and returns
3762 * the total count.
3763 **/
James Smarted957682007-06-17 19:56:37 -05003764static int
3765lpfc_sli_hbq_entry_count(void)
3766{
3767 int hbq_count = lpfc_sli_hbq_count();
3768 int count = 0;
3769 int i;
3770
3771 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003772 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003773 return count;
3774}
3775
James Smarte59058c2008-08-24 21:49:00 -04003776/**
James Smart3621a712009-04-06 18:47:14 -04003777 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003778 *
3779 * This function calculates amount of memory required for all hbq entries
3780 * to be configured and returns the total memory required.
3781 **/
dea31012005-04-17 16:05:31 -05003782int
James Smarted957682007-06-17 19:56:37 -05003783lpfc_sli_hbq_size(void)
3784{
3785 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3786}
3787
James Smarte59058c2008-08-24 21:49:00 -04003788/**
James Smart3621a712009-04-06 18:47:14 -04003789 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003790 * @phba: Pointer to HBA context object.
3791 *
3792 * This function is called during the SLI initialization to configure
3793 * all the HBQs and post buffers to the HBQ. The caller is not
3794 * required to hold any locks. This function will return zero if successful
3795 * else it will return negative error code.
3796 **/
James Smarted957682007-06-17 19:56:37 -05003797static int
3798lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3799{
3800 int hbq_count = lpfc_sli_hbq_count();
3801 LPFC_MBOXQ_t *pmb;
3802 MAILBOX_t *pmbox;
3803 uint32_t hbqno;
3804 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003805
James Smart92d7f7b2007-06-17 19:56:38 -05003806 /* Get a Mailbox buffer to setup mailbox
3807 * commands for HBA initialization
3808 */
James Smarted957682007-06-17 19:56:37 -05003809 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3810
3811 if (!pmb)
3812 return -ENOMEM;
3813
James Smart04c68492009-05-22 14:52:52 -04003814 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003815
3816 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3817 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003818 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003819
3820 hbq_entry_index = 0;
3821 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3822 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3823 phba->hbqs[hbqno].hbqPutIdx = 0;
3824 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3825 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003826 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003827 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3828 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003829 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3830
3831 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3832 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3833 mbxStatus <status>, ring <num> */
3834
3835 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003836 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003837 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003838 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003839 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003840 pmbox->mbxStatus, hbqno);
3841
3842 phba->link_state = LPFC_HBA_ERROR;
3843 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04003844 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003845 }
3846 }
3847 phba->hbq_count = hbq_count;
3848
James Smarted957682007-06-17 19:56:37 -05003849 mempool_free(pmb, phba->mbox_mem_pool);
3850
James Smart92d7f7b2007-06-17 19:56:38 -05003851 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003852 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3853 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003854 return 0;
3855}
3856
James Smarte59058c2008-08-24 21:49:00 -04003857/**
James Smart4f774512009-05-22 14:52:35 -04003858 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3859 * @phba: Pointer to HBA context object.
3860 *
3861 * This function is called during the SLI initialization to configure
3862 * all the HBQs and post buffers to the HBQ. The caller is not
3863 * required to hold any locks. This function will return zero if successful
3864 * else it will return negative error code.
3865 **/
3866static int
3867lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3868{
3869 phba->hbq_in_use = 1;
3870 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3871 phba->hbq_count = 1;
3872 /* Initially populate or replenish the HBQs */
3873 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3874 return 0;
3875}
3876
3877/**
James Smart3621a712009-04-06 18:47:14 -04003878 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003879 * @phba: Pointer to HBA context object.
3880 * @sli_mode: sli mode - 2/3
3881 *
3882 * This function is called by the sli intialization code path
3883 * to issue config_port mailbox command. This function restarts the
3884 * HBA firmware and issues a config_port mailbox command to configure
3885 * the SLI interface in the sli mode specified by sli_mode
3886 * variable. The caller is not required to hold any locks.
3887 * The function returns 0 if successful, else returns negative error
3888 * code.
3889 **/
James Smart93996272008-08-24 21:50:30 -04003890int
3891lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003892{
3893 LPFC_MBOXQ_t *pmb;
3894 uint32_t resetcount = 0, rc = 0, done = 0;
3895
3896 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3897 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003898 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003899 return -ENOMEM;
3900 }
3901
James Smarted957682007-06-17 19:56:37 -05003902 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003903 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003904 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003905 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003906 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003907 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003909 rc = lpfc_sli_chipset_init(phba);
3910 if (rc)
3911 break;
3912
James Smart2e0fef82007-06-17 19:56:36 -05003913 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003914 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003915 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003916 resetcount++;
3917
James Smarted957682007-06-17 19:56:37 -05003918 /* Call pre CONFIG_PORT mailbox command initialization. A
3919 * value of 0 means the call was successful. Any other
3920 * nonzero value is a failure, but if ERESTART is returned,
3921 * the driver may reset the HBA and try again.
3922 */
dea31012005-04-17 16:05:31 -05003923 rc = lpfc_config_port_prep(phba);
3924 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003925 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003926 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003927 } else if (rc)
dea31012005-04-17 16:05:31 -05003928 break;
James Smart2e0fef82007-06-17 19:56:36 -05003929 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003930 lpfc_config_port(phba, pmb);
3931 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003932 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3933 LPFC_SLI3_HBQ_ENABLED |
3934 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04003935 LPFC_SLI3_BG_ENABLED |
3936 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003937 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003939 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003940 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003941 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003942 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003943 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003944 spin_unlock_irq(&phba->hbalock);
3945 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003946 } else {
3947 /* Allow asynchronous mailbox command to go through */
3948 spin_lock_irq(&phba->hbalock);
3949 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3950 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003951 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003952 }
dea31012005-04-17 16:05:31 -05003953 }
James Smarted957682007-06-17 19:56:37 -05003954 if (!done) {
3955 rc = -EINVAL;
3956 goto do_prep_failed;
3957 }
James Smart04c68492009-05-22 14:52:52 -04003958 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3959 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003960 rc = -ENXIO;
3961 goto do_prep_failed;
3962 }
James Smart04c68492009-05-22 14:52:52 -04003963 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003964 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003965 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3966 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3967 phba->max_vpi : phba->max_vports;
3968
James Smart34b02dc2008-08-24 21:49:55 -04003969 } else
3970 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04003971 phba->fips_level = 0;
3972 phba->fips_spec_rev = 0;
3973 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04003974 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04003975 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
3976 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
3977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3978 "2850 Security Crypto Active. FIPS x%d "
3979 "(Spec Rev: x%d)",
3980 phba->fips_level, phba->fips_spec_rev);
3981 }
3982 if (pmb->u.mb.un.varCfgPort.sec_err) {
3983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3984 "2856 Config Port Security Crypto "
3985 "Error: x%x ",
3986 pmb->u.mb.un.varCfgPort.sec_err);
3987 }
James Smart04c68492009-05-22 14:52:52 -04003988 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003989 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003990 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003991 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04003992
3993 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3994 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05003995
3996 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003997 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003998 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3999 else
4000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4001 "0443 Adapter did not grant "
4002 "BlockGuard\n");
4003 }
James Smart34b02dc2008-08-24 21:49:55 -04004004 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004005 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004006 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004007 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004008 }
James Smart92d7f7b2007-06-17 19:56:38 -05004009do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004010 mempool_free(pmb, phba->mbox_mem_pool);
4011 return rc;
4012}
4013
James Smarte59058c2008-08-24 21:49:00 -04004014
4015/**
James Smart3621a712009-04-06 18:47:14 -04004016 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004017 * @phba: Pointer to HBA context object.
4018 *
4019 * This function is the main SLI intialization function. This function
4020 * is called by the HBA intialization code, HBA reset code and HBA
4021 * error attention handler code. Caller is not required to hold any
4022 * locks. This function issues config_port mailbox command to configure
4023 * the SLI, setup iocb rings and HBQ rings. In the end the function
4024 * calls the config_port_post function to issue init_link mailbox
4025 * command and to start the discovery. The function will return zero
4026 * if successful, else it will return negative error code.
4027 **/
James Smarted957682007-06-17 19:56:37 -05004028int
4029lpfc_sli_hba_setup(struct lpfc_hba *phba)
4030{
4031 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004032 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004033
4034 switch (lpfc_sli_mode) {
4035 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004036 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004038 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004039 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004040 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004041 break;
4042 }
James Smarted957682007-06-17 19:56:37 -05004043 mode = 2;
4044 break;
4045 case 0:
4046 case 3:
4047 break;
4048 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004050 "1819 Unrecognized lpfc_sli_mode "
4051 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004052
4053 break;
4054 }
4055
James Smart93996272008-08-24 21:50:30 -04004056 rc = lpfc_sli_config_port(phba, mode);
4057
James Smarted957682007-06-17 19:56:37 -05004058 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004060 "1820 Unable to select SLI-3. "
4061 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004062 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004063 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004064 if (rc)
dea31012005-04-17 16:05:31 -05004065 goto lpfc_sli_hba_setup_error;
4066
James Smart0d878412009-10-02 15:16:56 -04004067 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4068 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4069 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4070 if (!rc) {
4071 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4072 "2709 This device supports "
4073 "Advanced Error Reporting (AER)\n");
4074 spin_lock_irq(&phba->hbalock);
4075 phba->hba_flag |= HBA_AER_ENABLED;
4076 spin_unlock_irq(&phba->hbalock);
4077 } else {
4078 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4079 "2708 This device does not support "
4080 "Advanced Error Reporting (AER)\n");
4081 phba->cfg_aer_support = 0;
4082 }
4083 }
4084
James Smarted957682007-06-17 19:56:37 -05004085 if (phba->sli_rev == 3) {
4086 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4087 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004088 } else {
4089 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4090 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004091 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004092 }
4093
4094 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004095 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4096 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004097 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004098
4099 if (rc)
4100 goto lpfc_sli_hba_setup_error;
4101
James Smart93996272008-08-24 21:50:30 -04004102 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004103 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4104 rc = lpfc_sli_hbq_setup(phba);
4105 if (rc)
4106 goto lpfc_sli_hba_setup_error;
4107 }
James Smart04c68492009-05-22 14:52:52 -04004108 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004109 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004110 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004111
4112 rc = lpfc_config_port_post(phba);
4113 if (rc)
4114 goto lpfc_sli_hba_setup_error;
4115
James Smarted957682007-06-17 19:56:37 -05004116 return rc;
4117
James Smart92d7f7b2007-06-17 19:56:38 -05004118lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004119 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004121 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004122 return rc;
4123}
4124
James Smartda0436e2009-05-22 14:51:39 -04004125/**
4126 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4127 * @phba: Pointer to HBA context object.
4128 * @mboxq: mailbox pointer.
4129 * This function issue a dump mailbox command to read config region
4130 * 23 and parse the records in the region and populate driver
4131 * data structure.
4132 **/
4133static int
4134lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4135 LPFC_MBOXQ_t *mboxq)
4136{
4137 struct lpfc_dmabuf *mp;
4138 struct lpfc_mqe *mqe;
4139 uint32_t data_length;
4140 int rc;
4141
4142 /* Program the default value of vlan_id and fc_map */
4143 phba->valid_vlan = 0;
4144 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4145 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4146 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4147
4148 mqe = &mboxq->u.mqe;
4149 if (lpfc_dump_fcoe_param(phba, mboxq))
4150 return -ENOMEM;
4151
4152 mp = (struct lpfc_dmabuf *) mboxq->context1;
4153 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4154
4155 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4156 "(%d):2571 Mailbox cmd x%x Status x%x "
4157 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4158 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4159 "CQ: x%x x%x x%x x%x\n",
4160 mboxq->vport ? mboxq->vport->vpi : 0,
4161 bf_get(lpfc_mqe_command, mqe),
4162 bf_get(lpfc_mqe_status, mqe),
4163 mqe->un.mb_words[0], mqe->un.mb_words[1],
4164 mqe->un.mb_words[2], mqe->un.mb_words[3],
4165 mqe->un.mb_words[4], mqe->un.mb_words[5],
4166 mqe->un.mb_words[6], mqe->un.mb_words[7],
4167 mqe->un.mb_words[8], mqe->un.mb_words[9],
4168 mqe->un.mb_words[10], mqe->un.mb_words[11],
4169 mqe->un.mb_words[12], mqe->un.mb_words[13],
4170 mqe->un.mb_words[14], mqe->un.mb_words[15],
4171 mqe->un.mb_words[16], mqe->un.mb_words[50],
4172 mboxq->mcqe.word0,
4173 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4174 mboxq->mcqe.trailer);
4175
4176 if (rc) {
4177 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4178 kfree(mp);
4179 return -EIO;
4180 }
4181 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004182 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004183 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4184 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004185 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004186 }
James Smartda0436e2009-05-22 14:51:39 -04004187
4188 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4189 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4190 kfree(mp);
4191 return 0;
4192}
4193
4194/**
4195 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4196 * @phba: pointer to lpfc hba data structure.
4197 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4198 * @vpd: pointer to the memory to hold resulting port vpd data.
4199 * @vpd_size: On input, the number of bytes allocated to @vpd.
4200 * On output, the number of data bytes in @vpd.
4201 *
4202 * This routine executes a READ_REV SLI4 mailbox command. In
4203 * addition, this routine gets the port vpd data.
4204 *
4205 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004206 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004207 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004208 **/
4209static int
4210lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4211 uint8_t *vpd, uint32_t *vpd_size)
4212{
4213 int rc = 0;
4214 uint32_t dma_size;
4215 struct lpfc_dmabuf *dmabuf;
4216 struct lpfc_mqe *mqe;
4217
4218 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4219 if (!dmabuf)
4220 return -ENOMEM;
4221
4222 /*
4223 * Get a DMA buffer for the vpd data resulting from the READ_REV
4224 * mailbox command.
4225 */
4226 dma_size = *vpd_size;
4227 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4228 dma_size,
4229 &dmabuf->phys,
4230 GFP_KERNEL);
4231 if (!dmabuf->virt) {
4232 kfree(dmabuf);
4233 return -ENOMEM;
4234 }
4235 memset(dmabuf->virt, 0, dma_size);
4236
4237 /*
4238 * The SLI4 implementation of READ_REV conflicts at word1,
4239 * bits 31:16 and SLI4 adds vpd functionality not present
4240 * in SLI3. This code corrects the conflicts.
4241 */
4242 lpfc_read_rev(phba, mboxq);
4243 mqe = &mboxq->u.mqe;
4244 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4245 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4246 mqe->un.read_rev.word1 &= 0x0000FFFF;
4247 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4248 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4249
4250 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4251 if (rc) {
4252 dma_free_coherent(&phba->pcidev->dev, dma_size,
4253 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004254 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004255 return -EIO;
4256 }
4257
James Smartda0436e2009-05-22 14:51:39 -04004258 /*
4259 * The available vpd length cannot be bigger than the
4260 * DMA buffer passed to the port. Catch the less than
4261 * case and update the caller's size.
4262 */
4263 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4264 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4265
James Smartd7c47992010-06-08 18:31:54 -04004266 memcpy(vpd, dmabuf->virt, *vpd_size);
4267
James Smartda0436e2009-05-22 14:51:39 -04004268 dma_free_coherent(&phba->pcidev->dev, dma_size,
4269 dmabuf->virt, dmabuf->phys);
4270 kfree(dmabuf);
4271 return 0;
4272}
4273
4274/**
4275 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4276 * @phba: pointer to lpfc hba data structure.
4277 *
4278 * This routine is called to explicitly arm the SLI4 device's completion and
4279 * event queues
4280 **/
4281static void
4282lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4283{
4284 uint8_t fcp_eqidx;
4285
4286 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4287 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004288 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4289 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4290 LPFC_QUEUE_REARM);
4291 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4292 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4293 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4294 LPFC_QUEUE_REARM);
4295}
4296
4297/**
4298 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4299 * @phba: Pointer to HBA context object.
4300 *
4301 * This function is the main SLI4 device intialization PCI function. This
4302 * function is called by the HBA intialization code, HBA reset code and
4303 * HBA error attention handler code. Caller is not required to hold any
4304 * locks.
4305 **/
4306int
4307lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4308{
4309 int rc;
4310 LPFC_MBOXQ_t *mboxq;
4311 struct lpfc_mqe *mqe;
4312 uint8_t *vpd;
4313 uint32_t vpd_size;
4314 uint32_t ftr_rsp = 0;
4315 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4316 struct lpfc_vport *vport = phba->pport;
4317 struct lpfc_dmabuf *mp;
4318
4319 /* Perform a PCI function reset to start from clean */
4320 rc = lpfc_pci_function_reset(phba);
4321 if (unlikely(rc))
4322 return -ENODEV;
4323
4324 /* Check the HBA Host Status Register for readyness */
4325 rc = lpfc_sli4_post_status_check(phba);
4326 if (unlikely(rc))
4327 return -ENODEV;
4328 else {
4329 spin_lock_irq(&phba->hbalock);
4330 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4331 spin_unlock_irq(&phba->hbalock);
4332 }
4333
4334 /*
4335 * Allocate a single mailbox container for initializing the
4336 * port.
4337 */
4338 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4339 if (!mboxq)
4340 return -ENOMEM;
4341
4342 /*
4343 * Continue initialization with default values even if driver failed
4344 * to read FCoE param config regions
4345 */
4346 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004348 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004349
4350 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004351 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004352 vpd = kzalloc(vpd_size, GFP_KERNEL);
4353 if (!vpd) {
4354 rc = -ENOMEM;
4355 goto out_free_mbox;
4356 }
4357
4358 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05004359 if (unlikely(rc)) {
4360 kfree(vpd);
4361 goto out_free_mbox;
4362 }
James Smartda0436e2009-05-22 14:51:39 -04004363 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004364 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4365 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05004366 phba->hba_flag |= HBA_FCOE_MODE;
4367 else
4368 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04004369
4370 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4371 LPFC_DCBX_CEE_MODE)
4372 phba->hba_flag |= HBA_FIP_SUPPORT;
4373 else
4374 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4375
James Smartf1126682009-06-10 17:22:44 -04004376 if (phba->sli_rev != LPFC_SLI_REV4 ||
James Smart76a95d72010-11-20 23:11:48 -05004377 !(phba->hba_flag & HBA_FCOE_MODE)) {
James Smartda0436e2009-05-22 14:51:39 -04004378 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4379 "0376 READ_REV Error. SLI Level %d "
4380 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05004381 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04004382 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004383 kfree(vpd);
4384 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004385 }
James Smartda0436e2009-05-22 14:51:39 -04004386 /*
4387 * Evaluate the read rev and vpd data. Populate the driver
4388 * state with the results. If this routine fails, the failure
4389 * is not fatal as the driver will use generic values.
4390 */
4391 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4392 if (unlikely(!rc)) {
4393 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4394 "0377 Error %d parsing vpd. "
4395 "Using defaults.\n", rc);
4396 rc = 0;
4397 }
James Smart76a95d72010-11-20 23:11:48 -05004398 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04004399
James Smartf1126682009-06-10 17:22:44 -04004400 /* Save information as VPD data */
4401 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4402 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4403 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4404 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4405 &mqe->un.read_rev);
4406 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4407 &mqe->un.read_rev);
4408 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4409 &mqe->un.read_rev);
4410 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4411 &mqe->un.read_rev);
4412 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4413 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4414 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4415 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4416 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4417 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4418 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4419 "(%d):0380 READ_REV Status x%x "
4420 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4421 mboxq->vport ? mboxq->vport->vpi : 0,
4422 bf_get(lpfc_mqe_status, mqe),
4423 phba->vpd.rev.opFwName,
4424 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4425 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004426
4427 /*
4428 * Discover the port's supported feature set and match it against the
4429 * hosts requests.
4430 */
4431 lpfc_request_features(phba, mboxq);
4432 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4433 if (unlikely(rc)) {
4434 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004435 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004436 }
4437
4438 /*
4439 * The port must support FCP initiator mode as this is the
4440 * only mode running in the host.
4441 */
4442 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4443 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4444 "0378 No support for fcpi mode.\n");
4445 ftr_rsp++;
4446 }
4447
4448 /*
4449 * If the port cannot support the host's requested features
4450 * then turn off the global config parameters to disable the
4451 * feature in the driver. This is not a fatal error.
4452 */
4453 if ((phba->cfg_enable_bg) &&
4454 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4455 ftr_rsp++;
4456
4457 if (phba->max_vpi && phba->cfg_enable_npiv &&
4458 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4459 ftr_rsp++;
4460
4461 if (ftr_rsp) {
4462 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4463 "0379 Feature Mismatch Data: x%08x %08x "
4464 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4465 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4466 phba->cfg_enable_npiv, phba->max_vpi);
4467 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4468 phba->cfg_enable_bg = 0;
4469 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4470 phba->cfg_enable_npiv = 0;
4471 }
4472
4473 /* These SLI3 features are assumed in SLI4 */
4474 spin_lock_irq(&phba->hbalock);
4475 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4476 spin_unlock_irq(&phba->hbalock);
4477
4478 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004479 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4480 if (rc) {
4481 phba->link_state = LPFC_HBA_ERROR;
4482 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05004483 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05004484 }
4485
James Smartda0436e2009-05-22 14:51:39 -04004486 mboxq->vport = vport;
4487 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4488 mp = (struct lpfc_dmabuf *) mboxq->context1;
4489 if (rc == MBX_SUCCESS) {
4490 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4491 rc = 0;
4492 }
4493
4494 /*
4495 * This memory was allocated by the lpfc_read_sparam routine. Release
4496 * it to the mbuf pool.
4497 */
4498 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4499 kfree(mp);
4500 mboxq->context1 = NULL;
4501 if (unlikely(rc)) {
4502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4503 "0382 READ_SPARAM command failed "
4504 "status %d, mbxStatus x%x\n",
4505 rc, bf_get(lpfc_mqe_status, mqe));
4506 phba->link_state = LPFC_HBA_ERROR;
4507 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004508 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004509 }
4510
4511 if (phba->cfg_soft_wwnn)
4512 u64_to_wwn(phba->cfg_soft_wwnn,
4513 vport->fc_sparam.nodeName.u.wwn);
4514 if (phba->cfg_soft_wwpn)
4515 u64_to_wwn(phba->cfg_soft_wwpn,
4516 vport->fc_sparam.portName.u.wwn);
4517 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4518 sizeof(struct lpfc_name));
4519 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4520 sizeof(struct lpfc_name));
4521
4522 /* Update the fc_host data structures with new wwn. */
4523 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4524 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4525
4526 /* Register SGL pool to the device using non-embedded mailbox command */
4527 rc = lpfc_sli4_post_sgl_list(phba);
4528 if (unlikely(rc)) {
4529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004530 "0582 Error %d during sgl post operation\n",
4531 rc);
James Smartda0436e2009-05-22 14:51:39 -04004532 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004533 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004534 }
4535
4536 /* Register SCSI SGL pool to the device */
4537 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4538 if (unlikely(rc)) {
4539 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004540 "0383 Error %d during scsi sgl post "
4541 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004542 /* Some Scsi buffers were moved to the abort scsi list */
4543 /* A pci function reset will repost them */
4544 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004545 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004546 }
4547
4548 /* Post the rpi header region to the device. */
4549 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4550 if (unlikely(rc)) {
4551 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4552 "0393 Error %d during rpi post operation\n",
4553 rc);
4554 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004555 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004556 }
James Smartda0436e2009-05-22 14:51:39 -04004557
4558 /* Set up all the queues to the device */
4559 rc = lpfc_sli4_queue_setup(phba);
4560 if (unlikely(rc)) {
4561 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4562 "0381 Error %d during queue setup.\n ", rc);
4563 goto out_stop_timers;
4564 }
4565
4566 /* Arm the CQs and then EQs on device */
4567 lpfc_sli4_arm_cqeq_intr(phba);
4568
4569 /* Indicate device interrupt mode */
4570 phba->sli4_hba.intr_enable = 1;
4571
4572 /* Allow asynchronous mailbox command to go through */
4573 spin_lock_irq(&phba->hbalock);
4574 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4575 spin_unlock_irq(&phba->hbalock);
4576
4577 /* Post receive buffers to the device */
4578 lpfc_sli4_rb_setup(phba);
4579
James Smartfc2b9892010-02-26 14:15:29 -05004580 /* Reset HBA FCF states after HBA reset */
4581 phba->fcf.fcf_flag = 0;
4582 phba->fcf.current_rec.flag = 0;
4583
James Smartda0436e2009-05-22 14:51:39 -04004584 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004585 mod_timer(&vport->els_tmofunc,
4586 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004587
4588 /* Start heart beat timer */
4589 mod_timer(&phba->hb_tmofunc,
4590 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4591 phba->hb_outstanding = 0;
4592 phba->last_completion_time = jiffies;
4593
4594 /* Start error attention (ERATT) polling timer */
4595 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4596
James Smart75baf692010-06-08 18:31:21 -04004597 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4598 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4599 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4600 if (!rc) {
4601 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4602 "2829 This device supports "
4603 "Advanced Error Reporting (AER)\n");
4604 spin_lock_irq(&phba->hbalock);
4605 phba->hba_flag |= HBA_AER_ENABLED;
4606 spin_unlock_irq(&phba->hbalock);
4607 } else {
4608 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4609 "2830 This device does not support "
4610 "Advanced Error Reporting (AER)\n");
4611 phba->cfg_aer_support = 0;
4612 }
4613 }
4614
James Smart76a95d72010-11-20 23:11:48 -05004615 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4616 /*
4617 * The FC Port needs to register FCFI (index 0)
4618 */
4619 lpfc_reg_fcfi(phba, mboxq);
4620 mboxq->vport = phba->pport;
4621 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4622 if (rc == MBX_SUCCESS)
4623 rc = 0;
4624 else
4625 goto out_unset_queue;
4626 }
James Smartda0436e2009-05-22 14:51:39 -04004627 /*
4628 * The port is ready, set the host's link state to LINK_DOWN
4629 * in preparation for link interrupts.
4630 */
James Smartda0436e2009-05-22 14:51:39 -04004631 spin_lock_irq(&phba->hbalock);
4632 phba->link_state = LPFC_LINK_DOWN;
4633 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05004634 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
4635out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04004636 /* Unset all the queues set up in this routine when error out */
4637 if (rc)
4638 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004639out_stop_timers:
4640 if (rc)
4641 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04004642out_free_mbox:
4643 mempool_free(mboxq, phba->mbox_mem_pool);
4644 return rc;
4645}
James Smarte59058c2008-08-24 21:49:00 -04004646
4647/**
James Smart3621a712009-04-06 18:47:14 -04004648 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004649 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004650 *
James Smarte59058c2008-08-24 21:49:00 -04004651 * This is the callback function for mailbox timer. The mailbox
4652 * timer is armed when a new mailbox command is issued and the timer
4653 * is deleted when the mailbox complete. The function is called by
4654 * the kernel timer code when a mailbox does not complete within
4655 * expected time. This function wakes up the worker thread to
4656 * process the mailbox timeout and returns. All the processing is
4657 * done by the worker thread function lpfc_mbox_timeout_handler.
4658 **/
dea31012005-04-17 16:05:31 -05004659void
4660lpfc_mbox_timeout(unsigned long ptr)
4661{
James Smart92d7f7b2007-06-17 19:56:38 -05004662 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004663 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004664 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004665
James Smart2e0fef82007-06-17 19:56:36 -05004666 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004667 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004668 if (!tmo_posted)
4669 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4670 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4671
James Smart5e9d9b82008-06-14 22:52:53 -04004672 if (!tmo_posted)
4673 lpfc_worker_wake_up(phba);
4674 return;
dea31012005-04-17 16:05:31 -05004675}
4676
James Smarte59058c2008-08-24 21:49:00 -04004677
4678/**
James Smart3621a712009-04-06 18:47:14 -04004679 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004680 * @phba: Pointer to HBA context object.
4681 *
4682 * This function is called from worker thread when a mailbox command times out.
4683 * The caller is not required to hold any locks. This function will reset the
4684 * HBA and recover all the pending commands.
4685 **/
dea31012005-04-17 16:05:31 -05004686void
4687lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4688{
James Smart2e0fef82007-06-17 19:56:36 -05004689 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004690 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004691 struct lpfc_sli *psli = &phba->sli;
4692 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004693
James Smarta257bf92009-04-06 18:48:10 -04004694 /* Check the pmbox pointer first. There is a race condition
4695 * between the mbox timeout handler getting executed in the
4696 * worklist and the mailbox actually completing. When this
4697 * race condition occurs, the mbox_active will be NULL.
4698 */
4699 spin_lock_irq(&phba->hbalock);
4700 if (pmbox == NULL) {
4701 lpfc_printf_log(phba, KERN_WARNING,
4702 LOG_MBOX | LOG_SLI,
4703 "0353 Active Mailbox cleared - mailbox timeout "
4704 "exiting\n");
4705 spin_unlock_irq(&phba->hbalock);
4706 return;
4707 }
4708
dea31012005-04-17 16:05:31 -05004709 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004711 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004712 mb->mbxCommand,
4713 phba->pport->port_state,
4714 phba->sli.sli_flag,
4715 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004716 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004717
James Smart1dcb58e2007-04-25 09:51:30 -04004718 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4719 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4720 * it to fail all oustanding SCSI IO.
4721 */
James Smart2e0fef82007-06-17 19:56:36 -05004722 spin_lock_irq(&phba->pport->work_port_lock);
4723 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4724 spin_unlock_irq(&phba->pport->work_port_lock);
4725 spin_lock_irq(&phba->hbalock);
4726 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004727 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004728 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004729
4730 pring = &psli->ring[psli->fcp_ring];
4731 lpfc_sli_abort_iocb_ring(phba, pring);
4732
4733 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004734 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004735
4736 /* Reset the HBA device */
4737 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004738}
4739
James Smarte59058c2008-08-24 21:49:00 -04004740/**
James Smart3772a992009-05-22 14:50:54 -04004741 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004742 * @phba: Pointer to HBA context object.
4743 * @pmbox: Pointer to mailbox object.
4744 * @flag: Flag indicating how the mailbox need to be processed.
4745 *
4746 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004747 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4748 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004749 * The mailbox command can be submitted in polling mode, in which case
4750 * this function will wait in a polling loop for the completion of the
4751 * mailbox.
4752 * If the mailbox is submitted in no_wait mode (not polling) the
4753 * function will submit the command and returns immediately without waiting
4754 * for the mailbox completion. The no_wait is supported only when HBA
4755 * is in SLI2/SLI3 mode - interrupts are enabled.
4756 * The SLI interface allows only one mailbox pending at a time. If the
4757 * mailbox is issued in polling mode and there is already a mailbox
4758 * pending, then the function will return an error. If the mailbox is issued
4759 * in NO_WAIT mode and there is a mailbox pending already, the function
4760 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4761 * The sli layer owns the mailbox object until the completion of mailbox
4762 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4763 * return codes the caller owns the mailbox command after the return of
4764 * the function.
4765 **/
James Smart3772a992009-05-22 14:50:54 -04004766static int
4767lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4768 uint32_t flag)
dea31012005-04-17 16:05:31 -05004769{
dea31012005-04-17 16:05:31 -05004770 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004771 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004772 uint32_t status, evtctr;
4773 uint32_t ha_copy;
4774 int i;
James Smart09372822008-01-11 01:52:54 -05004775 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004776 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004777 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004778 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004779 int processing_queue = 0;
4780
4781 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4782 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004783 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004784 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004785 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4787 return MBX_SUCCESS;
4788 }
James Smart58da1ff2008-04-07 10:15:56 -04004789 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004790 pmbox = lpfc_mbox_get(phba);
4791 if (!pmbox) {
4792 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4793 return MBX_SUCCESS;
4794 }
4795 }
dea31012005-04-17 16:05:31 -05004796
James Smarted957682007-06-17 19:56:37 -05004797 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004798 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004799 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004800 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004801 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004802 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004803 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004804 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004805 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004806 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004807 }
4808 }
4809
Linas Vepstas8d63f372007-02-14 14:28:36 -06004810 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004811 if (unlikely(pci_channel_offline(phba->pcidev))) {
4812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4813 goto out_not_finished;
4814 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004815
James Smarta257bf92009-04-06 18:48:10 -04004816 /* If HBA has a deferred error attention, fail the iocb. */
4817 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4818 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4819 goto out_not_finished;
4820 }
4821
dea31012005-04-17 16:05:31 -05004822 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004823
James Smart3772a992009-05-22 14:50:54 -04004824 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004825 status = MBX_SUCCESS;
4826
James Smart2e0fef82007-06-17 19:56:36 -05004827 if (phba->link_state == LPFC_HBA_ERROR) {
4828 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004829
4830 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004831 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4832 "(%d):0311 Mailbox command x%x cannot "
4833 "issue Data: x%x x%x\n",
4834 pmbox->vport ? pmbox->vport->vpi : 0,
4835 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004836 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004837 }
4838
James Smart92908312006-03-07 15:04:13 -05004839 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4840 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004841 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004842 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4843 "(%d):2528 Mailbox command x%x cannot "
4844 "issue Data: x%x x%x\n",
4845 pmbox->vport ? pmbox->vport->vpi : 0,
4846 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004847 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004848 }
4849
dea31012005-04-17 16:05:31 -05004850 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4851 /* Polling for a mbox command when another one is already active
4852 * is not allowed in SLI. Also, the driver must have established
4853 * SLI2 mode to queue and process multiple mbox commands.
4854 */
4855
4856 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004857 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004858
4859 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4861 "(%d):2529 Mailbox command x%x "
4862 "cannot issue Data: x%x x%x\n",
4863 pmbox->vport ? pmbox->vport->vpi : 0,
4864 pmbox->u.mb.mbxCommand,
4865 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004866 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004867 }
4868
James Smart3772a992009-05-22 14:50:54 -04004869 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004870 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004871 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4873 "(%d):2530 Mailbox command x%x "
4874 "cannot issue Data: x%x x%x\n",
4875 pmbox->vport ? pmbox->vport->vpi : 0,
4876 pmbox->u.mb.mbxCommand,
4877 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004878 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004879 }
4880
dea31012005-04-17 16:05:31 -05004881 /* Another mailbox command is still being processed, queue this
4882 * command to be processed later.
4883 */
4884 lpfc_mbox_put(phba, pmbox);
4885
4886 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004887 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004888 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004889 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004890 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4891 mb->mbxCommand, phba->pport->port_state,
4892 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004893
4894 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004895 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004896
James Smart858c9f62007-06-17 19:56:39 -05004897 if (pmbox->vport) {
4898 lpfc_debugfs_disc_trc(pmbox->vport,
4899 LPFC_DISC_TRC_MBOX_VPORT,
4900 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4901 (uint32_t)mb->mbxCommand,
4902 mb->un.varWords[0], mb->un.varWords[1]);
4903 }
4904 else {
4905 lpfc_debugfs_disc_trc(phba->pport,
4906 LPFC_DISC_TRC_MBOX,
4907 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4908 (uint32_t)mb->mbxCommand,
4909 mb->un.varWords[0], mb->un.varWords[1]);
4910 }
4911
James Smart2e0fef82007-06-17 19:56:36 -05004912 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004913 }
4914
dea31012005-04-17 16:05:31 -05004915 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4916
4917 /* If we are not polling, we MUST be in SLI2 mode */
4918 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004919 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004920 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004921 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004922 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004923 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004924 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4925 "(%d):2531 Mailbox command x%x "
4926 "cannot issue Data: x%x x%x\n",
4927 pmbox->vport ? pmbox->vport->vpi : 0,
4928 pmbox->u.mb.mbxCommand,
4929 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004930 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004931 }
4932 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004933 mod_timer(&psli->mbox_tmo, (jiffies +
4934 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004935 }
4936
4937 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004938 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004939 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004940 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004941 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004942 mb->mbxCommand, phba->pport->port_state,
4943 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004944
James Smart858c9f62007-06-17 19:56:39 -05004945 if (mb->mbxCommand != MBX_HEARTBEAT) {
4946 if (pmbox->vport) {
4947 lpfc_debugfs_disc_trc(pmbox->vport,
4948 LPFC_DISC_TRC_MBOX_VPORT,
4949 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4950 (uint32_t)mb->mbxCommand,
4951 mb->un.varWords[0], mb->un.varWords[1]);
4952 }
4953 else {
4954 lpfc_debugfs_disc_trc(phba->pport,
4955 LPFC_DISC_TRC_MBOX,
4956 "MBOX Send: cmd:x%x mb:x%x x%x",
4957 (uint32_t)mb->mbxCommand,
4958 mb->un.varWords[0], mb->un.varWords[1]);
4959 }
4960 }
4961
dea31012005-04-17 16:05:31 -05004962 psli->slistat.mbox_cmd++;
4963 evtctr = psli->slistat.mbox_event;
4964
4965 /* next set own bit for the adapter and copy over command word */
4966 mb->mbxOwner = OWN_CHIP;
4967
James Smart3772a992009-05-22 14:50:54 -04004968 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004969 /* Populate mbox extension offset word. */
4970 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4971 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4972 = (uint8_t *)phba->mbox_ext
4973 - (uint8_t *)phba->mbox;
4974 }
4975
4976 /* Copy the mailbox extension data */
4977 if (pmbox->in_ext_byte_len && pmbox->context2) {
4978 lpfc_sli_pcimem_bcopy(pmbox->context2,
4979 (uint8_t *)phba->mbox_ext,
4980 pmbox->in_ext_byte_len);
4981 }
4982 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004983 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004984 } else {
James Smart7a470272010-03-15 11:25:20 -04004985 /* Populate mbox extension offset word. */
4986 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4987 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4988 = MAILBOX_HBA_EXT_OFFSET;
4989
4990 /* Copy the mailbox extension data */
4991 if (pmbox->in_ext_byte_len && pmbox->context2) {
4992 lpfc_memcpy_to_slim(phba->MBslimaddr +
4993 MAILBOX_HBA_EXT_OFFSET,
4994 pmbox->context2, pmbox->in_ext_byte_len);
4995
4996 }
James Smart92908312006-03-07 15:04:13 -05004997 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004998 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004999 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005000 }
5001
5002 /* First copy mbox command data to HBA SLIM, skip past first
5003 word */
5004 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5005 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5006 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5007
5008 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005009 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005010 to_slim = phba->MBslimaddr;
5011 writel(ldata, to_slim);
5012 readl(to_slim); /* flush */
5013
5014 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5015 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005016 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005017 }
5018 }
5019
5020 wmb();
dea31012005-04-17 16:05:31 -05005021
5022 switch (flag) {
5023 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005024 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005025 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005026 /* Interrupt board to do it */
5027 writel(CA_MBATT, phba->CAregaddr);
5028 readl(phba->CAregaddr); /* flush */
5029 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005030 break;
5031
5032 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005033 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005034 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005035 /* Interrupt board to do it */
5036 writel(CA_MBATT, phba->CAregaddr);
5037 readl(phba->CAregaddr); /* flush */
5038
James Smart3772a992009-05-22 14:50:54 -04005039 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005040 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005041 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005042 word0 = le32_to_cpu(word0);
5043 } else {
5044 /* First read mbox status word */
5045 word0 = readl(phba->MBslimaddr);
5046 }
5047
5048 /* Read the HBA Host Attention Register */
5049 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005050 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5051 mb->mbxCommand) *
5052 1000) + jiffies;
5053 i = 0;
dea31012005-04-17 16:05:31 -05005054 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005055 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5056 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005057 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005058 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005059 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005060 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005061 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005062 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005063 }
5064
5065 /* Check if we took a mbox interrupt while we were
5066 polling */
5067 if (((word0 & OWN_CHIP) != OWN_CHIP)
5068 && (evtctr != psli->slistat.mbox_event))
5069 break;
5070
James Smart09372822008-01-11 01:52:54 -05005071 if (i++ > 10) {
5072 spin_unlock_irqrestore(&phba->hbalock,
5073 drvr_flag);
5074 msleep(1);
5075 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5076 }
dea31012005-04-17 16:05:31 -05005077
James Smart3772a992009-05-22 14:50:54 -04005078 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005079 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005080 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005081 word0 = le32_to_cpu(word0);
5082 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5083 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005084 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005085 /* Check real SLIM for any errors */
5086 slimword0 = readl(phba->MBslimaddr);
5087 slimmb = (MAILBOX_t *) & slimword0;
5088 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5089 && slimmb->mbxStatus) {
5090 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005091 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005092 word0 = slimword0;
5093 }
5094 }
5095 } else {
5096 /* First copy command data */
5097 word0 = readl(phba->MBslimaddr);
5098 }
5099 /* Read the HBA Host Attention Register */
5100 ha_copy = readl(phba->HAregaddr);
5101 }
5102
James Smart3772a992009-05-22 14:50:54 -04005103 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005104 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005105 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005106 /* Copy the mailbox extension data */
5107 if (pmbox->out_ext_byte_len && pmbox->context2) {
5108 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5109 pmbox->context2,
5110 pmbox->out_ext_byte_len);
5111 }
dea31012005-04-17 16:05:31 -05005112 } else {
5113 /* First copy command data */
5114 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5115 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005116 /* Copy the mailbox extension data */
5117 if (pmbox->out_ext_byte_len && pmbox->context2) {
5118 lpfc_memcpy_from_slim(pmbox->context2,
5119 phba->MBslimaddr +
5120 MAILBOX_HBA_EXT_OFFSET,
5121 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005122 }
5123 }
5124
5125 writel(HA_MBATT, phba->HAregaddr);
5126 readl(phba->HAregaddr); /* flush */
5127
5128 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5129 status = mb->mbxStatus;
5130 }
5131
James Smart2e0fef82007-06-17 19:56:36 -05005132 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5133 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005134
5135out_not_finished:
5136 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005137 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005138 lpfc_mbox_cmpl_put(phba, pmbox);
5139 }
5140 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005141}
5142
James Smarte59058c2008-08-24 21:49:00 -04005143/**
James Smartf1126682009-06-10 17:22:44 -04005144 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5145 * @phba: Pointer to HBA context object.
5146 *
5147 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5148 * the driver internal pending mailbox queue. It will then try to wait out the
5149 * possible outstanding mailbox command before return.
5150 *
5151 * Returns:
5152 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5153 * the outstanding mailbox command timed out.
5154 **/
5155static int
5156lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5157{
5158 struct lpfc_sli *psli = &phba->sli;
5159 uint8_t actcmd = MBX_HEARTBEAT;
5160 int rc = 0;
5161 unsigned long timeout;
5162
5163 /* Mark the asynchronous mailbox command posting as blocked */
5164 spin_lock_irq(&phba->hbalock);
5165 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5166 if (phba->sli.mbox_active)
5167 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5168 spin_unlock_irq(&phba->hbalock);
5169 /* Determine how long we might wait for the active mailbox
5170 * command to be gracefully completed by firmware.
5171 */
5172 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5173 jiffies;
5174 /* Wait for the outstnading mailbox command to complete */
5175 while (phba->sli.mbox_active) {
5176 /* Check active mailbox complete status every 2ms */
5177 msleep(2);
5178 if (time_after(jiffies, timeout)) {
5179 /* Timeout, marked the outstanding cmd not complete */
5180 rc = 1;
5181 break;
5182 }
5183 }
5184
5185 /* Can not cleanly block async mailbox command, fails it */
5186 if (rc) {
5187 spin_lock_irq(&phba->hbalock);
5188 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5189 spin_unlock_irq(&phba->hbalock);
5190 }
5191 return rc;
5192}
5193
5194/**
5195 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5196 * @phba: Pointer to HBA context object.
5197 *
5198 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5199 * commands from the driver internal pending mailbox queue. It makes sure
5200 * that there is no outstanding mailbox command before resuming posting
5201 * asynchronous mailbox commands. If, for any reason, there is outstanding
5202 * mailbox command, it will try to wait it out before resuming asynchronous
5203 * mailbox command posting.
5204 **/
5205static void
5206lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5207{
5208 struct lpfc_sli *psli = &phba->sli;
5209
5210 spin_lock_irq(&phba->hbalock);
5211 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5212 /* Asynchronous mailbox posting is not blocked, do nothing */
5213 spin_unlock_irq(&phba->hbalock);
5214 return;
5215 }
5216
5217 /* Outstanding synchronous mailbox command is guaranteed to be done,
5218 * successful or timeout, after timing-out the outstanding mailbox
5219 * command shall always be removed, so just unblock posting async
5220 * mailbox command and resume
5221 */
5222 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5223 spin_unlock_irq(&phba->hbalock);
5224
5225 /* wake up worker thread to post asynchronlous mailbox command */
5226 lpfc_worker_wake_up(phba);
5227}
5228
5229/**
James Smartda0436e2009-05-22 14:51:39 -04005230 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5231 * @phba: Pointer to HBA context object.
5232 * @mboxq: Pointer to mailbox object.
5233 *
5234 * The function posts a mailbox to the port. The mailbox is expected
5235 * to be comletely filled in and ready for the port to operate on it.
5236 * This routine executes a synchronous completion operation on the
5237 * mailbox by polling for its completion.
5238 *
5239 * The caller must not be holding any locks when calling this routine.
5240 *
5241 * Returns:
5242 * MBX_SUCCESS - mailbox posted successfully
5243 * Any of the MBX error values.
5244 **/
5245static int
5246lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5247{
5248 int rc = MBX_SUCCESS;
5249 unsigned long iflag;
5250 uint32_t db_ready;
5251 uint32_t mcqe_status;
5252 uint32_t mbx_cmnd;
5253 unsigned long timeout;
5254 struct lpfc_sli *psli = &phba->sli;
5255 struct lpfc_mqe *mb = &mboxq->u.mqe;
5256 struct lpfc_bmbx_create *mbox_rgn;
5257 struct dma_address *dma_address;
5258 struct lpfc_register bmbx_reg;
5259
5260 /*
5261 * Only one mailbox can be active to the bootstrap mailbox region
5262 * at a time and there is no queueing provided.
5263 */
5264 spin_lock_irqsave(&phba->hbalock, iflag);
5265 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5266 spin_unlock_irqrestore(&phba->hbalock, iflag);
5267 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5268 "(%d):2532 Mailbox command x%x (x%x) "
5269 "cannot issue Data: x%x x%x\n",
5270 mboxq->vport ? mboxq->vport->vpi : 0,
5271 mboxq->u.mb.mbxCommand,
5272 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5273 psli->sli_flag, MBX_POLL);
5274 return MBXERR_ERROR;
5275 }
5276 /* The server grabs the token and owns it until release */
5277 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5278 phba->sli.mbox_active = mboxq;
5279 spin_unlock_irqrestore(&phba->hbalock, iflag);
5280
5281 /*
5282 * Initialize the bootstrap memory region to avoid stale data areas
5283 * in the mailbox post. Then copy the caller's mailbox contents to
5284 * the bmbx mailbox region.
5285 */
5286 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5287 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5288 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5289 sizeof(struct lpfc_mqe));
5290
5291 /* Post the high mailbox dma address to the port and wait for ready. */
5292 dma_address = &phba->sli4_hba.bmbx.dma_address;
5293 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5294
5295 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5296 * 1000) + jiffies;
5297 do {
5298 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5299 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5300 if (!db_ready)
5301 msleep(2);
5302
5303 if (time_after(jiffies, timeout)) {
5304 rc = MBXERR_ERROR;
5305 goto exit;
5306 }
5307 } while (!db_ready);
5308
5309 /* Post the low mailbox dma address to the port. */
5310 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5311 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5312 * 1000) + jiffies;
5313 do {
5314 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5315 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5316 if (!db_ready)
5317 msleep(2);
5318
5319 if (time_after(jiffies, timeout)) {
5320 rc = MBXERR_ERROR;
5321 goto exit;
5322 }
5323 } while (!db_ready);
5324
5325 /*
5326 * Read the CQ to ensure the mailbox has completed.
5327 * If so, update the mailbox status so that the upper layers
5328 * can complete the request normally.
5329 */
5330 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5331 sizeof(struct lpfc_mqe));
5332 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5333 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5334 sizeof(struct lpfc_mcqe));
5335 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5336
5337 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5338 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5339 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5340 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005341 } else
5342 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005343
5344 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5345 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5346 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5347 " x%x x%x CQ: x%x x%x x%x x%x\n",
5348 mboxq->vport ? mboxq->vport->vpi : 0,
5349 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5350 bf_get(lpfc_mqe_status, mb),
5351 mb->un.mb_words[0], mb->un.mb_words[1],
5352 mb->un.mb_words[2], mb->un.mb_words[3],
5353 mb->un.mb_words[4], mb->un.mb_words[5],
5354 mb->un.mb_words[6], mb->un.mb_words[7],
5355 mb->un.mb_words[8], mb->un.mb_words[9],
5356 mb->un.mb_words[10], mb->un.mb_words[11],
5357 mb->un.mb_words[12], mboxq->mcqe.word0,
5358 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5359 mboxq->mcqe.trailer);
5360exit:
5361 /* We are holding the token, no needed for lock when release */
5362 spin_lock_irqsave(&phba->hbalock, iflag);
5363 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5364 phba->sli.mbox_active = NULL;
5365 spin_unlock_irqrestore(&phba->hbalock, iflag);
5366 return rc;
5367}
5368
5369/**
5370 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5371 * @phba: Pointer to HBA context object.
5372 * @pmbox: Pointer to mailbox object.
5373 * @flag: Flag indicating how the mailbox need to be processed.
5374 *
5375 * This function is called by discovery code and HBA management code to submit
5376 * a mailbox command to firmware with SLI-4 interface spec.
5377 *
5378 * Return codes the caller owns the mailbox command after the return of the
5379 * function.
5380 **/
5381static int
5382lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5383 uint32_t flag)
5384{
5385 struct lpfc_sli *psli = &phba->sli;
5386 unsigned long iflags;
5387 int rc;
5388
James Smart8fa38512009-07-19 10:01:03 -04005389 rc = lpfc_mbox_dev_check(phba);
5390 if (unlikely(rc)) {
5391 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5392 "(%d):2544 Mailbox command x%x (x%x) "
5393 "cannot issue Data: x%x x%x\n",
5394 mboxq->vport ? mboxq->vport->vpi : 0,
5395 mboxq->u.mb.mbxCommand,
5396 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5397 psli->sli_flag, flag);
5398 goto out_not_finished;
5399 }
5400
James Smartda0436e2009-05-22 14:51:39 -04005401 /* Detect polling mode and jump to a handler */
5402 if (!phba->sli4_hba.intr_enable) {
5403 if (flag == MBX_POLL)
5404 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5405 else
5406 rc = -EIO;
5407 if (rc != MBX_SUCCESS)
5408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5409 "(%d):2541 Mailbox command x%x "
5410 "(x%x) cannot issue Data: x%x x%x\n",
5411 mboxq->vport ? mboxq->vport->vpi : 0,
5412 mboxq->u.mb.mbxCommand,
5413 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5414 psli->sli_flag, flag);
5415 return rc;
5416 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005417 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5418 "(%d):2542 Try to issue mailbox command "
5419 "x%x (x%x) synchronously ahead of async"
5420 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005421 mboxq->vport ? mboxq->vport->vpi : 0,
5422 mboxq->u.mb.mbxCommand,
5423 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5424 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005425 /* Try to block the asynchronous mailbox posting */
5426 rc = lpfc_sli4_async_mbox_block(phba);
5427 if (!rc) {
5428 /* Successfully blocked, now issue sync mbox cmd */
5429 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5430 if (rc != MBX_SUCCESS)
5431 lpfc_printf_log(phba, KERN_ERR,
5432 LOG_MBOX | LOG_SLI,
5433 "(%d):2597 Mailbox command "
5434 "x%x (x%x) cannot issue "
5435 "Data: x%x x%x\n",
5436 mboxq->vport ?
5437 mboxq->vport->vpi : 0,
5438 mboxq->u.mb.mbxCommand,
5439 lpfc_sli4_mbox_opcode_get(phba,
5440 mboxq),
5441 psli->sli_flag, flag);
5442 /* Unblock the async mailbox posting afterward */
5443 lpfc_sli4_async_mbox_unblock(phba);
5444 }
5445 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005446 }
5447
5448 /* Now, interrupt mode asynchrous mailbox command */
5449 rc = lpfc_mbox_cmd_check(phba, mboxq);
5450 if (rc) {
5451 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5452 "(%d):2543 Mailbox command x%x (x%x) "
5453 "cannot issue Data: x%x x%x\n",
5454 mboxq->vport ? mboxq->vport->vpi : 0,
5455 mboxq->u.mb.mbxCommand,
5456 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5457 psli->sli_flag, flag);
5458 goto out_not_finished;
5459 }
James Smartda0436e2009-05-22 14:51:39 -04005460
5461 /* Put the mailbox command to the driver internal FIFO */
5462 psli->slistat.mbox_busy++;
5463 spin_lock_irqsave(&phba->hbalock, iflags);
5464 lpfc_mbox_put(phba, mboxq);
5465 spin_unlock_irqrestore(&phba->hbalock, iflags);
5466 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5467 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5468 "x%x (x%x) x%x x%x x%x\n",
5469 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5470 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5471 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5472 phba->pport->port_state,
5473 psli->sli_flag, MBX_NOWAIT);
5474 /* Wake up worker thread to transport mailbox command from head */
5475 lpfc_worker_wake_up(phba);
5476
5477 return MBX_BUSY;
5478
5479out_not_finished:
5480 return MBX_NOT_FINISHED;
5481}
5482
5483/**
5484 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5485 * @phba: Pointer to HBA context object.
5486 *
5487 * This function is called by worker thread to send a mailbox command to
5488 * SLI4 HBA firmware.
5489 *
5490 **/
5491int
5492lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5493{
5494 struct lpfc_sli *psli = &phba->sli;
5495 LPFC_MBOXQ_t *mboxq;
5496 int rc = MBX_SUCCESS;
5497 unsigned long iflags;
5498 struct lpfc_mqe *mqe;
5499 uint32_t mbx_cmnd;
5500
5501 /* Check interrupt mode before post async mailbox command */
5502 if (unlikely(!phba->sli4_hba.intr_enable))
5503 return MBX_NOT_FINISHED;
5504
5505 /* Check for mailbox command service token */
5506 spin_lock_irqsave(&phba->hbalock, iflags);
5507 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5508 spin_unlock_irqrestore(&phba->hbalock, iflags);
5509 return MBX_NOT_FINISHED;
5510 }
5511 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5512 spin_unlock_irqrestore(&phba->hbalock, iflags);
5513 return MBX_NOT_FINISHED;
5514 }
5515 if (unlikely(phba->sli.mbox_active)) {
5516 spin_unlock_irqrestore(&phba->hbalock, iflags);
5517 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5518 "0384 There is pending active mailbox cmd\n");
5519 return MBX_NOT_FINISHED;
5520 }
5521 /* Take the mailbox command service token */
5522 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5523
5524 /* Get the next mailbox command from head of queue */
5525 mboxq = lpfc_mbox_get(phba);
5526
5527 /* If no more mailbox command waiting for post, we're done */
5528 if (!mboxq) {
5529 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5530 spin_unlock_irqrestore(&phba->hbalock, iflags);
5531 return MBX_SUCCESS;
5532 }
5533 phba->sli.mbox_active = mboxq;
5534 spin_unlock_irqrestore(&phba->hbalock, iflags);
5535
5536 /* Check device readiness for posting mailbox command */
5537 rc = lpfc_mbox_dev_check(phba);
5538 if (unlikely(rc))
5539 /* Driver clean routine will clean up pending mailbox */
5540 goto out_not_finished;
5541
5542 /* Prepare the mbox command to be posted */
5543 mqe = &mboxq->u.mqe;
5544 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5545
5546 /* Start timer for the mbox_tmo and log some mailbox post messages */
5547 mod_timer(&psli->mbox_tmo, (jiffies +
5548 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5549
5550 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5551 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5552 "x%x x%x\n",
5553 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5554 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5555 phba->pport->port_state, psli->sli_flag);
5556
5557 if (mbx_cmnd != MBX_HEARTBEAT) {
5558 if (mboxq->vport) {
5559 lpfc_debugfs_disc_trc(mboxq->vport,
5560 LPFC_DISC_TRC_MBOX_VPORT,
5561 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5562 mbx_cmnd, mqe->un.mb_words[0],
5563 mqe->un.mb_words[1]);
5564 } else {
5565 lpfc_debugfs_disc_trc(phba->pport,
5566 LPFC_DISC_TRC_MBOX,
5567 "MBOX Send: cmd:x%x mb:x%x x%x",
5568 mbx_cmnd, mqe->un.mb_words[0],
5569 mqe->un.mb_words[1]);
5570 }
5571 }
5572 psli->slistat.mbox_cmd++;
5573
5574 /* Post the mailbox command to the port */
5575 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5576 if (rc != MBX_SUCCESS) {
5577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5578 "(%d):2533 Mailbox command x%x (x%x) "
5579 "cannot issue Data: x%x x%x\n",
5580 mboxq->vport ? mboxq->vport->vpi : 0,
5581 mboxq->u.mb.mbxCommand,
5582 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5583 psli->sli_flag, MBX_NOWAIT);
5584 goto out_not_finished;
5585 }
5586
5587 return rc;
5588
5589out_not_finished:
5590 spin_lock_irqsave(&phba->hbalock, iflags);
5591 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5592 __lpfc_mbox_cmpl_put(phba, mboxq);
5593 /* Release the token */
5594 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5595 phba->sli.mbox_active = NULL;
5596 spin_unlock_irqrestore(&phba->hbalock, iflags);
5597
5598 return MBX_NOT_FINISHED;
5599}
5600
5601/**
5602 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5603 * @phba: Pointer to HBA context object.
5604 * @pmbox: Pointer to mailbox object.
5605 * @flag: Flag indicating how the mailbox need to be processed.
5606 *
5607 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5608 * the API jump table function pointer from the lpfc_hba struct.
5609 *
5610 * Return codes the caller owns the mailbox command after the return of the
5611 * function.
5612 **/
5613int
5614lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5615{
5616 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5617}
5618
5619/**
5620 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5621 * @phba: The hba struct for which this call is being executed.
5622 * @dev_grp: The HBA PCI-Device group number.
5623 *
5624 * This routine sets up the mbox interface API function jump table in @phba
5625 * struct.
5626 * Returns: 0 - success, -ENODEV - failure.
5627 **/
5628int
5629lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5630{
5631
5632 switch (dev_grp) {
5633 case LPFC_PCI_DEV_LP:
5634 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5635 phba->lpfc_sli_handle_slow_ring_event =
5636 lpfc_sli_handle_slow_ring_event_s3;
5637 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5638 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5639 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5640 break;
5641 case LPFC_PCI_DEV_OC:
5642 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5643 phba->lpfc_sli_handle_slow_ring_event =
5644 lpfc_sli_handle_slow_ring_event_s4;
5645 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5646 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5647 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5648 break;
5649 default:
5650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5651 "1420 Invalid HBA PCI-device group: 0x%x\n",
5652 dev_grp);
5653 return -ENODEV;
5654 break;
5655 }
5656 return 0;
5657}
5658
5659/**
James Smart3621a712009-04-06 18:47:14 -04005660 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005661 * @phba: Pointer to HBA context object.
5662 * @pring: Pointer to driver SLI ring object.
5663 * @piocb: Pointer to address of newly added command iocb.
5664 *
5665 * This function is called with hbalock held to add a command
5666 * iocb to the txq when SLI layer cannot submit the command iocb
5667 * to the ring.
5668 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04005669void
James Smart92d7f7b2007-06-17 19:56:38 -05005670__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005671 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005672{
5673 /* Insert the caller's iocb in the txq tail for later processing. */
5674 list_add_tail(&piocb->list, &pring->txq);
5675 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005676}
5677
James Smarte59058c2008-08-24 21:49:00 -04005678/**
James Smart3621a712009-04-06 18:47:14 -04005679 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005680 * @phba: Pointer to HBA context object.
5681 * @pring: Pointer to driver SLI ring object.
5682 * @piocb: Pointer to address of newly added command iocb.
5683 *
5684 * This function is called with hbalock held before a new
5685 * iocb is submitted to the firmware. This function checks
5686 * txq to flush the iocbs in txq to Firmware before
5687 * submitting new iocbs to the Firmware.
5688 * If there are iocbs in the txq which need to be submitted
5689 * to firmware, lpfc_sli_next_iocb returns the first element
5690 * of the txq after dequeuing it from txq.
5691 * If there is no iocb in the txq then the function will return
5692 * *piocb and *piocb is set to NULL. Caller needs to check
5693 * *piocb to find if there are more commands in the txq.
5694 **/
dea31012005-04-17 16:05:31 -05005695static struct lpfc_iocbq *
5696lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005697 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005698{
5699 struct lpfc_iocbq * nextiocb;
5700
5701 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5702 if (!nextiocb) {
5703 nextiocb = *piocb;
5704 *piocb = NULL;
5705 }
5706
5707 return nextiocb;
5708}
5709
James Smarte59058c2008-08-24 21:49:00 -04005710/**
James Smart3772a992009-05-22 14:50:54 -04005711 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005712 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005713 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005714 * @piocb: Pointer to command iocb.
5715 * @flag: Flag indicating if this command can be put into txq.
5716 *
James Smart3772a992009-05-22 14:50:54 -04005717 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5718 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5719 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5720 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5721 * this function allows only iocbs for posting buffers. This function finds
5722 * next available slot in the command ring and posts the command to the
5723 * available slot and writes the port attention register to request HBA start
5724 * processing new iocb. If there is no slot available in the ring and
5725 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5726 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005727 *
James Smart3772a992009-05-22 14:50:54 -04005728 * This function is called with hbalock held. The function will return success
5729 * after it successfully submit the iocb to firmware or after adding to the
5730 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005731 **/
James Smart98c9ea52007-10-27 13:37:33 -04005732static int
James Smart3772a992009-05-22 14:50:54 -04005733__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005734 struct lpfc_iocbq *piocb, uint32_t flag)
5735{
5736 struct lpfc_iocbq *nextiocb;
5737 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005738 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005739
James Smart92d7f7b2007-06-17 19:56:38 -05005740 if (piocb->iocb_cmpl && (!piocb->vport) &&
5741 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5742 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5743 lpfc_printf_log(phba, KERN_ERR,
5744 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005745 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005746 piocb->iocb.ulpCommand);
5747 dump_stack();
5748 return IOCB_ERROR;
5749 }
5750
5751
Linas Vepstas8d63f372007-02-14 14:28:36 -06005752 /* If the PCI channel is in offline state, do not post iocbs. */
5753 if (unlikely(pci_channel_offline(phba->pcidev)))
5754 return IOCB_ERROR;
5755
James Smarta257bf92009-04-06 18:48:10 -04005756 /* If HBA has a deferred error attention, fail the iocb. */
5757 if (unlikely(phba->hba_flag & DEFER_ERATT))
5758 return IOCB_ERROR;
5759
dea31012005-04-17 16:05:31 -05005760 /*
5761 * We should never get an IOCB if we are in a < LINK_DOWN state
5762 */
James Smart2e0fef82007-06-17 19:56:36 -05005763 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005764 return IOCB_ERROR;
5765
5766 /*
5767 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005768 * outstanding event.
dea31012005-04-17 16:05:31 -05005769 */
James Smart0b727fe2007-10-27 13:37:25 -04005770 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005771 goto iocb_busy;
5772
James Smart2e0fef82007-06-17 19:56:36 -05005773 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005774 /*
James Smart2680eea2007-04-25 09:52:55 -04005775 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005776 * can be issued if the link is not up.
5777 */
5778 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005779 case CMD_GEN_REQUEST64_CR:
5780 case CMD_GEN_REQUEST64_CX:
5781 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5782 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005783 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005784 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5785 MENLO_TRANSPORT_TYPE))
5786
5787 goto iocb_busy;
5788 break;
dea31012005-04-17 16:05:31 -05005789 case CMD_QUE_RING_BUF_CN:
5790 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005791 /*
5792 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5793 * completion, iocb_cmpl MUST be 0.
5794 */
5795 if (piocb->iocb_cmpl)
5796 piocb->iocb_cmpl = NULL;
5797 /*FALLTHROUGH*/
5798 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005799 case CMD_CLOSE_XRI_CN:
5800 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005801 break;
5802 default:
5803 goto iocb_busy;
5804 }
5805
5806 /*
5807 * For FCP commands, we must be in a state where we can process link
5808 * attention events.
5809 */
5810 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005811 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005812 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005813 }
dea31012005-04-17 16:05:31 -05005814
dea31012005-04-17 16:05:31 -05005815 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5816 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5817 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5818
5819 if (iocb)
5820 lpfc_sli_update_ring(phba, pring);
5821 else
5822 lpfc_sli_update_full_ring(phba, pring);
5823
5824 if (!piocb)
5825 return IOCB_SUCCESS;
5826
5827 goto out_busy;
5828
5829 iocb_busy:
5830 pring->stats.iocb_cmd_delay++;
5831
5832 out_busy:
5833
5834 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005835 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005836 return IOCB_SUCCESS;
5837 }
5838
5839 return IOCB_BUSY;
5840}
5841
James Smart3772a992009-05-22 14:50:54 -04005842/**
James Smart4f774512009-05-22 14:52:35 -04005843 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5844 * @phba: Pointer to HBA context object.
5845 * @piocb: Pointer to command iocb.
5846 * @sglq: Pointer to the scatter gather queue object.
5847 *
5848 * This routine converts the bpl or bde that is in the IOCB
5849 * to a sgl list for the sli4 hardware. The physical address
5850 * of the bpl/bde is converted back to a virtual address.
5851 * If the IOCB contains a BPL then the list of BDE's is
5852 * converted to sli4_sge's. If the IOCB contains a single
5853 * BDE then it is converted to a single sli_sge.
5854 * The IOCB is still in cpu endianess so the contents of
5855 * the bpl can be used without byte swapping.
5856 *
5857 * Returns valid XRI = Success, NO_XRI = Failure.
5858**/
5859static uint16_t
5860lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5861 struct lpfc_sglq *sglq)
5862{
5863 uint16_t xritag = NO_XRI;
5864 struct ulp_bde64 *bpl = NULL;
5865 struct ulp_bde64 bde;
5866 struct sli4_sge *sgl = NULL;
5867 IOCB_t *icmd;
5868 int numBdes = 0;
5869 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05005870 uint32_t offset = 0; /* accumulated offset in the sg request list */
5871 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04005872
5873 if (!piocbq || !sglq)
5874 return xritag;
5875
5876 sgl = (struct sli4_sge *)sglq->sgl;
5877 icmd = &piocbq->iocb;
5878 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5879 numBdes = icmd->un.genreq64.bdl.bdeSize /
5880 sizeof(struct ulp_bde64);
5881 /* The addrHigh and addrLow fields within the IOCB
5882 * have not been byteswapped yet so there is no
5883 * need to swap them back.
5884 */
5885 bpl = (struct ulp_bde64 *)
5886 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5887
5888 if (!bpl)
5889 return xritag;
5890
5891 for (i = 0; i < numBdes; i++) {
5892 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005893 sgl->addr_hi = bpl->addrHigh;
5894 sgl->addr_lo = bpl->addrLow;
5895
James Smart4f774512009-05-22 14:52:35 -04005896 if ((i+1) == numBdes)
5897 bf_set(lpfc_sli4_sge_last, sgl, 1);
5898 else
5899 bf_set(lpfc_sli4_sge_last, sgl, 0);
5900 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005901 /* swap the size field back to the cpu so we
5902 * can assign it to the sgl.
5903 */
5904 bde.tus.w = le32_to_cpu(bpl->tus.w);
5905 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05005906 /* The offsets in the sgl need to be accumulated
5907 * separately for the request and reply lists.
5908 * The request is always first, the reply follows.
5909 */
5910 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
5911 /* add up the reply sg entries */
5912 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
5913 inbound++;
5914 /* first inbound? reset the offset */
5915 if (inbound == 1)
5916 offset = 0;
5917 bf_set(lpfc_sli4_sge_offset, sgl, offset);
5918 offset += bde.tus.f.bdeSize;
5919 }
James Smart4f774512009-05-22 14:52:35 -04005920 bpl++;
5921 sgl++;
5922 }
5923 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5924 /* The addrHigh and addrLow fields of the BDE have not
5925 * been byteswapped yet so they need to be swapped
5926 * before putting them in the sgl.
5927 */
5928 sgl->addr_hi =
5929 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5930 sgl->addr_lo =
5931 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005932 bf_set(lpfc_sli4_sge_last, sgl, 1);
5933 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005934 sgl->sge_len =
5935 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005936 }
5937 return sglq->sli4_xritag;
5938}
5939
5940/**
5941 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5942 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005943 *
James Smarta93ff372010-10-22 11:06:08 -04005944 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005945 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5946 * held.
James Smart4f774512009-05-22 14:52:35 -04005947 *
5948 * Return: index into SLI4 fast-path FCP queue index.
5949 **/
5950static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005951lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005952{
James Smart8fa38512009-07-19 10:01:03 -04005953 ++phba->fcp_qidx;
5954 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5955 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005956
James Smart8fa38512009-07-19 10:01:03 -04005957 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005958}
5959
5960/**
5961 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5962 * @phba: Pointer to HBA context object.
5963 * @piocb: Pointer to command iocb.
5964 * @wqe: Pointer to the work queue entry.
5965 *
5966 * This routine converts the iocb command to its Work Queue Entry
5967 * equivalent. The wqe pointer should not have any fields set when
5968 * this routine is called because it will memcpy over them.
5969 * This routine does not set the CQ_ID or the WQEC bits in the
5970 * wqe.
5971 *
5972 * Returns: 0 = Success, IOCB_ERROR = Failure.
5973 **/
5974static int
5975lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5976 union lpfc_wqe *wqe)
5977{
James Smart5ffc2662009-11-18 15:39:44 -05005978 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005979 uint8_t ct = 0;
5980 uint32_t fip;
5981 uint32_t abort_tag;
5982 uint8_t command_type = ELS_COMMAND_NON_FIP;
5983 uint8_t cmnd;
5984 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04005985 uint16_t abrt_iotag;
5986 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04005987 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04005988 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005989 int numBdes, i;
5990 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005991
James Smart45ed1192009-10-02 15:17:02 -04005992 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005993 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005994 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005995 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005996 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005997 command_type = ELS_COMMAND_FIP;
5998 else
5999 command_type = ELS_COMMAND_NON_FIP;
6000
James Smart4f774512009-05-22 14:52:35 -04006001 /* Some of the fields are in the right position already */
6002 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6003 abort_tag = (uint32_t) iocbq->iotag;
6004 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04006005 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04006006 /* words0-2 bpl convert bde */
6007 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05006008 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6009 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04006010 bpl = (struct ulp_bde64 *)
6011 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6012 if (!bpl)
6013 return IOCB_ERROR;
6014
6015 /* Should already be byte swapped. */
6016 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6017 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6018 /* swap the size field back to the cpu so we
6019 * can assign it to the sgl.
6020 */
6021 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006022 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6023 total_len = 0;
6024 for (i = 0; i < numBdes; i++) {
6025 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6026 total_len += bde.tus.f.bdeSize;
6027 }
James Smart4f774512009-05-22 14:52:35 -04006028 } else
James Smart5ffc2662009-11-18 15:39:44 -05006029 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006030
6031 iocbq->iocb.ulpIoTag = iocbq->iotag;
6032 cmnd = iocbq->iocb.ulpCommand;
6033
6034 switch (iocbq->iocb.ulpCommand) {
6035 case CMD_ELS_REQUEST64_CR:
6036 if (!iocbq->iocb.ulpLe) {
6037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6038 "2007 Only Limited Edition cmd Format"
6039 " supported 0x%x\n",
6040 iocbq->iocb.ulpCommand);
6041 return IOCB_ERROR;
6042 }
James Smart5ffc2662009-11-18 15:39:44 -05006043 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006044 /* Els_reguest64 has a TMO */
6045 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6046 iocbq->iocb.ulpTimeout);
6047 /* Need a VF for word 4 set the vf bit*/
6048 bf_set(els_req64_vf, &wqe->els_req, 0);
6049 /* And a VFID for word 12 */
6050 bf_set(els_req64_vfid, &wqe->els_req, 0);
6051 /*
6052 * Set ct field to 3, indicates that the context_tag field
6053 * contains the FCFI and remote N_Port_ID is
6054 * in word 5.
6055 */
James Smart4f774512009-05-22 14:52:35 -04006056 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04006057 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6058 iocbq->iocb.ulpContext);
6059 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6060 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006061 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006062 if (command_type == ELS_COMMAND_FIP) {
6063 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6064 >> LPFC_FIP_ELS_ID_SHIFT);
6065 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006066 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6067 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6068 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6069 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6070 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6071 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006072 break;
James Smart5ffc2662009-11-18 15:39:44 -05006073 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04006074 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6075 iocbq->iocb.un.ulpWord[3]);
6076 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6077 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006078 /* The entire sequence is transmitted for this IOCB */
6079 xmit_len = total_len;
6080 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006081 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006082 /* word3 iocb=io_tag32 wqe=reserved */
6083 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04006084 /* word4 relative_offset memcpy */
6085 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006086 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6087 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6088 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6089 LPFC_WQE_IOD_WRITE);
6090 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6091 LPFC_WQE_LENLOC_WORD12);
6092 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006093 wqe->xmit_sequence.xmit_len = xmit_len;
6094 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006095 break;
6096 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04006097 /* word3 iocb=iotag32 wqe=seq_payload_len */
6098 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006099 /* word4 iocb=rsvd wqe=rsvd */
6100 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6101 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04006102 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006103 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04006104 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6105 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6106 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6107 LPFC_WQE_LENLOC_WORD3);
6108 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006109 break;
6110 case CMD_FCP_IWRITE64_CR:
6111 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04006112 /* word3 iocb=iotag wqe=payload_offset_len */
6113 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6114 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05006115 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04006116 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6117 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6118 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6119 iocbq->iocb.ulpFCP2Rcvy);
6120 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6121 /* Always open the exchange */
6122 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6123 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6124 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6125 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6126 LPFC_WQE_LENLOC_WORD4);
6127 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6128 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6129 break;
6130 case CMD_FCP_IREAD64_CR:
6131 /* word3 iocb=iotag wqe=payload_offset_len */
6132 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6133 wqe->fcp_iread.payload_offset_len =
6134 xmit_len + sizeof(struct fcp_rsp);
6135 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6136 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6137 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6138 iocbq->iocb.ulpFCP2Rcvy);
6139 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04006140 /* Always open the exchange */
6141 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04006142 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6143 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6144 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6145 LPFC_WQE_LENLOC_WORD4);
6146 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6147 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6148 break;
James Smartf1126682009-06-10 17:22:44 -04006149 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006150 /* word3 iocb=IO_TAG wqe=reserved */
6151 wqe->fcp_icmd.rsrvd3 = 0;
6152 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04006153 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04006154 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6155 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6156 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6157 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6158 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6159 LPFC_WQE_LENLOC_NONE);
6160 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006161 break;
6162 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05006163 /* For this command calculate the xmit length of the
6164 * request bde.
6165 */
6166 xmit_len = 0;
6167 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6168 sizeof(struct ulp_bde64);
6169 for (i = 0; i < numBdes; i++) {
6170 if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6171 break;
6172 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6173 xmit_len += bde.tus.f.bdeSize;
6174 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006175 /* word3 iocb=IO_TAG wqe=request_payload_len */
6176 wqe->gen_req.request_payload_len = xmit_len;
6177 /* word4 iocb=parameter wqe=relative_offset memcpy */
6178 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04006179 /* word6 context tag copied in memcpy */
6180 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6181 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6183 "2015 Invalid CT %x command 0x%x\n",
6184 ct, iocbq->iocb.ulpCommand);
6185 return IOCB_ERROR;
6186 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006187 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6188 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6189 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6190 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6191 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6192 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6193 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6194 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006195 command_type = OTHER_COMMAND;
6196 break;
6197 case CMD_XMIT_ELS_RSP64_CX:
6198 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006199 /* word3 iocb=iotag32 wqe=response_payload_len */
6200 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006201 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04006202 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04006203 /* word5 iocb=rsvd wge=did */
6204 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6205 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04006206 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6207 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6208 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6209 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6210 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04006211 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04006212 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006213 iocbq->vport->vpi + phba->vpi_base);
James Smartf0d9bcc2010-10-22 11:07:09 -04006214 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6215 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6216 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6217 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6218 LPFC_WQE_LENLOC_WORD3);
6219 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006220 command_type = OTHER_COMMAND;
6221 break;
6222 case CMD_CLOSE_XRI_CN:
6223 case CMD_ABORT_XRI_CN:
6224 case CMD_ABORT_XRI_CX:
6225 /* words 0-2 memcpy should be 0 rserved */
6226 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006227 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6228 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6229 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6230 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6231 } else
6232 fip = 0;
6233
6234 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006235 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006236 * The link is down, or the command was ELS_FIP
6237 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006238 * on the wire.
6239 */
6240 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6241 else
6242 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6243 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04006244 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6245 wqe->abort_cmd.rsrvd5 = 0;
6246 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006247 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6248 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006249 /*
6250 * The abort handler will send us CMD_ABORT_XRI_CN or
6251 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6252 */
James Smartf0d9bcc2010-10-22 11:07:09 -04006253 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6254 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6255 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6256 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04006257 cmnd = CMD_ABORT_XRI_CX;
6258 command_type = OTHER_COMMAND;
6259 xritag = 0;
6260 break;
James Smart6669f9b2009-10-02 15:16:45 -04006261 case CMD_XMIT_BLS_RSP64_CX:
6262 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6263 * we re-construct this WQE here based on information in
6264 * iocbq from scratch.
6265 */
6266 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006267 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006268 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006269 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6270 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6271 LPFC_ABTS_UNSOL_INT) {
6272 /* ABTS sent by initiator to CT exchange, the
6273 * RX_ID field will be filled with the newly
6274 * allocated responder XRI.
6275 */
6276 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6277 iocbq->sli4_xritag);
6278 } else {
6279 /* ABTS sent by responder to CT exchange, the
6280 * RX_ID field will be filled with the responder
6281 * RX_ID from ABTS.
6282 */
6283 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6284 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6285 }
James Smart6669f9b2009-10-02 15:16:45 -04006286 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6287 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6288 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6289 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04006290 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6291 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6292 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04006293 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6294 command_type = OTHER_COMMAND;
6295 break;
James Smart4f774512009-05-22 14:52:35 -04006296 case CMD_XRI_ABORTED_CX:
6297 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04006298 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6299 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6300 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6301 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6302 default:
6303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6304 "2014 Invalid command 0x%x\n",
6305 iocbq->iocb.ulpCommand);
6306 return IOCB_ERROR;
6307 break;
James Smart4f774512009-05-22 14:52:35 -04006308 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006309 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6310 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6311 wqe->generic.wqe_com.abort_tag = abort_tag;
6312 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6313 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6314 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6315 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04006316 return 0;
6317}
6318
6319/**
6320 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6321 * @phba: Pointer to HBA context object.
6322 * @ring_number: SLI ring number to issue iocb on.
6323 * @piocb: Pointer to command iocb.
6324 * @flag: Flag indicating if this command can be put into txq.
6325 *
6326 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6327 * an iocb command to an HBA with SLI-4 interface spec.
6328 *
6329 * This function is called with hbalock held. The function will return success
6330 * after it successfully submit the iocb to firmware or after adding to the
6331 * txq.
6332 **/
6333static int
6334__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6335 struct lpfc_iocbq *piocb, uint32_t flag)
6336{
6337 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006338 union lpfc_wqe wqe;
6339 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006340
6341 if (piocb->sli4_xritag == NO_XRI) {
6342 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006343 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006344 sglq = NULL;
6345 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006346 if (pring->txq_cnt) {
6347 if (!(flag & SLI_IOCB_RET_IOCB)) {
6348 __lpfc_sli_ringtx_put(phba,
6349 pring, piocb);
6350 return IOCB_SUCCESS;
6351 } else {
6352 return IOCB_BUSY;
6353 }
6354 } else {
James Smart4f774512009-05-22 14:52:35 -04006355 sglq = __lpfc_sli_get_sglq(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04006356 if (!sglq) {
6357 if (!(flag & SLI_IOCB_RET_IOCB)) {
6358 __lpfc_sli_ringtx_put(phba,
6359 pring,
6360 piocb);
6361 return IOCB_SUCCESS;
6362 } else
6363 return IOCB_BUSY;
6364 }
6365 }
James Smart4f774512009-05-22 14:52:35 -04006366 }
6367 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6368 sglq = NULL; /* These IO's already have an XRI and
6369 * a mapped sgl.
6370 */
6371 } else {
6372 /* This is a continuation of a commandi,(CX) so this
6373 * sglq is on the active list
6374 */
6375 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6376 if (!sglq)
6377 return IOCB_ERROR;
6378 }
6379
6380 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006381 piocb->sli4_xritag = sglq->sli4_xritag;
6382
6383 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006384 return IOCB_ERROR;
6385 }
6386
6387 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6388 return IOCB_ERROR;
6389
James Smart341af102010-01-26 23:07:37 -05006390 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6391 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006392 /*
6393 * For FCP command IOCB, get a new WQ index to distribute
6394 * WQE across the WQsr. On the other hand, for abort IOCB,
6395 * it carries the same WQ index to the original command
6396 * IOCB.
6397 */
James Smart341af102010-01-26 23:07:37 -05006398 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006399 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6400 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6401 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006402 return IOCB_ERROR;
6403 } else {
6404 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6405 return IOCB_ERROR;
6406 }
6407 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6408
6409 return 0;
6410}
6411
6412/**
James Smart3772a992009-05-22 14:50:54 -04006413 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6414 *
6415 * This routine wraps the actual lockless version for issusing IOCB function
6416 * pointer from the lpfc_hba struct.
6417 *
6418 * Return codes:
6419 * IOCB_ERROR - Error
6420 * IOCB_SUCCESS - Success
6421 * IOCB_BUSY - Busy
6422 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006423int
James Smart3772a992009-05-22 14:50:54 -04006424__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6425 struct lpfc_iocbq *piocb, uint32_t flag)
6426{
6427 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6428}
6429
6430/**
6431 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6432 * @phba: The hba struct for which this call is being executed.
6433 * @dev_grp: The HBA PCI-Device group number.
6434 *
6435 * This routine sets up the SLI interface API function jump table in @phba
6436 * struct.
6437 * Returns: 0 - success, -ENODEV - failure.
6438 **/
6439int
6440lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6441{
6442
6443 switch (dev_grp) {
6444 case LPFC_PCI_DEV_LP:
6445 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6446 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6447 break;
James Smart4f774512009-05-22 14:52:35 -04006448 case LPFC_PCI_DEV_OC:
6449 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6450 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6451 break;
James Smart3772a992009-05-22 14:50:54 -04006452 default:
6453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6454 "1419 Invalid HBA PCI-device group: 0x%x\n",
6455 dev_grp);
6456 return -ENODEV;
6457 break;
6458 }
6459 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6460 return 0;
6461}
James Smart92d7f7b2007-06-17 19:56:38 -05006462
James Smarte59058c2008-08-24 21:49:00 -04006463/**
James Smart3621a712009-04-06 18:47:14 -04006464 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006465 * @phba: Pointer to HBA context object.
6466 * @pring: Pointer to driver SLI ring object.
6467 * @piocb: Pointer to command iocb.
6468 * @flag: Flag indicating if this command can be put into txq.
6469 *
6470 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6471 * function. This function gets the hbalock and calls
6472 * __lpfc_sli_issue_iocb function and will return the error returned
6473 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6474 * functions which do not hold hbalock.
6475 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006476int
James Smart3772a992009-05-22 14:50:54 -04006477lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006478 struct lpfc_iocbq *piocb, uint32_t flag)
6479{
6480 unsigned long iflags;
6481 int rc;
6482
6483 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006484 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006485 spin_unlock_irqrestore(&phba->hbalock, iflags);
6486
6487 return rc;
6488}
6489
James Smarte59058c2008-08-24 21:49:00 -04006490/**
James Smart3621a712009-04-06 18:47:14 -04006491 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006492 * @phba: Pointer to HBA context object.
6493 *
6494 * This function is called while driver attaches with the
6495 * HBA to setup the extra ring. The extra ring is used
6496 * only when driver needs to support target mode functionality
6497 * or IP over FC functionalities.
6498 *
6499 * This function is called with no lock held.
6500 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006501static int
6502lpfc_extra_ring_setup( struct lpfc_hba *phba)
6503{
6504 struct lpfc_sli *psli;
6505 struct lpfc_sli_ring *pring;
6506
6507 psli = &phba->sli;
6508
6509 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006510
6511 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006512 pring = &psli->ring[psli->fcp_ring];
6513 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6514 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6515 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6516 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6517
James Smarta4bc3372006-12-02 13:34:16 -05006518 /* and give them to the extra ring */
6519 pring = &psli->ring[psli->extra_ring];
6520
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006521 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6522 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6523 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6524 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6525
6526 /* Setup default profile for this ring */
6527 pring->iotag_max = 4096;
6528 pring->num_mask = 1;
6529 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006530 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6531 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006532 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6533 return 0;
6534}
6535
James Smarte59058c2008-08-24 21:49:00 -04006536/**
James Smart3621a712009-04-06 18:47:14 -04006537 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006538 * @phba: Pointer to HBA context object.
6539 * @pring: Pointer to driver SLI ring object.
6540 * @iocbq: Pointer to iocb object.
6541 *
6542 * This function is called by the slow ring event handler
6543 * function when there is an ASYNC event iocb in the ring.
6544 * This function is called with no lock held.
6545 * Currently this function handles only temperature related
6546 * ASYNC events. The function decodes the temperature sensor
6547 * event message and posts events for the management applications.
6548 **/
James Smart98c9ea52007-10-27 13:37:33 -04006549static void
James Smart57127f12007-10-27 13:37:05 -04006550lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6551 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6552{
6553 IOCB_t *icmd;
6554 uint16_t evt_code;
6555 uint16_t temp;
6556 struct temp_event temp_event_data;
6557 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006558 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006559
6560 icmd = &iocbq->iocb;
6561 evt_code = icmd->un.asyncstat.evt_code;
6562 temp = icmd->ulpContext;
6563
6564 if ((evt_code != ASYNC_TEMP_WARN) &&
6565 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006566 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006567 lpfc_printf_log(phba,
6568 KERN_ERR,
6569 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006570 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006571 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006572 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6573 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6574 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6575 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006576 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006577 icmd->un.asyncstat.evt_code,
6578 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6579 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6580 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6581 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6582
James Smart57127f12007-10-27 13:37:05 -04006583 return;
6584 }
6585 temp_event_data.data = (uint32_t)temp;
6586 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6587 if (evt_code == ASYNC_TEMP_WARN) {
6588 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6589 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006590 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006591 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006592 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006593 "corrective action. temperature : %d Celsius\n",
6594 temp);
6595 }
6596 if (evt_code == ASYNC_TEMP_SAFE) {
6597 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6598 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006599 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006600 LOG_TEMP,
6601 "0340 Adapter temperature is OK now. "
6602 "temperature : %d Celsius\n",
6603 temp);
6604 }
6605
6606 /* Send temperature change event to applications */
6607 shost = lpfc_shost_from_vport(phba->pport);
6608 fc_host_post_vendor_event(shost, fc_get_event_number(),
6609 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006610 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006611
6612}
6613
6614
James Smarte59058c2008-08-24 21:49:00 -04006615/**
James Smart3621a712009-04-06 18:47:14 -04006616 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006617 * @phba: Pointer to HBA context object.
6618 *
6619 * lpfc_sli_setup sets up rings of the SLI interface with
6620 * number of iocbs per ring and iotags. This function is
6621 * called while driver attach to the HBA and before the
6622 * interrupts are enabled. So there is no need for locking.
6623 *
6624 * This function always returns 0.
6625 **/
dea31012005-04-17 16:05:31 -05006626int
6627lpfc_sli_setup(struct lpfc_hba *phba)
6628{
James Smarted957682007-06-17 19:56:37 -05006629 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006630 struct lpfc_sli *psli = &phba->sli;
6631 struct lpfc_sli_ring *pring;
6632
6633 psli->num_rings = MAX_CONFIGURED_RINGS;
6634 psli->sli_flag = 0;
6635 psli->fcp_ring = LPFC_FCP_RING;
6636 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006637 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006638
James Bottomley604a3e32005-10-29 10:28:33 -05006639 psli->iocbq_lookup = NULL;
6640 psli->iocbq_lookup_len = 0;
6641 psli->last_iotag = 0;
6642
dea31012005-04-17 16:05:31 -05006643 for (i = 0; i < psli->num_rings; i++) {
6644 pring = &psli->ring[i];
6645 switch (i) {
6646 case LPFC_FCP_RING: /* ring 0 - FCP */
6647 /* numCiocb and numRiocb are used in config_port */
6648 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6649 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6650 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6651 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6652 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6653 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006654 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006655 SLI3_IOCB_CMD_SIZE :
6656 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006657 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006658 SLI3_IOCB_RSP_SIZE :
6659 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006660 pring->iotag_ctr = 0;
6661 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006662 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006663 pring->fast_iotag = pring->iotag_max;
6664 pring->num_mask = 0;
6665 break;
James Smarta4bc3372006-12-02 13:34:16 -05006666 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006667 /* numCiocb and numRiocb are used in config_port */
6668 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6669 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006670 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006671 SLI3_IOCB_CMD_SIZE :
6672 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006673 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006674 SLI3_IOCB_RSP_SIZE :
6675 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006676 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006677 pring->num_mask = 0;
6678 break;
6679 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6680 /* numCiocb and numRiocb are used in config_port */
6681 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6682 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006683 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006684 SLI3_IOCB_CMD_SIZE :
6685 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006686 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006687 SLI3_IOCB_RSP_SIZE :
6688 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006689 pring->fast_iotag = 0;
6690 pring->iotag_ctr = 0;
6691 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006692 pring->lpfc_sli_rcv_async_status =
6693 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006694 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006695 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006696 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6697 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006698 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006699 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006700 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006701 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6702 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006703 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006704 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006705 pring->prt[2].profile = 0; /* Mask 2 */
6706 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006707 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006708 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006709 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006710 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006711 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006712 pring->prt[3].profile = 0; /* Mask 3 */
6713 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006714 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006715 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006716 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006717 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006718 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006719 /* abort unsolicited sequence */
6720 pring->prt[4].profile = 0; /* Mask 4 */
6721 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6722 pring->prt[4].type = FC_TYPE_BLS;
6723 pring->prt[4].lpfc_sli_rcv_unsol_event =
6724 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006725 break;
6726 }
James Smarted957682007-06-17 19:56:37 -05006727 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006728 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006729 }
James Smarted957682007-06-17 19:56:37 -05006730 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006731 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006732 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6733 "SLI2 SLIM Data: x%x x%lx\n",
6734 phba->brd_no, totiocbsize,
6735 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006736 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006737 if (phba->cfg_multi_ring_support == 2)
6738 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006739
6740 return 0;
6741}
6742
James Smarte59058c2008-08-24 21:49:00 -04006743/**
James Smart3621a712009-04-06 18:47:14 -04006744 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006745 * @phba: Pointer to HBA context object.
6746 *
6747 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6748 * ring. This function also initializes ring indices of each ring.
6749 * This function is called during the initialization of the SLI
6750 * interface of an HBA.
6751 * This function is called with no lock held and always returns
6752 * 1.
6753 **/
dea31012005-04-17 16:05:31 -05006754int
James Smart2e0fef82007-06-17 19:56:36 -05006755lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006756{
6757 struct lpfc_sli *psli;
6758 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006759 int i;
dea31012005-04-17 16:05:31 -05006760
6761 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006762 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006763 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006764 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006765 /* Initialize list headers for txq and txcmplq as double linked lists */
6766 for (i = 0; i < psli->num_rings; i++) {
6767 pring = &psli->ring[i];
6768 pring->ringno = i;
6769 pring->next_cmdidx = 0;
6770 pring->local_getidx = 0;
6771 pring->cmdidx = 0;
6772 INIT_LIST_HEAD(&pring->txq);
6773 INIT_LIST_HEAD(&pring->txcmplq);
6774 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006775 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006776 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006777 }
James Smart2e0fef82007-06-17 19:56:36 -05006778 spin_unlock_irq(&phba->hbalock);
6779 return 1;
dea31012005-04-17 16:05:31 -05006780}
6781
James Smarte59058c2008-08-24 21:49:00 -04006782/**
James Smart04c68492009-05-22 14:52:52 -04006783 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6784 * @phba: Pointer to HBA context object.
6785 *
6786 * This routine flushes the mailbox command subsystem. It will unconditionally
6787 * flush all the mailbox commands in the three possible stages in the mailbox
6788 * command sub-system: pending mailbox command queue; the outstanding mailbox
6789 * command; and completed mailbox command queue. It is caller's responsibility
6790 * to make sure that the driver is in the proper state to flush the mailbox
6791 * command sub-system. Namely, the posting of mailbox commands into the
6792 * pending mailbox command queue from the various clients must be stopped;
6793 * either the HBA is in a state that it will never works on the outstanding
6794 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6795 * mailbox command has been completed.
6796 **/
6797static void
6798lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6799{
6800 LIST_HEAD(completions);
6801 struct lpfc_sli *psli = &phba->sli;
6802 LPFC_MBOXQ_t *pmb;
6803 unsigned long iflag;
6804
6805 /* Flush all the mailbox commands in the mbox system */
6806 spin_lock_irqsave(&phba->hbalock, iflag);
6807 /* The pending mailbox command queue */
6808 list_splice_init(&phba->sli.mboxq, &completions);
6809 /* The outstanding active mailbox command */
6810 if (psli->mbox_active) {
6811 list_add_tail(&psli->mbox_active->list, &completions);
6812 psli->mbox_active = NULL;
6813 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6814 }
6815 /* The completed mailbox command queue */
6816 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6817 spin_unlock_irqrestore(&phba->hbalock, iflag);
6818
6819 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6820 while (!list_empty(&completions)) {
6821 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6822 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6823 if (pmb->mbox_cmpl)
6824 pmb->mbox_cmpl(phba, pmb);
6825 }
6826}
6827
6828/**
James Smart3621a712009-04-06 18:47:14 -04006829 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006830 * @vport: Pointer to virtual port object.
6831 *
6832 * lpfc_sli_host_down is called to clean up the resources
6833 * associated with a vport before destroying virtual
6834 * port data structures.
6835 * This function does following operations:
6836 * - Free discovery resources associated with this virtual
6837 * port.
6838 * - Free iocbs associated with this virtual port in
6839 * the txq.
6840 * - Send abort for all iocb commands associated with this
6841 * vport in txcmplq.
6842 *
6843 * This function is called with no lock held and always returns 1.
6844 **/
dea31012005-04-17 16:05:31 -05006845int
James Smart92d7f7b2007-06-17 19:56:38 -05006846lpfc_sli_host_down(struct lpfc_vport *vport)
6847{
James Smart858c9f62007-06-17 19:56:39 -05006848 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006849 struct lpfc_hba *phba = vport->phba;
6850 struct lpfc_sli *psli = &phba->sli;
6851 struct lpfc_sli_ring *pring;
6852 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006853 int i;
6854 unsigned long flags = 0;
6855 uint16_t prev_pring_flag;
6856
6857 lpfc_cleanup_discovery_resources(vport);
6858
6859 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006860 for (i = 0; i < psli->num_rings; i++) {
6861 pring = &psli->ring[i];
6862 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006863 /* Only slow rings */
6864 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006865 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006866 /* Set the lpfc data pending flag */
6867 set_bit(LPFC_DATA_READY, &phba->data_flags);
6868 }
James Smart92d7f7b2007-06-17 19:56:38 -05006869 /*
6870 * Error everything on the txq since these iocbs have not been
6871 * given to the FW yet.
6872 */
James Smart92d7f7b2007-06-17 19:56:38 -05006873 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6874 if (iocb->vport != vport)
6875 continue;
James Smart858c9f62007-06-17 19:56:39 -05006876 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006877 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006878 }
6879
6880 /* Next issue ABTS for everything on the txcmplq */
6881 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6882 list) {
6883 if (iocb->vport != vport)
6884 continue;
6885 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6886 }
6887
6888 pring->flag = prev_pring_flag;
6889 }
6890
6891 spin_unlock_irqrestore(&phba->hbalock, flags);
6892
James Smarta257bf92009-04-06 18:48:10 -04006893 /* Cancel all the IOCBs from the completions list */
6894 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6895 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006896 return 1;
6897}
6898
James Smarte59058c2008-08-24 21:49:00 -04006899/**
James Smart3621a712009-04-06 18:47:14 -04006900 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006901 * @phba: Pointer to HBA context object.
6902 *
6903 * This function cleans up all iocb, buffers, mailbox commands
6904 * while shutting down the HBA. This function is called with no
6905 * lock held and always returns 1.
6906 * This function does the following to cleanup driver resources:
6907 * - Free discovery resources for each virtual port
6908 * - Cleanup any pending fabric iocbs
6909 * - Iterate through the iocb txq and free each entry
6910 * in the list.
6911 * - Free up any buffer posted to the HBA
6912 * - Free mailbox commands in the mailbox queue.
6913 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006914int
James Smart2e0fef82007-06-17 19:56:36 -05006915lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006916{
James Smart2534ba72007-04-25 09:52:20 -04006917 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006918 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006919 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006920 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006921 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006922 int i;
6923
6924 /* Shutdown the mailbox command sub-system */
6925 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006926
dea31012005-04-17 16:05:31 -05006927 lpfc_hba_down_prep(phba);
6928
James Smart92d7f7b2007-06-17 19:56:38 -05006929 lpfc_fabric_abort_hba(phba);
6930
James Smart2e0fef82007-06-17 19:56:36 -05006931 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006932 for (i = 0; i < psli->num_rings; i++) {
6933 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006934 /* Only slow rings */
6935 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006936 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006937 /* Set the lpfc data pending flag */
6938 set_bit(LPFC_DATA_READY, &phba->data_flags);
6939 }
dea31012005-04-17 16:05:31 -05006940
6941 /*
6942 * Error everything on the txq since these iocbs have not been
6943 * given to the FW yet.
6944 */
James Smart2534ba72007-04-25 09:52:20 -04006945 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006946 pring->txq_cnt = 0;
6947
dea31012005-04-17 16:05:31 -05006948 }
James Smart2e0fef82007-06-17 19:56:36 -05006949 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006950
James Smarta257bf92009-04-06 18:48:10 -04006951 /* Cancel all the IOCBs from the completions list */
6952 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6953 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006954
James Smart0ff10d42008-01-11 01:52:36 -05006955 spin_lock_irqsave(&phba->hbalock, flags);
6956 list_splice_init(&phba->elsbuf, &completions);
6957 phba->elsbuf_cnt = 0;
6958 phba->elsbuf_prev_cnt = 0;
6959 spin_unlock_irqrestore(&phba->hbalock, flags);
6960
6961 while (!list_empty(&completions)) {
6962 list_remove_head(&completions, buf_ptr,
6963 struct lpfc_dmabuf, list);
6964 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6965 kfree(buf_ptr);
6966 }
6967
dea31012005-04-17 16:05:31 -05006968 /* Return any active mbox cmds */
6969 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006970
James Smartda0436e2009-05-22 14:51:39 -04006971 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006972 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006973 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006974
James Smartda0436e2009-05-22 14:51:39 -04006975 return 1;
6976}
James Smart92d7f7b2007-06-17 19:56:38 -05006977
James Smartda0436e2009-05-22 14:51:39 -04006978/**
James Smart3621a712009-04-06 18:47:14 -04006979 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006980 * @srcp: Source memory pointer.
6981 * @destp: Destination memory pointer.
6982 * @cnt: Number of words required to be copied.
6983 *
6984 * This function is used for copying data between driver memory
6985 * and the SLI memory. This function also changes the endianness
6986 * of each word if native endianness is different from SLI
6987 * endianness. This function can be called with or without
6988 * lock.
6989 **/
dea31012005-04-17 16:05:31 -05006990void
6991lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6992{
6993 uint32_t *src = srcp;
6994 uint32_t *dest = destp;
6995 uint32_t ldata;
6996 int i;
6997
6998 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6999 ldata = *src;
7000 ldata = le32_to_cpu(ldata);
7001 *dest = ldata;
7002 src++;
7003 dest++;
7004 }
7005}
7006
James Smarte59058c2008-08-24 21:49:00 -04007007
7008/**
James Smarta0c87cb2009-07-19 10:01:10 -04007009 * lpfc_sli_bemem_bcopy - SLI memory copy function
7010 * @srcp: Source memory pointer.
7011 * @destp: Destination memory pointer.
7012 * @cnt: Number of words required to be copied.
7013 *
7014 * This function is used for copying data between a data structure
7015 * with big endian representation to local endianness.
7016 * This function can be called with or without lock.
7017 **/
7018void
7019lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7020{
7021 uint32_t *src = srcp;
7022 uint32_t *dest = destp;
7023 uint32_t ldata;
7024 int i;
7025
7026 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7027 ldata = *src;
7028 ldata = be32_to_cpu(ldata);
7029 *dest = ldata;
7030 src++;
7031 dest++;
7032 }
7033}
7034
7035/**
James Smart3621a712009-04-06 18:47:14 -04007036 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04007037 * @phba: Pointer to HBA context object.
7038 * @pring: Pointer to driver SLI ring object.
7039 * @mp: Pointer to driver buffer object.
7040 *
7041 * This function is called with no lock held.
7042 * It always return zero after adding the buffer to the postbufq
7043 * buffer list.
7044 **/
dea31012005-04-17 16:05:31 -05007045int
James Smart2e0fef82007-06-17 19:56:36 -05007046lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7047 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007048{
7049 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7050 later */
James Smart2e0fef82007-06-17 19:56:36 -05007051 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007052 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007053 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007054 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007055 return 0;
7056}
7057
James Smarte59058c2008-08-24 21:49:00 -04007058/**
James Smart3621a712009-04-06 18:47:14 -04007059 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007060 * @phba: Pointer to HBA context object.
7061 *
7062 * When HBQ is enabled, buffers are searched based on tags. This function
7063 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7064 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7065 * does not conflict with tags of buffer posted for unsolicited events.
7066 * The function returns the allocated tag. The function is called with
7067 * no locks held.
7068 **/
James Smart76bb24e2007-10-27 13:38:00 -04007069uint32_t
7070lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7071{
7072 spin_lock_irq(&phba->hbalock);
7073 phba->buffer_tag_count++;
7074 /*
7075 * Always set the QUE_BUFTAG_BIT to distiguish between
7076 * a tag assigned by HBQ.
7077 */
7078 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7079 spin_unlock_irq(&phba->hbalock);
7080 return phba->buffer_tag_count;
7081}
7082
James Smarte59058c2008-08-24 21:49:00 -04007083/**
James Smart3621a712009-04-06 18:47:14 -04007084 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007085 * @phba: Pointer to HBA context object.
7086 * @pring: Pointer to driver SLI ring object.
7087 * @tag: Buffer tag.
7088 *
7089 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7090 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7091 * iocb is posted to the response ring with the tag of the buffer.
7092 * This function searches the pring->postbufq list using the tag
7093 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7094 * iocb. If the buffer is found then lpfc_dmabuf object of the
7095 * buffer is returned to the caller else NULL is returned.
7096 * This function is called with no lock held.
7097 **/
James Smart76bb24e2007-10-27 13:38:00 -04007098struct lpfc_dmabuf *
7099lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7100 uint32_t tag)
7101{
7102 struct lpfc_dmabuf *mp, *next_mp;
7103 struct list_head *slp = &pring->postbufq;
7104
7105 /* Search postbufq, from the begining, looking for a match on tag */
7106 spin_lock_irq(&phba->hbalock);
7107 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7108 if (mp->buffer_tag == tag) {
7109 list_del_init(&mp->list);
7110 pring->postbufq_cnt--;
7111 spin_unlock_irq(&phba->hbalock);
7112 return mp;
7113 }
7114 }
7115
7116 spin_unlock_irq(&phba->hbalock);
7117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007118 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007119 "ring %d Data x%lx x%p x%p x%x\n",
7120 pring->ringno, (unsigned long) tag,
7121 slp->next, slp->prev, pring->postbufq_cnt);
7122
7123 return NULL;
7124}
dea31012005-04-17 16:05:31 -05007125
James Smarte59058c2008-08-24 21:49:00 -04007126/**
James Smart3621a712009-04-06 18:47:14 -04007127 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007128 * @phba: Pointer to HBA context object.
7129 * @pring: Pointer to driver SLI ring object.
7130 * @phys: DMA address of the buffer.
7131 *
7132 * This function searches the buffer list using the dma_address
7133 * of unsolicited event to find the driver's lpfc_dmabuf object
7134 * corresponding to the dma_address. The function returns the
7135 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7136 * This function is called by the ct and els unsolicited event
7137 * handlers to get the buffer associated with the unsolicited
7138 * event.
7139 *
7140 * This function is called with no lock held.
7141 **/
dea31012005-04-17 16:05:31 -05007142struct lpfc_dmabuf *
7143lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7144 dma_addr_t phys)
7145{
7146 struct lpfc_dmabuf *mp, *next_mp;
7147 struct list_head *slp = &pring->postbufq;
7148
7149 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007150 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007151 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7152 if (mp->phys == phys) {
7153 list_del_init(&mp->list);
7154 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007155 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007156 return mp;
7157 }
7158 }
7159
James Smart2e0fef82007-06-17 19:56:36 -05007160 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007162 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007163 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007164 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007165 slp->next, slp->prev, pring->postbufq_cnt);
7166 return NULL;
7167}
7168
James Smarte59058c2008-08-24 21:49:00 -04007169/**
James Smart3621a712009-04-06 18:47:14 -04007170 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007171 * @phba: Pointer to HBA context object.
7172 * @cmdiocb: Pointer to driver command iocb object.
7173 * @rspiocb: Pointer to driver response iocb object.
7174 *
7175 * This function is the completion handler for the abort iocbs for
7176 * ELS commands. This function is called from the ELS ring event
7177 * handler with no lock held. This function frees memory resources
7178 * associated with the abort iocb.
7179 **/
dea31012005-04-17 16:05:31 -05007180static void
James Smart2e0fef82007-06-17 19:56:36 -05007181lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7182 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007183{
James Smart2e0fef82007-06-17 19:56:36 -05007184 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007185 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007186 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007187 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7188
7189 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007190
7191 if (irsp->ulpStatus) {
7192 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7193 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7194
James Smart2e0fef82007-06-17 19:56:36 -05007195 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007196 if (phba->sli_rev < LPFC_SLI_REV4) {
7197 if (abort_iotag != 0 &&
7198 abort_iotag <= phba->sli.last_iotag)
7199 abort_iocb =
7200 phba->sli.iocbq_lookup[abort_iotag];
7201 } else
7202 /* For sli4 the abort_tag is the XRI,
7203 * so the abort routine puts the iotag of the iocb
7204 * being aborted in the context field of the abort
7205 * IOCB.
7206 */
7207 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007208
James Smart2680eea2007-04-25 09:52:55 -04007209 /*
James Smart58da1ff2008-04-07 10:15:56 -04007210 * If the iocb is not found in Firmware queue the iocb
7211 * might have completed already. Do not free it again.
7212 */
James Smart9b379602008-04-07 10:16:00 -04007213 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007214 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7215 spin_unlock_irq(&phba->hbalock);
7216 lpfc_sli_release_iocbq(phba, cmdiocb);
7217 return;
7218 }
7219 /* For SLI4 the ulpContext field for abort IOCB
7220 * holds the iotag of the IOCB being aborted so
7221 * the local abort_context needs to be reset to
7222 * match the aborted IOCBs ulpContext.
7223 */
7224 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7225 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007226 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007227
7228 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7229 "0327 Cannot abort els iocb %p "
7230 "with tag %x context %x, abort status %x, "
7231 "abort code %x\n",
7232 abort_iocb, abort_iotag, abort_context,
7233 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007234 /*
James Smart2680eea2007-04-25 09:52:55 -04007235 * make sure we have the right iocbq before taking it
7236 * off the txcmplq and try to call completion routine.
7237 */
James Smart2e0fef82007-06-17 19:56:36 -05007238 if (!abort_iocb ||
7239 abort_iocb->iocb.ulpContext != abort_context ||
7240 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7241 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007242 else if (phba->sli_rev < LPFC_SLI_REV4) {
7243 /*
7244 * leave the SLI4 aborted command on the txcmplq
7245 * list and the command complete WCQE's XB bit
7246 * will tell whether the SGL (XRI) can be released
7247 * immediately or to the aborted SGL list for the
7248 * following abort XRI from the HBA.
7249 */
James Smart92d7f7b2007-06-17 19:56:38 -05007250 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007251 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7252 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7253 pring->txcmplq_cnt--;
7254 }
James Smart2680eea2007-04-25 09:52:55 -04007255
James Smart0ff10d42008-01-11 01:52:36 -05007256 /* Firmware could still be in progress of DMAing
7257 * payload, so don't free data buffer till after
7258 * a hbeat.
7259 */
7260 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007261 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007262 spin_unlock_irq(&phba->hbalock);
7263
James Smart92d7f7b2007-06-17 19:56:38 -05007264 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007265 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007266 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007267 } else
7268 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007269 }
7270
James Bottomley604a3e32005-10-29 10:28:33 -05007271 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007272 return;
7273}
7274
James Smarte59058c2008-08-24 21:49:00 -04007275/**
James Smart3621a712009-04-06 18:47:14 -04007276 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007277 * @phba: Pointer to HBA context object.
7278 * @cmdiocb: Pointer to driver command iocb object.
7279 * @rspiocb: Pointer to driver response iocb object.
7280 *
7281 * The function is called from SLI ring event handler with no
7282 * lock held. This function is the completion handler for ELS commands
7283 * which are aborted. The function frees memory resources used for
7284 * the aborted ELS commands.
7285 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007286static void
7287lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7288 struct lpfc_iocbq *rspiocb)
7289{
7290 IOCB_t *irsp = &rspiocb->iocb;
7291
7292 /* ELS cmd tag <ulpIoTag> completes */
7293 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007294 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007295 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007296 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007297 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007298 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7299 lpfc_ct_free_iocb(phba, cmdiocb);
7300 else
7301 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007302 return;
7303}
7304
James Smarte59058c2008-08-24 21:49:00 -04007305/**
James Smart5af5eee2010-10-22 11:06:38 -04007306 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007307 * @phba: Pointer to HBA context object.
7308 * @pring: Pointer to driver SLI ring object.
7309 * @cmdiocb: Pointer to driver command iocb object.
7310 *
James Smart5af5eee2010-10-22 11:06:38 -04007311 * This function issues an abort iocb for the provided command iocb down to
7312 * the port. Other than the case the outstanding command iocb is an abort
7313 * request, this function issues abort out unconditionally. This function is
7314 * called with hbalock held. The function returns 0 when it fails due to
7315 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007316 **/
James Smart5af5eee2010-10-22 11:06:38 -04007317static int
7318lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007319 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007320{
James Smart2e0fef82007-06-17 19:56:36 -05007321 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007322 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007323 IOCB_t *icmd = NULL;
7324 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007325 int retval;
James Smart07951072007-04-25 09:51:38 -04007326
James Smart92d7f7b2007-06-17 19:56:38 -05007327 /*
7328 * There are certain command types we don't want to abort. And we
7329 * don't want to abort commands that are already in the process of
7330 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007331 */
7332 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007333 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007334 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7335 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007336 return 0;
7337
dea31012005-04-17 16:05:31 -05007338 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007339 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007340 if (abtsiocbp == NULL)
7341 return 0;
dea31012005-04-17 16:05:31 -05007342
James Smart07951072007-04-25 09:51:38 -04007343 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007344 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007345 */
7346 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7347
dea31012005-04-17 16:05:31 -05007348 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007349 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7350 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007351 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007352 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007353 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7354 }
James Smartda0436e2009-05-22 14:51:39 -04007355 else
7356 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007357 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007358 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007359
James Smart5ffc2662009-11-18 15:39:44 -05007360 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7361 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007362 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7363 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007364
James Smart2e0fef82007-06-17 19:56:36 -05007365 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007366 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7367 else
7368 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7369
7370 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007371
James Smarte8b62012007-08-02 11:10:09 -04007372 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7373 "0339 Abort xri x%x, original iotag x%x, "
7374 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007375 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007376 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007377 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007378 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007379
James Smartd7c255b2008-08-24 21:50:00 -04007380 if (retval)
7381 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007382
7383 /*
7384 * Caller to this routine should check for IOCB_ERROR
7385 * and handle it properly. This routine no longer removes
7386 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7387 */
7388 return retval;
7389}
7390
7391/**
7392 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7393 * @phba: Pointer to HBA context object.
7394 * @pring: Pointer to driver SLI ring object.
7395 * @cmdiocb: Pointer to driver command iocb object.
7396 *
7397 * This function issues an abort iocb for the provided command iocb. In case
7398 * of unloading, the abort iocb will not be issued to commands on the ELS
7399 * ring. Instead, the callback function shall be changed to those commands
7400 * so that nothing happens when them finishes. This function is called with
7401 * hbalock held. The function returns 0 when the command iocb is an abort
7402 * request.
7403 **/
7404int
7405lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7406 struct lpfc_iocbq *cmdiocb)
7407{
7408 struct lpfc_vport *vport = cmdiocb->vport;
7409 int retval = IOCB_ERROR;
7410 IOCB_t *icmd = NULL;
7411
7412 /*
7413 * There are certain command types we don't want to abort. And we
7414 * don't want to abort commands that are already in the process of
7415 * being aborted.
7416 */
7417 icmd = &cmdiocb->iocb;
7418 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7419 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7420 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7421 return 0;
7422
7423 /*
7424 * If we're unloading, don't abort iocb on the ELS ring, but change
7425 * the callback so that nothing happens when it finishes.
7426 */
7427 if ((vport->load_flag & FC_UNLOADING) &&
7428 (pring->ringno == LPFC_ELS_RING)) {
7429 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7430 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7431 else
7432 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7433 goto abort_iotag_exit;
7434 }
7435
7436 /* Now, we try to issue the abort to the cmdiocb out */
7437 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7438
James Smart07951072007-04-25 09:51:38 -04007439abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007440 /*
7441 * Caller to this routine should check for IOCB_ERROR
7442 * and handle it properly. This routine no longer removes
7443 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007444 */
James Smart2e0fef82007-06-17 19:56:36 -05007445 return retval;
dea31012005-04-17 16:05:31 -05007446}
7447
James Smarte59058c2008-08-24 21:49:00 -04007448/**
James Smart5af5eee2010-10-22 11:06:38 -04007449 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7450 * @phba: Pointer to HBA context object.
7451 * @pring: Pointer to driver SLI ring object.
7452 *
7453 * This function aborts all iocbs in the given ring and frees all the iocb
7454 * objects in txq. This function issues abort iocbs unconditionally for all
7455 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7456 * to complete before the return of this function. The caller is not required
7457 * to hold any locks.
7458 **/
7459static void
7460lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7461{
7462 LIST_HEAD(completions);
7463 struct lpfc_iocbq *iocb, *next_iocb;
7464
7465 if (pring->ringno == LPFC_ELS_RING)
7466 lpfc_fabric_abort_hba(phba);
7467
7468 spin_lock_irq(&phba->hbalock);
7469
7470 /* Take off all the iocbs on txq for cancelling */
7471 list_splice_init(&pring->txq, &completions);
7472 pring->txq_cnt = 0;
7473
7474 /* Next issue ABTS for everything on the txcmplq */
7475 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7476 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7477
7478 spin_unlock_irq(&phba->hbalock);
7479
7480 /* Cancel all the IOCBs from the completions list */
7481 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7482 IOERR_SLI_ABORTED);
7483}
7484
7485/**
7486 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7487 * @phba: pointer to lpfc HBA data structure.
7488 *
7489 * This routine will abort all pending and outstanding iocbs to an HBA.
7490 **/
7491void
7492lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7493{
7494 struct lpfc_sli *psli = &phba->sli;
7495 struct lpfc_sli_ring *pring;
7496 int i;
7497
7498 for (i = 0; i < psli->num_rings; i++) {
7499 pring = &psli->ring[i];
7500 lpfc_sli_iocb_ring_abort(phba, pring);
7501 }
7502}
7503
7504/**
James Smart3621a712009-04-06 18:47:14 -04007505 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007506 * @iocbq: Pointer to driver iocb object.
7507 * @vport: Pointer to driver virtual port object.
7508 * @tgt_id: SCSI ID of the target.
7509 * @lun_id: LUN ID of the scsi device.
7510 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7511 *
James Smart3621a712009-04-06 18:47:14 -04007512 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007513 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7514 * 0 if the filtering criteria is met for the given iocb and will return
7515 * 1 if the filtering criteria is not met.
7516 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7517 * given iocb is for the SCSI device specified by vport, tgt_id and
7518 * lun_id parameter.
7519 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7520 * given iocb is for the SCSI target specified by vport and tgt_id
7521 * parameters.
7522 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7523 * given iocb is for the SCSI host associated with the given vport.
7524 * This function is called with no locks held.
7525 **/
dea31012005-04-17 16:05:31 -05007526static int
James Smart51ef4c22007-08-02 11:10:31 -04007527lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7528 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007529 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007530{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007531 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007532 int rc = 1;
7533
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007534 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7535 return rc;
7536
James Smart51ef4c22007-08-02 11:10:31 -04007537 if (iocbq->vport != vport)
7538 return rc;
7539
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007540 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007541
James Smart495a7142008-06-14 22:52:59 -04007542 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007543 return rc;
7544
7545 switch (ctx_cmd) {
7546 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007547 if ((lpfc_cmd->rdata->pnode) &&
7548 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7549 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007550 rc = 0;
7551 break;
7552 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007553 if ((lpfc_cmd->rdata->pnode) &&
7554 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007555 rc = 0;
7556 break;
dea31012005-04-17 16:05:31 -05007557 case LPFC_CTX_HOST:
7558 rc = 0;
7559 break;
7560 default:
7561 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007562 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007563 break;
7564 }
7565
7566 return rc;
7567}
7568
James Smarte59058c2008-08-24 21:49:00 -04007569/**
James Smart3621a712009-04-06 18:47:14 -04007570 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007571 * @vport: Pointer to virtual port.
7572 * @tgt_id: SCSI ID of the target.
7573 * @lun_id: LUN ID of the scsi device.
7574 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7575 *
7576 * This function returns number of FCP commands pending for the vport.
7577 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7578 * commands pending on the vport associated with SCSI device specified
7579 * by tgt_id and lun_id parameters.
7580 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7581 * commands pending on the vport associated with SCSI target specified
7582 * by tgt_id parameter.
7583 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7584 * commands pending on the vport.
7585 * This function returns the number of iocbs which satisfy the filter.
7586 * This function is called without any lock held.
7587 **/
dea31012005-04-17 16:05:31 -05007588int
James Smart51ef4c22007-08-02 11:10:31 -04007589lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7590 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007591{
James Smart51ef4c22007-08-02 11:10:31 -04007592 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007593 struct lpfc_iocbq *iocbq;
7594 int sum, i;
dea31012005-04-17 16:05:31 -05007595
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007596 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7597 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007598
James Smart51ef4c22007-08-02 11:10:31 -04007599 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7600 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007601 sum++;
dea31012005-04-17 16:05:31 -05007602 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007603
dea31012005-04-17 16:05:31 -05007604 return sum;
7605}
7606
James Smarte59058c2008-08-24 21:49:00 -04007607/**
James Smart3621a712009-04-06 18:47:14 -04007608 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007609 * @phba: Pointer to HBA context object
7610 * @cmdiocb: Pointer to command iocb object.
7611 * @rspiocb: Pointer to response iocb object.
7612 *
7613 * This function is called when an aborted FCP iocb completes. This
7614 * function is called by the ring event handler with no lock held.
7615 * This function frees the iocb.
7616 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007617void
James Smart2e0fef82007-06-17 19:56:36 -05007618lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7619 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007620{
James Bottomley604a3e32005-10-29 10:28:33 -05007621 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007622 return;
7623}
7624
James Smarte59058c2008-08-24 21:49:00 -04007625/**
James Smart3621a712009-04-06 18:47:14 -04007626 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007627 * @vport: Pointer to virtual port.
7628 * @pring: Pointer to driver SLI ring object.
7629 * @tgt_id: SCSI ID of the target.
7630 * @lun_id: LUN ID of the scsi device.
7631 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7632 *
7633 * This function sends an abort command for every SCSI command
7634 * associated with the given virtual port pending on the ring
7635 * filtered by lpfc_sli_validate_fcp_iocb function.
7636 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7637 * FCP iocbs associated with lun specified by tgt_id and lun_id
7638 * parameters
7639 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7640 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7641 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7642 * FCP iocbs associated with virtual port.
7643 * This function returns number of iocbs it failed to abort.
7644 * This function is called with no locks held.
7645 **/
dea31012005-04-17 16:05:31 -05007646int
James Smart51ef4c22007-08-02 11:10:31 -04007647lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7648 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007649{
James Smart51ef4c22007-08-02 11:10:31 -04007650 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007651 struct lpfc_iocbq *iocbq;
7652 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007653 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007654 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007655 int i;
dea31012005-04-17 16:05:31 -05007656
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007657 for (i = 1; i <= phba->sli.last_iotag; i++) {
7658 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007659
James Smart51ef4c22007-08-02 11:10:31 -04007660 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007661 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007662 continue;
7663
7664 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007665 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007666 if (abtsiocb == NULL) {
7667 errcnt++;
7668 continue;
7669 }
dea31012005-04-17 16:05:31 -05007670
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007671 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007672 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7673 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007674 if (phba->sli_rev == LPFC_SLI_REV4)
7675 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7676 else
7677 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007678 abtsiocb->iocb.ulpLe = 1;
7679 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007680 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007681
James Smart5ffc2662009-11-18 15:39:44 -05007682 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7683 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007684 if (iocbq->iocb_flag & LPFC_IO_FCP)
7685 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007686
James Smart2e0fef82007-06-17 19:56:36 -05007687 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007688 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7689 else
7690 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7691
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007692 /* Setup callback routine and issue the command. */
7693 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007694 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7695 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007696 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007697 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007698 errcnt++;
7699 continue;
7700 }
7701 }
7702
7703 return errcnt;
7704}
7705
James Smarte59058c2008-08-24 21:49:00 -04007706/**
James Smart3621a712009-04-06 18:47:14 -04007707 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007708 * @phba: Pointer to HBA context object.
7709 * @cmdiocbq: Pointer to command iocb.
7710 * @rspiocbq: Pointer to response iocb.
7711 *
7712 * This function is the completion handler for iocbs issued using
7713 * lpfc_sli_issue_iocb_wait function. This function is called by the
7714 * ring event handler function without any lock held. This function
7715 * can be called from both worker thread context and interrupt
7716 * context. This function also can be called from other thread which
7717 * cleans up the SLI layer objects.
7718 * This function copy the contents of the response iocb to the
7719 * response iocb memory object provided by the caller of
7720 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7721 * sleeps for the iocb completion.
7722 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007723static void
7724lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7725 struct lpfc_iocbq *cmdiocbq,
7726 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007727{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007728 wait_queue_head_t *pdone_q;
7729 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007730 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007731
James Smart2e0fef82007-06-17 19:56:36 -05007732 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007733 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7734 if (cmdiocbq->context2 && rspiocbq)
7735 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7736 &rspiocbq->iocb, sizeof(IOCB_t));
7737
James Smart0f65ff62010-02-26 14:14:23 -05007738 /* Set the exchange busy flag for task management commands */
7739 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7740 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7741 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7742 cur_iocbq);
7743 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7744 }
7745
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007746 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007747 if (pdone_q)
7748 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007749 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007750 return;
7751}
7752
James Smarte59058c2008-08-24 21:49:00 -04007753/**
James Smartd11e31d2009-06-10 17:23:06 -04007754 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7755 * @phba: Pointer to HBA context object..
7756 * @piocbq: Pointer to command iocb.
7757 * @flag: Flag to test.
7758 *
7759 * This routine grabs the hbalock and then test the iocb_flag to
7760 * see if the passed in flag is set.
7761 * Returns:
7762 * 1 if flag is set.
7763 * 0 if flag is not set.
7764 **/
7765static int
7766lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7767 struct lpfc_iocbq *piocbq, uint32_t flag)
7768{
7769 unsigned long iflags;
7770 int ret;
7771
7772 spin_lock_irqsave(&phba->hbalock, iflags);
7773 ret = piocbq->iocb_flag & flag;
7774 spin_unlock_irqrestore(&phba->hbalock, iflags);
7775 return ret;
7776
7777}
7778
7779/**
James Smart3621a712009-04-06 18:47:14 -04007780 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007781 * @phba: Pointer to HBA context object..
7782 * @pring: Pointer to sli ring.
7783 * @piocb: Pointer to command iocb.
7784 * @prspiocbq: Pointer to response iocb.
7785 * @timeout: Timeout in number of seconds.
7786 *
7787 * This function issues the iocb to firmware and waits for the
7788 * iocb to complete. If the iocb command is not
7789 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7790 * Caller should not free the iocb resources if this function
7791 * returns IOCB_TIMEDOUT.
7792 * The function waits for the iocb completion using an
7793 * non-interruptible wait.
7794 * This function will sleep while waiting for iocb completion.
7795 * So, this function should not be called from any context which
7796 * does not allow sleeping. Due to the same reason, this function
7797 * cannot be called with interrupt disabled.
7798 * This function assumes that the iocb completions occur while
7799 * this function sleep. So, this function cannot be called from
7800 * the thread which process iocb completion for this ring.
7801 * This function clears the iocb_flag of the iocb object before
7802 * issuing the iocb and the iocb completion handler sets this
7803 * flag and wakes this thread when the iocb completes.
7804 * The contents of the response iocb will be copied to prspiocbq
7805 * by the completion handler when the command completes.
7806 * This function returns IOCB_SUCCESS when success.
7807 * This function is called with no lock held.
7808 **/
dea31012005-04-17 16:05:31 -05007809int
James Smart2e0fef82007-06-17 19:56:36 -05007810lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007811 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007812 struct lpfc_iocbq *piocb,
7813 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007814 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007815{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007816 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007817 long timeleft, timeout_req = 0;
7818 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007819 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04007820 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05007821 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007822 * If the caller has provided a response iocbq buffer, then context2
7823 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007824 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007825 if (prspiocbq) {
7826 if (piocb->context2)
7827 return IOCB_ERROR;
7828 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007829 }
7830
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007831 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7832 piocb->context_un.wait_queue = &done_q;
7833 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007834
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007835 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7836 creg_val = readl(phba->HCregaddr);
7837 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7838 writel(creg_val, phba->HCregaddr);
7839 readl(phba->HCregaddr); /* flush */
7840 }
7841
James Smart2a9bf3d2010-06-07 15:24:45 -04007842 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7843 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007844 if (retval == IOCB_SUCCESS) {
7845 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007846 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007847 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007848 timeout_req);
dea31012005-04-17 16:05:31 -05007849
James Smart7054a602007-04-25 09:52:34 -04007850 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7851 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007852 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007853 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007854 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007855 "0338 IOCB wait timeout error - no "
7856 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007857 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007858 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007860 "0330 IOCB wake NOT set, "
7861 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007862 timeout, (timeleft / jiffies));
7863 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007864 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007865 } else if (retval == IOCB_BUSY) {
7866 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7867 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7868 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7869 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007870 } else {
7871 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007872 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007873 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007874 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007875 }
7876
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007877 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7878 creg_val = readl(phba->HCregaddr);
7879 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7880 writel(creg_val, phba->HCregaddr);
7881 readl(phba->HCregaddr); /* flush */
7882 }
7883
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007884 if (prspiocbq)
7885 piocb->context2 = NULL;
7886
7887 piocb->context_un.wait_queue = NULL;
7888 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007889 return retval;
7890}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007891
James Smarte59058c2008-08-24 21:49:00 -04007892/**
James Smart3621a712009-04-06 18:47:14 -04007893 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007894 * @phba: Pointer to HBA context object.
7895 * @pmboxq: Pointer to driver mailbox object.
7896 * @timeout: Timeout in number of seconds.
7897 *
7898 * This function issues the mailbox to firmware and waits for the
7899 * mailbox command to complete. If the mailbox command is not
7900 * completed within timeout seconds, it returns MBX_TIMEOUT.
7901 * The function waits for the mailbox completion using an
7902 * interruptible wait. If the thread is woken up due to a
7903 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7904 * should not free the mailbox resources, if this function returns
7905 * MBX_TIMEOUT.
7906 * This function will sleep while waiting for mailbox completion.
7907 * So, this function should not be called from any context which
7908 * does not allow sleeping. Due to the same reason, this function
7909 * cannot be called with interrupt disabled.
7910 * This function assumes that the mailbox completion occurs while
7911 * this function sleep. So, this function cannot be called from
7912 * the worker thread which processes mailbox completion.
7913 * This function is called in the context of HBA management
7914 * applications.
7915 * This function returns MBX_SUCCESS when successful.
7916 * This function is called with no lock held.
7917 **/
dea31012005-04-17 16:05:31 -05007918int
James Smart2e0fef82007-06-17 19:56:36 -05007919lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007920 uint32_t timeout)
7921{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007922 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007923 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007924 unsigned long flag;
dea31012005-04-17 16:05:31 -05007925
7926 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007927 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007928 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007929
James Smart495a7142008-06-14 22:52:59 -04007930 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007931 /* setup wake call as IOCB callback */
7932 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7933 /* setup context field to pass wait_queue pointer to wake function */
7934 pmboxq->context1 = &done_q;
7935
dea31012005-04-17 16:05:31 -05007936 /* now issue the command */
7937 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7938
7939 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007940 wait_event_interruptible_timeout(done_q,
7941 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7942 timeout * HZ);
7943
James Smart858c9f62007-06-17 19:56:39 -05007944 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007945 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007946 /*
7947 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7948 * else do not free the resources.
7949 */
James Smartd7c47992010-06-08 18:31:54 -04007950 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05007951 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04007952 lpfc_sli4_swap_str(phba, pmboxq);
7953 } else {
James Smart7054a602007-04-25 09:52:34 -04007954 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007955 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7956 }
7957 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007958 }
7959
dea31012005-04-17 16:05:31 -05007960 return retval;
7961}
7962
James Smarte59058c2008-08-24 21:49:00 -04007963/**
James Smart3772a992009-05-22 14:50:54 -04007964 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007965 * @phba: Pointer to HBA context.
7966 *
James Smart3772a992009-05-22 14:50:54 -04007967 * This function is called to shutdown the driver's mailbox sub-system.
7968 * It first marks the mailbox sub-system is in a block state to prevent
7969 * the asynchronous mailbox command from issued off the pending mailbox
7970 * command queue. If the mailbox command sub-system shutdown is due to
7971 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7972 * the mailbox sub-system flush routine to forcefully bring down the
7973 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7974 * as with offline or HBA function reset), this routine will wait for the
7975 * outstanding mailbox command to complete before invoking the mailbox
7976 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007977 **/
James Smart3772a992009-05-22 14:50:54 -04007978void
7979lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007980{
James Smart3772a992009-05-22 14:50:54 -04007981 struct lpfc_sli *psli = &phba->sli;
7982 uint8_t actcmd = MBX_HEARTBEAT;
7983 unsigned long timeout;
7984
7985 spin_lock_irq(&phba->hbalock);
7986 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7987 spin_unlock_irq(&phba->hbalock);
7988
7989 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7990 spin_lock_irq(&phba->hbalock);
7991 if (phba->sli.mbox_active)
7992 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7993 spin_unlock_irq(&phba->hbalock);
7994 /* Determine how long we might wait for the active mailbox
7995 * command to be gracefully completed by firmware.
7996 */
7997 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7998 1000) + jiffies;
7999 while (phba->sli.mbox_active) {
8000 /* Check active mailbox complete status every 2ms */
8001 msleep(2);
8002 if (time_after(jiffies, timeout))
8003 /* Timeout, let the mailbox flush routine to
8004 * forcefully release active mailbox command
8005 */
8006 break;
8007 }
8008 }
8009 lpfc_sli_mbox_sys_flush(phba);
8010}
8011
8012/**
8013 * lpfc_sli_eratt_read - read sli-3 error attention events
8014 * @phba: Pointer to HBA context.
8015 *
8016 * This function is called to read the SLI3 device error attention registers
8017 * for possible error attention events. The caller must hold the hostlock
8018 * with spin_lock_irq().
8019 *
8020 * This fucntion returns 1 when there is Error Attention in the Host Attention
8021 * Register and returns 0 otherwise.
8022 **/
8023static int
8024lpfc_sli_eratt_read(struct lpfc_hba *phba)
8025{
James Smarted957682007-06-17 19:56:37 -05008026 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04008027
James Smart3772a992009-05-22 14:50:54 -04008028 /* Read chip Host Attention (HA) register */
8029 ha_copy = readl(phba->HAregaddr);
8030 if (ha_copy & HA_ERATT) {
8031 /* Read host status register to retrieve error event */
8032 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04008033
James Smart3772a992009-05-22 14:50:54 -04008034 /* Check if there is a deferred error condition is active */
8035 if ((HS_FFER1 & phba->work_hs) &&
8036 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008037 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04008038 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04008039 /* Clear all interrupt enable conditions */
8040 writel(0, phba->HCregaddr);
8041 readl(phba->HCregaddr);
8042 }
8043
8044 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04008045 phba->work_ha |= HA_ERATT;
8046 /* Indicate polling handles this ERATT */
8047 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008048 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008049 }
James Smart3772a992009-05-22 14:50:54 -04008050 return 0;
James Smartb4c02652006-07-06 15:50:43 -04008051}
8052
James Smarte59058c2008-08-24 21:49:00 -04008053/**
James Smartda0436e2009-05-22 14:51:39 -04008054 * lpfc_sli4_eratt_read - read sli-4 error attention events
8055 * @phba: Pointer to HBA context.
8056 *
8057 * This function is called to read the SLI4 device error attention registers
8058 * for possible error attention events. The caller must hold the hostlock
8059 * with spin_lock_irq().
8060 *
8061 * This fucntion returns 1 when there is Error Attention in the Host Attention
8062 * Register and returns 0 otherwise.
8063 **/
8064static int
8065lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8066{
8067 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04008068
8069 /* For now, use the SLI4 device internal unrecoverable error
8070 * registers for error attention. This can be changed later.
8071 */
James Smarta747c9c2009-11-18 15:41:10 -05008072 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
8073 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
8074 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8075 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8077 "1423 HBA Unrecoverable error: "
8078 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8079 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
8080 uerr_sta_lo, uerr_sta_hi,
8081 phba->sli4_hba.ue_mask_lo,
8082 phba->sli4_hba.ue_mask_hi);
8083 phba->work_status[0] = uerr_sta_lo;
8084 phba->work_status[1] = uerr_sta_hi;
8085 /* Set the driver HA work bitmap */
8086 phba->work_ha |= HA_ERATT;
8087 /* Indicate polling handles this ERATT */
8088 phba->hba_flag |= HBA_ERATT_HANDLED;
8089 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008090 }
8091 return 0;
8092}
8093
8094/**
James Smart3621a712009-04-06 18:47:14 -04008095 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008096 * @phba: Pointer to HBA context.
8097 *
James Smart3772a992009-05-22 14:50:54 -04008098 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008099 * error attention register bit for error attention events.
8100 *
8101 * This fucntion returns 1 when there is Error Attention in the Host Attention
8102 * Register and returns 0 otherwise.
8103 **/
8104int
8105lpfc_sli_check_eratt(struct lpfc_hba *phba)
8106{
8107 uint32_t ha_copy;
8108
8109 /* If somebody is waiting to handle an eratt, don't process it
8110 * here. The brdkill function will do this.
8111 */
8112 if (phba->link_flag & LS_IGNORE_ERATT)
8113 return 0;
8114
8115 /* Check if interrupt handler handles this ERATT */
8116 spin_lock_irq(&phba->hbalock);
8117 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8118 /* Interrupt handler has handled ERATT */
8119 spin_unlock_irq(&phba->hbalock);
8120 return 0;
8121 }
8122
James Smarta257bf92009-04-06 18:48:10 -04008123 /*
8124 * If there is deferred error attention, do not check for error
8125 * attention
8126 */
8127 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8128 spin_unlock_irq(&phba->hbalock);
8129 return 0;
8130 }
8131
James Smart3772a992009-05-22 14:50:54 -04008132 /* If PCI channel is offline, don't process it */
8133 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008134 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008135 return 0;
8136 }
8137
8138 switch (phba->sli_rev) {
8139 case LPFC_SLI_REV2:
8140 case LPFC_SLI_REV3:
8141 /* Read chip Host Attention (HA) register */
8142 ha_copy = lpfc_sli_eratt_read(phba);
8143 break;
James Smartda0436e2009-05-22 14:51:39 -04008144 case LPFC_SLI_REV4:
8145 /* Read devcie Uncoverable Error (UERR) registers */
8146 ha_copy = lpfc_sli4_eratt_read(phba);
8147 break;
James Smart3772a992009-05-22 14:50:54 -04008148 default:
8149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8150 "0299 Invalid SLI revision (%d)\n",
8151 phba->sli_rev);
8152 ha_copy = 0;
8153 break;
James Smart93996272008-08-24 21:50:30 -04008154 }
8155 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008156
8157 return ha_copy;
8158}
8159
8160/**
8161 * lpfc_intr_state_check - Check device state for interrupt handling
8162 * @phba: Pointer to HBA context.
8163 *
8164 * This inline routine checks whether a device or its PCI slot is in a state
8165 * that the interrupt should be handled.
8166 *
8167 * This function returns 0 if the device or the PCI slot is in a state that
8168 * interrupt should be handled, otherwise -EIO.
8169 */
8170static inline int
8171lpfc_intr_state_check(struct lpfc_hba *phba)
8172{
8173 /* If the pci channel is offline, ignore all the interrupts */
8174 if (unlikely(pci_channel_offline(phba->pcidev)))
8175 return -EIO;
8176
8177 /* Update device level interrupt statistics */
8178 phba->sli.slistat.sli_intr++;
8179
8180 /* Ignore all interrupts during initialization. */
8181 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8182 return -EIO;
8183
James Smart93996272008-08-24 21:50:30 -04008184 return 0;
8185}
8186
8187/**
James Smart3772a992009-05-22 14:50:54 -04008188 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008189 * @irq: Interrupt number.
8190 * @dev_id: The device context pointer.
8191 *
James Smart93996272008-08-24 21:50:30 -04008192 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008193 * service routine when device with SLI-3 interface spec is enabled with
8194 * MSI-X multi-message interrupt mode and there are slow-path events in
8195 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8196 * interrupt mode, this function is called as part of the device-level
8197 * interrupt handler. When the PCI slot is in error recovery or the HBA
8198 * is undergoing initialization, the interrupt handler will not process
8199 * the interrupt. The link attention and ELS ring attention events are
8200 * handled by the worker thread. The interrupt handler signals the worker
8201 * thread and returns for these events. This function is called without
8202 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008203 * structures.
8204 *
8205 * This function returns IRQ_HANDLED when interrupt is handled else it
8206 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008207 **/
dea31012005-04-17 16:05:31 -05008208irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008209lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008210{
James Smart2e0fef82007-06-17 19:56:36 -05008211 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008212 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008213 uint32_t work_ha_copy;
8214 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008215 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008216 uint32_t control;
8217
James Smart92d7f7b2007-06-17 19:56:38 -05008218 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008219 struct lpfc_vport *vport;
8220 struct lpfc_nodelist *ndlp;
8221 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008222 LPFC_MBOXQ_t *pmb;
8223 int rc;
8224
dea31012005-04-17 16:05:31 -05008225 /*
8226 * Get the driver's phba structure from the dev_id and
8227 * assume the HBA is not interrupting.
8228 */
James Smart93996272008-08-24 21:50:30 -04008229 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008230
8231 if (unlikely(!phba))
8232 return IRQ_NONE;
8233
dea31012005-04-17 16:05:31 -05008234 /*
James Smart93996272008-08-24 21:50:30 -04008235 * Stuff needs to be attented to when this function is invoked as an
8236 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008237 */
James Smart93996272008-08-24 21:50:30 -04008238 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008239 /* Check device state for handling interrupt */
8240 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008241 return IRQ_NONE;
8242 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008243 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008244 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008245 /* If somebody is waiting to handle an eratt don't process it
8246 * here. The brdkill function will do this.
8247 */
8248 if (phba->link_flag & LS_IGNORE_ERATT)
8249 ha_copy &= ~HA_ERATT;
8250 /* Check the need for handling ERATT in interrupt handler */
8251 if (ha_copy & HA_ERATT) {
8252 if (phba->hba_flag & HBA_ERATT_HANDLED)
8253 /* ERATT polling has handled ERATT */
8254 ha_copy &= ~HA_ERATT;
8255 else
8256 /* Indicate interrupt handler handles ERATT */
8257 phba->hba_flag |= HBA_ERATT_HANDLED;
8258 }
James Smarta257bf92009-04-06 18:48:10 -04008259
8260 /*
8261 * If there is deferred error attention, do not check for any
8262 * interrupt.
8263 */
8264 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008265 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008266 return IRQ_NONE;
8267 }
8268
James Smart93996272008-08-24 21:50:30 -04008269 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008270 hc_copy = readl(phba->HCregaddr);
8271 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8272 HC_LAINT_ENA | HC_ERINT_ENA),
8273 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008274 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8275 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008276 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008277 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008278 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008279 } else
8280 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008281
dea31012005-04-17 16:05:31 -05008282 work_ha_copy = ha_copy & phba->work_ha_mask;
8283
James Smart93996272008-08-24 21:50:30 -04008284 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008285 if (work_ha_copy & HA_LATT) {
8286 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8287 /*
8288 * Turn off Link Attention interrupts
8289 * until CLEAR_LA done
8290 */
James Smart5b75da22008-12-04 22:39:35 -05008291 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008292 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8293 control = readl(phba->HCregaddr);
8294 control &= ~HC_LAINT_ENA;
8295 writel(control, phba->HCregaddr);
8296 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008297 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008298 }
8299 else
8300 work_ha_copy &= ~HA_LATT;
8301 }
8302
James Smart93996272008-08-24 21:50:30 -04008303 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008304 /*
8305 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8306 * the only slow ring.
8307 */
8308 status = (work_ha_copy &
8309 (HA_RXMASK << (4*LPFC_ELS_RING)));
8310 status >>= (4*LPFC_ELS_RING);
8311 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008312 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008313 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008314
8315 lpfc_debugfs_slow_ring_trc(phba,
8316 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8317 control, status,
8318 (uint32_t)phba->sli.slistat.sli_intr);
8319
James Smart858c9f62007-06-17 19:56:39 -05008320 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008321 lpfc_debugfs_slow_ring_trc(phba,
8322 "ISR Disable ring:"
8323 "pwork:x%x hawork:x%x wait:x%x",
8324 phba->work_ha, work_ha_copy,
8325 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008326 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008327
James Smart858c9f62007-06-17 19:56:39 -05008328 control &=
8329 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008330 writel(control, phba->HCregaddr);
8331 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008332 }
James Smarta58cbd52007-08-02 11:09:43 -04008333 else {
8334 lpfc_debugfs_slow_ring_trc(phba,
8335 "ISR slow ring: pwork:"
8336 "x%x hawork:x%x wait:x%x",
8337 phba->work_ha, work_ha_copy,
8338 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008339 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008340 }
James Smart5b75da22008-12-04 22:39:35 -05008341 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008342 }
8343 }
James Smart5b75da22008-12-04 22:39:35 -05008344 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008345 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008346 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008347 /*
8348 * Check if there is a deferred error condition
8349 * is active
8350 */
8351 if ((HS_FFER1 & phba->work_hs) &&
8352 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008353 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8354 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008355 phba->hba_flag |= DEFER_ERATT;
8356 /* Clear all interrupt enable conditions */
8357 writel(0, phba->HCregaddr);
8358 readl(phba->HCregaddr);
8359 }
8360 }
8361
James Smart93996272008-08-24 21:50:30 -04008362 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008363 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008364 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008365 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008366 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008367
8368 /* First check out the status word */
8369 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8370 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008371 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008372 /*
8373 * Stray Mailbox Interrupt, mbxCommand <cmd>
8374 * mbxStatus <status>
8375 */
James Smart09372822008-01-11 01:52:54 -05008376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008377 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008378 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008379 "Interrupt mbxCommand x%x "
8380 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008381 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008382 pmbox->mbxCommand,
8383 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008384 /* clear mailbox attention bit */
8385 work_ha_copy &= ~HA_MBATT;
8386 } else {
James Smart97eab632008-04-07 10:16:05 -04008387 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008388 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008389 phba->last_completion_time = jiffies;
8390 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008391 if (pmb->mbox_cmpl) {
8392 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8393 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008394 if (pmb->out_ext_byte_len &&
8395 pmb->context2)
8396 lpfc_sli_pcimem_bcopy(
8397 phba->mbox_ext,
8398 pmb->context2,
8399 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008400 }
James Smart09372822008-01-11 01:52:54 -05008401 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8402 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8403
8404 lpfc_debugfs_disc_trc(vport,
8405 LPFC_DISC_TRC_MBOX_VPORT,
8406 "MBOX dflt rpi: : "
8407 "status:x%x rpi:x%x",
8408 (uint32_t)pmbox->mbxStatus,
8409 pmbox->un.varWords[0], 0);
8410
8411 if (!pmbox->mbxStatus) {
8412 mp = (struct lpfc_dmabuf *)
8413 (pmb->context1);
8414 ndlp = (struct lpfc_nodelist *)
8415 pmb->context2;
8416
8417 /* Reg_LOGIN of dflt RPI was
8418 * successful. new lets get
8419 * rid of the RPI using the
8420 * same mbox buffer.
8421 */
8422 lpfc_unreg_login(phba,
8423 vport->vpi,
8424 pmbox->un.varWords[0],
8425 pmb);
8426 pmb->mbox_cmpl =
8427 lpfc_mbx_cmpl_dflt_rpi;
8428 pmb->context1 = mp;
8429 pmb->context2 = ndlp;
8430 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008431 rc = lpfc_sli_issue_mbox(phba,
8432 pmb,
8433 MBX_NOWAIT);
8434 if (rc != MBX_BUSY)
8435 lpfc_printf_log(phba,
8436 KERN_ERR,
8437 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008438 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008439 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008440 if (rc != MBX_NOT_FINISHED)
8441 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008442 }
8443 }
James Smart5b75da22008-12-04 22:39:35 -05008444 spin_lock_irqsave(
8445 &phba->pport->work_port_lock,
8446 iflag);
James Smart09372822008-01-11 01:52:54 -05008447 phba->pport->work_port_events &=
8448 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008449 spin_unlock_irqrestore(
8450 &phba->pport->work_port_lock,
8451 iflag);
James Smart09372822008-01-11 01:52:54 -05008452 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008453 }
James Smart97eab632008-04-07 10:16:05 -04008454 } else
James Smart5b75da22008-12-04 22:39:35 -05008455 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008456
James Smart92d7f7b2007-06-17 19:56:38 -05008457 if ((work_ha_copy & HA_MBATT) &&
8458 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008459send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008460 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008461 do {
8462 rc = lpfc_sli_issue_mbox(phba, NULL,
8463 MBX_NOWAIT);
8464 } while (rc == MBX_NOT_FINISHED);
8465 if (rc != MBX_SUCCESS)
8466 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8467 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008468 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008469 }
8470
James Smart5b75da22008-12-04 22:39:35 -05008471 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008472 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008473 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008474 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008475 }
James Smart93996272008-08-24 21:50:30 -04008476 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008477
James Smart3772a992009-05-22 14:50:54 -04008478} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008479
8480/**
James Smart3772a992009-05-22 14:50:54 -04008481 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008482 * @irq: Interrupt number.
8483 * @dev_id: The device context pointer.
8484 *
8485 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008486 * service routine when device with SLI-3 interface spec is enabled with
8487 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8488 * ring event in the HBA. However, when the device is enabled with either
8489 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8490 * device-level interrupt handler. When the PCI slot is in error recovery
8491 * or the HBA is undergoing initialization, the interrupt handler will not
8492 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8493 * the intrrupt context. This function is called without any lock held.
8494 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008495 *
8496 * This function returns IRQ_HANDLED when interrupt is handled else it
8497 * returns IRQ_NONE.
8498 **/
8499irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008500lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008501{
8502 struct lpfc_hba *phba;
8503 uint32_t ha_copy;
8504 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008505 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008506
8507 /* Get the driver's phba structure from the dev_id and
8508 * assume the HBA is not interrupting.
8509 */
8510 phba = (struct lpfc_hba *) dev_id;
8511
8512 if (unlikely(!phba))
8513 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008514
8515 /*
James Smart93996272008-08-24 21:50:30 -04008516 * Stuff needs to be attented to when this function is invoked as an
8517 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008518 */
James Smart93996272008-08-24 21:50:30 -04008519 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008520 /* Check device state for handling interrupt */
8521 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008522 return IRQ_NONE;
8523 /* Need to read HA REG for FCP ring and other ring events */
8524 ha_copy = readl(phba->HAregaddr);
8525 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008526 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008527 /*
8528 * If there is deferred error attention, do not check for
8529 * any interrupt.
8530 */
8531 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008532 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008533 return IRQ_NONE;
8534 }
James Smart93996272008-08-24 21:50:30 -04008535 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8536 phba->HAregaddr);
8537 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008538 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008539 } else
8540 ha_copy = phba->ha_copy;
8541
8542 /*
8543 * Process all events on FCP ring. Take the optimized path for FCP IO.
8544 */
8545 ha_copy &= ~(phba->work_ha_mask);
8546
8547 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008548 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008549 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008550 lpfc_sli_handle_fast_ring_event(phba,
8551 &phba->sli.ring[LPFC_FCP_RING],
8552 status);
James Smarta4bc3372006-12-02 13:34:16 -05008553
8554 if (phba->cfg_multi_ring_support == 2) {
8555 /*
James Smart93996272008-08-24 21:50:30 -04008556 * Process all events on extra ring. Take the optimized path
8557 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008558 */
James Smart93996272008-08-24 21:50:30 -04008559 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008560 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008561 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008562 lpfc_sli_handle_fast_ring_event(phba,
8563 &phba->sli.ring[LPFC_EXTRA_RING],
8564 status);
8565 }
8566 }
dea31012005-04-17 16:05:31 -05008567 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008568} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008569
James Smart93996272008-08-24 21:50:30 -04008570/**
James Smart3772a992009-05-22 14:50:54 -04008571 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008572 * @irq: Interrupt number.
8573 * @dev_id: The device context pointer.
8574 *
James Smart3772a992009-05-22 14:50:54 -04008575 * This function is the HBA device-level interrupt handler to device with
8576 * SLI-3 interface spec, called from the PCI layer when either MSI or
8577 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8578 * requires driver attention. This function invokes the slow-path interrupt
8579 * attention handling function and fast-path interrupt attention handling
8580 * function in turn to process the relevant HBA attention events. This
8581 * function is called without any lock held. It gets the hbalock to access
8582 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008583 *
8584 * This function returns IRQ_HANDLED when interrupt is handled, else it
8585 * returns IRQ_NONE.
8586 **/
8587irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008588lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008589{
8590 struct lpfc_hba *phba;
8591 irqreturn_t sp_irq_rc, fp_irq_rc;
8592 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008593 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008594
8595 /*
8596 * Get the driver's phba structure from the dev_id and
8597 * assume the HBA is not interrupting.
8598 */
8599 phba = (struct lpfc_hba *) dev_id;
8600
8601 if (unlikely(!phba))
8602 return IRQ_NONE;
8603
James Smart3772a992009-05-22 14:50:54 -04008604 /* Check device state for handling interrupt */
8605 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008606 return IRQ_NONE;
8607
8608 spin_lock(&phba->hbalock);
8609 phba->ha_copy = readl(phba->HAregaddr);
8610 if (unlikely(!phba->ha_copy)) {
8611 spin_unlock(&phba->hbalock);
8612 return IRQ_NONE;
8613 } else if (phba->ha_copy & HA_ERATT) {
8614 if (phba->hba_flag & HBA_ERATT_HANDLED)
8615 /* ERATT polling has handled ERATT */
8616 phba->ha_copy &= ~HA_ERATT;
8617 else
8618 /* Indicate interrupt handler handles ERATT */
8619 phba->hba_flag |= HBA_ERATT_HANDLED;
8620 }
8621
James Smarta257bf92009-04-06 18:48:10 -04008622 /*
8623 * If there is deferred error attention, do not check for any interrupt.
8624 */
8625 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02008626 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04008627 return IRQ_NONE;
8628 }
8629
James Smart93996272008-08-24 21:50:30 -04008630 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008631 hc_copy = readl(phba->HCregaddr);
8632 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8633 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8634 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008635 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008636 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008637 readl(phba->HAregaddr); /* flush */
8638 spin_unlock(&phba->hbalock);
8639
8640 /*
8641 * Invokes slow-path host attention interrupt handling as appropriate.
8642 */
8643
8644 /* status of events with mailbox and link attention */
8645 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8646
8647 /* status of events with ELS ring */
8648 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8649 status2 >>= (4*LPFC_ELS_RING);
8650
8651 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008652 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008653 else
8654 sp_irq_rc = IRQ_NONE;
8655
8656 /*
8657 * Invoke fast-path host attention interrupt handling as appropriate.
8658 */
8659
8660 /* status of events with FCP ring */
8661 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8662 status1 >>= (4*LPFC_FCP_RING);
8663
8664 /* status of events with extra ring */
8665 if (phba->cfg_multi_ring_support == 2) {
8666 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8667 status2 >>= (4*LPFC_EXTRA_RING);
8668 } else
8669 status2 = 0;
8670
8671 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008672 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008673 else
8674 fp_irq_rc = IRQ_NONE;
8675
8676 /* Return device-level interrupt handling status */
8677 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008678} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008679
8680/**
8681 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8682 * @phba: pointer to lpfc hba data structure.
8683 *
8684 * This routine is invoked by the worker thread to process all the pending
8685 * SLI4 FCP abort XRI events.
8686 **/
8687void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8688{
8689 struct lpfc_cq_event *cq_event;
8690
8691 /* First, declare the fcp xri abort event has been handled */
8692 spin_lock_irq(&phba->hbalock);
8693 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8694 spin_unlock_irq(&phba->hbalock);
8695 /* Now, handle all the fcp xri abort events */
8696 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8697 /* Get the first event from the head of the event queue */
8698 spin_lock_irq(&phba->hbalock);
8699 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8700 cq_event, struct lpfc_cq_event, list);
8701 spin_unlock_irq(&phba->hbalock);
8702 /* Notify aborted XRI for FCP work queue */
8703 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8704 /* Free the event processed back to the free pool */
8705 lpfc_sli4_cq_event_release(phba, cq_event);
8706 }
8707}
8708
8709/**
8710 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8711 * @phba: pointer to lpfc hba data structure.
8712 *
8713 * This routine is invoked by the worker thread to process all the pending
8714 * SLI4 els abort xri events.
8715 **/
8716void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8717{
8718 struct lpfc_cq_event *cq_event;
8719
8720 /* First, declare the els xri abort event has been handled */
8721 spin_lock_irq(&phba->hbalock);
8722 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8723 spin_unlock_irq(&phba->hbalock);
8724 /* Now, handle all the els xri abort events */
8725 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8726 /* Get the first event from the head of the event queue */
8727 spin_lock_irq(&phba->hbalock);
8728 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8729 cq_event, struct lpfc_cq_event, list);
8730 spin_unlock_irq(&phba->hbalock);
8731 /* Notify aborted XRI for ELS work queue */
8732 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8733 /* Free the event processed back to the free pool */
8734 lpfc_sli4_cq_event_release(phba, cq_event);
8735 }
8736}
8737
James Smart341af102010-01-26 23:07:37 -05008738/**
8739 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8740 * @phba: pointer to lpfc hba data structure
8741 * @pIocbIn: pointer to the rspiocbq
8742 * @pIocbOut: pointer to the cmdiocbq
8743 * @wcqe: pointer to the complete wcqe
8744 *
8745 * This routine transfers the fields of a command iocbq to a response iocbq
8746 * by copying all the IOCB fields from command iocbq and transferring the
8747 * completion status information from the complete wcqe.
8748 **/
James Smart4f774512009-05-22 14:52:35 -04008749static void
James Smart341af102010-01-26 23:07:37 -05008750lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8751 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008752 struct lpfc_iocbq *pIocbOut,
8753 struct lpfc_wcqe_complete *wcqe)
8754{
James Smart341af102010-01-26 23:07:37 -05008755 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008756 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8757
8758 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8759 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008760 /* Map WCQE parameters into irspiocb parameters */
8761 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8762 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8763 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8764 pIocbIn->iocb.un.fcpi.fcpi_parm =
8765 pIocbOut->iocb.un.fcpi.fcpi_parm -
8766 wcqe->total_data_placed;
8767 else
8768 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008769 else {
James Smart4f774512009-05-22 14:52:35 -04008770 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008771 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8772 }
James Smart341af102010-01-26 23:07:37 -05008773
8774 /* Pick up HBA exchange busy condition */
8775 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8776 spin_lock_irqsave(&phba->hbalock, iflags);
8777 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8778 spin_unlock_irqrestore(&phba->hbalock, iflags);
8779 }
James Smart4f774512009-05-22 14:52:35 -04008780}
8781
8782/**
James Smart45ed1192009-10-02 15:17:02 -04008783 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8784 * @phba: Pointer to HBA context object.
8785 * @wcqe: Pointer to work-queue completion queue entry.
8786 *
8787 * This routine handles an ELS work-queue completion event and construct
8788 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8789 * discovery engine to handle.
8790 *
8791 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8792 **/
8793static struct lpfc_iocbq *
8794lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8795 struct lpfc_iocbq *irspiocbq)
8796{
8797 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8798 struct lpfc_iocbq *cmdiocbq;
8799 struct lpfc_wcqe_complete *wcqe;
8800 unsigned long iflags;
8801
8802 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8803 spin_lock_irqsave(&phba->hbalock, iflags);
8804 pring->stats.iocb_event++;
8805 /* Look up the ELS command IOCB and create pseudo response IOCB */
8806 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8807 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8808 spin_unlock_irqrestore(&phba->hbalock, iflags);
8809
8810 if (unlikely(!cmdiocbq)) {
8811 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8812 "0386 ELS complete with no corresponding "
8813 "cmdiocb: iotag (%d)\n",
8814 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8815 lpfc_sli_release_iocbq(phba, irspiocbq);
8816 return NULL;
8817 }
8818
8819 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008820 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008821
8822 return irspiocbq;
8823}
8824
8825/**
James Smart04c68492009-05-22 14:52:52 -04008826 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8827 * @phba: Pointer to HBA context object.
8828 * @cqe: Pointer to mailbox completion queue entry.
8829 *
8830 * This routine process a mailbox completion queue entry with asynchrous
8831 * event.
8832 *
8833 * Return: true if work posted to worker thread, otherwise false.
8834 **/
8835static bool
8836lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8837{
8838 struct lpfc_cq_event *cq_event;
8839 unsigned long iflags;
8840
8841 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8842 "0392 Async Event: word0:x%x, word1:x%x, "
8843 "word2:x%x, word3:x%x\n", mcqe->word0,
8844 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8845
8846 /* Allocate a new internal CQ_EVENT entry */
8847 cq_event = lpfc_sli4_cq_event_alloc(phba);
8848 if (!cq_event) {
8849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8850 "0394 Failed to allocate CQ_EVENT entry\n");
8851 return false;
8852 }
8853
8854 /* Move the CQE into an asynchronous event entry */
8855 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8856 spin_lock_irqsave(&phba->hbalock, iflags);
8857 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8858 /* Set the async event flag */
8859 phba->hba_flag |= ASYNC_EVENT;
8860 spin_unlock_irqrestore(&phba->hbalock, iflags);
8861
8862 return true;
8863}
8864
8865/**
8866 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8867 * @phba: Pointer to HBA context object.
8868 * @cqe: Pointer to mailbox completion queue entry.
8869 *
8870 * This routine process a mailbox completion queue entry with mailbox
8871 * completion event.
8872 *
8873 * Return: true if work posted to worker thread, otherwise false.
8874 **/
8875static bool
8876lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8877{
8878 uint32_t mcqe_status;
8879 MAILBOX_t *mbox, *pmbox;
8880 struct lpfc_mqe *mqe;
8881 struct lpfc_vport *vport;
8882 struct lpfc_nodelist *ndlp;
8883 struct lpfc_dmabuf *mp;
8884 unsigned long iflags;
8885 LPFC_MBOXQ_t *pmb;
8886 bool workposted = false;
8887 int rc;
8888
8889 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8890 if (!bf_get(lpfc_trailer_completed, mcqe))
8891 goto out_no_mqe_complete;
8892
8893 /* Get the reference to the active mbox command */
8894 spin_lock_irqsave(&phba->hbalock, iflags);
8895 pmb = phba->sli.mbox_active;
8896 if (unlikely(!pmb)) {
8897 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8898 "1832 No pending MBOX command to handle\n");
8899 spin_unlock_irqrestore(&phba->hbalock, iflags);
8900 goto out_no_mqe_complete;
8901 }
8902 spin_unlock_irqrestore(&phba->hbalock, iflags);
8903 mqe = &pmb->u.mqe;
8904 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8905 mbox = phba->mbox;
8906 vport = pmb->vport;
8907
8908 /* Reset heartbeat timer */
8909 phba->last_completion_time = jiffies;
8910 del_timer(&phba->sli.mbox_tmo);
8911
8912 /* Move mbox data to caller's mailbox region, do endian swapping */
8913 if (pmb->mbox_cmpl && mbox)
8914 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8915 /* Set the mailbox status with SLI4 range 0x4000 */
8916 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8917 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8918 bf_set(lpfc_mqe_status, mqe,
8919 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8920
8921 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8922 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8923 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8924 "MBOX dflt rpi: status:x%x rpi:x%x",
8925 mcqe_status,
8926 pmbox->un.varWords[0], 0);
8927 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8928 mp = (struct lpfc_dmabuf *)(pmb->context1);
8929 ndlp = (struct lpfc_nodelist *)pmb->context2;
8930 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8931 * RID of the PPI using the same mbox buffer.
8932 */
8933 lpfc_unreg_login(phba, vport->vpi,
8934 pmbox->un.varWords[0], pmb);
8935 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8936 pmb->context1 = mp;
8937 pmb->context2 = ndlp;
8938 pmb->vport = vport;
8939 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8940 if (rc != MBX_BUSY)
8941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8942 LOG_SLI, "0385 rc should "
8943 "have been MBX_BUSY\n");
8944 if (rc != MBX_NOT_FINISHED)
8945 goto send_current_mbox;
8946 }
8947 }
8948 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8949 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8950 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8951
8952 /* There is mailbox completion work to do */
8953 spin_lock_irqsave(&phba->hbalock, iflags);
8954 __lpfc_mbox_cmpl_put(phba, pmb);
8955 phba->work_ha |= HA_MBATT;
8956 spin_unlock_irqrestore(&phba->hbalock, iflags);
8957 workposted = true;
8958
8959send_current_mbox:
8960 spin_lock_irqsave(&phba->hbalock, iflags);
8961 /* Release the mailbox command posting token */
8962 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8963 /* Setting active mailbox pointer need to be in sync to flag clear */
8964 phba->sli.mbox_active = NULL;
8965 spin_unlock_irqrestore(&phba->hbalock, iflags);
8966 /* Wake up worker thread to post the next pending mailbox command */
8967 lpfc_worker_wake_up(phba);
8968out_no_mqe_complete:
8969 if (bf_get(lpfc_trailer_consumed, mcqe))
8970 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8971 return workposted;
8972}
8973
8974/**
8975 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8976 * @phba: Pointer to HBA context object.
8977 * @cqe: Pointer to mailbox completion queue entry.
8978 *
8979 * This routine process a mailbox completion queue entry, it invokes the
8980 * proper mailbox complete handling or asynchrous event handling routine
8981 * according to the MCQE's async bit.
8982 *
8983 * Return: true if work posted to worker thread, otherwise false.
8984 **/
8985static bool
8986lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8987{
8988 struct lpfc_mcqe mcqe;
8989 bool workposted;
8990
8991 /* Copy the mailbox MCQE and convert endian order as needed */
8992 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8993
8994 /* Invoke the proper event handling routine */
8995 if (!bf_get(lpfc_trailer_async, &mcqe))
8996 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8997 else
8998 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8999 return workposted;
9000}
9001
9002/**
James Smart4f774512009-05-22 14:52:35 -04009003 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9004 * @phba: Pointer to HBA context object.
9005 * @wcqe: Pointer to work-queue completion queue entry.
9006 *
9007 * This routine handles an ELS work-queue completion event.
9008 *
9009 * Return: true if work posted to worker thread, otherwise false.
9010 **/
9011static bool
9012lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9013 struct lpfc_wcqe_complete *wcqe)
9014{
James Smart4f774512009-05-22 14:52:35 -04009015 struct lpfc_iocbq *irspiocbq;
9016 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04009017 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04009018
James Smart45ed1192009-10-02 15:17:02 -04009019 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04009020 irspiocbq = lpfc_sli_get_iocbq(phba);
9021 if (!irspiocbq) {
9022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04009023 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9024 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9025 pring->txq_cnt, phba->iocb_cnt,
9026 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9027 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04009028 return false;
James Smart4f774512009-05-22 14:52:35 -04009029 }
James Smart4f774512009-05-22 14:52:35 -04009030
James Smart45ed1192009-10-02 15:17:02 -04009031 /* Save off the slow-path queue event for work thread to process */
9032 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04009033 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04009034 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009035 &phba->sli4_hba.sp_queue_event);
9036 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04009037 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04009038
James Smart45ed1192009-10-02 15:17:02 -04009039 return true;
James Smart4f774512009-05-22 14:52:35 -04009040}
9041
9042/**
9043 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9044 * @phba: Pointer to HBA context object.
9045 * @wcqe: Pointer to work-queue completion queue entry.
9046 *
9047 * This routine handles slow-path WQ entry comsumed event by invoking the
9048 * proper WQ release routine to the slow-path WQ.
9049 **/
9050static void
9051lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9052 struct lpfc_wcqe_release *wcqe)
9053{
9054 /* Check for the slow-path ELS work queue */
9055 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9056 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9057 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9058 else
9059 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9060 "2579 Slow-path wqe consume event carries "
9061 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9062 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9063 phba->sli4_hba.els_wq->queue_id);
9064}
9065
9066/**
9067 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9068 * @phba: Pointer to HBA context object.
9069 * @cq: Pointer to a WQ completion queue.
9070 * @wcqe: Pointer to work-queue completion queue entry.
9071 *
9072 * This routine handles an XRI abort event.
9073 *
9074 * Return: true if work posted to worker thread, otherwise false.
9075 **/
9076static bool
9077lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9078 struct lpfc_queue *cq,
9079 struct sli4_wcqe_xri_aborted *wcqe)
9080{
9081 bool workposted = false;
9082 struct lpfc_cq_event *cq_event;
9083 unsigned long iflags;
9084
9085 /* Allocate a new internal CQ_EVENT entry */
9086 cq_event = lpfc_sli4_cq_event_alloc(phba);
9087 if (!cq_event) {
9088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9089 "0602 Failed to allocate CQ_EVENT entry\n");
9090 return false;
9091 }
9092
9093 /* Move the CQE into the proper xri abort event list */
9094 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9095 switch (cq->subtype) {
9096 case LPFC_FCP:
9097 spin_lock_irqsave(&phba->hbalock, iflags);
9098 list_add_tail(&cq_event->list,
9099 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9100 /* Set the fcp xri abort event flag */
9101 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9102 spin_unlock_irqrestore(&phba->hbalock, iflags);
9103 workposted = true;
9104 break;
9105 case LPFC_ELS:
9106 spin_lock_irqsave(&phba->hbalock, iflags);
9107 list_add_tail(&cq_event->list,
9108 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9109 /* Set the els xri abort event flag */
9110 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9111 spin_unlock_irqrestore(&phba->hbalock, iflags);
9112 workposted = true;
9113 break;
9114 default:
9115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9116 "0603 Invalid work queue CQE subtype (x%x)\n",
9117 cq->subtype);
9118 workposted = false;
9119 break;
9120 }
9121 return workposted;
9122}
9123
9124/**
James Smart4d9ab992009-10-02 15:16:39 -04009125 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009126 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009127 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009128 *
James Smart4d9ab992009-10-02 15:16:39 -04009129 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009130 *
9131 * Return: true if work posted to worker thread, otherwise false.
9132 **/
9133static bool
James Smart4d9ab992009-10-02 15:16:39 -04009134lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9135{
9136 bool workposted = false;
9137 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9138 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9139 struct hbq_dmabuf *dma_buf;
9140 uint32_t status;
9141 unsigned long iflags;
9142
James Smart4d9ab992009-10-02 15:16:39 -04009143 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9144 goto out;
9145
9146 status = bf_get(lpfc_rcqe_status, rcqe);
9147 switch (status) {
9148 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9150 "2537 Receive Frame Truncated!!\n");
9151 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009152 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009153 spin_lock_irqsave(&phba->hbalock, iflags);
9154 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9155 if (!dma_buf) {
9156 spin_unlock_irqrestore(&phba->hbalock, iflags);
9157 goto out;
9158 }
9159 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9160 /* save off the frame for the word thread to process */
9161 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009162 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009163 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009164 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009165 spin_unlock_irqrestore(&phba->hbalock, iflags);
9166 workposted = true;
9167 break;
9168 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9169 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9170 /* Post more buffers if possible */
9171 spin_lock_irqsave(&phba->hbalock, iflags);
9172 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9173 spin_unlock_irqrestore(&phba->hbalock, iflags);
9174 workposted = true;
9175 break;
9176 }
9177out:
9178 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009179}
9180
9181/**
9182 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9183 * @phba: Pointer to HBA context object.
9184 * @cq: Pointer to the completion queue.
9185 * @wcqe: Pointer to a completion queue entry.
9186 *
9187 * This routine process a slow-path work-queue or recieve queue completion queue
9188 * entry.
9189 *
9190 * Return: true if work posted to worker thread, otherwise false.
9191 **/
9192static bool
9193lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009194 struct lpfc_cqe *cqe)
9195{
James Smart45ed1192009-10-02 15:17:02 -04009196 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009197 bool workposted = false;
9198
9199 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009200 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009201
9202 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009203 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009204 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009205 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009206 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009207 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009208 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009209 break;
9210 case CQE_CODE_RELEASE_WQE:
9211 /* Process the WQ release event */
9212 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009213 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009214 break;
9215 case CQE_CODE_XRI_ABORTED:
9216 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009217 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009218 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009219 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009220 break;
James Smart4d9ab992009-10-02 15:16:39 -04009221 case CQE_CODE_RECEIVE:
9222 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009223 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009224 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009225 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009226 break;
James Smart4f774512009-05-22 14:52:35 -04009227 default:
9228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9229 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009230 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009231 break;
9232 }
9233 return workposted;
9234}
9235
9236/**
James Smart4f774512009-05-22 14:52:35 -04009237 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9238 * @phba: Pointer to HBA context object.
9239 * @eqe: Pointer to fast-path event queue entry.
9240 *
9241 * This routine process a event queue entry from the slow-path event queue.
9242 * It will check the MajorCode and MinorCode to determine this is for a
9243 * completion event on a completion queue, if not, an error shall be logged
9244 * and just return. Otherwise, it will get to the corresponding completion
9245 * queue and process all the entries on that completion queue, rearm the
9246 * completion queue, and then return.
9247 *
9248 **/
9249static void
9250lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9251{
9252 struct lpfc_queue *cq = NULL, *childq, *speq;
9253 struct lpfc_cqe *cqe;
9254 bool workposted = false;
9255 int ecount = 0;
9256 uint16_t cqid;
9257
James Smartcb5172e2010-03-15 11:25:07 -04009258 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9260 "0359 Not a valid slow-path completion "
9261 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009262 bf_get_le32(lpfc_eqe_major_code, eqe),
9263 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009264 return;
9265 }
9266
9267 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009268 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009269
9270 /* Search for completion queue pointer matching this cqid */
9271 speq = phba->sli4_hba.sp_eq;
9272 list_for_each_entry(childq, &speq->child_list, list) {
9273 if (childq->queue_id == cqid) {
9274 cq = childq;
9275 break;
9276 }
9277 }
9278 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009279 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9281 "0365 Slow-path CQ identifier "
9282 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009283 return;
9284 }
9285
9286 /* Process all the entries to the CQ */
9287 switch (cq->type) {
9288 case LPFC_MCQ:
9289 while ((cqe = lpfc_sli4_cq_get(cq))) {
9290 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9291 if (!(++ecount % LPFC_GET_QE_REL_INT))
9292 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9293 }
9294 break;
9295 case LPFC_WCQ:
9296 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009297 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009298 if (!(++ecount % LPFC_GET_QE_REL_INT))
9299 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9300 }
9301 break;
9302 default:
9303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9304 "0370 Invalid completion queue type (%d)\n",
9305 cq->type);
9306 return;
9307 }
9308
9309 /* Catch the no cq entry condition, log an error */
9310 if (unlikely(ecount == 0))
9311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9312 "0371 No entry from the CQ: identifier "
9313 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9314
9315 /* In any case, flash and re-arm the RCQ */
9316 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9317
9318 /* wake up worker thread if there are works to be done */
9319 if (workposted)
9320 lpfc_worker_wake_up(phba);
9321}
9322
9323/**
9324 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9325 * @eqe: Pointer to fast-path completion queue entry.
9326 *
9327 * This routine process a fast-path work queue completion entry from fast-path
9328 * event queue for FCP command response completion.
9329 **/
9330static void
9331lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9332 struct lpfc_wcqe_complete *wcqe)
9333{
9334 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9335 struct lpfc_iocbq *cmdiocbq;
9336 struct lpfc_iocbq irspiocbq;
9337 unsigned long iflags;
9338
9339 spin_lock_irqsave(&phba->hbalock, iflags);
9340 pring->stats.iocb_event++;
9341 spin_unlock_irqrestore(&phba->hbalock, iflags);
9342
9343 /* Check for response status */
9344 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9345 /* If resource errors reported from HBA, reduce queue
9346 * depth of the SCSI device.
9347 */
9348 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9349 IOSTAT_LOCAL_REJECT) &&
9350 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9351 phba->lpfc_rampdown_queue_depth(phba);
9352 }
9353 /* Log the error status */
9354 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9355 "0373 FCP complete error: status=x%x, "
9356 "hw_status=x%x, total_data_specified=%d, "
9357 "parameter=x%x, word3=x%x\n",
9358 bf_get(lpfc_wcqe_c_status, wcqe),
9359 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9360 wcqe->total_data_placed, wcqe->parameter,
9361 wcqe->word3);
9362 }
9363
9364 /* Look up the FCP command IOCB and create pseudo response IOCB */
9365 spin_lock_irqsave(&phba->hbalock, iflags);
9366 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9367 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9368 spin_unlock_irqrestore(&phba->hbalock, iflags);
9369 if (unlikely(!cmdiocbq)) {
9370 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9371 "0374 FCP complete with no corresponding "
9372 "cmdiocb: iotag (%d)\n",
9373 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9374 return;
9375 }
9376 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9377 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9378 "0375 FCP cmdiocb not callback function "
9379 "iotag: (%d)\n",
9380 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9381 return;
9382 }
9383
9384 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009385 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009386
James Smart0f65ff62010-02-26 14:14:23 -05009387 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9388 spin_lock_irqsave(&phba->hbalock, iflags);
9389 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9390 spin_unlock_irqrestore(&phba->hbalock, iflags);
9391 }
9392
James Smart4f774512009-05-22 14:52:35 -04009393 /* Pass the cmd_iocb and the rsp state to the upper layer */
9394 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9395}
9396
9397/**
9398 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9399 * @phba: Pointer to HBA context object.
9400 * @cq: Pointer to completion queue.
9401 * @wcqe: Pointer to work-queue completion queue entry.
9402 *
9403 * This routine handles an fast-path WQ entry comsumed event by invoking the
9404 * proper WQ release routine to the slow-path WQ.
9405 **/
9406static void
9407lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9408 struct lpfc_wcqe_release *wcqe)
9409{
9410 struct lpfc_queue *childwq;
9411 bool wqid_matched = false;
9412 uint16_t fcp_wqid;
9413
9414 /* Check for fast-path FCP work queue release */
9415 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9416 list_for_each_entry(childwq, &cq->child_list, list) {
9417 if (childwq->queue_id == fcp_wqid) {
9418 lpfc_sli4_wq_release(childwq,
9419 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9420 wqid_matched = true;
9421 break;
9422 }
9423 }
9424 /* Report warning log message if no match found */
9425 if (wqid_matched != true)
9426 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9427 "2580 Fast-path wqe consume event carries "
9428 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9429}
9430
9431/**
9432 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9433 * @cq: Pointer to the completion queue.
9434 * @eqe: Pointer to fast-path completion queue entry.
9435 *
9436 * This routine process a fast-path work queue completion entry from fast-path
9437 * event queue for FCP command response completion.
9438 **/
9439static int
9440lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9441 struct lpfc_cqe *cqe)
9442{
9443 struct lpfc_wcqe_release wcqe;
9444 bool workposted = false;
9445
9446 /* Copy the work queue CQE and convert endian order if needed */
9447 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9448
9449 /* Check and process for different type of WCQE and dispatch */
9450 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9451 case CQE_CODE_COMPL_WQE:
9452 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009453 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009454 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9455 (struct lpfc_wcqe_complete *)&wcqe);
9456 break;
9457 case CQE_CODE_RELEASE_WQE:
9458 /* Process the WQ release event */
9459 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9460 (struct lpfc_wcqe_release *)&wcqe);
9461 break;
9462 case CQE_CODE_XRI_ABORTED:
9463 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009464 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009465 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9466 (struct sli4_wcqe_xri_aborted *)&wcqe);
9467 break;
9468 default:
9469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9470 "0144 Not a valid WCQE code: x%x\n",
9471 bf_get(lpfc_wcqe_c_code, &wcqe));
9472 break;
9473 }
9474 return workposted;
9475}
9476
9477/**
9478 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9479 * @phba: Pointer to HBA context object.
9480 * @eqe: Pointer to fast-path event queue entry.
9481 *
9482 * This routine process a event queue entry from the fast-path event queue.
9483 * It will check the MajorCode and MinorCode to determine this is for a
9484 * completion event on a completion queue, if not, an error shall be logged
9485 * and just return. Otherwise, it will get to the corresponding completion
9486 * queue and process all the entries on the completion queue, rearm the
9487 * completion queue, and then return.
9488 **/
9489static void
9490lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9491 uint32_t fcp_cqidx)
9492{
9493 struct lpfc_queue *cq;
9494 struct lpfc_cqe *cqe;
9495 bool workposted = false;
9496 uint16_t cqid;
9497 int ecount = 0;
9498
James Smartcb5172e2010-03-15 11:25:07 -04009499 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9501 "0366 Not a valid fast-path completion "
9502 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009503 bf_get_le32(lpfc_eqe_major_code, eqe),
9504 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009505 return;
9506 }
9507
9508 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9509 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009510 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9512 "0367 Fast-path completion queue "
9513 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009514 return;
9515 }
9516
9517 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009518 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009519 if (unlikely(cqid != cq->queue_id)) {
9520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9521 "0368 Miss-matched fast-path completion "
9522 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9523 cqid, cq->queue_id);
9524 return;
9525 }
9526
9527 /* Process all the entries to the CQ */
9528 while ((cqe = lpfc_sli4_cq_get(cq))) {
9529 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9530 if (!(++ecount % LPFC_GET_QE_REL_INT))
9531 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9532 }
9533
9534 /* Catch the no cq entry condition */
9535 if (unlikely(ecount == 0))
9536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9537 "0369 No entry from fast-path completion "
9538 "queue fcpcqid=%d\n", cq->queue_id);
9539
9540 /* In any case, flash and re-arm the CQ */
9541 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9542
9543 /* wake up worker thread if there are works to be done */
9544 if (workposted)
9545 lpfc_worker_wake_up(phba);
9546}
9547
9548static void
9549lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9550{
9551 struct lpfc_eqe *eqe;
9552
9553 /* walk all the EQ entries and drop on the floor */
9554 while ((eqe = lpfc_sli4_eq_get(eq)))
9555 ;
9556
9557 /* Clear and re-arm the EQ */
9558 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9559}
9560
9561/**
9562 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9563 * @irq: Interrupt number.
9564 * @dev_id: The device context pointer.
9565 *
9566 * This function is directly called from the PCI layer as an interrupt
9567 * service routine when device with SLI-4 interface spec is enabled with
9568 * MSI-X multi-message interrupt mode and there are slow-path events in
9569 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9570 * interrupt mode, this function is called as part of the device-level
9571 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9572 * undergoing initialization, the interrupt handler will not process the
9573 * interrupt. The link attention and ELS ring attention events are handled
9574 * by the worker thread. The interrupt handler signals the worker thread
9575 * and returns for these events. This function is called without any lock
9576 * held. It gets the hbalock to access and update SLI data structures.
9577 *
9578 * This function returns IRQ_HANDLED when interrupt is handled else it
9579 * returns IRQ_NONE.
9580 **/
9581irqreturn_t
9582lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9583{
9584 struct lpfc_hba *phba;
9585 struct lpfc_queue *speq;
9586 struct lpfc_eqe *eqe;
9587 unsigned long iflag;
9588 int ecount = 0;
9589
9590 /*
9591 * Get the driver's phba structure from the dev_id
9592 */
9593 phba = (struct lpfc_hba *)dev_id;
9594
9595 if (unlikely(!phba))
9596 return IRQ_NONE;
9597
9598 /* Get to the EQ struct associated with this vector */
9599 speq = phba->sli4_hba.sp_eq;
9600
9601 /* Check device state for handling interrupt */
9602 if (unlikely(lpfc_intr_state_check(phba))) {
9603 /* Check again for link_state with lock held */
9604 spin_lock_irqsave(&phba->hbalock, iflag);
9605 if (phba->link_state < LPFC_LINK_DOWN)
9606 /* Flush, clear interrupt, and rearm the EQ */
9607 lpfc_sli4_eq_flush(phba, speq);
9608 spin_unlock_irqrestore(&phba->hbalock, iflag);
9609 return IRQ_NONE;
9610 }
9611
9612 /*
9613 * Process all the event on FCP slow-path EQ
9614 */
9615 while ((eqe = lpfc_sli4_eq_get(speq))) {
9616 lpfc_sli4_sp_handle_eqe(phba, eqe);
9617 if (!(++ecount % LPFC_GET_QE_REL_INT))
9618 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9619 }
9620
9621 /* Always clear and re-arm the slow-path EQ */
9622 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9623
9624 /* Catch the no cq entry condition */
9625 if (unlikely(ecount == 0)) {
9626 if (phba->intr_type == MSIX)
9627 /* MSI-X treated interrupt served as no EQ share INT */
9628 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9629 "0357 MSI-X interrupt with no EQE\n");
9630 else
9631 /* Non MSI-X treated on interrupt as EQ share INT */
9632 return IRQ_NONE;
9633 }
9634
9635 return IRQ_HANDLED;
9636} /* lpfc_sli4_sp_intr_handler */
9637
9638/**
9639 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9640 * @irq: Interrupt number.
9641 * @dev_id: The device context pointer.
9642 *
9643 * This function is directly called from the PCI layer as an interrupt
9644 * service routine when device with SLI-4 interface spec is enabled with
9645 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9646 * ring event in the HBA. However, when the device is enabled with either
9647 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9648 * device-level interrupt handler. When the PCI slot is in error recovery
9649 * or the HBA is undergoing initialization, the interrupt handler will not
9650 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9651 * the intrrupt context. This function is called without any lock held.
9652 * It gets the hbalock to access and update SLI data structures. Note that,
9653 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9654 * equal to that of FCP CQ index.
9655 *
9656 * This function returns IRQ_HANDLED when interrupt is handled else it
9657 * returns IRQ_NONE.
9658 **/
9659irqreturn_t
9660lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9661{
9662 struct lpfc_hba *phba;
9663 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9664 struct lpfc_queue *fpeq;
9665 struct lpfc_eqe *eqe;
9666 unsigned long iflag;
9667 int ecount = 0;
9668 uint32_t fcp_eqidx;
9669
9670 /* Get the driver's phba structure from the dev_id */
9671 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9672 phba = fcp_eq_hdl->phba;
9673 fcp_eqidx = fcp_eq_hdl->idx;
9674
9675 if (unlikely(!phba))
9676 return IRQ_NONE;
9677
9678 /* Get to the EQ struct associated with this vector */
9679 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9680
9681 /* Check device state for handling interrupt */
9682 if (unlikely(lpfc_intr_state_check(phba))) {
9683 /* Check again for link_state with lock held */
9684 spin_lock_irqsave(&phba->hbalock, iflag);
9685 if (phba->link_state < LPFC_LINK_DOWN)
9686 /* Flush, clear interrupt, and rearm the EQ */
9687 lpfc_sli4_eq_flush(phba, fpeq);
9688 spin_unlock_irqrestore(&phba->hbalock, iflag);
9689 return IRQ_NONE;
9690 }
9691
9692 /*
9693 * Process all the event on FCP fast-path EQ
9694 */
9695 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9696 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9697 if (!(++ecount % LPFC_GET_QE_REL_INT))
9698 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9699 }
9700
9701 /* Always clear and re-arm the fast-path EQ */
9702 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9703
9704 if (unlikely(ecount == 0)) {
9705 if (phba->intr_type == MSIX)
9706 /* MSI-X treated interrupt served as no EQ share INT */
9707 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9708 "0358 MSI-X interrupt with no EQE\n");
9709 else
9710 /* Non MSI-X treated on interrupt as EQ share INT */
9711 return IRQ_NONE;
9712 }
9713
9714 return IRQ_HANDLED;
9715} /* lpfc_sli4_fp_intr_handler */
9716
9717/**
9718 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9719 * @irq: Interrupt number.
9720 * @dev_id: The device context pointer.
9721 *
9722 * This function is the device-level interrupt handler to device with SLI-4
9723 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9724 * interrupt mode is enabled and there is an event in the HBA which requires
9725 * driver attention. This function invokes the slow-path interrupt attention
9726 * handling function and fast-path interrupt attention handling function in
9727 * turn to process the relevant HBA attention events. This function is called
9728 * without any lock held. It gets the hbalock to access and update SLI data
9729 * structures.
9730 *
9731 * This function returns IRQ_HANDLED when interrupt is handled, else it
9732 * returns IRQ_NONE.
9733 **/
9734irqreturn_t
9735lpfc_sli4_intr_handler(int irq, void *dev_id)
9736{
9737 struct lpfc_hba *phba;
9738 irqreturn_t sp_irq_rc, fp_irq_rc;
9739 bool fp_handled = false;
9740 uint32_t fcp_eqidx;
9741
9742 /* Get the driver's phba structure from the dev_id */
9743 phba = (struct lpfc_hba *)dev_id;
9744
9745 if (unlikely(!phba))
9746 return IRQ_NONE;
9747
9748 /*
9749 * Invokes slow-path host attention interrupt handling as appropriate.
9750 */
9751 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9752
9753 /*
9754 * Invoke fast-path host attention interrupt handling as appropriate.
9755 */
9756 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9757 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9758 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9759 if (fp_irq_rc == IRQ_HANDLED)
9760 fp_handled |= true;
9761 }
9762
9763 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9764} /* lpfc_sli4_intr_handler */
9765
9766/**
9767 * lpfc_sli4_queue_free - free a queue structure and associated memory
9768 * @queue: The queue structure to free.
9769 *
9770 * This function frees a queue structure and the DMAable memeory used for
9771 * the host resident queue. This function must be called after destroying the
9772 * queue on the HBA.
9773 **/
9774void
9775lpfc_sli4_queue_free(struct lpfc_queue *queue)
9776{
9777 struct lpfc_dmabuf *dmabuf;
9778
9779 if (!queue)
9780 return;
9781
9782 while (!list_empty(&queue->page_list)) {
9783 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9784 list);
James Smart49198b32010-04-06 15:04:33 -04009785 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009786 dmabuf->virt, dmabuf->phys);
9787 kfree(dmabuf);
9788 }
9789 kfree(queue);
9790 return;
9791}
9792
9793/**
9794 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9795 * @phba: The HBA that this queue is being created on.
9796 * @entry_size: The size of each queue entry for this queue.
9797 * @entry count: The number of entries that this queue will handle.
9798 *
9799 * This function allocates a queue structure and the DMAable memory used for
9800 * the host resident queue. This function must be called before creating the
9801 * queue on the HBA.
9802 **/
9803struct lpfc_queue *
9804lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9805 uint32_t entry_count)
9806{
9807 struct lpfc_queue *queue;
9808 struct lpfc_dmabuf *dmabuf;
9809 int x, total_qe_count;
9810 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009811 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009812
James Smartcb5172e2010-03-15 11:25:07 -04009813 if (!phba->sli4_hba.pc_sli4_params.supported)
9814 hw_page_size = SLI4_PAGE_SIZE;
9815
James Smart4f774512009-05-22 14:52:35 -04009816 queue = kzalloc(sizeof(struct lpfc_queue) +
9817 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9818 if (!queue)
9819 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009820 queue->page_count = (ALIGN(entry_size * entry_count,
9821 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009822 INIT_LIST_HEAD(&queue->list);
9823 INIT_LIST_HEAD(&queue->page_list);
9824 INIT_LIST_HEAD(&queue->child_list);
9825 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9826 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9827 if (!dmabuf)
9828 goto out_fail;
9829 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009830 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009831 GFP_KERNEL);
9832 if (!dmabuf->virt) {
9833 kfree(dmabuf);
9834 goto out_fail;
9835 }
James Smartcb5172e2010-03-15 11:25:07 -04009836 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009837 dmabuf->buffer_tag = x;
9838 list_add_tail(&dmabuf->list, &queue->page_list);
9839 /* initialize queue's entry array */
9840 dma_pointer = dmabuf->virt;
9841 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009842 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009843 total_qe_count++, dma_pointer += entry_size) {
9844 queue->qe[total_qe_count].address = dma_pointer;
9845 }
9846 }
9847 queue->entry_size = entry_size;
9848 queue->entry_count = entry_count;
9849 queue->phba = phba;
9850
9851 return queue;
9852out_fail:
9853 lpfc_sli4_queue_free(queue);
9854 return NULL;
9855}
9856
9857/**
9858 * lpfc_eq_create - Create an Event Queue on the HBA
9859 * @phba: HBA structure that indicates port to create a queue on.
9860 * @eq: The queue structure to use to create the event queue.
9861 * @imax: The maximum interrupt per second limit.
9862 *
9863 * This function creates an event queue, as detailed in @eq, on a port,
9864 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9865 *
9866 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9867 * is used to get the entry count and entry size that are necessary to
9868 * determine the number of pages to allocate and use for this queue. This
9869 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9870 * event queue. This function is asynchronous and will wait for the mailbox
9871 * command to finish before continuing.
9872 *
9873 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -04009874 * memory this function will return -ENOMEM. If the queue create mailbox command
9875 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -04009876 **/
9877uint32_t
9878lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9879{
9880 struct lpfc_mbx_eq_create *eq_create;
9881 LPFC_MBOXQ_t *mbox;
9882 int rc, length, status = 0;
9883 struct lpfc_dmabuf *dmabuf;
9884 uint32_t shdr_status, shdr_add_status;
9885 union lpfc_sli4_cfg_shdr *shdr;
9886 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009887 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9888
9889 if (!phba->sli4_hba.pc_sli4_params.supported)
9890 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009891
9892 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9893 if (!mbox)
9894 return -ENOMEM;
9895 length = (sizeof(struct lpfc_mbx_eq_create) -
9896 sizeof(struct lpfc_sli4_cfg_mhdr));
9897 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9898 LPFC_MBOX_OPCODE_EQ_CREATE,
9899 length, LPFC_SLI4_MBX_EMBED);
9900 eq_create = &mbox->u.mqe.un.eq_create;
9901 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9902 eq->page_count);
9903 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9904 LPFC_EQE_SIZE);
9905 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9906 /* Calculate delay multiper from maximum interrupt per second */
9907 dmult = LPFC_DMULT_CONST/imax - 1;
9908 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9909 dmult);
9910 switch (eq->entry_count) {
9911 default:
9912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9913 "0360 Unsupported EQ count. (%d)\n",
9914 eq->entry_count);
9915 if (eq->entry_count < 256)
9916 return -EINVAL;
9917 /* otherwise default to smallest count (drop through) */
9918 case 256:
9919 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9920 LPFC_EQ_CNT_256);
9921 break;
9922 case 512:
9923 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9924 LPFC_EQ_CNT_512);
9925 break;
9926 case 1024:
9927 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9928 LPFC_EQ_CNT_1024);
9929 break;
9930 case 2048:
9931 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9932 LPFC_EQ_CNT_2048);
9933 break;
9934 case 4096:
9935 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9936 LPFC_EQ_CNT_4096);
9937 break;
9938 }
9939 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009940 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009941 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9942 putPaddrLow(dmabuf->phys);
9943 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9944 putPaddrHigh(dmabuf->phys);
9945 }
9946 mbox->vport = phba->pport;
9947 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9948 mbox->context1 = NULL;
9949 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9950 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9951 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9952 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9953 if (shdr_status || shdr_add_status || rc) {
9954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9955 "2500 EQ_CREATE mailbox failed with "
9956 "status x%x add_status x%x, mbx status x%x\n",
9957 shdr_status, shdr_add_status, rc);
9958 status = -ENXIO;
9959 }
9960 eq->type = LPFC_EQ;
9961 eq->subtype = LPFC_NONE;
9962 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9963 if (eq->queue_id == 0xFFFF)
9964 status = -ENXIO;
9965 eq->host_index = 0;
9966 eq->hba_index = 0;
9967
James Smart8fa38512009-07-19 10:01:03 -04009968 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009969 return status;
9970}
9971
9972/**
9973 * lpfc_cq_create - Create a Completion Queue on the HBA
9974 * @phba: HBA structure that indicates port to create a queue on.
9975 * @cq: The queue structure to use to create the completion queue.
9976 * @eq: The event queue to bind this completion queue to.
9977 *
9978 * This function creates a completion queue, as detailed in @wq, on a port,
9979 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9980 *
9981 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9982 * is used to get the entry count and entry size that are necessary to
9983 * determine the number of pages to allocate and use for this queue. The @eq
9984 * is used to indicate which event queue to bind this completion queue to. This
9985 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9986 * completion queue. This function is asynchronous and will wait for the mailbox
9987 * command to finish before continuing.
9988 *
9989 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -04009990 * memory this function will return -ENOMEM. If the queue create mailbox command
9991 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -04009992 **/
9993uint32_t
9994lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9995 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9996{
9997 struct lpfc_mbx_cq_create *cq_create;
9998 struct lpfc_dmabuf *dmabuf;
9999 LPFC_MBOXQ_t *mbox;
10000 int rc, length, status = 0;
10001 uint32_t shdr_status, shdr_add_status;
10002 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010003 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10004
10005 if (!phba->sli4_hba.pc_sli4_params.supported)
10006 hw_page_size = SLI4_PAGE_SIZE;
10007
James Smart4f774512009-05-22 14:52:35 -040010008
10009 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10010 if (!mbox)
10011 return -ENOMEM;
10012 length = (sizeof(struct lpfc_mbx_cq_create) -
10013 sizeof(struct lpfc_sli4_cfg_mhdr));
10014 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10015 LPFC_MBOX_OPCODE_CQ_CREATE,
10016 length, LPFC_SLI4_MBX_EMBED);
10017 cq_create = &mbox->u.mqe.un.cq_create;
10018 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10019 cq->page_count);
10020 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10021 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10022 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10023 switch (cq->entry_count) {
10024 default:
10025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10026 "0361 Unsupported CQ count. (%d)\n",
10027 cq->entry_count);
10028 if (cq->entry_count < 256)
10029 return -EINVAL;
10030 /* otherwise default to smallest count (drop through) */
10031 case 256:
10032 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10033 LPFC_CQ_CNT_256);
10034 break;
10035 case 512:
10036 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10037 LPFC_CQ_CNT_512);
10038 break;
10039 case 1024:
10040 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10041 LPFC_CQ_CNT_1024);
10042 break;
10043 }
10044 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010045 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010046 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10047 putPaddrLow(dmabuf->phys);
10048 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10049 putPaddrHigh(dmabuf->phys);
10050 }
10051 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10052
10053 /* The IOCTL status is embedded in the mailbox subheader. */
10054 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10055 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10056 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10057 if (shdr_status || shdr_add_status || rc) {
10058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10059 "2501 CQ_CREATE mailbox failed with "
10060 "status x%x add_status x%x, mbx status x%x\n",
10061 shdr_status, shdr_add_status, rc);
10062 status = -ENXIO;
10063 goto out;
10064 }
10065 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10066 if (cq->queue_id == 0xFFFF) {
10067 status = -ENXIO;
10068 goto out;
10069 }
10070 /* link the cq onto the parent eq child list */
10071 list_add_tail(&cq->list, &eq->child_list);
10072 /* Set up completion queue's type and subtype */
10073 cq->type = type;
10074 cq->subtype = subtype;
10075 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10076 cq->host_index = 0;
10077 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010078
James Smart8fa38512009-07-19 10:01:03 -040010079out:
10080 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010081 return status;
10082}
10083
10084/**
James Smartb19a0612010-04-06 14:48:51 -040010085 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010086 * @phba: HBA structure that indicates port to create a queue on.
10087 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010088 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10089 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010090 *
James Smartb19a0612010-04-06 14:48:51 -040010091 * This function provides failback (fb) functionality when the
10092 * mq_create_ext fails on older FW generations. It's purpose is identical
10093 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010094 *
James Smartb19a0612010-04-06 14:48:51 -040010095 * This routine cannot fail as all attributes were previously accessed and
10096 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010097 **/
James Smartb19a0612010-04-06 14:48:51 -040010098static void
10099lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10100 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010101{
10102 struct lpfc_mbx_mq_create *mq_create;
10103 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010104 int length;
James Smart04c68492009-05-22 14:52:52 -040010105
James Smart04c68492009-05-22 14:52:52 -040010106 length = (sizeof(struct lpfc_mbx_mq_create) -
10107 sizeof(struct lpfc_sli4_cfg_mhdr));
10108 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10109 LPFC_MBOX_OPCODE_MQ_CREATE,
10110 length, LPFC_SLI4_MBX_EMBED);
10111 mq_create = &mbox->u.mqe.un.mq_create;
10112 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010113 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010114 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010115 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010116 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10117 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010118 case 16:
10119 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10120 LPFC_MQ_CNT_16);
10121 break;
10122 case 32:
10123 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10124 LPFC_MQ_CNT_32);
10125 break;
10126 case 64:
10127 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10128 LPFC_MQ_CNT_64);
10129 break;
10130 case 128:
10131 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10132 LPFC_MQ_CNT_128);
10133 break;
10134 }
10135 list_for_each_entry(dmabuf, &mq->page_list, list) {
10136 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010137 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010138 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010139 putPaddrHigh(dmabuf->phys);
10140 }
10141}
10142
10143/**
10144 * lpfc_mq_create - Create a mailbox Queue on the HBA
10145 * @phba: HBA structure that indicates port to create a queue on.
10146 * @mq: The queue structure to use to create the mailbox queue.
10147 * @cq: The completion queue to associate with this cq.
10148 * @subtype: The queue's subtype.
10149 *
10150 * This function creates a mailbox queue, as detailed in @mq, on a port,
10151 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10152 *
10153 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10154 * is used to get the entry count and entry size that are necessary to
10155 * determine the number of pages to allocate and use for this queue. This
10156 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10157 * mailbox queue. This function is asynchronous and will wait for the mailbox
10158 * command to finish before continuing.
10159 *
10160 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010161 * memory this function will return -ENOMEM. If the queue create mailbox command
10162 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010163 **/
10164int32_t
10165lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10166 struct lpfc_queue *cq, uint32_t subtype)
10167{
10168 struct lpfc_mbx_mq_create *mq_create;
10169 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10170 struct lpfc_dmabuf *dmabuf;
10171 LPFC_MBOXQ_t *mbox;
10172 int rc, length, status = 0;
10173 uint32_t shdr_status, shdr_add_status;
10174 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010175 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010176
James Smart49198b32010-04-06 15:04:33 -040010177 if (!phba->sli4_hba.pc_sli4_params.supported)
10178 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010179
10180 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10181 if (!mbox)
10182 return -ENOMEM;
10183 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10184 sizeof(struct lpfc_sli4_cfg_mhdr));
10185 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10186 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10187 length, LPFC_SLI4_MBX_EMBED);
10188
10189 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10190 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10191 mq->page_count);
10192 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10193 1);
10194 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10195 &mq_create_ext->u.request, 1);
10196 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10197 &mq_create_ext->u.request, 1);
10198 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10199 cq->queue_id);
10200 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10201 switch (mq->entry_count) {
10202 default:
10203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10204 "0362 Unsupported MQ count. (%d)\n",
10205 mq->entry_count);
10206 if (mq->entry_count < 16)
10207 return -EINVAL;
10208 /* otherwise default to smallest count (drop through) */
10209 case 16:
10210 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10211 LPFC_MQ_CNT_16);
10212 break;
10213 case 32:
10214 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10215 LPFC_MQ_CNT_32);
10216 break;
10217 case 64:
10218 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10219 LPFC_MQ_CNT_64);
10220 break;
10221 case 128:
10222 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10223 LPFC_MQ_CNT_128);
10224 break;
10225 }
10226 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010227 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010228 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10229 putPaddrLow(dmabuf->phys);
10230 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010231 putPaddrHigh(dmabuf->phys);
10232 }
10233 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010234 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10235 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10236 &mq_create_ext->u.response);
10237 if (rc != MBX_SUCCESS) {
10238 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10239 "2795 MQ_CREATE_EXT failed with "
10240 "status x%x. Failback to MQ_CREATE.\n",
10241 rc);
10242 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10243 mq_create = &mbox->u.mqe.un.mq_create;
10244 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10245 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10246 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10247 &mq_create->u.response);
10248 }
10249
James Smart04c68492009-05-22 14:52:52 -040010250 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010251 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10252 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10253 if (shdr_status || shdr_add_status || rc) {
10254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10255 "2502 MQ_CREATE mailbox failed with "
10256 "status x%x add_status x%x, mbx status x%x\n",
10257 shdr_status, shdr_add_status, rc);
10258 status = -ENXIO;
10259 goto out;
10260 }
James Smart04c68492009-05-22 14:52:52 -040010261 if (mq->queue_id == 0xFFFF) {
10262 status = -ENXIO;
10263 goto out;
10264 }
10265 mq->type = LPFC_MQ;
10266 mq->subtype = subtype;
10267 mq->host_index = 0;
10268 mq->hba_index = 0;
10269
10270 /* link the mq onto the parent cq child list */
10271 list_add_tail(&mq->list, &cq->child_list);
10272out:
James Smart8fa38512009-07-19 10:01:03 -040010273 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010274 return status;
10275}
10276
10277/**
James Smart4f774512009-05-22 14:52:35 -040010278 * lpfc_wq_create - Create a Work Queue on the HBA
10279 * @phba: HBA structure that indicates port to create a queue on.
10280 * @wq: The queue structure to use to create the work queue.
10281 * @cq: The completion queue to bind this work queue to.
10282 * @subtype: The subtype of the work queue indicating its functionality.
10283 *
10284 * This function creates a work queue, as detailed in @wq, on a port, described
10285 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10286 *
10287 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10288 * is used to get the entry count and entry size that are necessary to
10289 * determine the number of pages to allocate and use for this queue. The @cq
10290 * is used to indicate which completion queue to bind this work queue to. This
10291 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10292 * work queue. This function is asynchronous and will wait for the mailbox
10293 * command to finish before continuing.
10294 *
10295 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010296 * memory this function will return -ENOMEM. If the queue create mailbox command
10297 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010298 **/
10299uint32_t
10300lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10301 struct lpfc_queue *cq, uint32_t subtype)
10302{
10303 struct lpfc_mbx_wq_create *wq_create;
10304 struct lpfc_dmabuf *dmabuf;
10305 LPFC_MBOXQ_t *mbox;
10306 int rc, length, status = 0;
10307 uint32_t shdr_status, shdr_add_status;
10308 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010309 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10310
10311 if (!phba->sli4_hba.pc_sli4_params.supported)
10312 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010313
10314 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10315 if (!mbox)
10316 return -ENOMEM;
10317 length = (sizeof(struct lpfc_mbx_wq_create) -
10318 sizeof(struct lpfc_sli4_cfg_mhdr));
10319 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10320 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10321 length, LPFC_SLI4_MBX_EMBED);
10322 wq_create = &mbox->u.mqe.un.wq_create;
10323 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10324 wq->page_count);
10325 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10326 cq->queue_id);
10327 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010328 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010329 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10330 putPaddrLow(dmabuf->phys);
10331 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10332 putPaddrHigh(dmabuf->phys);
10333 }
10334 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10335 /* The IOCTL status is embedded in the mailbox subheader. */
10336 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10339 if (shdr_status || shdr_add_status || rc) {
10340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10341 "2503 WQ_CREATE mailbox failed with "
10342 "status x%x add_status x%x, mbx status x%x\n",
10343 shdr_status, shdr_add_status, rc);
10344 status = -ENXIO;
10345 goto out;
10346 }
10347 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10348 if (wq->queue_id == 0xFFFF) {
10349 status = -ENXIO;
10350 goto out;
10351 }
10352 wq->type = LPFC_WQ;
10353 wq->subtype = subtype;
10354 wq->host_index = 0;
10355 wq->hba_index = 0;
10356
10357 /* link the wq onto the parent cq child list */
10358 list_add_tail(&wq->list, &cq->child_list);
10359out:
James Smart8fa38512009-07-19 10:01:03 -040010360 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010361 return status;
10362}
10363
10364/**
10365 * lpfc_rq_create - Create a Receive Queue on the HBA
10366 * @phba: HBA structure that indicates port to create a queue on.
10367 * @hrq: The queue structure to use to create the header receive queue.
10368 * @drq: The queue structure to use to create the data receive queue.
10369 * @cq: The completion queue to bind this work queue to.
10370 *
10371 * This function creates a receive buffer queue pair , as detailed in @hrq and
10372 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10373 * to the HBA.
10374 *
10375 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10376 * struct is used to get the entry count that is necessary to determine the
10377 * number of pages to use for this queue. The @cq is used to indicate which
10378 * completion queue to bind received buffers that are posted to these queues to.
10379 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10380 * receive queue pair. This function is asynchronous and will wait for the
10381 * mailbox command to finish before continuing.
10382 *
10383 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010384 * memory this function will return -ENOMEM. If the queue create mailbox command
10385 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010386 **/
10387uint32_t
10388lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10389 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10390{
10391 struct lpfc_mbx_rq_create *rq_create;
10392 struct lpfc_dmabuf *dmabuf;
10393 LPFC_MBOXQ_t *mbox;
10394 int rc, length, status = 0;
10395 uint32_t shdr_status, shdr_add_status;
10396 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010397 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10398
10399 if (!phba->sli4_hba.pc_sli4_params.supported)
10400 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010401
10402 if (hrq->entry_count != drq->entry_count)
10403 return -EINVAL;
10404 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10405 if (!mbox)
10406 return -ENOMEM;
10407 length = (sizeof(struct lpfc_mbx_rq_create) -
10408 sizeof(struct lpfc_sli4_cfg_mhdr));
10409 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10410 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10411 length, LPFC_SLI4_MBX_EMBED);
10412 rq_create = &mbox->u.mqe.un.rq_create;
10413 switch (hrq->entry_count) {
10414 default:
10415 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10416 "2535 Unsupported RQ count. (%d)\n",
10417 hrq->entry_count);
10418 if (hrq->entry_count < 512)
10419 return -EINVAL;
10420 /* otherwise default to smallest count (drop through) */
10421 case 512:
10422 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10423 LPFC_RQ_RING_SIZE_512);
10424 break;
10425 case 1024:
10426 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10427 LPFC_RQ_RING_SIZE_1024);
10428 break;
10429 case 2048:
10430 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10431 LPFC_RQ_RING_SIZE_2048);
10432 break;
10433 case 4096:
10434 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10435 LPFC_RQ_RING_SIZE_4096);
10436 break;
10437 }
10438 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10439 cq->queue_id);
10440 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10441 hrq->page_count);
10442 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10443 LPFC_HDR_BUF_SIZE);
10444 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010445 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010446 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10447 putPaddrLow(dmabuf->phys);
10448 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10449 putPaddrHigh(dmabuf->phys);
10450 }
10451 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10452 /* The IOCTL status is embedded in the mailbox subheader. */
10453 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10454 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10455 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10456 if (shdr_status || shdr_add_status || rc) {
10457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10458 "2504 RQ_CREATE mailbox failed with "
10459 "status x%x add_status x%x, mbx status x%x\n",
10460 shdr_status, shdr_add_status, rc);
10461 status = -ENXIO;
10462 goto out;
10463 }
10464 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10465 if (hrq->queue_id == 0xFFFF) {
10466 status = -ENXIO;
10467 goto out;
10468 }
10469 hrq->type = LPFC_HRQ;
10470 hrq->subtype = subtype;
10471 hrq->host_index = 0;
10472 hrq->hba_index = 0;
10473
10474 /* now create the data queue */
10475 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10476 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10477 length, LPFC_SLI4_MBX_EMBED);
10478 switch (drq->entry_count) {
10479 default:
10480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10481 "2536 Unsupported RQ count. (%d)\n",
10482 drq->entry_count);
10483 if (drq->entry_count < 512)
10484 return -EINVAL;
10485 /* otherwise default to smallest count (drop through) */
10486 case 512:
10487 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10488 LPFC_RQ_RING_SIZE_512);
10489 break;
10490 case 1024:
10491 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10492 LPFC_RQ_RING_SIZE_1024);
10493 break;
10494 case 2048:
10495 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10496 LPFC_RQ_RING_SIZE_2048);
10497 break;
10498 case 4096:
10499 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10500 LPFC_RQ_RING_SIZE_4096);
10501 break;
10502 }
10503 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10504 cq->queue_id);
10505 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10506 drq->page_count);
10507 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10508 LPFC_DATA_BUF_SIZE);
10509 list_for_each_entry(dmabuf, &drq->page_list, list) {
10510 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10511 putPaddrLow(dmabuf->phys);
10512 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10513 putPaddrHigh(dmabuf->phys);
10514 }
10515 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10516 /* The IOCTL status is embedded in the mailbox subheader. */
10517 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10518 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10519 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10520 if (shdr_status || shdr_add_status || rc) {
10521 status = -ENXIO;
10522 goto out;
10523 }
10524 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10525 if (drq->queue_id == 0xFFFF) {
10526 status = -ENXIO;
10527 goto out;
10528 }
10529 drq->type = LPFC_DRQ;
10530 drq->subtype = subtype;
10531 drq->host_index = 0;
10532 drq->hba_index = 0;
10533
10534 /* link the header and data RQs onto the parent cq child list */
10535 list_add_tail(&hrq->list, &cq->child_list);
10536 list_add_tail(&drq->list, &cq->child_list);
10537
10538out:
James Smart8fa38512009-07-19 10:01:03 -040010539 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010540 return status;
10541}
10542
10543/**
10544 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10545 * @eq: The queue structure associated with the queue to destroy.
10546 *
10547 * This function destroys a queue, as detailed in @eq by sending an mailbox
10548 * command, specific to the type of queue, to the HBA.
10549 *
10550 * The @eq struct is used to get the queue ID of the queue to destroy.
10551 *
10552 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010553 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010554 **/
10555uint32_t
10556lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10557{
10558 LPFC_MBOXQ_t *mbox;
10559 int rc, length, status = 0;
10560 uint32_t shdr_status, shdr_add_status;
10561 union lpfc_sli4_cfg_shdr *shdr;
10562
10563 if (!eq)
10564 return -ENODEV;
10565 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10566 if (!mbox)
10567 return -ENOMEM;
10568 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10569 sizeof(struct lpfc_sli4_cfg_mhdr));
10570 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10571 LPFC_MBOX_OPCODE_EQ_DESTROY,
10572 length, LPFC_SLI4_MBX_EMBED);
10573 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10574 eq->queue_id);
10575 mbox->vport = eq->phba->pport;
10576 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10577
10578 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10579 /* The IOCTL status is embedded in the mailbox subheader. */
10580 shdr = (union lpfc_sli4_cfg_shdr *)
10581 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10582 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10583 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10584 if (shdr_status || shdr_add_status || rc) {
10585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10586 "2505 EQ_DESTROY mailbox failed with "
10587 "status x%x add_status x%x, mbx status x%x\n",
10588 shdr_status, shdr_add_status, rc);
10589 status = -ENXIO;
10590 }
10591
10592 /* Remove eq from any list */
10593 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010594 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010595 return status;
10596}
10597
10598/**
10599 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10600 * @cq: The queue structure associated with the queue to destroy.
10601 *
10602 * This function destroys a queue, as detailed in @cq by sending an mailbox
10603 * command, specific to the type of queue, to the HBA.
10604 *
10605 * The @cq struct is used to get the queue ID of the queue to destroy.
10606 *
10607 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010608 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010609 **/
10610uint32_t
10611lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10612{
10613 LPFC_MBOXQ_t *mbox;
10614 int rc, length, status = 0;
10615 uint32_t shdr_status, shdr_add_status;
10616 union lpfc_sli4_cfg_shdr *shdr;
10617
10618 if (!cq)
10619 return -ENODEV;
10620 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10621 if (!mbox)
10622 return -ENOMEM;
10623 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10624 sizeof(struct lpfc_sli4_cfg_mhdr));
10625 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10626 LPFC_MBOX_OPCODE_CQ_DESTROY,
10627 length, LPFC_SLI4_MBX_EMBED);
10628 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10629 cq->queue_id);
10630 mbox->vport = cq->phba->pport;
10631 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10632 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10633 /* The IOCTL status is embedded in the mailbox subheader. */
10634 shdr = (union lpfc_sli4_cfg_shdr *)
10635 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10636 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10637 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10638 if (shdr_status || shdr_add_status || rc) {
10639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10640 "2506 CQ_DESTROY mailbox failed with "
10641 "status x%x add_status x%x, mbx status x%x\n",
10642 shdr_status, shdr_add_status, rc);
10643 status = -ENXIO;
10644 }
10645 /* Remove cq from any list */
10646 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010647 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010648 return status;
10649}
10650
10651/**
James Smart04c68492009-05-22 14:52:52 -040010652 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10653 * @qm: The queue structure associated with the queue to destroy.
10654 *
10655 * This function destroys a queue, as detailed in @mq by sending an mailbox
10656 * command, specific to the type of queue, to the HBA.
10657 *
10658 * The @mq struct is used to get the queue ID of the queue to destroy.
10659 *
10660 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010661 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040010662 **/
10663uint32_t
10664lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10665{
10666 LPFC_MBOXQ_t *mbox;
10667 int rc, length, status = 0;
10668 uint32_t shdr_status, shdr_add_status;
10669 union lpfc_sli4_cfg_shdr *shdr;
10670
10671 if (!mq)
10672 return -ENODEV;
10673 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10674 if (!mbox)
10675 return -ENOMEM;
10676 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10677 sizeof(struct lpfc_sli4_cfg_mhdr));
10678 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10679 LPFC_MBOX_OPCODE_MQ_DESTROY,
10680 length, LPFC_SLI4_MBX_EMBED);
10681 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10682 mq->queue_id);
10683 mbox->vport = mq->phba->pport;
10684 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10685 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10686 /* The IOCTL status is embedded in the mailbox subheader. */
10687 shdr = (union lpfc_sli4_cfg_shdr *)
10688 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10691 if (shdr_status || shdr_add_status || rc) {
10692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10693 "2507 MQ_DESTROY mailbox failed with "
10694 "status x%x add_status x%x, mbx status x%x\n",
10695 shdr_status, shdr_add_status, rc);
10696 status = -ENXIO;
10697 }
10698 /* Remove mq from any list */
10699 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010700 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010701 return status;
10702}
10703
10704/**
James Smart4f774512009-05-22 14:52:35 -040010705 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10706 * @wq: The queue structure associated with the queue to destroy.
10707 *
10708 * This function destroys a queue, as detailed in @wq by sending an mailbox
10709 * command, specific to the type of queue, to the HBA.
10710 *
10711 * The @wq struct is used to get the queue ID of the queue to destroy.
10712 *
10713 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010714 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010715 **/
10716uint32_t
10717lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10718{
10719 LPFC_MBOXQ_t *mbox;
10720 int rc, length, status = 0;
10721 uint32_t shdr_status, shdr_add_status;
10722 union lpfc_sli4_cfg_shdr *shdr;
10723
10724 if (!wq)
10725 return -ENODEV;
10726 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10727 if (!mbox)
10728 return -ENOMEM;
10729 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10730 sizeof(struct lpfc_sli4_cfg_mhdr));
10731 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10732 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10733 length, LPFC_SLI4_MBX_EMBED);
10734 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10735 wq->queue_id);
10736 mbox->vport = wq->phba->pport;
10737 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10738 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10739 shdr = (union lpfc_sli4_cfg_shdr *)
10740 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10741 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10742 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10743 if (shdr_status || shdr_add_status || rc) {
10744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10745 "2508 WQ_DESTROY mailbox failed with "
10746 "status x%x add_status x%x, mbx status x%x\n",
10747 shdr_status, shdr_add_status, rc);
10748 status = -ENXIO;
10749 }
10750 /* Remove wq from any list */
10751 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010752 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010753 return status;
10754}
10755
10756/**
10757 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10758 * @rq: The queue structure associated with the queue to destroy.
10759 *
10760 * This function destroys a queue, as detailed in @rq by sending an mailbox
10761 * command, specific to the type of queue, to the HBA.
10762 *
10763 * The @rq struct is used to get the queue ID of the queue to destroy.
10764 *
10765 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010766 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010767 **/
10768uint32_t
10769lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10770 struct lpfc_queue *drq)
10771{
10772 LPFC_MBOXQ_t *mbox;
10773 int rc, length, status = 0;
10774 uint32_t shdr_status, shdr_add_status;
10775 union lpfc_sli4_cfg_shdr *shdr;
10776
10777 if (!hrq || !drq)
10778 return -ENODEV;
10779 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10780 if (!mbox)
10781 return -ENOMEM;
10782 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10783 sizeof(struct mbox_header));
10784 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10785 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10786 length, LPFC_SLI4_MBX_EMBED);
10787 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10788 hrq->queue_id);
10789 mbox->vport = hrq->phba->pport;
10790 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10791 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10792 /* The IOCTL status is embedded in the mailbox subheader. */
10793 shdr = (union lpfc_sli4_cfg_shdr *)
10794 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10795 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10796 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10797 if (shdr_status || shdr_add_status || rc) {
10798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10799 "2509 RQ_DESTROY mailbox failed with "
10800 "status x%x add_status x%x, mbx status x%x\n",
10801 shdr_status, shdr_add_status, rc);
10802 if (rc != MBX_TIMEOUT)
10803 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10804 return -ENXIO;
10805 }
10806 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10807 drq->queue_id);
10808 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10809 shdr = (union lpfc_sli4_cfg_shdr *)
10810 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10811 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10812 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10813 if (shdr_status || shdr_add_status || rc) {
10814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10815 "2510 RQ_DESTROY mailbox failed with "
10816 "status x%x add_status x%x, mbx status x%x\n",
10817 shdr_status, shdr_add_status, rc);
10818 status = -ENXIO;
10819 }
10820 list_del_init(&hrq->list);
10821 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010822 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010823 return status;
10824}
10825
10826/**
10827 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10828 * @phba: The virtual port for which this call being executed.
10829 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10830 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10831 * @xritag: the xritag that ties this io to the SGL pages.
10832 *
10833 * This routine will post the sgl pages for the IO that has the xritag
10834 * that is in the iocbq structure. The xritag is assigned during iocbq
10835 * creation and persists for as long as the driver is loaded.
10836 * if the caller has fewer than 256 scatter gather segments to map then
10837 * pdma_phys_addr1 should be 0.
10838 * If the caller needs to map more than 256 scatter gather segment then
10839 * pdma_phys_addr1 should be a valid physical address.
10840 * physical address for SGLs must be 64 byte aligned.
10841 * If you are going to map 2 SGL's then the first one must have 256 entries
10842 * the second sgl can have between 1 and 256 entries.
10843 *
10844 * Return codes:
10845 * 0 - Success
10846 * -ENXIO, -ENOMEM - Failure
10847 **/
10848int
10849lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10850 dma_addr_t pdma_phys_addr0,
10851 dma_addr_t pdma_phys_addr1,
10852 uint16_t xritag)
10853{
10854 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10855 LPFC_MBOXQ_t *mbox;
10856 int rc;
10857 uint32_t shdr_status, shdr_add_status;
10858 union lpfc_sli4_cfg_shdr *shdr;
10859
10860 if (xritag == NO_XRI) {
10861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10862 "0364 Invalid param:\n");
10863 return -EINVAL;
10864 }
10865
10866 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10867 if (!mbox)
10868 return -ENOMEM;
10869
10870 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10871 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10872 sizeof(struct lpfc_mbx_post_sgl_pages) -
10873 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10874
10875 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10876 &mbox->u.mqe.un.post_sgl_pages;
10877 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10878 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10879
10880 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10881 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10882 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10883 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10884
10885 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10886 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10887 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10888 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10889 if (!phba->sli4_hba.intr_enable)
10890 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10891 else
10892 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10893 /* The IOCTL status is embedded in the mailbox subheader. */
10894 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10895 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10896 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10897 if (rc != MBX_TIMEOUT)
10898 mempool_free(mbox, phba->mbox_mem_pool);
10899 if (shdr_status || shdr_add_status || rc) {
10900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10901 "2511 POST_SGL mailbox failed with "
10902 "status x%x add_status x%x, mbx status x%x\n",
10903 shdr_status, shdr_add_status, rc);
10904 rc = -ENXIO;
10905 }
10906 return 0;
10907}
James Smart4f774512009-05-22 14:52:35 -040010908
10909/**
10910 * lpfc_sli4_next_xritag - Get an xritag for the io
10911 * @phba: Pointer to HBA context object.
10912 *
10913 * This function gets an xritag for the iocb. If there is no unused xritag
10914 * it will return 0xffff.
10915 * The function returns the allocated xritag if successful, else returns zero.
10916 * Zero is not a valid xritag.
10917 * The caller is not required to hold any lock.
10918 **/
10919uint16_t
10920lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10921{
10922 uint16_t xritag;
10923
10924 spin_lock_irq(&phba->hbalock);
10925 xritag = phba->sli4_hba.next_xri;
10926 if ((xritag != (uint16_t) -1) && xritag <
10927 (phba->sli4_hba.max_cfg_param.max_xri
10928 + phba->sli4_hba.max_cfg_param.xri_base)) {
10929 phba->sli4_hba.next_xri++;
10930 phba->sli4_hba.max_cfg_param.xri_used++;
10931 spin_unlock_irq(&phba->hbalock);
10932 return xritag;
10933 }
10934 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010935 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010936 "2004 Failed to allocate XRI.last XRITAG is %d"
10937 " Max XRI is %d, Used XRI is %d\n",
10938 phba->sli4_hba.next_xri,
10939 phba->sli4_hba.max_cfg_param.max_xri,
10940 phba->sli4_hba.max_cfg_param.xri_used);
10941 return -1;
10942}
10943
10944/**
10945 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10946 * @phba: pointer to lpfc hba data structure.
10947 *
10948 * This routine is invoked to post a block of driver's sgl pages to the
10949 * HBA using non-embedded mailbox command. No Lock is held. This routine
10950 * is only called when the driver is loading and after all IO has been
10951 * stopped.
10952 **/
10953int
10954lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10955{
10956 struct lpfc_sglq *sglq_entry;
10957 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10958 struct sgl_page_pairs *sgl_pg_pairs;
10959 void *viraddr;
10960 LPFC_MBOXQ_t *mbox;
10961 uint32_t reqlen, alloclen, pg_pairs;
10962 uint32_t mbox_tmo;
10963 uint16_t xritag_start = 0;
10964 int els_xri_cnt, rc = 0;
10965 uint32_t shdr_status, shdr_add_status;
10966 union lpfc_sli4_cfg_shdr *shdr;
10967
10968 /* The number of sgls to be posted */
10969 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10970
10971 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10972 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010973 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010974 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10975 "2559 Block sgl registration required DMA "
10976 "size (%d) great than a page\n", reqlen);
10977 return -ENOMEM;
10978 }
10979 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10980 if (!mbox) {
10981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10982 "2560 Failed to allocate mbox cmd memory\n");
10983 return -ENOMEM;
10984 }
10985
10986 /* Allocate DMA memory and set up the non-embedded mailbox command */
10987 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10988 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10989 LPFC_SLI4_MBX_NEMBED);
10990
10991 if (alloclen < reqlen) {
10992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10993 "0285 Allocated DMA memory size (%d) is "
10994 "less than the requested DMA memory "
10995 "size (%d)\n", alloclen, reqlen);
10996 lpfc_sli4_mbox_cmd_free(phba, mbox);
10997 return -ENOMEM;
10998 }
James Smart4f774512009-05-22 14:52:35 -040010999 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011000 viraddr = mbox->sge_array->addr[0];
11001
11002 /* Set up the SGL pages in the non-embedded DMA pages */
11003 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11004 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11005
11006 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11007 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11008 /* Set up the sge entry */
11009 sgl_pg_pairs->sgl_pg0_addr_lo =
11010 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11011 sgl_pg_pairs->sgl_pg0_addr_hi =
11012 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11013 sgl_pg_pairs->sgl_pg1_addr_lo =
11014 cpu_to_le32(putPaddrLow(0));
11015 sgl_pg_pairs->sgl_pg1_addr_hi =
11016 cpu_to_le32(putPaddrHigh(0));
11017 /* Keep the first xritag on the list */
11018 if (pg_pairs == 0)
11019 xritag_start = sglq_entry->sli4_xritag;
11020 sgl_pg_pairs++;
11021 }
11022 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040011023 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040011024 /* Perform endian conversion if necessary */
11025 sgl->word0 = cpu_to_le32(sgl->word0);
11026
11027 if (!phba->sli4_hba.intr_enable)
11028 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11029 else {
11030 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11031 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11032 }
11033 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11034 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11035 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11036 if (rc != MBX_TIMEOUT)
11037 lpfc_sli4_mbox_cmd_free(phba, mbox);
11038 if (shdr_status || shdr_add_status || rc) {
11039 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11040 "2513 POST_SGL_BLOCK mailbox command failed "
11041 "status x%x add_status x%x mbx status x%x\n",
11042 shdr_status, shdr_add_status, rc);
11043 rc = -ENXIO;
11044 }
11045 return rc;
11046}
11047
11048/**
11049 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11050 * @phba: pointer to lpfc hba data structure.
11051 * @sblist: pointer to scsi buffer list.
11052 * @count: number of scsi buffers on the list.
11053 *
11054 * This routine is invoked to post a block of @count scsi sgl pages from a
11055 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11056 * No Lock is held.
11057 *
11058 **/
11059int
11060lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11061 int cnt)
11062{
11063 struct lpfc_scsi_buf *psb;
11064 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11065 struct sgl_page_pairs *sgl_pg_pairs;
11066 void *viraddr;
11067 LPFC_MBOXQ_t *mbox;
11068 uint32_t reqlen, alloclen, pg_pairs;
11069 uint32_t mbox_tmo;
11070 uint16_t xritag_start = 0;
11071 int rc = 0;
11072 uint32_t shdr_status, shdr_add_status;
11073 dma_addr_t pdma_phys_bpl1;
11074 union lpfc_sli4_cfg_shdr *shdr;
11075
11076 /* Calculate the requested length of the dma memory */
11077 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11078 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011079 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011080 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11081 "0217 Block sgl registration required DMA "
11082 "size (%d) great than a page\n", reqlen);
11083 return -ENOMEM;
11084 }
11085 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11086 if (!mbox) {
11087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11088 "0283 Failed to allocate mbox cmd memory\n");
11089 return -ENOMEM;
11090 }
11091
11092 /* Allocate DMA memory and set up the non-embedded mailbox command */
11093 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11094 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11095 LPFC_SLI4_MBX_NEMBED);
11096
11097 if (alloclen < reqlen) {
11098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11099 "2561 Allocated DMA memory size (%d) is "
11100 "less than the requested DMA memory "
11101 "size (%d)\n", alloclen, reqlen);
11102 lpfc_sli4_mbox_cmd_free(phba, mbox);
11103 return -ENOMEM;
11104 }
James Smart4f774512009-05-22 14:52:35 -040011105 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011106 viraddr = mbox->sge_array->addr[0];
11107
11108 /* Set up the SGL pages in the non-embedded DMA pages */
11109 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11110 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11111
11112 pg_pairs = 0;
11113 list_for_each_entry(psb, sblist, list) {
11114 /* Set up the sge entry */
11115 sgl_pg_pairs->sgl_pg0_addr_lo =
11116 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11117 sgl_pg_pairs->sgl_pg0_addr_hi =
11118 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11119 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11120 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11121 else
11122 pdma_phys_bpl1 = 0;
11123 sgl_pg_pairs->sgl_pg1_addr_lo =
11124 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11125 sgl_pg_pairs->sgl_pg1_addr_hi =
11126 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11127 /* Keep the first xritag on the list */
11128 if (pg_pairs == 0)
11129 xritag_start = psb->cur_iocbq.sli4_xritag;
11130 sgl_pg_pairs++;
11131 pg_pairs++;
11132 }
11133 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11134 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11135 /* Perform endian conversion if necessary */
11136 sgl->word0 = cpu_to_le32(sgl->word0);
11137
11138 if (!phba->sli4_hba.intr_enable)
11139 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11140 else {
11141 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11142 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11143 }
11144 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11145 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11146 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11147 if (rc != MBX_TIMEOUT)
11148 lpfc_sli4_mbox_cmd_free(phba, mbox);
11149 if (shdr_status || shdr_add_status || rc) {
11150 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11151 "2564 POST_SGL_BLOCK mailbox command failed "
11152 "status x%x add_status x%x mbx status x%x\n",
11153 shdr_status, shdr_add_status, rc);
11154 rc = -ENXIO;
11155 }
11156 return rc;
11157}
11158
11159/**
11160 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11161 * @phba: pointer to lpfc_hba struct that the frame was received on
11162 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11163 *
11164 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11165 * valid type of frame that the LPFC driver will handle. This function will
11166 * return a zero if the frame is a valid frame or a non zero value when the
11167 * frame does not pass the check.
11168 **/
11169static int
11170lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11171{
11172 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11173 char *type_names[] = FC_TYPE_NAMES_INIT;
11174 struct fc_vft_header *fc_vft_hdr;
11175
11176 switch (fc_hdr->fh_r_ctl) {
11177 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11178 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11179 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11180 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11181 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11182 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11183 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11184 case FC_RCTL_DD_CMD_STATUS: /* command status */
11185 case FC_RCTL_ELS_REQ: /* extended link services request */
11186 case FC_RCTL_ELS_REP: /* extended link services reply */
11187 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11188 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11189 case FC_RCTL_BA_NOP: /* basic link service NOP */
11190 case FC_RCTL_BA_ABTS: /* basic link service abort */
11191 case FC_RCTL_BA_RMC: /* remove connection */
11192 case FC_RCTL_BA_ACC: /* basic accept */
11193 case FC_RCTL_BA_RJT: /* basic reject */
11194 case FC_RCTL_BA_PRMT:
11195 case FC_RCTL_ACK_1: /* acknowledge_1 */
11196 case FC_RCTL_ACK_0: /* acknowledge_0 */
11197 case FC_RCTL_P_RJT: /* port reject */
11198 case FC_RCTL_F_RJT: /* fabric reject */
11199 case FC_RCTL_P_BSY: /* port busy */
11200 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11201 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11202 case FC_RCTL_LCR: /* link credit reset */
11203 case FC_RCTL_END: /* end */
11204 break;
11205 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11206 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11207 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11208 return lpfc_fc_frame_check(phba, fc_hdr);
11209 default:
11210 goto drop;
11211 }
11212 switch (fc_hdr->fh_type) {
11213 case FC_TYPE_BLS:
11214 case FC_TYPE_ELS:
11215 case FC_TYPE_FCP:
11216 case FC_TYPE_CT:
11217 break;
11218 case FC_TYPE_IP:
11219 case FC_TYPE_ILS:
11220 default:
11221 goto drop;
11222 }
11223 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11224 "2538 Received frame rctl:%s type:%s\n",
11225 rctl_names[fc_hdr->fh_r_ctl],
11226 type_names[fc_hdr->fh_type]);
11227 return 0;
11228drop:
11229 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11230 "2539 Dropped frame rctl:%s type:%s\n",
11231 rctl_names[fc_hdr->fh_r_ctl],
11232 type_names[fc_hdr->fh_type]);
11233 return 1;
11234}
11235
11236/**
11237 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11238 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11239 *
11240 * This function processes the FC header to retrieve the VFI from the VF
11241 * header, if one exists. This function will return the VFI if one exists
11242 * or 0 if no VSAN Header exists.
11243 **/
11244static uint32_t
11245lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11246{
11247 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11248
11249 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11250 return 0;
11251 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11252}
11253
11254/**
11255 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11256 * @phba: Pointer to the HBA structure to search for the vport on
11257 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11258 * @fcfi: The FC Fabric ID that the frame came from
11259 *
11260 * This function searches the @phba for a vport that matches the content of the
11261 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11262 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11263 * returns the matching vport pointer or NULL if unable to match frame to a
11264 * vport.
11265 **/
11266static struct lpfc_vport *
11267lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11268 uint16_t fcfi)
11269{
11270 struct lpfc_vport **vports;
11271 struct lpfc_vport *vport = NULL;
11272 int i;
11273 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11274 fc_hdr->fh_d_id[1] << 8 |
11275 fc_hdr->fh_d_id[2]);
11276
11277 vports = lpfc_create_vport_work_array(phba);
11278 if (vports != NULL)
11279 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11280 if (phba->fcf.fcfi == fcfi &&
11281 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11282 vports[i]->fc_myDID == did) {
11283 vport = vports[i];
11284 break;
11285 }
11286 }
11287 lpfc_destroy_vport_work_array(phba, vports);
11288 return vport;
11289}
11290
11291/**
James Smart45ed1192009-10-02 15:17:02 -040011292 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11293 * @vport: The vport to work on.
11294 *
11295 * This function updates the receive sequence time stamp for this vport. The
11296 * receive sequence time stamp indicates the time that the last frame of the
11297 * the sequence that has been idle for the longest amount of time was received.
11298 * the driver uses this time stamp to indicate if any received sequences have
11299 * timed out.
11300 **/
11301void
11302lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11303{
11304 struct lpfc_dmabuf *h_buf;
11305 struct hbq_dmabuf *dmabuf = NULL;
11306
11307 /* get the oldest sequence on the rcv list */
11308 h_buf = list_get_first(&vport->rcv_buffer_list,
11309 struct lpfc_dmabuf, list);
11310 if (!h_buf)
11311 return;
11312 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11313 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11314}
11315
11316/**
11317 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11318 * @vport: The vport that the received sequences were sent to.
11319 *
11320 * This function cleans up all outstanding received sequences. This is called
11321 * by the driver when a link event or user action invalidates all the received
11322 * sequences.
11323 **/
11324void
11325lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11326{
11327 struct lpfc_dmabuf *h_buf, *hnext;
11328 struct lpfc_dmabuf *d_buf, *dnext;
11329 struct hbq_dmabuf *dmabuf = NULL;
11330
11331 /* start with the oldest sequence on the rcv list */
11332 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11333 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11334 list_del_init(&dmabuf->hbuf.list);
11335 list_for_each_entry_safe(d_buf, dnext,
11336 &dmabuf->dbuf.list, list) {
11337 list_del_init(&d_buf->list);
11338 lpfc_in_buf_free(vport->phba, d_buf);
11339 }
11340 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11341 }
11342}
11343
11344/**
11345 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11346 * @vport: The vport that the received sequences were sent to.
11347 *
11348 * This function determines whether any received sequences have timed out by
11349 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11350 * indicates that there is at least one timed out sequence this routine will
11351 * go through the received sequences one at a time from most inactive to most
11352 * active to determine which ones need to be cleaned up. Once it has determined
11353 * that a sequence needs to be cleaned up it will simply free up the resources
11354 * without sending an abort.
11355 **/
11356void
11357lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11358{
11359 struct lpfc_dmabuf *h_buf, *hnext;
11360 struct lpfc_dmabuf *d_buf, *dnext;
11361 struct hbq_dmabuf *dmabuf = NULL;
11362 unsigned long timeout;
11363 int abort_count = 0;
11364
11365 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11366 vport->rcv_buffer_time_stamp);
11367 if (list_empty(&vport->rcv_buffer_list) ||
11368 time_before(jiffies, timeout))
11369 return;
11370 /* start with the oldest sequence on the rcv list */
11371 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11372 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11373 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11374 dmabuf->time_stamp);
11375 if (time_before(jiffies, timeout))
11376 break;
11377 abort_count++;
11378 list_del_init(&dmabuf->hbuf.list);
11379 list_for_each_entry_safe(d_buf, dnext,
11380 &dmabuf->dbuf.list, list) {
11381 list_del_init(&d_buf->list);
11382 lpfc_in_buf_free(vport->phba, d_buf);
11383 }
11384 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11385 }
11386 if (abort_count)
11387 lpfc_update_rcv_time_stamp(vport);
11388}
11389
11390/**
James Smart4f774512009-05-22 14:52:35 -040011391 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11392 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11393 *
11394 * This function searches through the existing incomplete sequences that have
11395 * been sent to this @vport. If the frame matches one of the incomplete
11396 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11397 * make up that sequence. If no sequence is found that matches this frame then
11398 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11399 * This function returns a pointer to the first dmabuf in the sequence list that
11400 * the frame was linked to.
11401 **/
11402static struct hbq_dmabuf *
11403lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11404{
11405 struct fc_frame_header *new_hdr;
11406 struct fc_frame_header *temp_hdr;
11407 struct lpfc_dmabuf *d_buf;
11408 struct lpfc_dmabuf *h_buf;
11409 struct hbq_dmabuf *seq_dmabuf = NULL;
11410 struct hbq_dmabuf *temp_dmabuf = NULL;
11411
James Smart4d9ab992009-10-02 15:16:39 -040011412 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011413 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011414 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11415 /* Use the hdr_buf to find the sequence that this frame belongs to */
11416 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11417 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11418 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11419 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11420 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11421 continue;
11422 /* found a pending sequence that matches this frame */
11423 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11424 break;
11425 }
11426 if (!seq_dmabuf) {
11427 /*
11428 * This indicates first frame received for this sequence.
11429 * Queue the buffer on the vport's rcv_buffer_list.
11430 */
11431 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011432 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011433 return dmabuf;
11434 }
11435 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011436 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11437 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011438 list_del_init(&seq_dmabuf->hbuf.list);
11439 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11440 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011441 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011442 return dmabuf;
11443 }
James Smart45ed1192009-10-02 15:17:02 -040011444 /* move this sequence to the tail to indicate a young sequence */
11445 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11446 seq_dmabuf->time_stamp = jiffies;
11447 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011448 if (list_empty(&seq_dmabuf->dbuf.list)) {
11449 temp_hdr = dmabuf->hbuf.virt;
11450 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11451 return seq_dmabuf;
11452 }
James Smart4f774512009-05-22 14:52:35 -040011453 /* find the correct place in the sequence to insert this frame */
11454 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11455 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11456 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11457 /*
11458 * If the frame's sequence count is greater than the frame on
11459 * the list then insert the frame right after this frame
11460 */
James Smarteeead812009-12-21 17:01:23 -050011461 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11462 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011463 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11464 return seq_dmabuf;
11465 }
11466 }
11467 return NULL;
11468}
11469
11470/**
James Smart6669f9b2009-10-02 15:16:45 -040011471 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11472 * @vport: pointer to a vitural port
11473 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11474 *
11475 * This function tries to abort from the partially assembed sequence, described
11476 * by the information from basic abbort @dmabuf. It checks to see whether such
11477 * partially assembled sequence held by the driver. If so, it shall free up all
11478 * the frames from the partially assembled sequence.
11479 *
11480 * Return
11481 * true -- if there is matching partially assembled sequence present and all
11482 * the frames freed with the sequence;
11483 * false -- if there is no matching partially assembled sequence present so
11484 * nothing got aborted in the lower layer driver
11485 **/
11486static bool
11487lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11488 struct hbq_dmabuf *dmabuf)
11489{
11490 struct fc_frame_header *new_hdr;
11491 struct fc_frame_header *temp_hdr;
11492 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11493 struct hbq_dmabuf *seq_dmabuf = NULL;
11494
11495 /* Use the hdr_buf to find the sequence that matches this frame */
11496 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11497 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11498 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11499 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11500 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11501 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11502 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11503 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11504 continue;
11505 /* found a pending sequence that matches this frame */
11506 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11507 break;
11508 }
11509
11510 /* Free up all the frames from the partially assembled sequence */
11511 if (seq_dmabuf) {
11512 list_for_each_entry_safe(d_buf, n_buf,
11513 &seq_dmabuf->dbuf.list, list) {
11514 list_del_init(&d_buf->list);
11515 lpfc_in_buf_free(vport->phba, d_buf);
11516 }
11517 return true;
11518 }
11519 return false;
11520}
11521
11522/**
11523 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11524 * @phba: Pointer to HBA context object.
11525 * @cmd_iocbq: pointer to the command iocbq structure.
11526 * @rsp_iocbq: pointer to the response iocbq structure.
11527 *
11528 * This function handles the sequence abort accept iocb command complete
11529 * event. It properly releases the memory allocated to the sequence abort
11530 * accept iocb.
11531 **/
11532static void
11533lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11534 struct lpfc_iocbq *cmd_iocbq,
11535 struct lpfc_iocbq *rsp_iocbq)
11536{
11537 if (cmd_iocbq)
11538 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11539}
11540
11541/**
11542 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11543 * @phba: Pointer to HBA context object.
11544 * @fc_hdr: pointer to a FC frame header.
11545 *
11546 * This function sends a basic accept to a previous unsol sequence abort
11547 * event after aborting the sequence handling.
11548 **/
11549static void
11550lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11551 struct fc_frame_header *fc_hdr)
11552{
11553 struct lpfc_iocbq *ctiocb = NULL;
11554 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011555 uint16_t oxid, rxid;
11556 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011557 IOCB_t *icmd;
11558
11559 if (!lpfc_is_link_up(phba))
11560 return;
11561
11562 sid = sli4_sid_from_fc_hdr(fc_hdr);
11563 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011564 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011565
11566 ndlp = lpfc_findnode_did(phba->pport, sid);
11567 if (!ndlp) {
11568 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11569 "1268 Find ndlp returned NULL for oxid:x%x "
11570 "SID:x%x\n", oxid, sid);
11571 return;
11572 }
11573
11574 /* Allocate buffer for acc iocb */
11575 ctiocb = lpfc_sli_get_iocbq(phba);
11576 if (!ctiocb)
11577 return;
11578
James Smart5ffc2662009-11-18 15:39:44 -050011579 /* Extract the F_CTL field from FC_HDR */
11580 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11581
James Smart6669f9b2009-10-02 15:16:45 -040011582 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011583 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011584 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011585 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11586 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11587 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11588
11589 /* Fill in the rest of iocb fields */
11590 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11591 icmd->ulpBdeCount = 0;
11592 icmd->ulpLe = 1;
11593 icmd->ulpClass = CLASS3;
11594 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011595
James Smart6669f9b2009-10-02 15:16:45 -040011596 ctiocb->iocb_cmpl = NULL;
11597 ctiocb->vport = phba->pport;
11598 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11599
James Smart5ffc2662009-11-18 15:39:44 -050011600 if (fctl & FC_FC_EX_CTX) {
11601 /* ABTS sent by responder to CT exchange, construction
11602 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11603 * field and RX_ID from ABTS for RX_ID field.
11604 */
11605 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11606 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11607 ctiocb->sli4_xritag = oxid;
11608 } else {
11609 /* ABTS sent by initiator to CT exchange, construction
11610 * of BA_ACC will need to allocate a new XRI as for the
11611 * XRI_TAG and RX_ID fields.
11612 */
11613 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11614 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11615 ctiocb->sli4_xritag = NO_XRI;
11616 }
11617 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11618
James Smart6669f9b2009-10-02 15:16:45 -040011619 /* Xmit CT abts accept on exchange <xid> */
11620 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11621 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11622 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11623 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11624}
11625
11626/**
11627 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11628 * @vport: Pointer to the vport on which this sequence was received
11629 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11630 *
11631 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11632 * receive sequence is only partially assembed by the driver, it shall abort
11633 * the partially assembled frames for the sequence. Otherwise, if the
11634 * unsolicited receive sequence has been completely assembled and passed to
11635 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11636 * unsolicited sequence has been aborted. After that, it will issue a basic
11637 * accept to accept the abort.
11638 **/
11639void
11640lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11641 struct hbq_dmabuf *dmabuf)
11642{
11643 struct lpfc_hba *phba = vport->phba;
11644 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011645 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011646 bool abts_par;
11647
James Smart6669f9b2009-10-02 15:16:45 -040011648 /* Make a copy of fc_hdr before the dmabuf being released */
11649 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011650 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011651
James Smart5ffc2662009-11-18 15:39:44 -050011652 if (fctl & FC_FC_EX_CTX) {
11653 /*
11654 * ABTS sent by responder to exchange, just free the buffer
11655 */
James Smart6669f9b2009-10-02 15:16:45 -040011656 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011657 } else {
11658 /*
11659 * ABTS sent by initiator to exchange, need to do cleanup
11660 */
11661 /* Try to abort partially assembled seq */
11662 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11663
11664 /* Send abort to ULP if partially seq abort failed */
11665 if (abts_par == false)
11666 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11667 else
11668 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11669 }
James Smart6669f9b2009-10-02 15:16:45 -040011670 /* Send basic accept (BA_ACC) to the abort requester */
11671 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11672}
11673
11674/**
James Smart4f774512009-05-22 14:52:35 -040011675 * lpfc_seq_complete - Indicates if a sequence is complete
11676 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11677 *
11678 * This function checks the sequence, starting with the frame described by
11679 * @dmabuf, to see if all the frames associated with this sequence are present.
11680 * the frames associated with this sequence are linked to the @dmabuf using the
11681 * dbuf list. This function looks for two major things. 1) That the first frame
11682 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11683 * set. 3) That there are no holes in the sequence count. The function will
11684 * return 1 when the sequence is complete, otherwise it will return 0.
11685 **/
11686static int
11687lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11688{
11689 struct fc_frame_header *hdr;
11690 struct lpfc_dmabuf *d_buf;
11691 struct hbq_dmabuf *seq_dmabuf;
11692 uint32_t fctl;
11693 int seq_count = 0;
11694
11695 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11696 /* make sure first fame of sequence has a sequence count of zero */
11697 if (hdr->fh_seq_cnt != seq_count)
11698 return 0;
11699 fctl = (hdr->fh_f_ctl[0] << 16 |
11700 hdr->fh_f_ctl[1] << 8 |
11701 hdr->fh_f_ctl[2]);
11702 /* If last frame of sequence we can return success. */
11703 if (fctl & FC_FC_END_SEQ)
11704 return 1;
11705 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11706 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11707 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11708 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011709 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011710 return 0;
11711 fctl = (hdr->fh_f_ctl[0] << 16 |
11712 hdr->fh_f_ctl[1] << 8 |
11713 hdr->fh_f_ctl[2]);
11714 /* If last frame of sequence we can return success. */
11715 if (fctl & FC_FC_END_SEQ)
11716 return 1;
11717 }
11718 return 0;
11719}
11720
11721/**
11722 * lpfc_prep_seq - Prep sequence for ULP processing
11723 * @vport: Pointer to the vport on which this sequence was received
11724 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11725 *
11726 * This function takes a sequence, described by a list of frames, and creates
11727 * a list of iocbq structures to describe the sequence. This iocbq list will be
11728 * used to issue to the generic unsolicited sequence handler. This routine
11729 * returns a pointer to the first iocbq in the list. If the function is unable
11730 * to allocate an iocbq then it throw out the received frames that were not
11731 * able to be described and return a pointer to the first iocbq. If unable to
11732 * allocate any iocbqs (including the first) this function will return NULL.
11733 **/
11734static struct lpfc_iocbq *
11735lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11736{
11737 struct lpfc_dmabuf *d_buf, *n_buf;
11738 struct lpfc_iocbq *first_iocbq, *iocbq;
11739 struct fc_frame_header *fc_hdr;
11740 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011741 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011742
11743 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11744 /* remove from receive buffer list */
11745 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011746 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011747 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011748 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011749 /* Get an iocbq struct to fill in. */
11750 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11751 if (first_iocbq) {
11752 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011753 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011754 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11755 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11756 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11757 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11758 vport->vpi + vport->phba->vpi_base;
11759 /* put the first buffer into the first IOCBq */
11760 first_iocbq->context2 = &seq_dmabuf->dbuf;
11761 first_iocbq->context3 = NULL;
11762 first_iocbq->iocb.ulpBdeCount = 1;
11763 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11764 LPFC_DATA_BUF_SIZE;
11765 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011766 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011767 bf_get(lpfc_rcqe_length,
11768 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011769 }
11770 iocbq = first_iocbq;
11771 /*
11772 * Each IOCBq can have two Buffers assigned, so go through the list
11773 * of buffers for this sequence and save two buffers in each IOCBq
11774 */
11775 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11776 if (!iocbq) {
11777 lpfc_in_buf_free(vport->phba, d_buf);
11778 continue;
11779 }
11780 if (!iocbq->context3) {
11781 iocbq->context3 = d_buf;
11782 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011783 pbde = (struct ulp_bde64 *)
11784 &iocbq->iocb.unsli3.sli3Words[4];
11785 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011786 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011787 bf_get(lpfc_rcqe_length,
11788 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011789 } else {
11790 iocbq = lpfc_sli_get_iocbq(vport->phba);
11791 if (!iocbq) {
11792 if (first_iocbq) {
11793 first_iocbq->iocb.ulpStatus =
11794 IOSTAT_FCP_RSP_ERROR;
11795 first_iocbq->iocb.un.ulpWord[4] =
11796 IOERR_NO_RESOURCES;
11797 }
11798 lpfc_in_buf_free(vport->phba, d_buf);
11799 continue;
11800 }
11801 iocbq->context2 = d_buf;
11802 iocbq->context3 = NULL;
11803 iocbq->iocb.ulpBdeCount = 1;
11804 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11805 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011806 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011807 bf_get(lpfc_rcqe_length,
11808 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011809 iocbq->iocb.un.rcvels.remoteID = sid;
11810 list_add_tail(&iocbq->list, &first_iocbq->list);
11811 }
11812 }
11813 return first_iocbq;
11814}
11815
James Smart6669f9b2009-10-02 15:16:45 -040011816static void
11817lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11818 struct hbq_dmabuf *seq_dmabuf)
11819{
11820 struct fc_frame_header *fc_hdr;
11821 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11822 struct lpfc_hba *phba = vport->phba;
11823
11824 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11825 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11826 if (!iocbq) {
11827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11828 "2707 Ring %d handler: Failed to allocate "
11829 "iocb Rctl x%x Type x%x received\n",
11830 LPFC_ELS_RING,
11831 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11832 return;
11833 }
11834 if (!lpfc_complete_unsol_iocb(phba,
11835 &phba->sli.ring[LPFC_ELS_RING],
11836 iocbq, fc_hdr->fh_r_ctl,
11837 fc_hdr->fh_type))
11838 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11839 "2540 Ring %d handler: unexpected Rctl "
11840 "x%x Type x%x received\n",
11841 LPFC_ELS_RING,
11842 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11843
11844 /* Free iocb created in lpfc_prep_seq */
11845 list_for_each_entry_safe(curr_iocb, next_iocb,
11846 &iocbq->list, list) {
11847 list_del_init(&curr_iocb->list);
11848 lpfc_sli_release_iocbq(phba, curr_iocb);
11849 }
11850 lpfc_sli_release_iocbq(phba, iocbq);
11851}
11852
James Smart4f774512009-05-22 14:52:35 -040011853/**
11854 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11855 * @phba: Pointer to HBA context object.
11856 *
11857 * This function is called with no lock held. This function processes all
11858 * the received buffers and gives it to upper layers when a received buffer
11859 * indicates that it is the final frame in the sequence. The interrupt
11860 * service routine processes received buffers at interrupt contexts and adds
11861 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11862 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11863 * appropriate receive function when the final frame in a sequence is received.
11864 **/
James Smart4d9ab992009-10-02 15:16:39 -040011865void
11866lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11867 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011868{
James Smart4d9ab992009-10-02 15:16:39 -040011869 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011870 struct fc_frame_header *fc_hdr;
11871 struct lpfc_vport *vport;
11872 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011873
James Smart4f774512009-05-22 14:52:35 -040011874 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011875 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11876 /* check to see if this a valid type of frame */
11877 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11878 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11879 return;
11880 }
11881 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11882 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011883 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011884 /* throw out the frame */
11885 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11886 return;
11887 }
James Smart6669f9b2009-10-02 15:16:45 -040011888 /* Handle the basic abort sequence (BA_ABTS) event */
11889 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11890 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11891 return;
11892 }
11893
James Smart4d9ab992009-10-02 15:16:39 -040011894 /* Link this frame */
11895 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11896 if (!seq_dmabuf) {
11897 /* unable to add frame to vport - throw it out */
11898 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11899 return;
11900 }
11901 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011902 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011903 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011904
James Smart6669f9b2009-10-02 15:16:45 -040011905 /* Send the complete sequence to the upper layer protocol */
11906 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011907}
James Smart6fb120a2009-05-22 14:52:59 -040011908
11909/**
11910 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11911 * @phba: pointer to lpfc hba data structure.
11912 *
11913 * This routine is invoked to post rpi header templates to the
11914 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011915 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11916 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011917 *
11918 * This routine does not require any locks. It's usage is expected
11919 * to be driver load or reset recovery when the driver is
11920 * sequential.
11921 *
11922 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011923 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040011924 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040011925 * When this error occurs, the driver is not guaranteed
11926 * to have any rpi regions posted to the device and
11927 * must either attempt to repost the regions or take a
11928 * fatal error.
11929 **/
11930int
11931lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11932{
11933 struct lpfc_rpi_hdr *rpi_page;
11934 uint32_t rc = 0;
11935
11936 /* Post all rpi memory regions to the port. */
11937 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11938 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11939 if (rc != MBX_SUCCESS) {
11940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11941 "2008 Error %d posting all rpi "
11942 "headers\n", rc);
11943 rc = -EIO;
11944 break;
11945 }
11946 }
11947
11948 return rc;
11949}
11950
11951/**
11952 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11953 * @phba: pointer to lpfc hba data structure.
11954 * @rpi_page: pointer to the rpi memory region.
11955 *
11956 * This routine is invoked to post a single rpi header to the
11957 * HBA consistent with the SLI-4 interface spec. This memory region
11958 * maps up to 64 rpi context regions.
11959 *
11960 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011961 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040011962 * -ENOMEM - No available memory
11963 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040011964 **/
11965int
11966lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11967{
11968 LPFC_MBOXQ_t *mboxq;
11969 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11970 uint32_t rc = 0;
11971 uint32_t mbox_tmo;
11972 uint32_t shdr_status, shdr_add_status;
11973 union lpfc_sli4_cfg_shdr *shdr;
11974
11975 /* The port is notified of the header region via a mailbox command. */
11976 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11977 if (!mboxq) {
11978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11979 "2001 Unable to allocate memory for issuing "
11980 "SLI_CONFIG_SPECIAL mailbox command\n");
11981 return -ENOMEM;
11982 }
11983
11984 /* Post all rpi memory regions to the port. */
11985 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11986 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11987 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11988 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11989 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11990 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11991 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11992 hdr_tmpl, rpi_page->page_count);
11993 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11994 rpi_page->start_rpi);
11995 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11996 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011997 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011998 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11999 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12000 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12001 if (rc != MBX_TIMEOUT)
12002 mempool_free(mboxq, phba->mbox_mem_pool);
12003 if (shdr_status || shdr_add_status || rc) {
12004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12005 "2514 POST_RPI_HDR mailbox failed with "
12006 "status x%x add_status x%x, mbx status x%x\n",
12007 shdr_status, shdr_add_status, rc);
12008 rc = -ENXIO;
12009 }
12010 return rc;
12011}
12012
12013/**
12014 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12015 * @phba: pointer to lpfc hba data structure.
12016 *
12017 * This routine is invoked to post rpi header templates to the
12018 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012019 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12020 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012021 *
12022 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012023 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040012024 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12025 **/
12026int
12027lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12028{
12029 int rpi;
12030 uint16_t max_rpi, rpi_base, rpi_limit;
12031 uint16_t rpi_remaining;
12032 struct lpfc_rpi_hdr *rpi_hdr;
12033
12034 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12035 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12036 rpi_limit = phba->sli4_hba.next_rpi;
12037
12038 /*
12039 * The valid rpi range is not guaranteed to be zero-based. Start
12040 * the search at the rpi_base as reported by the port.
12041 */
12042 spin_lock_irq(&phba->hbalock);
12043 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12044 if (rpi >= rpi_limit || rpi < rpi_base)
12045 rpi = LPFC_RPI_ALLOC_ERROR;
12046 else {
12047 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12048 phba->sli4_hba.max_cfg_param.rpi_used++;
12049 phba->sli4_hba.rpi_count++;
12050 }
12051
12052 /*
12053 * Don't try to allocate more rpi header regions if the device limit
12054 * on available rpis max has been exhausted.
12055 */
12056 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12057 (phba->sli4_hba.rpi_count >= max_rpi)) {
12058 spin_unlock_irq(&phba->hbalock);
12059 return rpi;
12060 }
12061
12062 /*
12063 * If the driver is running low on rpi resources, allocate another
12064 * page now. Note that the next_rpi value is used because
12065 * it represents how many are actually in use whereas max_rpi notes
12066 * how many are supported max by the device.
12067 */
12068 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12069 phba->sli4_hba.rpi_count;
12070 spin_unlock_irq(&phba->hbalock);
12071 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12072 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12073 if (!rpi_hdr) {
12074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12075 "2002 Error Could not grow rpi "
12076 "count\n");
12077 } else {
12078 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12079 }
12080 }
12081
12082 return rpi;
12083}
12084
12085/**
12086 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12087 * @phba: pointer to lpfc hba data structure.
12088 *
12089 * This routine is invoked to release an rpi to the pool of
12090 * available rpis maintained by the driver.
12091 **/
12092void
James Smartd7c47992010-06-08 18:31:54 -040012093__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12094{
12095 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12096 phba->sli4_hba.rpi_count--;
12097 phba->sli4_hba.max_cfg_param.rpi_used--;
12098 }
12099}
12100
12101/**
12102 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12103 * @phba: pointer to lpfc hba data structure.
12104 *
12105 * This routine is invoked to release an rpi to the pool of
12106 * available rpis maintained by the driver.
12107 **/
12108void
James Smart6fb120a2009-05-22 14:52:59 -040012109lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12110{
12111 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012112 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012113 spin_unlock_irq(&phba->hbalock);
12114}
12115
12116/**
12117 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12118 * @phba: pointer to lpfc hba data structure.
12119 *
12120 * This routine is invoked to remove the memory region that
12121 * provided rpi via a bitmask.
12122 **/
12123void
12124lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12125{
12126 kfree(phba->sli4_hba.rpi_bmask);
12127}
12128
12129/**
12130 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12131 * @phba: pointer to lpfc hba data structure.
12132 *
12133 * This routine is invoked to remove the memory region that
12134 * provided rpi via a bitmask.
12135 **/
12136int
12137lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12138{
12139 LPFC_MBOXQ_t *mboxq;
12140 struct lpfc_hba *phba = ndlp->phba;
12141 int rc;
12142
12143 /* The port is notified of the header region via a mailbox command. */
12144 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12145 if (!mboxq)
12146 return -ENOMEM;
12147
12148 /* Post all rpi memory regions to the port. */
12149 lpfc_resume_rpi(mboxq, ndlp);
12150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12151 if (rc == MBX_NOT_FINISHED) {
12152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12153 "2010 Resume RPI Mailbox failed "
12154 "status %d, mbxStatus x%x\n", rc,
12155 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12156 mempool_free(mboxq, phba->mbox_mem_pool);
12157 return -EIO;
12158 }
12159 return 0;
12160}
12161
12162/**
12163 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050012164 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040012165 *
James Smart76a95d72010-11-20 23:11:48 -050012166 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040012167 *
12168 * Returns:
12169 * 0 success
12170 * -Evalue otherwise
12171 **/
12172int
James Smart76a95d72010-11-20 23:11:48 -050012173lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040012174{
12175 LPFC_MBOXQ_t *mboxq;
12176 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012177 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012178 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050012179 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040012180 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12181 if (!mboxq)
12182 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050012183 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012184 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12185 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012186 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050012187 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040012188 "2022 INIT VPI Mailbox failed "
12189 "status %d, mbxStatus x%x\n", rc,
12190 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012191 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012192 }
James Smart6a9c52c2009-10-02 15:16:51 -040012193 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050012194 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040012195
12196 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012197}
12198
12199/**
12200 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12201 * @phba: pointer to lpfc hba data structure.
12202 * @mboxq: Pointer to mailbox object.
12203 *
12204 * This routine is invoked to manually add a single FCF record. The caller
12205 * must pass a completely initialized FCF_Record. This routine takes
12206 * care of the nonembedded mailbox operations.
12207 **/
12208static void
12209lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12210{
12211 void *virt_addr;
12212 union lpfc_sli4_cfg_shdr *shdr;
12213 uint32_t shdr_status, shdr_add_status;
12214
12215 virt_addr = mboxq->sge_array->addr[0];
12216 /* The IOCTL status is embedded in the mailbox subheader. */
12217 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12220
12221 if ((shdr_status || shdr_add_status) &&
12222 (shdr_status != STATUS_FCF_IN_USE))
12223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12224 "2558 ADD_FCF_RECORD mailbox failed with "
12225 "status x%x add_status x%x\n",
12226 shdr_status, shdr_add_status);
12227
12228 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12229}
12230
12231/**
12232 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12233 * @phba: pointer to lpfc hba data structure.
12234 * @fcf_record: pointer to the initialized fcf record to add.
12235 *
12236 * This routine is invoked to manually add a single FCF record. The caller
12237 * must pass a completely initialized FCF_Record. This routine takes
12238 * care of the nonembedded mailbox operations.
12239 **/
12240int
12241lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12242{
12243 int rc = 0;
12244 LPFC_MBOXQ_t *mboxq;
12245 uint8_t *bytep;
12246 void *virt_addr;
12247 dma_addr_t phys_addr;
12248 struct lpfc_mbx_sge sge;
12249 uint32_t alloc_len, req_len;
12250 uint32_t fcfindex;
12251
12252 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12253 if (!mboxq) {
12254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12255 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12256 return -ENOMEM;
12257 }
12258
12259 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12260 sizeof(uint32_t);
12261
12262 /* Allocate DMA memory and set up the non-embedded mailbox command */
12263 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12264 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12265 req_len, LPFC_SLI4_MBX_NEMBED);
12266 if (alloc_len < req_len) {
12267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12268 "2523 Allocated DMA memory size (x%x) is "
12269 "less than the requested DMA memory "
12270 "size (x%x)\n", alloc_len, req_len);
12271 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12272 return -ENOMEM;
12273 }
12274
12275 /*
12276 * Get the first SGE entry from the non-embedded DMA memory. This
12277 * routine only uses a single SGE.
12278 */
12279 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12280 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012281 virt_addr = mboxq->sge_array->addr[0];
12282 /*
12283 * Configure the FCF record for FCFI 0. This is the driver's
12284 * hardcoded default and gets used in nonFIP mode.
12285 */
12286 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12287 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12288 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12289
12290 /*
12291 * Copy the fcf_index and the FCF Record Data. The data starts after
12292 * the FCoE header plus word10. The data copy needs to be endian
12293 * correct.
12294 */
12295 bytep += sizeof(uint32_t);
12296 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12297 mboxq->vport = phba->pport;
12298 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12299 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12300 if (rc == MBX_NOT_FINISHED) {
12301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12302 "2515 ADD_FCF_RECORD mailbox failed with "
12303 "status 0x%x\n", rc);
12304 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12305 rc = -EIO;
12306 } else
12307 rc = 0;
12308
12309 return rc;
12310}
12311
12312/**
12313 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12314 * @phba: pointer to lpfc hba data structure.
12315 * @fcf_record: pointer to the fcf record to write the default data.
12316 * @fcf_index: FCF table entry index.
12317 *
12318 * This routine is invoked to build the driver's default FCF record. The
12319 * values used are hardcoded. This routine handles memory initialization.
12320 *
12321 **/
12322void
12323lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12324 struct fcf_record *fcf_record,
12325 uint16_t fcf_index)
12326{
12327 memset(fcf_record, 0, sizeof(struct fcf_record));
12328 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12329 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12330 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12331 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12332 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12333 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12334 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12335 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12336 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12337 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12338 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12339 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12340 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012341 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012342 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12343 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12344 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12345 /* Set the VLAN bit map */
12346 if (phba->valid_vlan) {
12347 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12348 = 1 << (phba->vlan_id % 8);
12349 }
12350}
12351
12352/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012353 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012354 * @phba: pointer to lpfc hba data structure.
12355 * @fcf_index: FCF table entry offset.
12356 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012357 * This routine is invoked to scan the entire FCF table by reading FCF
12358 * record and processing it one at a time starting from the @fcf_index
12359 * for initial FCF discovery or fast FCF failover rediscovery.
12360 *
12361 * Return 0 if the mailbox command is submitted sucessfully, none 0
12362 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012363 **/
12364int
James Smart0c9ab6f2010-02-26 14:15:57 -050012365lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012366{
12367 int rc = 0, error;
12368 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012369
James Smart32b97932009-07-19 10:01:21 -040012370 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012371 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12372 if (!mboxq) {
12373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12374 "2000 Failed to allocate mbox for "
12375 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012376 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012377 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012378 }
James Smartecfd03c2010-02-12 14:41:27 -050012379 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012380 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012381 if (rc) {
12382 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012383 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012384 }
James Smartecfd03c2010-02-12 14:41:27 -050012385 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012386 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012387 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040012388
12389 spin_lock_irq(&phba->hbalock);
12390 phba->hba_flag |= FCF_TS_INPROG;
12391 spin_unlock_irq(&phba->hbalock);
12392
James Smart6fb120a2009-05-22 14:52:59 -040012393 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012394 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012395 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012396 else {
James Smart38b92ef2010-08-04 16:11:39 -040012397 /* Reset eligible FCF count for new scan */
12398 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040012399 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040012400 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012401 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012402fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012403 if (error) {
12404 if (mboxq)
12405 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040012406 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040012407 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040012408 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040012409 spin_unlock_irq(&phba->hbalock);
12410 }
James Smart6fb120a2009-05-22 14:52:59 -040012411 return error;
12412}
James Smarta0c87cb2009-07-19 10:01:10 -040012413
12414/**
James Smarta93ff372010-10-22 11:06:08 -040012415 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050012416 * @phba: pointer to lpfc hba data structure.
12417 * @fcf_index: FCF table entry offset.
12418 *
12419 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040012420 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050012421 *
12422 * Return 0 if the mailbox command is submitted sucessfully, none 0
12423 * otherwise.
12424 **/
12425int
12426lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12427{
12428 int rc = 0, error;
12429 LPFC_MBOXQ_t *mboxq;
12430
12431 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12432 if (!mboxq) {
12433 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12434 "2763 Failed to allocate mbox for "
12435 "READ_FCF cmd\n");
12436 error = -ENOMEM;
12437 goto fail_fcf_read;
12438 }
12439 /* Construct the read FCF record mailbox command */
12440 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12441 if (rc) {
12442 error = -EINVAL;
12443 goto fail_fcf_read;
12444 }
12445 /* Issue the mailbox command asynchronously */
12446 mboxq->vport = phba->pport;
12447 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12448 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12449 if (rc == MBX_NOT_FINISHED)
12450 error = -EIO;
12451 else
12452 error = 0;
12453
12454fail_fcf_read:
12455 if (error && mboxq)
12456 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12457 return error;
12458}
12459
12460/**
12461 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12462 * @phba: pointer to lpfc hba data structure.
12463 * @fcf_index: FCF table entry offset.
12464 *
12465 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040012466 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050012467 *
12468 * Return 0 if the mailbox command is submitted sucessfully, none 0
12469 * otherwise.
12470 **/
12471int
12472lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12473{
12474 int rc = 0, error;
12475 LPFC_MBOXQ_t *mboxq;
12476
12477 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12478 if (!mboxq) {
12479 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12480 "2758 Failed to allocate mbox for "
12481 "READ_FCF cmd\n");
12482 error = -ENOMEM;
12483 goto fail_fcf_read;
12484 }
12485 /* Construct the read FCF record mailbox command */
12486 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12487 if (rc) {
12488 error = -EINVAL;
12489 goto fail_fcf_read;
12490 }
12491 /* Issue the mailbox command asynchronously */
12492 mboxq->vport = phba->pport;
12493 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12495 if (rc == MBX_NOT_FINISHED)
12496 error = -EIO;
12497 else
12498 error = 0;
12499
12500fail_fcf_read:
12501 if (error && mboxq)
12502 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12503 return error;
12504}
12505
12506/**
12507 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12508 * @phba: pointer to lpfc hba data structure.
12509 *
12510 * This routine is to get the next eligible FCF record index in a round
12511 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040012512 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050012513 * shall be returned, otherwise, the next eligible FCF record's index
12514 * shall be returned.
12515 **/
12516uint16_t
12517lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12518{
12519 uint16_t next_fcf_index;
12520
James Smart3804dc82010-07-14 15:31:37 -040012521 /* Search start from next bit of currently registered FCF index */
12522 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12523 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050012524 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12525 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040012526 next_fcf_index);
12527
James Smart0c9ab6f2010-02-26 14:15:57 -050012528 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12529 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12530 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12531 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050012532
James Smart3804dc82010-07-14 15:31:37 -040012533 /* Check roundrobin failover list empty condition */
12534 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12535 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12536 "2844 No roundrobin failover FCF available\n");
12537 return LPFC_FCOE_FCF_NEXT_NONE;
12538 }
12539
James Smart3804dc82010-07-14 15:31:37 -040012540 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012541 "2845 Get next roundrobin failover FCF (x%x)\n",
12542 next_fcf_index);
12543
James Smart0c9ab6f2010-02-26 14:15:57 -050012544 return next_fcf_index;
12545}
12546
12547/**
12548 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12549 * @phba: pointer to lpfc hba data structure.
12550 *
12551 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012552 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012553 * does not go beyond the range of the driver allocated bmask dimension
12554 * before setting the bit.
12555 *
12556 * Returns 0 if the index bit successfully set, otherwise, it returns
12557 * -EINVAL.
12558 **/
12559int
12560lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12561{
12562 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12563 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012564 "2610 FCF (x%x) reached driver's book "
12565 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012566 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12567 return -EINVAL;
12568 }
12569 /* Set the eligible FCF record index bmask */
12570 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12571
James Smart3804dc82010-07-14 15:31:37 -040012572 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012573 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040012574 "bmask\n", fcf_index);
12575
James Smart0c9ab6f2010-02-26 14:15:57 -050012576 return 0;
12577}
12578
12579/**
James Smart3804dc82010-07-14 15:31:37 -040012580 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050012581 * @phba: pointer to lpfc hba data structure.
12582 *
12583 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012584 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012585 * does not go beyond the range of the driver allocated bmask dimension
12586 * before clearing the bit.
12587 **/
12588void
12589lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12590{
12591 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12592 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012593 "2762 FCF (x%x) reached driver's book "
12594 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012595 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12596 return;
12597 }
12598 /* Clear the eligible FCF record index bmask */
12599 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040012600
12601 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012602 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040012603 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050012604}
12605
12606/**
James Smartecfd03c2010-02-12 14:41:27 -050012607 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12608 * @phba: pointer to lpfc hba data structure.
12609 *
12610 * This routine is the completion routine for the rediscover FCF table mailbox
12611 * command. If the mailbox command returned failure, it will try to stop the
12612 * FCF rediscover wait timer.
12613 **/
12614void
12615lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12616{
12617 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12618 uint32_t shdr_status, shdr_add_status;
12619
12620 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12621
12622 shdr_status = bf_get(lpfc_mbox_hdr_status,
12623 &redisc_fcf->header.cfg_shdr.response);
12624 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12625 &redisc_fcf->header.cfg_shdr.response);
12626 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012627 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012628 "2746 Requesting for FCF rediscovery failed "
12629 "status x%x add_status x%x\n",
12630 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012631 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012632 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012633 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012634 spin_unlock_irq(&phba->hbalock);
12635 /*
12636 * CVL event triggered FCF rediscover request failed,
12637 * last resort to re-try current registered FCF entry.
12638 */
12639 lpfc_retry_pport_discovery(phba);
12640 } else {
12641 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012642 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012643 spin_unlock_irq(&phba->hbalock);
12644 /*
12645 * DEAD FCF event triggered FCF rediscover request
12646 * failed, last resort to fail over as a link down
12647 * to FCF registration.
12648 */
12649 lpfc_sli4_fcf_dead_failthrough(phba);
12650 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012651 } else {
12652 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012653 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050012654 /*
12655 * Start FCF rediscovery wait timer for pending FCF
12656 * before rescan FCF record table.
12657 */
12658 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012659 }
James Smartecfd03c2010-02-12 14:41:27 -050012660
12661 mempool_free(mbox, phba->mbox_mem_pool);
12662}
12663
12664/**
James Smart3804dc82010-07-14 15:31:37 -040012665 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050012666 * @phba: pointer to lpfc hba data structure.
12667 *
12668 * This routine is invoked to request for rediscovery of the entire FCF table
12669 * by the port.
12670 **/
12671int
12672lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12673{
12674 LPFC_MBOXQ_t *mbox;
12675 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12676 int rc, length;
12677
James Smart0c9ab6f2010-02-26 14:15:57 -050012678 /* Cancel retry delay timers to all vports before FCF rediscover */
12679 lpfc_cancel_all_vport_retry_delay_timer(phba);
12680
James Smartecfd03c2010-02-12 14:41:27 -050012681 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12682 if (!mbox) {
12683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12684 "2745 Failed to allocate mbox for "
12685 "requesting FCF rediscover.\n");
12686 return -ENOMEM;
12687 }
12688
12689 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12690 sizeof(struct lpfc_sli4_cfg_mhdr));
12691 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12692 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12693 length, LPFC_SLI4_MBX_EMBED);
12694
12695 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12696 /* Set count to 0 for invalidating the entire FCF database */
12697 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12698
12699 /* Issue the mailbox command asynchronously */
12700 mbox->vport = phba->pport;
12701 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12702 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12703
12704 if (rc == MBX_NOT_FINISHED) {
12705 mempool_free(mbox, phba->mbox_mem_pool);
12706 return -EIO;
12707 }
12708 return 0;
12709}
12710
12711/**
James Smartfc2b9892010-02-26 14:15:29 -050012712 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12713 * @phba: pointer to lpfc hba data structure.
12714 *
12715 * This function is the failover routine as a last resort to the FCF DEAD
12716 * event when driver failed to perform fast FCF failover.
12717 **/
12718void
12719lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12720{
12721 uint32_t link_state;
12722
12723 /*
12724 * Last resort as FCF DEAD event failover will treat this as
12725 * a link down, but save the link state because we don't want
12726 * it to be changed to Link Down unless it is already down.
12727 */
12728 link_state = phba->link_state;
12729 lpfc_linkdown(phba);
12730 phba->link_state = link_state;
12731
12732 /* Unregister FCF if no devices connected to it */
12733 lpfc_unregister_unused_fcf(phba);
12734}
12735
12736/**
James Smarta0c87cb2009-07-19 10:01:10 -040012737 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12738 * @phba: pointer to lpfc hba data structure.
12739 *
12740 * This function read region 23 and parse TLV for port status to
12741 * decide if the user disaled the port. If the TLV indicates the
12742 * port is disabled, the hba_flag is set accordingly.
12743 **/
12744void
12745lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12746{
12747 LPFC_MBOXQ_t *pmb = NULL;
12748 MAILBOX_t *mb;
12749 uint8_t *rgn23_data = NULL;
12750 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12751 int rc;
12752
12753 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12754 if (!pmb) {
12755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12756 "2600 lpfc_sli_read_serdes_param failed to"
12757 " allocate mailbox memory\n");
12758 goto out;
12759 }
12760 mb = &pmb->u.mb;
12761
12762 /* Get adapter Region 23 data */
12763 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12764 if (!rgn23_data)
12765 goto out;
12766
12767 do {
12768 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12769 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12770
12771 if (rc != MBX_SUCCESS) {
12772 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12773 "2601 lpfc_sli_read_link_ste failed to"
12774 " read config region 23 rc 0x%x Status 0x%x\n",
12775 rc, mb->mbxStatus);
12776 mb->un.varDmp.word_cnt = 0;
12777 }
12778 /*
12779 * dump mem may return a zero when finished or we got a
12780 * mailbox error, either way we are done.
12781 */
12782 if (mb->un.varDmp.word_cnt == 0)
12783 break;
12784 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12785 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12786
12787 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12788 rgn23_data + offset,
12789 mb->un.varDmp.word_cnt);
12790 offset += mb->un.varDmp.word_cnt;
12791 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12792
12793 data_size = offset;
12794 offset = 0;
12795
12796 if (!data_size)
12797 goto out;
12798
12799 /* Check the region signature first */
12800 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12802 "2619 Config region 23 has bad signature\n");
12803 goto out;
12804 }
12805 offset += 4;
12806
12807 /* Check the data structure version */
12808 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12810 "2620 Config region 23 has bad version\n");
12811 goto out;
12812 }
12813 offset += 4;
12814
12815 /* Parse TLV entries in the region */
12816 while (offset < data_size) {
12817 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12818 break;
12819 /*
12820 * If the TLV is not driver specific TLV or driver id is
12821 * not linux driver id, skip the record.
12822 */
12823 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12824 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12825 (rgn23_data[offset + 3] != 0)) {
12826 offset += rgn23_data[offset + 1] * 4 + 4;
12827 continue;
12828 }
12829
12830 /* Driver found a driver specific TLV in the config region */
12831 sub_tlv_len = rgn23_data[offset + 1] * 4;
12832 offset += 4;
12833 tlv_offset = 0;
12834
12835 /*
12836 * Search for configured port state sub-TLV.
12837 */
12838 while ((offset < data_size) &&
12839 (tlv_offset < sub_tlv_len)) {
12840 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12841 offset += 4;
12842 tlv_offset += 4;
12843 break;
12844 }
12845 if (rgn23_data[offset] != PORT_STE_TYPE) {
12846 offset += rgn23_data[offset + 1] * 4 + 4;
12847 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12848 continue;
12849 }
12850
12851 /* This HBA contains PORT_STE configured */
12852 if (!rgn23_data[offset + 2])
12853 phba->hba_flag |= LINK_DISABLED;
12854
12855 goto out;
12856 }
12857 }
12858out:
12859 if (pmb)
12860 mempool_free(pmb, phba->mbox_mem_pool);
12861 kfree(rgn23_data);
12862 return;
12863}
James Smart695a8142010-01-26 23:08:03 -050012864
12865/**
12866 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12867 * @vport: pointer to vport data structure.
12868 *
12869 * This function iterate through the mailboxq and clean up all REG_LOGIN
12870 * and REG_VPI mailbox commands associated with the vport. This function
12871 * is called when driver want to restart discovery of the vport due to
12872 * a Clear Virtual Link event.
12873 **/
12874void
12875lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12876{
12877 struct lpfc_hba *phba = vport->phba;
12878 LPFC_MBOXQ_t *mb, *nextmb;
12879 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040012880 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040012881 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040012882 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040012883 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050012884 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050012885
James Smartd439d282010-09-29 11:18:45 -040012886 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050012887 spin_lock_irq(&phba->hbalock);
12888 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12889 if (mb->vport != vport)
12890 continue;
12891
12892 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12893 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12894 continue;
12895
James Smartd439d282010-09-29 11:18:45 -040012896 list_del(&mb->list);
12897 list_add_tail(&mb->list, &mbox_cmd_list);
12898 }
12899 /* Clean up active mailbox command with the vport */
12900 mb = phba->sli.mbox_active;
12901 if (mb && (mb->vport == vport)) {
12902 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12903 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12904 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12905 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12906 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
12907 /* Put reference count for delayed processing */
12908 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
12909 /* Unregister the RPI when mailbox complete */
12910 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12911 }
12912 }
James Smart63e801c2010-11-20 23:14:19 -050012913 /* Cleanup any mailbox completions which are not yet processed */
12914 do {
12915 restart_loop = 0;
12916 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
12917 /*
12918 * If this mailox is already processed or it is
12919 * for another vport ignore it.
12920 */
12921 if ((mb->vport != vport) ||
12922 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
12923 continue;
12924
12925 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12926 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12927 continue;
12928
12929 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12930 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12931 ndlp = (struct lpfc_nodelist *)mb->context2;
12932 /* Unregister the RPI when mailbox complete */
12933 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12934 restart_loop = 1;
12935 spin_unlock_irq(&phba->hbalock);
12936 spin_lock(shost->host_lock);
12937 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12938 spin_unlock(shost->host_lock);
12939 spin_lock_irq(&phba->hbalock);
12940 break;
12941 }
12942 }
12943 } while (restart_loop);
12944
James Smartd439d282010-09-29 11:18:45 -040012945 spin_unlock_irq(&phba->hbalock);
12946
12947 /* Release the cleaned-up mailbox commands */
12948 while (!list_empty(&mbox_cmd_list)) {
12949 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050012950 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smartd7c47992010-06-08 18:31:54 -040012951 if (phba->sli_rev == LPFC_SLI_REV4)
12952 __lpfc_sli4_free_rpi(phba,
12953 mb->u.mb.un.varRegLogin.rpi);
James Smart695a8142010-01-26 23:08:03 -050012954 mp = (struct lpfc_dmabuf *) (mb->context1);
12955 if (mp) {
12956 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12957 kfree(mp);
12958 }
James Smart78730cf2010-04-06 15:06:30 -040012959 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040012960 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040012961 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012962 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040012963 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012964 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040012965 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040012966 }
James Smart695a8142010-01-26 23:08:03 -050012967 }
James Smart695a8142010-01-26 23:08:03 -050012968 mempool_free(mb, phba->mbox_mem_pool);
12969 }
James Smartd439d282010-09-29 11:18:45 -040012970
12971 /* Release the ndlp with the cleaned-up active mailbox command */
12972 if (act_mbx_ndlp) {
12973 spin_lock(shost->host_lock);
12974 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12975 spin_unlock(shost->host_lock);
12976 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050012977 }
James Smart695a8142010-01-26 23:08:03 -050012978}
12979
James Smart2a9bf3d2010-06-07 15:24:45 -040012980/**
12981 * lpfc_drain_txq - Drain the txq
12982 * @phba: Pointer to HBA context object.
12983 *
12984 * This function attempt to submit IOCBs on the txq
12985 * to the adapter. For SLI4 adapters, the txq contains
12986 * ELS IOCBs that have been deferred because the there
12987 * are no SGLs. This congestion can occur with large
12988 * vport counts during node discovery.
12989 **/
12990
12991uint32_t
12992lpfc_drain_txq(struct lpfc_hba *phba)
12993{
12994 LIST_HEAD(completions);
12995 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12996 struct lpfc_iocbq *piocbq = 0;
12997 unsigned long iflags = 0;
12998 char *fail_msg = NULL;
12999 struct lpfc_sglq *sglq;
13000 union lpfc_wqe wqe;
13001
13002 spin_lock_irqsave(&phba->hbalock, iflags);
13003 if (pring->txq_cnt > pring->txq_max)
13004 pring->txq_max = pring->txq_cnt;
13005
13006 spin_unlock_irqrestore(&phba->hbalock, iflags);
13007
13008 while (pring->txq_cnt) {
13009 spin_lock_irqsave(&phba->hbalock, iflags);
13010
13011 sglq = __lpfc_sli_get_sglq(phba);
13012 if (!sglq) {
13013 spin_unlock_irqrestore(&phba->hbalock, iflags);
13014 break;
13015 } else {
13016 piocbq = lpfc_sli_ringtx_get(phba, pring);
13017 if (!piocbq) {
13018 /* The txq_cnt out of sync. This should
13019 * never happen
13020 */
13021 sglq = __lpfc_clear_active_sglq(phba,
13022 sglq->sli4_xritag);
13023 spin_unlock_irqrestore(&phba->hbalock, iflags);
13024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13025 "2823 txq empty and txq_cnt is %d\n ",
13026 pring->txq_cnt);
13027 break;
13028 }
13029 }
13030
13031 /* The xri and iocb resources secured,
13032 * attempt to issue request
13033 */
13034 piocbq->sli4_xritag = sglq->sli4_xritag;
13035 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13036 fail_msg = "to convert bpl to sgl";
13037 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13038 fail_msg = "to convert iocb to wqe";
13039 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13040 fail_msg = " - Wq is full";
13041 else
13042 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13043
13044 if (fail_msg) {
13045 /* Failed means we can't issue and need to cancel */
13046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13047 "2822 IOCB failed %s iotag 0x%x "
13048 "xri 0x%x\n",
13049 fail_msg,
13050 piocbq->iotag, piocbq->sli4_xritag);
13051 list_add_tail(&piocbq->list, &completions);
13052 }
13053 spin_unlock_irqrestore(&phba->hbalock, iflags);
13054 }
13055
James Smart2a9bf3d2010-06-07 15:24:45 -040013056 /* Cancel all the IOCBs that cannot be issued */
13057 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13058 IOERR_SLI_ABORTED);
13059
13060 return pring->txq_cnt;
13061}