blob: 752601509549974843125e1fb9f061a6172ea2e6 [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/**
James Smart19ca7602010-11-20 23:11:55 -0500516 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
517 * @phba: Pointer to HBA context object.
518 * @ndlp: nodelist pointer for this target.
519 * @xritag: xri used in this exchange.
520 * @rxid: Remote Exchange ID.
521 * @send_rrq: Flag used to determine if we should send rrq els cmd.
522 *
523 * This function is called with hbalock held.
524 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
525 * rrq struct and adds it to the active_rrq_list.
526 *
527 * returns 0 for rrq slot for this xri
528 * < 0 Were not able to get rrq mem or invalid parameter.
529 **/
530static int
531__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
532 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
533{
534 uint16_t adj_xri;
535 struct lpfc_node_rrq *rrq;
536 int empty;
537
538 /*
539 * set the active bit even if there is no mem available.
540 */
541 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
542 if (!ndlp)
543 return -EINVAL;
544 if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
545 return -EINVAL;
546 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
547 if (rrq) {
548 rrq->send_rrq = send_rrq;
549 rrq->xritag = xritag;
550 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
551 rrq->ndlp = ndlp;
552 rrq->nlp_DID = ndlp->nlp_DID;
553 rrq->vport = ndlp->vport;
554 rrq->rxid = rxid;
555 empty = list_empty(&phba->active_rrq_list);
556 if (phba->cfg_enable_rrq && send_rrq)
557 /*
558 * We need the xri before we can add this to the
559 * phba active rrq list.
560 */
561 rrq->send_rrq = send_rrq;
562 else
563 rrq->send_rrq = 0;
564 list_add_tail(&rrq->list, &phba->active_rrq_list);
565 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
566 phba->hba_flag |= HBA_RRQ_ACTIVE;
567 if (empty)
568 lpfc_worker_wake_up(phba);
569 }
570 return 0;
571 }
572 return -ENOMEM;
573}
574
575/**
576 * __lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
577 * @phba: Pointer to HBA context object.
578 * @xritag: xri used in this exchange.
579 * @rrq: The RRQ to be cleared.
580 *
581 * This function is called with hbalock held. This function
582 **/
583static void
584__lpfc_clr_rrq_active(struct lpfc_hba *phba,
585 uint16_t xritag,
586 struct lpfc_node_rrq *rrq)
587{
588 uint16_t adj_xri;
589 struct lpfc_nodelist *ndlp;
590
591 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
592
593 /* The target DID could have been swapped (cable swap)
594 * we should use the ndlp from the findnode if it is
595 * available.
596 */
597 if (!ndlp)
598 ndlp = rrq->ndlp;
599
600 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
601 if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
602 rrq->send_rrq = 0;
603 rrq->xritag = 0;
604 rrq->rrq_stop_time = 0;
605 }
606 mempool_free(rrq, phba->rrq_pool);
607}
608
609/**
610 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
611 * @phba: Pointer to HBA context object.
612 *
613 * This function is called with hbalock held. This function
614 * Checks if stop_time (ratov from setting rrq active) has
615 * been reached, if it has and the send_rrq flag is set then
616 * it will call lpfc_send_rrq. If the send_rrq flag is not set
617 * then it will just call the routine to clear the rrq and
618 * free the rrq resource.
619 * The timer is set to the next rrq that is going to expire before
620 * leaving the routine.
621 *
622 **/
623void
624lpfc_handle_rrq_active(struct lpfc_hba *phba)
625{
626 struct lpfc_node_rrq *rrq;
627 struct lpfc_node_rrq *nextrrq;
628 unsigned long next_time;
629 unsigned long iflags;
630
631 spin_lock_irqsave(&phba->hbalock, iflags);
632 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
633 next_time = jiffies + HZ * (phba->fc_ratov + 1);
634 list_for_each_entry_safe(rrq, nextrrq,
635 &phba->active_rrq_list, list) {
636 if (time_after(jiffies, rrq->rrq_stop_time)) {
637 list_del(&rrq->list);
638 if (!rrq->send_rrq)
639 /* this call will free the rrq */
640 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
641 else {
642 /* if we send the rrq then the completion handler
643 * will clear the bit in the xribitmap.
644 */
645 spin_unlock_irqrestore(&phba->hbalock, iflags);
646 if (lpfc_send_rrq(phba, rrq)) {
647 lpfc_clr_rrq_active(phba, rrq->xritag,
648 rrq);
649 }
650 spin_lock_irqsave(&phba->hbalock, iflags);
651 }
652 } else if (time_before(rrq->rrq_stop_time, next_time))
653 next_time = rrq->rrq_stop_time;
654 }
655 spin_unlock_irqrestore(&phba->hbalock, iflags);
656 if (!list_empty(&phba->active_rrq_list))
657 mod_timer(&phba->rrq_tmr, next_time);
658}
659
660/**
661 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
662 * @vport: Pointer to vport context object.
663 * @xri: The xri used in the exchange.
664 * @did: The targets DID for this exchange.
665 *
666 * returns NULL = rrq not found in the phba->active_rrq_list.
667 * rrq = rrq for this xri and target.
668 **/
669struct lpfc_node_rrq *
670lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
671{
672 struct lpfc_hba *phba = vport->phba;
673 struct lpfc_node_rrq *rrq;
674 struct lpfc_node_rrq *nextrrq;
675 unsigned long iflags;
676
677 if (phba->sli_rev != LPFC_SLI_REV4)
678 return NULL;
679 spin_lock_irqsave(&phba->hbalock, iflags);
680 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
681 if (rrq->vport == vport && rrq->xritag == xri &&
682 rrq->nlp_DID == did){
683 list_del(&rrq->list);
684 spin_unlock_irqrestore(&phba->hbalock, iflags);
685 return rrq;
686 }
687 }
688 spin_unlock_irqrestore(&phba->hbalock, iflags);
689 return NULL;
690}
691
692/**
693 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
694 * @vport: Pointer to vport context object.
695 *
696 * Remove all active RRQs for this vport from the phba->active_rrq_list and
697 * clear the rrq.
698 **/
699void
700lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport)
701
702{
703 struct lpfc_hba *phba = vport->phba;
704 struct lpfc_node_rrq *rrq;
705 struct lpfc_node_rrq *nextrrq;
706 unsigned long iflags;
707
708 if (phba->sli_rev != LPFC_SLI_REV4)
709 return;
710 spin_lock_irqsave(&phba->hbalock, iflags);
711 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
712 if (rrq->vport == vport) {
713 list_del(&rrq->list);
714 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
715 }
716 }
717 spin_unlock_irqrestore(&phba->hbalock, iflags);
718}
719
720/**
721 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
722 * @phba: Pointer to HBA context object.
723 *
724 * Remove all rrqs from the phba->active_rrq_list and free them by
725 * calling __lpfc_clr_active_rrq
726 *
727 **/
728void
729lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
730{
731 struct lpfc_node_rrq *rrq;
732 struct lpfc_node_rrq *nextrrq;
733 unsigned long next_time;
734 unsigned long iflags;
735
736 if (phba->sli_rev != LPFC_SLI_REV4)
737 return;
738 spin_lock_irqsave(&phba->hbalock, iflags);
739 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
740 next_time = jiffies + HZ * (phba->fc_ratov * 2);
741 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
742 list_del(&rrq->list);
743 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
744 }
745 spin_unlock_irqrestore(&phba->hbalock, iflags);
746 if (!list_empty(&phba->active_rrq_list))
747 mod_timer(&phba->rrq_tmr, next_time);
748}
749
750
751/**
752 * __lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
753 * @phba: Pointer to HBA context object.
754 * @ndlp: Targets nodelist pointer for this exchange.
755 * @xritag the xri in the bitmap to test.
756 *
757 * This function is called with hbalock held. This function
758 * returns 0 = rrq not active for this xri
759 * 1 = rrq is valid for this xri.
760 **/
761static int
762__lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
763 uint16_t xritag)
764{
765 uint16_t adj_xri;
766
767 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
768 if (!ndlp)
769 return 0;
770 if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
771 return 1;
772 else
773 return 0;
774}
775
776/**
777 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
778 * @phba: Pointer to HBA context object.
779 * @ndlp: nodelist pointer for this target.
780 * @xritag: xri used in this exchange.
781 * @rxid: Remote Exchange ID.
782 * @send_rrq: Flag used to determine if we should send rrq els cmd.
783 *
784 * This function takes the hbalock.
785 * The active bit is always set in the active rrq xri_bitmap even
786 * if there is no slot avaiable for the other rrq information.
787 *
788 * returns 0 rrq actived for this xri
789 * < 0 No memory or invalid ndlp.
790 **/
791int
792lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
793 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
794{
795 int ret;
796 unsigned long iflags;
797
798 spin_lock_irqsave(&phba->hbalock, iflags);
799 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
800 spin_unlock_irqrestore(&phba->hbalock, iflags);
801 return ret;
802}
803
804/**
805 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
806 * @phba: Pointer to HBA context object.
807 * @xritag: xri used in this exchange.
808 * @rrq: The RRQ to be cleared.
809 *
810 * This function is takes the hbalock.
811 **/
812void
813lpfc_clr_rrq_active(struct lpfc_hba *phba,
814 uint16_t xritag,
815 struct lpfc_node_rrq *rrq)
816{
817 unsigned long iflags;
818
819 spin_lock_irqsave(&phba->hbalock, iflags);
820 __lpfc_clr_rrq_active(phba, xritag, rrq);
821 spin_unlock_irqrestore(&phba->hbalock, iflags);
822 return;
823}
824
825
826
827/**
828 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
829 * @phba: Pointer to HBA context object.
830 * @ndlp: Targets nodelist pointer for this exchange.
831 * @xritag the xri in the bitmap to test.
832 *
833 * This function takes the hbalock.
834 * returns 0 = rrq not active for this xri
835 * 1 = rrq is valid for this xri.
836 **/
837int
838lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
839 uint16_t xritag)
840{
841 int ret;
842 unsigned long iflags;
843
844 spin_lock_irqsave(&phba->hbalock, iflags);
845 ret = __lpfc_test_rrq_active(phba, ndlp, xritag);
846 spin_unlock_irqrestore(&phba->hbalock, iflags);
847 return ret;
848}
849
850/**
James Smartda0436e2009-05-22 14:51:39 -0400851 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
852 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500853 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400854 *
855 * This function is called with hbalock held. This function
856 * Gets a new driver sglq object from the sglq list. If the
857 * list is not empty then it is successful, it returns pointer to the newly
858 * allocated sglq object else it returns NULL.
859 **/
860static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500861__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400862{
863 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
864 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500865 struct lpfc_sglq *start_sglq = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400866 uint16_t adj_xri;
James Smart19ca7602010-11-20 23:11:55 -0500867 struct lpfc_scsi_buf *lpfc_cmd;
868 struct lpfc_nodelist *ndlp;
869 int found = 0;
870
871 if (piocbq->iocb_flag & LPFC_IO_FCP) {
872 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
873 ndlp = lpfc_cmd->rdata->pnode;
874 } else if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
875 ndlp = piocbq->context_un.ndlp;
876 else if (piocbq->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
877 ndlp = lpfc_findnode_did(piocbq->vport,
878 piocbq->iocb.ulpContext);
879 else
880 ndlp = piocbq->context1;
881
James Smartda0436e2009-05-22 14:51:39 -0400882 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500883 start_sglq = sglq;
884 while (!found) {
885 if (!sglq)
886 return NULL;
887 adj_xri = sglq->sli4_xritag -
888 phba->sli4_hba.max_cfg_param.xri_base;
889 if (__lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
890 /* This xri has an rrq outstanding for this DID.
891 * put it back in the list and get another xri.
892 */
893 list_add_tail(&sglq->list, lpfc_sgl_list);
894 sglq = NULL;
895 list_remove_head(lpfc_sgl_list, sglq,
896 struct lpfc_sglq, list);
897 if (sglq == start_sglq) {
898 sglq = NULL;
899 break;
900 } else
901 continue;
902 }
903 sglq->ndlp = ndlp;
904 found = 1;
905 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
906 sglq->state = SGL_ALLOCATED;
907 }
James Smartda0436e2009-05-22 14:51:39 -0400908 return sglq;
909}
910
911/**
James Smart3621a712009-04-06 18:47:14 -0400912 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400913 * @phba: Pointer to HBA context object.
914 *
915 * This function is called with no lock held. This function
916 * allocates a new driver iocb object from the iocb pool. If the
917 * allocation is successful, it returns pointer to the newly
918 * allocated iocb object else it returns NULL.
919 **/
James Smart2e0fef82007-06-17 19:56:36 -0500920struct lpfc_iocbq *
921lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500922{
James Smart2e0fef82007-06-17 19:56:36 -0500923 struct lpfc_iocbq * iocbq = NULL;
924 unsigned long iflags;
925
926 spin_lock_irqsave(&phba->hbalock, iflags);
927 iocbq = __lpfc_sli_get_iocbq(phba);
928 spin_unlock_irqrestore(&phba->hbalock, iflags);
929 return iocbq;
930}
931
James Smarte59058c2008-08-24 21:49:00 -0400932/**
James Smart4f774512009-05-22 14:52:35 -0400933 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
934 * @phba: Pointer to HBA context object.
935 * @iocbq: Pointer to driver iocb object.
936 *
937 * This function is called with hbalock held to release driver
938 * iocb object to the iocb pool. The iotag in the iocb object
939 * does not change for each use of the iocb object. This function
940 * clears all other fields of the iocb object when it is freed.
941 * The sqlq structure that holds the xritag and phys and virtual
942 * mappings for the scatter gather list is retrieved from the
943 * active array of sglq. The get of the sglq pointer also clears
944 * the entry in the array. If the status of the IO indiactes that
945 * this IO was aborted then the sglq entry it put on the
946 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
947 * IO has good status or fails for any other reason then the sglq
948 * entry is added to the free list (lpfc_sgl_list).
949 **/
950static void
951__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
952{
953 struct lpfc_sglq *sglq;
954 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400955 unsigned long iflag = 0;
956 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400957
958 if (iocbq->sli4_xritag == NO_XRI)
959 sglq = NULL;
960 else
961 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
962 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500963 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
964 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400965 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
966 iflag);
967 list_add(&sglq->list,
968 &phba->sli4_hba.lpfc_abts_els_sgl_list);
969 spin_unlock_irqrestore(
970 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500971 } else {
972 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500973 sglq->ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -0400974 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400975
976 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400977 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400978 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500979 }
James Smart4f774512009-05-22 14:52:35 -0400980 }
981
982
983 /*
984 * Clean all volatile data fields, preserve iotag and node struct.
985 */
986 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
987 iocbq->sli4_xritag = NO_XRI;
988 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
989}
990
James Smart2a9bf3d2010-06-07 15:24:45 -0400991
James Smart4f774512009-05-22 14:52:35 -0400992/**
James Smart3772a992009-05-22 14:50:54 -0400993 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
994 * @phba: Pointer to HBA context object.
995 * @iocbq: Pointer to driver iocb object.
996 *
997 * This function is called with hbalock held to release driver
998 * iocb object to the iocb pool. The iotag in the iocb object
999 * does not change for each use of the iocb object. This function
1000 * clears all other fields of the iocb object when it is freed.
1001 **/
1002static void
1003__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1004{
1005 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1006
1007 /*
1008 * Clean all volatile data fields, preserve iotag and node struct.
1009 */
1010 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1011 iocbq->sli4_xritag = NO_XRI;
1012 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1013}
1014
1015/**
James Smart3621a712009-04-06 18:47:14 -04001016 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001017 * @phba: Pointer to HBA context object.
1018 * @iocbq: Pointer to driver iocb object.
1019 *
1020 * This function is called with hbalock held to release driver
1021 * iocb object to the iocb pool. The iotag in the iocb object
1022 * does not change for each use of the iocb object. This function
1023 * clears all other fields of the iocb object when it is freed.
1024 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001025static void
James Smart2e0fef82007-06-17 19:56:36 -05001026__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1027{
James Smart3772a992009-05-22 14:50:54 -04001028 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001029 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001030}
1031
James Smarte59058c2008-08-24 21:49:00 -04001032/**
James Smart3621a712009-04-06 18:47:14 -04001033 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001034 * @phba: Pointer to HBA context object.
1035 * @iocbq: Pointer to driver iocb object.
1036 *
1037 * This function is called with no lock held to release the iocb to
1038 * iocb pool.
1039 **/
James Smart2e0fef82007-06-17 19:56:36 -05001040void
1041lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1042{
1043 unsigned long iflags;
1044
1045 /*
1046 * Clean all volatile data fields, preserve iotag and node struct.
1047 */
1048 spin_lock_irqsave(&phba->hbalock, iflags);
1049 __lpfc_sli_release_iocbq(phba, iocbq);
1050 spin_unlock_irqrestore(&phba->hbalock, iflags);
1051}
1052
James Smarte59058c2008-08-24 21:49:00 -04001053/**
James Smarta257bf92009-04-06 18:48:10 -04001054 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1055 * @phba: Pointer to HBA context object.
1056 * @iocblist: List of IOCBs.
1057 * @ulpstatus: ULP status in IOCB command field.
1058 * @ulpWord4: ULP word-4 in IOCB command field.
1059 *
1060 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1061 * on the list by invoking the complete callback function associated with the
1062 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1063 * fields.
1064 **/
1065void
1066lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1067 uint32_t ulpstatus, uint32_t ulpWord4)
1068{
1069 struct lpfc_iocbq *piocb;
1070
1071 while (!list_empty(iocblist)) {
1072 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1073
1074 if (!piocb->iocb_cmpl)
1075 lpfc_sli_release_iocbq(phba, piocb);
1076 else {
1077 piocb->iocb.ulpStatus = ulpstatus;
1078 piocb->iocb.un.ulpWord[4] = ulpWord4;
1079 (piocb->iocb_cmpl) (phba, piocb, piocb);
1080 }
1081 }
1082 return;
1083}
1084
1085/**
James Smart3621a712009-04-06 18:47:14 -04001086 * lpfc_sli_iocb_cmd_type - Get the iocb type
1087 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001088 *
1089 * This function is called by ring event handler function to get the iocb type.
1090 * This function translates the iocb command to an iocb command type used to
1091 * decide the final disposition of each completed IOCB.
1092 * The function returns
1093 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1094 * LPFC_SOL_IOCB if it is a solicited iocb completion
1095 * LPFC_ABORT_IOCB if it is an abort iocb
1096 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1097 *
1098 * The caller is not required to hold any lock.
1099 **/
dea31012005-04-17 16:05:31 -05001100static lpfc_iocb_type
1101lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1102{
1103 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1104
1105 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1106 return 0;
1107
1108 switch (iocb_cmnd) {
1109 case CMD_XMIT_SEQUENCE_CR:
1110 case CMD_XMIT_SEQUENCE_CX:
1111 case CMD_XMIT_BCAST_CN:
1112 case CMD_XMIT_BCAST_CX:
1113 case CMD_ELS_REQUEST_CR:
1114 case CMD_ELS_REQUEST_CX:
1115 case CMD_CREATE_XRI_CR:
1116 case CMD_CREATE_XRI_CX:
1117 case CMD_GET_RPI_CN:
1118 case CMD_XMIT_ELS_RSP_CX:
1119 case CMD_GET_RPI_CR:
1120 case CMD_FCP_IWRITE_CR:
1121 case CMD_FCP_IWRITE_CX:
1122 case CMD_FCP_IREAD_CR:
1123 case CMD_FCP_IREAD_CX:
1124 case CMD_FCP_ICMND_CR:
1125 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001126 case CMD_FCP_TSEND_CX:
1127 case CMD_FCP_TRSP_CX:
1128 case CMD_FCP_TRECEIVE_CX:
1129 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001130 case CMD_ADAPTER_MSG:
1131 case CMD_ADAPTER_DUMP:
1132 case CMD_XMIT_SEQUENCE64_CR:
1133 case CMD_XMIT_SEQUENCE64_CX:
1134 case CMD_XMIT_BCAST64_CN:
1135 case CMD_XMIT_BCAST64_CX:
1136 case CMD_ELS_REQUEST64_CR:
1137 case CMD_ELS_REQUEST64_CX:
1138 case CMD_FCP_IWRITE64_CR:
1139 case CMD_FCP_IWRITE64_CX:
1140 case CMD_FCP_IREAD64_CR:
1141 case CMD_FCP_IREAD64_CX:
1142 case CMD_FCP_ICMND64_CR:
1143 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001144 case CMD_FCP_TSEND64_CX:
1145 case CMD_FCP_TRSP64_CX:
1146 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001147 case CMD_GEN_REQUEST64_CR:
1148 case CMD_GEN_REQUEST64_CX:
1149 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001150 case DSSCMD_IWRITE64_CR:
1151 case DSSCMD_IWRITE64_CX:
1152 case DSSCMD_IREAD64_CR:
1153 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001154 type = LPFC_SOL_IOCB;
1155 break;
1156 case CMD_ABORT_XRI_CN:
1157 case CMD_ABORT_XRI_CX:
1158 case CMD_CLOSE_XRI_CN:
1159 case CMD_CLOSE_XRI_CX:
1160 case CMD_XRI_ABORTED_CX:
1161 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001162 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001163 type = LPFC_ABORT_IOCB;
1164 break;
1165 case CMD_RCV_SEQUENCE_CX:
1166 case CMD_RCV_ELS_REQ_CX:
1167 case CMD_RCV_SEQUENCE64_CX:
1168 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001169 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001170 case CMD_IOCB_RCV_SEQ64_CX:
1171 case CMD_IOCB_RCV_ELS64_CX:
1172 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001173 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001174 type = LPFC_UNSOL_IOCB;
1175 break;
James Smart3163f722008-02-08 18:50:25 -05001176 case CMD_IOCB_XMIT_MSEQ64_CR:
1177 case CMD_IOCB_XMIT_MSEQ64_CX:
1178 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1179 case CMD_IOCB_RCV_ELS_LIST64_CX:
1180 case CMD_IOCB_CLOSE_EXTENDED_CN:
1181 case CMD_IOCB_ABORT_EXTENDED_CN:
1182 case CMD_IOCB_RET_HBQE64_CN:
1183 case CMD_IOCB_FCP_IBIDIR64_CR:
1184 case CMD_IOCB_FCP_IBIDIR64_CX:
1185 case CMD_IOCB_FCP_ITASKMGT64_CX:
1186 case CMD_IOCB_LOGENTRY_CN:
1187 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1188 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001189 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001190 type = LPFC_UNKNOWN_IOCB;
1191 break;
dea31012005-04-17 16:05:31 -05001192 default:
1193 type = LPFC_UNKNOWN_IOCB;
1194 break;
1195 }
1196
1197 return type;
1198}
1199
James Smarte59058c2008-08-24 21:49:00 -04001200/**
James Smart3621a712009-04-06 18:47:14 -04001201 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001202 * @phba: Pointer to HBA context object.
1203 *
1204 * This function is called from SLI initialization code
1205 * to configure every ring of the HBA's SLI interface. The
1206 * caller is not required to hold any lock. This function issues
1207 * a config_ring mailbox command for each ring.
1208 * This function returns zero if successful else returns a negative
1209 * error code.
1210 **/
dea31012005-04-17 16:05:31 -05001211static int
James Smarted957682007-06-17 19:56:37 -05001212lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001213{
1214 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001215 LPFC_MBOXQ_t *pmb;
1216 MAILBOX_t *pmbox;
1217 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001218
James Smarted957682007-06-17 19:56:37 -05001219 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1220 if (!pmb)
1221 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001222 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001223 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001224 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001225 lpfc_config_ring(phba, i, pmb);
1226 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1227 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001229 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001230 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1231 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001232 rc, pmbox->mbxCommand,
1233 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001234 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001235 ret = -ENXIO;
1236 break;
dea31012005-04-17 16:05:31 -05001237 }
1238 }
James Smarted957682007-06-17 19:56:37 -05001239 mempool_free(pmb, phba->mbox_mem_pool);
1240 return ret;
dea31012005-04-17 16:05:31 -05001241}
1242
James Smarte59058c2008-08-24 21:49:00 -04001243/**
James Smart3621a712009-04-06 18:47:14 -04001244 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001245 * @phba: Pointer to HBA context object.
1246 * @pring: Pointer to driver SLI ring object.
1247 * @piocb: Pointer to the driver iocb object.
1248 *
1249 * This function is called with hbalock held. The function adds the
1250 * new iocb to txcmplq of the given ring. This function always returns
1251 * 0. If this function is called for ELS ring, this function checks if
1252 * there is a vport associated with the ELS command. This function also
1253 * starts els_tmofunc timer if this is an ELS command.
1254 **/
dea31012005-04-17 16:05:31 -05001255static int
James Smart2e0fef82007-06-17 19:56:36 -05001256lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1257 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001258{
dea31012005-04-17 16:05:31 -05001259 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001260 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001261 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001262 if (pring->txcmplq_cnt > pring->txcmplq_max)
1263 pring->txcmplq_max = pring->txcmplq_cnt;
1264
James Smart92d7f7b2007-06-17 19:56:38 -05001265 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1266 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1267 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1268 if (!piocb->vport)
1269 BUG();
1270 else
1271 mod_timer(&piocb->vport->els_tmofunc,
1272 jiffies + HZ * (phba->fc_ratov << 1));
1273 }
1274
dea31012005-04-17 16:05:31 -05001275
James Smart2e0fef82007-06-17 19:56:36 -05001276 return 0;
dea31012005-04-17 16:05:31 -05001277}
1278
James Smarte59058c2008-08-24 21:49:00 -04001279/**
James Smart3621a712009-04-06 18:47:14 -04001280 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001281 * @phba: Pointer to HBA context object.
1282 * @pring: Pointer to driver SLI ring object.
1283 *
1284 * This function is called with hbalock held to get next
1285 * iocb in txq of the given ring. If there is any iocb in
1286 * the txq, the function returns first iocb in the list after
1287 * removing the iocb from the list, else it returns NULL.
1288 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001289struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001290lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001291{
dea31012005-04-17 16:05:31 -05001292 struct lpfc_iocbq *cmd_iocb;
1293
James Smart858c9f62007-06-17 19:56:39 -05001294 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1295 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001296 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001297 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001298}
1299
James Smarte59058c2008-08-24 21:49:00 -04001300/**
James Smart3621a712009-04-06 18:47:14 -04001301 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001302 * @phba: Pointer to HBA context object.
1303 * @pring: Pointer to driver SLI ring object.
1304 *
1305 * This function is called with hbalock held and the caller must post the
1306 * iocb without releasing the lock. If the caller releases the lock,
1307 * iocb slot returned by the function is not guaranteed to be available.
1308 * The function returns pointer to the next available iocb slot if there
1309 * is available slot in the ring, else it returns NULL.
1310 * If the get index of the ring is ahead of the put index, the function
1311 * will post an error attention event to the worker thread to take the
1312 * HBA to offline state.
1313 **/
dea31012005-04-17 16:05:31 -05001314static IOCB_t *
1315lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1316{
James Smart34b02dc2008-08-24 21:49:55 -04001317 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001318 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001319 if ((pring->next_cmdidx == pring->cmdidx) &&
1320 (++pring->next_cmdidx >= max_cmd_idx))
1321 pring->next_cmdidx = 0;
1322
1323 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1324
1325 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1326
1327 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001329 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001330 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001331 pring->ringno,
dea31012005-04-17 16:05:31 -05001332 pring->local_getidx, max_cmd_idx);
1333
James Smart2e0fef82007-06-17 19:56:36 -05001334 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001335 /*
1336 * All error attention handlers are posted to
1337 * worker thread
1338 */
1339 phba->work_ha |= HA_ERATT;
1340 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001341
James Smart5e9d9b82008-06-14 22:52:53 -04001342 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001343
1344 return NULL;
1345 }
1346
1347 if (pring->local_getidx == pring->next_cmdidx)
1348 return NULL;
1349 }
1350
James Smarted957682007-06-17 19:56:37 -05001351 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001352}
1353
James Smarte59058c2008-08-24 21:49:00 -04001354/**
James Smart3621a712009-04-06 18:47:14 -04001355 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001356 * @phba: Pointer to HBA context object.
1357 * @iocbq: Pointer to driver iocb object.
1358 *
1359 * This function gets an iotag for the iocb. If there is no unused iotag and
1360 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1361 * array and assigns a new iotag.
1362 * The function returns the allocated iotag if successful, else returns zero.
1363 * Zero is not a valid iotag.
1364 * The caller is not required to hold any lock.
1365 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001366uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001367lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001368{
James Smart2e0fef82007-06-17 19:56:36 -05001369 struct lpfc_iocbq **new_arr;
1370 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001371 size_t new_len;
1372 struct lpfc_sli *psli = &phba->sli;
1373 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001374
James Smart2e0fef82007-06-17 19:56:36 -05001375 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001376 iotag = psli->last_iotag;
1377 if(++iotag < psli->iocbq_lookup_len) {
1378 psli->last_iotag = iotag;
1379 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001380 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001381 iocbq->iotag = iotag;
1382 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001383 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001384 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1385 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001386 spin_unlock_irq(&phba->hbalock);
1387 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001388 GFP_KERNEL);
1389 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001390 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001391 old_arr = psli->iocbq_lookup;
1392 if (new_len <= psli->iocbq_lookup_len) {
1393 /* highly unprobable case */
1394 kfree(new_arr);
1395 iotag = psli->last_iotag;
1396 if(++iotag < psli->iocbq_lookup_len) {
1397 psli->last_iotag = iotag;
1398 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001399 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001400 iocbq->iotag = iotag;
1401 return iotag;
1402 }
James Smart2e0fef82007-06-17 19:56:36 -05001403 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001404 return 0;
1405 }
1406 if (psli->iocbq_lookup)
1407 memcpy(new_arr, old_arr,
1408 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001409 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001410 psli->iocbq_lookup = new_arr;
1411 psli->iocbq_lookup_len = new_len;
1412 psli->last_iotag = iotag;
1413 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001414 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001415 iocbq->iotag = iotag;
1416 kfree(old_arr);
1417 return iotag;
1418 }
James Smart8f6d98d2006-08-01 07:34:00 -04001419 } else
James Smart2e0fef82007-06-17 19:56:36 -05001420 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001421
James Smartbc739052010-08-04 16:11:18 -04001422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001423 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1424 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001425
James Bottomley604a3e32005-10-29 10:28:33 -05001426 return 0;
dea31012005-04-17 16:05:31 -05001427}
1428
James Smarte59058c2008-08-24 21:49:00 -04001429/**
James Smart3621a712009-04-06 18:47:14 -04001430 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001431 * @phba: Pointer to HBA context object.
1432 * @pring: Pointer to driver SLI ring object.
1433 * @iocb: Pointer to iocb slot in the ring.
1434 * @nextiocb: Pointer to driver iocb object which need to be
1435 * posted to firmware.
1436 *
1437 * This function is called with hbalock held to post a new iocb to
1438 * the firmware. This function copies the new iocb to ring iocb slot and
1439 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1440 * a completion call back for this iocb else the function will free the
1441 * iocb object.
1442 **/
dea31012005-04-17 16:05:31 -05001443static void
1444lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1445 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1446{
1447 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001448 * Set up an iotag
dea31012005-04-17 16:05:31 -05001449 */
James Bottomley604a3e32005-10-29 10:28:33 -05001450 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001451
James Smarte2a0a9d2008-12-04 22:40:02 -05001452
James Smarta58cbd52007-08-02 11:09:43 -04001453 if (pring->ringno == LPFC_ELS_RING) {
1454 lpfc_debugfs_slow_ring_trc(phba,
1455 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1456 *(((uint32_t *) &nextiocb->iocb) + 4),
1457 *(((uint32_t *) &nextiocb->iocb) + 6),
1458 *(((uint32_t *) &nextiocb->iocb) + 7));
1459 }
1460
dea31012005-04-17 16:05:31 -05001461 /*
1462 * Issue iocb command to adapter
1463 */
James Smart92d7f7b2007-06-17 19:56:38 -05001464 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001465 wmb();
1466 pring->stats.iocb_cmd++;
1467
1468 /*
1469 * If there is no completion routine to call, we can release the
1470 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1471 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1472 */
1473 if (nextiocb->iocb_cmpl)
1474 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001475 else
James Smart2e0fef82007-06-17 19:56:36 -05001476 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001477
1478 /*
1479 * Let the HBA know what IOCB slot will be the next one the
1480 * driver will put a command into.
1481 */
1482 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001483 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001484}
1485
James Smarte59058c2008-08-24 21:49:00 -04001486/**
James Smart3621a712009-04-06 18:47:14 -04001487 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001488 * @phba: Pointer to HBA context object.
1489 * @pring: Pointer to driver SLI ring object.
1490 *
1491 * The caller is not required to hold any lock for calling this function.
1492 * This function updates the chip attention bits for the ring to inform firmware
1493 * that there are pending work to be done for this ring and requests an
1494 * interrupt when there is space available in the ring. This function is
1495 * called when the driver is unable to post more iocbs to the ring due
1496 * to unavailability of space in the ring.
1497 **/
dea31012005-04-17 16:05:31 -05001498static void
James Smart2e0fef82007-06-17 19:56:36 -05001499lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001500{
1501 int ringno = pring->ringno;
1502
1503 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1504
1505 wmb();
1506
1507 /*
1508 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1509 * The HBA will tell us when an IOCB entry is available.
1510 */
1511 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1512 readl(phba->CAregaddr); /* flush */
1513
1514 pring->stats.iocb_cmd_full++;
1515}
1516
James Smarte59058c2008-08-24 21:49:00 -04001517/**
James Smart3621a712009-04-06 18:47:14 -04001518 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001519 * @phba: Pointer to HBA context object.
1520 * @pring: Pointer to driver SLI ring object.
1521 *
1522 * This function updates the chip attention register bit for the
1523 * given ring to inform HBA that there is more work to be done
1524 * in this ring. The caller is not required to hold any lock.
1525 **/
dea31012005-04-17 16:05:31 -05001526static void
James Smart2e0fef82007-06-17 19:56:36 -05001527lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001528{
1529 int ringno = pring->ringno;
1530
1531 /*
1532 * Tell the HBA that there is work to do in this ring.
1533 */
James Smart34b02dc2008-08-24 21:49:55 -04001534 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1535 wmb();
1536 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1537 readl(phba->CAregaddr); /* flush */
1538 }
dea31012005-04-17 16:05:31 -05001539}
1540
James Smarte59058c2008-08-24 21:49:00 -04001541/**
James Smart3621a712009-04-06 18:47:14 -04001542 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001543 * @phba: Pointer to HBA context object.
1544 * @pring: Pointer to driver SLI ring object.
1545 *
1546 * This function is called with hbalock held to post pending iocbs
1547 * in the txq to the firmware. This function is called when driver
1548 * detects space available in the ring.
1549 **/
dea31012005-04-17 16:05:31 -05001550static void
James Smart2e0fef82007-06-17 19:56:36 -05001551lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001552{
1553 IOCB_t *iocb;
1554 struct lpfc_iocbq *nextiocb;
1555
1556 /*
1557 * Check to see if:
1558 * (a) there is anything on the txq to send
1559 * (b) link is up
1560 * (c) link attention events can be processed (fcp ring only)
1561 * (d) IOCB processing is not blocked by the outstanding mbox command.
1562 */
1563 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001564 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001565 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001566 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001567
1568 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1569 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1570 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1571
1572 if (iocb)
1573 lpfc_sli_update_ring(phba, pring);
1574 else
1575 lpfc_sli_update_full_ring(phba, pring);
1576 }
1577
1578 return;
1579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001583 * @phba: Pointer to HBA context object.
1584 * @hbqno: HBQ number.
1585 *
1586 * This function is called with hbalock held to get the next
1587 * available slot for the given HBQ. If there is free slot
1588 * available for the HBQ it will return pointer to the next available
1589 * HBQ entry else it will return NULL.
1590 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001591static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001592lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1593{
1594 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1595
1596 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1597 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1598 hbqp->next_hbqPutIdx = 0;
1599
1600 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001601 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001602 uint32_t getidx = le32_to_cpu(raw_index);
1603
1604 hbqp->local_hbqGetIdx = getidx;
1605
1606 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1607 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001608 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001609 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001610 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001611 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001612 hbqp->entry_count);
1613
1614 phba->link_state = LPFC_HBA_ERROR;
1615 return NULL;
1616 }
1617
1618 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1619 return NULL;
1620 }
1621
James Smart51ef4c22007-08-02 11:10:31 -04001622 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1623 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001624}
1625
James Smarte59058c2008-08-24 21:49:00 -04001626/**
James Smart3621a712009-04-06 18:47:14 -04001627 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001628 * @phba: Pointer to HBA context object.
1629 *
1630 * This function is called with no lock held to free all the
1631 * hbq buffers while uninitializing the SLI interface. It also
1632 * frees the HBQ buffers returned by the firmware but not yet
1633 * processed by the upper layers.
1634 **/
James Smarted957682007-06-17 19:56:37 -05001635void
1636lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1637{
James Smart92d7f7b2007-06-17 19:56:38 -05001638 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1639 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001640 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001641 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001642 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001643
James Smart51ef4c22007-08-02 11:10:31 -04001644 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001645 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001646 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001647 for (i = 0; i < hbq_count; ++i) {
1648 list_for_each_entry_safe(dmabuf, next_dmabuf,
1649 &phba->hbqs[i].hbq_buffer_list, list) {
1650 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1651 list_del(&hbq_buf->dbuf.list);
1652 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1653 }
James Smarta8adb832007-10-27 13:37:53 -04001654 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001655 }
James Smart3163f722008-02-08 18:50:25 -05001656 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001657 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1658 list) {
James Smart3163f722008-02-08 18:50:25 -05001659 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1660 list_del(&hbq_buf->dbuf.list);
1661 if (hbq_buf->tag == -1) {
1662 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1663 (phba, hbq_buf);
1664 } else {
1665 hbqno = hbq_buf->tag >> 16;
1666 if (hbqno >= LPFC_MAX_HBQS)
1667 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1668 (phba, hbq_buf);
1669 else
1670 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1671 hbq_buf);
1672 }
1673 }
1674
1675 /* Mark the HBQs not in use */
1676 phba->hbq_in_use = 0;
1677 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001678}
1679
James Smarte59058c2008-08-24 21:49:00 -04001680/**
James Smart3621a712009-04-06 18:47:14 -04001681 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001682 * @phba: Pointer to HBA context object.
1683 * @hbqno: HBQ number.
1684 * @hbq_buf: Pointer to HBQ buffer.
1685 *
1686 * This function is called with the hbalock held to post a
1687 * hbq buffer to the firmware. If the function finds an empty
1688 * slot in the HBQ, it will post the buffer. The function will return
1689 * pointer to the hbq entry if it successfully post the buffer
1690 * else it will return NULL.
1691 **/
James Smart3772a992009-05-22 14:50:54 -04001692static int
James Smarted957682007-06-17 19:56:37 -05001693lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001694 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001695{
James Smart3772a992009-05-22 14:50:54 -04001696 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1697}
1698
1699/**
1700 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1701 * @phba: Pointer to HBA context object.
1702 * @hbqno: HBQ number.
1703 * @hbq_buf: Pointer to HBQ buffer.
1704 *
1705 * This function is called with the hbalock held to post a hbq buffer to the
1706 * firmware. If the function finds an empty slot in the HBQ, it will post the
1707 * buffer and place it on the hbq_buffer_list. The function will return zero if
1708 * it successfully post the buffer else it will return an error.
1709 **/
1710static int
1711lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1712 struct hbq_dmabuf *hbq_buf)
1713{
James Smarted957682007-06-17 19:56:37 -05001714 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001715 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001716
1717 /* Get next HBQ entry slot to use */
1718 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1719 if (hbqe) {
1720 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1721
James Smart92d7f7b2007-06-17 19:56:38 -05001722 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1723 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001724 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001725 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001726 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1727 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1728 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001729 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1730 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001731 /* flush */
James Smarted957682007-06-17 19:56:37 -05001732 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001733 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001734 return 0;
1735 } else
1736 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001737}
1738
James Smart4f774512009-05-22 14:52:35 -04001739/**
1740 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1741 * @phba: Pointer to HBA context object.
1742 * @hbqno: HBQ number.
1743 * @hbq_buf: Pointer to HBQ buffer.
1744 *
1745 * This function is called with the hbalock held to post an RQE to the SLI4
1746 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1747 * the hbq_buffer_list and return zero, otherwise it will return an error.
1748 **/
1749static int
1750lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1751 struct hbq_dmabuf *hbq_buf)
1752{
1753 int rc;
1754 struct lpfc_rqe hrqe;
1755 struct lpfc_rqe drqe;
1756
1757 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1758 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1759 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1760 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1761 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1762 &hrqe, &drqe);
1763 if (rc < 0)
1764 return rc;
1765 hbq_buf->tag = rc;
1766 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1767 return 0;
1768}
1769
James Smarte59058c2008-08-24 21:49:00 -04001770/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001771static struct lpfc_hbq_init lpfc_els_hbq = {
1772 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001773 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001774 .mask_count = 0,
1775 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001776 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001777 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001778 .init_count = 40,
1779 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001780};
James Smarted957682007-06-17 19:56:37 -05001781
James Smarte59058c2008-08-24 21:49:00 -04001782/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001783static struct lpfc_hbq_init lpfc_extra_hbq = {
1784 .rn = 1,
1785 .entry_count = 200,
1786 .mask_count = 0,
1787 .profile = 0,
1788 .ring_mask = (1 << LPFC_EXTRA_RING),
1789 .buffer_count = 0,
1790 .init_count = 0,
1791 .add_count = 5,
1792};
1793
James Smarte59058c2008-08-24 21:49:00 -04001794/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001795struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001796 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001797 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001798};
1799
James Smarte59058c2008-08-24 21:49:00 -04001800/**
James Smart3621a712009-04-06 18:47:14 -04001801 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001802 * @phba: Pointer to HBA context object.
1803 * @hbqno: HBQ number.
1804 * @count: Number of HBQ buffers to be posted.
1805 *
James Smartd7c255b2008-08-24 21:50:00 -04001806 * This function is called with no lock held to post more hbq buffers to the
1807 * given HBQ. The function returns the number of HBQ buffers successfully
1808 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001809 **/
James Smart311464e2007-08-02 11:10:37 -04001810static int
James Smart92d7f7b2007-06-17 19:56:38 -05001811lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1812{
James Smartd7c255b2008-08-24 21:50:00 -04001813 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001814 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001815 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001816 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001817 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001818 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001819
James Smartd7c255b2008-08-24 21:50:00 -04001820 if ((phba->hbqs[hbqno].buffer_count + count) >
1821 lpfc_hbq_defs[hbqno]->entry_count)
1822 count = lpfc_hbq_defs[hbqno]->entry_count -
1823 phba->hbqs[hbqno].buffer_count;
1824 if (!count)
1825 return 0;
1826 /* Allocate HBQ entries */
1827 for (i = 0; i < count; i++) {
1828 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1829 if (!hbq_buffer)
1830 break;
1831 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1832 }
James Smart3163f722008-02-08 18:50:25 -05001833 /* Check whether HBQ is still in use */
1834 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001835 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001836 goto err;
1837 while (!list_empty(&hbq_buf_list)) {
1838 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1839 dbuf.list);
1840 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1841 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001842 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001843 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001844 posted++;
1845 } else
James Smart51ef4c22007-08-02 11:10:31 -04001846 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001847 }
James Smart3163f722008-02-08 18:50:25 -05001848 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001849 return posted;
1850err:
1851 spin_unlock_irqrestore(&phba->hbalock, flags);
1852 while (!list_empty(&hbq_buf_list)) {
1853 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1854 dbuf.list);
1855 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1856 }
James Smart92d7f7b2007-06-17 19:56:38 -05001857 return 0;
James Smarted957682007-06-17 19:56:37 -05001858}
1859
James Smarte59058c2008-08-24 21:49:00 -04001860/**
James Smart3621a712009-04-06 18:47:14 -04001861 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001862 * @phba: Pointer to HBA context object.
1863 * @qno: HBQ number.
1864 *
1865 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001866 * is called with no lock held. The function returns the number of HBQ entries
1867 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001868 **/
James Smarted957682007-06-17 19:56:37 -05001869int
James Smart92d7f7b2007-06-17 19:56:38 -05001870lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001871{
James Smartdef9c7a2009-12-21 17:02:28 -05001872 if (phba->sli_rev == LPFC_SLI_REV4)
1873 return 0;
1874 else
1875 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1876 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001877}
1878
James Smarte59058c2008-08-24 21:49:00 -04001879/**
James Smart3621a712009-04-06 18:47:14 -04001880 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001881 * @phba: Pointer to HBA context object.
1882 * @qno: HBQ queue number.
1883 *
1884 * This function is called from SLI initialization code path with
1885 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001886 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001887 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001888static int
James Smart92d7f7b2007-06-17 19:56:38 -05001889lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001890{
James Smartdef9c7a2009-12-21 17:02:28 -05001891 if (phba->sli_rev == LPFC_SLI_REV4)
1892 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1893 lpfc_hbq_defs[qno]->entry_count);
1894 else
1895 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1896 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001897}
1898
James Smarte59058c2008-08-24 21:49:00 -04001899/**
James Smart3772a992009-05-22 14:50:54 -04001900 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1901 * @phba: Pointer to HBA context object.
1902 * @hbqno: HBQ number.
1903 *
1904 * This function removes the first hbq buffer on an hbq list and returns a
1905 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1906 **/
1907static struct hbq_dmabuf *
1908lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1909{
1910 struct lpfc_dmabuf *d_buf;
1911
1912 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1913 if (!d_buf)
1914 return NULL;
1915 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1916}
1917
1918/**
James Smart3621a712009-04-06 18:47:14 -04001919 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001920 * @phba: Pointer to HBA context object.
1921 * @tag: Tag of the hbq buffer.
1922 *
1923 * This function is called with hbalock held. This function searches
1924 * for the hbq buffer associated with the given tag in the hbq buffer
1925 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1926 * it returns NULL.
1927 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001928static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001929lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1930{
James Smart92d7f7b2007-06-17 19:56:38 -05001931 struct lpfc_dmabuf *d_buf;
1932 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001933 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001934
James Smart51ef4c22007-08-02 11:10:31 -04001935 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001936 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001937 return NULL;
1938
James Smart3772a992009-05-22 14:50:54 -04001939 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001940 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001941 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001942 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001943 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001944 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001945 }
1946 }
James Smart3772a992009-05-22 14:50:54 -04001947 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001949 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001950 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001951 return NULL;
James Smarted957682007-06-17 19:56:37 -05001952}
1953
James Smarte59058c2008-08-24 21:49:00 -04001954/**
James Smart3621a712009-04-06 18:47:14 -04001955 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001956 * @phba: Pointer to HBA context object.
1957 * @hbq_buffer: Pointer to HBQ buffer.
1958 *
1959 * This function is called with hbalock. This function gives back
1960 * the hbq buffer to firmware. If the HBQ does not have space to
1961 * post the buffer, it will free the buffer.
1962 **/
James Smarted957682007-06-17 19:56:37 -05001963void
James Smart51ef4c22007-08-02 11:10:31 -04001964lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001965{
1966 uint32_t hbqno;
1967
James Smart51ef4c22007-08-02 11:10:31 -04001968 if (hbq_buffer) {
1969 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001970 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001971 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001972 }
1973}
1974
James Smarte59058c2008-08-24 21:49:00 -04001975/**
James Smart3621a712009-04-06 18:47:14 -04001976 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001977 * @mbxCommand: mailbox command code.
1978 *
1979 * This function is called by the mailbox event handler function to verify
1980 * that the completed mailbox command is a legitimate mailbox command. If the
1981 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1982 * and the mailbox event handler will take the HBA offline.
1983 **/
dea31012005-04-17 16:05:31 -05001984static int
1985lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1986{
1987 uint8_t ret;
1988
1989 switch (mbxCommand) {
1990 case MBX_LOAD_SM:
1991 case MBX_READ_NV:
1992 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001993 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001994 case MBX_RUN_BIU_DIAG:
1995 case MBX_INIT_LINK:
1996 case MBX_DOWN_LINK:
1997 case MBX_CONFIG_LINK:
1998 case MBX_CONFIG_RING:
1999 case MBX_RESET_RING:
2000 case MBX_READ_CONFIG:
2001 case MBX_READ_RCONFIG:
2002 case MBX_READ_SPARM:
2003 case MBX_READ_STATUS:
2004 case MBX_READ_RPI:
2005 case MBX_READ_XRI:
2006 case MBX_READ_REV:
2007 case MBX_READ_LNK_STAT:
2008 case MBX_REG_LOGIN:
2009 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002010 case MBX_CLEAR_LA:
2011 case MBX_DUMP_MEMORY:
2012 case MBX_DUMP_CONTEXT:
2013 case MBX_RUN_DIAGS:
2014 case MBX_RESTART:
2015 case MBX_UPDATE_CFG:
2016 case MBX_DOWN_LOAD:
2017 case MBX_DEL_LD_ENTRY:
2018 case MBX_RUN_PROGRAM:
2019 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002020 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002021 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002022 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002023 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002024 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002025 case MBX_LOAD_AREA:
2026 case MBX_RUN_BIU_DIAG64:
2027 case MBX_CONFIG_PORT:
2028 case MBX_READ_SPARM64:
2029 case MBX_READ_RPI64:
2030 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002031 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002032 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002033 case MBX_SET_DEBUG:
2034 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002035 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002036 case MBX_REG_VPI:
2037 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002038 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002039 case MBX_PORT_CAPABILITIES:
2040 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002041 case MBX_SLI4_CONFIG:
2042 case MBX_SLI4_REQ_FTRS:
2043 case MBX_REG_FCFI:
2044 case MBX_UNREG_FCFI:
2045 case MBX_REG_VFI:
2046 case MBX_UNREG_VFI:
2047 case MBX_INIT_VPI:
2048 case MBX_INIT_VFI:
2049 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002050 case MBX_READ_EVENT_LOG_STATUS:
2051 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002052 case MBX_SECURITY_MGMT:
2053 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002054 ret = mbxCommand;
2055 break;
2056 default:
2057 ret = MBX_SHUTDOWN;
2058 break;
2059 }
James Smart2e0fef82007-06-17 19:56:36 -05002060 return ret;
dea31012005-04-17 16:05:31 -05002061}
James Smarte59058c2008-08-24 21:49:00 -04002062
2063/**
James Smart3621a712009-04-06 18:47:14 -04002064 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002065 * @phba: Pointer to HBA context object.
2066 * @pmboxq: Pointer to mailbox command.
2067 *
2068 * This is completion handler function for mailbox commands issued from
2069 * lpfc_sli_issue_mbox_wait function. This function is called by the
2070 * mailbox event handler function with no lock held. This function
2071 * will wake up thread waiting on the wait queue pointed by context1
2072 * of the mailbox.
2073 **/
James Smart04c68492009-05-22 14:52:52 -04002074void
James Smart2e0fef82007-06-17 19:56:36 -05002075lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002076{
2077 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002078 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002079
2080 /*
2081 * If pdone_q is empty, the driver thread gave up waiting and
2082 * continued running.
2083 */
James Smart7054a602007-04-25 09:52:34 -04002084 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002085 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002086 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2087 if (pdone_q)
2088 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002089 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002090 return;
2091}
2092
James Smarte59058c2008-08-24 21:49:00 -04002093
2094/**
James Smart3621a712009-04-06 18:47:14 -04002095 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002096 * @phba: Pointer to HBA context object.
2097 * @pmb: Pointer to mailbox object.
2098 *
2099 * This function is the default mailbox completion handler. It
2100 * frees the memory resources associated with the completed mailbox
2101 * command. If the completed command is a REG_LOGIN mailbox command,
2102 * this function will issue a UREG_LOGIN to re-claim the RPI.
2103 **/
dea31012005-04-17 16:05:31 -05002104void
James Smart2e0fef82007-06-17 19:56:36 -05002105lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002106{
James Smartd439d282010-09-29 11:18:45 -04002107 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002108 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002109 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002110 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002111 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002112 int rc;
2113
dea31012005-04-17 16:05:31 -05002114 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002115
dea31012005-04-17 16:05:31 -05002116 if (mp) {
2117 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2118 kfree(mp);
2119 }
James Smart7054a602007-04-25 09:52:34 -04002120
James Smart04c68492009-05-22 14:52:52 -04002121 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
James Smart5af5eee2010-10-22 11:06:38 -04002122 (phba->sli_rev == LPFC_SLI_REV4) &&
2123 (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
James Smart04c68492009-05-22 14:52:52 -04002124 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
2125
James Smart7054a602007-04-25 09:52:34 -04002126 /*
2127 * If a REG_LOGIN succeeded after node is destroyed or node
2128 * is in re-discovery driver need to cleanup the RPI.
2129 */
James Smart2e0fef82007-06-17 19:56:36 -05002130 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002131 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2132 !pmb->u.mb.mbxStatus) {
2133 rpi = pmb->u.mb.un.varWords[0];
2134 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2135 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002136 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002137 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2138 if (rc != MBX_NOT_FINISHED)
2139 return;
2140 }
2141
James Smart695a8142010-01-26 23:08:03 -05002142 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2143 !(phba->pport->load_flag & FC_UNLOADING) &&
2144 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002145 shost = lpfc_shost_from_vport(vport);
2146 spin_lock_irq(shost->host_lock);
2147 vport->vpi_state |= LPFC_VPI_REGISTERED;
2148 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2149 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002150 }
2151
James Smartd439d282010-09-29 11:18:45 -04002152 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2153 ndlp = (struct lpfc_nodelist *)pmb->context2;
2154 lpfc_nlp_put(ndlp);
2155 pmb->context2 = NULL;
2156 }
2157
James Smartdcf2a4e2010-09-29 11:18:53 -04002158 /* Check security permission status on INIT_LINK mailbox command */
2159 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2160 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2161 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2162 "2860 SLI authentication is required "
2163 "for INIT_LINK but has not done yet\n");
2164
James Smart04c68492009-05-22 14:52:52 -04002165 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2166 lpfc_sli4_mbox_cmd_free(phba, pmb);
2167 else
2168 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002169}
2170
James Smarte59058c2008-08-24 21:49:00 -04002171/**
James Smart3621a712009-04-06 18:47:14 -04002172 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002173 * @phba: Pointer to HBA context object.
2174 *
2175 * This function is called with no lock held. This function processes all
2176 * the completed mailbox commands and gives it to upper layers. The interrupt
2177 * service routine processes mailbox completion interrupt and adds completed
2178 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2179 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2180 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2181 * function returns the mailbox commands to the upper layer by calling the
2182 * completion handler function of each mailbox.
2183 **/
dea31012005-04-17 16:05:31 -05002184int
James Smart2e0fef82007-06-17 19:56:36 -05002185lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002186{
James Smart92d7f7b2007-06-17 19:56:38 -05002187 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002188 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002189 int rc;
2190 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002191
2192 phba->sli.slistat.mbox_event++;
2193
James Smart92d7f7b2007-06-17 19:56:38 -05002194 /* Get all completed mailboxe buffers into the cmplq */
2195 spin_lock_irq(&phba->hbalock);
2196 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2197 spin_unlock_irq(&phba->hbalock);
2198
dea31012005-04-17 16:05:31 -05002199 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002200 do {
2201 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2202 if (pmb == NULL)
2203 break;
2204
James Smart04c68492009-05-22 14:52:52 -04002205 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002206
James Smart858c9f62007-06-17 19:56:39 -05002207 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2208 if (pmb->vport) {
2209 lpfc_debugfs_disc_trc(pmb->vport,
2210 LPFC_DISC_TRC_MBOX_VPORT,
2211 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2212 (uint32_t)pmbox->mbxCommand,
2213 pmbox->un.varWords[0],
2214 pmbox->un.varWords[1]);
2215 }
2216 else {
2217 lpfc_debugfs_disc_trc(phba->pport,
2218 LPFC_DISC_TRC_MBOX,
2219 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2220 (uint32_t)pmbox->mbxCommand,
2221 pmbox->un.varWords[0],
2222 pmbox->un.varWords[1]);
2223 }
2224 }
2225
dea31012005-04-17 16:05:31 -05002226 /*
2227 * It is a fatal error if unknown mbox command completion.
2228 */
2229 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2230 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002231 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002233 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04002234 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002235 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002236 pmbox->mbxCommand,
2237 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002238 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002239 phba->work_hs = HS_FFER3;
2240 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002241 continue;
dea31012005-04-17 16:05:31 -05002242 }
2243
dea31012005-04-17 16:05:31 -05002244 if (pmbox->mbxStatus) {
2245 phba->sli.slistat.mbox_stat_err++;
2246 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2247 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002248 lpfc_printf_log(phba, KERN_INFO,
2249 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002250 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002251 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002252 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002253 pmb->vport ? pmb->vport->vpi :0,
2254 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002255 lpfc_sli4_mbox_opcode_get(phba,
2256 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002257 pmbox->mbxStatus,
2258 pmbox->un.varWords[0],
2259 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002260 pmbox->mbxStatus = 0;
2261 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002262 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002263 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002264 continue;
dea31012005-04-17 16:05:31 -05002265 }
2266 }
2267
2268 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002269 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002270 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002271 "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 -05002272 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002273 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002274 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002275 pmb->mbox_cmpl,
2276 *((uint32_t *) pmbox),
2277 pmbox->un.varWords[0],
2278 pmbox->un.varWords[1],
2279 pmbox->un.varWords[2],
2280 pmbox->un.varWords[3],
2281 pmbox->un.varWords[4],
2282 pmbox->un.varWords[5],
2283 pmbox->un.varWords[6],
2284 pmbox->un.varWords[7]);
2285
James Smart92d7f7b2007-06-17 19:56:38 -05002286 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002287 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002288 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002289 return 0;
dea31012005-04-17 16:05:31 -05002290}
James Smart92d7f7b2007-06-17 19:56:38 -05002291
James Smarte59058c2008-08-24 21:49:00 -04002292/**
James Smart3621a712009-04-06 18:47:14 -04002293 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002294 * @phba: Pointer to HBA context object.
2295 * @pring: Pointer to driver SLI ring object.
2296 * @tag: buffer tag.
2297 *
2298 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2299 * is set in the tag the buffer is posted for a particular exchange,
2300 * the function will return the buffer without replacing the buffer.
2301 * If the buffer is for unsolicited ELS or CT traffic, this function
2302 * returns the buffer and also posts another buffer to the firmware.
2303 **/
James Smart76bb24e2007-10-27 13:38:00 -04002304static struct lpfc_dmabuf *
2305lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002306 struct lpfc_sli_ring *pring,
2307 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002308{
James Smart9f1e1b52008-12-04 22:39:40 -05002309 struct hbq_dmabuf *hbq_entry;
2310
James Smart76bb24e2007-10-27 13:38:00 -04002311 if (tag & QUE_BUFTAG_BIT)
2312 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002313 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2314 if (!hbq_entry)
2315 return NULL;
2316 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002317}
James Smart57127f12007-10-27 13:37:05 -04002318
James Smart3772a992009-05-22 14:50:54 -04002319/**
2320 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2321 * @phba: Pointer to HBA context object.
2322 * @pring: Pointer to driver SLI ring object.
2323 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2324 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2325 * @fch_type: the type for the first frame of the sequence.
2326 *
2327 * This function is called with no lock held. This function uses the r_ctl and
2328 * type of the received sequence to find the correct callback function to call
2329 * to process the sequence.
2330 **/
2331static int
2332lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2333 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2334 uint32_t fch_type)
2335{
2336 int i;
2337
2338 /* unSolicited Responses */
2339 if (pring->prt[0].profile) {
2340 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2341 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2342 saveq);
2343 return 1;
2344 }
2345 /* We must search, based on rctl / type
2346 for the right routine */
2347 for (i = 0; i < pring->num_mask; i++) {
2348 if ((pring->prt[i].rctl == fch_r_ctl) &&
2349 (pring->prt[i].type == fch_type)) {
2350 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2351 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2352 (phba, pring, saveq);
2353 return 1;
2354 }
2355 }
2356 return 0;
2357}
James Smarte59058c2008-08-24 21:49:00 -04002358
2359/**
James Smart3621a712009-04-06 18:47:14 -04002360 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002361 * @phba: Pointer to HBA context object.
2362 * @pring: Pointer to driver SLI ring object.
2363 * @saveq: Pointer to the unsolicited iocb.
2364 *
2365 * This function is called with no lock held by the ring event handler
2366 * when there is an unsolicited iocb posted to the response ring by the
2367 * firmware. This function gets the buffer associated with the iocbs
2368 * and calls the event handler for the ring. This function handles both
2369 * qring buffers and hbq buffers.
2370 * When the function returns 1 the caller can free the iocb object otherwise
2371 * upper layer functions will free the iocb objects.
2372 **/
dea31012005-04-17 16:05:31 -05002373static int
2374lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2375 struct lpfc_iocbq *saveq)
2376{
2377 IOCB_t * irsp;
2378 WORD5 * w5p;
2379 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002380 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002381 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002382 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002383
2384 match = 0;
2385 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002386
2387 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2388 if (pring->lpfc_sli_rcv_async_status)
2389 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2390 else
2391 lpfc_printf_log(phba,
2392 KERN_WARNING,
2393 LOG_SLI,
2394 "0316 Ring %d handler: unexpected "
2395 "ASYNC_STATUS iocb received evt_code "
2396 "0x%x\n",
2397 pring->ringno,
2398 irsp->un.asyncstat.evt_code);
2399 return 1;
2400 }
2401
James Smart3163f722008-02-08 18:50:25 -05002402 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2403 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2404 if (irsp->ulpBdeCount > 0) {
2405 dmzbuf = lpfc_sli_get_buff(phba, pring,
2406 irsp->un.ulpWord[3]);
2407 lpfc_in_buf_free(phba, dmzbuf);
2408 }
2409
2410 if (irsp->ulpBdeCount > 1) {
2411 dmzbuf = lpfc_sli_get_buff(phba, pring,
2412 irsp->unsli3.sli3Words[3]);
2413 lpfc_in_buf_free(phba, dmzbuf);
2414 }
2415
2416 if (irsp->ulpBdeCount > 2) {
2417 dmzbuf = lpfc_sli_get_buff(phba, pring,
2418 irsp->unsli3.sli3Words[7]);
2419 lpfc_in_buf_free(phba, dmzbuf);
2420 }
2421
2422 return 1;
2423 }
2424
James Smart92d7f7b2007-06-17 19:56:38 -05002425 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002426 if (irsp->ulpBdeCount != 0) {
2427 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002428 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002429 if (!saveq->context2)
2430 lpfc_printf_log(phba,
2431 KERN_ERR,
2432 LOG_SLI,
2433 "0341 Ring %d Cannot find buffer for "
2434 "an unsolicited iocb. tag 0x%x\n",
2435 pring->ringno,
2436 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002437 }
2438 if (irsp->ulpBdeCount == 2) {
2439 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002440 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002441 if (!saveq->context3)
2442 lpfc_printf_log(phba,
2443 KERN_ERR,
2444 LOG_SLI,
2445 "0342 Ring %d Cannot find buffer for an"
2446 " unsolicited iocb. tag 0x%x\n",
2447 pring->ringno,
2448 irsp->unsli3.sli3Words[7]);
2449 }
2450 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002451 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002452 if (irsp->ulpBdeCount != 0) {
2453 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2454 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002455 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002456 lpfc_printf_log(phba,
2457 KERN_ERR,
2458 LOG_SLI,
2459 "0343 Ring %d Cannot find "
2460 "buffer for an unsolicited iocb"
2461 ". tag 0x%x\n", pring->ringno,
2462 irsp->un.ulpWord[3]);
2463 }
2464 if (irsp->ulpBdeCount == 2) {
2465 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2466 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002467 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002468 lpfc_printf_log(phba,
2469 KERN_ERR,
2470 LOG_SLI,
2471 "0344 Ring %d Cannot find "
2472 "buffer for an unsolicited "
2473 "iocb. tag 0x%x\n",
2474 pring->ringno,
2475 irsp->unsli3.sli3Words[7]);
2476 }
2477 }
James Smart92d7f7b2007-06-17 19:56:38 -05002478 }
James Smart9c2face2008-01-11 01:53:18 -05002479 if (irsp->ulpBdeCount != 0 &&
2480 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2481 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2482 int found = 0;
2483
2484 /* search continue save q for same XRI */
2485 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2486 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2487 list_add_tail(&saveq->list, &iocbq->list);
2488 found = 1;
2489 break;
2490 }
2491 }
2492 if (!found)
2493 list_add_tail(&saveq->clist,
2494 &pring->iocb_continue_saveq);
2495 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2496 list_del_init(&iocbq->clist);
2497 saveq = iocbq;
2498 irsp = &(saveq->iocb);
2499 } else
2500 return 0;
2501 }
2502 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2503 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2504 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002505 Rctl = FC_RCTL_ELS_REQ;
2506 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002507 } else {
2508 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2509 Rctl = w5p->hcsw.Rctl;
2510 Type = w5p->hcsw.Type;
2511
2512 /* Firmware Workaround */
2513 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2514 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2515 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002516 Rctl = FC_RCTL_ELS_REQ;
2517 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002518 w5p->hcsw.Rctl = Rctl;
2519 w5p->hcsw.Type = Type;
2520 }
2521 }
James Smart92d7f7b2007-06-17 19:56:38 -05002522
James Smart3772a992009-05-22 14:50:54 -04002523 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002524 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002525 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002526 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002527 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002528
James Smart92d7f7b2007-06-17 19:56:38 -05002529 return 1;
dea31012005-04-17 16:05:31 -05002530}
2531
James Smarte59058c2008-08-24 21:49:00 -04002532/**
James Smart3621a712009-04-06 18:47:14 -04002533 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002534 * @phba: Pointer to HBA context object.
2535 * @pring: Pointer to driver SLI ring object.
2536 * @prspiocb: Pointer to response iocb object.
2537 *
2538 * This function looks up the iocb_lookup table to get the command iocb
2539 * corresponding to the given response iocb using the iotag of the
2540 * response iocb. This function is called with the hbalock held.
2541 * This function returns the command iocb object if it finds the command
2542 * iocb else returns NULL.
2543 **/
dea31012005-04-17 16:05:31 -05002544static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002545lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2546 struct lpfc_sli_ring *pring,
2547 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002548{
dea31012005-04-17 16:05:31 -05002549 struct lpfc_iocbq *cmd_iocb = NULL;
2550 uint16_t iotag;
2551
James Bottomley604a3e32005-10-29 10:28:33 -05002552 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002553
James Bottomley604a3e32005-10-29 10:28:33 -05002554 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2555 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002556 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002557 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2558 pring->txcmplq_cnt--;
2559 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2560 }
James Bottomley604a3e32005-10-29 10:28:33 -05002561 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002562 }
2563
dea31012005-04-17 16:05:31 -05002564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002565 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002566 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002567 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002568 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002569 return NULL;
2570}
2571
James Smarte59058c2008-08-24 21:49:00 -04002572/**
James Smart3772a992009-05-22 14:50:54 -04002573 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2574 * @phba: Pointer to HBA context object.
2575 * @pring: Pointer to driver SLI ring object.
2576 * @iotag: IOCB tag.
2577 *
2578 * This function looks up the iocb_lookup table to get the command iocb
2579 * corresponding to the given iotag. This function is called with the
2580 * hbalock held.
2581 * This function returns the command iocb object if it finds the command
2582 * iocb else returns NULL.
2583 **/
2584static struct lpfc_iocbq *
2585lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2586 struct lpfc_sli_ring *pring, uint16_t iotag)
2587{
2588 struct lpfc_iocbq *cmd_iocb;
2589
2590 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2591 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2592 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002593 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2594 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2595 pring->txcmplq_cnt--;
2596 }
James Smart3772a992009-05-22 14:50:54 -04002597 return cmd_iocb;
2598 }
2599
2600 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2601 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2602 iotag, phba->sli.last_iotag);
2603 return NULL;
2604}
2605
2606/**
James Smart3621a712009-04-06 18:47:14 -04002607 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002608 * @phba: Pointer to HBA context object.
2609 * @pring: Pointer to driver SLI ring object.
2610 * @saveq: Pointer to the response iocb to be processed.
2611 *
2612 * This function is called by the ring event handler for non-fcp
2613 * rings when there is a new response iocb in the response ring.
2614 * The caller is not required to hold any locks. This function
2615 * gets the command iocb associated with the response iocb and
2616 * calls the completion handler for the command iocb. If there
2617 * is no completion handler, the function will free the resources
2618 * associated with command iocb. If the response iocb is for
2619 * an already aborted command iocb, the status of the completion
2620 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2621 * This function always returns 1.
2622 **/
dea31012005-04-17 16:05:31 -05002623static int
James Smart2e0fef82007-06-17 19:56:36 -05002624lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002625 struct lpfc_iocbq *saveq)
2626{
James Smart2e0fef82007-06-17 19:56:36 -05002627 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002628 int rc = 1;
2629 unsigned long iflag;
2630
2631 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002632 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002633 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002634 spin_unlock_irqrestore(&phba->hbalock, iflag);
2635
dea31012005-04-17 16:05:31 -05002636 if (cmdiocbp) {
2637 if (cmdiocbp->iocb_cmpl) {
2638 /*
James Smartea2151b2008-09-07 11:52:10 -04002639 * If an ELS command failed send an event to mgmt
2640 * application.
2641 */
2642 if (saveq->iocb.ulpStatus &&
2643 (pring->ringno == LPFC_ELS_RING) &&
2644 (cmdiocbp->iocb.ulpCommand ==
2645 CMD_ELS_REQUEST64_CR))
2646 lpfc_send_els_failure_event(phba,
2647 cmdiocbp, saveq);
2648
2649 /*
dea31012005-04-17 16:05:31 -05002650 * Post all ELS completions to the worker thread.
2651 * All other are passed to the completion callback.
2652 */
2653 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002654 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2655 (cmdiocbp->iocb_flag &
2656 LPFC_DRIVER_ABORTED)) {
2657 spin_lock_irqsave(&phba->hbalock,
2658 iflag);
James Smart07951072007-04-25 09:51:38 -04002659 cmdiocbp->iocb_flag &=
2660 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002661 spin_unlock_irqrestore(&phba->hbalock,
2662 iflag);
James Smart07951072007-04-25 09:51:38 -04002663 saveq->iocb.ulpStatus =
2664 IOSTAT_LOCAL_REJECT;
2665 saveq->iocb.un.ulpWord[4] =
2666 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002667
2668 /* Firmware could still be in progress
2669 * of DMAing payload, so don't free data
2670 * buffer till after a hbeat.
2671 */
James Smart341af102010-01-26 23:07:37 -05002672 spin_lock_irqsave(&phba->hbalock,
2673 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002674 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002675 spin_unlock_irqrestore(&phba->hbalock,
2676 iflag);
2677 }
James Smart0f65ff62010-02-26 14:14:23 -05002678 if (phba->sli_rev == LPFC_SLI_REV4) {
2679 if (saveq->iocb_flag &
2680 LPFC_EXCHANGE_BUSY) {
2681 /* Set cmdiocb flag for the
2682 * exchange busy so sgl (xri)
2683 * will not be released until
2684 * the abort xri is received
2685 * from hba.
2686 */
2687 spin_lock_irqsave(
2688 &phba->hbalock, iflag);
2689 cmdiocbp->iocb_flag |=
2690 LPFC_EXCHANGE_BUSY;
2691 spin_unlock_irqrestore(
2692 &phba->hbalock, iflag);
2693 }
2694 if (cmdiocbp->iocb_flag &
2695 LPFC_DRIVER_ABORTED) {
2696 /*
2697 * Clear LPFC_DRIVER_ABORTED
2698 * bit in case it was driver
2699 * initiated abort.
2700 */
2701 spin_lock_irqsave(
2702 &phba->hbalock, iflag);
2703 cmdiocbp->iocb_flag &=
2704 ~LPFC_DRIVER_ABORTED;
2705 spin_unlock_irqrestore(
2706 &phba->hbalock, iflag);
2707 cmdiocbp->iocb.ulpStatus =
2708 IOSTAT_LOCAL_REJECT;
2709 cmdiocbp->iocb.un.ulpWord[4] =
2710 IOERR_ABORT_REQUESTED;
2711 /*
2712 * For SLI4, irsiocb contains
2713 * NO_XRI in sli_xritag, it
2714 * shall not affect releasing
2715 * sgl (xri) process.
2716 */
2717 saveq->iocb.ulpStatus =
2718 IOSTAT_LOCAL_REJECT;
2719 saveq->iocb.un.ulpWord[4] =
2720 IOERR_SLI_ABORTED;
2721 spin_lock_irqsave(
2722 &phba->hbalock, iflag);
2723 saveq->iocb_flag |=
2724 LPFC_DELAY_MEM_FREE;
2725 spin_unlock_irqrestore(
2726 &phba->hbalock, iflag);
2727 }
James Smart07951072007-04-25 09:51:38 -04002728 }
dea31012005-04-17 16:05:31 -05002729 }
James Smart2e0fef82007-06-17 19:56:36 -05002730 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002731 } else
2732 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002733 } else {
2734 /*
2735 * Unknown initiating command based on the response iotag.
2736 * This could be the case on the ELS ring because of
2737 * lpfc_els_abort().
2738 */
2739 if (pring->ringno != LPFC_ELS_RING) {
2740 /*
2741 * Ring <ringno> handler: unexpected completion IoTag
2742 * <IoTag>
2743 */
James Smarta257bf92009-04-06 18:48:10 -04002744 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002745 "0322 Ring %d handler: "
2746 "unexpected completion IoTag x%x "
2747 "Data: x%x x%x x%x x%x\n",
2748 pring->ringno,
2749 saveq->iocb.ulpIoTag,
2750 saveq->iocb.ulpStatus,
2751 saveq->iocb.un.ulpWord[4],
2752 saveq->iocb.ulpCommand,
2753 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002754 }
2755 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002756
dea31012005-04-17 16:05:31 -05002757 return rc;
2758}
2759
James Smarte59058c2008-08-24 21:49:00 -04002760/**
James Smart3621a712009-04-06 18:47:14 -04002761 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002762 * @phba: Pointer to HBA context object.
2763 * @pring: Pointer to driver SLI ring object.
2764 *
2765 * This function is called from the iocb ring event handlers when
2766 * put pointer is ahead of the get pointer for a ring. This function signal
2767 * an error attention condition to the worker thread and the worker
2768 * thread will transition the HBA to offline state.
2769 **/
James Smart2e0fef82007-06-17 19:56:36 -05002770static void
2771lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002772{
James Smart34b02dc2008-08-24 21:49:55 -04002773 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002774 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002775 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002776 * rsp ring <portRspMax>
2777 */
2778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002779 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002780 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002781 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002782 pring->numRiocb);
2783
James Smart2e0fef82007-06-17 19:56:36 -05002784 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002785
2786 /*
2787 * All error attention handlers are posted to
2788 * worker thread
2789 */
2790 phba->work_ha |= HA_ERATT;
2791 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002792
James Smart5e9d9b82008-06-14 22:52:53 -04002793 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002794
2795 return;
2796}
2797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002800 * @ptr: Pointer to address of HBA context object.
2801 *
2802 * This function is invoked by the Error Attention polling timer when the
2803 * timer times out. It will check the SLI Error Attention register for
2804 * possible attention events. If so, it will post an Error Attention event
2805 * and wake up worker thread to process it. Otherwise, it will set up the
2806 * Error Attention polling timer for the next poll.
2807 **/
2808void lpfc_poll_eratt(unsigned long ptr)
2809{
2810 struct lpfc_hba *phba;
2811 uint32_t eratt = 0;
2812
2813 phba = (struct lpfc_hba *)ptr;
2814
2815 /* Check chip HA register for error event */
2816 eratt = lpfc_sli_check_eratt(phba);
2817
2818 if (eratt)
2819 /* Tell the worker thread there is work to do */
2820 lpfc_worker_wake_up(phba);
2821 else
2822 /* Restart the timer for next eratt poll */
2823 mod_timer(&phba->eratt_poll, jiffies +
2824 HZ * LPFC_ERATT_POLL_INTERVAL);
2825 return;
2826}
2827
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002828
James Smarte59058c2008-08-24 21:49:00 -04002829/**
James Smart3621a712009-04-06 18:47:14 -04002830 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002831 * @phba: Pointer to HBA context object.
2832 * @pring: Pointer to driver SLI ring object.
2833 * @mask: Host attention register mask for this ring.
2834 *
2835 * This function is called from the interrupt context when there is a ring
2836 * event for the fcp ring. The caller does not hold any lock.
2837 * The function processes each response iocb in the response ring until it
2838 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2839 * LE bit set. The function will call the completion handler of the command iocb
2840 * if the response iocb indicates a completion for a command iocb or it is
2841 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2842 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002843 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002844 * to check it explicitly.
2845 */
2846int
James Smart2e0fef82007-06-17 19:56:36 -05002847lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2848 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002849{
James Smart34b02dc2008-08-24 21:49:55 -04002850 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002851 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002852 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002853 struct lpfc_iocbq *cmdiocbq = NULL;
2854 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002855 uint32_t status;
2856 uint32_t portRspPut, portRspMax;
2857 int rc = 1;
2858 lpfc_iocb_type type;
2859 unsigned long iflag;
2860 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002861
James Smart2e0fef82007-06-17 19:56:36 -05002862 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002863 pring->stats.iocb_event++;
2864
dea31012005-04-17 16:05:31 -05002865 /*
2866 * The next available response entry should never exceed the maximum
2867 * entries. If it does, treat it as an adapter hardware error.
2868 */
2869 portRspMax = pring->numRiocb;
2870 portRspPut = le32_to_cpu(pgp->rspPutInx);
2871 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002872 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002873 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002874 return 1;
2875 }
James Smart45ed1192009-10-02 15:17:02 -04002876 if (phba->fcp_ring_in_use) {
2877 spin_unlock_irqrestore(&phba->hbalock, iflag);
2878 return 1;
2879 } else
2880 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002881
2882 rmb();
2883 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002884 /*
2885 * Fetch an entry off the ring and copy it into a local data
2886 * structure. The copy involves a byte-swap since the
2887 * network byte order and pci byte orders are different.
2888 */
James Smarted957682007-06-17 19:56:37 -05002889 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002890 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002891
2892 if (++pring->rspidx >= portRspMax)
2893 pring->rspidx = 0;
2894
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002895 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2896 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002897 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002898 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002899 irsp = &rspiocbq.iocb;
2900
dea31012005-04-17 16:05:31 -05002901 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2902 pring->stats.iocb_rsp++;
2903 rsp_cmpl++;
2904
2905 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002906 /*
2907 * If resource errors reported from HBA, reduce
2908 * queuedepths of the SCSI device.
2909 */
2910 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2911 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2912 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002913 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002914 spin_lock_irqsave(&phba->hbalock, iflag);
2915 }
2916
dea31012005-04-17 16:05:31 -05002917 /* Rsp ring <ringno> error: IOCB */
2918 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002919 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002920 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002921 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002922 irsp->un.ulpWord[0],
2923 irsp->un.ulpWord[1],
2924 irsp->un.ulpWord[2],
2925 irsp->un.ulpWord[3],
2926 irsp->un.ulpWord[4],
2927 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002928 *(uint32_t *)&irsp->un1,
2929 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002930 }
2931
2932 switch (type) {
2933 case LPFC_ABORT_IOCB:
2934 case LPFC_SOL_IOCB:
2935 /*
2936 * Idle exchange closed via ABTS from port. No iocb
2937 * resources need to be recovered.
2938 */
2939 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002940 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002941 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002942 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002943 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002944 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002945 break;
2946 }
2947
James Bottomley604a3e32005-10-29 10:28:33 -05002948 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2949 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002950 if (unlikely(!cmdiocbq))
2951 break;
2952 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2953 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2954 if (cmdiocbq->iocb_cmpl) {
2955 spin_unlock_irqrestore(&phba->hbalock, iflag);
2956 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2957 &rspiocbq);
2958 spin_lock_irqsave(&phba->hbalock, iflag);
2959 }
dea31012005-04-17 16:05:31 -05002960 break;
James Smarta4bc3372006-12-02 13:34:16 -05002961 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002962 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002963 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002964 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002965 break;
dea31012005-04-17 16:05:31 -05002966 default:
2967 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2968 char adaptermsg[LPFC_MAX_ADPTMSG];
2969 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2970 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2971 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002972 dev_warn(&((phba->pcidev)->dev),
2973 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002974 phba->brd_no, adaptermsg);
2975 } else {
2976 /* Unknown IOCB command */
2977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002978 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002979 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002980 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002981 irsp->ulpStatus,
2982 irsp->ulpIoTag,
2983 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002984 }
2985 break;
2986 }
2987
2988 /*
2989 * The response IOCB has been processed. Update the ring
2990 * pointer in SLIM. If the port response put pointer has not
2991 * been updated, sync the pgp->rspPutInx and fetch the new port
2992 * response put pointer.
2993 */
James Smarted957682007-06-17 19:56:37 -05002994 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002995
2996 if (pring->rspidx == portRspPut)
2997 portRspPut = le32_to_cpu(pgp->rspPutInx);
2998 }
2999
3000 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3001 pring->stats.iocb_rsp_full++;
3002 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3003 writel(status, phba->CAregaddr);
3004 readl(phba->CAregaddr);
3005 }
3006 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3007 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3008 pring->stats.iocb_cmd_empty++;
3009
3010 /* Force update of the local copy of cmdGetInx */
3011 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3012 lpfc_sli_resume_iocb(phba, pring);
3013
3014 if ((pring->lpfc_sli_cmd_available))
3015 (pring->lpfc_sli_cmd_available) (phba, pring);
3016
3017 }
3018
James Smart45ed1192009-10-02 15:17:02 -04003019 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003020 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003021 return rc;
3022}
3023
James Smarte59058c2008-08-24 21:49:00 -04003024/**
James Smart3772a992009-05-22 14:50:54 -04003025 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3026 * @phba: Pointer to HBA context object.
3027 * @pring: Pointer to driver SLI ring object.
3028 * @rspiocbp: Pointer to driver response IOCB object.
3029 *
3030 * This function is called from the worker thread when there is a slow-path
3031 * response IOCB to process. This function chains all the response iocbs until
3032 * seeing the iocb with the LE bit set. The function will call
3033 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3034 * completion of a command iocb. The function will call the
3035 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3036 * The function frees the resources or calls the completion handler if this
3037 * iocb is an abort completion. The function returns NULL when the response
3038 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3039 * this function shall chain the iocb on to the iocb_continueq and return the
3040 * response iocb passed in.
3041 **/
3042static struct lpfc_iocbq *
3043lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3044 struct lpfc_iocbq *rspiocbp)
3045{
3046 struct lpfc_iocbq *saveq;
3047 struct lpfc_iocbq *cmdiocbp;
3048 struct lpfc_iocbq *next_iocb;
3049 IOCB_t *irsp = NULL;
3050 uint32_t free_saveq;
3051 uint8_t iocb_cmd_type;
3052 lpfc_iocb_type type;
3053 unsigned long iflag;
3054 int rc;
3055
3056 spin_lock_irqsave(&phba->hbalock, iflag);
3057 /* First add the response iocb to the countinueq list */
3058 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3059 pring->iocb_continueq_cnt++;
3060
3061 /* Now, determine whetehr the list is completed for processing */
3062 irsp = &rspiocbp->iocb;
3063 if (irsp->ulpLe) {
3064 /*
3065 * By default, the driver expects to free all resources
3066 * associated with this iocb completion.
3067 */
3068 free_saveq = 1;
3069 saveq = list_get_first(&pring->iocb_continueq,
3070 struct lpfc_iocbq, list);
3071 irsp = &(saveq->iocb);
3072 list_del_init(&pring->iocb_continueq);
3073 pring->iocb_continueq_cnt = 0;
3074
3075 pring->stats.iocb_rsp++;
3076
3077 /*
3078 * If resource errors reported from HBA, reduce
3079 * queuedepths of the SCSI device.
3080 */
3081 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3082 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3083 spin_unlock_irqrestore(&phba->hbalock, iflag);
3084 phba->lpfc_rampdown_queue_depth(phba);
3085 spin_lock_irqsave(&phba->hbalock, iflag);
3086 }
3087
3088 if (irsp->ulpStatus) {
3089 /* Rsp ring <ringno> error: IOCB */
3090 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3091 "0328 Rsp Ring %d error: "
3092 "IOCB Data: "
3093 "x%x x%x x%x x%x "
3094 "x%x x%x x%x x%x "
3095 "x%x x%x x%x x%x "
3096 "x%x x%x x%x x%x\n",
3097 pring->ringno,
3098 irsp->un.ulpWord[0],
3099 irsp->un.ulpWord[1],
3100 irsp->un.ulpWord[2],
3101 irsp->un.ulpWord[3],
3102 irsp->un.ulpWord[4],
3103 irsp->un.ulpWord[5],
3104 *(((uint32_t *) irsp) + 6),
3105 *(((uint32_t *) irsp) + 7),
3106 *(((uint32_t *) irsp) + 8),
3107 *(((uint32_t *) irsp) + 9),
3108 *(((uint32_t *) irsp) + 10),
3109 *(((uint32_t *) irsp) + 11),
3110 *(((uint32_t *) irsp) + 12),
3111 *(((uint32_t *) irsp) + 13),
3112 *(((uint32_t *) irsp) + 14),
3113 *(((uint32_t *) irsp) + 15));
3114 }
3115
3116 /*
3117 * Fetch the IOCB command type and call the correct completion
3118 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3119 * get freed back to the lpfc_iocb_list by the discovery
3120 * kernel thread.
3121 */
3122 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3123 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3124 switch (type) {
3125 case LPFC_SOL_IOCB:
3126 spin_unlock_irqrestore(&phba->hbalock, iflag);
3127 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3128 spin_lock_irqsave(&phba->hbalock, iflag);
3129 break;
3130
3131 case LPFC_UNSOL_IOCB:
3132 spin_unlock_irqrestore(&phba->hbalock, iflag);
3133 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3134 spin_lock_irqsave(&phba->hbalock, iflag);
3135 if (!rc)
3136 free_saveq = 0;
3137 break;
3138
3139 case LPFC_ABORT_IOCB:
3140 cmdiocbp = NULL;
3141 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3142 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3143 saveq);
3144 if (cmdiocbp) {
3145 /* Call the specified completion routine */
3146 if (cmdiocbp->iocb_cmpl) {
3147 spin_unlock_irqrestore(&phba->hbalock,
3148 iflag);
3149 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3150 saveq);
3151 spin_lock_irqsave(&phba->hbalock,
3152 iflag);
3153 } else
3154 __lpfc_sli_release_iocbq(phba,
3155 cmdiocbp);
3156 }
3157 break;
3158
3159 case LPFC_UNKNOWN_IOCB:
3160 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3161 char adaptermsg[LPFC_MAX_ADPTMSG];
3162 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3163 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3164 MAX_MSG_DATA);
3165 dev_warn(&((phba->pcidev)->dev),
3166 "lpfc%d: %s\n",
3167 phba->brd_no, adaptermsg);
3168 } else {
3169 /* Unknown IOCB command */
3170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3171 "0335 Unknown IOCB "
3172 "command Data: x%x "
3173 "x%x x%x x%x\n",
3174 irsp->ulpCommand,
3175 irsp->ulpStatus,
3176 irsp->ulpIoTag,
3177 irsp->ulpContext);
3178 }
3179 break;
3180 }
3181
3182 if (free_saveq) {
3183 list_for_each_entry_safe(rspiocbp, next_iocb,
3184 &saveq->list, list) {
3185 list_del(&rspiocbp->list);
3186 __lpfc_sli_release_iocbq(phba, rspiocbp);
3187 }
3188 __lpfc_sli_release_iocbq(phba, saveq);
3189 }
3190 rspiocbp = NULL;
3191 }
3192 spin_unlock_irqrestore(&phba->hbalock, iflag);
3193 return rspiocbp;
3194}
3195
3196/**
3197 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003198 * @phba: Pointer to HBA context object.
3199 * @pring: Pointer to driver SLI ring object.
3200 * @mask: Host attention register mask for this ring.
3201 *
James Smart3772a992009-05-22 14:50:54 -04003202 * This routine wraps the actual slow_ring event process routine from the
3203 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003204 **/
James Smart3772a992009-05-22 14:50:54 -04003205void
James Smart2e0fef82007-06-17 19:56:36 -05003206lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3207 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003208{
James Smart3772a992009-05-22 14:50:54 -04003209 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3210}
3211
3212/**
3213 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3214 * @phba: Pointer to HBA context object.
3215 * @pring: Pointer to driver SLI ring object.
3216 * @mask: Host attention register mask for this ring.
3217 *
3218 * This function is called from the worker thread when there is a ring event
3219 * for non-fcp rings. The caller does not hold any lock. The function will
3220 * remove each response iocb in the response ring and calls the handle
3221 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3222 **/
3223static void
3224lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3225 struct lpfc_sli_ring *pring, uint32_t mask)
3226{
James Smart34b02dc2008-08-24 21:49:55 -04003227 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003228 IOCB_t *entry;
3229 IOCB_t *irsp = NULL;
3230 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003231 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003232 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003233 uint32_t status;
dea31012005-04-17 16:05:31 -05003234
James Smart34b02dc2008-08-24 21:49:55 -04003235 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003236 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003237 pring->stats.iocb_event++;
3238
dea31012005-04-17 16:05:31 -05003239 /*
3240 * The next available response entry should never exceed the maximum
3241 * entries. If it does, treat it as an adapter hardware error.
3242 */
3243 portRspMax = pring->numRiocb;
3244 portRspPut = le32_to_cpu(pgp->rspPutInx);
3245 if (portRspPut >= portRspMax) {
3246 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003247 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003248 * rsp ring <portRspMax>
3249 */
James Smarted957682007-06-17 19:56:37 -05003250 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003251 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003252 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003253 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003254
James Smart2e0fef82007-06-17 19:56:36 -05003255 phba->link_state = LPFC_HBA_ERROR;
3256 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003257
3258 phba->work_hs = HS_FFER3;
3259 lpfc_handle_eratt(phba);
3260
James Smart3772a992009-05-22 14:50:54 -04003261 return;
dea31012005-04-17 16:05:31 -05003262 }
3263
3264 rmb();
dea31012005-04-17 16:05:31 -05003265 while (pring->rspidx != portRspPut) {
3266 /*
3267 * Build a completion list and call the appropriate handler.
3268 * The process is to get the next available response iocb, get
3269 * a free iocb from the list, copy the response data into the
3270 * free iocb, insert to the continuation list, and update the
3271 * next response index to slim. This process makes response
3272 * iocb's in the ring available to DMA as fast as possible but
3273 * pays a penalty for a copy operation. Since the iocb is
3274 * only 32 bytes, this penalty is considered small relative to
3275 * the PCI reads for register values and a slim write. When
3276 * the ulpLe field is set, the entire Command has been
3277 * received.
3278 */
James Smarted957682007-06-17 19:56:37 -05003279 entry = lpfc_resp_iocb(phba, pring);
3280
James Smart858c9f62007-06-17 19:56:39 -05003281 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003282 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003283 if (rspiocbp == NULL) {
3284 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003285 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003286 break;
3287 }
3288
James Smarted957682007-06-17 19:56:37 -05003289 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3290 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003291 irsp = &rspiocbp->iocb;
3292
3293 if (++pring->rspidx >= portRspMax)
3294 pring->rspidx = 0;
3295
James Smarta58cbd52007-08-02 11:09:43 -04003296 if (pring->ringno == LPFC_ELS_RING) {
3297 lpfc_debugfs_slow_ring_trc(phba,
3298 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3299 *(((uint32_t *) irsp) + 4),
3300 *(((uint32_t *) irsp) + 6),
3301 *(((uint32_t *) irsp) + 7));
3302 }
3303
James Smarted957682007-06-17 19:56:37 -05003304 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003305
James Smart3772a992009-05-22 14:50:54 -04003306 spin_unlock_irqrestore(&phba->hbalock, iflag);
3307 /* Handle the response IOCB */
3308 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3309 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003310
3311 /*
3312 * If the port response put pointer has not been updated, sync
3313 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3314 * response put pointer.
3315 */
3316 if (pring->rspidx == portRspPut) {
3317 portRspPut = le32_to_cpu(pgp->rspPutInx);
3318 }
3319 } /* while (pring->rspidx != portRspPut) */
3320
James Smart92d7f7b2007-06-17 19:56:38 -05003321 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003322 /* At least one response entry has been freed */
3323 pring->stats.iocb_rsp_full++;
3324 /* SET RxRE_RSP in Chip Att register */
3325 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3326 writel(status, phba->CAregaddr);
3327 readl(phba->CAregaddr); /* flush */
3328 }
3329 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3330 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3331 pring->stats.iocb_cmd_empty++;
3332
3333 /* Force update of the local copy of cmdGetInx */
3334 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3335 lpfc_sli_resume_iocb(phba, pring);
3336
3337 if ((pring->lpfc_sli_cmd_available))
3338 (pring->lpfc_sli_cmd_available) (phba, pring);
3339
3340 }
3341
James Smart2e0fef82007-06-17 19:56:36 -05003342 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003343 return;
dea31012005-04-17 16:05:31 -05003344}
3345
James Smarte59058c2008-08-24 21:49:00 -04003346/**
James Smart4f774512009-05-22 14:52:35 -04003347 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3348 * @phba: Pointer to HBA context object.
3349 * @pring: Pointer to driver SLI ring object.
3350 * @mask: Host attention register mask for this ring.
3351 *
3352 * This function is called from the worker thread when there is a pending
3353 * ELS response iocb on the driver internal slow-path response iocb worker
3354 * queue. The caller does not hold any lock. The function will remove each
3355 * response iocb from the response worker queue and calls the handle
3356 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3357 **/
3358static void
3359lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3360 struct lpfc_sli_ring *pring, uint32_t mask)
3361{
3362 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003363 struct hbq_dmabuf *dmabuf;
3364 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003365 unsigned long iflag;
3366
James Smart45ed1192009-10-02 15:17:02 -04003367 spin_lock_irqsave(&phba->hbalock, iflag);
3368 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3369 spin_unlock_irqrestore(&phba->hbalock, iflag);
3370 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003371 /* Get the response iocb from the head of work queue */
3372 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003373 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003374 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003375 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003376
3377 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3378 case CQE_CODE_COMPL_WQE:
3379 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3380 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003381 /* Translate ELS WCQE to response IOCBQ */
3382 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3383 irspiocbq);
3384 if (irspiocbq)
3385 lpfc_sli_sp_handle_rspiocb(phba, pring,
3386 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003387 break;
3388 case CQE_CODE_RECEIVE:
3389 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3390 cq_event);
3391 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3392 break;
3393 default:
3394 break;
3395 }
James Smart4f774512009-05-22 14:52:35 -04003396 }
3397}
3398
3399/**
James Smart3621a712009-04-06 18:47:14 -04003400 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003401 * @phba: Pointer to HBA context object.
3402 * @pring: Pointer to driver SLI ring object.
3403 *
3404 * This function aborts all iocbs in the given ring and frees all the iocb
3405 * objects in txq. This function issues an abort iocb for all the iocb commands
3406 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3407 * the return of this function. The caller is not required to hold any locks.
3408 **/
James Smart2e0fef82007-06-17 19:56:36 -05003409void
dea31012005-04-17 16:05:31 -05003410lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3411{
James Smart2534ba72007-04-25 09:52:20 -04003412 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003413 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003414
James Smart92d7f7b2007-06-17 19:56:38 -05003415 if (pring->ringno == LPFC_ELS_RING) {
3416 lpfc_fabric_abort_hba(phba);
3417 }
3418
dea31012005-04-17 16:05:31 -05003419 /* Error everything on txq and txcmplq
3420 * First do the txq.
3421 */
James Smart2e0fef82007-06-17 19:56:36 -05003422 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003423 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003424 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003425
3426 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003427 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3428 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3429
James Smart2e0fef82007-06-17 19:56:36 -05003430 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003431
James Smarta257bf92009-04-06 18:48:10 -04003432 /* Cancel all the IOCBs from the completions list */
3433 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3434 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003435}
3436
James Smarte59058c2008-08-24 21:49:00 -04003437/**
James Smart3621a712009-04-06 18:47:14 -04003438 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003439 * @phba: Pointer to HBA context object.
3440 *
3441 * This function flushes all iocbs in the fcp ring and frees all the iocb
3442 * objects in txq and txcmplq. This function will not issue abort iocbs
3443 * for all the iocb commands in txcmplq, they will just be returned with
3444 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3445 * slot has been permanently disabled.
3446 **/
3447void
3448lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3449{
3450 LIST_HEAD(txq);
3451 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003452 struct lpfc_sli *psli = &phba->sli;
3453 struct lpfc_sli_ring *pring;
3454
3455 /* Currently, only one fcp ring */
3456 pring = &psli->ring[psli->fcp_ring];
3457
3458 spin_lock_irq(&phba->hbalock);
3459 /* Retrieve everything on txq */
3460 list_splice_init(&pring->txq, &txq);
3461 pring->txq_cnt = 0;
3462
3463 /* Retrieve everything on the txcmplq */
3464 list_splice_init(&pring->txcmplq, &txcmplq);
3465 pring->txcmplq_cnt = 0;
3466 spin_unlock_irq(&phba->hbalock);
3467
3468 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003469 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3470 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003471
3472 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003473 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3474 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003475}
3476
3477/**
James Smart3772a992009-05-22 14:50:54 -04003478 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003479 * @phba: Pointer to HBA context object.
3480 * @mask: Bit mask to be checked.
3481 *
3482 * This function reads the host status register and compares
3483 * with the provided bit mask to check if HBA completed
3484 * the restart. This function will wait in a loop for the
3485 * HBA to complete restart. If the HBA does not restart within
3486 * 15 iterations, the function will reset the HBA again. The
3487 * function returns 1 when HBA fail to restart otherwise returns
3488 * zero.
3489 **/
James Smart3772a992009-05-22 14:50:54 -04003490static int
3491lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003492{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003493 uint32_t status;
3494 int i = 0;
3495 int retval = 0;
dea31012005-04-17 16:05:31 -05003496
Jamie Wellnitz41415862006-02-28 19:25:27 -05003497 /* Read the HBA Host Status Register */
3498 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003499
Jamie Wellnitz41415862006-02-28 19:25:27 -05003500 /*
3501 * Check status register every 100ms for 5 retries, then every
3502 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3503 * every 2.5 sec for 4.
3504 * Break our of the loop if errors occurred during init.
3505 */
3506 while (((status & mask) != mask) &&
3507 !(status & HS_FFERM) &&
3508 i++ < 20) {
dea31012005-04-17 16:05:31 -05003509
Jamie Wellnitz41415862006-02-28 19:25:27 -05003510 if (i <= 5)
3511 msleep(10);
3512 else if (i <= 10)
3513 msleep(500);
3514 else
3515 msleep(2500);
dea31012005-04-17 16:05:31 -05003516
Jamie Wellnitz41415862006-02-28 19:25:27 -05003517 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003518 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003519 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003520 lpfc_sli_brdrestart(phba);
3521 }
3522 /* Read the HBA Host Status Register */
3523 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003524 }
dea31012005-04-17 16:05:31 -05003525
Jamie Wellnitz41415862006-02-28 19:25:27 -05003526 /* Check to see if any errors occurred during init */
3527 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3529 "2751 Adapter failed to restart, "
3530 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3531 status,
3532 readl(phba->MBslimaddr + 0xa8),
3533 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003534 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535 retval = 1;
3536 }
dea31012005-04-17 16:05:31 -05003537
Jamie Wellnitz41415862006-02-28 19:25:27 -05003538 return retval;
dea31012005-04-17 16:05:31 -05003539}
3540
James Smartda0436e2009-05-22 14:51:39 -04003541/**
3542 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3543 * @phba: Pointer to HBA context object.
3544 * @mask: Bit mask to be checked.
3545 *
3546 * This function checks the host status register to check if HBA is
3547 * ready. This function will wait in a loop for the HBA to be ready
3548 * If the HBA is not ready , the function will will reset the HBA PCI
3549 * function again. The function returns 1 when HBA fail to be ready
3550 * otherwise returns zero.
3551 **/
3552static int
3553lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3554{
3555 uint32_t status;
3556 int retval = 0;
3557
3558 /* Read the HBA Host Status Register */
3559 status = lpfc_sli4_post_status_check(phba);
3560
3561 if (status) {
3562 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3563 lpfc_sli_brdrestart(phba);
3564 status = lpfc_sli4_post_status_check(phba);
3565 }
3566
3567 /* Check to see if any errors occurred during init */
3568 if (status) {
3569 phba->link_state = LPFC_HBA_ERROR;
3570 retval = 1;
3571 } else
3572 phba->sli4_hba.intr_enable = 0;
3573
3574 return retval;
3575}
3576
3577/**
3578 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3579 * @phba: Pointer to HBA context object.
3580 * @mask: Bit mask to be checked.
3581 *
3582 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3583 * from the API jump table function pointer from the lpfc_hba struct.
3584 **/
3585int
3586lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3587{
3588 return phba->lpfc_sli_brdready(phba, mask);
3589}
3590
James Smart92908312006-03-07 15:04:13 -05003591#define BARRIER_TEST_PATTERN (0xdeadbeef)
3592
James Smarte59058c2008-08-24 21:49:00 -04003593/**
James Smart3621a712009-04-06 18:47:14 -04003594 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003595 * @phba: Pointer to HBA context object.
3596 *
3597 * This function is called before resetting an HBA. This
3598 * function requests HBA to quiesce DMAs before a reset.
3599 **/
James Smart2e0fef82007-06-17 19:56:36 -05003600void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003601{
James Smart65a29c12006-07-06 15:50:50 -04003602 uint32_t __iomem *resp_buf;
3603 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003604 volatile uint32_t mbox;
3605 uint32_t hc_copy;
3606 int i;
3607 uint8_t hdrtype;
3608
3609 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3610 if (hdrtype != 0x80 ||
3611 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3612 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3613 return;
3614
3615 /*
3616 * Tell the other part of the chip to suspend temporarily all
3617 * its DMA activity.
3618 */
James Smart65a29c12006-07-06 15:50:50 -04003619 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003620
3621 /* Disable the error attention */
3622 hc_copy = readl(phba->HCregaddr);
3623 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3624 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003625 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003626
3627 if (readl(phba->HAregaddr) & HA_ERATT) {
3628 /* Clear Chip error bit */
3629 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003630 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003631 }
3632
3633 mbox = 0;
3634 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3635 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3636
3637 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003638 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003639 writel(mbox, mbox_buf);
3640
3641 for (i = 0;
3642 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3643 mdelay(1);
3644
3645 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003646 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003647 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003648 goto restore_hc;
3649 else
3650 goto clear_errat;
3651 }
3652
3653 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3654 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3655 mdelay(1);
3656
3657clear_errat:
3658
3659 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3660 mdelay(1);
3661
3662 if (readl(phba->HAregaddr) & HA_ERATT) {
3663 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003664 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003665 }
3666
3667restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003668 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003669 writel(hc_copy, phba->HCregaddr);
3670 readl(phba->HCregaddr); /* flush */
3671}
3672
James Smarte59058c2008-08-24 21:49:00 -04003673/**
James Smart3621a712009-04-06 18:47:14 -04003674 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003675 * @phba: Pointer to HBA context object.
3676 *
3677 * This function issues a kill_board mailbox command and waits for
3678 * the error attention interrupt. This function is called for stopping
3679 * the firmware processing. The caller is not required to hold any
3680 * locks. This function calls lpfc_hba_down_post function to free
3681 * any pending commands after the kill. The function will return 1 when it
3682 * fails to kill the board else will return 0.
3683 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003684int
James Smart2e0fef82007-06-17 19:56:36 -05003685lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003686{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003687 struct lpfc_sli *psli;
3688 LPFC_MBOXQ_t *pmb;
3689 uint32_t status;
3690 uint32_t ha_copy;
3691 int retval;
3692 int i = 0;
3693
3694 psli = &phba->sli;
3695
3696 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003697 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003698 "0329 Kill HBA Data: x%x x%x\n",
3699 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003700
James Smart98c9ea52007-10-27 13:37:33 -04003701 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3702 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003703 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003704
3705 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003706 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003707 status = readl(phba->HCregaddr);
3708 status &= ~HC_ERINT_ENA;
3709 writel(status, phba->HCregaddr);
3710 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003711 phba->link_flag |= LS_IGNORE_ERATT;
3712 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713
3714 lpfc_kill_board(phba, pmb);
3715 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3716 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3717
3718 if (retval != MBX_SUCCESS) {
3719 if (retval != MBX_BUSY)
3720 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3722 "2752 KILL_BOARD command failed retval %d\n",
3723 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003724 spin_lock_irq(&phba->hbalock);
3725 phba->link_flag &= ~LS_IGNORE_ERATT;
3726 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003727 return 1;
3728 }
3729
James Smartf4b4c682009-05-22 14:53:12 -04003730 spin_lock_irq(&phba->hbalock);
3731 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3732 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003733
Jamie Wellnitz41415862006-02-28 19:25:27 -05003734 mempool_free(pmb, phba->mbox_mem_pool);
3735
3736 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3737 * attention every 100ms for 3 seconds. If we don't get ERATT after
3738 * 3 seconds we still set HBA_ERROR state because the status of the
3739 * board is now undefined.
3740 */
3741 ha_copy = readl(phba->HAregaddr);
3742
3743 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3744 mdelay(100);
3745 ha_copy = readl(phba->HAregaddr);
3746 }
3747
3748 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003749 if (ha_copy & HA_ERATT) {
3750 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003751 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003752 }
James Smart2e0fef82007-06-17 19:56:36 -05003753 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003754 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003755 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003756 phba->link_flag &= ~LS_IGNORE_ERATT;
3757 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003758
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003760 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003761
James Smart2e0fef82007-06-17 19:56:36 -05003762 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003763}
3764
James Smarte59058c2008-08-24 21:49:00 -04003765/**
James Smart3772a992009-05-22 14:50:54 -04003766 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003767 * @phba: Pointer to HBA context object.
3768 *
3769 * This function resets the HBA by writing HC_INITFF to the control
3770 * register. After the HBA resets, this function resets all the iocb ring
3771 * indices. This function disables PCI layer parity checking during
3772 * the reset.
3773 * This function returns 0 always.
3774 * The caller is not required to hold any locks.
3775 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003776int
James Smart2e0fef82007-06-17 19:56:36 -05003777lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003778{
3779 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003780 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003781 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003782 int i;
dea31012005-04-17 16:05:31 -05003783
Jamie Wellnitz41415862006-02-28 19:25:27 -05003784 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003785
Jamie Wellnitz41415862006-02-28 19:25:27 -05003786 /* Reset HBA */
3787 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003788 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003789 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003790
3791 /* perform board reset */
3792 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003793 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003794 phba->pport->fc_myDID = 0;
3795 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003796
Jamie Wellnitz41415862006-02-28 19:25:27 -05003797 /* Turn off parity checking and serr during the physical reset */
3798 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3799 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3800 (cfg_value &
3801 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3802
James Smart3772a992009-05-22 14:50:54 -04003803 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3804
Jamie Wellnitz41415862006-02-28 19:25:27 -05003805 /* Now toggle INITFF bit in the Host Control Register */
3806 writel(HC_INITFF, phba->HCregaddr);
3807 mdelay(1);
3808 readl(phba->HCregaddr); /* flush */
3809 writel(0, phba->HCregaddr);
3810 readl(phba->HCregaddr); /* flush */
3811
3812 /* Restore PCI cmd register */
3813 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003814
3815 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003816 for (i = 0; i < psli->num_rings; i++) {
3817 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003818 pring->flag = 0;
3819 pring->rspidx = 0;
3820 pring->next_cmdidx = 0;
3821 pring->local_getidx = 0;
3822 pring->cmdidx = 0;
3823 pring->missbufcnt = 0;
3824 }
dea31012005-04-17 16:05:31 -05003825
James Smart2e0fef82007-06-17 19:56:36 -05003826 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827 return 0;
3828}
3829
James Smarte59058c2008-08-24 21:49:00 -04003830/**
James Smartda0436e2009-05-22 14:51:39 -04003831 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3832 * @phba: Pointer to HBA context object.
3833 *
3834 * This function resets a SLI4 HBA. This function disables PCI layer parity
3835 * checking during resets the device. The caller is not required to hold
3836 * any locks.
3837 *
3838 * This function returns 0 always.
3839 **/
3840int
3841lpfc_sli4_brdreset(struct lpfc_hba *phba)
3842{
3843 struct lpfc_sli *psli = &phba->sli;
3844 uint16_t cfg_value;
3845 uint8_t qindx;
3846
3847 /* Reset HBA */
3848 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3849 "0295 Reset HBA Data: x%x x%x\n",
3850 phba->pport->port_state, psli->sli_flag);
3851
3852 /* perform board reset */
3853 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003854 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003855 phba->pport->fc_myDID = 0;
3856 phba->pport->fc_prevDID = 0;
3857
3858 /* Turn off parity checking and serr during the physical reset */
3859 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3860 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3861 (cfg_value &
3862 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3863
3864 spin_lock_irq(&phba->hbalock);
3865 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3866 phba->fcf.fcf_flag = 0;
3867 /* Clean up the child queue list for the CQs */
3868 list_del_init(&phba->sli4_hba.mbx_wq->list);
3869 list_del_init(&phba->sli4_hba.els_wq->list);
3870 list_del_init(&phba->sli4_hba.hdr_rq->list);
3871 list_del_init(&phba->sli4_hba.dat_rq->list);
3872 list_del_init(&phba->sli4_hba.mbx_cq->list);
3873 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003874 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3875 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3876 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3877 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3878 spin_unlock_irq(&phba->hbalock);
3879
3880 /* Now physically reset the device */
3881 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3882 "0389 Performing PCI function reset!\n");
3883 /* Perform FCoE PCI function reset */
3884 lpfc_pci_function_reset(phba);
3885
3886 return 0;
3887}
3888
3889/**
3890 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003891 * @phba: Pointer to HBA context object.
3892 *
3893 * This function is called in the SLI initialization code path to
3894 * restart the HBA. The caller is not required to hold any lock.
3895 * This function writes MBX_RESTART mailbox command to the SLIM and
3896 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3897 * function to free any pending commands. The function enables
3898 * POST only during the first initialization. The function returns zero.
3899 * The function does not guarantee completion of MBX_RESTART mailbox
3900 * command before the return of this function.
3901 **/
James Smartda0436e2009-05-22 14:51:39 -04003902static int
3903lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003904{
3905 MAILBOX_t *mb;
3906 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003907 volatile uint32_t word0;
3908 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003909 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910
James Smart2e0fef82007-06-17 19:56:36 -05003911 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003912
James Smart0d878412009-10-02 15:16:56 -04003913 /* Take PCIe device Advanced Error Reporting (AER) state */
3914 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3915
Jamie Wellnitz41415862006-02-28 19:25:27 -05003916 psli = &phba->sli;
3917
3918 /* Restart HBA */
3919 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003920 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003921 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003922
3923 word0 = 0;
3924 mb = (MAILBOX_t *) &word0;
3925 mb->mbxCommand = MBX_RESTART;
3926 mb->mbxHc = 1;
3927
James Smart92908312006-03-07 15:04:13 -05003928 lpfc_reset_barrier(phba);
3929
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930 to_slim = phba->MBslimaddr;
3931 writel(*(uint32_t *) mb, to_slim);
3932 readl(to_slim); /* flush */
3933
3934 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003935 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003937 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003938 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003939 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003940 writel(*(uint32_t *) mb, to_slim);
3941 readl(to_slim); /* flush */
3942
3943 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003944 phba->pport->stopped = 0;
3945 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003946 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003947 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948
James Smart64ba8812006-08-02 15:24:34 -04003949 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3950 psli->stats_start = get_seconds();
3951
James Smarteaf15d52008-12-04 22:39:29 -05003952 /* Give the INITFF and Post time to settle. */
3953 mdelay(100);
dea31012005-04-17 16:05:31 -05003954
James Smart0d878412009-10-02 15:16:56 -04003955 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3956 if (hba_aer_enabled)
3957 pci_disable_pcie_error_reporting(phba->pcidev);
3958
Jamie Wellnitz41415862006-02-28 19:25:27 -05003959 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003960
3961 return 0;
3962}
3963
James Smarte59058c2008-08-24 21:49:00 -04003964/**
James Smartda0436e2009-05-22 14:51:39 -04003965 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3966 * @phba: Pointer to HBA context object.
3967 *
3968 * This function is called in the SLI initialization code path to restart
3969 * a SLI4 HBA. The caller is not required to hold any lock.
3970 * At the end of the function, it calls lpfc_hba_down_post function to
3971 * free any pending commands.
3972 **/
3973static int
3974lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3975{
3976 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003977 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003978
3979 /* Restart HBA */
3980 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3981 "0296 Restart HBA Data: x%x x%x\n",
3982 phba->pport->port_state, psli->sli_flag);
3983
James Smart75baf692010-06-08 18:31:21 -04003984 /* Take PCIe device Advanced Error Reporting (AER) state */
3985 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3986
James Smartda0436e2009-05-22 14:51:39 -04003987 lpfc_sli4_brdreset(phba);
3988
3989 spin_lock_irq(&phba->hbalock);
3990 phba->pport->stopped = 0;
3991 phba->link_state = LPFC_INIT_START;
3992 phba->hba_flag = 0;
3993 spin_unlock_irq(&phba->hbalock);
3994
3995 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3996 psli->stats_start = get_seconds();
3997
James Smart75baf692010-06-08 18:31:21 -04003998 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3999 if (hba_aer_enabled)
4000 pci_disable_pcie_error_reporting(phba->pcidev);
4001
James Smartda0436e2009-05-22 14:51:39 -04004002 lpfc_hba_down_post(phba);
4003
4004 return 0;
4005}
4006
4007/**
4008 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4009 * @phba: Pointer to HBA context object.
4010 *
4011 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4012 * API jump table function pointer from the lpfc_hba struct.
4013**/
4014int
4015lpfc_sli_brdrestart(struct lpfc_hba *phba)
4016{
4017 return phba->lpfc_sli_brdrestart(phba);
4018}
4019
4020/**
James Smart3621a712009-04-06 18:47:14 -04004021 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004022 * @phba: Pointer to HBA context object.
4023 *
4024 * This function is called after a HBA restart to wait for successful
4025 * restart of the HBA. Successful restart of the HBA is indicated by
4026 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4027 * iteration, the function will restart the HBA again. The function returns
4028 * zero if HBA successfully restarted else returns negative error code.
4029 **/
dea31012005-04-17 16:05:31 -05004030static int
4031lpfc_sli_chipset_init(struct lpfc_hba *phba)
4032{
4033 uint32_t status, i = 0;
4034
4035 /* Read the HBA Host Status Register */
4036 status = readl(phba->HSregaddr);
4037
4038 /* Check status register to see what current state is */
4039 i = 0;
4040 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4041
James Smartdcf2a4e2010-09-29 11:18:53 -04004042 /* Check every 10ms for 10 retries, then every 100ms for 90
4043 * retries, then every 1 sec for 50 retires for a total of
4044 * ~60 seconds before reset the board again and check every
4045 * 1 sec for 50 retries. The up to 60 seconds before the
4046 * board ready is required by the Falcon FIPS zeroization
4047 * complete, and any reset the board in between shall cause
4048 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004049 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004050 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004051 /* Adapter failed to init, timeout, status reg
4052 <status> */
James Smarted957682007-06-17 19:56:37 -05004053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004054 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004055 "timeout, status reg x%x, "
4056 "FW Data: A8 x%x AC x%x\n", status,
4057 readl(phba->MBslimaddr + 0xa8),
4058 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004059 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004060 return -ETIMEDOUT;
4061 }
4062
4063 /* Check to see if any errors occurred during init */
4064 if (status & HS_FFERM) {
4065 /* ERROR: During chipset initialization */
4066 /* Adapter failed to init, chipset, status reg
4067 <status> */
James Smarted957682007-06-17 19:56:37 -05004068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004069 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004070 "chipset, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004074 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004075 return -EIO;
4076 }
4077
James Smartdcf2a4e2010-09-29 11:18:53 -04004078 if (i <= 10)
dea31012005-04-17 16:05:31 -05004079 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004080 else if (i <= 100)
4081 msleep(100);
4082 else
4083 msleep(1000);
dea31012005-04-17 16:05:31 -05004084
James Smartdcf2a4e2010-09-29 11:18:53 -04004085 if (i == 150) {
4086 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004087 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004088 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004089 }
4090 /* Read the HBA Host Status Register */
4091 status = readl(phba->HSregaddr);
4092 }
4093
4094 /* Check to see if any errors occurred during init */
4095 if (status & HS_FFERM) {
4096 /* ERROR: During chipset initialization */
4097 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004099 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004100 "status reg x%x, "
4101 "FW Data: A8 x%x AC x%x\n", status,
4102 readl(phba->MBslimaddr + 0xa8),
4103 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004104 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004105 return -EIO;
4106 }
4107
4108 /* Clear all interrupt enable conditions */
4109 writel(0, phba->HCregaddr);
4110 readl(phba->HCregaddr); /* flush */
4111
4112 /* setup host attn register */
4113 writel(0xffffffff, phba->HAregaddr);
4114 readl(phba->HAregaddr); /* flush */
4115 return 0;
4116}
4117
James Smarte59058c2008-08-24 21:49:00 -04004118/**
James Smart3621a712009-04-06 18:47:14 -04004119 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004120 *
4121 * This function calculates and returns the number of HBQs required to be
4122 * configured.
4123 **/
James Smart78b2d852007-08-02 11:10:21 -04004124int
James Smarted957682007-06-17 19:56:37 -05004125lpfc_sli_hbq_count(void)
4126{
James Smart92d7f7b2007-06-17 19:56:38 -05004127 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004128}
4129
James Smarte59058c2008-08-24 21:49:00 -04004130/**
James Smart3621a712009-04-06 18:47:14 -04004131 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004132 *
4133 * This function adds the number of hbq entries in every HBQ to get
4134 * the total number of hbq entries required for the HBA and returns
4135 * the total count.
4136 **/
James Smarted957682007-06-17 19:56:37 -05004137static int
4138lpfc_sli_hbq_entry_count(void)
4139{
4140 int hbq_count = lpfc_sli_hbq_count();
4141 int count = 0;
4142 int i;
4143
4144 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004145 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004146 return count;
4147}
4148
James Smarte59058c2008-08-24 21:49:00 -04004149/**
James Smart3621a712009-04-06 18:47:14 -04004150 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004151 *
4152 * This function calculates amount of memory required for all hbq entries
4153 * to be configured and returns the total memory required.
4154 **/
dea31012005-04-17 16:05:31 -05004155int
James Smarted957682007-06-17 19:56:37 -05004156lpfc_sli_hbq_size(void)
4157{
4158 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4159}
4160
James Smarte59058c2008-08-24 21:49:00 -04004161/**
James Smart3621a712009-04-06 18:47:14 -04004162 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004163 * @phba: Pointer to HBA context object.
4164 *
4165 * This function is called during the SLI initialization to configure
4166 * all the HBQs and post buffers to the HBQ. The caller is not
4167 * required to hold any locks. This function will return zero if successful
4168 * else it will return negative error code.
4169 **/
James Smarted957682007-06-17 19:56:37 -05004170static int
4171lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4172{
4173 int hbq_count = lpfc_sli_hbq_count();
4174 LPFC_MBOXQ_t *pmb;
4175 MAILBOX_t *pmbox;
4176 uint32_t hbqno;
4177 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004178
James Smart92d7f7b2007-06-17 19:56:38 -05004179 /* Get a Mailbox buffer to setup mailbox
4180 * commands for HBA initialization
4181 */
James Smarted957682007-06-17 19:56:37 -05004182 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4183
4184 if (!pmb)
4185 return -ENOMEM;
4186
James Smart04c68492009-05-22 14:52:52 -04004187 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004188
4189 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4190 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004191 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004192
4193 hbq_entry_index = 0;
4194 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4195 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4196 phba->hbqs[hbqno].hbqPutIdx = 0;
4197 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4198 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004199 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004200 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4201 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004202 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4203
4204 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4205 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4206 mbxStatus <status>, ring <num> */
4207
4208 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004209 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004210 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004211 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004212 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004213 pmbox->mbxStatus, hbqno);
4214
4215 phba->link_state = LPFC_HBA_ERROR;
4216 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004217 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004218 }
4219 }
4220 phba->hbq_count = hbq_count;
4221
James Smarted957682007-06-17 19:56:37 -05004222 mempool_free(pmb, phba->mbox_mem_pool);
4223
James Smart92d7f7b2007-06-17 19:56:38 -05004224 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004225 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4226 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004227 return 0;
4228}
4229
James Smarte59058c2008-08-24 21:49:00 -04004230/**
James Smart4f774512009-05-22 14:52:35 -04004231 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4232 * @phba: Pointer to HBA context object.
4233 *
4234 * This function is called during the SLI initialization to configure
4235 * all the HBQs and post buffers to the HBQ. The caller is not
4236 * required to hold any locks. This function will return zero if successful
4237 * else it will return negative error code.
4238 **/
4239static int
4240lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4241{
4242 phba->hbq_in_use = 1;
4243 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4244 phba->hbq_count = 1;
4245 /* Initially populate or replenish the HBQs */
4246 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4247 return 0;
4248}
4249
4250/**
James Smart3621a712009-04-06 18:47:14 -04004251 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004252 * @phba: Pointer to HBA context object.
4253 * @sli_mode: sli mode - 2/3
4254 *
4255 * This function is called by the sli intialization code path
4256 * to issue config_port mailbox command. This function restarts the
4257 * HBA firmware and issues a config_port mailbox command to configure
4258 * the SLI interface in the sli mode specified by sli_mode
4259 * variable. The caller is not required to hold any locks.
4260 * The function returns 0 if successful, else returns negative error
4261 * code.
4262 **/
James Smart93996272008-08-24 21:50:30 -04004263int
4264lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004265{
4266 LPFC_MBOXQ_t *pmb;
4267 uint32_t resetcount = 0, rc = 0, done = 0;
4268
4269 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4270 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004271 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004272 return -ENOMEM;
4273 }
4274
James Smarted957682007-06-17 19:56:37 -05004275 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004276 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004277 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004278 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004279 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004280 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004281 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004282 rc = lpfc_sli_chipset_init(phba);
4283 if (rc)
4284 break;
4285
James Smart2e0fef82007-06-17 19:56:36 -05004286 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004287 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004288 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004289 resetcount++;
4290
James Smarted957682007-06-17 19:56:37 -05004291 /* Call pre CONFIG_PORT mailbox command initialization. A
4292 * value of 0 means the call was successful. Any other
4293 * nonzero value is a failure, but if ERESTART is returned,
4294 * the driver may reset the HBA and try again.
4295 */
dea31012005-04-17 16:05:31 -05004296 rc = lpfc_config_port_prep(phba);
4297 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004298 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004299 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004300 } else if (rc)
dea31012005-04-17 16:05:31 -05004301 break;
James Smart2e0fef82007-06-17 19:56:36 -05004302 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004303 lpfc_config_port(phba, pmb);
4304 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004305 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4306 LPFC_SLI3_HBQ_ENABLED |
4307 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004308 LPFC_SLI3_BG_ENABLED |
4309 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004310 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004311 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004312 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004313 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004314 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004315 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004316 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004317 spin_unlock_irq(&phba->hbalock);
4318 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004319 } else {
4320 /* Allow asynchronous mailbox command to go through */
4321 spin_lock_irq(&phba->hbalock);
4322 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4323 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004324 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004325 }
dea31012005-04-17 16:05:31 -05004326 }
James Smarted957682007-06-17 19:56:37 -05004327 if (!done) {
4328 rc = -EINVAL;
4329 goto do_prep_failed;
4330 }
James Smart04c68492009-05-22 14:52:52 -04004331 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4332 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004333 rc = -ENXIO;
4334 goto do_prep_failed;
4335 }
James Smart04c68492009-05-22 14:52:52 -04004336 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004337 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004338 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4339 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4340 phba->max_vpi : phba->max_vports;
4341
James Smart34b02dc2008-08-24 21:49:55 -04004342 } else
4343 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004344 phba->fips_level = 0;
4345 phba->fips_spec_rev = 0;
4346 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004347 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004348 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4349 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4351 "2850 Security Crypto Active. FIPS x%d "
4352 "(Spec Rev: x%d)",
4353 phba->fips_level, phba->fips_spec_rev);
4354 }
4355 if (pmb->u.mb.un.varCfgPort.sec_err) {
4356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4357 "2856 Config Port Security Crypto "
4358 "Error: x%x ",
4359 pmb->u.mb.un.varCfgPort.sec_err);
4360 }
James Smart04c68492009-05-22 14:52:52 -04004361 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004362 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004363 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004364 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004365
4366 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4367 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004368
4369 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004370 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004371 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4372 else
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "0443 Adapter did not grant "
4375 "BlockGuard\n");
4376 }
James Smart34b02dc2008-08-24 21:49:55 -04004377 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004378 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004379 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004380 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004381 }
James Smart92d7f7b2007-06-17 19:56:38 -05004382do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004383 mempool_free(pmb, phba->mbox_mem_pool);
4384 return rc;
4385}
4386
James Smarte59058c2008-08-24 21:49:00 -04004387
4388/**
James Smart3621a712009-04-06 18:47:14 -04004389 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004390 * @phba: Pointer to HBA context object.
4391 *
4392 * This function is the main SLI intialization function. This function
4393 * is called by the HBA intialization code, HBA reset code and HBA
4394 * error attention handler code. Caller is not required to hold any
4395 * locks. This function issues config_port mailbox command to configure
4396 * the SLI, setup iocb rings and HBQ rings. In the end the function
4397 * calls the config_port_post function to issue init_link mailbox
4398 * command and to start the discovery. The function will return zero
4399 * if successful, else it will return negative error code.
4400 **/
James Smarted957682007-06-17 19:56:37 -05004401int
4402lpfc_sli_hba_setup(struct lpfc_hba *phba)
4403{
4404 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004405 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004406
4407 switch (lpfc_sli_mode) {
4408 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004409 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004411 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004412 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004413 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004414 break;
4415 }
James Smarted957682007-06-17 19:56:37 -05004416 mode = 2;
4417 break;
4418 case 0:
4419 case 3:
4420 break;
4421 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004423 "1819 Unrecognized lpfc_sli_mode "
4424 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004425
4426 break;
4427 }
4428
James Smart93996272008-08-24 21:50:30 -04004429 rc = lpfc_sli_config_port(phba, mode);
4430
James Smarted957682007-06-17 19:56:37 -05004431 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004433 "1820 Unable to select SLI-3. "
4434 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004435 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004436 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004437 if (rc)
dea31012005-04-17 16:05:31 -05004438 goto lpfc_sli_hba_setup_error;
4439
James Smart0d878412009-10-02 15:16:56 -04004440 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4441 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4442 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4443 if (!rc) {
4444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4445 "2709 This device supports "
4446 "Advanced Error Reporting (AER)\n");
4447 spin_lock_irq(&phba->hbalock);
4448 phba->hba_flag |= HBA_AER_ENABLED;
4449 spin_unlock_irq(&phba->hbalock);
4450 } else {
4451 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4452 "2708 This device does not support "
4453 "Advanced Error Reporting (AER)\n");
4454 phba->cfg_aer_support = 0;
4455 }
4456 }
4457
James Smarted957682007-06-17 19:56:37 -05004458 if (phba->sli_rev == 3) {
4459 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4460 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004461 } else {
4462 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4463 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004464 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004465 }
4466
4467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004468 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4469 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004470 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004471
4472 if (rc)
4473 goto lpfc_sli_hba_setup_error;
4474
James Smart93996272008-08-24 21:50:30 -04004475 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004476 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4477 rc = lpfc_sli_hbq_setup(phba);
4478 if (rc)
4479 goto lpfc_sli_hba_setup_error;
4480 }
James Smart04c68492009-05-22 14:52:52 -04004481 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004482 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004483 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004484
4485 rc = lpfc_config_port_post(phba);
4486 if (rc)
4487 goto lpfc_sli_hba_setup_error;
4488
James Smarted957682007-06-17 19:56:37 -05004489 return rc;
4490
James Smart92d7f7b2007-06-17 19:56:38 -05004491lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004492 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004494 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004495 return rc;
4496}
4497
James Smartda0436e2009-05-22 14:51:39 -04004498/**
4499 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4500 * @phba: Pointer to HBA context object.
4501 * @mboxq: mailbox pointer.
4502 * This function issue a dump mailbox command to read config region
4503 * 23 and parse the records in the region and populate driver
4504 * data structure.
4505 **/
4506static int
4507lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4508 LPFC_MBOXQ_t *mboxq)
4509{
4510 struct lpfc_dmabuf *mp;
4511 struct lpfc_mqe *mqe;
4512 uint32_t data_length;
4513 int rc;
4514
4515 /* Program the default value of vlan_id and fc_map */
4516 phba->valid_vlan = 0;
4517 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4518 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4519 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4520
4521 mqe = &mboxq->u.mqe;
4522 if (lpfc_dump_fcoe_param(phba, mboxq))
4523 return -ENOMEM;
4524
4525 mp = (struct lpfc_dmabuf *) mboxq->context1;
4526 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4527
4528 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4529 "(%d):2571 Mailbox cmd x%x Status x%x "
4530 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4531 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4532 "CQ: x%x x%x x%x x%x\n",
4533 mboxq->vport ? mboxq->vport->vpi : 0,
4534 bf_get(lpfc_mqe_command, mqe),
4535 bf_get(lpfc_mqe_status, mqe),
4536 mqe->un.mb_words[0], mqe->un.mb_words[1],
4537 mqe->un.mb_words[2], mqe->un.mb_words[3],
4538 mqe->un.mb_words[4], mqe->un.mb_words[5],
4539 mqe->un.mb_words[6], mqe->un.mb_words[7],
4540 mqe->un.mb_words[8], mqe->un.mb_words[9],
4541 mqe->un.mb_words[10], mqe->un.mb_words[11],
4542 mqe->un.mb_words[12], mqe->un.mb_words[13],
4543 mqe->un.mb_words[14], mqe->un.mb_words[15],
4544 mqe->un.mb_words[16], mqe->un.mb_words[50],
4545 mboxq->mcqe.word0,
4546 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4547 mboxq->mcqe.trailer);
4548
4549 if (rc) {
4550 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4551 kfree(mp);
4552 return -EIO;
4553 }
4554 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004555 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004556 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4557 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004558 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004559 }
James Smartda0436e2009-05-22 14:51:39 -04004560
4561 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4562 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4563 kfree(mp);
4564 return 0;
4565}
4566
4567/**
4568 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4569 * @phba: pointer to lpfc hba data structure.
4570 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4571 * @vpd: pointer to the memory to hold resulting port vpd data.
4572 * @vpd_size: On input, the number of bytes allocated to @vpd.
4573 * On output, the number of data bytes in @vpd.
4574 *
4575 * This routine executes a READ_REV SLI4 mailbox command. In
4576 * addition, this routine gets the port vpd data.
4577 *
4578 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004579 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004580 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004581 **/
4582static int
4583lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4584 uint8_t *vpd, uint32_t *vpd_size)
4585{
4586 int rc = 0;
4587 uint32_t dma_size;
4588 struct lpfc_dmabuf *dmabuf;
4589 struct lpfc_mqe *mqe;
4590
4591 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4592 if (!dmabuf)
4593 return -ENOMEM;
4594
4595 /*
4596 * Get a DMA buffer for the vpd data resulting from the READ_REV
4597 * mailbox command.
4598 */
4599 dma_size = *vpd_size;
4600 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4601 dma_size,
4602 &dmabuf->phys,
4603 GFP_KERNEL);
4604 if (!dmabuf->virt) {
4605 kfree(dmabuf);
4606 return -ENOMEM;
4607 }
4608 memset(dmabuf->virt, 0, dma_size);
4609
4610 /*
4611 * The SLI4 implementation of READ_REV conflicts at word1,
4612 * bits 31:16 and SLI4 adds vpd functionality not present
4613 * in SLI3. This code corrects the conflicts.
4614 */
4615 lpfc_read_rev(phba, mboxq);
4616 mqe = &mboxq->u.mqe;
4617 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4618 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4619 mqe->un.read_rev.word1 &= 0x0000FFFF;
4620 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4621 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4622
4623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4624 if (rc) {
4625 dma_free_coherent(&phba->pcidev->dev, dma_size,
4626 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004627 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004628 return -EIO;
4629 }
4630
James Smartda0436e2009-05-22 14:51:39 -04004631 /*
4632 * The available vpd length cannot be bigger than the
4633 * DMA buffer passed to the port. Catch the less than
4634 * case and update the caller's size.
4635 */
4636 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4637 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4638
James Smartd7c47992010-06-08 18:31:54 -04004639 memcpy(vpd, dmabuf->virt, *vpd_size);
4640
James Smartda0436e2009-05-22 14:51:39 -04004641 dma_free_coherent(&phba->pcidev->dev, dma_size,
4642 dmabuf->virt, dmabuf->phys);
4643 kfree(dmabuf);
4644 return 0;
4645}
4646
4647/**
4648 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4649 * @phba: pointer to lpfc hba data structure.
4650 *
4651 * This routine is called to explicitly arm the SLI4 device's completion and
4652 * event queues
4653 **/
4654static void
4655lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4656{
4657 uint8_t fcp_eqidx;
4658
4659 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4660 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004661 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4662 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4663 LPFC_QUEUE_REARM);
4664 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4665 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4666 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4667 LPFC_QUEUE_REARM);
4668}
4669
4670/**
4671 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4672 * @phba: Pointer to HBA context object.
4673 *
4674 * This function is the main SLI4 device intialization PCI function. This
4675 * function is called by the HBA intialization code, HBA reset code and
4676 * HBA error attention handler code. Caller is not required to hold any
4677 * locks.
4678 **/
4679int
4680lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4681{
4682 int rc;
4683 LPFC_MBOXQ_t *mboxq;
4684 struct lpfc_mqe *mqe;
4685 uint8_t *vpd;
4686 uint32_t vpd_size;
4687 uint32_t ftr_rsp = 0;
4688 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4689 struct lpfc_vport *vport = phba->pport;
4690 struct lpfc_dmabuf *mp;
4691
4692 /* Perform a PCI function reset to start from clean */
4693 rc = lpfc_pci_function_reset(phba);
4694 if (unlikely(rc))
4695 return -ENODEV;
4696
4697 /* Check the HBA Host Status Register for readyness */
4698 rc = lpfc_sli4_post_status_check(phba);
4699 if (unlikely(rc))
4700 return -ENODEV;
4701 else {
4702 spin_lock_irq(&phba->hbalock);
4703 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4704 spin_unlock_irq(&phba->hbalock);
4705 }
4706
4707 /*
4708 * Allocate a single mailbox container for initializing the
4709 * port.
4710 */
4711 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4712 if (!mboxq)
4713 return -ENOMEM;
4714
4715 /*
4716 * Continue initialization with default values even if driver failed
4717 * to read FCoE param config regions
4718 */
4719 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4720 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004721 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004722
4723 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004724 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004725 vpd = kzalloc(vpd_size, GFP_KERNEL);
4726 if (!vpd) {
4727 rc = -ENOMEM;
4728 goto out_free_mbox;
4729 }
4730
4731 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05004732 if (unlikely(rc)) {
4733 kfree(vpd);
4734 goto out_free_mbox;
4735 }
James Smartda0436e2009-05-22 14:51:39 -04004736 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004737 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4738 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05004739 phba->hba_flag |= HBA_FCOE_MODE;
4740 else
4741 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04004742
4743 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4744 LPFC_DCBX_CEE_MODE)
4745 phba->hba_flag |= HBA_FIP_SUPPORT;
4746 else
4747 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4748
James Smartf1126682009-06-10 17:22:44 -04004749 if (phba->sli_rev != LPFC_SLI_REV4 ||
James Smart76a95d72010-11-20 23:11:48 -05004750 !(phba->hba_flag & HBA_FCOE_MODE)) {
James Smartda0436e2009-05-22 14:51:39 -04004751 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4752 "0376 READ_REV Error. SLI Level %d "
4753 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05004754 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04004755 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004756 kfree(vpd);
4757 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004758 }
James Smartda0436e2009-05-22 14:51:39 -04004759 /*
4760 * Evaluate the read rev and vpd data. Populate the driver
4761 * state with the results. If this routine fails, the failure
4762 * is not fatal as the driver will use generic values.
4763 */
4764 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4765 if (unlikely(!rc)) {
4766 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4767 "0377 Error %d parsing vpd. "
4768 "Using defaults.\n", rc);
4769 rc = 0;
4770 }
James Smart76a95d72010-11-20 23:11:48 -05004771 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04004772
James Smartf1126682009-06-10 17:22:44 -04004773 /* Save information as VPD data */
4774 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4775 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4776 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4777 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4778 &mqe->un.read_rev);
4779 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4780 &mqe->un.read_rev);
4781 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4782 &mqe->un.read_rev);
4783 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4784 &mqe->un.read_rev);
4785 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4786 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4787 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4788 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4789 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4790 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4791 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4792 "(%d):0380 READ_REV Status x%x "
4793 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4794 mboxq->vport ? mboxq->vport->vpi : 0,
4795 bf_get(lpfc_mqe_status, mqe),
4796 phba->vpd.rev.opFwName,
4797 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4798 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004799
4800 /*
4801 * Discover the port's supported feature set and match it against the
4802 * hosts requests.
4803 */
4804 lpfc_request_features(phba, mboxq);
4805 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4806 if (unlikely(rc)) {
4807 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004808 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004809 }
4810
4811 /*
4812 * The port must support FCP initiator mode as this is the
4813 * only mode running in the host.
4814 */
4815 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4816 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4817 "0378 No support for fcpi mode.\n");
4818 ftr_rsp++;
4819 }
4820
4821 /*
4822 * If the port cannot support the host's requested features
4823 * then turn off the global config parameters to disable the
4824 * feature in the driver. This is not a fatal error.
4825 */
4826 if ((phba->cfg_enable_bg) &&
4827 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4828 ftr_rsp++;
4829
4830 if (phba->max_vpi && phba->cfg_enable_npiv &&
4831 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4832 ftr_rsp++;
4833
4834 if (ftr_rsp) {
4835 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4836 "0379 Feature Mismatch Data: x%08x %08x "
4837 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4838 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4839 phba->cfg_enable_npiv, phba->max_vpi);
4840 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4841 phba->cfg_enable_bg = 0;
4842 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4843 phba->cfg_enable_npiv = 0;
4844 }
4845
4846 /* These SLI3 features are assumed in SLI4 */
4847 spin_lock_irq(&phba->hbalock);
4848 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4849 spin_unlock_irq(&phba->hbalock);
4850
4851 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004852 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4853 if (rc) {
4854 phba->link_state = LPFC_HBA_ERROR;
4855 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05004856 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05004857 }
4858
James Smartda0436e2009-05-22 14:51:39 -04004859 mboxq->vport = vport;
4860 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4861 mp = (struct lpfc_dmabuf *) mboxq->context1;
4862 if (rc == MBX_SUCCESS) {
4863 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4864 rc = 0;
4865 }
4866
4867 /*
4868 * This memory was allocated by the lpfc_read_sparam routine. Release
4869 * it to the mbuf pool.
4870 */
4871 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4872 kfree(mp);
4873 mboxq->context1 = NULL;
4874 if (unlikely(rc)) {
4875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4876 "0382 READ_SPARAM command failed "
4877 "status %d, mbxStatus x%x\n",
4878 rc, bf_get(lpfc_mqe_status, mqe));
4879 phba->link_state = LPFC_HBA_ERROR;
4880 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004881 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004882 }
4883
4884 if (phba->cfg_soft_wwnn)
4885 u64_to_wwn(phba->cfg_soft_wwnn,
4886 vport->fc_sparam.nodeName.u.wwn);
4887 if (phba->cfg_soft_wwpn)
4888 u64_to_wwn(phba->cfg_soft_wwpn,
4889 vport->fc_sparam.portName.u.wwn);
4890 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4891 sizeof(struct lpfc_name));
4892 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4893 sizeof(struct lpfc_name));
4894
4895 /* Update the fc_host data structures with new wwn. */
4896 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4897 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4898
4899 /* Register SGL pool to the device using non-embedded mailbox command */
4900 rc = lpfc_sli4_post_sgl_list(phba);
4901 if (unlikely(rc)) {
4902 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004903 "0582 Error %d during sgl post operation\n",
4904 rc);
James Smartda0436e2009-05-22 14:51:39 -04004905 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004906 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004907 }
4908
4909 /* Register SCSI SGL pool to the device */
4910 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4911 if (unlikely(rc)) {
4912 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004913 "0383 Error %d during scsi sgl post "
4914 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004915 /* Some Scsi buffers were moved to the abort scsi list */
4916 /* A pci function reset will repost them */
4917 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004918 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004919 }
4920
4921 /* Post the rpi header region to the device. */
4922 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4923 if (unlikely(rc)) {
4924 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4925 "0393 Error %d during rpi post operation\n",
4926 rc);
4927 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004928 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004929 }
James Smartda0436e2009-05-22 14:51:39 -04004930
4931 /* Set up all the queues to the device */
4932 rc = lpfc_sli4_queue_setup(phba);
4933 if (unlikely(rc)) {
4934 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4935 "0381 Error %d during queue setup.\n ", rc);
4936 goto out_stop_timers;
4937 }
4938
4939 /* Arm the CQs and then EQs on device */
4940 lpfc_sli4_arm_cqeq_intr(phba);
4941
4942 /* Indicate device interrupt mode */
4943 phba->sli4_hba.intr_enable = 1;
4944
4945 /* Allow asynchronous mailbox command to go through */
4946 spin_lock_irq(&phba->hbalock);
4947 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4948 spin_unlock_irq(&phba->hbalock);
4949
4950 /* Post receive buffers to the device */
4951 lpfc_sli4_rb_setup(phba);
4952
James Smartfc2b9892010-02-26 14:15:29 -05004953 /* Reset HBA FCF states after HBA reset */
4954 phba->fcf.fcf_flag = 0;
4955 phba->fcf.current_rec.flag = 0;
4956
James Smartda0436e2009-05-22 14:51:39 -04004957 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004958 mod_timer(&vport->els_tmofunc,
4959 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004960
4961 /* Start heart beat timer */
4962 mod_timer(&phba->hb_tmofunc,
4963 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4964 phba->hb_outstanding = 0;
4965 phba->last_completion_time = jiffies;
4966
4967 /* Start error attention (ERATT) polling timer */
4968 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4969
James Smart75baf692010-06-08 18:31:21 -04004970 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4971 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4972 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4973 if (!rc) {
4974 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4975 "2829 This device supports "
4976 "Advanced Error Reporting (AER)\n");
4977 spin_lock_irq(&phba->hbalock);
4978 phba->hba_flag |= HBA_AER_ENABLED;
4979 spin_unlock_irq(&phba->hbalock);
4980 } else {
4981 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4982 "2830 This device does not support "
4983 "Advanced Error Reporting (AER)\n");
4984 phba->cfg_aer_support = 0;
4985 }
4986 }
4987
James Smart76a95d72010-11-20 23:11:48 -05004988 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4989 /*
4990 * The FC Port needs to register FCFI (index 0)
4991 */
4992 lpfc_reg_fcfi(phba, mboxq);
4993 mboxq->vport = phba->pport;
4994 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4995 if (rc == MBX_SUCCESS)
4996 rc = 0;
4997 else
4998 goto out_unset_queue;
4999 }
James Smartda0436e2009-05-22 14:51:39 -04005000 /*
5001 * The port is ready, set the host's link state to LINK_DOWN
5002 * in preparation for link interrupts.
5003 */
James Smartda0436e2009-05-22 14:51:39 -04005004 spin_lock_irq(&phba->hbalock);
5005 phba->link_state = LPFC_LINK_DOWN;
5006 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05005007 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5008out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04005009 /* Unset all the queues set up in this routine when error out */
5010 if (rc)
5011 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005012out_stop_timers:
5013 if (rc)
5014 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04005015out_free_mbox:
5016 mempool_free(mboxq, phba->mbox_mem_pool);
5017 return rc;
5018}
James Smarte59058c2008-08-24 21:49:00 -04005019
5020/**
James Smart3621a712009-04-06 18:47:14 -04005021 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04005022 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05005023 *
James Smarte59058c2008-08-24 21:49:00 -04005024 * This is the callback function for mailbox timer. The mailbox
5025 * timer is armed when a new mailbox command is issued and the timer
5026 * is deleted when the mailbox complete. The function is called by
5027 * the kernel timer code when a mailbox does not complete within
5028 * expected time. This function wakes up the worker thread to
5029 * process the mailbox timeout and returns. All the processing is
5030 * done by the worker thread function lpfc_mbox_timeout_handler.
5031 **/
dea31012005-04-17 16:05:31 -05005032void
5033lpfc_mbox_timeout(unsigned long ptr)
5034{
James Smart92d7f7b2007-06-17 19:56:38 -05005035 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05005036 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05005037 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005038
James Smart2e0fef82007-06-17 19:56:36 -05005039 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005040 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005041 if (!tmo_posted)
5042 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5043 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5044
James Smart5e9d9b82008-06-14 22:52:53 -04005045 if (!tmo_posted)
5046 lpfc_worker_wake_up(phba);
5047 return;
dea31012005-04-17 16:05:31 -05005048}
5049
James Smarte59058c2008-08-24 21:49:00 -04005050
5051/**
James Smart3621a712009-04-06 18:47:14 -04005052 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04005053 * @phba: Pointer to HBA context object.
5054 *
5055 * This function is called from worker thread when a mailbox command times out.
5056 * The caller is not required to hold any locks. This function will reset the
5057 * HBA and recover all the pending commands.
5058 **/
dea31012005-04-17 16:05:31 -05005059void
5060lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5061{
James Smart2e0fef82007-06-17 19:56:36 -05005062 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04005063 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04005064 struct lpfc_sli *psli = &phba->sli;
5065 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005066
James Smarta257bf92009-04-06 18:48:10 -04005067 /* Check the pmbox pointer first. There is a race condition
5068 * between the mbox timeout handler getting executed in the
5069 * worklist and the mailbox actually completing. When this
5070 * race condition occurs, the mbox_active will be NULL.
5071 */
5072 spin_lock_irq(&phba->hbalock);
5073 if (pmbox == NULL) {
5074 lpfc_printf_log(phba, KERN_WARNING,
5075 LOG_MBOX | LOG_SLI,
5076 "0353 Active Mailbox cleared - mailbox timeout "
5077 "exiting\n");
5078 spin_unlock_irq(&phba->hbalock);
5079 return;
5080 }
5081
dea31012005-04-17 16:05:31 -05005082 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05005083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005084 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005085 mb->mbxCommand,
5086 phba->pport->port_state,
5087 phba->sli.sli_flag,
5088 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04005089 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005090
James Smart1dcb58e2007-04-25 09:51:30 -04005091 /* Setting state unknown so lpfc_sli_abort_iocb_ring
5092 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5093 * it to fail all oustanding SCSI IO.
5094 */
James Smart2e0fef82007-06-17 19:56:36 -05005095 spin_lock_irq(&phba->pport->work_port_lock);
5096 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5097 spin_unlock_irq(&phba->pport->work_port_lock);
5098 spin_lock_irq(&phba->hbalock);
5099 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04005100 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005101 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04005102
5103 pring = &psli->ring[psli->fcp_ring];
5104 lpfc_sli_abort_iocb_ring(phba, pring);
5105
5106 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005107 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04005108
5109 /* Reset the HBA device */
5110 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05005111}
5112
James Smarte59058c2008-08-24 21:49:00 -04005113/**
James Smart3772a992009-05-22 14:50:54 -04005114 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04005115 * @phba: Pointer to HBA context object.
5116 * @pmbox: Pointer to mailbox object.
5117 * @flag: Flag indicating how the mailbox need to be processed.
5118 *
5119 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04005120 * to submit a mailbox command to firmware with SLI-3 interface spec. This
5121 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04005122 * The mailbox command can be submitted in polling mode, in which case
5123 * this function will wait in a polling loop for the completion of the
5124 * mailbox.
5125 * If the mailbox is submitted in no_wait mode (not polling) the
5126 * function will submit the command and returns immediately without waiting
5127 * for the mailbox completion. The no_wait is supported only when HBA
5128 * is in SLI2/SLI3 mode - interrupts are enabled.
5129 * The SLI interface allows only one mailbox pending at a time. If the
5130 * mailbox is issued in polling mode and there is already a mailbox
5131 * pending, then the function will return an error. If the mailbox is issued
5132 * in NO_WAIT mode and there is a mailbox pending already, the function
5133 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5134 * The sli layer owns the mailbox object until the completion of mailbox
5135 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5136 * return codes the caller owns the mailbox command after the return of
5137 * the function.
5138 **/
James Smart3772a992009-05-22 14:50:54 -04005139static int
5140lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5141 uint32_t flag)
dea31012005-04-17 16:05:31 -05005142{
dea31012005-04-17 16:05:31 -05005143 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05005144 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005145 uint32_t status, evtctr;
5146 uint32_t ha_copy;
5147 int i;
James Smart09372822008-01-11 01:52:54 -05005148 unsigned long timeout;
dea31012005-04-17 16:05:31 -05005149 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04005150 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05005151 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04005152 int processing_queue = 0;
5153
5154 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5155 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04005156 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04005157 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04005158 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5159 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5160 return MBX_SUCCESS;
5161 }
James Smart58da1ff2008-04-07 10:15:56 -04005162 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04005163 pmbox = lpfc_mbox_get(phba);
5164 if (!pmbox) {
5165 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5166 return MBX_SUCCESS;
5167 }
5168 }
dea31012005-04-17 16:05:31 -05005169
James Smarted957682007-06-17 19:56:37 -05005170 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05005171 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05005172 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04005173 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05005174 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005175 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005176 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04005177 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05005178 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04005179 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05005180 }
5181 }
5182
Linas Vepstas8d63f372007-02-14 14:28:36 -06005183 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04005184 if (unlikely(pci_channel_offline(phba->pcidev))) {
5185 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5186 goto out_not_finished;
5187 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06005188
James Smarta257bf92009-04-06 18:48:10 -04005189 /* If HBA has a deferred error attention, fail the iocb. */
5190 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5191 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5192 goto out_not_finished;
5193 }
5194
dea31012005-04-17 16:05:31 -05005195 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05005196
James Smart3772a992009-05-22 14:50:54 -04005197 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05005198 status = MBX_SUCCESS;
5199
James Smart2e0fef82007-06-17 19:56:36 -05005200 if (phba->link_state == LPFC_HBA_ERROR) {
5201 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005202
5203 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5205 "(%d):0311 Mailbox command x%x cannot "
5206 "issue Data: x%x x%x\n",
5207 pmbox->vport ? pmbox->vport->vpi : 0,
5208 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005209 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005210 }
5211
James Smart92908312006-03-07 15:04:13 -05005212 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
5213 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05005214 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04005215 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5216 "(%d):2528 Mailbox command x%x cannot "
5217 "issue Data: x%x x%x\n",
5218 pmbox->vport ? pmbox->vport->vpi : 0,
5219 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005220 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05005221 }
5222
dea31012005-04-17 16:05:31 -05005223 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5224 /* Polling for a mbox command when another one is already active
5225 * is not allowed in SLI. Also, the driver must have established
5226 * SLI2 mode to queue and process multiple mbox commands.
5227 */
5228
5229 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05005230 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005231
5232 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005233 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5234 "(%d):2529 Mailbox command x%x "
5235 "cannot issue Data: x%x x%x\n",
5236 pmbox->vport ? pmbox->vport->vpi : 0,
5237 pmbox->u.mb.mbxCommand,
5238 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005239 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005240 }
5241
James Smart3772a992009-05-22 14:50:54 -04005242 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005243 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005244 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005245 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5246 "(%d):2530 Mailbox command x%x "
5247 "cannot issue Data: x%x x%x\n",
5248 pmbox->vport ? pmbox->vport->vpi : 0,
5249 pmbox->u.mb.mbxCommand,
5250 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005251 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005252 }
5253
dea31012005-04-17 16:05:31 -05005254 /* Another mailbox command is still being processed, queue this
5255 * command to be processed later.
5256 */
5257 lpfc_mbox_put(phba, pmbox);
5258
5259 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05005260 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005261 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05005262 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005263 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5264 mb->mbxCommand, phba->pport->port_state,
5265 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005266
5267 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05005268 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005269
James Smart858c9f62007-06-17 19:56:39 -05005270 if (pmbox->vport) {
5271 lpfc_debugfs_disc_trc(pmbox->vport,
5272 LPFC_DISC_TRC_MBOX_VPORT,
5273 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
5274 (uint32_t)mb->mbxCommand,
5275 mb->un.varWords[0], mb->un.varWords[1]);
5276 }
5277 else {
5278 lpfc_debugfs_disc_trc(phba->pport,
5279 LPFC_DISC_TRC_MBOX,
5280 "MBOX Bsy: cmd:x%x mb:x%x x%x",
5281 (uint32_t)mb->mbxCommand,
5282 mb->un.varWords[0], mb->un.varWords[1]);
5283 }
5284
James Smart2e0fef82007-06-17 19:56:36 -05005285 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05005286 }
5287
dea31012005-04-17 16:05:31 -05005288 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5289
5290 /* If we are not polling, we MUST be in SLI2 mode */
5291 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04005292 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05005293 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05005294 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005295 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005296 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005297 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5298 "(%d):2531 Mailbox command x%x "
5299 "cannot issue Data: x%x x%x\n",
5300 pmbox->vport ? pmbox->vport->vpi : 0,
5301 pmbox->u.mb.mbxCommand,
5302 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005303 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005304 }
5305 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04005306 mod_timer(&psli->mbox_tmo, (jiffies +
5307 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05005308 }
5309
5310 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05005311 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005312 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005313 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005314 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05005315 mb->mbxCommand, phba->pport->port_state,
5316 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005317
James Smart858c9f62007-06-17 19:56:39 -05005318 if (mb->mbxCommand != MBX_HEARTBEAT) {
5319 if (pmbox->vport) {
5320 lpfc_debugfs_disc_trc(pmbox->vport,
5321 LPFC_DISC_TRC_MBOX_VPORT,
5322 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5323 (uint32_t)mb->mbxCommand,
5324 mb->un.varWords[0], mb->un.varWords[1]);
5325 }
5326 else {
5327 lpfc_debugfs_disc_trc(phba->pport,
5328 LPFC_DISC_TRC_MBOX,
5329 "MBOX Send: cmd:x%x mb:x%x x%x",
5330 (uint32_t)mb->mbxCommand,
5331 mb->un.varWords[0], mb->un.varWords[1]);
5332 }
5333 }
5334
dea31012005-04-17 16:05:31 -05005335 psli->slistat.mbox_cmd++;
5336 evtctr = psli->slistat.mbox_event;
5337
5338 /* next set own bit for the adapter and copy over command word */
5339 mb->mbxOwner = OWN_CHIP;
5340
James Smart3772a992009-05-22 14:50:54 -04005341 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04005342 /* Populate mbox extension offset word. */
5343 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5344 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5345 = (uint8_t *)phba->mbox_ext
5346 - (uint8_t *)phba->mbox;
5347 }
5348
5349 /* Copy the mailbox extension data */
5350 if (pmbox->in_ext_byte_len && pmbox->context2) {
5351 lpfc_sli_pcimem_bcopy(pmbox->context2,
5352 (uint8_t *)phba->mbox_ext,
5353 pmbox->in_ext_byte_len);
5354 }
5355 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04005356 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005357 } else {
James Smart7a470272010-03-15 11:25:20 -04005358 /* Populate mbox extension offset word. */
5359 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5360 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5361 = MAILBOX_HBA_EXT_OFFSET;
5362
5363 /* Copy the mailbox extension data */
5364 if (pmbox->in_ext_byte_len && pmbox->context2) {
5365 lpfc_memcpy_to_slim(phba->MBslimaddr +
5366 MAILBOX_HBA_EXT_OFFSET,
5367 pmbox->context2, pmbox->in_ext_byte_len);
5368
5369 }
James Smart92908312006-03-07 15:04:13 -05005370 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05005371 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04005372 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005373 }
5374
5375 /* First copy mbox command data to HBA SLIM, skip past first
5376 word */
5377 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5378 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5379 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5380
5381 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005382 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005383 to_slim = phba->MBslimaddr;
5384 writel(ldata, to_slim);
5385 readl(to_slim); /* flush */
5386
5387 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5388 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005389 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005390 }
5391 }
5392
5393 wmb();
dea31012005-04-17 16:05:31 -05005394
5395 switch (flag) {
5396 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005397 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005398 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005399 /* Interrupt board to do it */
5400 writel(CA_MBATT, phba->CAregaddr);
5401 readl(phba->CAregaddr); /* flush */
5402 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005403 break;
5404
5405 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005406 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005407 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005408 /* Interrupt board to do it */
5409 writel(CA_MBATT, phba->CAregaddr);
5410 readl(phba->CAregaddr); /* flush */
5411
James Smart3772a992009-05-22 14:50:54 -04005412 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005413 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005414 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005415 word0 = le32_to_cpu(word0);
5416 } else {
5417 /* First read mbox status word */
5418 word0 = readl(phba->MBslimaddr);
5419 }
5420
5421 /* Read the HBA Host Attention Register */
5422 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005423 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5424 mb->mbxCommand) *
5425 1000) + jiffies;
5426 i = 0;
dea31012005-04-17 16:05:31 -05005427 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005428 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5429 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005430 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005431 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005432 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005433 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005434 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005435 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005436 }
5437
5438 /* Check if we took a mbox interrupt while we were
5439 polling */
5440 if (((word0 & OWN_CHIP) != OWN_CHIP)
5441 && (evtctr != psli->slistat.mbox_event))
5442 break;
5443
James Smart09372822008-01-11 01:52:54 -05005444 if (i++ > 10) {
5445 spin_unlock_irqrestore(&phba->hbalock,
5446 drvr_flag);
5447 msleep(1);
5448 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5449 }
dea31012005-04-17 16:05:31 -05005450
James Smart3772a992009-05-22 14:50:54 -04005451 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005452 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005453 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005454 word0 = le32_to_cpu(word0);
5455 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5456 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005457 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005458 /* Check real SLIM for any errors */
5459 slimword0 = readl(phba->MBslimaddr);
5460 slimmb = (MAILBOX_t *) & slimword0;
5461 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5462 && slimmb->mbxStatus) {
5463 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005464 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005465 word0 = slimword0;
5466 }
5467 }
5468 } else {
5469 /* First copy command data */
5470 word0 = readl(phba->MBslimaddr);
5471 }
5472 /* Read the HBA Host Attention Register */
5473 ha_copy = readl(phba->HAregaddr);
5474 }
5475
James Smart3772a992009-05-22 14:50:54 -04005476 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005477 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005478 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005479 /* Copy the mailbox extension data */
5480 if (pmbox->out_ext_byte_len && pmbox->context2) {
5481 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5482 pmbox->context2,
5483 pmbox->out_ext_byte_len);
5484 }
dea31012005-04-17 16:05:31 -05005485 } else {
5486 /* First copy command data */
5487 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5488 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005489 /* Copy the mailbox extension data */
5490 if (pmbox->out_ext_byte_len && pmbox->context2) {
5491 lpfc_memcpy_from_slim(pmbox->context2,
5492 phba->MBslimaddr +
5493 MAILBOX_HBA_EXT_OFFSET,
5494 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005495 }
5496 }
5497
5498 writel(HA_MBATT, phba->HAregaddr);
5499 readl(phba->HAregaddr); /* flush */
5500
5501 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5502 status = mb->mbxStatus;
5503 }
5504
James Smart2e0fef82007-06-17 19:56:36 -05005505 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5506 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005507
5508out_not_finished:
5509 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005510 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005511 lpfc_mbox_cmpl_put(phba, pmbox);
5512 }
5513 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005514}
5515
James Smarte59058c2008-08-24 21:49:00 -04005516/**
James Smartf1126682009-06-10 17:22:44 -04005517 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5518 * @phba: Pointer to HBA context object.
5519 *
5520 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5521 * the driver internal pending mailbox queue. It will then try to wait out the
5522 * possible outstanding mailbox command before return.
5523 *
5524 * Returns:
5525 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5526 * the outstanding mailbox command timed out.
5527 **/
5528static int
5529lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5530{
5531 struct lpfc_sli *psli = &phba->sli;
5532 uint8_t actcmd = MBX_HEARTBEAT;
5533 int rc = 0;
5534 unsigned long timeout;
5535
5536 /* Mark the asynchronous mailbox command posting as blocked */
5537 spin_lock_irq(&phba->hbalock);
5538 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5539 if (phba->sli.mbox_active)
5540 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5541 spin_unlock_irq(&phba->hbalock);
5542 /* Determine how long we might wait for the active mailbox
5543 * command to be gracefully completed by firmware.
5544 */
5545 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5546 jiffies;
5547 /* Wait for the outstnading mailbox command to complete */
5548 while (phba->sli.mbox_active) {
5549 /* Check active mailbox complete status every 2ms */
5550 msleep(2);
5551 if (time_after(jiffies, timeout)) {
5552 /* Timeout, marked the outstanding cmd not complete */
5553 rc = 1;
5554 break;
5555 }
5556 }
5557
5558 /* Can not cleanly block async mailbox command, fails it */
5559 if (rc) {
5560 spin_lock_irq(&phba->hbalock);
5561 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5562 spin_unlock_irq(&phba->hbalock);
5563 }
5564 return rc;
5565}
5566
5567/**
5568 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5569 * @phba: Pointer to HBA context object.
5570 *
5571 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5572 * commands from the driver internal pending mailbox queue. It makes sure
5573 * that there is no outstanding mailbox command before resuming posting
5574 * asynchronous mailbox commands. If, for any reason, there is outstanding
5575 * mailbox command, it will try to wait it out before resuming asynchronous
5576 * mailbox command posting.
5577 **/
5578static void
5579lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5580{
5581 struct lpfc_sli *psli = &phba->sli;
5582
5583 spin_lock_irq(&phba->hbalock);
5584 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5585 /* Asynchronous mailbox posting is not blocked, do nothing */
5586 spin_unlock_irq(&phba->hbalock);
5587 return;
5588 }
5589
5590 /* Outstanding synchronous mailbox command is guaranteed to be done,
5591 * successful or timeout, after timing-out the outstanding mailbox
5592 * command shall always be removed, so just unblock posting async
5593 * mailbox command and resume
5594 */
5595 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5596 spin_unlock_irq(&phba->hbalock);
5597
5598 /* wake up worker thread to post asynchronlous mailbox command */
5599 lpfc_worker_wake_up(phba);
5600}
5601
5602/**
James Smartda0436e2009-05-22 14:51:39 -04005603 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5604 * @phba: Pointer to HBA context object.
5605 * @mboxq: Pointer to mailbox object.
5606 *
5607 * The function posts a mailbox to the port. The mailbox is expected
5608 * to be comletely filled in and ready for the port to operate on it.
5609 * This routine executes a synchronous completion operation on the
5610 * mailbox by polling for its completion.
5611 *
5612 * The caller must not be holding any locks when calling this routine.
5613 *
5614 * Returns:
5615 * MBX_SUCCESS - mailbox posted successfully
5616 * Any of the MBX error values.
5617 **/
5618static int
5619lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5620{
5621 int rc = MBX_SUCCESS;
5622 unsigned long iflag;
5623 uint32_t db_ready;
5624 uint32_t mcqe_status;
5625 uint32_t mbx_cmnd;
5626 unsigned long timeout;
5627 struct lpfc_sli *psli = &phba->sli;
5628 struct lpfc_mqe *mb = &mboxq->u.mqe;
5629 struct lpfc_bmbx_create *mbox_rgn;
5630 struct dma_address *dma_address;
5631 struct lpfc_register bmbx_reg;
5632
5633 /*
5634 * Only one mailbox can be active to the bootstrap mailbox region
5635 * at a time and there is no queueing provided.
5636 */
5637 spin_lock_irqsave(&phba->hbalock, iflag);
5638 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5639 spin_unlock_irqrestore(&phba->hbalock, iflag);
5640 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5641 "(%d):2532 Mailbox command x%x (x%x) "
5642 "cannot issue Data: x%x x%x\n",
5643 mboxq->vport ? mboxq->vport->vpi : 0,
5644 mboxq->u.mb.mbxCommand,
5645 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5646 psli->sli_flag, MBX_POLL);
5647 return MBXERR_ERROR;
5648 }
5649 /* The server grabs the token and owns it until release */
5650 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5651 phba->sli.mbox_active = mboxq;
5652 spin_unlock_irqrestore(&phba->hbalock, iflag);
5653
5654 /*
5655 * Initialize the bootstrap memory region to avoid stale data areas
5656 * in the mailbox post. Then copy the caller's mailbox contents to
5657 * the bmbx mailbox region.
5658 */
5659 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5660 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5661 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5662 sizeof(struct lpfc_mqe));
5663
5664 /* Post the high mailbox dma address to the port and wait for ready. */
5665 dma_address = &phba->sli4_hba.bmbx.dma_address;
5666 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5667
5668 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5669 * 1000) + jiffies;
5670 do {
5671 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5672 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5673 if (!db_ready)
5674 msleep(2);
5675
5676 if (time_after(jiffies, timeout)) {
5677 rc = MBXERR_ERROR;
5678 goto exit;
5679 }
5680 } while (!db_ready);
5681
5682 /* Post the low mailbox dma address to the port. */
5683 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5684 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5685 * 1000) + jiffies;
5686 do {
5687 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5688 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5689 if (!db_ready)
5690 msleep(2);
5691
5692 if (time_after(jiffies, timeout)) {
5693 rc = MBXERR_ERROR;
5694 goto exit;
5695 }
5696 } while (!db_ready);
5697
5698 /*
5699 * Read the CQ to ensure the mailbox has completed.
5700 * If so, update the mailbox status so that the upper layers
5701 * can complete the request normally.
5702 */
5703 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5704 sizeof(struct lpfc_mqe));
5705 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5706 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5707 sizeof(struct lpfc_mcqe));
5708 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5709
5710 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5711 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5712 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5713 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005714 } else
5715 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005716
5717 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5718 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5719 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5720 " x%x x%x CQ: x%x x%x x%x x%x\n",
5721 mboxq->vport ? mboxq->vport->vpi : 0,
5722 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5723 bf_get(lpfc_mqe_status, mb),
5724 mb->un.mb_words[0], mb->un.mb_words[1],
5725 mb->un.mb_words[2], mb->un.mb_words[3],
5726 mb->un.mb_words[4], mb->un.mb_words[5],
5727 mb->un.mb_words[6], mb->un.mb_words[7],
5728 mb->un.mb_words[8], mb->un.mb_words[9],
5729 mb->un.mb_words[10], mb->un.mb_words[11],
5730 mb->un.mb_words[12], mboxq->mcqe.word0,
5731 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5732 mboxq->mcqe.trailer);
5733exit:
5734 /* We are holding the token, no needed for lock when release */
5735 spin_lock_irqsave(&phba->hbalock, iflag);
5736 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5737 phba->sli.mbox_active = NULL;
5738 spin_unlock_irqrestore(&phba->hbalock, iflag);
5739 return rc;
5740}
5741
5742/**
5743 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5744 * @phba: Pointer to HBA context object.
5745 * @pmbox: Pointer to mailbox object.
5746 * @flag: Flag indicating how the mailbox need to be processed.
5747 *
5748 * This function is called by discovery code and HBA management code to submit
5749 * a mailbox command to firmware with SLI-4 interface spec.
5750 *
5751 * Return codes the caller owns the mailbox command after the return of the
5752 * function.
5753 **/
5754static int
5755lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5756 uint32_t flag)
5757{
5758 struct lpfc_sli *psli = &phba->sli;
5759 unsigned long iflags;
5760 int rc;
5761
James Smart8fa38512009-07-19 10:01:03 -04005762 rc = lpfc_mbox_dev_check(phba);
5763 if (unlikely(rc)) {
5764 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5765 "(%d):2544 Mailbox command x%x (x%x) "
5766 "cannot issue Data: x%x x%x\n",
5767 mboxq->vport ? mboxq->vport->vpi : 0,
5768 mboxq->u.mb.mbxCommand,
5769 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5770 psli->sli_flag, flag);
5771 goto out_not_finished;
5772 }
5773
James Smartda0436e2009-05-22 14:51:39 -04005774 /* Detect polling mode and jump to a handler */
5775 if (!phba->sli4_hba.intr_enable) {
5776 if (flag == MBX_POLL)
5777 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5778 else
5779 rc = -EIO;
5780 if (rc != MBX_SUCCESS)
5781 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5782 "(%d):2541 Mailbox command x%x "
5783 "(x%x) cannot issue Data: x%x x%x\n",
5784 mboxq->vport ? mboxq->vport->vpi : 0,
5785 mboxq->u.mb.mbxCommand,
5786 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5787 psli->sli_flag, flag);
5788 return rc;
5789 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005790 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5791 "(%d):2542 Try to issue mailbox command "
5792 "x%x (x%x) synchronously ahead of async"
5793 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005794 mboxq->vport ? mboxq->vport->vpi : 0,
5795 mboxq->u.mb.mbxCommand,
5796 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5797 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005798 /* Try to block the asynchronous mailbox posting */
5799 rc = lpfc_sli4_async_mbox_block(phba);
5800 if (!rc) {
5801 /* Successfully blocked, now issue sync mbox cmd */
5802 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5803 if (rc != MBX_SUCCESS)
5804 lpfc_printf_log(phba, KERN_ERR,
5805 LOG_MBOX | LOG_SLI,
5806 "(%d):2597 Mailbox command "
5807 "x%x (x%x) cannot issue "
5808 "Data: x%x x%x\n",
5809 mboxq->vport ?
5810 mboxq->vport->vpi : 0,
5811 mboxq->u.mb.mbxCommand,
5812 lpfc_sli4_mbox_opcode_get(phba,
5813 mboxq),
5814 psli->sli_flag, flag);
5815 /* Unblock the async mailbox posting afterward */
5816 lpfc_sli4_async_mbox_unblock(phba);
5817 }
5818 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005819 }
5820
5821 /* Now, interrupt mode asynchrous mailbox command */
5822 rc = lpfc_mbox_cmd_check(phba, mboxq);
5823 if (rc) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5825 "(%d):2543 Mailbox command x%x (x%x) "
5826 "cannot issue Data: x%x x%x\n",
5827 mboxq->vport ? mboxq->vport->vpi : 0,
5828 mboxq->u.mb.mbxCommand,
5829 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5830 psli->sli_flag, flag);
5831 goto out_not_finished;
5832 }
James Smartda0436e2009-05-22 14:51:39 -04005833
5834 /* Put the mailbox command to the driver internal FIFO */
5835 psli->slistat.mbox_busy++;
5836 spin_lock_irqsave(&phba->hbalock, iflags);
5837 lpfc_mbox_put(phba, mboxq);
5838 spin_unlock_irqrestore(&phba->hbalock, iflags);
5839 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5840 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5841 "x%x (x%x) x%x x%x x%x\n",
5842 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5843 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5844 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5845 phba->pport->port_state,
5846 psli->sli_flag, MBX_NOWAIT);
5847 /* Wake up worker thread to transport mailbox command from head */
5848 lpfc_worker_wake_up(phba);
5849
5850 return MBX_BUSY;
5851
5852out_not_finished:
5853 return MBX_NOT_FINISHED;
5854}
5855
5856/**
5857 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5858 * @phba: Pointer to HBA context object.
5859 *
5860 * This function is called by worker thread to send a mailbox command to
5861 * SLI4 HBA firmware.
5862 *
5863 **/
5864int
5865lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5866{
5867 struct lpfc_sli *psli = &phba->sli;
5868 LPFC_MBOXQ_t *mboxq;
5869 int rc = MBX_SUCCESS;
5870 unsigned long iflags;
5871 struct lpfc_mqe *mqe;
5872 uint32_t mbx_cmnd;
5873
5874 /* Check interrupt mode before post async mailbox command */
5875 if (unlikely(!phba->sli4_hba.intr_enable))
5876 return MBX_NOT_FINISHED;
5877
5878 /* Check for mailbox command service token */
5879 spin_lock_irqsave(&phba->hbalock, iflags);
5880 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5881 spin_unlock_irqrestore(&phba->hbalock, iflags);
5882 return MBX_NOT_FINISHED;
5883 }
5884 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5885 spin_unlock_irqrestore(&phba->hbalock, iflags);
5886 return MBX_NOT_FINISHED;
5887 }
5888 if (unlikely(phba->sli.mbox_active)) {
5889 spin_unlock_irqrestore(&phba->hbalock, iflags);
5890 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5891 "0384 There is pending active mailbox cmd\n");
5892 return MBX_NOT_FINISHED;
5893 }
5894 /* Take the mailbox command service token */
5895 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5896
5897 /* Get the next mailbox command from head of queue */
5898 mboxq = lpfc_mbox_get(phba);
5899
5900 /* If no more mailbox command waiting for post, we're done */
5901 if (!mboxq) {
5902 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5903 spin_unlock_irqrestore(&phba->hbalock, iflags);
5904 return MBX_SUCCESS;
5905 }
5906 phba->sli.mbox_active = mboxq;
5907 spin_unlock_irqrestore(&phba->hbalock, iflags);
5908
5909 /* Check device readiness for posting mailbox command */
5910 rc = lpfc_mbox_dev_check(phba);
5911 if (unlikely(rc))
5912 /* Driver clean routine will clean up pending mailbox */
5913 goto out_not_finished;
5914
5915 /* Prepare the mbox command to be posted */
5916 mqe = &mboxq->u.mqe;
5917 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5918
5919 /* Start timer for the mbox_tmo and log some mailbox post messages */
5920 mod_timer(&psli->mbox_tmo, (jiffies +
5921 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5922
5923 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5924 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5925 "x%x x%x\n",
5926 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5927 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5928 phba->pport->port_state, psli->sli_flag);
5929
5930 if (mbx_cmnd != MBX_HEARTBEAT) {
5931 if (mboxq->vport) {
5932 lpfc_debugfs_disc_trc(mboxq->vport,
5933 LPFC_DISC_TRC_MBOX_VPORT,
5934 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5935 mbx_cmnd, mqe->un.mb_words[0],
5936 mqe->un.mb_words[1]);
5937 } else {
5938 lpfc_debugfs_disc_trc(phba->pport,
5939 LPFC_DISC_TRC_MBOX,
5940 "MBOX Send: cmd:x%x mb:x%x x%x",
5941 mbx_cmnd, mqe->un.mb_words[0],
5942 mqe->un.mb_words[1]);
5943 }
5944 }
5945 psli->slistat.mbox_cmd++;
5946
5947 /* Post the mailbox command to the port */
5948 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5949 if (rc != MBX_SUCCESS) {
5950 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5951 "(%d):2533 Mailbox command x%x (x%x) "
5952 "cannot issue Data: x%x x%x\n",
5953 mboxq->vport ? mboxq->vport->vpi : 0,
5954 mboxq->u.mb.mbxCommand,
5955 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5956 psli->sli_flag, MBX_NOWAIT);
5957 goto out_not_finished;
5958 }
5959
5960 return rc;
5961
5962out_not_finished:
5963 spin_lock_irqsave(&phba->hbalock, iflags);
5964 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5965 __lpfc_mbox_cmpl_put(phba, mboxq);
5966 /* Release the token */
5967 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5968 phba->sli.mbox_active = NULL;
5969 spin_unlock_irqrestore(&phba->hbalock, iflags);
5970
5971 return MBX_NOT_FINISHED;
5972}
5973
5974/**
5975 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5976 * @phba: Pointer to HBA context object.
5977 * @pmbox: Pointer to mailbox object.
5978 * @flag: Flag indicating how the mailbox need to be processed.
5979 *
5980 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5981 * the API jump table function pointer from the lpfc_hba struct.
5982 *
5983 * Return codes the caller owns the mailbox command after the return of the
5984 * function.
5985 **/
5986int
5987lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5988{
5989 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5990}
5991
5992/**
5993 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5994 * @phba: The hba struct for which this call is being executed.
5995 * @dev_grp: The HBA PCI-Device group number.
5996 *
5997 * This routine sets up the mbox interface API function jump table in @phba
5998 * struct.
5999 * Returns: 0 - success, -ENODEV - failure.
6000 **/
6001int
6002lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6003{
6004
6005 switch (dev_grp) {
6006 case LPFC_PCI_DEV_LP:
6007 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6008 phba->lpfc_sli_handle_slow_ring_event =
6009 lpfc_sli_handle_slow_ring_event_s3;
6010 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6011 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6012 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6013 break;
6014 case LPFC_PCI_DEV_OC:
6015 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6016 phba->lpfc_sli_handle_slow_ring_event =
6017 lpfc_sli_handle_slow_ring_event_s4;
6018 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6019 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6020 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6021 break;
6022 default:
6023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6024 "1420 Invalid HBA PCI-device group: 0x%x\n",
6025 dev_grp);
6026 return -ENODEV;
6027 break;
6028 }
6029 return 0;
6030}
6031
6032/**
James Smart3621a712009-04-06 18:47:14 -04006033 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04006034 * @phba: Pointer to HBA context object.
6035 * @pring: Pointer to driver SLI ring object.
6036 * @piocb: Pointer to address of newly added command iocb.
6037 *
6038 * This function is called with hbalock held to add a command
6039 * iocb to the txq when SLI layer cannot submit the command iocb
6040 * to the ring.
6041 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006042void
James Smart92d7f7b2007-06-17 19:56:38 -05006043__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006044 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05006045{
6046 /* Insert the caller's iocb in the txq tail for later processing. */
6047 list_add_tail(&piocb->list, &pring->txq);
6048 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05006049}
6050
James Smarte59058c2008-08-24 21:49:00 -04006051/**
James Smart3621a712009-04-06 18:47:14 -04006052 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04006053 * @phba: Pointer to HBA context object.
6054 * @pring: Pointer to driver SLI ring object.
6055 * @piocb: Pointer to address of newly added command iocb.
6056 *
6057 * This function is called with hbalock held before a new
6058 * iocb is submitted to the firmware. This function checks
6059 * txq to flush the iocbs in txq to Firmware before
6060 * submitting new iocbs to the Firmware.
6061 * If there are iocbs in the txq which need to be submitted
6062 * to firmware, lpfc_sli_next_iocb returns the first element
6063 * of the txq after dequeuing it from txq.
6064 * If there is no iocb in the txq then the function will return
6065 * *piocb and *piocb is set to NULL. Caller needs to check
6066 * *piocb to find if there are more commands in the txq.
6067 **/
dea31012005-04-17 16:05:31 -05006068static struct lpfc_iocbq *
6069lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006070 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05006071{
6072 struct lpfc_iocbq * nextiocb;
6073
6074 nextiocb = lpfc_sli_ringtx_get(phba, pring);
6075 if (!nextiocb) {
6076 nextiocb = *piocb;
6077 *piocb = NULL;
6078 }
6079
6080 return nextiocb;
6081}
6082
James Smarte59058c2008-08-24 21:49:00 -04006083/**
James Smart3772a992009-05-22 14:50:54 -04006084 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006085 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04006086 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04006087 * @piocb: Pointer to command iocb.
6088 * @flag: Flag indicating if this command can be put into txq.
6089 *
James Smart3772a992009-05-22 14:50:54 -04006090 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6091 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6092 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6093 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6094 * this function allows only iocbs for posting buffers. This function finds
6095 * next available slot in the command ring and posts the command to the
6096 * available slot and writes the port attention register to request HBA start
6097 * processing new iocb. If there is no slot available in the ring and
6098 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6099 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04006100 *
James Smart3772a992009-05-22 14:50:54 -04006101 * This function is called with hbalock held. The function will return success
6102 * after it successfully submit the iocb to firmware or after adding to the
6103 * txq.
James Smarte59058c2008-08-24 21:49:00 -04006104 **/
James Smart98c9ea52007-10-27 13:37:33 -04006105static int
James Smart3772a992009-05-22 14:50:54 -04006106__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05006107 struct lpfc_iocbq *piocb, uint32_t flag)
6108{
6109 struct lpfc_iocbq *nextiocb;
6110 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04006111 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05006112
James Smart92d7f7b2007-06-17 19:56:38 -05006113 if (piocb->iocb_cmpl && (!piocb->vport) &&
6114 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6115 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6116 lpfc_printf_log(phba, KERN_ERR,
6117 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006118 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006119 piocb->iocb.ulpCommand);
6120 dump_stack();
6121 return IOCB_ERROR;
6122 }
6123
6124
Linas Vepstas8d63f372007-02-14 14:28:36 -06006125 /* If the PCI channel is in offline state, do not post iocbs. */
6126 if (unlikely(pci_channel_offline(phba->pcidev)))
6127 return IOCB_ERROR;
6128
James Smarta257bf92009-04-06 18:48:10 -04006129 /* If HBA has a deferred error attention, fail the iocb. */
6130 if (unlikely(phba->hba_flag & DEFER_ERATT))
6131 return IOCB_ERROR;
6132
dea31012005-04-17 16:05:31 -05006133 /*
6134 * We should never get an IOCB if we are in a < LINK_DOWN state
6135 */
James Smart2e0fef82007-06-17 19:56:36 -05006136 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05006137 return IOCB_ERROR;
6138
6139 /*
6140 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04006141 * outstanding event.
dea31012005-04-17 16:05:31 -05006142 */
James Smart0b727fe2007-10-27 13:37:25 -04006143 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05006144 goto iocb_busy;
6145
James Smart2e0fef82007-06-17 19:56:36 -05006146 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05006147 /*
James Smart2680eea2007-04-25 09:52:55 -04006148 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05006149 * can be issued if the link is not up.
6150 */
6151 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04006152 case CMD_GEN_REQUEST64_CR:
6153 case CMD_GEN_REQUEST64_CX:
6154 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6155 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04006156 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04006157 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6158 MENLO_TRANSPORT_TYPE))
6159
6160 goto iocb_busy;
6161 break;
dea31012005-04-17 16:05:31 -05006162 case CMD_QUE_RING_BUF_CN:
6163 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05006164 /*
6165 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6166 * completion, iocb_cmpl MUST be 0.
6167 */
6168 if (piocb->iocb_cmpl)
6169 piocb->iocb_cmpl = NULL;
6170 /*FALLTHROUGH*/
6171 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04006172 case CMD_CLOSE_XRI_CN:
6173 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05006174 break;
6175 default:
6176 goto iocb_busy;
6177 }
6178
6179 /*
6180 * For FCP commands, we must be in a state where we can process link
6181 * attention events.
6182 */
6183 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05006184 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05006185 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05006186 }
dea31012005-04-17 16:05:31 -05006187
dea31012005-04-17 16:05:31 -05006188 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6189 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6190 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6191
6192 if (iocb)
6193 lpfc_sli_update_ring(phba, pring);
6194 else
6195 lpfc_sli_update_full_ring(phba, pring);
6196
6197 if (!piocb)
6198 return IOCB_SUCCESS;
6199
6200 goto out_busy;
6201
6202 iocb_busy:
6203 pring->stats.iocb_cmd_delay++;
6204
6205 out_busy:
6206
6207 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05006208 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05006209 return IOCB_SUCCESS;
6210 }
6211
6212 return IOCB_BUSY;
6213}
6214
James Smart3772a992009-05-22 14:50:54 -04006215/**
James Smart4f774512009-05-22 14:52:35 -04006216 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6217 * @phba: Pointer to HBA context object.
6218 * @piocb: Pointer to command iocb.
6219 * @sglq: Pointer to the scatter gather queue object.
6220 *
6221 * This routine converts the bpl or bde that is in the IOCB
6222 * to a sgl list for the sli4 hardware. The physical address
6223 * of the bpl/bde is converted back to a virtual address.
6224 * If the IOCB contains a BPL then the list of BDE's is
6225 * converted to sli4_sge's. If the IOCB contains a single
6226 * BDE then it is converted to a single sli_sge.
6227 * The IOCB is still in cpu endianess so the contents of
6228 * the bpl can be used without byte swapping.
6229 *
6230 * Returns valid XRI = Success, NO_XRI = Failure.
6231**/
6232static uint16_t
6233lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6234 struct lpfc_sglq *sglq)
6235{
6236 uint16_t xritag = NO_XRI;
6237 struct ulp_bde64 *bpl = NULL;
6238 struct ulp_bde64 bde;
6239 struct sli4_sge *sgl = NULL;
6240 IOCB_t *icmd;
6241 int numBdes = 0;
6242 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05006243 uint32_t offset = 0; /* accumulated offset in the sg request list */
6244 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04006245
6246 if (!piocbq || !sglq)
6247 return xritag;
6248
6249 sgl = (struct sli4_sge *)sglq->sgl;
6250 icmd = &piocbq->iocb;
6251 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6252 numBdes = icmd->un.genreq64.bdl.bdeSize /
6253 sizeof(struct ulp_bde64);
6254 /* The addrHigh and addrLow fields within the IOCB
6255 * have not been byteswapped yet so there is no
6256 * need to swap them back.
6257 */
6258 bpl = (struct ulp_bde64 *)
6259 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6260
6261 if (!bpl)
6262 return xritag;
6263
6264 for (i = 0; i < numBdes; i++) {
6265 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05006266 sgl->addr_hi = bpl->addrHigh;
6267 sgl->addr_lo = bpl->addrLow;
6268
James Smart4f774512009-05-22 14:52:35 -04006269 if ((i+1) == numBdes)
6270 bf_set(lpfc_sli4_sge_last, sgl, 1);
6271 else
6272 bf_set(lpfc_sli4_sge_last, sgl, 0);
6273 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006274 /* swap the size field back to the cpu so we
6275 * can assign it to the sgl.
6276 */
6277 bde.tus.w = le32_to_cpu(bpl->tus.w);
6278 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05006279 /* The offsets in the sgl need to be accumulated
6280 * separately for the request and reply lists.
6281 * The request is always first, the reply follows.
6282 */
6283 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6284 /* add up the reply sg entries */
6285 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6286 inbound++;
6287 /* first inbound? reset the offset */
6288 if (inbound == 1)
6289 offset = 0;
6290 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6291 offset += bde.tus.f.bdeSize;
6292 }
James Smart4f774512009-05-22 14:52:35 -04006293 bpl++;
6294 sgl++;
6295 }
6296 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6297 /* The addrHigh and addrLow fields of the BDE have not
6298 * been byteswapped yet so they need to be swapped
6299 * before putting them in the sgl.
6300 */
6301 sgl->addr_hi =
6302 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6303 sgl->addr_lo =
6304 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04006305 bf_set(lpfc_sli4_sge_last, sgl, 1);
6306 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006307 sgl->sge_len =
6308 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04006309 }
6310 return sglq->sli4_xritag;
6311}
6312
6313/**
6314 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6315 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04006316 *
James Smarta93ff372010-10-22 11:06:08 -04006317 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04006318 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6319 * held.
James Smart4f774512009-05-22 14:52:35 -04006320 *
6321 * Return: index into SLI4 fast-path FCP queue index.
6322 **/
6323static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04006324lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04006325{
James Smart8fa38512009-07-19 10:01:03 -04006326 ++phba->fcp_qidx;
6327 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6328 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04006329
James Smart8fa38512009-07-19 10:01:03 -04006330 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04006331}
6332
6333/**
6334 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6335 * @phba: Pointer to HBA context object.
6336 * @piocb: Pointer to command iocb.
6337 * @wqe: Pointer to the work queue entry.
6338 *
6339 * This routine converts the iocb command to its Work Queue Entry
6340 * equivalent. The wqe pointer should not have any fields set when
6341 * this routine is called because it will memcpy over them.
6342 * This routine does not set the CQ_ID or the WQEC bits in the
6343 * wqe.
6344 *
6345 * Returns: 0 = Success, IOCB_ERROR = Failure.
6346 **/
6347static int
6348lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6349 union lpfc_wqe *wqe)
6350{
James Smart5ffc2662009-11-18 15:39:44 -05006351 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04006352 uint8_t ct = 0;
6353 uint32_t fip;
6354 uint32_t abort_tag;
6355 uint8_t command_type = ELS_COMMAND_NON_FIP;
6356 uint8_t cmnd;
6357 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04006358 uint16_t abrt_iotag;
6359 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04006360 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04006361 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05006362 int numBdes, i;
6363 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04006364
James Smart45ed1192009-10-02 15:17:02 -04006365 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04006366 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04006367 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04006368 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05006369 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04006370 command_type = ELS_COMMAND_FIP;
6371 else
6372 command_type = ELS_COMMAND_NON_FIP;
6373
James Smart4f774512009-05-22 14:52:35 -04006374 /* Some of the fields are in the right position already */
6375 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6376 abort_tag = (uint32_t) iocbq->iotag;
6377 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04006378 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04006379 /* words0-2 bpl convert bde */
6380 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05006381 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6382 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04006383 bpl = (struct ulp_bde64 *)
6384 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6385 if (!bpl)
6386 return IOCB_ERROR;
6387
6388 /* Should already be byte swapped. */
6389 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6390 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6391 /* swap the size field back to the cpu so we
6392 * can assign it to the sgl.
6393 */
6394 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006395 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6396 total_len = 0;
6397 for (i = 0; i < numBdes; i++) {
6398 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6399 total_len += bde.tus.f.bdeSize;
6400 }
James Smart4f774512009-05-22 14:52:35 -04006401 } else
James Smart5ffc2662009-11-18 15:39:44 -05006402 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006403
6404 iocbq->iocb.ulpIoTag = iocbq->iotag;
6405 cmnd = iocbq->iocb.ulpCommand;
6406
6407 switch (iocbq->iocb.ulpCommand) {
6408 case CMD_ELS_REQUEST64_CR:
6409 if (!iocbq->iocb.ulpLe) {
6410 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6411 "2007 Only Limited Edition cmd Format"
6412 " supported 0x%x\n",
6413 iocbq->iocb.ulpCommand);
6414 return IOCB_ERROR;
6415 }
James Smart5ffc2662009-11-18 15:39:44 -05006416 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006417 /* Els_reguest64 has a TMO */
6418 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6419 iocbq->iocb.ulpTimeout);
6420 /* Need a VF for word 4 set the vf bit*/
6421 bf_set(els_req64_vf, &wqe->els_req, 0);
6422 /* And a VFID for word 12 */
6423 bf_set(els_req64_vfid, &wqe->els_req, 0);
6424 /*
6425 * Set ct field to 3, indicates that the context_tag field
6426 * contains the FCFI and remote N_Port_ID is
6427 * in word 5.
6428 */
James Smart4f774512009-05-22 14:52:35 -04006429 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04006430 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6431 iocbq->iocb.ulpContext);
6432 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6433 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006434 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006435 if (command_type == ELS_COMMAND_FIP) {
6436 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6437 >> LPFC_FIP_ELS_ID_SHIFT);
6438 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006439 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6440 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6441 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6442 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6443 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6444 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006445 break;
James Smart5ffc2662009-11-18 15:39:44 -05006446 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04006447 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6448 iocbq->iocb.un.ulpWord[3]);
6449 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6450 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006451 /* The entire sequence is transmitted for this IOCB */
6452 xmit_len = total_len;
6453 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006454 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006455 /* word3 iocb=io_tag32 wqe=reserved */
6456 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04006457 /* word4 relative_offset memcpy */
6458 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006459 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6460 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6461 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6462 LPFC_WQE_IOD_WRITE);
6463 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6464 LPFC_WQE_LENLOC_WORD12);
6465 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006466 wqe->xmit_sequence.xmit_len = xmit_len;
6467 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006468 break;
6469 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04006470 /* word3 iocb=iotag32 wqe=seq_payload_len */
6471 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006472 /* word4 iocb=rsvd wqe=rsvd */
6473 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6474 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04006475 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006476 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04006477 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6478 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6479 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6480 LPFC_WQE_LENLOC_WORD3);
6481 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006482 break;
6483 case CMD_FCP_IWRITE64_CR:
6484 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04006485 /* word3 iocb=iotag wqe=payload_offset_len */
6486 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6487 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05006488 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04006489 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6490 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6491 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6492 iocbq->iocb.ulpFCP2Rcvy);
6493 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6494 /* Always open the exchange */
6495 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6496 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6497 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6498 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6499 LPFC_WQE_LENLOC_WORD4);
6500 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6501 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6502 break;
6503 case CMD_FCP_IREAD64_CR:
6504 /* word3 iocb=iotag wqe=payload_offset_len */
6505 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6506 wqe->fcp_iread.payload_offset_len =
6507 xmit_len + sizeof(struct fcp_rsp);
6508 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6509 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6510 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6511 iocbq->iocb.ulpFCP2Rcvy);
6512 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04006513 /* Always open the exchange */
6514 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04006515 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6516 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6517 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6518 LPFC_WQE_LENLOC_WORD4);
6519 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6520 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6521 break;
James Smartf1126682009-06-10 17:22:44 -04006522 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006523 /* word3 iocb=IO_TAG wqe=reserved */
6524 wqe->fcp_icmd.rsrvd3 = 0;
6525 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04006526 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04006527 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6528 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6529 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6530 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6531 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6532 LPFC_WQE_LENLOC_NONE);
6533 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006534 break;
6535 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05006536 /* For this command calculate the xmit length of the
6537 * request bde.
6538 */
6539 xmit_len = 0;
6540 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6541 sizeof(struct ulp_bde64);
6542 for (i = 0; i < numBdes; i++) {
6543 if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6544 break;
6545 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6546 xmit_len += bde.tus.f.bdeSize;
6547 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006548 /* word3 iocb=IO_TAG wqe=request_payload_len */
6549 wqe->gen_req.request_payload_len = xmit_len;
6550 /* word4 iocb=parameter wqe=relative_offset memcpy */
6551 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04006552 /* word6 context tag copied in memcpy */
6553 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6554 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6556 "2015 Invalid CT %x command 0x%x\n",
6557 ct, iocbq->iocb.ulpCommand);
6558 return IOCB_ERROR;
6559 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006560 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6561 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6562 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6563 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6564 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6565 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6566 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6567 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006568 command_type = OTHER_COMMAND;
6569 break;
6570 case CMD_XMIT_ELS_RSP64_CX:
6571 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006572 /* word3 iocb=iotag32 wqe=response_payload_len */
6573 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006574 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04006575 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04006576 /* word5 iocb=rsvd wge=did */
6577 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6578 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04006579 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6580 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6581 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6582 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6583 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04006584 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04006585 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006586 iocbq->vport->vpi + phba->vpi_base);
James Smartf0d9bcc2010-10-22 11:07:09 -04006587 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6588 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6589 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6590 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6591 LPFC_WQE_LENLOC_WORD3);
6592 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006593 command_type = OTHER_COMMAND;
6594 break;
6595 case CMD_CLOSE_XRI_CN:
6596 case CMD_ABORT_XRI_CN:
6597 case CMD_ABORT_XRI_CX:
6598 /* words 0-2 memcpy should be 0 rserved */
6599 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006600 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6601 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6602 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6603 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6604 } else
6605 fip = 0;
6606
6607 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006608 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006609 * The link is down, or the command was ELS_FIP
6610 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006611 * on the wire.
6612 */
6613 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6614 else
6615 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6616 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04006617 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6618 wqe->abort_cmd.rsrvd5 = 0;
6619 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006620 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6621 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006622 /*
6623 * The abort handler will send us CMD_ABORT_XRI_CN or
6624 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6625 */
James Smartf0d9bcc2010-10-22 11:07:09 -04006626 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6627 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6628 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6629 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04006630 cmnd = CMD_ABORT_XRI_CX;
6631 command_type = OTHER_COMMAND;
6632 xritag = 0;
6633 break;
James Smart6669f9b2009-10-02 15:16:45 -04006634 case CMD_XMIT_BLS_RSP64_CX:
6635 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6636 * we re-construct this WQE here based on information in
6637 * iocbq from scratch.
6638 */
6639 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006640 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006641 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006642 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6643 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6644 LPFC_ABTS_UNSOL_INT) {
6645 /* ABTS sent by initiator to CT exchange, the
6646 * RX_ID field will be filled with the newly
6647 * allocated responder XRI.
6648 */
6649 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6650 iocbq->sli4_xritag);
6651 } else {
6652 /* ABTS sent by responder to CT exchange, the
6653 * RX_ID field will be filled with the responder
6654 * RX_ID from ABTS.
6655 */
6656 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6657 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6658 }
James Smart6669f9b2009-10-02 15:16:45 -04006659 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6660 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6661 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6662 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04006663 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6664 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6665 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04006666 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6667 command_type = OTHER_COMMAND;
6668 break;
James Smart4f774512009-05-22 14:52:35 -04006669 case CMD_XRI_ABORTED_CX:
6670 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04006671 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6672 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6673 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6674 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6675 default:
6676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6677 "2014 Invalid command 0x%x\n",
6678 iocbq->iocb.ulpCommand);
6679 return IOCB_ERROR;
6680 break;
James Smart4f774512009-05-22 14:52:35 -04006681 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006682 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6683 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6684 wqe->generic.wqe_com.abort_tag = abort_tag;
6685 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6686 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6687 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6688 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04006689 return 0;
6690}
6691
6692/**
6693 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6694 * @phba: Pointer to HBA context object.
6695 * @ring_number: SLI ring number to issue iocb on.
6696 * @piocb: Pointer to command iocb.
6697 * @flag: Flag indicating if this command can be put into txq.
6698 *
6699 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6700 * an iocb command to an HBA with SLI-4 interface spec.
6701 *
6702 * This function is called with hbalock held. The function will return success
6703 * after it successfully submit the iocb to firmware or after adding to the
6704 * txq.
6705 **/
6706static int
6707__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6708 struct lpfc_iocbq *piocb, uint32_t flag)
6709{
6710 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006711 union lpfc_wqe wqe;
6712 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006713
6714 if (piocb->sli4_xritag == NO_XRI) {
6715 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006716 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006717 sglq = NULL;
6718 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006719 if (pring->txq_cnt) {
6720 if (!(flag & SLI_IOCB_RET_IOCB)) {
6721 __lpfc_sli_ringtx_put(phba,
6722 pring, piocb);
6723 return IOCB_SUCCESS;
6724 } else {
6725 return IOCB_BUSY;
6726 }
6727 } else {
James Smart19ca7602010-11-20 23:11:55 -05006728 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04006729 if (!sglq) {
6730 if (!(flag & SLI_IOCB_RET_IOCB)) {
6731 __lpfc_sli_ringtx_put(phba,
6732 pring,
6733 piocb);
6734 return IOCB_SUCCESS;
6735 } else
6736 return IOCB_BUSY;
6737 }
6738 }
James Smart4f774512009-05-22 14:52:35 -04006739 }
6740 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6741 sglq = NULL; /* These IO's already have an XRI and
6742 * a mapped sgl.
6743 */
6744 } else {
6745 /* This is a continuation of a commandi,(CX) so this
6746 * sglq is on the active list
6747 */
6748 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6749 if (!sglq)
6750 return IOCB_ERROR;
6751 }
6752
6753 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006754 piocb->sli4_xritag = sglq->sli4_xritag;
6755
6756 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006757 return IOCB_ERROR;
6758 }
6759
6760 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6761 return IOCB_ERROR;
6762
James Smart341af102010-01-26 23:07:37 -05006763 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6764 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006765 /*
6766 * For FCP command IOCB, get a new WQ index to distribute
6767 * WQE across the WQsr. On the other hand, for abort IOCB,
6768 * it carries the same WQ index to the original command
6769 * IOCB.
6770 */
James Smart341af102010-01-26 23:07:37 -05006771 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006772 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6773 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6774 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006775 return IOCB_ERROR;
6776 } else {
6777 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6778 return IOCB_ERROR;
6779 }
6780 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6781
6782 return 0;
6783}
6784
6785/**
James Smart3772a992009-05-22 14:50:54 -04006786 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6787 *
6788 * This routine wraps the actual lockless version for issusing IOCB function
6789 * pointer from the lpfc_hba struct.
6790 *
6791 * Return codes:
6792 * IOCB_ERROR - Error
6793 * IOCB_SUCCESS - Success
6794 * IOCB_BUSY - Busy
6795 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006796int
James Smart3772a992009-05-22 14:50:54 -04006797__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6798 struct lpfc_iocbq *piocb, uint32_t flag)
6799{
6800 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6801}
6802
6803/**
6804 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6805 * @phba: The hba struct for which this call is being executed.
6806 * @dev_grp: The HBA PCI-Device group number.
6807 *
6808 * This routine sets up the SLI interface API function jump table in @phba
6809 * struct.
6810 * Returns: 0 - success, -ENODEV - failure.
6811 **/
6812int
6813lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6814{
6815
6816 switch (dev_grp) {
6817 case LPFC_PCI_DEV_LP:
6818 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6819 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6820 break;
James Smart4f774512009-05-22 14:52:35 -04006821 case LPFC_PCI_DEV_OC:
6822 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6823 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6824 break;
James Smart3772a992009-05-22 14:50:54 -04006825 default:
6826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6827 "1419 Invalid HBA PCI-device group: 0x%x\n",
6828 dev_grp);
6829 return -ENODEV;
6830 break;
6831 }
6832 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6833 return 0;
6834}
James Smart92d7f7b2007-06-17 19:56:38 -05006835
James Smarte59058c2008-08-24 21:49:00 -04006836/**
James Smart3621a712009-04-06 18:47:14 -04006837 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006838 * @phba: Pointer to HBA context object.
6839 * @pring: Pointer to driver SLI ring object.
6840 * @piocb: Pointer to command iocb.
6841 * @flag: Flag indicating if this command can be put into txq.
6842 *
6843 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6844 * function. This function gets the hbalock and calls
6845 * __lpfc_sli_issue_iocb function and will return the error returned
6846 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6847 * functions which do not hold hbalock.
6848 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006849int
James Smart3772a992009-05-22 14:50:54 -04006850lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006851 struct lpfc_iocbq *piocb, uint32_t flag)
6852{
6853 unsigned long iflags;
6854 int rc;
6855
6856 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006857 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006858 spin_unlock_irqrestore(&phba->hbalock, iflags);
6859
6860 return rc;
6861}
6862
James Smarte59058c2008-08-24 21:49:00 -04006863/**
James Smart3621a712009-04-06 18:47:14 -04006864 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006865 * @phba: Pointer to HBA context object.
6866 *
6867 * This function is called while driver attaches with the
6868 * HBA to setup the extra ring. The extra ring is used
6869 * only when driver needs to support target mode functionality
6870 * or IP over FC functionalities.
6871 *
6872 * This function is called with no lock held.
6873 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006874static int
6875lpfc_extra_ring_setup( struct lpfc_hba *phba)
6876{
6877 struct lpfc_sli *psli;
6878 struct lpfc_sli_ring *pring;
6879
6880 psli = &phba->sli;
6881
6882 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006883
6884 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006885 pring = &psli->ring[psli->fcp_ring];
6886 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6887 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6888 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6889 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6890
James Smarta4bc3372006-12-02 13:34:16 -05006891 /* and give them to the extra ring */
6892 pring = &psli->ring[psli->extra_ring];
6893
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006894 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6895 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6896 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6897 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6898
6899 /* Setup default profile for this ring */
6900 pring->iotag_max = 4096;
6901 pring->num_mask = 1;
6902 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006903 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6904 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006905 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6906 return 0;
6907}
6908
James Smarte59058c2008-08-24 21:49:00 -04006909/**
James Smart3621a712009-04-06 18:47:14 -04006910 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006911 * @phba: Pointer to HBA context object.
6912 * @pring: Pointer to driver SLI ring object.
6913 * @iocbq: Pointer to iocb object.
6914 *
6915 * This function is called by the slow ring event handler
6916 * function when there is an ASYNC event iocb in the ring.
6917 * This function is called with no lock held.
6918 * Currently this function handles only temperature related
6919 * ASYNC events. The function decodes the temperature sensor
6920 * event message and posts events for the management applications.
6921 **/
James Smart98c9ea52007-10-27 13:37:33 -04006922static void
James Smart57127f12007-10-27 13:37:05 -04006923lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6924 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6925{
6926 IOCB_t *icmd;
6927 uint16_t evt_code;
6928 uint16_t temp;
6929 struct temp_event temp_event_data;
6930 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006931 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006932
6933 icmd = &iocbq->iocb;
6934 evt_code = icmd->un.asyncstat.evt_code;
6935 temp = icmd->ulpContext;
6936
6937 if ((evt_code != ASYNC_TEMP_WARN) &&
6938 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006939 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006940 lpfc_printf_log(phba,
6941 KERN_ERR,
6942 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006943 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006944 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006945 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6946 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6947 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6948 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006949 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006950 icmd->un.asyncstat.evt_code,
6951 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6952 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6953 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6954 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6955
James Smart57127f12007-10-27 13:37:05 -04006956 return;
6957 }
6958 temp_event_data.data = (uint32_t)temp;
6959 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6960 if (evt_code == ASYNC_TEMP_WARN) {
6961 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6962 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006963 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006964 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006965 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006966 "corrective action. temperature : %d Celsius\n",
6967 temp);
6968 }
6969 if (evt_code == ASYNC_TEMP_SAFE) {
6970 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6971 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006972 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006973 LOG_TEMP,
6974 "0340 Adapter temperature is OK now. "
6975 "temperature : %d Celsius\n",
6976 temp);
6977 }
6978
6979 /* Send temperature change event to applications */
6980 shost = lpfc_shost_from_vport(phba->pport);
6981 fc_host_post_vendor_event(shost, fc_get_event_number(),
6982 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006983 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006984
6985}
6986
6987
James Smarte59058c2008-08-24 21:49:00 -04006988/**
James Smart3621a712009-04-06 18:47:14 -04006989 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006990 * @phba: Pointer to HBA context object.
6991 *
6992 * lpfc_sli_setup sets up rings of the SLI interface with
6993 * number of iocbs per ring and iotags. This function is
6994 * called while driver attach to the HBA and before the
6995 * interrupts are enabled. So there is no need for locking.
6996 *
6997 * This function always returns 0.
6998 **/
dea31012005-04-17 16:05:31 -05006999int
7000lpfc_sli_setup(struct lpfc_hba *phba)
7001{
James Smarted957682007-06-17 19:56:37 -05007002 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05007003 struct lpfc_sli *psli = &phba->sli;
7004 struct lpfc_sli_ring *pring;
7005
7006 psli->num_rings = MAX_CONFIGURED_RINGS;
7007 psli->sli_flag = 0;
7008 psli->fcp_ring = LPFC_FCP_RING;
7009 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05007010 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05007011
James Bottomley604a3e32005-10-29 10:28:33 -05007012 psli->iocbq_lookup = NULL;
7013 psli->iocbq_lookup_len = 0;
7014 psli->last_iotag = 0;
7015
dea31012005-04-17 16:05:31 -05007016 for (i = 0; i < psli->num_rings; i++) {
7017 pring = &psli->ring[i];
7018 switch (i) {
7019 case LPFC_FCP_RING: /* ring 0 - FCP */
7020 /* numCiocb and numRiocb are used in config_port */
7021 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7022 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7023 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7024 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7025 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7026 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007027 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007028 SLI3_IOCB_CMD_SIZE :
7029 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007030 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007031 SLI3_IOCB_RSP_SIZE :
7032 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007033 pring->iotag_ctr = 0;
7034 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05007035 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05007036 pring->fast_iotag = pring->iotag_max;
7037 pring->num_mask = 0;
7038 break;
James Smarta4bc3372006-12-02 13:34:16 -05007039 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05007040 /* numCiocb and numRiocb are used in config_port */
7041 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7042 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007043 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007044 SLI3_IOCB_CMD_SIZE :
7045 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007046 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007047 SLI3_IOCB_RSP_SIZE :
7048 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05007049 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05007050 pring->num_mask = 0;
7051 break;
7052 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
7053 /* numCiocb and numRiocb are used in config_port */
7054 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7055 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007056 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007057 SLI3_IOCB_CMD_SIZE :
7058 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007059 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007060 SLI3_IOCB_RSP_SIZE :
7061 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007062 pring->fast_iotag = 0;
7063 pring->iotag_ctr = 0;
7064 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04007065 pring->lpfc_sli_rcv_async_status =
7066 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04007067 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05007068 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04007069 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7070 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007071 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007072 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007073 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04007074 pring->prt[1].rctl = FC_RCTL_ELS_REP;
7075 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007076 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007077 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007078 pring->prt[2].profile = 0; /* Mask 2 */
7079 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04007080 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05007081 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007082 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007083 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007084 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05007085 pring->prt[3].profile = 0; /* Mask 3 */
7086 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04007087 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05007088 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007089 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007090 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007091 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04007092 /* abort unsolicited sequence */
7093 pring->prt[4].profile = 0; /* Mask 4 */
7094 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7095 pring->prt[4].type = FC_TYPE_BLS;
7096 pring->prt[4].lpfc_sli_rcv_unsol_event =
7097 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05007098 break;
7099 }
James Smarted957682007-06-17 19:56:37 -05007100 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05007101 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05007102 }
James Smarted957682007-06-17 19:56:37 -05007103 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05007104 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04007105 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7106 "SLI2 SLIM Data: x%x x%lx\n",
7107 phba->brd_no, totiocbsize,
7108 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05007109 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007110 if (phba->cfg_multi_ring_support == 2)
7111 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05007112
7113 return 0;
7114}
7115
James Smarte59058c2008-08-24 21:49:00 -04007116/**
James Smart3621a712009-04-06 18:47:14 -04007117 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04007118 * @phba: Pointer to HBA context object.
7119 *
7120 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7121 * ring. This function also initializes ring indices of each ring.
7122 * This function is called during the initialization of the SLI
7123 * interface of an HBA.
7124 * This function is called with no lock held and always returns
7125 * 1.
7126 **/
dea31012005-04-17 16:05:31 -05007127int
James Smart2e0fef82007-06-17 19:56:36 -05007128lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007129{
7130 struct lpfc_sli *psli;
7131 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05007132 int i;
dea31012005-04-17 16:05:31 -05007133
7134 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05007135 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007136 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05007137 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05007138 /* Initialize list headers for txq and txcmplq as double linked lists */
7139 for (i = 0; i < psli->num_rings; i++) {
7140 pring = &psli->ring[i];
7141 pring->ringno = i;
7142 pring->next_cmdidx = 0;
7143 pring->local_getidx = 0;
7144 pring->cmdidx = 0;
7145 INIT_LIST_HEAD(&pring->txq);
7146 INIT_LIST_HEAD(&pring->txcmplq);
7147 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05007148 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05007149 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05007150 }
James Smart2e0fef82007-06-17 19:56:36 -05007151 spin_unlock_irq(&phba->hbalock);
7152 return 1;
dea31012005-04-17 16:05:31 -05007153}
7154
James Smarte59058c2008-08-24 21:49:00 -04007155/**
James Smart04c68492009-05-22 14:52:52 -04007156 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7157 * @phba: Pointer to HBA context object.
7158 *
7159 * This routine flushes the mailbox command subsystem. It will unconditionally
7160 * flush all the mailbox commands in the three possible stages in the mailbox
7161 * command sub-system: pending mailbox command queue; the outstanding mailbox
7162 * command; and completed mailbox command queue. It is caller's responsibility
7163 * to make sure that the driver is in the proper state to flush the mailbox
7164 * command sub-system. Namely, the posting of mailbox commands into the
7165 * pending mailbox command queue from the various clients must be stopped;
7166 * either the HBA is in a state that it will never works on the outstanding
7167 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7168 * mailbox command has been completed.
7169 **/
7170static void
7171lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7172{
7173 LIST_HEAD(completions);
7174 struct lpfc_sli *psli = &phba->sli;
7175 LPFC_MBOXQ_t *pmb;
7176 unsigned long iflag;
7177
7178 /* Flush all the mailbox commands in the mbox system */
7179 spin_lock_irqsave(&phba->hbalock, iflag);
7180 /* The pending mailbox command queue */
7181 list_splice_init(&phba->sli.mboxq, &completions);
7182 /* The outstanding active mailbox command */
7183 if (psli->mbox_active) {
7184 list_add_tail(&psli->mbox_active->list, &completions);
7185 psli->mbox_active = NULL;
7186 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7187 }
7188 /* The completed mailbox command queue */
7189 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7190 spin_unlock_irqrestore(&phba->hbalock, iflag);
7191
7192 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7193 while (!list_empty(&completions)) {
7194 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7195 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7196 if (pmb->mbox_cmpl)
7197 pmb->mbox_cmpl(phba, pmb);
7198 }
7199}
7200
7201/**
James Smart3621a712009-04-06 18:47:14 -04007202 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04007203 * @vport: Pointer to virtual port object.
7204 *
7205 * lpfc_sli_host_down is called to clean up the resources
7206 * associated with a vport before destroying virtual
7207 * port data structures.
7208 * This function does following operations:
7209 * - Free discovery resources associated with this virtual
7210 * port.
7211 * - Free iocbs associated with this virtual port in
7212 * the txq.
7213 * - Send abort for all iocb commands associated with this
7214 * vport in txcmplq.
7215 *
7216 * This function is called with no lock held and always returns 1.
7217 **/
dea31012005-04-17 16:05:31 -05007218int
James Smart92d7f7b2007-06-17 19:56:38 -05007219lpfc_sli_host_down(struct lpfc_vport *vport)
7220{
James Smart858c9f62007-06-17 19:56:39 -05007221 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007222 struct lpfc_hba *phba = vport->phba;
7223 struct lpfc_sli *psli = &phba->sli;
7224 struct lpfc_sli_ring *pring;
7225 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05007226 int i;
7227 unsigned long flags = 0;
7228 uint16_t prev_pring_flag;
7229
7230 lpfc_cleanup_discovery_resources(vport);
7231
7232 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007233 for (i = 0; i < psli->num_rings; i++) {
7234 pring = &psli->ring[i];
7235 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04007236 /* Only slow rings */
7237 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007238 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007239 /* Set the lpfc data pending flag */
7240 set_bit(LPFC_DATA_READY, &phba->data_flags);
7241 }
James Smart92d7f7b2007-06-17 19:56:38 -05007242 /*
7243 * Error everything on the txq since these iocbs have not been
7244 * given to the FW yet.
7245 */
James Smart92d7f7b2007-06-17 19:56:38 -05007246 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7247 if (iocb->vport != vport)
7248 continue;
James Smart858c9f62007-06-17 19:56:39 -05007249 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007250 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05007251 }
7252
7253 /* Next issue ABTS for everything on the txcmplq */
7254 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7255 list) {
7256 if (iocb->vport != vport)
7257 continue;
7258 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7259 }
7260
7261 pring->flag = prev_pring_flag;
7262 }
7263
7264 spin_unlock_irqrestore(&phba->hbalock, flags);
7265
James Smarta257bf92009-04-06 18:48:10 -04007266 /* Cancel all the IOCBs from the completions list */
7267 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7268 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05007269 return 1;
7270}
7271
James Smarte59058c2008-08-24 21:49:00 -04007272/**
James Smart3621a712009-04-06 18:47:14 -04007273 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04007274 * @phba: Pointer to HBA context object.
7275 *
7276 * This function cleans up all iocb, buffers, mailbox commands
7277 * while shutting down the HBA. This function is called with no
7278 * lock held and always returns 1.
7279 * This function does the following to cleanup driver resources:
7280 * - Free discovery resources for each virtual port
7281 * - Cleanup any pending fabric iocbs
7282 * - Iterate through the iocb txq and free each entry
7283 * in the list.
7284 * - Free up any buffer posted to the HBA
7285 * - Free mailbox commands in the mailbox queue.
7286 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007287int
James Smart2e0fef82007-06-17 19:56:36 -05007288lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007289{
James Smart2534ba72007-04-25 09:52:20 -04007290 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05007291 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007292 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05007293 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05007294 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04007295 int i;
7296
7297 /* Shutdown the mailbox command sub-system */
7298 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05007299
dea31012005-04-17 16:05:31 -05007300 lpfc_hba_down_prep(phba);
7301
James Smart92d7f7b2007-06-17 19:56:38 -05007302 lpfc_fabric_abort_hba(phba);
7303
James Smart2e0fef82007-06-17 19:56:36 -05007304 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007305 for (i = 0; i < psli->num_rings; i++) {
7306 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04007307 /* Only slow rings */
7308 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007309 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007310 /* Set the lpfc data pending flag */
7311 set_bit(LPFC_DATA_READY, &phba->data_flags);
7312 }
dea31012005-04-17 16:05:31 -05007313
7314 /*
7315 * Error everything on the txq since these iocbs have not been
7316 * given to the FW yet.
7317 */
James Smart2534ba72007-04-25 09:52:20 -04007318 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05007319 pring->txq_cnt = 0;
7320
dea31012005-04-17 16:05:31 -05007321 }
James Smart2e0fef82007-06-17 19:56:36 -05007322 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007323
James Smarta257bf92009-04-06 18:48:10 -04007324 /* Cancel all the IOCBs from the completions list */
7325 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7326 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04007327
James Smart0ff10d42008-01-11 01:52:36 -05007328 spin_lock_irqsave(&phba->hbalock, flags);
7329 list_splice_init(&phba->elsbuf, &completions);
7330 phba->elsbuf_cnt = 0;
7331 phba->elsbuf_prev_cnt = 0;
7332 spin_unlock_irqrestore(&phba->hbalock, flags);
7333
7334 while (!list_empty(&completions)) {
7335 list_remove_head(&completions, buf_ptr,
7336 struct lpfc_dmabuf, list);
7337 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7338 kfree(buf_ptr);
7339 }
7340
dea31012005-04-17 16:05:31 -05007341 /* Return any active mbox cmds */
7342 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05007343
James Smartda0436e2009-05-22 14:51:39 -04007344 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007345 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04007346 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007347
James Smartda0436e2009-05-22 14:51:39 -04007348 return 1;
7349}
James Smart92d7f7b2007-06-17 19:56:38 -05007350
James Smartda0436e2009-05-22 14:51:39 -04007351/**
James Smart3621a712009-04-06 18:47:14 -04007352 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04007353 * @srcp: Source memory pointer.
7354 * @destp: Destination memory pointer.
7355 * @cnt: Number of words required to be copied.
7356 *
7357 * This function is used for copying data between driver memory
7358 * and the SLI memory. This function also changes the endianness
7359 * of each word if native endianness is different from SLI
7360 * endianness. This function can be called with or without
7361 * lock.
7362 **/
dea31012005-04-17 16:05:31 -05007363void
7364lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7365{
7366 uint32_t *src = srcp;
7367 uint32_t *dest = destp;
7368 uint32_t ldata;
7369 int i;
7370
7371 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7372 ldata = *src;
7373 ldata = le32_to_cpu(ldata);
7374 *dest = ldata;
7375 src++;
7376 dest++;
7377 }
7378}
7379
James Smarte59058c2008-08-24 21:49:00 -04007380
7381/**
James Smarta0c87cb2009-07-19 10:01:10 -04007382 * lpfc_sli_bemem_bcopy - SLI memory copy function
7383 * @srcp: Source memory pointer.
7384 * @destp: Destination memory pointer.
7385 * @cnt: Number of words required to be copied.
7386 *
7387 * This function is used for copying data between a data structure
7388 * with big endian representation to local endianness.
7389 * This function can be called with or without lock.
7390 **/
7391void
7392lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7393{
7394 uint32_t *src = srcp;
7395 uint32_t *dest = destp;
7396 uint32_t ldata;
7397 int i;
7398
7399 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7400 ldata = *src;
7401 ldata = be32_to_cpu(ldata);
7402 *dest = ldata;
7403 src++;
7404 dest++;
7405 }
7406}
7407
7408/**
James Smart3621a712009-04-06 18:47:14 -04007409 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04007410 * @phba: Pointer to HBA context object.
7411 * @pring: Pointer to driver SLI ring object.
7412 * @mp: Pointer to driver buffer object.
7413 *
7414 * This function is called with no lock held.
7415 * It always return zero after adding the buffer to the postbufq
7416 * buffer list.
7417 **/
dea31012005-04-17 16:05:31 -05007418int
James Smart2e0fef82007-06-17 19:56:36 -05007419lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7420 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007421{
7422 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7423 later */
James Smart2e0fef82007-06-17 19:56:36 -05007424 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007425 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007426 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007427 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007428 return 0;
7429}
7430
James Smarte59058c2008-08-24 21:49:00 -04007431/**
James Smart3621a712009-04-06 18:47:14 -04007432 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007433 * @phba: Pointer to HBA context object.
7434 *
7435 * When HBQ is enabled, buffers are searched based on tags. This function
7436 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7437 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7438 * does not conflict with tags of buffer posted for unsolicited events.
7439 * The function returns the allocated tag. The function is called with
7440 * no locks held.
7441 **/
James Smart76bb24e2007-10-27 13:38:00 -04007442uint32_t
7443lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7444{
7445 spin_lock_irq(&phba->hbalock);
7446 phba->buffer_tag_count++;
7447 /*
7448 * Always set the QUE_BUFTAG_BIT to distiguish between
7449 * a tag assigned by HBQ.
7450 */
7451 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7452 spin_unlock_irq(&phba->hbalock);
7453 return phba->buffer_tag_count;
7454}
7455
James Smarte59058c2008-08-24 21:49:00 -04007456/**
James Smart3621a712009-04-06 18:47:14 -04007457 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007458 * @phba: Pointer to HBA context object.
7459 * @pring: Pointer to driver SLI ring object.
7460 * @tag: Buffer tag.
7461 *
7462 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7463 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7464 * iocb is posted to the response ring with the tag of the buffer.
7465 * This function searches the pring->postbufq list using the tag
7466 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7467 * iocb. If the buffer is found then lpfc_dmabuf object of the
7468 * buffer is returned to the caller else NULL is returned.
7469 * This function is called with no lock held.
7470 **/
James Smart76bb24e2007-10-27 13:38:00 -04007471struct lpfc_dmabuf *
7472lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7473 uint32_t tag)
7474{
7475 struct lpfc_dmabuf *mp, *next_mp;
7476 struct list_head *slp = &pring->postbufq;
7477
7478 /* Search postbufq, from the begining, looking for a match on tag */
7479 spin_lock_irq(&phba->hbalock);
7480 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7481 if (mp->buffer_tag == tag) {
7482 list_del_init(&mp->list);
7483 pring->postbufq_cnt--;
7484 spin_unlock_irq(&phba->hbalock);
7485 return mp;
7486 }
7487 }
7488
7489 spin_unlock_irq(&phba->hbalock);
7490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007491 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007492 "ring %d Data x%lx x%p x%p x%x\n",
7493 pring->ringno, (unsigned long) tag,
7494 slp->next, slp->prev, pring->postbufq_cnt);
7495
7496 return NULL;
7497}
dea31012005-04-17 16:05:31 -05007498
James Smarte59058c2008-08-24 21:49:00 -04007499/**
James Smart3621a712009-04-06 18:47:14 -04007500 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007501 * @phba: Pointer to HBA context object.
7502 * @pring: Pointer to driver SLI ring object.
7503 * @phys: DMA address of the buffer.
7504 *
7505 * This function searches the buffer list using the dma_address
7506 * of unsolicited event to find the driver's lpfc_dmabuf object
7507 * corresponding to the dma_address. The function returns the
7508 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7509 * This function is called by the ct and els unsolicited event
7510 * handlers to get the buffer associated with the unsolicited
7511 * event.
7512 *
7513 * This function is called with no lock held.
7514 **/
dea31012005-04-17 16:05:31 -05007515struct lpfc_dmabuf *
7516lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7517 dma_addr_t phys)
7518{
7519 struct lpfc_dmabuf *mp, *next_mp;
7520 struct list_head *slp = &pring->postbufq;
7521
7522 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007523 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007524 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7525 if (mp->phys == phys) {
7526 list_del_init(&mp->list);
7527 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007528 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007529 return mp;
7530 }
7531 }
7532
James Smart2e0fef82007-06-17 19:56:36 -05007533 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007535 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007536 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007537 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007538 slp->next, slp->prev, pring->postbufq_cnt);
7539 return NULL;
7540}
7541
James Smarte59058c2008-08-24 21:49:00 -04007542/**
James Smart3621a712009-04-06 18:47:14 -04007543 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007544 * @phba: Pointer to HBA context object.
7545 * @cmdiocb: Pointer to driver command iocb object.
7546 * @rspiocb: Pointer to driver response iocb object.
7547 *
7548 * This function is the completion handler for the abort iocbs for
7549 * ELS commands. This function is called from the ELS ring event
7550 * handler with no lock held. This function frees memory resources
7551 * associated with the abort iocb.
7552 **/
dea31012005-04-17 16:05:31 -05007553static void
James Smart2e0fef82007-06-17 19:56:36 -05007554lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7555 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007556{
James Smart2e0fef82007-06-17 19:56:36 -05007557 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007558 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007559 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007560 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7561
7562 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007563
7564 if (irsp->ulpStatus) {
7565 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7566 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7567
James Smart2e0fef82007-06-17 19:56:36 -05007568 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007569 if (phba->sli_rev < LPFC_SLI_REV4) {
7570 if (abort_iotag != 0 &&
7571 abort_iotag <= phba->sli.last_iotag)
7572 abort_iocb =
7573 phba->sli.iocbq_lookup[abort_iotag];
7574 } else
7575 /* For sli4 the abort_tag is the XRI,
7576 * so the abort routine puts the iotag of the iocb
7577 * being aborted in the context field of the abort
7578 * IOCB.
7579 */
7580 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007581
James Smart2680eea2007-04-25 09:52:55 -04007582 /*
James Smart58da1ff2008-04-07 10:15:56 -04007583 * If the iocb is not found in Firmware queue the iocb
7584 * might have completed already. Do not free it again.
7585 */
James Smart9b379602008-04-07 10:16:00 -04007586 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007587 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7588 spin_unlock_irq(&phba->hbalock);
7589 lpfc_sli_release_iocbq(phba, cmdiocb);
7590 return;
7591 }
7592 /* For SLI4 the ulpContext field for abort IOCB
7593 * holds the iotag of the IOCB being aborted so
7594 * the local abort_context needs to be reset to
7595 * match the aborted IOCBs ulpContext.
7596 */
7597 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7598 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007599 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007600
7601 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7602 "0327 Cannot abort els iocb %p "
7603 "with tag %x context %x, abort status %x, "
7604 "abort code %x\n",
7605 abort_iocb, abort_iotag, abort_context,
7606 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007607 /*
James Smart2680eea2007-04-25 09:52:55 -04007608 * make sure we have the right iocbq before taking it
7609 * off the txcmplq and try to call completion routine.
7610 */
James Smart2e0fef82007-06-17 19:56:36 -05007611 if (!abort_iocb ||
7612 abort_iocb->iocb.ulpContext != abort_context ||
7613 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7614 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007615 else if (phba->sli_rev < LPFC_SLI_REV4) {
7616 /*
7617 * leave the SLI4 aborted command on the txcmplq
7618 * list and the command complete WCQE's XB bit
7619 * will tell whether the SGL (XRI) can be released
7620 * immediately or to the aborted SGL list for the
7621 * following abort XRI from the HBA.
7622 */
James Smart92d7f7b2007-06-17 19:56:38 -05007623 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007624 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7625 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7626 pring->txcmplq_cnt--;
7627 }
James Smart2680eea2007-04-25 09:52:55 -04007628
James Smart0ff10d42008-01-11 01:52:36 -05007629 /* Firmware could still be in progress of DMAing
7630 * payload, so don't free data buffer till after
7631 * a hbeat.
7632 */
7633 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007634 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007635 spin_unlock_irq(&phba->hbalock);
7636
James Smart92d7f7b2007-06-17 19:56:38 -05007637 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007638 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007639 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007640 } else
7641 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007642 }
7643
James Bottomley604a3e32005-10-29 10:28:33 -05007644 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007645 return;
7646}
7647
James Smarte59058c2008-08-24 21:49:00 -04007648/**
James Smart3621a712009-04-06 18:47:14 -04007649 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007650 * @phba: Pointer to HBA context object.
7651 * @cmdiocb: Pointer to driver command iocb object.
7652 * @rspiocb: Pointer to driver response iocb object.
7653 *
7654 * The function is called from SLI ring event handler with no
7655 * lock held. This function is the completion handler for ELS commands
7656 * which are aborted. The function frees memory resources used for
7657 * the aborted ELS commands.
7658 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007659static void
7660lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7661 struct lpfc_iocbq *rspiocb)
7662{
7663 IOCB_t *irsp = &rspiocb->iocb;
7664
7665 /* ELS cmd tag <ulpIoTag> completes */
7666 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007667 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007668 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007669 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007670 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007671 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7672 lpfc_ct_free_iocb(phba, cmdiocb);
7673 else
7674 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007675 return;
7676}
7677
James Smarte59058c2008-08-24 21:49:00 -04007678/**
James Smart5af5eee2010-10-22 11:06:38 -04007679 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007680 * @phba: Pointer to HBA context object.
7681 * @pring: Pointer to driver SLI ring object.
7682 * @cmdiocb: Pointer to driver command iocb object.
7683 *
James Smart5af5eee2010-10-22 11:06:38 -04007684 * This function issues an abort iocb for the provided command iocb down to
7685 * the port. Other than the case the outstanding command iocb is an abort
7686 * request, this function issues abort out unconditionally. This function is
7687 * called with hbalock held. The function returns 0 when it fails due to
7688 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007689 **/
James Smart5af5eee2010-10-22 11:06:38 -04007690static int
7691lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007692 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007693{
James Smart2e0fef82007-06-17 19:56:36 -05007694 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007695 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007696 IOCB_t *icmd = NULL;
7697 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007698 int retval;
James Smart07951072007-04-25 09:51:38 -04007699
James Smart92d7f7b2007-06-17 19:56:38 -05007700 /*
7701 * There are certain command types we don't want to abort. And we
7702 * don't want to abort commands that are already in the process of
7703 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007704 */
7705 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007706 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007707 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7708 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007709 return 0;
7710
dea31012005-04-17 16:05:31 -05007711 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007712 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007713 if (abtsiocbp == NULL)
7714 return 0;
dea31012005-04-17 16:05:31 -05007715
James Smart07951072007-04-25 09:51:38 -04007716 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007717 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007718 */
7719 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7720
dea31012005-04-17 16:05:31 -05007721 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007722 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7723 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007724 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007725 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007726 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7727 }
James Smartda0436e2009-05-22 14:51:39 -04007728 else
7729 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007730 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007731 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007732
James Smart5ffc2662009-11-18 15:39:44 -05007733 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7734 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007735 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7736 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007737
James Smart2e0fef82007-06-17 19:56:36 -05007738 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007739 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7740 else
7741 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7742
7743 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007744
James Smarte8b62012007-08-02 11:10:09 -04007745 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7746 "0339 Abort xri x%x, original iotag x%x, "
7747 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007748 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007749 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007750 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007751 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007752
James Smartd7c255b2008-08-24 21:50:00 -04007753 if (retval)
7754 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007755
7756 /*
7757 * Caller to this routine should check for IOCB_ERROR
7758 * and handle it properly. This routine no longer removes
7759 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7760 */
7761 return retval;
7762}
7763
7764/**
7765 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7766 * @phba: Pointer to HBA context object.
7767 * @pring: Pointer to driver SLI ring object.
7768 * @cmdiocb: Pointer to driver command iocb object.
7769 *
7770 * This function issues an abort iocb for the provided command iocb. In case
7771 * of unloading, the abort iocb will not be issued to commands on the ELS
7772 * ring. Instead, the callback function shall be changed to those commands
7773 * so that nothing happens when them finishes. This function is called with
7774 * hbalock held. The function returns 0 when the command iocb is an abort
7775 * request.
7776 **/
7777int
7778lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7779 struct lpfc_iocbq *cmdiocb)
7780{
7781 struct lpfc_vport *vport = cmdiocb->vport;
7782 int retval = IOCB_ERROR;
7783 IOCB_t *icmd = NULL;
7784
7785 /*
7786 * There are certain command types we don't want to abort. And we
7787 * don't want to abort commands that are already in the process of
7788 * being aborted.
7789 */
7790 icmd = &cmdiocb->iocb;
7791 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7792 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7793 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7794 return 0;
7795
7796 /*
7797 * If we're unloading, don't abort iocb on the ELS ring, but change
7798 * the callback so that nothing happens when it finishes.
7799 */
7800 if ((vport->load_flag & FC_UNLOADING) &&
7801 (pring->ringno == LPFC_ELS_RING)) {
7802 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7803 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7804 else
7805 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7806 goto abort_iotag_exit;
7807 }
7808
7809 /* Now, we try to issue the abort to the cmdiocb out */
7810 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7811
James Smart07951072007-04-25 09:51:38 -04007812abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007813 /*
7814 * Caller to this routine should check for IOCB_ERROR
7815 * and handle it properly. This routine no longer removes
7816 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007817 */
James Smart2e0fef82007-06-17 19:56:36 -05007818 return retval;
dea31012005-04-17 16:05:31 -05007819}
7820
James Smarte59058c2008-08-24 21:49:00 -04007821/**
James Smart5af5eee2010-10-22 11:06:38 -04007822 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7823 * @phba: Pointer to HBA context object.
7824 * @pring: Pointer to driver SLI ring object.
7825 *
7826 * This function aborts all iocbs in the given ring and frees all the iocb
7827 * objects in txq. This function issues abort iocbs unconditionally for all
7828 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7829 * to complete before the return of this function. The caller is not required
7830 * to hold any locks.
7831 **/
7832static void
7833lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7834{
7835 LIST_HEAD(completions);
7836 struct lpfc_iocbq *iocb, *next_iocb;
7837
7838 if (pring->ringno == LPFC_ELS_RING)
7839 lpfc_fabric_abort_hba(phba);
7840
7841 spin_lock_irq(&phba->hbalock);
7842
7843 /* Take off all the iocbs on txq for cancelling */
7844 list_splice_init(&pring->txq, &completions);
7845 pring->txq_cnt = 0;
7846
7847 /* Next issue ABTS for everything on the txcmplq */
7848 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7849 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7850
7851 spin_unlock_irq(&phba->hbalock);
7852
7853 /* Cancel all the IOCBs from the completions list */
7854 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7855 IOERR_SLI_ABORTED);
7856}
7857
7858/**
7859 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7860 * @phba: pointer to lpfc HBA data structure.
7861 *
7862 * This routine will abort all pending and outstanding iocbs to an HBA.
7863 **/
7864void
7865lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7866{
7867 struct lpfc_sli *psli = &phba->sli;
7868 struct lpfc_sli_ring *pring;
7869 int i;
7870
7871 for (i = 0; i < psli->num_rings; i++) {
7872 pring = &psli->ring[i];
7873 lpfc_sli_iocb_ring_abort(phba, pring);
7874 }
7875}
7876
7877/**
James Smart3621a712009-04-06 18:47:14 -04007878 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007879 * @iocbq: Pointer to driver iocb object.
7880 * @vport: Pointer to driver virtual port object.
7881 * @tgt_id: SCSI ID of the target.
7882 * @lun_id: LUN ID of the scsi device.
7883 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7884 *
James Smart3621a712009-04-06 18:47:14 -04007885 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007886 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7887 * 0 if the filtering criteria is met for the given iocb and will return
7888 * 1 if the filtering criteria is not met.
7889 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7890 * given iocb is for the SCSI device specified by vport, tgt_id and
7891 * lun_id parameter.
7892 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7893 * given iocb is for the SCSI target specified by vport and tgt_id
7894 * parameters.
7895 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7896 * given iocb is for the SCSI host associated with the given vport.
7897 * This function is called with no locks held.
7898 **/
dea31012005-04-17 16:05:31 -05007899static int
James Smart51ef4c22007-08-02 11:10:31 -04007900lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7901 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007902 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007903{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007904 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007905 int rc = 1;
7906
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007907 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7908 return rc;
7909
James Smart51ef4c22007-08-02 11:10:31 -04007910 if (iocbq->vport != vport)
7911 return rc;
7912
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007913 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007914
James Smart495a7142008-06-14 22:52:59 -04007915 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007916 return rc;
7917
7918 switch (ctx_cmd) {
7919 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007920 if ((lpfc_cmd->rdata->pnode) &&
7921 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7922 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007923 rc = 0;
7924 break;
7925 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007926 if ((lpfc_cmd->rdata->pnode) &&
7927 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007928 rc = 0;
7929 break;
dea31012005-04-17 16:05:31 -05007930 case LPFC_CTX_HOST:
7931 rc = 0;
7932 break;
7933 default:
7934 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007935 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007936 break;
7937 }
7938
7939 return rc;
7940}
7941
James Smarte59058c2008-08-24 21:49:00 -04007942/**
James Smart3621a712009-04-06 18:47:14 -04007943 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007944 * @vport: Pointer to virtual port.
7945 * @tgt_id: SCSI ID of the target.
7946 * @lun_id: LUN ID of the scsi device.
7947 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7948 *
7949 * This function returns number of FCP commands pending for the vport.
7950 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7951 * commands pending on the vport associated with SCSI device specified
7952 * by tgt_id and lun_id parameters.
7953 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7954 * commands pending on the vport associated with SCSI target specified
7955 * by tgt_id parameter.
7956 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7957 * commands pending on the vport.
7958 * This function returns the number of iocbs which satisfy the filter.
7959 * This function is called without any lock held.
7960 **/
dea31012005-04-17 16:05:31 -05007961int
James Smart51ef4c22007-08-02 11:10:31 -04007962lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7963 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007964{
James Smart51ef4c22007-08-02 11:10:31 -04007965 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007966 struct lpfc_iocbq *iocbq;
7967 int sum, i;
dea31012005-04-17 16:05:31 -05007968
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007969 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7970 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007971
James Smart51ef4c22007-08-02 11:10:31 -04007972 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7973 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007974 sum++;
dea31012005-04-17 16:05:31 -05007975 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007976
dea31012005-04-17 16:05:31 -05007977 return sum;
7978}
7979
James Smarte59058c2008-08-24 21:49:00 -04007980/**
James Smart3621a712009-04-06 18:47:14 -04007981 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007982 * @phba: Pointer to HBA context object
7983 * @cmdiocb: Pointer to command iocb object.
7984 * @rspiocb: Pointer to response iocb object.
7985 *
7986 * This function is called when an aborted FCP iocb completes. This
7987 * function is called by the ring event handler with no lock held.
7988 * This function frees the iocb.
7989 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007990void
James Smart2e0fef82007-06-17 19:56:36 -05007991lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7992 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007993{
James Bottomley604a3e32005-10-29 10:28:33 -05007994 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007995 return;
7996}
7997
James Smarte59058c2008-08-24 21:49:00 -04007998/**
James Smart3621a712009-04-06 18:47:14 -04007999 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04008000 * @vport: Pointer to virtual port.
8001 * @pring: Pointer to driver SLI ring object.
8002 * @tgt_id: SCSI ID of the target.
8003 * @lun_id: LUN ID of the scsi device.
8004 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8005 *
8006 * This function sends an abort command for every SCSI command
8007 * associated with the given virtual port pending on the ring
8008 * filtered by lpfc_sli_validate_fcp_iocb function.
8009 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8010 * FCP iocbs associated with lun specified by tgt_id and lun_id
8011 * parameters
8012 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8013 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8014 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8015 * FCP iocbs associated with virtual port.
8016 * This function returns number of iocbs it failed to abort.
8017 * This function is called with no locks held.
8018 **/
dea31012005-04-17 16:05:31 -05008019int
James Smart51ef4c22007-08-02 11:10:31 -04008020lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8021 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05008022{
James Smart51ef4c22007-08-02 11:10:31 -04008023 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008024 struct lpfc_iocbq *iocbq;
8025 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05008026 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05008027 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008028 int i;
dea31012005-04-17 16:05:31 -05008029
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008030 for (i = 1; i <= phba->sli.last_iotag; i++) {
8031 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008032
James Smart51ef4c22007-08-02 11:10:31 -04008033 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05008034 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05008035 continue;
8036
8037 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008038 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008039 if (abtsiocb == NULL) {
8040 errcnt++;
8041 continue;
8042 }
dea31012005-04-17 16:05:31 -05008043
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008044 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05008045 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8046 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04008047 if (phba->sli_rev == LPFC_SLI_REV4)
8048 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8049 else
8050 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008051 abtsiocb->iocb.ulpLe = 1;
8052 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05008053 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05008054
James Smart5ffc2662009-11-18 15:39:44 -05008055 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8056 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008057 if (iocbq->iocb_flag & LPFC_IO_FCP)
8058 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008059
James Smart2e0fef82007-06-17 19:56:36 -05008060 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05008061 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8062 else
8063 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8064
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008065 /* Setup callback routine and issue the command. */
8066 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04008067 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8068 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05008069 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05008070 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05008071 errcnt++;
8072 continue;
8073 }
8074 }
8075
8076 return errcnt;
8077}
8078
James Smarte59058c2008-08-24 21:49:00 -04008079/**
James Smart3621a712009-04-06 18:47:14 -04008080 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04008081 * @phba: Pointer to HBA context object.
8082 * @cmdiocbq: Pointer to command iocb.
8083 * @rspiocbq: Pointer to response iocb.
8084 *
8085 * This function is the completion handler for iocbs issued using
8086 * lpfc_sli_issue_iocb_wait function. This function is called by the
8087 * ring event handler function without any lock held. This function
8088 * can be called from both worker thread context and interrupt
8089 * context. This function also can be called from other thread which
8090 * cleans up the SLI layer objects.
8091 * This function copy the contents of the response iocb to the
8092 * response iocb memory object provided by the caller of
8093 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8094 * sleeps for the iocb completion.
8095 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008096static void
8097lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8098 struct lpfc_iocbq *cmdiocbq,
8099 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05008100{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008101 wait_queue_head_t *pdone_q;
8102 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05008103 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008104
James Smart2e0fef82007-06-17 19:56:36 -05008105 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008106 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8107 if (cmdiocbq->context2 && rspiocbq)
8108 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8109 &rspiocbq->iocb, sizeof(IOCB_t));
8110
James Smart0f65ff62010-02-26 14:14:23 -05008111 /* Set the exchange busy flag for task management commands */
8112 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8113 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8114 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8115 cur_iocbq);
8116 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8117 }
8118
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008119 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008120 if (pdone_q)
8121 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05008122 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05008123 return;
8124}
8125
James Smarte59058c2008-08-24 21:49:00 -04008126/**
James Smartd11e31d2009-06-10 17:23:06 -04008127 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8128 * @phba: Pointer to HBA context object..
8129 * @piocbq: Pointer to command iocb.
8130 * @flag: Flag to test.
8131 *
8132 * This routine grabs the hbalock and then test the iocb_flag to
8133 * see if the passed in flag is set.
8134 * Returns:
8135 * 1 if flag is set.
8136 * 0 if flag is not set.
8137 **/
8138static int
8139lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8140 struct lpfc_iocbq *piocbq, uint32_t flag)
8141{
8142 unsigned long iflags;
8143 int ret;
8144
8145 spin_lock_irqsave(&phba->hbalock, iflags);
8146 ret = piocbq->iocb_flag & flag;
8147 spin_unlock_irqrestore(&phba->hbalock, iflags);
8148 return ret;
8149
8150}
8151
8152/**
James Smart3621a712009-04-06 18:47:14 -04008153 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04008154 * @phba: Pointer to HBA context object..
8155 * @pring: Pointer to sli ring.
8156 * @piocb: Pointer to command iocb.
8157 * @prspiocbq: Pointer to response iocb.
8158 * @timeout: Timeout in number of seconds.
8159 *
8160 * This function issues the iocb to firmware and waits for the
8161 * iocb to complete. If the iocb command is not
8162 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8163 * Caller should not free the iocb resources if this function
8164 * returns IOCB_TIMEDOUT.
8165 * The function waits for the iocb completion using an
8166 * non-interruptible wait.
8167 * This function will sleep while waiting for iocb completion.
8168 * So, this function should not be called from any context which
8169 * does not allow sleeping. Due to the same reason, this function
8170 * cannot be called with interrupt disabled.
8171 * This function assumes that the iocb completions occur while
8172 * this function sleep. So, this function cannot be called from
8173 * the thread which process iocb completion for this ring.
8174 * This function clears the iocb_flag of the iocb object before
8175 * issuing the iocb and the iocb completion handler sets this
8176 * flag and wakes this thread when the iocb completes.
8177 * The contents of the response iocb will be copied to prspiocbq
8178 * by the completion handler when the command completes.
8179 * This function returns IOCB_SUCCESS when success.
8180 * This function is called with no lock held.
8181 **/
dea31012005-04-17 16:05:31 -05008182int
James Smart2e0fef82007-06-17 19:56:36 -05008183lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04008184 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05008185 struct lpfc_iocbq *piocb,
8186 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008187 uint32_t timeout)
dea31012005-04-17 16:05:31 -05008188{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008189 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008190 long timeleft, timeout_req = 0;
8191 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008192 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04008193 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05008194 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008195 * If the caller has provided a response iocbq buffer, then context2
8196 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05008197 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008198 if (prspiocbq) {
8199 if (piocb->context2)
8200 return IOCB_ERROR;
8201 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05008202 }
8203
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008204 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8205 piocb->context_un.wait_queue = &done_q;
8206 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05008207
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008208 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8209 creg_val = readl(phba->HCregaddr);
8210 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8211 writel(creg_val, phba->HCregaddr);
8212 readl(phba->HCregaddr); /* flush */
8213 }
8214
James Smart2a9bf3d2010-06-07 15:24:45 -04008215 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8216 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008217 if (retval == IOCB_SUCCESS) {
8218 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008219 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04008220 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008221 timeout_req);
dea31012005-04-17 16:05:31 -05008222
James Smart7054a602007-04-25 09:52:34 -04008223 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8224 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008225 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04008226 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008228 "0338 IOCB wait timeout error - no "
8229 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008230 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04008231 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008233 "0330 IOCB wake NOT set, "
8234 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008235 timeout, (timeleft / jiffies));
8236 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05008237 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008238 } else if (retval == IOCB_BUSY) {
8239 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8240 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8241 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8242 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008243 } else {
8244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008245 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008246 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008247 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05008248 }
8249
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008250 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8251 creg_val = readl(phba->HCregaddr);
8252 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8253 writel(creg_val, phba->HCregaddr);
8254 readl(phba->HCregaddr); /* flush */
8255 }
8256
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008257 if (prspiocbq)
8258 piocb->context2 = NULL;
8259
8260 piocb->context_un.wait_queue = NULL;
8261 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05008262 return retval;
8263}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008264
James Smarte59058c2008-08-24 21:49:00 -04008265/**
James Smart3621a712009-04-06 18:47:14 -04008266 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04008267 * @phba: Pointer to HBA context object.
8268 * @pmboxq: Pointer to driver mailbox object.
8269 * @timeout: Timeout in number of seconds.
8270 *
8271 * This function issues the mailbox to firmware and waits for the
8272 * mailbox command to complete. If the mailbox command is not
8273 * completed within timeout seconds, it returns MBX_TIMEOUT.
8274 * The function waits for the mailbox completion using an
8275 * interruptible wait. If the thread is woken up due to a
8276 * signal, MBX_TIMEOUT error is returned to the caller. Caller
8277 * should not free the mailbox resources, if this function returns
8278 * MBX_TIMEOUT.
8279 * This function will sleep while waiting for mailbox completion.
8280 * So, this function should not be called from any context which
8281 * does not allow sleeping. Due to the same reason, this function
8282 * cannot be called with interrupt disabled.
8283 * This function assumes that the mailbox completion occurs while
8284 * this function sleep. So, this function cannot be called from
8285 * the worker thread which processes mailbox completion.
8286 * This function is called in the context of HBA management
8287 * applications.
8288 * This function returns MBX_SUCCESS when successful.
8289 * This function is called with no lock held.
8290 **/
dea31012005-04-17 16:05:31 -05008291int
James Smart2e0fef82007-06-17 19:56:36 -05008292lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05008293 uint32_t timeout)
8294{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008295 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05008296 int retval;
James Smart858c9f62007-06-17 19:56:39 -05008297 unsigned long flag;
dea31012005-04-17 16:05:31 -05008298
8299 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04008300 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05008301 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008302
James Smart495a7142008-06-14 22:52:59 -04008303 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05008304 /* setup wake call as IOCB callback */
8305 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8306 /* setup context field to pass wait_queue pointer to wake function */
8307 pmboxq->context1 = &done_q;
8308
dea31012005-04-17 16:05:31 -05008309 /* now issue the command */
8310 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8311
8312 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04008313 wait_event_interruptible_timeout(done_q,
8314 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8315 timeout * HZ);
8316
James Smart858c9f62007-06-17 19:56:39 -05008317 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008318 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04008319 /*
8320 * if LPFC_MBX_WAKE flag is set the mailbox is completed
8321 * else do not free the resources.
8322 */
James Smartd7c47992010-06-08 18:31:54 -04008323 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05008324 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04008325 lpfc_sli4_swap_str(phba, pmboxq);
8326 } else {
James Smart7054a602007-04-25 09:52:34 -04008327 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05008328 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8329 }
8330 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008331 }
8332
dea31012005-04-17 16:05:31 -05008333 return retval;
8334}
8335
James Smarte59058c2008-08-24 21:49:00 -04008336/**
James Smart3772a992009-05-22 14:50:54 -04008337 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04008338 * @phba: Pointer to HBA context.
8339 *
James Smart3772a992009-05-22 14:50:54 -04008340 * This function is called to shutdown the driver's mailbox sub-system.
8341 * It first marks the mailbox sub-system is in a block state to prevent
8342 * the asynchronous mailbox command from issued off the pending mailbox
8343 * command queue. If the mailbox command sub-system shutdown is due to
8344 * HBA error conditions such as EEH or ERATT, this routine shall invoke
8345 * the mailbox sub-system flush routine to forcefully bring down the
8346 * mailbox sub-system. Otherwise, if it is due to normal condition (such
8347 * as with offline or HBA function reset), this routine will wait for the
8348 * outstanding mailbox command to complete before invoking the mailbox
8349 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04008350 **/
James Smart3772a992009-05-22 14:50:54 -04008351void
8352lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04008353{
James Smart3772a992009-05-22 14:50:54 -04008354 struct lpfc_sli *psli = &phba->sli;
8355 uint8_t actcmd = MBX_HEARTBEAT;
8356 unsigned long timeout;
8357
8358 spin_lock_irq(&phba->hbalock);
8359 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8360 spin_unlock_irq(&phba->hbalock);
8361
8362 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8363 spin_lock_irq(&phba->hbalock);
8364 if (phba->sli.mbox_active)
8365 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8366 spin_unlock_irq(&phba->hbalock);
8367 /* Determine how long we might wait for the active mailbox
8368 * command to be gracefully completed by firmware.
8369 */
8370 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8371 1000) + jiffies;
8372 while (phba->sli.mbox_active) {
8373 /* Check active mailbox complete status every 2ms */
8374 msleep(2);
8375 if (time_after(jiffies, timeout))
8376 /* Timeout, let the mailbox flush routine to
8377 * forcefully release active mailbox command
8378 */
8379 break;
8380 }
8381 }
8382 lpfc_sli_mbox_sys_flush(phba);
8383}
8384
8385/**
8386 * lpfc_sli_eratt_read - read sli-3 error attention events
8387 * @phba: Pointer to HBA context.
8388 *
8389 * This function is called to read the SLI3 device error attention registers
8390 * for possible error attention events. The caller must hold the hostlock
8391 * with spin_lock_irq().
8392 *
8393 * This fucntion returns 1 when there is Error Attention in the Host Attention
8394 * Register and returns 0 otherwise.
8395 **/
8396static int
8397lpfc_sli_eratt_read(struct lpfc_hba *phba)
8398{
James Smarted957682007-06-17 19:56:37 -05008399 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04008400
James Smart3772a992009-05-22 14:50:54 -04008401 /* Read chip Host Attention (HA) register */
8402 ha_copy = readl(phba->HAregaddr);
8403 if (ha_copy & HA_ERATT) {
8404 /* Read host status register to retrieve error event */
8405 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04008406
James Smart3772a992009-05-22 14:50:54 -04008407 /* Check if there is a deferred error condition is active */
8408 if ((HS_FFER1 & phba->work_hs) &&
8409 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008410 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04008411 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04008412 /* Clear all interrupt enable conditions */
8413 writel(0, phba->HCregaddr);
8414 readl(phba->HCregaddr);
8415 }
8416
8417 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04008418 phba->work_ha |= HA_ERATT;
8419 /* Indicate polling handles this ERATT */
8420 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008421 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008422 }
James Smart3772a992009-05-22 14:50:54 -04008423 return 0;
James Smartb4c02652006-07-06 15:50:43 -04008424}
8425
James Smarte59058c2008-08-24 21:49:00 -04008426/**
James Smartda0436e2009-05-22 14:51:39 -04008427 * lpfc_sli4_eratt_read - read sli-4 error attention events
8428 * @phba: Pointer to HBA context.
8429 *
8430 * This function is called to read the SLI4 device error attention registers
8431 * for possible error attention events. The caller must hold the hostlock
8432 * with spin_lock_irq().
8433 *
8434 * This fucntion returns 1 when there is Error Attention in the Host Attention
8435 * Register and returns 0 otherwise.
8436 **/
8437static int
8438lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8439{
8440 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04008441
8442 /* For now, use the SLI4 device internal unrecoverable error
8443 * registers for error attention. This can be changed later.
8444 */
James Smarta747c9c2009-11-18 15:41:10 -05008445 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
8446 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
8447 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8448 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8450 "1423 HBA Unrecoverable error: "
8451 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8452 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
8453 uerr_sta_lo, uerr_sta_hi,
8454 phba->sli4_hba.ue_mask_lo,
8455 phba->sli4_hba.ue_mask_hi);
8456 phba->work_status[0] = uerr_sta_lo;
8457 phba->work_status[1] = uerr_sta_hi;
8458 /* Set the driver HA work bitmap */
8459 phba->work_ha |= HA_ERATT;
8460 /* Indicate polling handles this ERATT */
8461 phba->hba_flag |= HBA_ERATT_HANDLED;
8462 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008463 }
8464 return 0;
8465}
8466
8467/**
James Smart3621a712009-04-06 18:47:14 -04008468 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008469 * @phba: Pointer to HBA context.
8470 *
James Smart3772a992009-05-22 14:50:54 -04008471 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008472 * error attention register bit for error attention events.
8473 *
8474 * This fucntion returns 1 when there is Error Attention in the Host Attention
8475 * Register and returns 0 otherwise.
8476 **/
8477int
8478lpfc_sli_check_eratt(struct lpfc_hba *phba)
8479{
8480 uint32_t ha_copy;
8481
8482 /* If somebody is waiting to handle an eratt, don't process it
8483 * here. The brdkill function will do this.
8484 */
8485 if (phba->link_flag & LS_IGNORE_ERATT)
8486 return 0;
8487
8488 /* Check if interrupt handler handles this ERATT */
8489 spin_lock_irq(&phba->hbalock);
8490 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8491 /* Interrupt handler has handled ERATT */
8492 spin_unlock_irq(&phba->hbalock);
8493 return 0;
8494 }
8495
James Smarta257bf92009-04-06 18:48:10 -04008496 /*
8497 * If there is deferred error attention, do not check for error
8498 * attention
8499 */
8500 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8501 spin_unlock_irq(&phba->hbalock);
8502 return 0;
8503 }
8504
James Smart3772a992009-05-22 14:50:54 -04008505 /* If PCI channel is offline, don't process it */
8506 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008507 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008508 return 0;
8509 }
8510
8511 switch (phba->sli_rev) {
8512 case LPFC_SLI_REV2:
8513 case LPFC_SLI_REV3:
8514 /* Read chip Host Attention (HA) register */
8515 ha_copy = lpfc_sli_eratt_read(phba);
8516 break;
James Smartda0436e2009-05-22 14:51:39 -04008517 case LPFC_SLI_REV4:
8518 /* Read devcie Uncoverable Error (UERR) registers */
8519 ha_copy = lpfc_sli4_eratt_read(phba);
8520 break;
James Smart3772a992009-05-22 14:50:54 -04008521 default:
8522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8523 "0299 Invalid SLI revision (%d)\n",
8524 phba->sli_rev);
8525 ha_copy = 0;
8526 break;
James Smart93996272008-08-24 21:50:30 -04008527 }
8528 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008529
8530 return ha_copy;
8531}
8532
8533/**
8534 * lpfc_intr_state_check - Check device state for interrupt handling
8535 * @phba: Pointer to HBA context.
8536 *
8537 * This inline routine checks whether a device or its PCI slot is in a state
8538 * that the interrupt should be handled.
8539 *
8540 * This function returns 0 if the device or the PCI slot is in a state that
8541 * interrupt should be handled, otherwise -EIO.
8542 */
8543static inline int
8544lpfc_intr_state_check(struct lpfc_hba *phba)
8545{
8546 /* If the pci channel is offline, ignore all the interrupts */
8547 if (unlikely(pci_channel_offline(phba->pcidev)))
8548 return -EIO;
8549
8550 /* Update device level interrupt statistics */
8551 phba->sli.slistat.sli_intr++;
8552
8553 /* Ignore all interrupts during initialization. */
8554 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8555 return -EIO;
8556
James Smart93996272008-08-24 21:50:30 -04008557 return 0;
8558}
8559
8560/**
James Smart3772a992009-05-22 14:50:54 -04008561 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008562 * @irq: Interrupt number.
8563 * @dev_id: The device context pointer.
8564 *
James Smart93996272008-08-24 21:50:30 -04008565 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008566 * service routine when device with SLI-3 interface spec is enabled with
8567 * MSI-X multi-message interrupt mode and there are slow-path events in
8568 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8569 * interrupt mode, this function is called as part of the device-level
8570 * interrupt handler. When the PCI slot is in error recovery or the HBA
8571 * is undergoing initialization, the interrupt handler will not process
8572 * the interrupt. The link attention and ELS ring attention events are
8573 * handled by the worker thread. The interrupt handler signals the worker
8574 * thread and returns for these events. This function is called without
8575 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008576 * structures.
8577 *
8578 * This function returns IRQ_HANDLED when interrupt is handled else it
8579 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008580 **/
dea31012005-04-17 16:05:31 -05008581irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008582lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008583{
James Smart2e0fef82007-06-17 19:56:36 -05008584 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008585 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008586 uint32_t work_ha_copy;
8587 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008588 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008589 uint32_t control;
8590
James Smart92d7f7b2007-06-17 19:56:38 -05008591 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008592 struct lpfc_vport *vport;
8593 struct lpfc_nodelist *ndlp;
8594 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008595 LPFC_MBOXQ_t *pmb;
8596 int rc;
8597
dea31012005-04-17 16:05:31 -05008598 /*
8599 * Get the driver's phba structure from the dev_id and
8600 * assume the HBA is not interrupting.
8601 */
James Smart93996272008-08-24 21:50:30 -04008602 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008603
8604 if (unlikely(!phba))
8605 return IRQ_NONE;
8606
dea31012005-04-17 16:05:31 -05008607 /*
James Smart93996272008-08-24 21:50:30 -04008608 * Stuff needs to be attented to when this function is invoked as an
8609 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008610 */
James Smart93996272008-08-24 21:50:30 -04008611 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008612 /* Check device state for handling interrupt */
8613 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008614 return IRQ_NONE;
8615 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008616 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008617 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008618 /* If somebody is waiting to handle an eratt don't process it
8619 * here. The brdkill function will do this.
8620 */
8621 if (phba->link_flag & LS_IGNORE_ERATT)
8622 ha_copy &= ~HA_ERATT;
8623 /* Check the need for handling ERATT in interrupt handler */
8624 if (ha_copy & HA_ERATT) {
8625 if (phba->hba_flag & HBA_ERATT_HANDLED)
8626 /* ERATT polling has handled ERATT */
8627 ha_copy &= ~HA_ERATT;
8628 else
8629 /* Indicate interrupt handler handles ERATT */
8630 phba->hba_flag |= HBA_ERATT_HANDLED;
8631 }
James Smarta257bf92009-04-06 18:48:10 -04008632
8633 /*
8634 * If there is deferred error attention, do not check for any
8635 * interrupt.
8636 */
8637 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008638 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008639 return IRQ_NONE;
8640 }
8641
James Smart93996272008-08-24 21:50:30 -04008642 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008643 hc_copy = readl(phba->HCregaddr);
8644 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8645 HC_LAINT_ENA | HC_ERINT_ENA),
8646 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008647 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8648 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008649 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008650 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008651 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008652 } else
8653 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008654
dea31012005-04-17 16:05:31 -05008655 work_ha_copy = ha_copy & phba->work_ha_mask;
8656
James Smart93996272008-08-24 21:50:30 -04008657 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008658 if (work_ha_copy & HA_LATT) {
8659 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8660 /*
8661 * Turn off Link Attention interrupts
8662 * until CLEAR_LA done
8663 */
James Smart5b75da22008-12-04 22:39:35 -05008664 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008665 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8666 control = readl(phba->HCregaddr);
8667 control &= ~HC_LAINT_ENA;
8668 writel(control, phba->HCregaddr);
8669 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008670 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008671 }
8672 else
8673 work_ha_copy &= ~HA_LATT;
8674 }
8675
James Smart93996272008-08-24 21:50:30 -04008676 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008677 /*
8678 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8679 * the only slow ring.
8680 */
8681 status = (work_ha_copy &
8682 (HA_RXMASK << (4*LPFC_ELS_RING)));
8683 status >>= (4*LPFC_ELS_RING);
8684 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008685 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008686 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008687
8688 lpfc_debugfs_slow_ring_trc(phba,
8689 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8690 control, status,
8691 (uint32_t)phba->sli.slistat.sli_intr);
8692
James Smart858c9f62007-06-17 19:56:39 -05008693 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008694 lpfc_debugfs_slow_ring_trc(phba,
8695 "ISR Disable ring:"
8696 "pwork:x%x hawork:x%x wait:x%x",
8697 phba->work_ha, work_ha_copy,
8698 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008699 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008700
James Smart858c9f62007-06-17 19:56:39 -05008701 control &=
8702 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008703 writel(control, phba->HCregaddr);
8704 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008705 }
James Smarta58cbd52007-08-02 11:09:43 -04008706 else {
8707 lpfc_debugfs_slow_ring_trc(phba,
8708 "ISR slow ring: pwork:"
8709 "x%x hawork:x%x wait:x%x",
8710 phba->work_ha, work_ha_copy,
8711 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008712 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008713 }
James Smart5b75da22008-12-04 22:39:35 -05008714 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008715 }
8716 }
James Smart5b75da22008-12-04 22:39:35 -05008717 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008718 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008719 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008720 /*
8721 * Check if there is a deferred error condition
8722 * is active
8723 */
8724 if ((HS_FFER1 & phba->work_hs) &&
8725 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008726 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8727 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008728 phba->hba_flag |= DEFER_ERATT;
8729 /* Clear all interrupt enable conditions */
8730 writel(0, phba->HCregaddr);
8731 readl(phba->HCregaddr);
8732 }
8733 }
8734
James Smart93996272008-08-24 21:50:30 -04008735 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008736 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008737 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008738 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008739 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008740
8741 /* First check out the status word */
8742 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8743 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008744 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008745 /*
8746 * Stray Mailbox Interrupt, mbxCommand <cmd>
8747 * mbxStatus <status>
8748 */
James Smart09372822008-01-11 01:52:54 -05008749 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008750 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008751 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008752 "Interrupt mbxCommand x%x "
8753 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008754 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008755 pmbox->mbxCommand,
8756 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008757 /* clear mailbox attention bit */
8758 work_ha_copy &= ~HA_MBATT;
8759 } else {
James Smart97eab632008-04-07 10:16:05 -04008760 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008761 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008762 phba->last_completion_time = jiffies;
8763 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008764 if (pmb->mbox_cmpl) {
8765 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8766 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008767 if (pmb->out_ext_byte_len &&
8768 pmb->context2)
8769 lpfc_sli_pcimem_bcopy(
8770 phba->mbox_ext,
8771 pmb->context2,
8772 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008773 }
James Smart09372822008-01-11 01:52:54 -05008774 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8775 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8776
8777 lpfc_debugfs_disc_trc(vport,
8778 LPFC_DISC_TRC_MBOX_VPORT,
8779 "MBOX dflt rpi: : "
8780 "status:x%x rpi:x%x",
8781 (uint32_t)pmbox->mbxStatus,
8782 pmbox->un.varWords[0], 0);
8783
8784 if (!pmbox->mbxStatus) {
8785 mp = (struct lpfc_dmabuf *)
8786 (pmb->context1);
8787 ndlp = (struct lpfc_nodelist *)
8788 pmb->context2;
8789
8790 /* Reg_LOGIN of dflt RPI was
8791 * successful. new lets get
8792 * rid of the RPI using the
8793 * same mbox buffer.
8794 */
8795 lpfc_unreg_login(phba,
8796 vport->vpi,
8797 pmbox->un.varWords[0],
8798 pmb);
8799 pmb->mbox_cmpl =
8800 lpfc_mbx_cmpl_dflt_rpi;
8801 pmb->context1 = mp;
8802 pmb->context2 = ndlp;
8803 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008804 rc = lpfc_sli_issue_mbox(phba,
8805 pmb,
8806 MBX_NOWAIT);
8807 if (rc != MBX_BUSY)
8808 lpfc_printf_log(phba,
8809 KERN_ERR,
8810 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008811 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008812 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008813 if (rc != MBX_NOT_FINISHED)
8814 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008815 }
8816 }
James Smart5b75da22008-12-04 22:39:35 -05008817 spin_lock_irqsave(
8818 &phba->pport->work_port_lock,
8819 iflag);
James Smart09372822008-01-11 01:52:54 -05008820 phba->pport->work_port_events &=
8821 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008822 spin_unlock_irqrestore(
8823 &phba->pport->work_port_lock,
8824 iflag);
James Smart09372822008-01-11 01:52:54 -05008825 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008826 }
James Smart97eab632008-04-07 10:16:05 -04008827 } else
James Smart5b75da22008-12-04 22:39:35 -05008828 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008829
James Smart92d7f7b2007-06-17 19:56:38 -05008830 if ((work_ha_copy & HA_MBATT) &&
8831 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008832send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008833 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008834 do {
8835 rc = lpfc_sli_issue_mbox(phba, NULL,
8836 MBX_NOWAIT);
8837 } while (rc == MBX_NOT_FINISHED);
8838 if (rc != MBX_SUCCESS)
8839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8840 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008841 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008842 }
8843
James Smart5b75da22008-12-04 22:39:35 -05008844 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008845 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008847 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008848 }
James Smart93996272008-08-24 21:50:30 -04008849 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008850
James Smart3772a992009-05-22 14:50:54 -04008851} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008852
8853/**
James Smart3772a992009-05-22 14:50:54 -04008854 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008855 * @irq: Interrupt number.
8856 * @dev_id: The device context pointer.
8857 *
8858 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008859 * service routine when device with SLI-3 interface spec is enabled with
8860 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8861 * ring event in the HBA. However, when the device is enabled with either
8862 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8863 * device-level interrupt handler. When the PCI slot is in error recovery
8864 * or the HBA is undergoing initialization, the interrupt handler will not
8865 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8866 * the intrrupt context. This function is called without any lock held.
8867 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008868 *
8869 * This function returns IRQ_HANDLED when interrupt is handled else it
8870 * returns IRQ_NONE.
8871 **/
8872irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008873lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008874{
8875 struct lpfc_hba *phba;
8876 uint32_t ha_copy;
8877 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008878 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008879
8880 /* Get the driver's phba structure from the dev_id and
8881 * assume the HBA is not interrupting.
8882 */
8883 phba = (struct lpfc_hba *) dev_id;
8884
8885 if (unlikely(!phba))
8886 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008887
8888 /*
James Smart93996272008-08-24 21:50:30 -04008889 * Stuff needs to be attented to when this function is invoked as an
8890 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008891 */
James Smart93996272008-08-24 21:50:30 -04008892 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008893 /* Check device state for handling interrupt */
8894 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008895 return IRQ_NONE;
8896 /* Need to read HA REG for FCP ring and other ring events */
8897 ha_copy = readl(phba->HAregaddr);
8898 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008899 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008900 /*
8901 * If there is deferred error attention, do not check for
8902 * any interrupt.
8903 */
8904 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008905 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008906 return IRQ_NONE;
8907 }
James Smart93996272008-08-24 21:50:30 -04008908 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8909 phba->HAregaddr);
8910 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008911 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008912 } else
8913 ha_copy = phba->ha_copy;
8914
8915 /*
8916 * Process all events on FCP ring. Take the optimized path for FCP IO.
8917 */
8918 ha_copy &= ~(phba->work_ha_mask);
8919
8920 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008921 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008922 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008923 lpfc_sli_handle_fast_ring_event(phba,
8924 &phba->sli.ring[LPFC_FCP_RING],
8925 status);
James Smarta4bc3372006-12-02 13:34:16 -05008926
8927 if (phba->cfg_multi_ring_support == 2) {
8928 /*
James Smart93996272008-08-24 21:50:30 -04008929 * Process all events on extra ring. Take the optimized path
8930 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008931 */
James Smart93996272008-08-24 21:50:30 -04008932 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008933 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008934 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008935 lpfc_sli_handle_fast_ring_event(phba,
8936 &phba->sli.ring[LPFC_EXTRA_RING],
8937 status);
8938 }
8939 }
dea31012005-04-17 16:05:31 -05008940 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008941} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008942
James Smart93996272008-08-24 21:50:30 -04008943/**
James Smart3772a992009-05-22 14:50:54 -04008944 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008945 * @irq: Interrupt number.
8946 * @dev_id: The device context pointer.
8947 *
James Smart3772a992009-05-22 14:50:54 -04008948 * This function is the HBA device-level interrupt handler to device with
8949 * SLI-3 interface spec, called from the PCI layer when either MSI or
8950 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8951 * requires driver attention. This function invokes the slow-path interrupt
8952 * attention handling function and fast-path interrupt attention handling
8953 * function in turn to process the relevant HBA attention events. This
8954 * function is called without any lock held. It gets the hbalock to access
8955 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008956 *
8957 * This function returns IRQ_HANDLED when interrupt is handled, else it
8958 * returns IRQ_NONE.
8959 **/
8960irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008961lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008962{
8963 struct lpfc_hba *phba;
8964 irqreturn_t sp_irq_rc, fp_irq_rc;
8965 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008966 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008967
8968 /*
8969 * Get the driver's phba structure from the dev_id and
8970 * assume the HBA is not interrupting.
8971 */
8972 phba = (struct lpfc_hba *) dev_id;
8973
8974 if (unlikely(!phba))
8975 return IRQ_NONE;
8976
James Smart3772a992009-05-22 14:50:54 -04008977 /* Check device state for handling interrupt */
8978 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008979 return IRQ_NONE;
8980
8981 spin_lock(&phba->hbalock);
8982 phba->ha_copy = readl(phba->HAregaddr);
8983 if (unlikely(!phba->ha_copy)) {
8984 spin_unlock(&phba->hbalock);
8985 return IRQ_NONE;
8986 } else if (phba->ha_copy & HA_ERATT) {
8987 if (phba->hba_flag & HBA_ERATT_HANDLED)
8988 /* ERATT polling has handled ERATT */
8989 phba->ha_copy &= ~HA_ERATT;
8990 else
8991 /* Indicate interrupt handler handles ERATT */
8992 phba->hba_flag |= HBA_ERATT_HANDLED;
8993 }
8994
James Smarta257bf92009-04-06 18:48:10 -04008995 /*
8996 * If there is deferred error attention, do not check for any interrupt.
8997 */
8998 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02008999 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04009000 return IRQ_NONE;
9001 }
9002
James Smart93996272008-08-24 21:50:30 -04009003 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05009004 hc_copy = readl(phba->HCregaddr);
9005 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9006 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9007 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009008 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009009 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009010 readl(phba->HAregaddr); /* flush */
9011 spin_unlock(&phba->hbalock);
9012
9013 /*
9014 * Invokes slow-path host attention interrupt handling as appropriate.
9015 */
9016
9017 /* status of events with mailbox and link attention */
9018 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9019
9020 /* status of events with ELS ring */
9021 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
9022 status2 >>= (4*LPFC_ELS_RING);
9023
9024 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009025 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009026 else
9027 sp_irq_rc = IRQ_NONE;
9028
9029 /*
9030 * Invoke fast-path host attention interrupt handling as appropriate.
9031 */
9032
9033 /* status of events with FCP ring */
9034 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9035 status1 >>= (4*LPFC_FCP_RING);
9036
9037 /* status of events with extra ring */
9038 if (phba->cfg_multi_ring_support == 2) {
9039 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9040 status2 >>= (4*LPFC_EXTRA_RING);
9041 } else
9042 status2 = 0;
9043
9044 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009045 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009046 else
9047 fp_irq_rc = IRQ_NONE;
9048
9049 /* Return device-level interrupt handling status */
9050 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04009051} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04009052
9053/**
9054 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9055 * @phba: pointer to lpfc hba data structure.
9056 *
9057 * This routine is invoked by the worker thread to process all the pending
9058 * SLI4 FCP abort XRI events.
9059 **/
9060void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9061{
9062 struct lpfc_cq_event *cq_event;
9063
9064 /* First, declare the fcp xri abort event has been handled */
9065 spin_lock_irq(&phba->hbalock);
9066 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9067 spin_unlock_irq(&phba->hbalock);
9068 /* Now, handle all the fcp xri abort events */
9069 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9070 /* Get the first event from the head of the event queue */
9071 spin_lock_irq(&phba->hbalock);
9072 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9073 cq_event, struct lpfc_cq_event, list);
9074 spin_unlock_irq(&phba->hbalock);
9075 /* Notify aborted XRI for FCP work queue */
9076 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9077 /* Free the event processed back to the free pool */
9078 lpfc_sli4_cq_event_release(phba, cq_event);
9079 }
9080}
9081
9082/**
9083 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9084 * @phba: pointer to lpfc hba data structure.
9085 *
9086 * This routine is invoked by the worker thread to process all the pending
9087 * SLI4 els abort xri events.
9088 **/
9089void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9090{
9091 struct lpfc_cq_event *cq_event;
9092
9093 /* First, declare the els xri abort event has been handled */
9094 spin_lock_irq(&phba->hbalock);
9095 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9096 spin_unlock_irq(&phba->hbalock);
9097 /* Now, handle all the els xri abort events */
9098 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9099 /* Get the first event from the head of the event queue */
9100 spin_lock_irq(&phba->hbalock);
9101 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9102 cq_event, struct lpfc_cq_event, list);
9103 spin_unlock_irq(&phba->hbalock);
9104 /* Notify aborted XRI for ELS work queue */
9105 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9106 /* Free the event processed back to the free pool */
9107 lpfc_sli4_cq_event_release(phba, cq_event);
9108 }
9109}
9110
James Smart341af102010-01-26 23:07:37 -05009111/**
9112 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9113 * @phba: pointer to lpfc hba data structure
9114 * @pIocbIn: pointer to the rspiocbq
9115 * @pIocbOut: pointer to the cmdiocbq
9116 * @wcqe: pointer to the complete wcqe
9117 *
9118 * This routine transfers the fields of a command iocbq to a response iocbq
9119 * by copying all the IOCB fields from command iocbq and transferring the
9120 * completion status information from the complete wcqe.
9121 **/
James Smart4f774512009-05-22 14:52:35 -04009122static void
James Smart341af102010-01-26 23:07:37 -05009123lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9124 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04009125 struct lpfc_iocbq *pIocbOut,
9126 struct lpfc_wcqe_complete *wcqe)
9127{
James Smart341af102010-01-26 23:07:37 -05009128 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04009129 size_t offset = offsetof(struct lpfc_iocbq, iocb);
9130
9131 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9132 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04009133 /* Map WCQE parameters into irspiocb parameters */
9134 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9135 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9136 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9137 pIocbIn->iocb.un.fcpi.fcpi_parm =
9138 pIocbOut->iocb.un.fcpi.fcpi_parm -
9139 wcqe->total_data_placed;
9140 else
9141 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009142 else {
James Smart4f774512009-05-22 14:52:35 -04009143 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009144 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9145 }
James Smart341af102010-01-26 23:07:37 -05009146
9147 /* Pick up HBA exchange busy condition */
9148 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9149 spin_lock_irqsave(&phba->hbalock, iflags);
9150 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9151 spin_unlock_irqrestore(&phba->hbalock, iflags);
9152 }
James Smart4f774512009-05-22 14:52:35 -04009153}
9154
9155/**
James Smart45ed1192009-10-02 15:17:02 -04009156 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9157 * @phba: Pointer to HBA context object.
9158 * @wcqe: Pointer to work-queue completion queue entry.
9159 *
9160 * This routine handles an ELS work-queue completion event and construct
9161 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9162 * discovery engine to handle.
9163 *
9164 * Return: Pointer to the receive IOCBQ, NULL otherwise.
9165 **/
9166static struct lpfc_iocbq *
9167lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9168 struct lpfc_iocbq *irspiocbq)
9169{
9170 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9171 struct lpfc_iocbq *cmdiocbq;
9172 struct lpfc_wcqe_complete *wcqe;
9173 unsigned long iflags;
9174
9175 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9176 spin_lock_irqsave(&phba->hbalock, iflags);
9177 pring->stats.iocb_event++;
9178 /* Look up the ELS command IOCB and create pseudo response IOCB */
9179 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9180 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9181 spin_unlock_irqrestore(&phba->hbalock, iflags);
9182
9183 if (unlikely(!cmdiocbq)) {
9184 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9185 "0386 ELS complete with no corresponding "
9186 "cmdiocb: iotag (%d)\n",
9187 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9188 lpfc_sli_release_iocbq(phba, irspiocbq);
9189 return NULL;
9190 }
9191
9192 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009193 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04009194
9195 return irspiocbq;
9196}
9197
9198/**
James Smart04c68492009-05-22 14:52:52 -04009199 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9200 * @phba: Pointer to HBA context object.
9201 * @cqe: Pointer to mailbox completion queue entry.
9202 *
9203 * This routine process a mailbox completion queue entry with asynchrous
9204 * event.
9205 *
9206 * Return: true if work posted to worker thread, otherwise false.
9207 **/
9208static bool
9209lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9210{
9211 struct lpfc_cq_event *cq_event;
9212 unsigned long iflags;
9213
9214 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9215 "0392 Async Event: word0:x%x, word1:x%x, "
9216 "word2:x%x, word3:x%x\n", mcqe->word0,
9217 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9218
9219 /* Allocate a new internal CQ_EVENT entry */
9220 cq_event = lpfc_sli4_cq_event_alloc(phba);
9221 if (!cq_event) {
9222 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9223 "0394 Failed to allocate CQ_EVENT entry\n");
9224 return false;
9225 }
9226
9227 /* Move the CQE into an asynchronous event entry */
9228 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9229 spin_lock_irqsave(&phba->hbalock, iflags);
9230 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9231 /* Set the async event flag */
9232 phba->hba_flag |= ASYNC_EVENT;
9233 spin_unlock_irqrestore(&phba->hbalock, iflags);
9234
9235 return true;
9236}
9237
9238/**
9239 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9240 * @phba: Pointer to HBA context object.
9241 * @cqe: Pointer to mailbox completion queue entry.
9242 *
9243 * This routine process a mailbox completion queue entry with mailbox
9244 * completion event.
9245 *
9246 * Return: true if work posted to worker thread, otherwise false.
9247 **/
9248static bool
9249lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9250{
9251 uint32_t mcqe_status;
9252 MAILBOX_t *mbox, *pmbox;
9253 struct lpfc_mqe *mqe;
9254 struct lpfc_vport *vport;
9255 struct lpfc_nodelist *ndlp;
9256 struct lpfc_dmabuf *mp;
9257 unsigned long iflags;
9258 LPFC_MBOXQ_t *pmb;
9259 bool workposted = false;
9260 int rc;
9261
9262 /* If not a mailbox complete MCQE, out by checking mailbox consume */
9263 if (!bf_get(lpfc_trailer_completed, mcqe))
9264 goto out_no_mqe_complete;
9265
9266 /* Get the reference to the active mbox command */
9267 spin_lock_irqsave(&phba->hbalock, iflags);
9268 pmb = phba->sli.mbox_active;
9269 if (unlikely(!pmb)) {
9270 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9271 "1832 No pending MBOX command to handle\n");
9272 spin_unlock_irqrestore(&phba->hbalock, iflags);
9273 goto out_no_mqe_complete;
9274 }
9275 spin_unlock_irqrestore(&phba->hbalock, iflags);
9276 mqe = &pmb->u.mqe;
9277 pmbox = (MAILBOX_t *)&pmb->u.mqe;
9278 mbox = phba->mbox;
9279 vport = pmb->vport;
9280
9281 /* Reset heartbeat timer */
9282 phba->last_completion_time = jiffies;
9283 del_timer(&phba->sli.mbox_tmo);
9284
9285 /* Move mbox data to caller's mailbox region, do endian swapping */
9286 if (pmb->mbox_cmpl && mbox)
9287 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9288 /* Set the mailbox status with SLI4 range 0x4000 */
9289 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9290 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9291 bf_set(lpfc_mqe_status, mqe,
9292 (LPFC_MBX_ERROR_RANGE | mcqe_status));
9293
9294 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9295 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9296 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9297 "MBOX dflt rpi: status:x%x rpi:x%x",
9298 mcqe_status,
9299 pmbox->un.varWords[0], 0);
9300 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9301 mp = (struct lpfc_dmabuf *)(pmb->context1);
9302 ndlp = (struct lpfc_nodelist *)pmb->context2;
9303 /* Reg_LOGIN of dflt RPI was successful. Now lets get
9304 * RID of the PPI using the same mbox buffer.
9305 */
9306 lpfc_unreg_login(phba, vport->vpi,
9307 pmbox->un.varWords[0], pmb);
9308 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9309 pmb->context1 = mp;
9310 pmb->context2 = ndlp;
9311 pmb->vport = vport;
9312 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9313 if (rc != MBX_BUSY)
9314 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9315 LOG_SLI, "0385 rc should "
9316 "have been MBX_BUSY\n");
9317 if (rc != MBX_NOT_FINISHED)
9318 goto send_current_mbox;
9319 }
9320 }
9321 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9322 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9323 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9324
9325 /* There is mailbox completion work to do */
9326 spin_lock_irqsave(&phba->hbalock, iflags);
9327 __lpfc_mbox_cmpl_put(phba, pmb);
9328 phba->work_ha |= HA_MBATT;
9329 spin_unlock_irqrestore(&phba->hbalock, iflags);
9330 workposted = true;
9331
9332send_current_mbox:
9333 spin_lock_irqsave(&phba->hbalock, iflags);
9334 /* Release the mailbox command posting token */
9335 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9336 /* Setting active mailbox pointer need to be in sync to flag clear */
9337 phba->sli.mbox_active = NULL;
9338 spin_unlock_irqrestore(&phba->hbalock, iflags);
9339 /* Wake up worker thread to post the next pending mailbox command */
9340 lpfc_worker_wake_up(phba);
9341out_no_mqe_complete:
9342 if (bf_get(lpfc_trailer_consumed, mcqe))
9343 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9344 return workposted;
9345}
9346
9347/**
9348 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9349 * @phba: Pointer to HBA context object.
9350 * @cqe: Pointer to mailbox completion queue entry.
9351 *
9352 * This routine process a mailbox completion queue entry, it invokes the
9353 * proper mailbox complete handling or asynchrous event handling routine
9354 * according to the MCQE's async bit.
9355 *
9356 * Return: true if work posted to worker thread, otherwise false.
9357 **/
9358static bool
9359lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9360{
9361 struct lpfc_mcqe mcqe;
9362 bool workposted;
9363
9364 /* Copy the mailbox MCQE and convert endian order as needed */
9365 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9366
9367 /* Invoke the proper event handling routine */
9368 if (!bf_get(lpfc_trailer_async, &mcqe))
9369 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9370 else
9371 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9372 return workposted;
9373}
9374
9375/**
James Smart4f774512009-05-22 14:52:35 -04009376 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9377 * @phba: Pointer to HBA context object.
9378 * @wcqe: Pointer to work-queue completion queue entry.
9379 *
9380 * This routine handles an ELS work-queue completion event.
9381 *
9382 * Return: true if work posted to worker thread, otherwise false.
9383 **/
9384static bool
9385lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9386 struct lpfc_wcqe_complete *wcqe)
9387{
James Smart4f774512009-05-22 14:52:35 -04009388 struct lpfc_iocbq *irspiocbq;
9389 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04009390 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04009391
James Smart45ed1192009-10-02 15:17:02 -04009392 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04009393 irspiocbq = lpfc_sli_get_iocbq(phba);
9394 if (!irspiocbq) {
9395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04009396 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9397 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9398 pring->txq_cnt, phba->iocb_cnt,
9399 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9400 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04009401 return false;
James Smart4f774512009-05-22 14:52:35 -04009402 }
James Smart4f774512009-05-22 14:52:35 -04009403
James Smart45ed1192009-10-02 15:17:02 -04009404 /* Save off the slow-path queue event for work thread to process */
9405 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04009406 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04009407 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009408 &phba->sli4_hba.sp_queue_event);
9409 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04009410 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04009411
James Smart45ed1192009-10-02 15:17:02 -04009412 return true;
James Smart4f774512009-05-22 14:52:35 -04009413}
9414
9415/**
9416 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9417 * @phba: Pointer to HBA context object.
9418 * @wcqe: Pointer to work-queue completion queue entry.
9419 *
9420 * This routine handles slow-path WQ entry comsumed event by invoking the
9421 * proper WQ release routine to the slow-path WQ.
9422 **/
9423static void
9424lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9425 struct lpfc_wcqe_release *wcqe)
9426{
9427 /* Check for the slow-path ELS work queue */
9428 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9429 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9430 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9431 else
9432 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9433 "2579 Slow-path wqe consume event carries "
9434 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9435 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9436 phba->sli4_hba.els_wq->queue_id);
9437}
9438
9439/**
9440 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9441 * @phba: Pointer to HBA context object.
9442 * @cq: Pointer to a WQ completion queue.
9443 * @wcqe: Pointer to work-queue completion queue entry.
9444 *
9445 * This routine handles an XRI abort event.
9446 *
9447 * Return: true if work posted to worker thread, otherwise false.
9448 **/
9449static bool
9450lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9451 struct lpfc_queue *cq,
9452 struct sli4_wcqe_xri_aborted *wcqe)
9453{
9454 bool workposted = false;
9455 struct lpfc_cq_event *cq_event;
9456 unsigned long iflags;
9457
9458 /* Allocate a new internal CQ_EVENT entry */
9459 cq_event = lpfc_sli4_cq_event_alloc(phba);
9460 if (!cq_event) {
9461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9462 "0602 Failed to allocate CQ_EVENT entry\n");
9463 return false;
9464 }
9465
9466 /* Move the CQE into the proper xri abort event list */
9467 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9468 switch (cq->subtype) {
9469 case LPFC_FCP:
9470 spin_lock_irqsave(&phba->hbalock, iflags);
9471 list_add_tail(&cq_event->list,
9472 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9473 /* Set the fcp xri abort event flag */
9474 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9475 spin_unlock_irqrestore(&phba->hbalock, iflags);
9476 workposted = true;
9477 break;
9478 case LPFC_ELS:
9479 spin_lock_irqsave(&phba->hbalock, iflags);
9480 list_add_tail(&cq_event->list,
9481 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9482 /* Set the els xri abort event flag */
9483 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9484 spin_unlock_irqrestore(&phba->hbalock, iflags);
9485 workposted = true;
9486 break;
9487 default:
9488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9489 "0603 Invalid work queue CQE subtype (x%x)\n",
9490 cq->subtype);
9491 workposted = false;
9492 break;
9493 }
9494 return workposted;
9495}
9496
9497/**
James Smart4d9ab992009-10-02 15:16:39 -04009498 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009499 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009500 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009501 *
James Smart4d9ab992009-10-02 15:16:39 -04009502 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009503 *
9504 * Return: true if work posted to worker thread, otherwise false.
9505 **/
9506static bool
James Smart4d9ab992009-10-02 15:16:39 -04009507lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9508{
9509 bool workposted = false;
9510 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9511 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9512 struct hbq_dmabuf *dma_buf;
9513 uint32_t status;
9514 unsigned long iflags;
9515
James Smart4d9ab992009-10-02 15:16:39 -04009516 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9517 goto out;
9518
9519 status = bf_get(lpfc_rcqe_status, rcqe);
9520 switch (status) {
9521 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9522 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9523 "2537 Receive Frame Truncated!!\n");
9524 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009525 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009526 spin_lock_irqsave(&phba->hbalock, iflags);
9527 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9528 if (!dma_buf) {
9529 spin_unlock_irqrestore(&phba->hbalock, iflags);
9530 goto out;
9531 }
9532 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9533 /* save off the frame for the word thread to process */
9534 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009535 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009536 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009537 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009538 spin_unlock_irqrestore(&phba->hbalock, iflags);
9539 workposted = true;
9540 break;
9541 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9542 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9543 /* Post more buffers if possible */
9544 spin_lock_irqsave(&phba->hbalock, iflags);
9545 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9546 spin_unlock_irqrestore(&phba->hbalock, iflags);
9547 workposted = true;
9548 break;
9549 }
9550out:
9551 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009552}
9553
9554/**
9555 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9556 * @phba: Pointer to HBA context object.
9557 * @cq: Pointer to the completion queue.
9558 * @wcqe: Pointer to a completion queue entry.
9559 *
9560 * This routine process a slow-path work-queue or recieve queue completion queue
9561 * entry.
9562 *
9563 * Return: true if work posted to worker thread, otherwise false.
9564 **/
9565static bool
9566lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009567 struct lpfc_cqe *cqe)
9568{
James Smart45ed1192009-10-02 15:17:02 -04009569 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009570 bool workposted = false;
9571
9572 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009573 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009574
9575 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009576 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009577 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009578 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009579 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009580 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009581 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009582 break;
9583 case CQE_CODE_RELEASE_WQE:
9584 /* Process the WQ release event */
9585 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009586 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009587 break;
9588 case CQE_CODE_XRI_ABORTED:
9589 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009590 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009591 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009592 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009593 break;
James Smart4d9ab992009-10-02 15:16:39 -04009594 case CQE_CODE_RECEIVE:
9595 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009596 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009597 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009598 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009599 break;
James Smart4f774512009-05-22 14:52:35 -04009600 default:
9601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9602 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009603 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009604 break;
9605 }
9606 return workposted;
9607}
9608
9609/**
James Smart4f774512009-05-22 14:52:35 -04009610 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9611 * @phba: Pointer to HBA context object.
9612 * @eqe: Pointer to fast-path event queue entry.
9613 *
9614 * This routine process a event queue entry from the slow-path event queue.
9615 * It will check the MajorCode and MinorCode to determine this is for a
9616 * completion event on a completion queue, if not, an error shall be logged
9617 * and just return. Otherwise, it will get to the corresponding completion
9618 * queue and process all the entries on that completion queue, rearm the
9619 * completion queue, and then return.
9620 *
9621 **/
9622static void
9623lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9624{
9625 struct lpfc_queue *cq = NULL, *childq, *speq;
9626 struct lpfc_cqe *cqe;
9627 bool workposted = false;
9628 int ecount = 0;
9629 uint16_t cqid;
9630
James Smartcb5172e2010-03-15 11:25:07 -04009631 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009632 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9633 "0359 Not a valid slow-path completion "
9634 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009635 bf_get_le32(lpfc_eqe_major_code, eqe),
9636 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009637 return;
9638 }
9639
9640 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009641 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009642
9643 /* Search for completion queue pointer matching this cqid */
9644 speq = phba->sli4_hba.sp_eq;
9645 list_for_each_entry(childq, &speq->child_list, list) {
9646 if (childq->queue_id == cqid) {
9647 cq = childq;
9648 break;
9649 }
9650 }
9651 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009652 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9654 "0365 Slow-path CQ identifier "
9655 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009656 return;
9657 }
9658
9659 /* Process all the entries to the CQ */
9660 switch (cq->type) {
9661 case LPFC_MCQ:
9662 while ((cqe = lpfc_sli4_cq_get(cq))) {
9663 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9664 if (!(++ecount % LPFC_GET_QE_REL_INT))
9665 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9666 }
9667 break;
9668 case LPFC_WCQ:
9669 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009670 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009671 if (!(++ecount % LPFC_GET_QE_REL_INT))
9672 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9673 }
9674 break;
9675 default:
9676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9677 "0370 Invalid completion queue type (%d)\n",
9678 cq->type);
9679 return;
9680 }
9681
9682 /* Catch the no cq entry condition, log an error */
9683 if (unlikely(ecount == 0))
9684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9685 "0371 No entry from the CQ: identifier "
9686 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9687
9688 /* In any case, flash and re-arm the RCQ */
9689 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9690
9691 /* wake up worker thread if there are works to be done */
9692 if (workposted)
9693 lpfc_worker_wake_up(phba);
9694}
9695
9696/**
9697 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9698 * @eqe: Pointer to fast-path completion queue entry.
9699 *
9700 * This routine process a fast-path work queue completion entry from fast-path
9701 * event queue for FCP command response completion.
9702 **/
9703static void
9704lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9705 struct lpfc_wcqe_complete *wcqe)
9706{
9707 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9708 struct lpfc_iocbq *cmdiocbq;
9709 struct lpfc_iocbq irspiocbq;
9710 unsigned long iflags;
9711
9712 spin_lock_irqsave(&phba->hbalock, iflags);
9713 pring->stats.iocb_event++;
9714 spin_unlock_irqrestore(&phba->hbalock, iflags);
9715
9716 /* Check for response status */
9717 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9718 /* If resource errors reported from HBA, reduce queue
9719 * depth of the SCSI device.
9720 */
9721 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9722 IOSTAT_LOCAL_REJECT) &&
9723 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9724 phba->lpfc_rampdown_queue_depth(phba);
9725 }
9726 /* Log the error status */
9727 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9728 "0373 FCP complete error: status=x%x, "
9729 "hw_status=x%x, total_data_specified=%d, "
9730 "parameter=x%x, word3=x%x\n",
9731 bf_get(lpfc_wcqe_c_status, wcqe),
9732 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9733 wcqe->total_data_placed, wcqe->parameter,
9734 wcqe->word3);
9735 }
9736
9737 /* Look up the FCP command IOCB and create pseudo response IOCB */
9738 spin_lock_irqsave(&phba->hbalock, iflags);
9739 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9740 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9741 spin_unlock_irqrestore(&phba->hbalock, iflags);
9742 if (unlikely(!cmdiocbq)) {
9743 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9744 "0374 FCP complete with no corresponding "
9745 "cmdiocb: iotag (%d)\n",
9746 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9747 return;
9748 }
9749 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9751 "0375 FCP cmdiocb not callback function "
9752 "iotag: (%d)\n",
9753 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9754 return;
9755 }
9756
9757 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009758 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009759
James Smart0f65ff62010-02-26 14:14:23 -05009760 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9761 spin_lock_irqsave(&phba->hbalock, iflags);
9762 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9763 spin_unlock_irqrestore(&phba->hbalock, iflags);
9764 }
9765
James Smart4f774512009-05-22 14:52:35 -04009766 /* Pass the cmd_iocb and the rsp state to the upper layer */
9767 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9768}
9769
9770/**
9771 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9772 * @phba: Pointer to HBA context object.
9773 * @cq: Pointer to completion queue.
9774 * @wcqe: Pointer to work-queue completion queue entry.
9775 *
9776 * This routine handles an fast-path WQ entry comsumed event by invoking the
9777 * proper WQ release routine to the slow-path WQ.
9778 **/
9779static void
9780lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9781 struct lpfc_wcqe_release *wcqe)
9782{
9783 struct lpfc_queue *childwq;
9784 bool wqid_matched = false;
9785 uint16_t fcp_wqid;
9786
9787 /* Check for fast-path FCP work queue release */
9788 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9789 list_for_each_entry(childwq, &cq->child_list, list) {
9790 if (childwq->queue_id == fcp_wqid) {
9791 lpfc_sli4_wq_release(childwq,
9792 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9793 wqid_matched = true;
9794 break;
9795 }
9796 }
9797 /* Report warning log message if no match found */
9798 if (wqid_matched != true)
9799 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9800 "2580 Fast-path wqe consume event carries "
9801 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9802}
9803
9804/**
9805 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9806 * @cq: Pointer to the completion queue.
9807 * @eqe: Pointer to fast-path completion queue entry.
9808 *
9809 * This routine process a fast-path work queue completion entry from fast-path
9810 * event queue for FCP command response completion.
9811 **/
9812static int
9813lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9814 struct lpfc_cqe *cqe)
9815{
9816 struct lpfc_wcqe_release wcqe;
9817 bool workposted = false;
9818
9819 /* Copy the work queue CQE and convert endian order if needed */
9820 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9821
9822 /* Check and process for different type of WCQE and dispatch */
9823 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9824 case CQE_CODE_COMPL_WQE:
9825 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009826 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009827 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9828 (struct lpfc_wcqe_complete *)&wcqe);
9829 break;
9830 case CQE_CODE_RELEASE_WQE:
9831 /* Process the WQ release event */
9832 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9833 (struct lpfc_wcqe_release *)&wcqe);
9834 break;
9835 case CQE_CODE_XRI_ABORTED:
9836 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009837 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009838 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9839 (struct sli4_wcqe_xri_aborted *)&wcqe);
9840 break;
9841 default:
9842 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9843 "0144 Not a valid WCQE code: x%x\n",
9844 bf_get(lpfc_wcqe_c_code, &wcqe));
9845 break;
9846 }
9847 return workposted;
9848}
9849
9850/**
9851 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9852 * @phba: Pointer to HBA context object.
9853 * @eqe: Pointer to fast-path event queue entry.
9854 *
9855 * This routine process a event queue entry from the fast-path event queue.
9856 * It will check the MajorCode and MinorCode to determine this is for a
9857 * completion event on a completion queue, if not, an error shall be logged
9858 * and just return. Otherwise, it will get to the corresponding completion
9859 * queue and process all the entries on the completion queue, rearm the
9860 * completion queue, and then return.
9861 **/
9862static void
9863lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9864 uint32_t fcp_cqidx)
9865{
9866 struct lpfc_queue *cq;
9867 struct lpfc_cqe *cqe;
9868 bool workposted = false;
9869 uint16_t cqid;
9870 int ecount = 0;
9871
James Smartcb5172e2010-03-15 11:25:07 -04009872 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009873 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9874 "0366 Not a valid fast-path completion "
9875 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009876 bf_get_le32(lpfc_eqe_major_code, eqe),
9877 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009878 return;
9879 }
9880
9881 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9882 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009883 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9885 "0367 Fast-path completion queue "
9886 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009887 return;
9888 }
9889
9890 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009891 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009892 if (unlikely(cqid != cq->queue_id)) {
9893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9894 "0368 Miss-matched fast-path completion "
9895 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9896 cqid, cq->queue_id);
9897 return;
9898 }
9899
9900 /* Process all the entries to the CQ */
9901 while ((cqe = lpfc_sli4_cq_get(cq))) {
9902 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9903 if (!(++ecount % LPFC_GET_QE_REL_INT))
9904 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9905 }
9906
9907 /* Catch the no cq entry condition */
9908 if (unlikely(ecount == 0))
9909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9910 "0369 No entry from fast-path completion "
9911 "queue fcpcqid=%d\n", cq->queue_id);
9912
9913 /* In any case, flash and re-arm the CQ */
9914 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9915
9916 /* wake up worker thread if there are works to be done */
9917 if (workposted)
9918 lpfc_worker_wake_up(phba);
9919}
9920
9921static void
9922lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9923{
9924 struct lpfc_eqe *eqe;
9925
9926 /* walk all the EQ entries and drop on the floor */
9927 while ((eqe = lpfc_sli4_eq_get(eq)))
9928 ;
9929
9930 /* Clear and re-arm the EQ */
9931 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9932}
9933
9934/**
9935 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9936 * @irq: Interrupt number.
9937 * @dev_id: The device context pointer.
9938 *
9939 * This function is directly called from the PCI layer as an interrupt
9940 * service routine when device with SLI-4 interface spec is enabled with
9941 * MSI-X multi-message interrupt mode and there are slow-path events in
9942 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9943 * interrupt mode, this function is called as part of the device-level
9944 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9945 * undergoing initialization, the interrupt handler will not process the
9946 * interrupt. The link attention and ELS ring attention events are handled
9947 * by the worker thread. The interrupt handler signals the worker thread
9948 * and returns for these events. This function is called without any lock
9949 * held. It gets the hbalock to access and update SLI data structures.
9950 *
9951 * This function returns IRQ_HANDLED when interrupt is handled else it
9952 * returns IRQ_NONE.
9953 **/
9954irqreturn_t
9955lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9956{
9957 struct lpfc_hba *phba;
9958 struct lpfc_queue *speq;
9959 struct lpfc_eqe *eqe;
9960 unsigned long iflag;
9961 int ecount = 0;
9962
9963 /*
9964 * Get the driver's phba structure from the dev_id
9965 */
9966 phba = (struct lpfc_hba *)dev_id;
9967
9968 if (unlikely(!phba))
9969 return IRQ_NONE;
9970
9971 /* Get to the EQ struct associated with this vector */
9972 speq = phba->sli4_hba.sp_eq;
9973
9974 /* Check device state for handling interrupt */
9975 if (unlikely(lpfc_intr_state_check(phba))) {
9976 /* Check again for link_state with lock held */
9977 spin_lock_irqsave(&phba->hbalock, iflag);
9978 if (phba->link_state < LPFC_LINK_DOWN)
9979 /* Flush, clear interrupt, and rearm the EQ */
9980 lpfc_sli4_eq_flush(phba, speq);
9981 spin_unlock_irqrestore(&phba->hbalock, iflag);
9982 return IRQ_NONE;
9983 }
9984
9985 /*
9986 * Process all the event on FCP slow-path EQ
9987 */
9988 while ((eqe = lpfc_sli4_eq_get(speq))) {
9989 lpfc_sli4_sp_handle_eqe(phba, eqe);
9990 if (!(++ecount % LPFC_GET_QE_REL_INT))
9991 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9992 }
9993
9994 /* Always clear and re-arm the slow-path EQ */
9995 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9996
9997 /* Catch the no cq entry condition */
9998 if (unlikely(ecount == 0)) {
9999 if (phba->intr_type == MSIX)
10000 /* MSI-X treated interrupt served as no EQ share INT */
10001 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10002 "0357 MSI-X interrupt with no EQE\n");
10003 else
10004 /* Non MSI-X treated on interrupt as EQ share INT */
10005 return IRQ_NONE;
10006 }
10007
10008 return IRQ_HANDLED;
10009} /* lpfc_sli4_sp_intr_handler */
10010
10011/**
10012 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10013 * @irq: Interrupt number.
10014 * @dev_id: The device context pointer.
10015 *
10016 * This function is directly called from the PCI layer as an interrupt
10017 * service routine when device with SLI-4 interface spec is enabled with
10018 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10019 * ring event in the HBA. However, when the device is enabled with either
10020 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10021 * device-level interrupt handler. When the PCI slot is in error recovery
10022 * or the HBA is undergoing initialization, the interrupt handler will not
10023 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10024 * the intrrupt context. This function is called without any lock held.
10025 * It gets the hbalock to access and update SLI data structures. Note that,
10026 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10027 * equal to that of FCP CQ index.
10028 *
10029 * This function returns IRQ_HANDLED when interrupt is handled else it
10030 * returns IRQ_NONE.
10031 **/
10032irqreturn_t
10033lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10034{
10035 struct lpfc_hba *phba;
10036 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10037 struct lpfc_queue *fpeq;
10038 struct lpfc_eqe *eqe;
10039 unsigned long iflag;
10040 int ecount = 0;
10041 uint32_t fcp_eqidx;
10042
10043 /* Get the driver's phba structure from the dev_id */
10044 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10045 phba = fcp_eq_hdl->phba;
10046 fcp_eqidx = fcp_eq_hdl->idx;
10047
10048 if (unlikely(!phba))
10049 return IRQ_NONE;
10050
10051 /* Get to the EQ struct associated with this vector */
10052 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10053
10054 /* Check device state for handling interrupt */
10055 if (unlikely(lpfc_intr_state_check(phba))) {
10056 /* Check again for link_state with lock held */
10057 spin_lock_irqsave(&phba->hbalock, iflag);
10058 if (phba->link_state < LPFC_LINK_DOWN)
10059 /* Flush, clear interrupt, and rearm the EQ */
10060 lpfc_sli4_eq_flush(phba, fpeq);
10061 spin_unlock_irqrestore(&phba->hbalock, iflag);
10062 return IRQ_NONE;
10063 }
10064
10065 /*
10066 * Process all the event on FCP fast-path EQ
10067 */
10068 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10069 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10070 if (!(++ecount % LPFC_GET_QE_REL_INT))
10071 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10072 }
10073
10074 /* Always clear and re-arm the fast-path EQ */
10075 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10076
10077 if (unlikely(ecount == 0)) {
10078 if (phba->intr_type == MSIX)
10079 /* MSI-X treated interrupt served as no EQ share INT */
10080 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10081 "0358 MSI-X interrupt with no EQE\n");
10082 else
10083 /* Non MSI-X treated on interrupt as EQ share INT */
10084 return IRQ_NONE;
10085 }
10086
10087 return IRQ_HANDLED;
10088} /* lpfc_sli4_fp_intr_handler */
10089
10090/**
10091 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10092 * @irq: Interrupt number.
10093 * @dev_id: The device context pointer.
10094 *
10095 * This function is the device-level interrupt handler to device with SLI-4
10096 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10097 * interrupt mode is enabled and there is an event in the HBA which requires
10098 * driver attention. This function invokes the slow-path interrupt attention
10099 * handling function and fast-path interrupt attention handling function in
10100 * turn to process the relevant HBA attention events. This function is called
10101 * without any lock held. It gets the hbalock to access and update SLI data
10102 * structures.
10103 *
10104 * This function returns IRQ_HANDLED when interrupt is handled, else it
10105 * returns IRQ_NONE.
10106 **/
10107irqreturn_t
10108lpfc_sli4_intr_handler(int irq, void *dev_id)
10109{
10110 struct lpfc_hba *phba;
10111 irqreturn_t sp_irq_rc, fp_irq_rc;
10112 bool fp_handled = false;
10113 uint32_t fcp_eqidx;
10114
10115 /* Get the driver's phba structure from the dev_id */
10116 phba = (struct lpfc_hba *)dev_id;
10117
10118 if (unlikely(!phba))
10119 return IRQ_NONE;
10120
10121 /*
10122 * Invokes slow-path host attention interrupt handling as appropriate.
10123 */
10124 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10125
10126 /*
10127 * Invoke fast-path host attention interrupt handling as appropriate.
10128 */
10129 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10130 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10131 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10132 if (fp_irq_rc == IRQ_HANDLED)
10133 fp_handled |= true;
10134 }
10135
10136 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10137} /* lpfc_sli4_intr_handler */
10138
10139/**
10140 * lpfc_sli4_queue_free - free a queue structure and associated memory
10141 * @queue: The queue structure to free.
10142 *
10143 * This function frees a queue structure and the DMAable memeory used for
10144 * the host resident queue. This function must be called after destroying the
10145 * queue on the HBA.
10146 **/
10147void
10148lpfc_sli4_queue_free(struct lpfc_queue *queue)
10149{
10150 struct lpfc_dmabuf *dmabuf;
10151
10152 if (!queue)
10153 return;
10154
10155 while (!list_empty(&queue->page_list)) {
10156 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10157 list);
James Smart49198b32010-04-06 15:04:33 -040010158 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040010159 dmabuf->virt, dmabuf->phys);
10160 kfree(dmabuf);
10161 }
10162 kfree(queue);
10163 return;
10164}
10165
10166/**
10167 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10168 * @phba: The HBA that this queue is being created on.
10169 * @entry_size: The size of each queue entry for this queue.
10170 * @entry count: The number of entries that this queue will handle.
10171 *
10172 * This function allocates a queue structure and the DMAable memory used for
10173 * the host resident queue. This function must be called before creating the
10174 * queue on the HBA.
10175 **/
10176struct lpfc_queue *
10177lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10178 uint32_t entry_count)
10179{
10180 struct lpfc_queue *queue;
10181 struct lpfc_dmabuf *dmabuf;
10182 int x, total_qe_count;
10183 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040010184 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040010185
James Smartcb5172e2010-03-15 11:25:07 -040010186 if (!phba->sli4_hba.pc_sli4_params.supported)
10187 hw_page_size = SLI4_PAGE_SIZE;
10188
James Smart4f774512009-05-22 14:52:35 -040010189 queue = kzalloc(sizeof(struct lpfc_queue) +
10190 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10191 if (!queue)
10192 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040010193 queue->page_count = (ALIGN(entry_size * entry_count,
10194 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040010195 INIT_LIST_HEAD(&queue->list);
10196 INIT_LIST_HEAD(&queue->page_list);
10197 INIT_LIST_HEAD(&queue->child_list);
10198 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10199 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10200 if (!dmabuf)
10201 goto out_fail;
10202 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040010203 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040010204 GFP_KERNEL);
10205 if (!dmabuf->virt) {
10206 kfree(dmabuf);
10207 goto out_fail;
10208 }
James Smartcb5172e2010-03-15 11:25:07 -040010209 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010210 dmabuf->buffer_tag = x;
10211 list_add_tail(&dmabuf->list, &queue->page_list);
10212 /* initialize queue's entry array */
10213 dma_pointer = dmabuf->virt;
10214 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040010215 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040010216 total_qe_count++, dma_pointer += entry_size) {
10217 queue->qe[total_qe_count].address = dma_pointer;
10218 }
10219 }
10220 queue->entry_size = entry_size;
10221 queue->entry_count = entry_count;
10222 queue->phba = phba;
10223
10224 return queue;
10225out_fail:
10226 lpfc_sli4_queue_free(queue);
10227 return NULL;
10228}
10229
10230/**
10231 * lpfc_eq_create - Create an Event Queue on the HBA
10232 * @phba: HBA structure that indicates port to create a queue on.
10233 * @eq: The queue structure to use to create the event queue.
10234 * @imax: The maximum interrupt per second limit.
10235 *
10236 * This function creates an event queue, as detailed in @eq, on a port,
10237 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10238 *
10239 * The @phba struct is used to send mailbox command to HBA. The @eq struct
10240 * is used to get the entry count and entry size that are necessary to
10241 * determine the number of pages to allocate and use for this queue. This
10242 * function will send the EQ_CREATE mailbox command to the HBA to setup the
10243 * event queue. This function is asynchronous and will wait for the mailbox
10244 * command to finish before continuing.
10245 *
10246 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010247 * memory this function will return -ENOMEM. If the queue create mailbox command
10248 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010249 **/
10250uint32_t
10251lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10252{
10253 struct lpfc_mbx_eq_create *eq_create;
10254 LPFC_MBOXQ_t *mbox;
10255 int rc, length, status = 0;
10256 struct lpfc_dmabuf *dmabuf;
10257 uint32_t shdr_status, shdr_add_status;
10258 union lpfc_sli4_cfg_shdr *shdr;
10259 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040010260 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10261
10262 if (!phba->sli4_hba.pc_sli4_params.supported)
10263 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010264
10265 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10266 if (!mbox)
10267 return -ENOMEM;
10268 length = (sizeof(struct lpfc_mbx_eq_create) -
10269 sizeof(struct lpfc_sli4_cfg_mhdr));
10270 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10271 LPFC_MBOX_OPCODE_EQ_CREATE,
10272 length, LPFC_SLI4_MBX_EMBED);
10273 eq_create = &mbox->u.mqe.un.eq_create;
10274 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10275 eq->page_count);
10276 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10277 LPFC_EQE_SIZE);
10278 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10279 /* Calculate delay multiper from maximum interrupt per second */
10280 dmult = LPFC_DMULT_CONST/imax - 1;
10281 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10282 dmult);
10283 switch (eq->entry_count) {
10284 default:
10285 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10286 "0360 Unsupported EQ count. (%d)\n",
10287 eq->entry_count);
10288 if (eq->entry_count < 256)
10289 return -EINVAL;
10290 /* otherwise default to smallest count (drop through) */
10291 case 256:
10292 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10293 LPFC_EQ_CNT_256);
10294 break;
10295 case 512:
10296 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10297 LPFC_EQ_CNT_512);
10298 break;
10299 case 1024:
10300 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10301 LPFC_EQ_CNT_1024);
10302 break;
10303 case 2048:
10304 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10305 LPFC_EQ_CNT_2048);
10306 break;
10307 case 4096:
10308 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10309 LPFC_EQ_CNT_4096);
10310 break;
10311 }
10312 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010313 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010314 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10315 putPaddrLow(dmabuf->phys);
10316 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10317 putPaddrHigh(dmabuf->phys);
10318 }
10319 mbox->vport = phba->pport;
10320 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10321 mbox->context1 = NULL;
10322 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10323 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10324 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10325 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10326 if (shdr_status || shdr_add_status || rc) {
10327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10328 "2500 EQ_CREATE mailbox failed with "
10329 "status x%x add_status x%x, mbx status x%x\n",
10330 shdr_status, shdr_add_status, rc);
10331 status = -ENXIO;
10332 }
10333 eq->type = LPFC_EQ;
10334 eq->subtype = LPFC_NONE;
10335 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10336 if (eq->queue_id == 0xFFFF)
10337 status = -ENXIO;
10338 eq->host_index = 0;
10339 eq->hba_index = 0;
10340
James Smart8fa38512009-07-19 10:01:03 -040010341 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010342 return status;
10343}
10344
10345/**
10346 * lpfc_cq_create - Create a Completion Queue on the HBA
10347 * @phba: HBA structure that indicates port to create a queue on.
10348 * @cq: The queue structure to use to create the completion queue.
10349 * @eq: The event queue to bind this completion queue to.
10350 *
10351 * This function creates a completion queue, as detailed in @wq, on a port,
10352 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10353 *
10354 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10355 * is used to get the entry count and entry size that are necessary to
10356 * determine the number of pages to allocate and use for this queue. The @eq
10357 * is used to indicate which event queue to bind this completion queue to. This
10358 * function will send the CQ_CREATE mailbox command to the HBA to setup the
10359 * completion queue. This function is asynchronous and will wait for the mailbox
10360 * command to finish before continuing.
10361 *
10362 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010363 * memory this function will return -ENOMEM. If the queue create mailbox command
10364 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010365 **/
10366uint32_t
10367lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10368 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10369{
10370 struct lpfc_mbx_cq_create *cq_create;
10371 struct lpfc_dmabuf *dmabuf;
10372 LPFC_MBOXQ_t *mbox;
10373 int rc, length, status = 0;
10374 uint32_t shdr_status, shdr_add_status;
10375 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010376 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10377
10378 if (!phba->sli4_hba.pc_sli4_params.supported)
10379 hw_page_size = SLI4_PAGE_SIZE;
10380
James Smart4f774512009-05-22 14:52:35 -040010381
10382 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10383 if (!mbox)
10384 return -ENOMEM;
10385 length = (sizeof(struct lpfc_mbx_cq_create) -
10386 sizeof(struct lpfc_sli4_cfg_mhdr));
10387 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10388 LPFC_MBOX_OPCODE_CQ_CREATE,
10389 length, LPFC_SLI4_MBX_EMBED);
10390 cq_create = &mbox->u.mqe.un.cq_create;
10391 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10392 cq->page_count);
10393 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10394 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10395 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10396 switch (cq->entry_count) {
10397 default:
10398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10399 "0361 Unsupported CQ count. (%d)\n",
10400 cq->entry_count);
10401 if (cq->entry_count < 256)
10402 return -EINVAL;
10403 /* otherwise default to smallest count (drop through) */
10404 case 256:
10405 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10406 LPFC_CQ_CNT_256);
10407 break;
10408 case 512:
10409 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10410 LPFC_CQ_CNT_512);
10411 break;
10412 case 1024:
10413 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10414 LPFC_CQ_CNT_1024);
10415 break;
10416 }
10417 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010418 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010419 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10420 putPaddrLow(dmabuf->phys);
10421 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10422 putPaddrHigh(dmabuf->phys);
10423 }
10424 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10425
10426 /* The IOCTL status is embedded in the mailbox subheader. */
10427 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10428 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10429 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10430 if (shdr_status || shdr_add_status || rc) {
10431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10432 "2501 CQ_CREATE mailbox failed with "
10433 "status x%x add_status x%x, mbx status x%x\n",
10434 shdr_status, shdr_add_status, rc);
10435 status = -ENXIO;
10436 goto out;
10437 }
10438 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10439 if (cq->queue_id == 0xFFFF) {
10440 status = -ENXIO;
10441 goto out;
10442 }
10443 /* link the cq onto the parent eq child list */
10444 list_add_tail(&cq->list, &eq->child_list);
10445 /* Set up completion queue's type and subtype */
10446 cq->type = type;
10447 cq->subtype = subtype;
10448 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10449 cq->host_index = 0;
10450 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010451
James Smart8fa38512009-07-19 10:01:03 -040010452out:
10453 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010454 return status;
10455}
10456
10457/**
James Smartb19a0612010-04-06 14:48:51 -040010458 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010459 * @phba: HBA structure that indicates port to create a queue on.
10460 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010461 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10462 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010463 *
James Smartb19a0612010-04-06 14:48:51 -040010464 * This function provides failback (fb) functionality when the
10465 * mq_create_ext fails on older FW generations. It's purpose is identical
10466 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010467 *
James Smartb19a0612010-04-06 14:48:51 -040010468 * This routine cannot fail as all attributes were previously accessed and
10469 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010470 **/
James Smartb19a0612010-04-06 14:48:51 -040010471static void
10472lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10473 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010474{
10475 struct lpfc_mbx_mq_create *mq_create;
10476 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010477 int length;
James Smart04c68492009-05-22 14:52:52 -040010478
James Smart04c68492009-05-22 14:52:52 -040010479 length = (sizeof(struct lpfc_mbx_mq_create) -
10480 sizeof(struct lpfc_sli4_cfg_mhdr));
10481 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10482 LPFC_MBOX_OPCODE_MQ_CREATE,
10483 length, LPFC_SLI4_MBX_EMBED);
10484 mq_create = &mbox->u.mqe.un.mq_create;
10485 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010486 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010487 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010488 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010489 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10490 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010491 case 16:
10492 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10493 LPFC_MQ_CNT_16);
10494 break;
10495 case 32:
10496 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10497 LPFC_MQ_CNT_32);
10498 break;
10499 case 64:
10500 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10501 LPFC_MQ_CNT_64);
10502 break;
10503 case 128:
10504 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10505 LPFC_MQ_CNT_128);
10506 break;
10507 }
10508 list_for_each_entry(dmabuf, &mq->page_list, list) {
10509 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010510 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010511 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010512 putPaddrHigh(dmabuf->phys);
10513 }
10514}
10515
10516/**
10517 * lpfc_mq_create - Create a mailbox Queue on the HBA
10518 * @phba: HBA structure that indicates port to create a queue on.
10519 * @mq: The queue structure to use to create the mailbox queue.
10520 * @cq: The completion queue to associate with this cq.
10521 * @subtype: The queue's subtype.
10522 *
10523 * This function creates a mailbox queue, as detailed in @mq, on a port,
10524 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10525 *
10526 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10527 * is used to get the entry count and entry size that are necessary to
10528 * determine the number of pages to allocate and use for this queue. This
10529 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10530 * mailbox queue. This function is asynchronous and will wait for the mailbox
10531 * command to finish before continuing.
10532 *
10533 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010534 * memory this function will return -ENOMEM. If the queue create mailbox command
10535 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010536 **/
10537int32_t
10538lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10539 struct lpfc_queue *cq, uint32_t subtype)
10540{
10541 struct lpfc_mbx_mq_create *mq_create;
10542 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10543 struct lpfc_dmabuf *dmabuf;
10544 LPFC_MBOXQ_t *mbox;
10545 int rc, length, status = 0;
10546 uint32_t shdr_status, shdr_add_status;
10547 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010548 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010549
James Smart49198b32010-04-06 15:04:33 -040010550 if (!phba->sli4_hba.pc_sli4_params.supported)
10551 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010552
10553 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10554 if (!mbox)
10555 return -ENOMEM;
10556 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10557 sizeof(struct lpfc_sli4_cfg_mhdr));
10558 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10559 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10560 length, LPFC_SLI4_MBX_EMBED);
10561
10562 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10563 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10564 mq->page_count);
10565 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10566 1);
10567 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10568 &mq_create_ext->u.request, 1);
10569 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10570 &mq_create_ext->u.request, 1);
10571 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10572 cq->queue_id);
10573 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10574 switch (mq->entry_count) {
10575 default:
10576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10577 "0362 Unsupported MQ count. (%d)\n",
10578 mq->entry_count);
10579 if (mq->entry_count < 16)
10580 return -EINVAL;
10581 /* otherwise default to smallest count (drop through) */
10582 case 16:
10583 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10584 LPFC_MQ_CNT_16);
10585 break;
10586 case 32:
10587 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10588 LPFC_MQ_CNT_32);
10589 break;
10590 case 64:
10591 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10592 LPFC_MQ_CNT_64);
10593 break;
10594 case 128:
10595 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10596 LPFC_MQ_CNT_128);
10597 break;
10598 }
10599 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010600 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010601 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10602 putPaddrLow(dmabuf->phys);
10603 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010604 putPaddrHigh(dmabuf->phys);
10605 }
10606 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010607 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10608 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10609 &mq_create_ext->u.response);
10610 if (rc != MBX_SUCCESS) {
10611 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10612 "2795 MQ_CREATE_EXT failed with "
10613 "status x%x. Failback to MQ_CREATE.\n",
10614 rc);
10615 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10616 mq_create = &mbox->u.mqe.un.mq_create;
10617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10618 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10619 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10620 &mq_create->u.response);
10621 }
10622
James Smart04c68492009-05-22 14:52:52 -040010623 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10626 if (shdr_status || shdr_add_status || rc) {
10627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10628 "2502 MQ_CREATE mailbox failed with "
10629 "status x%x add_status x%x, mbx status x%x\n",
10630 shdr_status, shdr_add_status, rc);
10631 status = -ENXIO;
10632 goto out;
10633 }
James Smart04c68492009-05-22 14:52:52 -040010634 if (mq->queue_id == 0xFFFF) {
10635 status = -ENXIO;
10636 goto out;
10637 }
10638 mq->type = LPFC_MQ;
10639 mq->subtype = subtype;
10640 mq->host_index = 0;
10641 mq->hba_index = 0;
10642
10643 /* link the mq onto the parent cq child list */
10644 list_add_tail(&mq->list, &cq->child_list);
10645out:
James Smart8fa38512009-07-19 10:01:03 -040010646 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010647 return status;
10648}
10649
10650/**
James Smart4f774512009-05-22 14:52:35 -040010651 * lpfc_wq_create - Create a Work Queue on the HBA
10652 * @phba: HBA structure that indicates port to create a queue on.
10653 * @wq: The queue structure to use to create the work queue.
10654 * @cq: The completion queue to bind this work queue to.
10655 * @subtype: The subtype of the work queue indicating its functionality.
10656 *
10657 * This function creates a work queue, as detailed in @wq, on a port, described
10658 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10659 *
10660 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10661 * is used to get the entry count and entry size that are necessary to
10662 * determine the number of pages to allocate and use for this queue. The @cq
10663 * is used to indicate which completion queue to bind this work queue to. This
10664 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10665 * work queue. This function is asynchronous and will wait for the mailbox
10666 * command to finish before continuing.
10667 *
10668 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010669 * memory this function will return -ENOMEM. If the queue create mailbox command
10670 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010671 **/
10672uint32_t
10673lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10674 struct lpfc_queue *cq, uint32_t subtype)
10675{
10676 struct lpfc_mbx_wq_create *wq_create;
10677 struct lpfc_dmabuf *dmabuf;
10678 LPFC_MBOXQ_t *mbox;
10679 int rc, length, status = 0;
10680 uint32_t shdr_status, shdr_add_status;
10681 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010682 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10683
10684 if (!phba->sli4_hba.pc_sli4_params.supported)
10685 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010686
10687 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10688 if (!mbox)
10689 return -ENOMEM;
10690 length = (sizeof(struct lpfc_mbx_wq_create) -
10691 sizeof(struct lpfc_sli4_cfg_mhdr));
10692 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10693 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10694 length, LPFC_SLI4_MBX_EMBED);
10695 wq_create = &mbox->u.mqe.un.wq_create;
10696 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10697 wq->page_count);
10698 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10699 cq->queue_id);
10700 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010701 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010702 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10703 putPaddrLow(dmabuf->phys);
10704 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10705 putPaddrHigh(dmabuf->phys);
10706 }
10707 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10708 /* The IOCTL status is embedded in the mailbox subheader. */
10709 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10710 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10711 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10712 if (shdr_status || shdr_add_status || rc) {
10713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10714 "2503 WQ_CREATE mailbox failed with "
10715 "status x%x add_status x%x, mbx status x%x\n",
10716 shdr_status, shdr_add_status, rc);
10717 status = -ENXIO;
10718 goto out;
10719 }
10720 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10721 if (wq->queue_id == 0xFFFF) {
10722 status = -ENXIO;
10723 goto out;
10724 }
10725 wq->type = LPFC_WQ;
10726 wq->subtype = subtype;
10727 wq->host_index = 0;
10728 wq->hba_index = 0;
10729
10730 /* link the wq onto the parent cq child list */
10731 list_add_tail(&wq->list, &cq->child_list);
10732out:
James Smart8fa38512009-07-19 10:01:03 -040010733 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010734 return status;
10735}
10736
10737/**
10738 * lpfc_rq_create - Create a Receive Queue on the HBA
10739 * @phba: HBA structure that indicates port to create a queue on.
10740 * @hrq: The queue structure to use to create the header receive queue.
10741 * @drq: The queue structure to use to create the data receive queue.
10742 * @cq: The completion queue to bind this work queue to.
10743 *
10744 * This function creates a receive buffer queue pair , as detailed in @hrq and
10745 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10746 * to the HBA.
10747 *
10748 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10749 * struct is used to get the entry count that is necessary to determine the
10750 * number of pages to use for this queue. The @cq is used to indicate which
10751 * completion queue to bind received buffers that are posted to these queues to.
10752 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10753 * receive queue pair. This function is asynchronous and will wait for the
10754 * mailbox command to finish before continuing.
10755 *
10756 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010757 * memory this function will return -ENOMEM. If the queue create mailbox command
10758 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010759 **/
10760uint32_t
10761lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10762 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10763{
10764 struct lpfc_mbx_rq_create *rq_create;
10765 struct lpfc_dmabuf *dmabuf;
10766 LPFC_MBOXQ_t *mbox;
10767 int rc, length, status = 0;
10768 uint32_t shdr_status, shdr_add_status;
10769 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010770 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10771
10772 if (!phba->sli4_hba.pc_sli4_params.supported)
10773 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010774
10775 if (hrq->entry_count != drq->entry_count)
10776 return -EINVAL;
10777 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10778 if (!mbox)
10779 return -ENOMEM;
10780 length = (sizeof(struct lpfc_mbx_rq_create) -
10781 sizeof(struct lpfc_sli4_cfg_mhdr));
10782 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10783 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10784 length, LPFC_SLI4_MBX_EMBED);
10785 rq_create = &mbox->u.mqe.un.rq_create;
10786 switch (hrq->entry_count) {
10787 default:
10788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10789 "2535 Unsupported RQ count. (%d)\n",
10790 hrq->entry_count);
10791 if (hrq->entry_count < 512)
10792 return -EINVAL;
10793 /* otherwise default to smallest count (drop through) */
10794 case 512:
10795 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10796 LPFC_RQ_RING_SIZE_512);
10797 break;
10798 case 1024:
10799 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10800 LPFC_RQ_RING_SIZE_1024);
10801 break;
10802 case 2048:
10803 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10804 LPFC_RQ_RING_SIZE_2048);
10805 break;
10806 case 4096:
10807 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10808 LPFC_RQ_RING_SIZE_4096);
10809 break;
10810 }
10811 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10812 cq->queue_id);
10813 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10814 hrq->page_count);
10815 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10816 LPFC_HDR_BUF_SIZE);
10817 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010818 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010819 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10820 putPaddrLow(dmabuf->phys);
10821 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10822 putPaddrHigh(dmabuf->phys);
10823 }
10824 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10825 /* The IOCTL status is embedded in the mailbox subheader. */
10826 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10827 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10828 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10829 if (shdr_status || shdr_add_status || rc) {
10830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10831 "2504 RQ_CREATE mailbox failed with "
10832 "status x%x add_status x%x, mbx status x%x\n",
10833 shdr_status, shdr_add_status, rc);
10834 status = -ENXIO;
10835 goto out;
10836 }
10837 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10838 if (hrq->queue_id == 0xFFFF) {
10839 status = -ENXIO;
10840 goto out;
10841 }
10842 hrq->type = LPFC_HRQ;
10843 hrq->subtype = subtype;
10844 hrq->host_index = 0;
10845 hrq->hba_index = 0;
10846
10847 /* now create the data queue */
10848 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10849 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10850 length, LPFC_SLI4_MBX_EMBED);
10851 switch (drq->entry_count) {
10852 default:
10853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10854 "2536 Unsupported RQ count. (%d)\n",
10855 drq->entry_count);
10856 if (drq->entry_count < 512)
10857 return -EINVAL;
10858 /* otherwise default to smallest count (drop through) */
10859 case 512:
10860 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10861 LPFC_RQ_RING_SIZE_512);
10862 break;
10863 case 1024:
10864 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10865 LPFC_RQ_RING_SIZE_1024);
10866 break;
10867 case 2048:
10868 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10869 LPFC_RQ_RING_SIZE_2048);
10870 break;
10871 case 4096:
10872 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10873 LPFC_RQ_RING_SIZE_4096);
10874 break;
10875 }
10876 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10877 cq->queue_id);
10878 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10879 drq->page_count);
10880 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10881 LPFC_DATA_BUF_SIZE);
10882 list_for_each_entry(dmabuf, &drq->page_list, list) {
10883 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10884 putPaddrLow(dmabuf->phys);
10885 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10886 putPaddrHigh(dmabuf->phys);
10887 }
10888 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10889 /* The IOCTL status is embedded in the mailbox subheader. */
10890 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10891 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10892 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10893 if (shdr_status || shdr_add_status || rc) {
10894 status = -ENXIO;
10895 goto out;
10896 }
10897 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10898 if (drq->queue_id == 0xFFFF) {
10899 status = -ENXIO;
10900 goto out;
10901 }
10902 drq->type = LPFC_DRQ;
10903 drq->subtype = subtype;
10904 drq->host_index = 0;
10905 drq->hba_index = 0;
10906
10907 /* link the header and data RQs onto the parent cq child list */
10908 list_add_tail(&hrq->list, &cq->child_list);
10909 list_add_tail(&drq->list, &cq->child_list);
10910
10911out:
James Smart8fa38512009-07-19 10:01:03 -040010912 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010913 return status;
10914}
10915
10916/**
10917 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10918 * @eq: The queue structure associated with the queue to destroy.
10919 *
10920 * This function destroys a queue, as detailed in @eq by sending an mailbox
10921 * command, specific to the type of queue, to the HBA.
10922 *
10923 * The @eq struct is used to get the queue ID of the queue to destroy.
10924 *
10925 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010926 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010927 **/
10928uint32_t
10929lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10930{
10931 LPFC_MBOXQ_t *mbox;
10932 int rc, length, status = 0;
10933 uint32_t shdr_status, shdr_add_status;
10934 union lpfc_sli4_cfg_shdr *shdr;
10935
10936 if (!eq)
10937 return -ENODEV;
10938 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10939 if (!mbox)
10940 return -ENOMEM;
10941 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10942 sizeof(struct lpfc_sli4_cfg_mhdr));
10943 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10944 LPFC_MBOX_OPCODE_EQ_DESTROY,
10945 length, LPFC_SLI4_MBX_EMBED);
10946 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10947 eq->queue_id);
10948 mbox->vport = eq->phba->pport;
10949 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10950
10951 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10952 /* The IOCTL status is embedded in the mailbox subheader. */
10953 shdr = (union lpfc_sli4_cfg_shdr *)
10954 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10955 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10956 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10957 if (shdr_status || shdr_add_status || rc) {
10958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10959 "2505 EQ_DESTROY mailbox failed with "
10960 "status x%x add_status x%x, mbx status x%x\n",
10961 shdr_status, shdr_add_status, rc);
10962 status = -ENXIO;
10963 }
10964
10965 /* Remove eq from any list */
10966 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010967 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010968 return status;
10969}
10970
10971/**
10972 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10973 * @cq: The queue structure associated with the queue to destroy.
10974 *
10975 * This function destroys a queue, as detailed in @cq by sending an mailbox
10976 * command, specific to the type of queue, to the HBA.
10977 *
10978 * The @cq struct is used to get the queue ID of the queue to destroy.
10979 *
10980 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010981 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010982 **/
10983uint32_t
10984lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10985{
10986 LPFC_MBOXQ_t *mbox;
10987 int rc, length, status = 0;
10988 uint32_t shdr_status, shdr_add_status;
10989 union lpfc_sli4_cfg_shdr *shdr;
10990
10991 if (!cq)
10992 return -ENODEV;
10993 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10994 if (!mbox)
10995 return -ENOMEM;
10996 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10997 sizeof(struct lpfc_sli4_cfg_mhdr));
10998 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10999 LPFC_MBOX_OPCODE_CQ_DESTROY,
11000 length, LPFC_SLI4_MBX_EMBED);
11001 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11002 cq->queue_id);
11003 mbox->vport = cq->phba->pport;
11004 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11005 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11006 /* The IOCTL status is embedded in the mailbox subheader. */
11007 shdr = (union lpfc_sli4_cfg_shdr *)
11008 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11009 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11010 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11011 if (shdr_status || shdr_add_status || rc) {
11012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11013 "2506 CQ_DESTROY mailbox failed with "
11014 "status x%x add_status x%x, mbx status x%x\n",
11015 shdr_status, shdr_add_status, rc);
11016 status = -ENXIO;
11017 }
11018 /* Remove cq from any list */
11019 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040011020 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011021 return status;
11022}
11023
11024/**
James Smart04c68492009-05-22 14:52:52 -040011025 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11026 * @qm: The queue structure associated with the queue to destroy.
11027 *
11028 * This function destroys a queue, as detailed in @mq by sending an mailbox
11029 * command, specific to the type of queue, to the HBA.
11030 *
11031 * The @mq struct is used to get the queue ID of the queue to destroy.
11032 *
11033 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011034 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040011035 **/
11036uint32_t
11037lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11038{
11039 LPFC_MBOXQ_t *mbox;
11040 int rc, length, status = 0;
11041 uint32_t shdr_status, shdr_add_status;
11042 union lpfc_sli4_cfg_shdr *shdr;
11043
11044 if (!mq)
11045 return -ENODEV;
11046 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11047 if (!mbox)
11048 return -ENOMEM;
11049 length = (sizeof(struct lpfc_mbx_mq_destroy) -
11050 sizeof(struct lpfc_sli4_cfg_mhdr));
11051 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11052 LPFC_MBOX_OPCODE_MQ_DESTROY,
11053 length, LPFC_SLI4_MBX_EMBED);
11054 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11055 mq->queue_id);
11056 mbox->vport = mq->phba->pport;
11057 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11058 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11059 /* The IOCTL status is embedded in the mailbox subheader. */
11060 shdr = (union lpfc_sli4_cfg_shdr *)
11061 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11062 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11063 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11064 if (shdr_status || shdr_add_status || rc) {
11065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11066 "2507 MQ_DESTROY mailbox failed with "
11067 "status x%x add_status x%x, mbx status x%x\n",
11068 shdr_status, shdr_add_status, rc);
11069 status = -ENXIO;
11070 }
11071 /* Remove mq from any list */
11072 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040011073 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011074 return status;
11075}
11076
11077/**
James Smart4f774512009-05-22 14:52:35 -040011078 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11079 * @wq: The queue structure associated with the queue to destroy.
11080 *
11081 * This function destroys a queue, as detailed in @wq by sending an mailbox
11082 * command, specific to the type of queue, to the HBA.
11083 *
11084 * The @wq struct is used to get the queue ID of the queue to destroy.
11085 *
11086 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011087 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011088 **/
11089uint32_t
11090lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11091{
11092 LPFC_MBOXQ_t *mbox;
11093 int rc, length, status = 0;
11094 uint32_t shdr_status, shdr_add_status;
11095 union lpfc_sli4_cfg_shdr *shdr;
11096
11097 if (!wq)
11098 return -ENODEV;
11099 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11100 if (!mbox)
11101 return -ENOMEM;
11102 length = (sizeof(struct lpfc_mbx_wq_destroy) -
11103 sizeof(struct lpfc_sli4_cfg_mhdr));
11104 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11105 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11106 length, LPFC_SLI4_MBX_EMBED);
11107 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11108 wq->queue_id);
11109 mbox->vport = wq->phba->pport;
11110 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11111 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11112 shdr = (union lpfc_sli4_cfg_shdr *)
11113 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11114 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11115 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11116 if (shdr_status || shdr_add_status || rc) {
11117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11118 "2508 WQ_DESTROY mailbox failed with "
11119 "status x%x add_status x%x, mbx status x%x\n",
11120 shdr_status, shdr_add_status, rc);
11121 status = -ENXIO;
11122 }
11123 /* Remove wq from any list */
11124 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040011125 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011126 return status;
11127}
11128
11129/**
11130 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11131 * @rq: The queue structure associated with the queue to destroy.
11132 *
11133 * This function destroys a queue, as detailed in @rq by sending an mailbox
11134 * command, specific to the type of queue, to the HBA.
11135 *
11136 * The @rq struct is used to get the queue ID of the queue to destroy.
11137 *
11138 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011139 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011140 **/
11141uint32_t
11142lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11143 struct lpfc_queue *drq)
11144{
11145 LPFC_MBOXQ_t *mbox;
11146 int rc, length, status = 0;
11147 uint32_t shdr_status, shdr_add_status;
11148 union lpfc_sli4_cfg_shdr *shdr;
11149
11150 if (!hrq || !drq)
11151 return -ENODEV;
11152 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11153 if (!mbox)
11154 return -ENOMEM;
11155 length = (sizeof(struct lpfc_mbx_rq_destroy) -
11156 sizeof(struct mbox_header));
11157 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11158 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11159 length, LPFC_SLI4_MBX_EMBED);
11160 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11161 hrq->queue_id);
11162 mbox->vport = hrq->phba->pport;
11163 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11164 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11165 /* The IOCTL status is embedded in the mailbox subheader. */
11166 shdr = (union lpfc_sli4_cfg_shdr *)
11167 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11168 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11169 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11170 if (shdr_status || shdr_add_status || rc) {
11171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11172 "2509 RQ_DESTROY mailbox failed with "
11173 "status x%x add_status x%x, mbx status x%x\n",
11174 shdr_status, shdr_add_status, rc);
11175 if (rc != MBX_TIMEOUT)
11176 mempool_free(mbox, hrq->phba->mbox_mem_pool);
11177 return -ENXIO;
11178 }
11179 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11180 drq->queue_id);
11181 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11182 shdr = (union lpfc_sli4_cfg_shdr *)
11183 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11184 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11185 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11186 if (shdr_status || shdr_add_status || rc) {
11187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11188 "2510 RQ_DESTROY mailbox failed with "
11189 "status x%x add_status x%x, mbx status x%x\n",
11190 shdr_status, shdr_add_status, rc);
11191 status = -ENXIO;
11192 }
11193 list_del_init(&hrq->list);
11194 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040011195 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011196 return status;
11197}
11198
11199/**
11200 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11201 * @phba: The virtual port for which this call being executed.
11202 * @pdma_phys_addr0: Physical address of the 1st SGL page.
11203 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11204 * @xritag: the xritag that ties this io to the SGL pages.
11205 *
11206 * This routine will post the sgl pages for the IO that has the xritag
11207 * that is in the iocbq structure. The xritag is assigned during iocbq
11208 * creation and persists for as long as the driver is loaded.
11209 * if the caller has fewer than 256 scatter gather segments to map then
11210 * pdma_phys_addr1 should be 0.
11211 * If the caller needs to map more than 256 scatter gather segment then
11212 * pdma_phys_addr1 should be a valid physical address.
11213 * physical address for SGLs must be 64 byte aligned.
11214 * If you are going to map 2 SGL's then the first one must have 256 entries
11215 * the second sgl can have between 1 and 256 entries.
11216 *
11217 * Return codes:
11218 * 0 - Success
11219 * -ENXIO, -ENOMEM - Failure
11220 **/
11221int
11222lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11223 dma_addr_t pdma_phys_addr0,
11224 dma_addr_t pdma_phys_addr1,
11225 uint16_t xritag)
11226{
11227 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11228 LPFC_MBOXQ_t *mbox;
11229 int rc;
11230 uint32_t shdr_status, shdr_add_status;
11231 union lpfc_sli4_cfg_shdr *shdr;
11232
11233 if (xritag == NO_XRI) {
11234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11235 "0364 Invalid param:\n");
11236 return -EINVAL;
11237 }
11238
11239 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11240 if (!mbox)
11241 return -ENOMEM;
11242
11243 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11244 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11245 sizeof(struct lpfc_mbx_post_sgl_pages) -
11246 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11247
11248 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11249 &mbox->u.mqe.un.post_sgl_pages;
11250 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11251 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11252
11253 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11254 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11255 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11256 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11257
11258 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11259 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11260 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11261 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11262 if (!phba->sli4_hba.intr_enable)
11263 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11264 else
11265 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11266 /* The IOCTL status is embedded in the mailbox subheader. */
11267 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11268 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11269 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11270 if (rc != MBX_TIMEOUT)
11271 mempool_free(mbox, phba->mbox_mem_pool);
11272 if (shdr_status || shdr_add_status || rc) {
11273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11274 "2511 POST_SGL mailbox failed with "
11275 "status x%x add_status x%x, mbx status x%x\n",
11276 shdr_status, shdr_add_status, rc);
11277 rc = -ENXIO;
11278 }
11279 return 0;
11280}
James Smart4f774512009-05-22 14:52:35 -040011281
11282/**
11283 * lpfc_sli4_next_xritag - Get an xritag for the io
11284 * @phba: Pointer to HBA context object.
11285 *
11286 * This function gets an xritag for the iocb. If there is no unused xritag
11287 * it will return 0xffff.
11288 * The function returns the allocated xritag if successful, else returns zero.
11289 * Zero is not a valid xritag.
11290 * The caller is not required to hold any lock.
11291 **/
11292uint16_t
11293lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11294{
11295 uint16_t xritag;
11296
11297 spin_lock_irq(&phba->hbalock);
11298 xritag = phba->sli4_hba.next_xri;
11299 if ((xritag != (uint16_t) -1) && xritag <
11300 (phba->sli4_hba.max_cfg_param.max_xri
11301 + phba->sli4_hba.max_cfg_param.xri_base)) {
11302 phba->sli4_hba.next_xri++;
11303 phba->sli4_hba.max_cfg_param.xri_used++;
11304 spin_unlock_irq(&phba->hbalock);
11305 return xritag;
11306 }
11307 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040011308 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040011309 "2004 Failed to allocate XRI.last XRITAG is %d"
11310 " Max XRI is %d, Used XRI is %d\n",
11311 phba->sli4_hba.next_xri,
11312 phba->sli4_hba.max_cfg_param.max_xri,
11313 phba->sli4_hba.max_cfg_param.xri_used);
11314 return -1;
11315}
11316
11317/**
11318 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11319 * @phba: pointer to lpfc hba data structure.
11320 *
11321 * This routine is invoked to post a block of driver's sgl pages to the
11322 * HBA using non-embedded mailbox command. No Lock is held. This routine
11323 * is only called when the driver is loading and after all IO has been
11324 * stopped.
11325 **/
11326int
11327lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11328{
11329 struct lpfc_sglq *sglq_entry;
11330 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11331 struct sgl_page_pairs *sgl_pg_pairs;
11332 void *viraddr;
11333 LPFC_MBOXQ_t *mbox;
11334 uint32_t reqlen, alloclen, pg_pairs;
11335 uint32_t mbox_tmo;
11336 uint16_t xritag_start = 0;
11337 int els_xri_cnt, rc = 0;
11338 uint32_t shdr_status, shdr_add_status;
11339 union lpfc_sli4_cfg_shdr *shdr;
11340
11341 /* The number of sgls to be posted */
11342 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11343
11344 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11345 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011346 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011347 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11348 "2559 Block sgl registration required DMA "
11349 "size (%d) great than a page\n", reqlen);
11350 return -ENOMEM;
11351 }
11352 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11353 if (!mbox) {
11354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11355 "2560 Failed to allocate mbox cmd memory\n");
11356 return -ENOMEM;
11357 }
11358
11359 /* Allocate DMA memory and set up the non-embedded mailbox command */
11360 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11361 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11362 LPFC_SLI4_MBX_NEMBED);
11363
11364 if (alloclen < reqlen) {
11365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11366 "0285 Allocated DMA memory size (%d) is "
11367 "less than the requested DMA memory "
11368 "size (%d)\n", alloclen, reqlen);
11369 lpfc_sli4_mbox_cmd_free(phba, mbox);
11370 return -ENOMEM;
11371 }
James Smart4f774512009-05-22 14:52:35 -040011372 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011373 viraddr = mbox->sge_array->addr[0];
11374
11375 /* Set up the SGL pages in the non-embedded DMA pages */
11376 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11377 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11378
11379 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11380 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11381 /* Set up the sge entry */
11382 sgl_pg_pairs->sgl_pg0_addr_lo =
11383 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11384 sgl_pg_pairs->sgl_pg0_addr_hi =
11385 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11386 sgl_pg_pairs->sgl_pg1_addr_lo =
11387 cpu_to_le32(putPaddrLow(0));
11388 sgl_pg_pairs->sgl_pg1_addr_hi =
11389 cpu_to_le32(putPaddrHigh(0));
11390 /* Keep the first xritag on the list */
11391 if (pg_pairs == 0)
11392 xritag_start = sglq_entry->sli4_xritag;
11393 sgl_pg_pairs++;
11394 }
11395 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040011396 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040011397 /* Perform endian conversion if necessary */
11398 sgl->word0 = cpu_to_le32(sgl->word0);
11399
11400 if (!phba->sli4_hba.intr_enable)
11401 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11402 else {
11403 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11404 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11405 }
11406 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11407 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11408 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11409 if (rc != MBX_TIMEOUT)
11410 lpfc_sli4_mbox_cmd_free(phba, mbox);
11411 if (shdr_status || shdr_add_status || rc) {
11412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11413 "2513 POST_SGL_BLOCK mailbox command failed "
11414 "status x%x add_status x%x mbx status x%x\n",
11415 shdr_status, shdr_add_status, rc);
11416 rc = -ENXIO;
11417 }
11418 return rc;
11419}
11420
11421/**
11422 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11423 * @phba: pointer to lpfc hba data structure.
11424 * @sblist: pointer to scsi buffer list.
11425 * @count: number of scsi buffers on the list.
11426 *
11427 * This routine is invoked to post a block of @count scsi sgl pages from a
11428 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11429 * No Lock is held.
11430 *
11431 **/
11432int
11433lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11434 int cnt)
11435{
11436 struct lpfc_scsi_buf *psb;
11437 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11438 struct sgl_page_pairs *sgl_pg_pairs;
11439 void *viraddr;
11440 LPFC_MBOXQ_t *mbox;
11441 uint32_t reqlen, alloclen, pg_pairs;
11442 uint32_t mbox_tmo;
11443 uint16_t xritag_start = 0;
11444 int rc = 0;
11445 uint32_t shdr_status, shdr_add_status;
11446 dma_addr_t pdma_phys_bpl1;
11447 union lpfc_sli4_cfg_shdr *shdr;
11448
11449 /* Calculate the requested length of the dma memory */
11450 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11451 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011452 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011453 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11454 "0217 Block sgl registration required DMA "
11455 "size (%d) great than a page\n", reqlen);
11456 return -ENOMEM;
11457 }
11458 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11459 if (!mbox) {
11460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11461 "0283 Failed to allocate mbox cmd memory\n");
11462 return -ENOMEM;
11463 }
11464
11465 /* Allocate DMA memory and set up the non-embedded mailbox command */
11466 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11467 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11468 LPFC_SLI4_MBX_NEMBED);
11469
11470 if (alloclen < reqlen) {
11471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11472 "2561 Allocated DMA memory size (%d) is "
11473 "less than the requested DMA memory "
11474 "size (%d)\n", alloclen, reqlen);
11475 lpfc_sli4_mbox_cmd_free(phba, mbox);
11476 return -ENOMEM;
11477 }
James Smart4f774512009-05-22 14:52:35 -040011478 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011479 viraddr = mbox->sge_array->addr[0];
11480
11481 /* Set up the SGL pages in the non-embedded DMA pages */
11482 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11483 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11484
11485 pg_pairs = 0;
11486 list_for_each_entry(psb, sblist, list) {
11487 /* Set up the sge entry */
11488 sgl_pg_pairs->sgl_pg0_addr_lo =
11489 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11490 sgl_pg_pairs->sgl_pg0_addr_hi =
11491 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11492 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11493 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11494 else
11495 pdma_phys_bpl1 = 0;
11496 sgl_pg_pairs->sgl_pg1_addr_lo =
11497 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11498 sgl_pg_pairs->sgl_pg1_addr_hi =
11499 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11500 /* Keep the first xritag on the list */
11501 if (pg_pairs == 0)
11502 xritag_start = psb->cur_iocbq.sli4_xritag;
11503 sgl_pg_pairs++;
11504 pg_pairs++;
11505 }
11506 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11507 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11508 /* Perform endian conversion if necessary */
11509 sgl->word0 = cpu_to_le32(sgl->word0);
11510
11511 if (!phba->sli4_hba.intr_enable)
11512 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11513 else {
11514 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11515 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11516 }
11517 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11518 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11519 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11520 if (rc != MBX_TIMEOUT)
11521 lpfc_sli4_mbox_cmd_free(phba, mbox);
11522 if (shdr_status || shdr_add_status || rc) {
11523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11524 "2564 POST_SGL_BLOCK mailbox command failed "
11525 "status x%x add_status x%x mbx status x%x\n",
11526 shdr_status, shdr_add_status, rc);
11527 rc = -ENXIO;
11528 }
11529 return rc;
11530}
11531
11532/**
11533 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11534 * @phba: pointer to lpfc_hba struct that the frame was received on
11535 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11536 *
11537 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11538 * valid type of frame that the LPFC driver will handle. This function will
11539 * return a zero if the frame is a valid frame or a non zero value when the
11540 * frame does not pass the check.
11541 **/
11542static int
11543lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11544{
11545 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11546 char *type_names[] = FC_TYPE_NAMES_INIT;
11547 struct fc_vft_header *fc_vft_hdr;
11548
11549 switch (fc_hdr->fh_r_ctl) {
11550 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11551 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11552 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11553 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11554 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11555 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11556 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11557 case FC_RCTL_DD_CMD_STATUS: /* command status */
11558 case FC_RCTL_ELS_REQ: /* extended link services request */
11559 case FC_RCTL_ELS_REP: /* extended link services reply */
11560 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11561 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11562 case FC_RCTL_BA_NOP: /* basic link service NOP */
11563 case FC_RCTL_BA_ABTS: /* basic link service abort */
11564 case FC_RCTL_BA_RMC: /* remove connection */
11565 case FC_RCTL_BA_ACC: /* basic accept */
11566 case FC_RCTL_BA_RJT: /* basic reject */
11567 case FC_RCTL_BA_PRMT:
11568 case FC_RCTL_ACK_1: /* acknowledge_1 */
11569 case FC_RCTL_ACK_0: /* acknowledge_0 */
11570 case FC_RCTL_P_RJT: /* port reject */
11571 case FC_RCTL_F_RJT: /* fabric reject */
11572 case FC_RCTL_P_BSY: /* port busy */
11573 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11574 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11575 case FC_RCTL_LCR: /* link credit reset */
11576 case FC_RCTL_END: /* end */
11577 break;
11578 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11579 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11580 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11581 return lpfc_fc_frame_check(phba, fc_hdr);
11582 default:
11583 goto drop;
11584 }
11585 switch (fc_hdr->fh_type) {
11586 case FC_TYPE_BLS:
11587 case FC_TYPE_ELS:
11588 case FC_TYPE_FCP:
11589 case FC_TYPE_CT:
11590 break;
11591 case FC_TYPE_IP:
11592 case FC_TYPE_ILS:
11593 default:
11594 goto drop;
11595 }
11596 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11597 "2538 Received frame rctl:%s type:%s\n",
11598 rctl_names[fc_hdr->fh_r_ctl],
11599 type_names[fc_hdr->fh_type]);
11600 return 0;
11601drop:
11602 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11603 "2539 Dropped frame rctl:%s type:%s\n",
11604 rctl_names[fc_hdr->fh_r_ctl],
11605 type_names[fc_hdr->fh_type]);
11606 return 1;
11607}
11608
11609/**
11610 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11611 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11612 *
11613 * This function processes the FC header to retrieve the VFI from the VF
11614 * header, if one exists. This function will return the VFI if one exists
11615 * or 0 if no VSAN Header exists.
11616 **/
11617static uint32_t
11618lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11619{
11620 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11621
11622 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11623 return 0;
11624 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11625}
11626
11627/**
11628 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11629 * @phba: Pointer to the HBA structure to search for the vport on
11630 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11631 * @fcfi: The FC Fabric ID that the frame came from
11632 *
11633 * This function searches the @phba for a vport that matches the content of the
11634 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11635 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11636 * returns the matching vport pointer or NULL if unable to match frame to a
11637 * vport.
11638 **/
11639static struct lpfc_vport *
11640lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11641 uint16_t fcfi)
11642{
11643 struct lpfc_vport **vports;
11644 struct lpfc_vport *vport = NULL;
11645 int i;
11646 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11647 fc_hdr->fh_d_id[1] << 8 |
11648 fc_hdr->fh_d_id[2]);
11649
11650 vports = lpfc_create_vport_work_array(phba);
11651 if (vports != NULL)
11652 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11653 if (phba->fcf.fcfi == fcfi &&
11654 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11655 vports[i]->fc_myDID == did) {
11656 vport = vports[i];
11657 break;
11658 }
11659 }
11660 lpfc_destroy_vport_work_array(phba, vports);
11661 return vport;
11662}
11663
11664/**
James Smart45ed1192009-10-02 15:17:02 -040011665 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11666 * @vport: The vport to work on.
11667 *
11668 * This function updates the receive sequence time stamp for this vport. The
11669 * receive sequence time stamp indicates the time that the last frame of the
11670 * the sequence that has been idle for the longest amount of time was received.
11671 * the driver uses this time stamp to indicate if any received sequences have
11672 * timed out.
11673 **/
11674void
11675lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11676{
11677 struct lpfc_dmabuf *h_buf;
11678 struct hbq_dmabuf *dmabuf = NULL;
11679
11680 /* get the oldest sequence on the rcv list */
11681 h_buf = list_get_first(&vport->rcv_buffer_list,
11682 struct lpfc_dmabuf, list);
11683 if (!h_buf)
11684 return;
11685 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11686 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11687}
11688
11689/**
11690 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11691 * @vport: The vport that the received sequences were sent to.
11692 *
11693 * This function cleans up all outstanding received sequences. This is called
11694 * by the driver when a link event or user action invalidates all the received
11695 * sequences.
11696 **/
11697void
11698lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11699{
11700 struct lpfc_dmabuf *h_buf, *hnext;
11701 struct lpfc_dmabuf *d_buf, *dnext;
11702 struct hbq_dmabuf *dmabuf = NULL;
11703
11704 /* start with the oldest sequence on the rcv list */
11705 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11706 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11707 list_del_init(&dmabuf->hbuf.list);
11708 list_for_each_entry_safe(d_buf, dnext,
11709 &dmabuf->dbuf.list, list) {
11710 list_del_init(&d_buf->list);
11711 lpfc_in_buf_free(vport->phba, d_buf);
11712 }
11713 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11714 }
11715}
11716
11717/**
11718 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11719 * @vport: The vport that the received sequences were sent to.
11720 *
11721 * This function determines whether any received sequences have timed out by
11722 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11723 * indicates that there is at least one timed out sequence this routine will
11724 * go through the received sequences one at a time from most inactive to most
11725 * active to determine which ones need to be cleaned up. Once it has determined
11726 * that a sequence needs to be cleaned up it will simply free up the resources
11727 * without sending an abort.
11728 **/
11729void
11730lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11731{
11732 struct lpfc_dmabuf *h_buf, *hnext;
11733 struct lpfc_dmabuf *d_buf, *dnext;
11734 struct hbq_dmabuf *dmabuf = NULL;
11735 unsigned long timeout;
11736 int abort_count = 0;
11737
11738 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11739 vport->rcv_buffer_time_stamp);
11740 if (list_empty(&vport->rcv_buffer_list) ||
11741 time_before(jiffies, timeout))
11742 return;
11743 /* start with the oldest sequence on the rcv list */
11744 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11745 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11746 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11747 dmabuf->time_stamp);
11748 if (time_before(jiffies, timeout))
11749 break;
11750 abort_count++;
11751 list_del_init(&dmabuf->hbuf.list);
11752 list_for_each_entry_safe(d_buf, dnext,
11753 &dmabuf->dbuf.list, list) {
11754 list_del_init(&d_buf->list);
11755 lpfc_in_buf_free(vport->phba, d_buf);
11756 }
11757 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11758 }
11759 if (abort_count)
11760 lpfc_update_rcv_time_stamp(vport);
11761}
11762
11763/**
James Smart4f774512009-05-22 14:52:35 -040011764 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11765 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11766 *
11767 * This function searches through the existing incomplete sequences that have
11768 * been sent to this @vport. If the frame matches one of the incomplete
11769 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11770 * make up that sequence. If no sequence is found that matches this frame then
11771 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11772 * This function returns a pointer to the first dmabuf in the sequence list that
11773 * the frame was linked to.
11774 **/
11775static struct hbq_dmabuf *
11776lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11777{
11778 struct fc_frame_header *new_hdr;
11779 struct fc_frame_header *temp_hdr;
11780 struct lpfc_dmabuf *d_buf;
11781 struct lpfc_dmabuf *h_buf;
11782 struct hbq_dmabuf *seq_dmabuf = NULL;
11783 struct hbq_dmabuf *temp_dmabuf = NULL;
11784
James Smart4d9ab992009-10-02 15:16:39 -040011785 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011786 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011787 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11788 /* Use the hdr_buf to find the sequence that this frame belongs to */
11789 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11790 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11791 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11792 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11793 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11794 continue;
11795 /* found a pending sequence that matches this frame */
11796 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11797 break;
11798 }
11799 if (!seq_dmabuf) {
11800 /*
11801 * This indicates first frame received for this sequence.
11802 * Queue the buffer on the vport's rcv_buffer_list.
11803 */
11804 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011805 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011806 return dmabuf;
11807 }
11808 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011809 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11810 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011811 list_del_init(&seq_dmabuf->hbuf.list);
11812 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11813 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011814 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011815 return dmabuf;
11816 }
James Smart45ed1192009-10-02 15:17:02 -040011817 /* move this sequence to the tail to indicate a young sequence */
11818 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11819 seq_dmabuf->time_stamp = jiffies;
11820 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011821 if (list_empty(&seq_dmabuf->dbuf.list)) {
11822 temp_hdr = dmabuf->hbuf.virt;
11823 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11824 return seq_dmabuf;
11825 }
James Smart4f774512009-05-22 14:52:35 -040011826 /* find the correct place in the sequence to insert this frame */
11827 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11828 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11829 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11830 /*
11831 * If the frame's sequence count is greater than the frame on
11832 * the list then insert the frame right after this frame
11833 */
James Smarteeead812009-12-21 17:01:23 -050011834 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11835 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011836 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11837 return seq_dmabuf;
11838 }
11839 }
11840 return NULL;
11841}
11842
11843/**
James Smart6669f9b2009-10-02 15:16:45 -040011844 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11845 * @vport: pointer to a vitural port
11846 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11847 *
11848 * This function tries to abort from the partially assembed sequence, described
11849 * by the information from basic abbort @dmabuf. It checks to see whether such
11850 * partially assembled sequence held by the driver. If so, it shall free up all
11851 * the frames from the partially assembled sequence.
11852 *
11853 * Return
11854 * true -- if there is matching partially assembled sequence present and all
11855 * the frames freed with the sequence;
11856 * false -- if there is no matching partially assembled sequence present so
11857 * nothing got aborted in the lower layer driver
11858 **/
11859static bool
11860lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11861 struct hbq_dmabuf *dmabuf)
11862{
11863 struct fc_frame_header *new_hdr;
11864 struct fc_frame_header *temp_hdr;
11865 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11866 struct hbq_dmabuf *seq_dmabuf = NULL;
11867
11868 /* Use the hdr_buf to find the sequence that matches this frame */
11869 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11870 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11871 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11872 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11873 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11874 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11875 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11876 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11877 continue;
11878 /* found a pending sequence that matches this frame */
11879 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11880 break;
11881 }
11882
11883 /* Free up all the frames from the partially assembled sequence */
11884 if (seq_dmabuf) {
11885 list_for_each_entry_safe(d_buf, n_buf,
11886 &seq_dmabuf->dbuf.list, list) {
11887 list_del_init(&d_buf->list);
11888 lpfc_in_buf_free(vport->phba, d_buf);
11889 }
11890 return true;
11891 }
11892 return false;
11893}
11894
11895/**
11896 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11897 * @phba: Pointer to HBA context object.
11898 * @cmd_iocbq: pointer to the command iocbq structure.
11899 * @rsp_iocbq: pointer to the response iocbq structure.
11900 *
11901 * This function handles the sequence abort accept iocb command complete
11902 * event. It properly releases the memory allocated to the sequence abort
11903 * accept iocb.
11904 **/
11905static void
11906lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11907 struct lpfc_iocbq *cmd_iocbq,
11908 struct lpfc_iocbq *rsp_iocbq)
11909{
11910 if (cmd_iocbq)
11911 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11912}
11913
11914/**
11915 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11916 * @phba: Pointer to HBA context object.
11917 * @fc_hdr: pointer to a FC frame header.
11918 *
11919 * This function sends a basic accept to a previous unsol sequence abort
11920 * event after aborting the sequence handling.
11921 **/
11922static void
11923lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11924 struct fc_frame_header *fc_hdr)
11925{
11926 struct lpfc_iocbq *ctiocb = NULL;
11927 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011928 uint16_t oxid, rxid;
11929 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011930 IOCB_t *icmd;
11931
11932 if (!lpfc_is_link_up(phba))
11933 return;
11934
11935 sid = sli4_sid_from_fc_hdr(fc_hdr);
11936 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011937 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011938
11939 ndlp = lpfc_findnode_did(phba->pport, sid);
11940 if (!ndlp) {
11941 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11942 "1268 Find ndlp returned NULL for oxid:x%x "
11943 "SID:x%x\n", oxid, sid);
11944 return;
11945 }
James Smart19ca7602010-11-20 23:11:55 -050011946 if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
11947 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
11948 + phba->sli4_hba.max_cfg_param.xri_base))
11949 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040011950
11951 /* Allocate buffer for acc iocb */
11952 ctiocb = lpfc_sli_get_iocbq(phba);
11953 if (!ctiocb)
11954 return;
11955
James Smart5ffc2662009-11-18 15:39:44 -050011956 /* Extract the F_CTL field from FC_HDR */
11957 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11958
James Smart6669f9b2009-10-02 15:16:45 -040011959 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011960 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011961 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011962 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11963 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11964 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11965
11966 /* Fill in the rest of iocb fields */
11967 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11968 icmd->ulpBdeCount = 0;
11969 icmd->ulpLe = 1;
11970 icmd->ulpClass = CLASS3;
11971 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011972
James Smart6669f9b2009-10-02 15:16:45 -040011973 ctiocb->iocb_cmpl = NULL;
11974 ctiocb->vport = phba->pport;
11975 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11976
James Smart5ffc2662009-11-18 15:39:44 -050011977 if (fctl & FC_FC_EX_CTX) {
11978 /* ABTS sent by responder to CT exchange, construction
11979 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11980 * field and RX_ID from ABTS for RX_ID field.
11981 */
11982 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11983 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11984 ctiocb->sli4_xritag = oxid;
11985 } else {
11986 /* ABTS sent by initiator to CT exchange, construction
11987 * of BA_ACC will need to allocate a new XRI as for the
11988 * XRI_TAG and RX_ID fields.
11989 */
11990 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11991 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11992 ctiocb->sli4_xritag = NO_XRI;
11993 }
11994 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11995
James Smart6669f9b2009-10-02 15:16:45 -040011996 /* Xmit CT abts accept on exchange <xid> */
11997 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11998 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11999 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
12000 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12001}
12002
12003/**
12004 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12005 * @vport: Pointer to the vport on which this sequence was received
12006 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12007 *
12008 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12009 * receive sequence is only partially assembed by the driver, it shall abort
12010 * the partially assembled frames for the sequence. Otherwise, if the
12011 * unsolicited receive sequence has been completely assembled and passed to
12012 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12013 * unsolicited sequence has been aborted. After that, it will issue a basic
12014 * accept to accept the abort.
12015 **/
12016void
12017lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12018 struct hbq_dmabuf *dmabuf)
12019{
12020 struct lpfc_hba *phba = vport->phba;
12021 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050012022 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040012023 bool abts_par;
12024
James Smart6669f9b2009-10-02 15:16:45 -040012025 /* Make a copy of fc_hdr before the dmabuf being released */
12026 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050012027 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040012028
James Smart5ffc2662009-11-18 15:39:44 -050012029 if (fctl & FC_FC_EX_CTX) {
12030 /*
12031 * ABTS sent by responder to exchange, just free the buffer
12032 */
James Smart6669f9b2009-10-02 15:16:45 -040012033 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050012034 } else {
12035 /*
12036 * ABTS sent by initiator to exchange, need to do cleanup
12037 */
12038 /* Try to abort partially assembled seq */
12039 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12040
12041 /* Send abort to ULP if partially seq abort failed */
12042 if (abts_par == false)
12043 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12044 else
12045 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12046 }
James Smart6669f9b2009-10-02 15:16:45 -040012047 /* Send basic accept (BA_ACC) to the abort requester */
12048 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
12049}
12050
12051/**
James Smart4f774512009-05-22 14:52:35 -040012052 * lpfc_seq_complete - Indicates if a sequence is complete
12053 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12054 *
12055 * This function checks the sequence, starting with the frame described by
12056 * @dmabuf, to see if all the frames associated with this sequence are present.
12057 * the frames associated with this sequence are linked to the @dmabuf using the
12058 * dbuf list. This function looks for two major things. 1) That the first frame
12059 * has a sequence count of zero. 2) There is a frame with last frame of sequence
12060 * set. 3) That there are no holes in the sequence count. The function will
12061 * return 1 when the sequence is complete, otherwise it will return 0.
12062 **/
12063static int
12064lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12065{
12066 struct fc_frame_header *hdr;
12067 struct lpfc_dmabuf *d_buf;
12068 struct hbq_dmabuf *seq_dmabuf;
12069 uint32_t fctl;
12070 int seq_count = 0;
12071
12072 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12073 /* make sure first fame of sequence has a sequence count of zero */
12074 if (hdr->fh_seq_cnt != seq_count)
12075 return 0;
12076 fctl = (hdr->fh_f_ctl[0] << 16 |
12077 hdr->fh_f_ctl[1] << 8 |
12078 hdr->fh_f_ctl[2]);
12079 /* If last frame of sequence we can return success. */
12080 if (fctl & FC_FC_END_SEQ)
12081 return 1;
12082 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12083 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12084 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12085 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050012086 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040012087 return 0;
12088 fctl = (hdr->fh_f_ctl[0] << 16 |
12089 hdr->fh_f_ctl[1] << 8 |
12090 hdr->fh_f_ctl[2]);
12091 /* If last frame of sequence we can return success. */
12092 if (fctl & FC_FC_END_SEQ)
12093 return 1;
12094 }
12095 return 0;
12096}
12097
12098/**
12099 * lpfc_prep_seq - Prep sequence for ULP processing
12100 * @vport: Pointer to the vport on which this sequence was received
12101 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12102 *
12103 * This function takes a sequence, described by a list of frames, and creates
12104 * a list of iocbq structures to describe the sequence. This iocbq list will be
12105 * used to issue to the generic unsolicited sequence handler. This routine
12106 * returns a pointer to the first iocbq in the list. If the function is unable
12107 * to allocate an iocbq then it throw out the received frames that were not
12108 * able to be described and return a pointer to the first iocbq. If unable to
12109 * allocate any iocbqs (including the first) this function will return NULL.
12110 **/
12111static struct lpfc_iocbq *
12112lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12113{
12114 struct lpfc_dmabuf *d_buf, *n_buf;
12115 struct lpfc_iocbq *first_iocbq, *iocbq;
12116 struct fc_frame_header *fc_hdr;
12117 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050012118 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040012119
12120 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12121 /* remove from receive buffer list */
12122 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040012123 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012124 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040012125 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040012126 /* Get an iocbq struct to fill in. */
12127 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12128 if (first_iocbq) {
12129 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040012130 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040012131 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12132 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12133 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12134 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12135 vport->vpi + vport->phba->vpi_base;
12136 /* put the first buffer into the first IOCBq */
12137 first_iocbq->context2 = &seq_dmabuf->dbuf;
12138 first_iocbq->context3 = NULL;
12139 first_iocbq->iocb.ulpBdeCount = 1;
12140 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12141 LPFC_DATA_BUF_SIZE;
12142 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040012143 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012144 bf_get(lpfc_rcqe_length,
12145 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012146 }
12147 iocbq = first_iocbq;
12148 /*
12149 * Each IOCBq can have two Buffers assigned, so go through the list
12150 * of buffers for this sequence and save two buffers in each IOCBq
12151 */
12152 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12153 if (!iocbq) {
12154 lpfc_in_buf_free(vport->phba, d_buf);
12155 continue;
12156 }
12157 if (!iocbq->context3) {
12158 iocbq->context3 = d_buf;
12159 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050012160 pbde = (struct ulp_bde64 *)
12161 &iocbq->iocb.unsli3.sli3Words[4];
12162 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012163 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012164 bf_get(lpfc_rcqe_length,
12165 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012166 } else {
12167 iocbq = lpfc_sli_get_iocbq(vport->phba);
12168 if (!iocbq) {
12169 if (first_iocbq) {
12170 first_iocbq->iocb.ulpStatus =
12171 IOSTAT_FCP_RSP_ERROR;
12172 first_iocbq->iocb.un.ulpWord[4] =
12173 IOERR_NO_RESOURCES;
12174 }
12175 lpfc_in_buf_free(vport->phba, d_buf);
12176 continue;
12177 }
12178 iocbq->context2 = d_buf;
12179 iocbq->context3 = NULL;
12180 iocbq->iocb.ulpBdeCount = 1;
12181 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12182 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012183 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012184 bf_get(lpfc_rcqe_length,
12185 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012186 iocbq->iocb.un.rcvels.remoteID = sid;
12187 list_add_tail(&iocbq->list, &first_iocbq->list);
12188 }
12189 }
12190 return first_iocbq;
12191}
12192
James Smart6669f9b2009-10-02 15:16:45 -040012193static void
12194lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12195 struct hbq_dmabuf *seq_dmabuf)
12196{
12197 struct fc_frame_header *fc_hdr;
12198 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12199 struct lpfc_hba *phba = vport->phba;
12200
12201 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12202 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12203 if (!iocbq) {
12204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12205 "2707 Ring %d handler: Failed to allocate "
12206 "iocb Rctl x%x Type x%x received\n",
12207 LPFC_ELS_RING,
12208 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12209 return;
12210 }
12211 if (!lpfc_complete_unsol_iocb(phba,
12212 &phba->sli.ring[LPFC_ELS_RING],
12213 iocbq, fc_hdr->fh_r_ctl,
12214 fc_hdr->fh_type))
12215 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12216 "2540 Ring %d handler: unexpected Rctl "
12217 "x%x Type x%x received\n",
12218 LPFC_ELS_RING,
12219 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12220
12221 /* Free iocb created in lpfc_prep_seq */
12222 list_for_each_entry_safe(curr_iocb, next_iocb,
12223 &iocbq->list, list) {
12224 list_del_init(&curr_iocb->list);
12225 lpfc_sli_release_iocbq(phba, curr_iocb);
12226 }
12227 lpfc_sli_release_iocbq(phba, iocbq);
12228}
12229
James Smart4f774512009-05-22 14:52:35 -040012230/**
12231 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12232 * @phba: Pointer to HBA context object.
12233 *
12234 * This function is called with no lock held. This function processes all
12235 * the received buffers and gives it to upper layers when a received buffer
12236 * indicates that it is the final frame in the sequence. The interrupt
12237 * service routine processes received buffers at interrupt contexts and adds
12238 * received dma buffers to the rb_pend_list queue and signals the worker thread.
12239 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12240 * appropriate receive function when the final frame in a sequence is received.
12241 **/
James Smart4d9ab992009-10-02 15:16:39 -040012242void
12243lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12244 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040012245{
James Smart4d9ab992009-10-02 15:16:39 -040012246 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040012247 struct fc_frame_header *fc_hdr;
12248 struct lpfc_vport *vport;
12249 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040012250
James Smart4f774512009-05-22 14:52:35 -040012251 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040012252 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12253 /* check to see if this a valid type of frame */
12254 if (lpfc_fc_frame_check(phba, fc_hdr)) {
12255 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12256 return;
12257 }
12258 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12259 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050012260 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040012261 /* throw out the frame */
12262 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12263 return;
12264 }
James Smart6669f9b2009-10-02 15:16:45 -040012265 /* Handle the basic abort sequence (BA_ABTS) event */
12266 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12267 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12268 return;
12269 }
12270
James Smart4d9ab992009-10-02 15:16:39 -040012271 /* Link this frame */
12272 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12273 if (!seq_dmabuf) {
12274 /* unable to add frame to vport - throw it out */
12275 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12276 return;
12277 }
12278 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050012279 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040012280 return;
James Smartdef9c7a2009-12-21 17:02:28 -050012281
James Smart6669f9b2009-10-02 15:16:45 -040012282 /* Send the complete sequence to the upper layer protocol */
12283 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040012284}
James Smart6fb120a2009-05-22 14:52:59 -040012285
12286/**
12287 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12288 * @phba: pointer to lpfc hba data structure.
12289 *
12290 * This routine is invoked to post rpi header templates to the
12291 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012292 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12293 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012294 *
12295 * This routine does not require any locks. It's usage is expected
12296 * to be driver load or reset recovery when the driver is
12297 * sequential.
12298 *
12299 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012300 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012301 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012302 * When this error occurs, the driver is not guaranteed
12303 * to have any rpi regions posted to the device and
12304 * must either attempt to repost the regions or take a
12305 * fatal error.
12306 **/
12307int
12308lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12309{
12310 struct lpfc_rpi_hdr *rpi_page;
12311 uint32_t rc = 0;
12312
12313 /* Post all rpi memory regions to the port. */
12314 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12315 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12316 if (rc != MBX_SUCCESS) {
12317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12318 "2008 Error %d posting all rpi "
12319 "headers\n", rc);
12320 rc = -EIO;
12321 break;
12322 }
12323 }
12324
12325 return rc;
12326}
12327
12328/**
12329 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12330 * @phba: pointer to lpfc hba data structure.
12331 * @rpi_page: pointer to the rpi memory region.
12332 *
12333 * This routine is invoked to post a single rpi header to the
12334 * HBA consistent with the SLI-4 interface spec. This memory region
12335 * maps up to 64 rpi context regions.
12336 *
12337 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012338 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012339 * -ENOMEM - No available memory
12340 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012341 **/
12342int
12343lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12344{
12345 LPFC_MBOXQ_t *mboxq;
12346 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12347 uint32_t rc = 0;
12348 uint32_t mbox_tmo;
12349 uint32_t shdr_status, shdr_add_status;
12350 union lpfc_sli4_cfg_shdr *shdr;
12351
12352 /* The port is notified of the header region via a mailbox command. */
12353 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12354 if (!mboxq) {
12355 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12356 "2001 Unable to allocate memory for issuing "
12357 "SLI_CONFIG_SPECIAL mailbox command\n");
12358 return -ENOMEM;
12359 }
12360
12361 /* Post all rpi memory regions to the port. */
12362 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12363 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12364 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12365 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12366 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
12367 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
12368 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12369 hdr_tmpl, rpi_page->page_count);
12370 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12371 rpi_page->start_rpi);
12372 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12373 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040012374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040012375 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12376 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12377 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12378 if (rc != MBX_TIMEOUT)
12379 mempool_free(mboxq, phba->mbox_mem_pool);
12380 if (shdr_status || shdr_add_status || rc) {
12381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12382 "2514 POST_RPI_HDR mailbox failed with "
12383 "status x%x add_status x%x, mbx status x%x\n",
12384 shdr_status, shdr_add_status, rc);
12385 rc = -ENXIO;
12386 }
12387 return rc;
12388}
12389
12390/**
12391 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12392 * @phba: pointer to lpfc hba data structure.
12393 *
12394 * This routine is invoked to post rpi header templates to the
12395 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012396 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12397 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012398 *
12399 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012400 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040012401 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12402 **/
12403int
12404lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12405{
12406 int rpi;
12407 uint16_t max_rpi, rpi_base, rpi_limit;
12408 uint16_t rpi_remaining;
12409 struct lpfc_rpi_hdr *rpi_hdr;
12410
12411 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12412 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12413 rpi_limit = phba->sli4_hba.next_rpi;
12414
12415 /*
12416 * The valid rpi range is not guaranteed to be zero-based. Start
12417 * the search at the rpi_base as reported by the port.
12418 */
12419 spin_lock_irq(&phba->hbalock);
12420 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12421 if (rpi >= rpi_limit || rpi < rpi_base)
12422 rpi = LPFC_RPI_ALLOC_ERROR;
12423 else {
12424 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12425 phba->sli4_hba.max_cfg_param.rpi_used++;
12426 phba->sli4_hba.rpi_count++;
12427 }
12428
12429 /*
12430 * Don't try to allocate more rpi header regions if the device limit
12431 * on available rpis max has been exhausted.
12432 */
12433 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12434 (phba->sli4_hba.rpi_count >= max_rpi)) {
12435 spin_unlock_irq(&phba->hbalock);
12436 return rpi;
12437 }
12438
12439 /*
12440 * If the driver is running low on rpi resources, allocate another
12441 * page now. Note that the next_rpi value is used because
12442 * it represents how many are actually in use whereas max_rpi notes
12443 * how many are supported max by the device.
12444 */
12445 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12446 phba->sli4_hba.rpi_count;
12447 spin_unlock_irq(&phba->hbalock);
12448 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12449 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12450 if (!rpi_hdr) {
12451 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12452 "2002 Error Could not grow rpi "
12453 "count\n");
12454 } else {
12455 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12456 }
12457 }
12458
12459 return rpi;
12460}
12461
12462/**
12463 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12464 * @phba: pointer to lpfc hba data structure.
12465 *
12466 * This routine is invoked to release an rpi to the pool of
12467 * available rpis maintained by the driver.
12468 **/
12469void
James Smartd7c47992010-06-08 18:31:54 -040012470__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12471{
12472 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12473 phba->sli4_hba.rpi_count--;
12474 phba->sli4_hba.max_cfg_param.rpi_used--;
12475 }
12476}
12477
12478/**
12479 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12480 * @phba: pointer to lpfc hba data structure.
12481 *
12482 * This routine is invoked to release an rpi to the pool of
12483 * available rpis maintained by the driver.
12484 **/
12485void
James Smart6fb120a2009-05-22 14:52:59 -040012486lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12487{
12488 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012489 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012490 spin_unlock_irq(&phba->hbalock);
12491}
12492
12493/**
12494 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12495 * @phba: pointer to lpfc hba data structure.
12496 *
12497 * This routine is invoked to remove the memory region that
12498 * provided rpi via a bitmask.
12499 **/
12500void
12501lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12502{
12503 kfree(phba->sli4_hba.rpi_bmask);
12504}
12505
12506/**
12507 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12508 * @phba: pointer to lpfc hba data structure.
12509 *
12510 * This routine is invoked to remove the memory region that
12511 * provided rpi via a bitmask.
12512 **/
12513int
12514lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12515{
12516 LPFC_MBOXQ_t *mboxq;
12517 struct lpfc_hba *phba = ndlp->phba;
12518 int rc;
12519
12520 /* The port is notified of the header region via a mailbox command. */
12521 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12522 if (!mboxq)
12523 return -ENOMEM;
12524
12525 /* Post all rpi memory regions to the port. */
12526 lpfc_resume_rpi(mboxq, ndlp);
12527 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12528 if (rc == MBX_NOT_FINISHED) {
12529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12530 "2010 Resume RPI Mailbox failed "
12531 "status %d, mbxStatus x%x\n", rc,
12532 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12533 mempool_free(mboxq, phba->mbox_mem_pool);
12534 return -EIO;
12535 }
12536 return 0;
12537}
12538
12539/**
12540 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050012541 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040012542 *
James Smart76a95d72010-11-20 23:11:48 -050012543 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040012544 *
12545 * Returns:
12546 * 0 success
12547 * -Evalue otherwise
12548 **/
12549int
James Smart76a95d72010-11-20 23:11:48 -050012550lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040012551{
12552 LPFC_MBOXQ_t *mboxq;
12553 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012554 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012555 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050012556 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040012557 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12558 if (!mboxq)
12559 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050012560 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012561 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12562 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012563 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050012564 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040012565 "2022 INIT VPI Mailbox failed "
12566 "status %d, mbxStatus x%x\n", rc,
12567 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012568 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012569 }
James Smart6a9c52c2009-10-02 15:16:51 -040012570 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050012571 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040012572
12573 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012574}
12575
12576/**
12577 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12578 * @phba: pointer to lpfc hba data structure.
12579 * @mboxq: Pointer to mailbox object.
12580 *
12581 * This routine is invoked to manually add a single FCF record. The caller
12582 * must pass a completely initialized FCF_Record. This routine takes
12583 * care of the nonembedded mailbox operations.
12584 **/
12585static void
12586lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12587{
12588 void *virt_addr;
12589 union lpfc_sli4_cfg_shdr *shdr;
12590 uint32_t shdr_status, shdr_add_status;
12591
12592 virt_addr = mboxq->sge_array->addr[0];
12593 /* The IOCTL status is embedded in the mailbox subheader. */
12594 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12595 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12596 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12597
12598 if ((shdr_status || shdr_add_status) &&
12599 (shdr_status != STATUS_FCF_IN_USE))
12600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12601 "2558 ADD_FCF_RECORD mailbox failed with "
12602 "status x%x add_status x%x\n",
12603 shdr_status, shdr_add_status);
12604
12605 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12606}
12607
12608/**
12609 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12610 * @phba: pointer to lpfc hba data structure.
12611 * @fcf_record: pointer to the initialized fcf record to add.
12612 *
12613 * This routine is invoked to manually add a single FCF record. The caller
12614 * must pass a completely initialized FCF_Record. This routine takes
12615 * care of the nonembedded mailbox operations.
12616 **/
12617int
12618lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12619{
12620 int rc = 0;
12621 LPFC_MBOXQ_t *mboxq;
12622 uint8_t *bytep;
12623 void *virt_addr;
12624 dma_addr_t phys_addr;
12625 struct lpfc_mbx_sge sge;
12626 uint32_t alloc_len, req_len;
12627 uint32_t fcfindex;
12628
12629 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12630 if (!mboxq) {
12631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12632 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12633 return -ENOMEM;
12634 }
12635
12636 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12637 sizeof(uint32_t);
12638
12639 /* Allocate DMA memory and set up the non-embedded mailbox command */
12640 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12641 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12642 req_len, LPFC_SLI4_MBX_NEMBED);
12643 if (alloc_len < req_len) {
12644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12645 "2523 Allocated DMA memory size (x%x) is "
12646 "less than the requested DMA memory "
12647 "size (x%x)\n", alloc_len, req_len);
12648 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12649 return -ENOMEM;
12650 }
12651
12652 /*
12653 * Get the first SGE entry from the non-embedded DMA memory. This
12654 * routine only uses a single SGE.
12655 */
12656 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12657 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012658 virt_addr = mboxq->sge_array->addr[0];
12659 /*
12660 * Configure the FCF record for FCFI 0. This is the driver's
12661 * hardcoded default and gets used in nonFIP mode.
12662 */
12663 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12664 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12665 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12666
12667 /*
12668 * Copy the fcf_index and the FCF Record Data. The data starts after
12669 * the FCoE header plus word10. The data copy needs to be endian
12670 * correct.
12671 */
12672 bytep += sizeof(uint32_t);
12673 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12674 mboxq->vport = phba->pport;
12675 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12676 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12677 if (rc == MBX_NOT_FINISHED) {
12678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12679 "2515 ADD_FCF_RECORD mailbox failed with "
12680 "status 0x%x\n", rc);
12681 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12682 rc = -EIO;
12683 } else
12684 rc = 0;
12685
12686 return rc;
12687}
12688
12689/**
12690 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12691 * @phba: pointer to lpfc hba data structure.
12692 * @fcf_record: pointer to the fcf record to write the default data.
12693 * @fcf_index: FCF table entry index.
12694 *
12695 * This routine is invoked to build the driver's default FCF record. The
12696 * values used are hardcoded. This routine handles memory initialization.
12697 *
12698 **/
12699void
12700lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12701 struct fcf_record *fcf_record,
12702 uint16_t fcf_index)
12703{
12704 memset(fcf_record, 0, sizeof(struct fcf_record));
12705 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12706 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12707 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12708 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12709 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12710 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12711 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12712 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12713 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12714 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12715 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12716 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12717 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012718 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012719 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12720 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12721 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12722 /* Set the VLAN bit map */
12723 if (phba->valid_vlan) {
12724 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12725 = 1 << (phba->vlan_id % 8);
12726 }
12727}
12728
12729/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012730 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012731 * @phba: pointer to lpfc hba data structure.
12732 * @fcf_index: FCF table entry offset.
12733 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012734 * This routine is invoked to scan the entire FCF table by reading FCF
12735 * record and processing it one at a time starting from the @fcf_index
12736 * for initial FCF discovery or fast FCF failover rediscovery.
12737 *
12738 * Return 0 if the mailbox command is submitted sucessfully, none 0
12739 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012740 **/
12741int
James Smart0c9ab6f2010-02-26 14:15:57 -050012742lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012743{
12744 int rc = 0, error;
12745 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012746
James Smart32b97932009-07-19 10:01:21 -040012747 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012748 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12749 if (!mboxq) {
12750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12751 "2000 Failed to allocate mbox for "
12752 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012753 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012754 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012755 }
James Smartecfd03c2010-02-12 14:41:27 -050012756 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012757 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012758 if (rc) {
12759 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012760 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012761 }
James Smartecfd03c2010-02-12 14:41:27 -050012762 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012763 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012764 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040012765
12766 spin_lock_irq(&phba->hbalock);
12767 phba->hba_flag |= FCF_TS_INPROG;
12768 spin_unlock_irq(&phba->hbalock);
12769
James Smart6fb120a2009-05-22 14:52:59 -040012770 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012771 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012772 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012773 else {
James Smart38b92ef2010-08-04 16:11:39 -040012774 /* Reset eligible FCF count for new scan */
12775 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040012776 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040012777 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012778 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012779fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012780 if (error) {
12781 if (mboxq)
12782 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040012783 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040012784 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040012785 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040012786 spin_unlock_irq(&phba->hbalock);
12787 }
James Smart6fb120a2009-05-22 14:52:59 -040012788 return error;
12789}
James Smarta0c87cb2009-07-19 10:01:10 -040012790
12791/**
James Smarta93ff372010-10-22 11:06:08 -040012792 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050012793 * @phba: pointer to lpfc hba data structure.
12794 * @fcf_index: FCF table entry offset.
12795 *
12796 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040012797 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050012798 *
12799 * Return 0 if the mailbox command is submitted sucessfully, none 0
12800 * otherwise.
12801 **/
12802int
12803lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12804{
12805 int rc = 0, error;
12806 LPFC_MBOXQ_t *mboxq;
12807
12808 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12809 if (!mboxq) {
12810 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12811 "2763 Failed to allocate mbox for "
12812 "READ_FCF cmd\n");
12813 error = -ENOMEM;
12814 goto fail_fcf_read;
12815 }
12816 /* Construct the read FCF record mailbox command */
12817 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12818 if (rc) {
12819 error = -EINVAL;
12820 goto fail_fcf_read;
12821 }
12822 /* Issue the mailbox command asynchronously */
12823 mboxq->vport = phba->pport;
12824 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12825 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12826 if (rc == MBX_NOT_FINISHED)
12827 error = -EIO;
12828 else
12829 error = 0;
12830
12831fail_fcf_read:
12832 if (error && mboxq)
12833 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12834 return error;
12835}
12836
12837/**
12838 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12839 * @phba: pointer to lpfc hba data structure.
12840 * @fcf_index: FCF table entry offset.
12841 *
12842 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040012843 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050012844 *
12845 * Return 0 if the mailbox command is submitted sucessfully, none 0
12846 * otherwise.
12847 **/
12848int
12849lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12850{
12851 int rc = 0, error;
12852 LPFC_MBOXQ_t *mboxq;
12853
12854 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12855 if (!mboxq) {
12856 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12857 "2758 Failed to allocate mbox for "
12858 "READ_FCF cmd\n");
12859 error = -ENOMEM;
12860 goto fail_fcf_read;
12861 }
12862 /* Construct the read FCF record mailbox command */
12863 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12864 if (rc) {
12865 error = -EINVAL;
12866 goto fail_fcf_read;
12867 }
12868 /* Issue the mailbox command asynchronously */
12869 mboxq->vport = phba->pport;
12870 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12871 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12872 if (rc == MBX_NOT_FINISHED)
12873 error = -EIO;
12874 else
12875 error = 0;
12876
12877fail_fcf_read:
12878 if (error && mboxq)
12879 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12880 return error;
12881}
12882
12883/**
12884 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12885 * @phba: pointer to lpfc hba data structure.
12886 *
12887 * This routine is to get the next eligible FCF record index in a round
12888 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040012889 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050012890 * shall be returned, otherwise, the next eligible FCF record's index
12891 * shall be returned.
12892 **/
12893uint16_t
12894lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12895{
12896 uint16_t next_fcf_index;
12897
James Smart3804dc82010-07-14 15:31:37 -040012898 /* Search start from next bit of currently registered FCF index */
12899 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12900 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050012901 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12902 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040012903 next_fcf_index);
12904
James Smart0c9ab6f2010-02-26 14:15:57 -050012905 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12906 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12907 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12908 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050012909
James Smart3804dc82010-07-14 15:31:37 -040012910 /* Check roundrobin failover list empty condition */
12911 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12912 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12913 "2844 No roundrobin failover FCF available\n");
12914 return LPFC_FCOE_FCF_NEXT_NONE;
12915 }
12916
James Smart3804dc82010-07-14 15:31:37 -040012917 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012918 "2845 Get next roundrobin failover FCF (x%x)\n",
12919 next_fcf_index);
12920
James Smart0c9ab6f2010-02-26 14:15:57 -050012921 return next_fcf_index;
12922}
12923
12924/**
12925 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12926 * @phba: pointer to lpfc hba data structure.
12927 *
12928 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012929 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012930 * does not go beyond the range of the driver allocated bmask dimension
12931 * before setting the bit.
12932 *
12933 * Returns 0 if the index bit successfully set, otherwise, it returns
12934 * -EINVAL.
12935 **/
12936int
12937lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12938{
12939 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12940 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012941 "2610 FCF (x%x) reached driver's book "
12942 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012943 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12944 return -EINVAL;
12945 }
12946 /* Set the eligible FCF record index bmask */
12947 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12948
James Smart3804dc82010-07-14 15:31:37 -040012949 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012950 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040012951 "bmask\n", fcf_index);
12952
James Smart0c9ab6f2010-02-26 14:15:57 -050012953 return 0;
12954}
12955
12956/**
James Smart3804dc82010-07-14 15:31:37 -040012957 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050012958 * @phba: pointer to lpfc hba data structure.
12959 *
12960 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012961 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012962 * does not go beyond the range of the driver allocated bmask dimension
12963 * before clearing the bit.
12964 **/
12965void
12966lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12967{
12968 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12969 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012970 "2762 FCF (x%x) reached driver's book "
12971 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012972 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12973 return;
12974 }
12975 /* Clear the eligible FCF record index bmask */
12976 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040012977
12978 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012979 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040012980 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050012981}
12982
12983/**
James Smartecfd03c2010-02-12 14:41:27 -050012984 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12985 * @phba: pointer to lpfc hba data structure.
12986 *
12987 * This routine is the completion routine for the rediscover FCF table mailbox
12988 * command. If the mailbox command returned failure, it will try to stop the
12989 * FCF rediscover wait timer.
12990 **/
12991void
12992lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12993{
12994 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12995 uint32_t shdr_status, shdr_add_status;
12996
12997 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12998
12999 shdr_status = bf_get(lpfc_mbox_hdr_status,
13000 &redisc_fcf->header.cfg_shdr.response);
13001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13002 &redisc_fcf->header.cfg_shdr.response);
13003 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050013004 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050013005 "2746 Requesting for FCF rediscovery failed "
13006 "status x%x add_status x%x\n",
13007 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050013008 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050013009 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013010 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013011 spin_unlock_irq(&phba->hbalock);
13012 /*
13013 * CVL event triggered FCF rediscover request failed,
13014 * last resort to re-try current registered FCF entry.
13015 */
13016 lpfc_retry_pport_discovery(phba);
13017 } else {
13018 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013019 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013020 spin_unlock_irq(&phba->hbalock);
13021 /*
13022 * DEAD FCF event triggered FCF rediscover request
13023 * failed, last resort to fail over as a link down
13024 * to FCF registration.
13025 */
13026 lpfc_sli4_fcf_dead_failthrough(phba);
13027 }
James Smart0c9ab6f2010-02-26 14:15:57 -050013028 } else {
13029 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013030 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050013031 /*
13032 * Start FCF rediscovery wait timer for pending FCF
13033 * before rescan FCF record table.
13034 */
13035 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050013036 }
James Smartecfd03c2010-02-12 14:41:27 -050013037
13038 mempool_free(mbox, phba->mbox_mem_pool);
13039}
13040
13041/**
James Smart3804dc82010-07-14 15:31:37 -040013042 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050013043 * @phba: pointer to lpfc hba data structure.
13044 *
13045 * This routine is invoked to request for rediscovery of the entire FCF table
13046 * by the port.
13047 **/
13048int
13049lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13050{
13051 LPFC_MBOXQ_t *mbox;
13052 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13053 int rc, length;
13054
James Smart0c9ab6f2010-02-26 14:15:57 -050013055 /* Cancel retry delay timers to all vports before FCF rediscover */
13056 lpfc_cancel_all_vport_retry_delay_timer(phba);
13057
James Smartecfd03c2010-02-12 14:41:27 -050013058 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13059 if (!mbox) {
13060 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13061 "2745 Failed to allocate mbox for "
13062 "requesting FCF rediscover.\n");
13063 return -ENOMEM;
13064 }
13065
13066 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13067 sizeof(struct lpfc_sli4_cfg_mhdr));
13068 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13069 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13070 length, LPFC_SLI4_MBX_EMBED);
13071
13072 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13073 /* Set count to 0 for invalidating the entire FCF database */
13074 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13075
13076 /* Issue the mailbox command asynchronously */
13077 mbox->vport = phba->pport;
13078 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13079 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13080
13081 if (rc == MBX_NOT_FINISHED) {
13082 mempool_free(mbox, phba->mbox_mem_pool);
13083 return -EIO;
13084 }
13085 return 0;
13086}
13087
13088/**
James Smartfc2b9892010-02-26 14:15:29 -050013089 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13090 * @phba: pointer to lpfc hba data structure.
13091 *
13092 * This function is the failover routine as a last resort to the FCF DEAD
13093 * event when driver failed to perform fast FCF failover.
13094 **/
13095void
13096lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13097{
13098 uint32_t link_state;
13099
13100 /*
13101 * Last resort as FCF DEAD event failover will treat this as
13102 * a link down, but save the link state because we don't want
13103 * it to be changed to Link Down unless it is already down.
13104 */
13105 link_state = phba->link_state;
13106 lpfc_linkdown(phba);
13107 phba->link_state = link_state;
13108
13109 /* Unregister FCF if no devices connected to it */
13110 lpfc_unregister_unused_fcf(phba);
13111}
13112
13113/**
James Smarta0c87cb2009-07-19 10:01:10 -040013114 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13115 * @phba: pointer to lpfc hba data structure.
13116 *
13117 * This function read region 23 and parse TLV for port status to
13118 * decide if the user disaled the port. If the TLV indicates the
13119 * port is disabled, the hba_flag is set accordingly.
13120 **/
13121void
13122lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13123{
13124 LPFC_MBOXQ_t *pmb = NULL;
13125 MAILBOX_t *mb;
13126 uint8_t *rgn23_data = NULL;
13127 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13128 int rc;
13129
13130 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13131 if (!pmb) {
13132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13133 "2600 lpfc_sli_read_serdes_param failed to"
13134 " allocate mailbox memory\n");
13135 goto out;
13136 }
13137 mb = &pmb->u.mb;
13138
13139 /* Get adapter Region 23 data */
13140 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13141 if (!rgn23_data)
13142 goto out;
13143
13144 do {
13145 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13146 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13147
13148 if (rc != MBX_SUCCESS) {
13149 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13150 "2601 lpfc_sli_read_link_ste failed to"
13151 " read config region 23 rc 0x%x Status 0x%x\n",
13152 rc, mb->mbxStatus);
13153 mb->un.varDmp.word_cnt = 0;
13154 }
13155 /*
13156 * dump mem may return a zero when finished or we got a
13157 * mailbox error, either way we are done.
13158 */
13159 if (mb->un.varDmp.word_cnt == 0)
13160 break;
13161 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13162 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13163
13164 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13165 rgn23_data + offset,
13166 mb->un.varDmp.word_cnt);
13167 offset += mb->un.varDmp.word_cnt;
13168 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13169
13170 data_size = offset;
13171 offset = 0;
13172
13173 if (!data_size)
13174 goto out;
13175
13176 /* Check the region signature first */
13177 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13179 "2619 Config region 23 has bad signature\n");
13180 goto out;
13181 }
13182 offset += 4;
13183
13184 /* Check the data structure version */
13185 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13187 "2620 Config region 23 has bad version\n");
13188 goto out;
13189 }
13190 offset += 4;
13191
13192 /* Parse TLV entries in the region */
13193 while (offset < data_size) {
13194 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13195 break;
13196 /*
13197 * If the TLV is not driver specific TLV or driver id is
13198 * not linux driver id, skip the record.
13199 */
13200 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13201 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13202 (rgn23_data[offset + 3] != 0)) {
13203 offset += rgn23_data[offset + 1] * 4 + 4;
13204 continue;
13205 }
13206
13207 /* Driver found a driver specific TLV in the config region */
13208 sub_tlv_len = rgn23_data[offset + 1] * 4;
13209 offset += 4;
13210 tlv_offset = 0;
13211
13212 /*
13213 * Search for configured port state sub-TLV.
13214 */
13215 while ((offset < data_size) &&
13216 (tlv_offset < sub_tlv_len)) {
13217 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13218 offset += 4;
13219 tlv_offset += 4;
13220 break;
13221 }
13222 if (rgn23_data[offset] != PORT_STE_TYPE) {
13223 offset += rgn23_data[offset + 1] * 4 + 4;
13224 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13225 continue;
13226 }
13227
13228 /* This HBA contains PORT_STE configured */
13229 if (!rgn23_data[offset + 2])
13230 phba->hba_flag |= LINK_DISABLED;
13231
13232 goto out;
13233 }
13234 }
13235out:
13236 if (pmb)
13237 mempool_free(pmb, phba->mbox_mem_pool);
13238 kfree(rgn23_data);
13239 return;
13240}
James Smart695a8142010-01-26 23:08:03 -050013241
13242/**
13243 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13244 * @vport: pointer to vport data structure.
13245 *
13246 * This function iterate through the mailboxq and clean up all REG_LOGIN
13247 * and REG_VPI mailbox commands associated with the vport. This function
13248 * is called when driver want to restart discovery of the vport due to
13249 * a Clear Virtual Link event.
13250 **/
13251void
13252lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13253{
13254 struct lpfc_hba *phba = vport->phba;
13255 LPFC_MBOXQ_t *mb, *nextmb;
13256 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040013257 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040013258 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040013259 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040013260 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050013261 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050013262
James Smartd439d282010-09-29 11:18:45 -040013263 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050013264 spin_lock_irq(&phba->hbalock);
13265 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13266 if (mb->vport != vport)
13267 continue;
13268
13269 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13270 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13271 continue;
13272
James Smartd439d282010-09-29 11:18:45 -040013273 list_del(&mb->list);
13274 list_add_tail(&mb->list, &mbox_cmd_list);
13275 }
13276 /* Clean up active mailbox command with the vport */
13277 mb = phba->sli.mbox_active;
13278 if (mb && (mb->vport == vport)) {
13279 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13280 (mb->u.mb.mbxCommand == MBX_REG_VPI))
13281 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13282 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13283 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13284 /* Put reference count for delayed processing */
13285 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13286 /* Unregister the RPI when mailbox complete */
13287 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13288 }
13289 }
James Smart63e801c2010-11-20 23:14:19 -050013290 /* Cleanup any mailbox completions which are not yet processed */
13291 do {
13292 restart_loop = 0;
13293 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13294 /*
13295 * If this mailox is already processed or it is
13296 * for another vport ignore it.
13297 */
13298 if ((mb->vport != vport) ||
13299 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13300 continue;
13301
13302 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13303 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13304 continue;
13305
13306 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13307 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13308 ndlp = (struct lpfc_nodelist *)mb->context2;
13309 /* Unregister the RPI when mailbox complete */
13310 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13311 restart_loop = 1;
13312 spin_unlock_irq(&phba->hbalock);
13313 spin_lock(shost->host_lock);
13314 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13315 spin_unlock(shost->host_lock);
13316 spin_lock_irq(&phba->hbalock);
13317 break;
13318 }
13319 }
13320 } while (restart_loop);
13321
James Smartd439d282010-09-29 11:18:45 -040013322 spin_unlock_irq(&phba->hbalock);
13323
13324 /* Release the cleaned-up mailbox commands */
13325 while (!list_empty(&mbox_cmd_list)) {
13326 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050013327 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smartd7c47992010-06-08 18:31:54 -040013328 if (phba->sli_rev == LPFC_SLI_REV4)
13329 __lpfc_sli4_free_rpi(phba,
13330 mb->u.mb.un.varRegLogin.rpi);
James Smart695a8142010-01-26 23:08:03 -050013331 mp = (struct lpfc_dmabuf *) (mb->context1);
13332 if (mp) {
13333 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13334 kfree(mp);
13335 }
James Smart78730cf2010-04-06 15:06:30 -040013336 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040013337 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040013338 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013339 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040013340 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013341 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040013342 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040013343 }
James Smart695a8142010-01-26 23:08:03 -050013344 }
James Smart695a8142010-01-26 23:08:03 -050013345 mempool_free(mb, phba->mbox_mem_pool);
13346 }
James Smartd439d282010-09-29 11:18:45 -040013347
13348 /* Release the ndlp with the cleaned-up active mailbox command */
13349 if (act_mbx_ndlp) {
13350 spin_lock(shost->host_lock);
13351 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13352 spin_unlock(shost->host_lock);
13353 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050013354 }
James Smart695a8142010-01-26 23:08:03 -050013355}
13356
James Smart2a9bf3d2010-06-07 15:24:45 -040013357/**
13358 * lpfc_drain_txq - Drain the txq
13359 * @phba: Pointer to HBA context object.
13360 *
13361 * This function attempt to submit IOCBs on the txq
13362 * to the adapter. For SLI4 adapters, the txq contains
13363 * ELS IOCBs that have been deferred because the there
13364 * are no SGLs. This congestion can occur with large
13365 * vport counts during node discovery.
13366 **/
13367
13368uint32_t
13369lpfc_drain_txq(struct lpfc_hba *phba)
13370{
13371 LIST_HEAD(completions);
13372 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13373 struct lpfc_iocbq *piocbq = 0;
13374 unsigned long iflags = 0;
13375 char *fail_msg = NULL;
13376 struct lpfc_sglq *sglq;
13377 union lpfc_wqe wqe;
13378
13379 spin_lock_irqsave(&phba->hbalock, iflags);
13380 if (pring->txq_cnt > pring->txq_max)
13381 pring->txq_max = pring->txq_cnt;
13382
13383 spin_unlock_irqrestore(&phba->hbalock, iflags);
13384
13385 while (pring->txq_cnt) {
13386 spin_lock_irqsave(&phba->hbalock, iflags);
13387
James Smart19ca7602010-11-20 23:11:55 -050013388 piocbq = lpfc_sli_ringtx_get(phba, pring);
13389 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013390 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050013391 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013392 spin_unlock_irqrestore(&phba->hbalock, iflags);
13393 break;
13394 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040013395 if (!piocbq) {
13396 /* The txq_cnt out of sync. This should
13397 * never happen
13398 */
13399 sglq = __lpfc_clear_active_sglq(phba,
13400 sglq->sli4_xritag);
13401 spin_unlock_irqrestore(&phba->hbalock, iflags);
13402 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13403 "2823 txq empty and txq_cnt is %d\n ",
13404 pring->txq_cnt);
13405 break;
13406 }
13407 }
13408
13409 /* The xri and iocb resources secured,
13410 * attempt to issue request
13411 */
13412 piocbq->sli4_xritag = sglq->sli4_xritag;
13413 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13414 fail_msg = "to convert bpl to sgl";
13415 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13416 fail_msg = "to convert iocb to wqe";
13417 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13418 fail_msg = " - Wq is full";
13419 else
13420 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13421
13422 if (fail_msg) {
13423 /* Failed means we can't issue and need to cancel */
13424 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13425 "2822 IOCB failed %s iotag 0x%x "
13426 "xri 0x%x\n",
13427 fail_msg,
13428 piocbq->iotag, piocbq->sli4_xritag);
13429 list_add_tail(&piocbq->list, &completions);
13430 }
13431 spin_unlock_irqrestore(&phba->hbalock, iflags);
13432 }
13433
James Smart2a9bf3d2010-06-07 15:24:45 -040013434 /* Cancel all the IOCBs that cannot be issued */
13435 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13436 IOERR_SLI_ABORTED);
13437
13438 return pring->txq_cnt;
13439}