blob: ce4145377efddd7d9b8e2dfca1c0fba6d838443f [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040068static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
James Smarte59058c2008-08-24 21:49:00 -0400408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
James Smarted957682007-06-17 19:56:37 -0500418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
James Smarte59058c2008-08-24 21:49:00 -0400425/**
James Smart3621a712009-04-06 18:47:14 -0400426 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
James Smarted957682007-06-17 19:56:37 -0500435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
James Smarte59058c2008-08-24 21:49:00 -0400442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
James Smart2e0fef82007-06-17 19:56:36 -0500451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400458
459 if (iocbq)
460 phba->iocb_cnt++;
461 if (phba->iocb_cnt > phba->iocb_max)
462 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400463 return iocbq;
464}
465
James Smarte59058c2008-08-24 21:49:00 -0400466/**
James Smartda0436e2009-05-22 14:51:39 -0400467 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468 * @phba: Pointer to HBA context object.
469 * @xritag: XRI value.
470 *
471 * This function clears the sglq pointer from the array of acive
472 * sglq's. The xritag that is passed in is used to index into the
473 * array. Before the xritag can be used it needs to be adjusted
474 * by subtracting the xribase.
475 *
476 * Returns sglq ponter = success, NULL = Failure.
477 **/
478static struct lpfc_sglq *
479__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480{
481 uint16_t adj_xri;
482 struct lpfc_sglq *sglq;
483 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 return NULL;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
James Smart0f65ff62010-02-26 14:14:23 -0500503struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
506 uint16_t adj_xri;
507 struct lpfc_sglq *sglq;
508 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 return NULL;
511 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 return sglq;
513}
514
515/**
516 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
517 * @phba: Pointer to HBA context object.
518 *
519 * This function is called with hbalock held. This function
520 * Gets a new driver sglq object from the sglq list. If the
521 * list is not empty then it is successful, it returns pointer to the newly
522 * allocated sglq object else it returns NULL.
523 **/
524static struct lpfc_sglq *
525__lpfc_sli_get_sglq(struct lpfc_hba *phba)
526{
527 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528 struct lpfc_sglq *sglq = NULL;
529 uint16_t adj_xri;
530 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400531 if (!sglq)
532 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400533 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
James Smart0f65ff62010-02-26 14:14:23 -0500535 sglq->state = SGL_ALLOCATED;
James Smartda0436e2009-05-22 14:51:39 -0400536 return sglq;
537}
538
539/**
James Smart3621a712009-04-06 18:47:14 -0400540 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400541 * @phba: Pointer to HBA context object.
542 *
543 * This function is called with no lock held. This function
544 * allocates a new driver iocb object from the iocb pool. If the
545 * allocation is successful, it returns pointer to the newly
546 * allocated iocb object else it returns NULL.
547 **/
James Smart2e0fef82007-06-17 19:56:36 -0500548struct lpfc_iocbq *
549lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500550{
James Smart2e0fef82007-06-17 19:56:36 -0500551 struct lpfc_iocbq * iocbq = NULL;
552 unsigned long iflags;
553
554 spin_lock_irqsave(&phba->hbalock, iflags);
555 iocbq = __lpfc_sli_get_iocbq(phba);
556 spin_unlock_irqrestore(&phba->hbalock, iflags);
557 return iocbq;
558}
559
James Smarte59058c2008-08-24 21:49:00 -0400560/**
James Smart4f774512009-05-22 14:52:35 -0400561 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562 * @phba: Pointer to HBA context object.
563 * @iocbq: Pointer to driver iocb object.
564 *
565 * This function is called with hbalock held to release driver
566 * iocb object to the iocb pool. The iotag in the iocb object
567 * does not change for each use of the iocb object. This function
568 * clears all other fields of the iocb object when it is freed.
569 * The sqlq structure that holds the xritag and phys and virtual
570 * mappings for the scatter gather list is retrieved from the
571 * active array of sglq. The get of the sglq pointer also clears
572 * the entry in the array. If the status of the IO indiactes that
573 * this IO was aborted then the sglq entry it put on the
574 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575 * IO has good status or fails for any other reason then the sglq
576 * entry is added to the free list (lpfc_sgl_list).
577 **/
578static void
579__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580{
581 struct lpfc_sglq *sglq;
582 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400583 unsigned long iflag = 0;
584 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400585
586 if (iocbq->sli4_xritag == NO_XRI)
587 sglq = NULL;
588 else
589 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500591 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400593 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594 iflag);
595 list_add(&sglq->list,
596 &phba->sli4_hba.lpfc_abts_els_sgl_list);
597 spin_unlock_irqrestore(
598 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500599 } else {
600 sglq->state = SGL_FREED;
James Smart4f774512009-05-22 14:52:35 -0400601 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400602
603 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400604 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400605 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500606 }
James Smart4f774512009-05-22 14:52:35 -0400607 }
608
609
610 /*
611 * Clean all volatile data fields, preserve iotag and node struct.
612 */
613 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
614 iocbq->sli4_xritag = NO_XRI;
615 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
616}
617
James Smart2a9bf3d2010-06-07 15:24:45 -0400618
James Smart4f774512009-05-22 14:52:35 -0400619/**
James Smart3772a992009-05-22 14:50:54 -0400620 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
621 * @phba: Pointer to HBA context object.
622 * @iocbq: Pointer to driver iocb object.
623 *
624 * This function is called with hbalock held to release driver
625 * iocb object to the iocb pool. The iotag in the iocb object
626 * does not change for each use of the iocb object. This function
627 * clears all other fields of the iocb object when it is freed.
628 **/
629static void
630__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
631{
632 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
633
634 /*
635 * Clean all volatile data fields, preserve iotag and node struct.
636 */
637 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
638 iocbq->sli4_xritag = NO_XRI;
639 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
640}
641
642/**
James Smart3621a712009-04-06 18:47:14 -0400643 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400644 * @phba: Pointer to HBA context object.
645 * @iocbq: Pointer to driver iocb object.
646 *
647 * This function is called with hbalock held to release driver
648 * iocb object to the iocb pool. The iotag in the iocb object
649 * does not change for each use of the iocb object. This function
650 * clears all other fields of the iocb object when it is freed.
651 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100652static void
James Smart2e0fef82007-06-17 19:56:36 -0500653__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
654{
James Smart3772a992009-05-22 14:50:54 -0400655 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400656 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -0500657}
658
James Smarte59058c2008-08-24 21:49:00 -0400659/**
James Smart3621a712009-04-06 18:47:14 -0400660 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400661 * @phba: Pointer to HBA context object.
662 * @iocbq: Pointer to driver iocb object.
663 *
664 * This function is called with no lock held to release the iocb to
665 * iocb pool.
666 **/
James Smart2e0fef82007-06-17 19:56:36 -0500667void
668lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
669{
670 unsigned long iflags;
671
672 /*
673 * Clean all volatile data fields, preserve iotag and node struct.
674 */
675 spin_lock_irqsave(&phba->hbalock, iflags);
676 __lpfc_sli_release_iocbq(phba, iocbq);
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678}
679
James Smarte59058c2008-08-24 21:49:00 -0400680/**
James Smarta257bf92009-04-06 18:48:10 -0400681 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
682 * @phba: Pointer to HBA context object.
683 * @iocblist: List of IOCBs.
684 * @ulpstatus: ULP status in IOCB command field.
685 * @ulpWord4: ULP word-4 in IOCB command field.
686 *
687 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
688 * on the list by invoking the complete callback function associated with the
689 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
690 * fields.
691 **/
692void
693lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
694 uint32_t ulpstatus, uint32_t ulpWord4)
695{
696 struct lpfc_iocbq *piocb;
697
698 while (!list_empty(iocblist)) {
699 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
700
701 if (!piocb->iocb_cmpl)
702 lpfc_sli_release_iocbq(phba, piocb);
703 else {
704 piocb->iocb.ulpStatus = ulpstatus;
705 piocb->iocb.un.ulpWord[4] = ulpWord4;
706 (piocb->iocb_cmpl) (phba, piocb, piocb);
707 }
708 }
709 return;
710}
711
712/**
James Smart3621a712009-04-06 18:47:14 -0400713 * lpfc_sli_iocb_cmd_type - Get the iocb type
714 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400715 *
716 * This function is called by ring event handler function to get the iocb type.
717 * This function translates the iocb command to an iocb command type used to
718 * decide the final disposition of each completed IOCB.
719 * The function returns
720 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
721 * LPFC_SOL_IOCB if it is a solicited iocb completion
722 * LPFC_ABORT_IOCB if it is an abort iocb
723 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
724 *
725 * The caller is not required to hold any lock.
726 **/
dea31012005-04-17 16:05:31 -0500727static lpfc_iocb_type
728lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
729{
730 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
731
732 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
733 return 0;
734
735 switch (iocb_cmnd) {
736 case CMD_XMIT_SEQUENCE_CR:
737 case CMD_XMIT_SEQUENCE_CX:
738 case CMD_XMIT_BCAST_CN:
739 case CMD_XMIT_BCAST_CX:
740 case CMD_ELS_REQUEST_CR:
741 case CMD_ELS_REQUEST_CX:
742 case CMD_CREATE_XRI_CR:
743 case CMD_CREATE_XRI_CX:
744 case CMD_GET_RPI_CN:
745 case CMD_XMIT_ELS_RSP_CX:
746 case CMD_GET_RPI_CR:
747 case CMD_FCP_IWRITE_CR:
748 case CMD_FCP_IWRITE_CX:
749 case CMD_FCP_IREAD_CR:
750 case CMD_FCP_IREAD_CX:
751 case CMD_FCP_ICMND_CR:
752 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500753 case CMD_FCP_TSEND_CX:
754 case CMD_FCP_TRSP_CX:
755 case CMD_FCP_TRECEIVE_CX:
756 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500757 case CMD_ADAPTER_MSG:
758 case CMD_ADAPTER_DUMP:
759 case CMD_XMIT_SEQUENCE64_CR:
760 case CMD_XMIT_SEQUENCE64_CX:
761 case CMD_XMIT_BCAST64_CN:
762 case CMD_XMIT_BCAST64_CX:
763 case CMD_ELS_REQUEST64_CR:
764 case CMD_ELS_REQUEST64_CX:
765 case CMD_FCP_IWRITE64_CR:
766 case CMD_FCP_IWRITE64_CX:
767 case CMD_FCP_IREAD64_CR:
768 case CMD_FCP_IREAD64_CX:
769 case CMD_FCP_ICMND64_CR:
770 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500771 case CMD_FCP_TSEND64_CX:
772 case CMD_FCP_TRSP64_CX:
773 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500774 case CMD_GEN_REQUEST64_CR:
775 case CMD_GEN_REQUEST64_CX:
776 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400777 case DSSCMD_IWRITE64_CR:
778 case DSSCMD_IWRITE64_CX:
779 case DSSCMD_IREAD64_CR:
780 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500781 type = LPFC_SOL_IOCB;
782 break;
783 case CMD_ABORT_XRI_CN:
784 case CMD_ABORT_XRI_CX:
785 case CMD_CLOSE_XRI_CN:
786 case CMD_CLOSE_XRI_CX:
787 case CMD_XRI_ABORTED_CX:
788 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400789 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500790 type = LPFC_ABORT_IOCB;
791 break;
792 case CMD_RCV_SEQUENCE_CX:
793 case CMD_RCV_ELS_REQ_CX:
794 case CMD_RCV_SEQUENCE64_CX:
795 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400796 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500797 case CMD_IOCB_RCV_SEQ64_CX:
798 case CMD_IOCB_RCV_ELS64_CX:
799 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500800 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500801 type = LPFC_UNSOL_IOCB;
802 break;
James Smart3163f722008-02-08 18:50:25 -0500803 case CMD_IOCB_XMIT_MSEQ64_CR:
804 case CMD_IOCB_XMIT_MSEQ64_CX:
805 case CMD_IOCB_RCV_SEQ_LIST64_CX:
806 case CMD_IOCB_RCV_ELS_LIST64_CX:
807 case CMD_IOCB_CLOSE_EXTENDED_CN:
808 case CMD_IOCB_ABORT_EXTENDED_CN:
809 case CMD_IOCB_RET_HBQE64_CN:
810 case CMD_IOCB_FCP_IBIDIR64_CR:
811 case CMD_IOCB_FCP_IBIDIR64_CX:
812 case CMD_IOCB_FCP_ITASKMGT64_CX:
813 case CMD_IOCB_LOGENTRY_CN:
814 case CMD_IOCB_LOGENTRY_ASYNC_CN:
815 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700816 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500817 type = LPFC_UNKNOWN_IOCB;
818 break;
dea31012005-04-17 16:05:31 -0500819 default:
820 type = LPFC_UNKNOWN_IOCB;
821 break;
822 }
823
824 return type;
825}
826
James Smarte59058c2008-08-24 21:49:00 -0400827/**
James Smart3621a712009-04-06 18:47:14 -0400828 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400829 * @phba: Pointer to HBA context object.
830 *
831 * This function is called from SLI initialization code
832 * to configure every ring of the HBA's SLI interface. The
833 * caller is not required to hold any lock. This function issues
834 * a config_ring mailbox command for each ring.
835 * This function returns zero if successful else returns a negative
836 * error code.
837 **/
dea31012005-04-17 16:05:31 -0500838static int
James Smarted957682007-06-17 19:56:37 -0500839lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500840{
841 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500842 LPFC_MBOXQ_t *pmb;
843 MAILBOX_t *pmbox;
844 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500845
James Smarted957682007-06-17 19:56:37 -0500846 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
847 if (!pmb)
848 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400849 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500850 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500851 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500852 lpfc_config_ring(phba, i, pmb);
853 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
854 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400856 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500857 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
858 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400859 rc, pmbox->mbxCommand,
860 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500861 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500862 ret = -ENXIO;
863 break;
dea31012005-04-17 16:05:31 -0500864 }
865 }
James Smarted957682007-06-17 19:56:37 -0500866 mempool_free(pmb, phba->mbox_mem_pool);
867 return ret;
dea31012005-04-17 16:05:31 -0500868}
869
James Smarte59058c2008-08-24 21:49:00 -0400870/**
James Smart3621a712009-04-06 18:47:14 -0400871 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400872 * @phba: Pointer to HBA context object.
873 * @pring: Pointer to driver SLI ring object.
874 * @piocb: Pointer to the driver iocb object.
875 *
876 * This function is called with hbalock held. The function adds the
877 * new iocb to txcmplq of the given ring. This function always returns
878 * 0. If this function is called for ELS ring, this function checks if
879 * there is a vport associated with the ELS command. This function also
880 * starts els_tmofunc timer if this is an ELS command.
881 **/
dea31012005-04-17 16:05:31 -0500882static int
James Smart2e0fef82007-06-17 19:56:36 -0500883lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
884 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500885{
dea31012005-04-17 16:05:31 -0500886 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400887 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -0500888 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -0400889 if (pring->txcmplq_cnt > pring->txcmplq_max)
890 pring->txcmplq_max = pring->txcmplq_cnt;
891
James Smart92d7f7b2007-06-17 19:56:38 -0500892 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
893 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
894 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
895 if (!piocb->vport)
896 BUG();
897 else
898 mod_timer(&piocb->vport->els_tmofunc,
899 jiffies + HZ * (phba->fc_ratov << 1));
900 }
901
dea31012005-04-17 16:05:31 -0500902
James Smart2e0fef82007-06-17 19:56:36 -0500903 return 0;
dea31012005-04-17 16:05:31 -0500904}
905
James Smarte59058c2008-08-24 21:49:00 -0400906/**
James Smart3621a712009-04-06 18:47:14 -0400907 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400908 * @phba: Pointer to HBA context object.
909 * @pring: Pointer to driver SLI ring object.
910 *
911 * This function is called with hbalock held to get next
912 * iocb in txq of the given ring. If there is any iocb in
913 * the txq, the function returns first iocb in the list after
914 * removing the iocb from the list, else it returns NULL.
915 **/
James Smart2a9bf3d2010-06-07 15:24:45 -0400916struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500917lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500918{
dea31012005-04-17 16:05:31 -0500919 struct lpfc_iocbq *cmd_iocb;
920
James Smart858c9f62007-06-17 19:56:39 -0500921 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
922 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500923 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500924 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500925}
926
James Smarte59058c2008-08-24 21:49:00 -0400927/**
James Smart3621a712009-04-06 18:47:14 -0400928 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400929 * @phba: Pointer to HBA context object.
930 * @pring: Pointer to driver SLI ring object.
931 *
932 * This function is called with hbalock held and the caller must post the
933 * iocb without releasing the lock. If the caller releases the lock,
934 * iocb slot returned by the function is not guaranteed to be available.
935 * The function returns pointer to the next available iocb slot if there
936 * is available slot in the ring, else it returns NULL.
937 * If the get index of the ring is ahead of the put index, the function
938 * will post an error attention event to the worker thread to take the
939 * HBA to offline state.
940 **/
dea31012005-04-17 16:05:31 -0500941static IOCB_t *
942lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
943{
James Smart34b02dc2008-08-24 21:49:55 -0400944 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500945 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500946 if ((pring->next_cmdidx == pring->cmdidx) &&
947 (++pring->next_cmdidx >= max_cmd_idx))
948 pring->next_cmdidx = 0;
949
950 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
951
952 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
953
954 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400956 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200957 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400958 pring->ringno,
dea31012005-04-17 16:05:31 -0500959 pring->local_getidx, max_cmd_idx);
960
James Smart2e0fef82007-06-17 19:56:36 -0500961 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500962 /*
963 * All error attention handlers are posted to
964 * worker thread
965 */
966 phba->work_ha |= HA_ERATT;
967 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500968
James Smart5e9d9b82008-06-14 22:52:53 -0400969 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500970
971 return NULL;
972 }
973
974 if (pring->local_getidx == pring->next_cmdidx)
975 return NULL;
976 }
977
James Smarted957682007-06-17 19:56:37 -0500978 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500979}
980
James Smarte59058c2008-08-24 21:49:00 -0400981/**
James Smart3621a712009-04-06 18:47:14 -0400982 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400983 * @phba: Pointer to HBA context object.
984 * @iocbq: Pointer to driver iocb object.
985 *
986 * This function gets an iotag for the iocb. If there is no unused iotag and
987 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
988 * array and assigns a new iotag.
989 * The function returns the allocated iotag if successful, else returns zero.
990 * Zero is not a valid iotag.
991 * The caller is not required to hold any lock.
992 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500993uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500994lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500995{
James Smart2e0fef82007-06-17 19:56:36 -0500996 struct lpfc_iocbq **new_arr;
997 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500998 size_t new_len;
999 struct lpfc_sli *psli = &phba->sli;
1000 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001001
James Smart2e0fef82007-06-17 19:56:36 -05001002 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001003 iotag = psli->last_iotag;
1004 if(++iotag < psli->iocbq_lookup_len) {
1005 psli->last_iotag = iotag;
1006 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001007 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001008 iocbq->iotag = iotag;
1009 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001010 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001011 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1012 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001013 spin_unlock_irq(&phba->hbalock);
1014 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001015 GFP_KERNEL);
1016 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001017 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001018 old_arr = psli->iocbq_lookup;
1019 if (new_len <= psli->iocbq_lookup_len) {
1020 /* highly unprobable case */
1021 kfree(new_arr);
1022 iotag = psli->last_iotag;
1023 if(++iotag < psli->iocbq_lookup_len) {
1024 psli->last_iotag = iotag;
1025 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001026 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001027 iocbq->iotag = iotag;
1028 return iotag;
1029 }
James Smart2e0fef82007-06-17 19:56:36 -05001030 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001031 return 0;
1032 }
1033 if (psli->iocbq_lookup)
1034 memcpy(new_arr, old_arr,
1035 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001036 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001037 psli->iocbq_lookup = new_arr;
1038 psli->iocbq_lookup_len = new_len;
1039 psli->last_iotag = iotag;
1040 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001041 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001042 iocbq->iotag = iotag;
1043 kfree(old_arr);
1044 return iotag;
1045 }
James Smart8f6d98d2006-08-01 07:34:00 -04001046 } else
James Smart2e0fef82007-06-17 19:56:36 -05001047 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001048
James Smartbc739052010-08-04 16:11:18 -04001049 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001050 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1051 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001052
James Bottomley604a3e32005-10-29 10:28:33 -05001053 return 0;
dea31012005-04-17 16:05:31 -05001054}
1055
James Smarte59058c2008-08-24 21:49:00 -04001056/**
James Smart3621a712009-04-06 18:47:14 -04001057 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001058 * @phba: Pointer to HBA context object.
1059 * @pring: Pointer to driver SLI ring object.
1060 * @iocb: Pointer to iocb slot in the ring.
1061 * @nextiocb: Pointer to driver iocb object which need to be
1062 * posted to firmware.
1063 *
1064 * This function is called with hbalock held to post a new iocb to
1065 * the firmware. This function copies the new iocb to ring iocb slot and
1066 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1067 * a completion call back for this iocb else the function will free the
1068 * iocb object.
1069 **/
dea31012005-04-17 16:05:31 -05001070static void
1071lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1072 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1073{
1074 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001075 * Set up an iotag
dea31012005-04-17 16:05:31 -05001076 */
James Bottomley604a3e32005-10-29 10:28:33 -05001077 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001078
James Smarte2a0a9d2008-12-04 22:40:02 -05001079
James Smarta58cbd52007-08-02 11:09:43 -04001080 if (pring->ringno == LPFC_ELS_RING) {
1081 lpfc_debugfs_slow_ring_trc(phba,
1082 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1083 *(((uint32_t *) &nextiocb->iocb) + 4),
1084 *(((uint32_t *) &nextiocb->iocb) + 6),
1085 *(((uint32_t *) &nextiocb->iocb) + 7));
1086 }
1087
dea31012005-04-17 16:05:31 -05001088 /*
1089 * Issue iocb command to adapter
1090 */
James Smart92d7f7b2007-06-17 19:56:38 -05001091 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001092 wmb();
1093 pring->stats.iocb_cmd++;
1094
1095 /*
1096 * If there is no completion routine to call, we can release the
1097 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1098 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1099 */
1100 if (nextiocb->iocb_cmpl)
1101 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001102 else
James Smart2e0fef82007-06-17 19:56:36 -05001103 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001104
1105 /*
1106 * Let the HBA know what IOCB slot will be the next one the
1107 * driver will put a command into.
1108 */
1109 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001110 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001111}
1112
James Smarte59058c2008-08-24 21:49:00 -04001113/**
James Smart3621a712009-04-06 18:47:14 -04001114 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001115 * @phba: Pointer to HBA context object.
1116 * @pring: Pointer to driver SLI ring object.
1117 *
1118 * The caller is not required to hold any lock for calling this function.
1119 * This function updates the chip attention bits for the ring to inform firmware
1120 * that there are pending work to be done for this ring and requests an
1121 * interrupt when there is space available in the ring. This function is
1122 * called when the driver is unable to post more iocbs to the ring due
1123 * to unavailability of space in the ring.
1124 **/
dea31012005-04-17 16:05:31 -05001125static void
James Smart2e0fef82007-06-17 19:56:36 -05001126lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001127{
1128 int ringno = pring->ringno;
1129
1130 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1131
1132 wmb();
1133
1134 /*
1135 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1136 * The HBA will tell us when an IOCB entry is available.
1137 */
1138 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1139 readl(phba->CAregaddr); /* flush */
1140
1141 pring->stats.iocb_cmd_full++;
1142}
1143
James Smarte59058c2008-08-24 21:49:00 -04001144/**
James Smart3621a712009-04-06 18:47:14 -04001145 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001146 * @phba: Pointer to HBA context object.
1147 * @pring: Pointer to driver SLI ring object.
1148 *
1149 * This function updates the chip attention register bit for the
1150 * given ring to inform HBA that there is more work to be done
1151 * in this ring. The caller is not required to hold any lock.
1152 **/
dea31012005-04-17 16:05:31 -05001153static void
James Smart2e0fef82007-06-17 19:56:36 -05001154lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001155{
1156 int ringno = pring->ringno;
1157
1158 /*
1159 * Tell the HBA that there is work to do in this ring.
1160 */
James Smart34b02dc2008-08-24 21:49:55 -04001161 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1162 wmb();
1163 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1164 readl(phba->CAregaddr); /* flush */
1165 }
dea31012005-04-17 16:05:31 -05001166}
1167
James Smarte59058c2008-08-24 21:49:00 -04001168/**
James Smart3621a712009-04-06 18:47:14 -04001169 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001170 * @phba: Pointer to HBA context object.
1171 * @pring: Pointer to driver SLI ring object.
1172 *
1173 * This function is called with hbalock held to post pending iocbs
1174 * in the txq to the firmware. This function is called when driver
1175 * detects space available in the ring.
1176 **/
dea31012005-04-17 16:05:31 -05001177static void
James Smart2e0fef82007-06-17 19:56:36 -05001178lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001179{
1180 IOCB_t *iocb;
1181 struct lpfc_iocbq *nextiocb;
1182
1183 /*
1184 * Check to see if:
1185 * (a) there is anything on the txq to send
1186 * (b) link is up
1187 * (c) link attention events can be processed (fcp ring only)
1188 * (d) IOCB processing is not blocked by the outstanding mbox command.
1189 */
1190 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001191 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001192 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001193 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001194
1195 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1196 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1197 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1198
1199 if (iocb)
1200 lpfc_sli_update_ring(phba, pring);
1201 else
1202 lpfc_sli_update_full_ring(phba, pring);
1203 }
1204
1205 return;
1206}
1207
James Smarte59058c2008-08-24 21:49:00 -04001208/**
James Smart3621a712009-04-06 18:47:14 -04001209 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001210 * @phba: Pointer to HBA context object.
1211 * @hbqno: HBQ number.
1212 *
1213 * This function is called with hbalock held to get the next
1214 * available slot for the given HBQ. If there is free slot
1215 * available for the HBQ it will return pointer to the next available
1216 * HBQ entry else it will return NULL.
1217 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001218static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001219lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1220{
1221 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1222
1223 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1224 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1225 hbqp->next_hbqPutIdx = 0;
1226
1227 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001228 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001229 uint32_t getidx = le32_to_cpu(raw_index);
1230
1231 hbqp->local_hbqGetIdx = getidx;
1232
1233 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1234 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001235 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001236 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001237 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001238 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001239 hbqp->entry_count);
1240
1241 phba->link_state = LPFC_HBA_ERROR;
1242 return NULL;
1243 }
1244
1245 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1246 return NULL;
1247 }
1248
James Smart51ef4c22007-08-02 11:10:31 -04001249 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1250 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001251}
1252
James Smarte59058c2008-08-24 21:49:00 -04001253/**
James Smart3621a712009-04-06 18:47:14 -04001254 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001255 * @phba: Pointer to HBA context object.
1256 *
1257 * This function is called with no lock held to free all the
1258 * hbq buffers while uninitializing the SLI interface. It also
1259 * frees the HBQ buffers returned by the firmware but not yet
1260 * processed by the upper layers.
1261 **/
James Smarted957682007-06-17 19:56:37 -05001262void
1263lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1264{
James Smart92d7f7b2007-06-17 19:56:38 -05001265 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1266 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001267 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001268 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001269 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001270
James Smart51ef4c22007-08-02 11:10:31 -04001271 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001272 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001273 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001274 for (i = 0; i < hbq_count; ++i) {
1275 list_for_each_entry_safe(dmabuf, next_dmabuf,
1276 &phba->hbqs[i].hbq_buffer_list, list) {
1277 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1278 list_del(&hbq_buf->dbuf.list);
1279 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1280 }
James Smarta8adb832007-10-27 13:37:53 -04001281 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001282 }
James Smart3163f722008-02-08 18:50:25 -05001283 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001284 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1285 list) {
James Smart3163f722008-02-08 18:50:25 -05001286 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1287 list_del(&hbq_buf->dbuf.list);
1288 if (hbq_buf->tag == -1) {
1289 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1290 (phba, hbq_buf);
1291 } else {
1292 hbqno = hbq_buf->tag >> 16;
1293 if (hbqno >= LPFC_MAX_HBQS)
1294 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1295 (phba, hbq_buf);
1296 else
1297 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1298 hbq_buf);
1299 }
1300 }
1301
1302 /* Mark the HBQs not in use */
1303 phba->hbq_in_use = 0;
1304 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001305}
1306
James Smarte59058c2008-08-24 21:49:00 -04001307/**
James Smart3621a712009-04-06 18:47:14 -04001308 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001309 * @phba: Pointer to HBA context object.
1310 * @hbqno: HBQ number.
1311 * @hbq_buf: Pointer to HBQ buffer.
1312 *
1313 * This function is called with the hbalock held to post a
1314 * hbq buffer to the firmware. If the function finds an empty
1315 * slot in the HBQ, it will post the buffer. The function will return
1316 * pointer to the hbq entry if it successfully post the buffer
1317 * else it will return NULL.
1318 **/
James Smart3772a992009-05-22 14:50:54 -04001319static int
James Smarted957682007-06-17 19:56:37 -05001320lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001321 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001322{
James Smart3772a992009-05-22 14:50:54 -04001323 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1324}
1325
1326/**
1327 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1328 * @phba: Pointer to HBA context object.
1329 * @hbqno: HBQ number.
1330 * @hbq_buf: Pointer to HBQ buffer.
1331 *
1332 * This function is called with the hbalock held to post a hbq buffer to the
1333 * firmware. If the function finds an empty slot in the HBQ, it will post the
1334 * buffer and place it on the hbq_buffer_list. The function will return zero if
1335 * it successfully post the buffer else it will return an error.
1336 **/
1337static int
1338lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1339 struct hbq_dmabuf *hbq_buf)
1340{
James Smarted957682007-06-17 19:56:37 -05001341 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001342 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001343
1344 /* Get next HBQ entry slot to use */
1345 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1346 if (hbqe) {
1347 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1348
James Smart92d7f7b2007-06-17 19:56:38 -05001349 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1350 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001351 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001352 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001353 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1354 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1355 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001356 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1357 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001358 /* flush */
James Smarted957682007-06-17 19:56:37 -05001359 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001360 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001361 return 0;
1362 } else
1363 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001364}
1365
James Smart4f774512009-05-22 14:52:35 -04001366/**
1367 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1368 * @phba: Pointer to HBA context object.
1369 * @hbqno: HBQ number.
1370 * @hbq_buf: Pointer to HBQ buffer.
1371 *
1372 * This function is called with the hbalock held to post an RQE to the SLI4
1373 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1374 * the hbq_buffer_list and return zero, otherwise it will return an error.
1375 **/
1376static int
1377lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1378 struct hbq_dmabuf *hbq_buf)
1379{
1380 int rc;
1381 struct lpfc_rqe hrqe;
1382 struct lpfc_rqe drqe;
1383
1384 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1385 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1386 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1387 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1388 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1389 &hrqe, &drqe);
1390 if (rc < 0)
1391 return rc;
1392 hbq_buf->tag = rc;
1393 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1394 return 0;
1395}
1396
James Smarte59058c2008-08-24 21:49:00 -04001397/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001398static struct lpfc_hbq_init lpfc_els_hbq = {
1399 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001400 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001401 .mask_count = 0,
1402 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001403 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001404 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001405 .init_count = 40,
1406 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001407};
James Smarted957682007-06-17 19:56:37 -05001408
James Smarte59058c2008-08-24 21:49:00 -04001409/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001410static struct lpfc_hbq_init lpfc_extra_hbq = {
1411 .rn = 1,
1412 .entry_count = 200,
1413 .mask_count = 0,
1414 .profile = 0,
1415 .ring_mask = (1 << LPFC_EXTRA_RING),
1416 .buffer_count = 0,
1417 .init_count = 0,
1418 .add_count = 5,
1419};
1420
James Smarte59058c2008-08-24 21:49:00 -04001421/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001422struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001423 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001424 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001425};
1426
James Smarte59058c2008-08-24 21:49:00 -04001427/**
James Smart3621a712009-04-06 18:47:14 -04001428 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001429 * @phba: Pointer to HBA context object.
1430 * @hbqno: HBQ number.
1431 * @count: Number of HBQ buffers to be posted.
1432 *
James Smartd7c255b2008-08-24 21:50:00 -04001433 * This function is called with no lock held to post more hbq buffers to the
1434 * given HBQ. The function returns the number of HBQ buffers successfully
1435 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001436 **/
James Smart311464e2007-08-02 11:10:37 -04001437static int
James Smart92d7f7b2007-06-17 19:56:38 -05001438lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1439{
James Smartd7c255b2008-08-24 21:50:00 -04001440 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001441 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001442 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001443 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001444 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001445 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001446
James Smartd7c255b2008-08-24 21:50:00 -04001447 if ((phba->hbqs[hbqno].buffer_count + count) >
1448 lpfc_hbq_defs[hbqno]->entry_count)
1449 count = lpfc_hbq_defs[hbqno]->entry_count -
1450 phba->hbqs[hbqno].buffer_count;
1451 if (!count)
1452 return 0;
1453 /* Allocate HBQ entries */
1454 for (i = 0; i < count; i++) {
1455 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1456 if (!hbq_buffer)
1457 break;
1458 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1459 }
James Smart3163f722008-02-08 18:50:25 -05001460 /* Check whether HBQ is still in use */
1461 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001462 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001463 goto err;
1464 while (!list_empty(&hbq_buf_list)) {
1465 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1466 dbuf.list);
1467 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1468 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001469 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001470 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001471 posted++;
1472 } else
James Smart51ef4c22007-08-02 11:10:31 -04001473 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001474 }
James Smart3163f722008-02-08 18:50:25 -05001475 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001476 return posted;
1477err:
1478 spin_unlock_irqrestore(&phba->hbalock, flags);
1479 while (!list_empty(&hbq_buf_list)) {
1480 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1481 dbuf.list);
1482 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1483 }
James Smart92d7f7b2007-06-17 19:56:38 -05001484 return 0;
James Smarted957682007-06-17 19:56:37 -05001485}
1486
James Smarte59058c2008-08-24 21:49:00 -04001487/**
James Smart3621a712009-04-06 18:47:14 -04001488 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001489 * @phba: Pointer to HBA context object.
1490 * @qno: HBQ number.
1491 *
1492 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001493 * is called with no lock held. The function returns the number of HBQ entries
1494 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001495 **/
James Smarted957682007-06-17 19:56:37 -05001496int
James Smart92d7f7b2007-06-17 19:56:38 -05001497lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001498{
James Smartdef9c7a2009-12-21 17:02:28 -05001499 if (phba->sli_rev == LPFC_SLI_REV4)
1500 return 0;
1501 else
1502 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001504}
1505
James Smarte59058c2008-08-24 21:49:00 -04001506/**
James Smart3621a712009-04-06 18:47:14 -04001507 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001508 * @phba: Pointer to HBA context object.
1509 * @qno: HBQ queue number.
1510 *
1511 * This function is called from SLI initialization code path with
1512 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001513 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001514 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001515static int
James Smart92d7f7b2007-06-17 19:56:38 -05001516lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001517{
James Smartdef9c7a2009-12-21 17:02:28 -05001518 if (phba->sli_rev == LPFC_SLI_REV4)
1519 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1520 lpfc_hbq_defs[qno]->entry_count);
1521 else
1522 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1523 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001524}
1525
James Smarte59058c2008-08-24 21:49:00 -04001526/**
James Smart3772a992009-05-22 14:50:54 -04001527 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1528 * @phba: Pointer to HBA context object.
1529 * @hbqno: HBQ number.
1530 *
1531 * This function removes the first hbq buffer on an hbq list and returns a
1532 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1533 **/
1534static struct hbq_dmabuf *
1535lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1536{
1537 struct lpfc_dmabuf *d_buf;
1538
1539 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1540 if (!d_buf)
1541 return NULL;
1542 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1543}
1544
1545/**
James Smart3621a712009-04-06 18:47:14 -04001546 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001547 * @phba: Pointer to HBA context object.
1548 * @tag: Tag of the hbq buffer.
1549 *
1550 * This function is called with hbalock held. This function searches
1551 * for the hbq buffer associated with the given tag in the hbq buffer
1552 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1553 * it returns NULL.
1554 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001555static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001556lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1557{
James Smart92d7f7b2007-06-17 19:56:38 -05001558 struct lpfc_dmabuf *d_buf;
1559 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001560 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001561
James Smart51ef4c22007-08-02 11:10:31 -04001562 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001563 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001564 return NULL;
1565
James Smart3772a992009-05-22 14:50:54 -04001566 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001567 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001568 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001569 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001570 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001571 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001572 }
1573 }
James Smart3772a992009-05-22 14:50:54 -04001574 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001576 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001577 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001578 return NULL;
James Smarted957682007-06-17 19:56:37 -05001579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001583 * @phba: Pointer to HBA context object.
1584 * @hbq_buffer: Pointer to HBQ buffer.
1585 *
1586 * This function is called with hbalock. This function gives back
1587 * the hbq buffer to firmware. If the HBQ does not have space to
1588 * post the buffer, it will free the buffer.
1589 **/
James Smarted957682007-06-17 19:56:37 -05001590void
James Smart51ef4c22007-08-02 11:10:31 -04001591lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001592{
1593 uint32_t hbqno;
1594
James Smart51ef4c22007-08-02 11:10:31 -04001595 if (hbq_buffer) {
1596 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001597 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001598 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001599 }
1600}
1601
James Smarte59058c2008-08-24 21:49:00 -04001602/**
James Smart3621a712009-04-06 18:47:14 -04001603 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001604 * @mbxCommand: mailbox command code.
1605 *
1606 * This function is called by the mailbox event handler function to verify
1607 * that the completed mailbox command is a legitimate mailbox command. If the
1608 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1609 * and the mailbox event handler will take the HBA offline.
1610 **/
dea31012005-04-17 16:05:31 -05001611static int
1612lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1613{
1614 uint8_t ret;
1615
1616 switch (mbxCommand) {
1617 case MBX_LOAD_SM:
1618 case MBX_READ_NV:
1619 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001620 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001621 case MBX_RUN_BIU_DIAG:
1622 case MBX_INIT_LINK:
1623 case MBX_DOWN_LINK:
1624 case MBX_CONFIG_LINK:
1625 case MBX_CONFIG_RING:
1626 case MBX_RESET_RING:
1627 case MBX_READ_CONFIG:
1628 case MBX_READ_RCONFIG:
1629 case MBX_READ_SPARM:
1630 case MBX_READ_STATUS:
1631 case MBX_READ_RPI:
1632 case MBX_READ_XRI:
1633 case MBX_READ_REV:
1634 case MBX_READ_LNK_STAT:
1635 case MBX_REG_LOGIN:
1636 case MBX_UNREG_LOGIN:
1637 case MBX_READ_LA:
1638 case MBX_CLEAR_LA:
1639 case MBX_DUMP_MEMORY:
1640 case MBX_DUMP_CONTEXT:
1641 case MBX_RUN_DIAGS:
1642 case MBX_RESTART:
1643 case MBX_UPDATE_CFG:
1644 case MBX_DOWN_LOAD:
1645 case MBX_DEL_LD_ENTRY:
1646 case MBX_RUN_PROGRAM:
1647 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001648 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001649 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001650 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001651 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001652 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001653 case MBX_LOAD_AREA:
1654 case MBX_RUN_BIU_DIAG64:
1655 case MBX_CONFIG_PORT:
1656 case MBX_READ_SPARM64:
1657 case MBX_READ_RPI64:
1658 case MBX_REG_LOGIN64:
1659 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001660 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001661 case MBX_SET_DEBUG:
1662 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001663 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001664 case MBX_REG_VPI:
1665 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001666 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001667 case MBX_PORT_CAPABILITIES:
1668 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001669 case MBX_SLI4_CONFIG:
1670 case MBX_SLI4_REQ_FTRS:
1671 case MBX_REG_FCFI:
1672 case MBX_UNREG_FCFI:
1673 case MBX_REG_VFI:
1674 case MBX_UNREG_VFI:
1675 case MBX_INIT_VPI:
1676 case MBX_INIT_VFI:
1677 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04001678 case MBX_READ_EVENT_LOG_STATUS:
1679 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04001680 case MBX_SECURITY_MGMT:
1681 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05001682 ret = mbxCommand;
1683 break;
1684 default:
1685 ret = MBX_SHUTDOWN;
1686 break;
1687 }
James Smart2e0fef82007-06-17 19:56:36 -05001688 return ret;
dea31012005-04-17 16:05:31 -05001689}
James Smarte59058c2008-08-24 21:49:00 -04001690
1691/**
James Smart3621a712009-04-06 18:47:14 -04001692 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001693 * @phba: Pointer to HBA context object.
1694 * @pmboxq: Pointer to mailbox command.
1695 *
1696 * This is completion handler function for mailbox commands issued from
1697 * lpfc_sli_issue_mbox_wait function. This function is called by the
1698 * mailbox event handler function with no lock held. This function
1699 * will wake up thread waiting on the wait queue pointed by context1
1700 * of the mailbox.
1701 **/
James Smart04c68492009-05-22 14:52:52 -04001702void
James Smart2e0fef82007-06-17 19:56:36 -05001703lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001704{
1705 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001706 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001707
1708 /*
1709 * If pdone_q is empty, the driver thread gave up waiting and
1710 * continued running.
1711 */
James Smart7054a602007-04-25 09:52:34 -04001712 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001713 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001714 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1715 if (pdone_q)
1716 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001718 return;
1719}
1720
James Smarte59058c2008-08-24 21:49:00 -04001721
1722/**
James Smart3621a712009-04-06 18:47:14 -04001723 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001724 * @phba: Pointer to HBA context object.
1725 * @pmb: Pointer to mailbox object.
1726 *
1727 * This function is the default mailbox completion handler. It
1728 * frees the memory resources associated with the completed mailbox
1729 * command. If the completed command is a REG_LOGIN mailbox command,
1730 * this function will issue a UREG_LOGIN to re-claim the RPI.
1731 **/
dea31012005-04-17 16:05:31 -05001732void
James Smart2e0fef82007-06-17 19:56:36 -05001733lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001734{
James Smartd439d282010-09-29 11:18:45 -04001735 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001736 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04001737 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04001738 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04001739 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001740 int rc;
1741
dea31012005-04-17 16:05:31 -05001742 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001743
dea31012005-04-17 16:05:31 -05001744 if (mp) {
1745 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1746 kfree(mp);
1747 }
James Smart7054a602007-04-25 09:52:34 -04001748
James Smart04c68492009-05-22 14:52:52 -04001749 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
James Smart5af5eee2010-10-22 11:06:38 -04001750 (phba->sli_rev == LPFC_SLI_REV4) &&
1751 (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
James Smart04c68492009-05-22 14:52:52 -04001752 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1753
James Smart7054a602007-04-25 09:52:34 -04001754 /*
1755 * If a REG_LOGIN succeeded after node is destroyed or node
1756 * is in re-discovery driver need to cleanup the RPI.
1757 */
James Smart2e0fef82007-06-17 19:56:36 -05001758 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001759 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1760 !pmb->u.mb.mbxStatus) {
1761 rpi = pmb->u.mb.un.varWords[0];
1762 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1763 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001764 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001765 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1766 if (rc != MBX_NOT_FINISHED)
1767 return;
1768 }
1769
James Smart695a8142010-01-26 23:08:03 -05001770 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1771 !(phba->pport->load_flag & FC_UNLOADING) &&
1772 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04001773 shost = lpfc_shost_from_vport(vport);
1774 spin_lock_irq(shost->host_lock);
1775 vport->vpi_state |= LPFC_VPI_REGISTERED;
1776 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
1777 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05001778 }
1779
James Smartd439d282010-09-29 11:18:45 -04001780 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
1781 ndlp = (struct lpfc_nodelist *)pmb->context2;
1782 lpfc_nlp_put(ndlp);
1783 pmb->context2 = NULL;
1784 }
1785
James Smartdcf2a4e2010-09-29 11:18:53 -04001786 /* Check security permission status on INIT_LINK mailbox command */
1787 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
1788 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
1789 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1790 "2860 SLI authentication is required "
1791 "for INIT_LINK but has not done yet\n");
1792
James Smart04c68492009-05-22 14:52:52 -04001793 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1794 lpfc_sli4_mbox_cmd_free(phba, pmb);
1795 else
1796 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001797}
1798
James Smarte59058c2008-08-24 21:49:00 -04001799/**
James Smart3621a712009-04-06 18:47:14 -04001800 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001801 * @phba: Pointer to HBA context object.
1802 *
1803 * This function is called with no lock held. This function processes all
1804 * the completed mailbox commands and gives it to upper layers. The interrupt
1805 * service routine processes mailbox completion interrupt and adds completed
1806 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1807 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1808 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1809 * function returns the mailbox commands to the upper layer by calling the
1810 * completion handler function of each mailbox.
1811 **/
dea31012005-04-17 16:05:31 -05001812int
James Smart2e0fef82007-06-17 19:56:36 -05001813lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001814{
James Smart92d7f7b2007-06-17 19:56:38 -05001815 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001816 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001817 int rc;
1818 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001819
1820 phba->sli.slistat.mbox_event++;
1821
James Smart92d7f7b2007-06-17 19:56:38 -05001822 /* Get all completed mailboxe buffers into the cmplq */
1823 spin_lock_irq(&phba->hbalock);
1824 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1825 spin_unlock_irq(&phba->hbalock);
1826
dea31012005-04-17 16:05:31 -05001827 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001828 do {
1829 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1830 if (pmb == NULL)
1831 break;
1832
James Smart04c68492009-05-22 14:52:52 -04001833 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001834
James Smart858c9f62007-06-17 19:56:39 -05001835 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1836 if (pmb->vport) {
1837 lpfc_debugfs_disc_trc(pmb->vport,
1838 LPFC_DISC_TRC_MBOX_VPORT,
1839 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1840 (uint32_t)pmbox->mbxCommand,
1841 pmbox->un.varWords[0],
1842 pmbox->un.varWords[1]);
1843 }
1844 else {
1845 lpfc_debugfs_disc_trc(phba->pport,
1846 LPFC_DISC_TRC_MBOX,
1847 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1848 (uint32_t)pmbox->mbxCommand,
1849 pmbox->un.varWords[0],
1850 pmbox->un.varWords[1]);
1851 }
1852 }
1853
dea31012005-04-17 16:05:31 -05001854 /*
1855 * It is a fatal error if unknown mbox command completion.
1856 */
1857 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1858 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001859 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001861 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001862 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001863 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001864 pmbox->mbxCommand,
1865 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001866 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001867 phba->work_hs = HS_FFER3;
1868 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001869 continue;
dea31012005-04-17 16:05:31 -05001870 }
1871
dea31012005-04-17 16:05:31 -05001872 if (pmbox->mbxStatus) {
1873 phba->sli.slistat.mbox_stat_err++;
1874 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1875 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001876 lpfc_printf_log(phba, KERN_INFO,
1877 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001878 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001879 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001880 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001881 pmb->vport ? pmb->vport->vpi :0,
1882 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001883 lpfc_sli4_mbox_opcode_get(phba,
1884 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001885 pmbox->mbxStatus,
1886 pmbox->un.varWords[0],
1887 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001888 pmbox->mbxStatus = 0;
1889 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001890 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001891 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001892 continue;
dea31012005-04-17 16:05:31 -05001893 }
1894 }
1895
1896 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001897 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001898 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001899 "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 -05001900 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001901 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001902 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001903 pmb->mbox_cmpl,
1904 *((uint32_t *) pmbox),
1905 pmbox->un.varWords[0],
1906 pmbox->un.varWords[1],
1907 pmbox->un.varWords[2],
1908 pmbox->un.varWords[3],
1909 pmbox->un.varWords[4],
1910 pmbox->un.varWords[5],
1911 pmbox->un.varWords[6],
1912 pmbox->un.varWords[7]);
1913
James Smart92d7f7b2007-06-17 19:56:38 -05001914 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001915 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001916 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001917 return 0;
dea31012005-04-17 16:05:31 -05001918}
James Smart92d7f7b2007-06-17 19:56:38 -05001919
James Smarte59058c2008-08-24 21:49:00 -04001920/**
James Smart3621a712009-04-06 18:47:14 -04001921 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001922 * @phba: Pointer to HBA context object.
1923 * @pring: Pointer to driver SLI ring object.
1924 * @tag: buffer tag.
1925 *
1926 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1927 * is set in the tag the buffer is posted for a particular exchange,
1928 * the function will return the buffer without replacing the buffer.
1929 * If the buffer is for unsolicited ELS or CT traffic, this function
1930 * returns the buffer and also posts another buffer to the firmware.
1931 **/
James Smart76bb24e2007-10-27 13:38:00 -04001932static struct lpfc_dmabuf *
1933lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001934 struct lpfc_sli_ring *pring,
1935 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001936{
James Smart9f1e1b52008-12-04 22:39:40 -05001937 struct hbq_dmabuf *hbq_entry;
1938
James Smart76bb24e2007-10-27 13:38:00 -04001939 if (tag & QUE_BUFTAG_BIT)
1940 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001941 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1942 if (!hbq_entry)
1943 return NULL;
1944 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001945}
James Smart57127f12007-10-27 13:37:05 -04001946
James Smart3772a992009-05-22 14:50:54 -04001947/**
1948 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1949 * @phba: Pointer to HBA context object.
1950 * @pring: Pointer to driver SLI ring object.
1951 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1952 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1953 * @fch_type: the type for the first frame of the sequence.
1954 *
1955 * This function is called with no lock held. This function uses the r_ctl and
1956 * type of the received sequence to find the correct callback function to call
1957 * to process the sequence.
1958 **/
1959static int
1960lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1961 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1962 uint32_t fch_type)
1963{
1964 int i;
1965
1966 /* unSolicited Responses */
1967 if (pring->prt[0].profile) {
1968 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1969 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1970 saveq);
1971 return 1;
1972 }
1973 /* We must search, based on rctl / type
1974 for the right routine */
1975 for (i = 0; i < pring->num_mask; i++) {
1976 if ((pring->prt[i].rctl == fch_r_ctl) &&
1977 (pring->prt[i].type == fch_type)) {
1978 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1979 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1980 (phba, pring, saveq);
1981 return 1;
1982 }
1983 }
1984 return 0;
1985}
James Smarte59058c2008-08-24 21:49:00 -04001986
1987/**
James Smart3621a712009-04-06 18:47:14 -04001988 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001989 * @phba: Pointer to HBA context object.
1990 * @pring: Pointer to driver SLI ring object.
1991 * @saveq: Pointer to the unsolicited iocb.
1992 *
1993 * This function is called with no lock held by the ring event handler
1994 * when there is an unsolicited iocb posted to the response ring by the
1995 * firmware. This function gets the buffer associated with the iocbs
1996 * and calls the event handler for the ring. This function handles both
1997 * qring buffers and hbq buffers.
1998 * When the function returns 1 the caller can free the iocb object otherwise
1999 * upper layer functions will free the iocb objects.
2000 **/
dea31012005-04-17 16:05:31 -05002001static int
2002lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2003 struct lpfc_iocbq *saveq)
2004{
2005 IOCB_t * irsp;
2006 WORD5 * w5p;
2007 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002008 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002009 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002010 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002011
2012 match = 0;
2013 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002014
2015 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2016 if (pring->lpfc_sli_rcv_async_status)
2017 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2018 else
2019 lpfc_printf_log(phba,
2020 KERN_WARNING,
2021 LOG_SLI,
2022 "0316 Ring %d handler: unexpected "
2023 "ASYNC_STATUS iocb received evt_code "
2024 "0x%x\n",
2025 pring->ringno,
2026 irsp->un.asyncstat.evt_code);
2027 return 1;
2028 }
2029
James Smart3163f722008-02-08 18:50:25 -05002030 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2031 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2032 if (irsp->ulpBdeCount > 0) {
2033 dmzbuf = lpfc_sli_get_buff(phba, pring,
2034 irsp->un.ulpWord[3]);
2035 lpfc_in_buf_free(phba, dmzbuf);
2036 }
2037
2038 if (irsp->ulpBdeCount > 1) {
2039 dmzbuf = lpfc_sli_get_buff(phba, pring,
2040 irsp->unsli3.sli3Words[3]);
2041 lpfc_in_buf_free(phba, dmzbuf);
2042 }
2043
2044 if (irsp->ulpBdeCount > 2) {
2045 dmzbuf = lpfc_sli_get_buff(phba, pring,
2046 irsp->unsli3.sli3Words[7]);
2047 lpfc_in_buf_free(phba, dmzbuf);
2048 }
2049
2050 return 1;
2051 }
2052
James Smart92d7f7b2007-06-17 19:56:38 -05002053 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002054 if (irsp->ulpBdeCount != 0) {
2055 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002056 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002057 if (!saveq->context2)
2058 lpfc_printf_log(phba,
2059 KERN_ERR,
2060 LOG_SLI,
2061 "0341 Ring %d Cannot find buffer for "
2062 "an unsolicited iocb. tag 0x%x\n",
2063 pring->ringno,
2064 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002065 }
2066 if (irsp->ulpBdeCount == 2) {
2067 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002068 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002069 if (!saveq->context3)
2070 lpfc_printf_log(phba,
2071 KERN_ERR,
2072 LOG_SLI,
2073 "0342 Ring %d Cannot find buffer for an"
2074 " unsolicited iocb. tag 0x%x\n",
2075 pring->ringno,
2076 irsp->unsli3.sli3Words[7]);
2077 }
2078 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002079 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002080 if (irsp->ulpBdeCount != 0) {
2081 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2082 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002083 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002084 lpfc_printf_log(phba,
2085 KERN_ERR,
2086 LOG_SLI,
2087 "0343 Ring %d Cannot find "
2088 "buffer for an unsolicited iocb"
2089 ". tag 0x%x\n", pring->ringno,
2090 irsp->un.ulpWord[3]);
2091 }
2092 if (irsp->ulpBdeCount == 2) {
2093 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2094 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002095 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002096 lpfc_printf_log(phba,
2097 KERN_ERR,
2098 LOG_SLI,
2099 "0344 Ring %d Cannot find "
2100 "buffer for an unsolicited "
2101 "iocb. tag 0x%x\n",
2102 pring->ringno,
2103 irsp->unsli3.sli3Words[7]);
2104 }
2105 }
James Smart92d7f7b2007-06-17 19:56:38 -05002106 }
James Smart9c2face2008-01-11 01:53:18 -05002107 if (irsp->ulpBdeCount != 0 &&
2108 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2109 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2110 int found = 0;
2111
2112 /* search continue save q for same XRI */
2113 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2114 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2115 list_add_tail(&saveq->list, &iocbq->list);
2116 found = 1;
2117 break;
2118 }
2119 }
2120 if (!found)
2121 list_add_tail(&saveq->clist,
2122 &pring->iocb_continue_saveq);
2123 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2124 list_del_init(&iocbq->clist);
2125 saveq = iocbq;
2126 irsp = &(saveq->iocb);
2127 } else
2128 return 0;
2129 }
2130 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2131 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2132 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002133 Rctl = FC_RCTL_ELS_REQ;
2134 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002135 } else {
2136 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2137 Rctl = w5p->hcsw.Rctl;
2138 Type = w5p->hcsw.Type;
2139
2140 /* Firmware Workaround */
2141 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2142 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2143 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002144 Rctl = FC_RCTL_ELS_REQ;
2145 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002146 w5p->hcsw.Rctl = Rctl;
2147 w5p->hcsw.Type = Type;
2148 }
2149 }
James Smart92d7f7b2007-06-17 19:56:38 -05002150
James Smart3772a992009-05-22 14:50:54 -04002151 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002153 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002154 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002155 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002156
James Smart92d7f7b2007-06-17 19:56:38 -05002157 return 1;
dea31012005-04-17 16:05:31 -05002158}
2159
James Smarte59058c2008-08-24 21:49:00 -04002160/**
James Smart3621a712009-04-06 18:47:14 -04002161 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002162 * @phba: Pointer to HBA context object.
2163 * @pring: Pointer to driver SLI ring object.
2164 * @prspiocb: Pointer to response iocb object.
2165 *
2166 * This function looks up the iocb_lookup table to get the command iocb
2167 * corresponding to the given response iocb using the iotag of the
2168 * response iocb. This function is called with the hbalock held.
2169 * This function returns the command iocb object if it finds the command
2170 * iocb else returns NULL.
2171 **/
dea31012005-04-17 16:05:31 -05002172static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002173lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2174 struct lpfc_sli_ring *pring,
2175 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002176{
dea31012005-04-17 16:05:31 -05002177 struct lpfc_iocbq *cmd_iocb = NULL;
2178 uint16_t iotag;
2179
James Bottomley604a3e32005-10-29 10:28:33 -05002180 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002181
James Bottomley604a3e32005-10-29 10:28:33 -05002182 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2183 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002184 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002185 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2186 pring->txcmplq_cnt--;
2187 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2188 }
James Bottomley604a3e32005-10-29 10:28:33 -05002189 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002190 }
2191
dea31012005-04-17 16:05:31 -05002192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002193 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002194 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002195 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002196 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002197 return NULL;
2198}
2199
James Smarte59058c2008-08-24 21:49:00 -04002200/**
James Smart3772a992009-05-22 14:50:54 -04002201 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2202 * @phba: Pointer to HBA context object.
2203 * @pring: Pointer to driver SLI ring object.
2204 * @iotag: IOCB tag.
2205 *
2206 * This function looks up the iocb_lookup table to get the command iocb
2207 * corresponding to the given iotag. This function is called with the
2208 * hbalock held.
2209 * This function returns the command iocb object if it finds the command
2210 * iocb else returns NULL.
2211 **/
2212static struct lpfc_iocbq *
2213lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2214 struct lpfc_sli_ring *pring, uint16_t iotag)
2215{
2216 struct lpfc_iocbq *cmd_iocb;
2217
2218 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2219 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2220 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002221 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2222 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2223 pring->txcmplq_cnt--;
2224 }
James Smart3772a992009-05-22 14:50:54 -04002225 return cmd_iocb;
2226 }
2227
2228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2229 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2230 iotag, phba->sli.last_iotag);
2231 return NULL;
2232}
2233
2234/**
James Smart3621a712009-04-06 18:47:14 -04002235 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002236 * @phba: Pointer to HBA context object.
2237 * @pring: Pointer to driver SLI ring object.
2238 * @saveq: Pointer to the response iocb to be processed.
2239 *
2240 * This function is called by the ring event handler for non-fcp
2241 * rings when there is a new response iocb in the response ring.
2242 * The caller is not required to hold any locks. This function
2243 * gets the command iocb associated with the response iocb and
2244 * calls the completion handler for the command iocb. If there
2245 * is no completion handler, the function will free the resources
2246 * associated with command iocb. If the response iocb is for
2247 * an already aborted command iocb, the status of the completion
2248 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2249 * This function always returns 1.
2250 **/
dea31012005-04-17 16:05:31 -05002251static int
James Smart2e0fef82007-06-17 19:56:36 -05002252lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002253 struct lpfc_iocbq *saveq)
2254{
James Smart2e0fef82007-06-17 19:56:36 -05002255 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002256 int rc = 1;
2257 unsigned long iflag;
2258
2259 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002260 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002261 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002262 spin_unlock_irqrestore(&phba->hbalock, iflag);
2263
dea31012005-04-17 16:05:31 -05002264 if (cmdiocbp) {
2265 if (cmdiocbp->iocb_cmpl) {
2266 /*
James Smartea2151b2008-09-07 11:52:10 -04002267 * If an ELS command failed send an event to mgmt
2268 * application.
2269 */
2270 if (saveq->iocb.ulpStatus &&
2271 (pring->ringno == LPFC_ELS_RING) &&
2272 (cmdiocbp->iocb.ulpCommand ==
2273 CMD_ELS_REQUEST64_CR))
2274 lpfc_send_els_failure_event(phba,
2275 cmdiocbp, saveq);
2276
2277 /*
dea31012005-04-17 16:05:31 -05002278 * Post all ELS completions to the worker thread.
2279 * All other are passed to the completion callback.
2280 */
2281 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002282 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2283 (cmdiocbp->iocb_flag &
2284 LPFC_DRIVER_ABORTED)) {
2285 spin_lock_irqsave(&phba->hbalock,
2286 iflag);
James Smart07951072007-04-25 09:51:38 -04002287 cmdiocbp->iocb_flag &=
2288 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002289 spin_unlock_irqrestore(&phba->hbalock,
2290 iflag);
James Smart07951072007-04-25 09:51:38 -04002291 saveq->iocb.ulpStatus =
2292 IOSTAT_LOCAL_REJECT;
2293 saveq->iocb.un.ulpWord[4] =
2294 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002295
2296 /* Firmware could still be in progress
2297 * of DMAing payload, so don't free data
2298 * buffer till after a hbeat.
2299 */
James Smart341af102010-01-26 23:07:37 -05002300 spin_lock_irqsave(&phba->hbalock,
2301 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002302 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002303 spin_unlock_irqrestore(&phba->hbalock,
2304 iflag);
2305 }
James Smart0f65ff62010-02-26 14:14:23 -05002306 if (phba->sli_rev == LPFC_SLI_REV4) {
2307 if (saveq->iocb_flag &
2308 LPFC_EXCHANGE_BUSY) {
2309 /* Set cmdiocb flag for the
2310 * exchange busy so sgl (xri)
2311 * will not be released until
2312 * the abort xri is received
2313 * from hba.
2314 */
2315 spin_lock_irqsave(
2316 &phba->hbalock, iflag);
2317 cmdiocbp->iocb_flag |=
2318 LPFC_EXCHANGE_BUSY;
2319 spin_unlock_irqrestore(
2320 &phba->hbalock, iflag);
2321 }
2322 if (cmdiocbp->iocb_flag &
2323 LPFC_DRIVER_ABORTED) {
2324 /*
2325 * Clear LPFC_DRIVER_ABORTED
2326 * bit in case it was driver
2327 * initiated abort.
2328 */
2329 spin_lock_irqsave(
2330 &phba->hbalock, iflag);
2331 cmdiocbp->iocb_flag &=
2332 ~LPFC_DRIVER_ABORTED;
2333 spin_unlock_irqrestore(
2334 &phba->hbalock, iflag);
2335 cmdiocbp->iocb.ulpStatus =
2336 IOSTAT_LOCAL_REJECT;
2337 cmdiocbp->iocb.un.ulpWord[4] =
2338 IOERR_ABORT_REQUESTED;
2339 /*
2340 * For SLI4, irsiocb contains
2341 * NO_XRI in sli_xritag, it
2342 * shall not affect releasing
2343 * sgl (xri) process.
2344 */
2345 saveq->iocb.ulpStatus =
2346 IOSTAT_LOCAL_REJECT;
2347 saveq->iocb.un.ulpWord[4] =
2348 IOERR_SLI_ABORTED;
2349 spin_lock_irqsave(
2350 &phba->hbalock, iflag);
2351 saveq->iocb_flag |=
2352 LPFC_DELAY_MEM_FREE;
2353 spin_unlock_irqrestore(
2354 &phba->hbalock, iflag);
2355 }
James Smart07951072007-04-25 09:51:38 -04002356 }
dea31012005-04-17 16:05:31 -05002357 }
James Smart2e0fef82007-06-17 19:56:36 -05002358 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002359 } else
2360 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002361 } else {
2362 /*
2363 * Unknown initiating command based on the response iotag.
2364 * This could be the case on the ELS ring because of
2365 * lpfc_els_abort().
2366 */
2367 if (pring->ringno != LPFC_ELS_RING) {
2368 /*
2369 * Ring <ringno> handler: unexpected completion IoTag
2370 * <IoTag>
2371 */
James Smarta257bf92009-04-06 18:48:10 -04002372 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002373 "0322 Ring %d handler: "
2374 "unexpected completion IoTag x%x "
2375 "Data: x%x x%x x%x x%x\n",
2376 pring->ringno,
2377 saveq->iocb.ulpIoTag,
2378 saveq->iocb.ulpStatus,
2379 saveq->iocb.un.ulpWord[4],
2380 saveq->iocb.ulpCommand,
2381 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002382 }
2383 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002384
dea31012005-04-17 16:05:31 -05002385 return rc;
2386}
2387
James Smarte59058c2008-08-24 21:49:00 -04002388/**
James Smart3621a712009-04-06 18:47:14 -04002389 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002390 * @phba: Pointer to HBA context object.
2391 * @pring: Pointer to driver SLI ring object.
2392 *
2393 * This function is called from the iocb ring event handlers when
2394 * put pointer is ahead of the get pointer for a ring. This function signal
2395 * an error attention condition to the worker thread and the worker
2396 * thread will transition the HBA to offline state.
2397 **/
James Smart2e0fef82007-06-17 19:56:36 -05002398static void
2399lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002400{
James Smart34b02dc2008-08-24 21:49:55 -04002401 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002402 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002403 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002404 * rsp ring <portRspMax>
2405 */
2406 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002407 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002408 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002409 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002410 pring->numRiocb);
2411
James Smart2e0fef82007-06-17 19:56:36 -05002412 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002413
2414 /*
2415 * All error attention handlers are posted to
2416 * worker thread
2417 */
2418 phba->work_ha |= HA_ERATT;
2419 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002420
James Smart5e9d9b82008-06-14 22:52:53 -04002421 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002422
2423 return;
2424}
2425
James Smarte59058c2008-08-24 21:49:00 -04002426/**
James Smart3621a712009-04-06 18:47:14 -04002427 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002428 * @ptr: Pointer to address of HBA context object.
2429 *
2430 * This function is invoked by the Error Attention polling timer when the
2431 * timer times out. It will check the SLI Error Attention register for
2432 * possible attention events. If so, it will post an Error Attention event
2433 * and wake up worker thread to process it. Otherwise, it will set up the
2434 * Error Attention polling timer for the next poll.
2435 **/
2436void lpfc_poll_eratt(unsigned long ptr)
2437{
2438 struct lpfc_hba *phba;
2439 uint32_t eratt = 0;
2440
2441 phba = (struct lpfc_hba *)ptr;
2442
2443 /* Check chip HA register for error event */
2444 eratt = lpfc_sli_check_eratt(phba);
2445
2446 if (eratt)
2447 /* Tell the worker thread there is work to do */
2448 lpfc_worker_wake_up(phba);
2449 else
2450 /* Restart the timer for next eratt poll */
2451 mod_timer(&phba->eratt_poll, jiffies +
2452 HZ * LPFC_ERATT_POLL_INTERVAL);
2453 return;
2454}
2455
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002456
James Smarte59058c2008-08-24 21:49:00 -04002457/**
James Smart3621a712009-04-06 18:47:14 -04002458 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002459 * @phba: Pointer to HBA context object.
2460 * @pring: Pointer to driver SLI ring object.
2461 * @mask: Host attention register mask for this ring.
2462 *
2463 * This function is called from the interrupt context when there is a ring
2464 * event for the fcp ring. The caller does not hold any lock.
2465 * The function processes each response iocb in the response ring until it
2466 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2467 * LE bit set. The function will call the completion handler of the command iocb
2468 * if the response iocb indicates a completion for a command iocb or it is
2469 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2470 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002471 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002472 * to check it explicitly.
2473 */
2474int
James Smart2e0fef82007-06-17 19:56:36 -05002475lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2476 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002477{
James Smart34b02dc2008-08-24 21:49:55 -04002478 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002479 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002480 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002481 struct lpfc_iocbq *cmdiocbq = NULL;
2482 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002483 uint32_t status;
2484 uint32_t portRspPut, portRspMax;
2485 int rc = 1;
2486 lpfc_iocb_type type;
2487 unsigned long iflag;
2488 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002489
James Smart2e0fef82007-06-17 19:56:36 -05002490 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002491 pring->stats.iocb_event++;
2492
dea31012005-04-17 16:05:31 -05002493 /*
2494 * The next available response entry should never exceed the maximum
2495 * entries. If it does, treat it as an adapter hardware error.
2496 */
2497 portRspMax = pring->numRiocb;
2498 portRspPut = le32_to_cpu(pgp->rspPutInx);
2499 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002500 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002501 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002502 return 1;
2503 }
James Smart45ed1192009-10-02 15:17:02 -04002504 if (phba->fcp_ring_in_use) {
2505 spin_unlock_irqrestore(&phba->hbalock, iflag);
2506 return 1;
2507 } else
2508 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002509
2510 rmb();
2511 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002512 /*
2513 * Fetch an entry off the ring and copy it into a local data
2514 * structure. The copy involves a byte-swap since the
2515 * network byte order and pci byte orders are different.
2516 */
James Smarted957682007-06-17 19:56:37 -05002517 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002518 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002519
2520 if (++pring->rspidx >= portRspMax)
2521 pring->rspidx = 0;
2522
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002523 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2524 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002525 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002526 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002527 irsp = &rspiocbq.iocb;
2528
dea31012005-04-17 16:05:31 -05002529 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2530 pring->stats.iocb_rsp++;
2531 rsp_cmpl++;
2532
2533 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002534 /*
2535 * If resource errors reported from HBA, reduce
2536 * queuedepths of the SCSI device.
2537 */
2538 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2539 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2540 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002541 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002542 spin_lock_irqsave(&phba->hbalock, iflag);
2543 }
2544
dea31012005-04-17 16:05:31 -05002545 /* Rsp ring <ringno> error: IOCB */
2546 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002547 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002548 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002549 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002550 irsp->un.ulpWord[0],
2551 irsp->un.ulpWord[1],
2552 irsp->un.ulpWord[2],
2553 irsp->un.ulpWord[3],
2554 irsp->un.ulpWord[4],
2555 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002556 *(uint32_t *)&irsp->un1,
2557 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002558 }
2559
2560 switch (type) {
2561 case LPFC_ABORT_IOCB:
2562 case LPFC_SOL_IOCB:
2563 /*
2564 * Idle exchange closed via ABTS from port. No iocb
2565 * resources need to be recovered.
2566 */
2567 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002569 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002570 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002571 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002572 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002573 break;
2574 }
2575
James Bottomley604a3e32005-10-29 10:28:33 -05002576 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2577 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002578 if (unlikely(!cmdiocbq))
2579 break;
2580 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2581 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2582 if (cmdiocbq->iocb_cmpl) {
2583 spin_unlock_irqrestore(&phba->hbalock, iflag);
2584 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2585 &rspiocbq);
2586 spin_lock_irqsave(&phba->hbalock, iflag);
2587 }
dea31012005-04-17 16:05:31 -05002588 break;
James Smarta4bc3372006-12-02 13:34:16 -05002589 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002590 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002591 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002592 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002593 break;
dea31012005-04-17 16:05:31 -05002594 default:
2595 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2596 char adaptermsg[LPFC_MAX_ADPTMSG];
2597 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2598 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2599 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002600 dev_warn(&((phba->pcidev)->dev),
2601 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002602 phba->brd_no, adaptermsg);
2603 } else {
2604 /* Unknown IOCB command */
2605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002606 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002607 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002608 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002609 irsp->ulpStatus,
2610 irsp->ulpIoTag,
2611 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002612 }
2613 break;
2614 }
2615
2616 /*
2617 * The response IOCB has been processed. Update the ring
2618 * pointer in SLIM. If the port response put pointer has not
2619 * been updated, sync the pgp->rspPutInx and fetch the new port
2620 * response put pointer.
2621 */
James Smarted957682007-06-17 19:56:37 -05002622 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002623
2624 if (pring->rspidx == portRspPut)
2625 portRspPut = le32_to_cpu(pgp->rspPutInx);
2626 }
2627
2628 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2629 pring->stats.iocb_rsp_full++;
2630 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2631 writel(status, phba->CAregaddr);
2632 readl(phba->CAregaddr);
2633 }
2634 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2635 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2636 pring->stats.iocb_cmd_empty++;
2637
2638 /* Force update of the local copy of cmdGetInx */
2639 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2640 lpfc_sli_resume_iocb(phba, pring);
2641
2642 if ((pring->lpfc_sli_cmd_available))
2643 (pring->lpfc_sli_cmd_available) (phba, pring);
2644
2645 }
2646
James Smart45ed1192009-10-02 15:17:02 -04002647 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002648 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002649 return rc;
2650}
2651
James Smarte59058c2008-08-24 21:49:00 -04002652/**
James Smart3772a992009-05-22 14:50:54 -04002653 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2654 * @phba: Pointer to HBA context object.
2655 * @pring: Pointer to driver SLI ring object.
2656 * @rspiocbp: Pointer to driver response IOCB object.
2657 *
2658 * This function is called from the worker thread when there is a slow-path
2659 * response IOCB to process. This function chains all the response iocbs until
2660 * seeing the iocb with the LE bit set. The function will call
2661 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2662 * completion of a command iocb. The function will call the
2663 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2664 * The function frees the resources or calls the completion handler if this
2665 * iocb is an abort completion. The function returns NULL when the response
2666 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2667 * this function shall chain the iocb on to the iocb_continueq and return the
2668 * response iocb passed in.
2669 **/
2670static struct lpfc_iocbq *
2671lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2672 struct lpfc_iocbq *rspiocbp)
2673{
2674 struct lpfc_iocbq *saveq;
2675 struct lpfc_iocbq *cmdiocbp;
2676 struct lpfc_iocbq *next_iocb;
2677 IOCB_t *irsp = NULL;
2678 uint32_t free_saveq;
2679 uint8_t iocb_cmd_type;
2680 lpfc_iocb_type type;
2681 unsigned long iflag;
2682 int rc;
2683
2684 spin_lock_irqsave(&phba->hbalock, iflag);
2685 /* First add the response iocb to the countinueq list */
2686 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2687 pring->iocb_continueq_cnt++;
2688
2689 /* Now, determine whetehr the list is completed for processing */
2690 irsp = &rspiocbp->iocb;
2691 if (irsp->ulpLe) {
2692 /*
2693 * By default, the driver expects to free all resources
2694 * associated with this iocb completion.
2695 */
2696 free_saveq = 1;
2697 saveq = list_get_first(&pring->iocb_continueq,
2698 struct lpfc_iocbq, list);
2699 irsp = &(saveq->iocb);
2700 list_del_init(&pring->iocb_continueq);
2701 pring->iocb_continueq_cnt = 0;
2702
2703 pring->stats.iocb_rsp++;
2704
2705 /*
2706 * If resource errors reported from HBA, reduce
2707 * queuedepths of the SCSI device.
2708 */
2709 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2710 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2711 spin_unlock_irqrestore(&phba->hbalock, iflag);
2712 phba->lpfc_rampdown_queue_depth(phba);
2713 spin_lock_irqsave(&phba->hbalock, iflag);
2714 }
2715
2716 if (irsp->ulpStatus) {
2717 /* Rsp ring <ringno> error: IOCB */
2718 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2719 "0328 Rsp Ring %d error: "
2720 "IOCB Data: "
2721 "x%x x%x x%x x%x "
2722 "x%x x%x x%x x%x "
2723 "x%x x%x x%x x%x "
2724 "x%x x%x x%x x%x\n",
2725 pring->ringno,
2726 irsp->un.ulpWord[0],
2727 irsp->un.ulpWord[1],
2728 irsp->un.ulpWord[2],
2729 irsp->un.ulpWord[3],
2730 irsp->un.ulpWord[4],
2731 irsp->un.ulpWord[5],
2732 *(((uint32_t *) irsp) + 6),
2733 *(((uint32_t *) irsp) + 7),
2734 *(((uint32_t *) irsp) + 8),
2735 *(((uint32_t *) irsp) + 9),
2736 *(((uint32_t *) irsp) + 10),
2737 *(((uint32_t *) irsp) + 11),
2738 *(((uint32_t *) irsp) + 12),
2739 *(((uint32_t *) irsp) + 13),
2740 *(((uint32_t *) irsp) + 14),
2741 *(((uint32_t *) irsp) + 15));
2742 }
2743
2744 /*
2745 * Fetch the IOCB command type and call the correct completion
2746 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2747 * get freed back to the lpfc_iocb_list by the discovery
2748 * kernel thread.
2749 */
2750 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2751 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2752 switch (type) {
2753 case LPFC_SOL_IOCB:
2754 spin_unlock_irqrestore(&phba->hbalock, iflag);
2755 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2756 spin_lock_irqsave(&phba->hbalock, iflag);
2757 break;
2758
2759 case LPFC_UNSOL_IOCB:
2760 spin_unlock_irqrestore(&phba->hbalock, iflag);
2761 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2762 spin_lock_irqsave(&phba->hbalock, iflag);
2763 if (!rc)
2764 free_saveq = 0;
2765 break;
2766
2767 case LPFC_ABORT_IOCB:
2768 cmdiocbp = NULL;
2769 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2770 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2771 saveq);
2772 if (cmdiocbp) {
2773 /* Call the specified completion routine */
2774 if (cmdiocbp->iocb_cmpl) {
2775 spin_unlock_irqrestore(&phba->hbalock,
2776 iflag);
2777 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2778 saveq);
2779 spin_lock_irqsave(&phba->hbalock,
2780 iflag);
2781 } else
2782 __lpfc_sli_release_iocbq(phba,
2783 cmdiocbp);
2784 }
2785 break;
2786
2787 case LPFC_UNKNOWN_IOCB:
2788 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2789 char adaptermsg[LPFC_MAX_ADPTMSG];
2790 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2791 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2792 MAX_MSG_DATA);
2793 dev_warn(&((phba->pcidev)->dev),
2794 "lpfc%d: %s\n",
2795 phba->brd_no, adaptermsg);
2796 } else {
2797 /* Unknown IOCB command */
2798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2799 "0335 Unknown IOCB "
2800 "command Data: x%x "
2801 "x%x x%x x%x\n",
2802 irsp->ulpCommand,
2803 irsp->ulpStatus,
2804 irsp->ulpIoTag,
2805 irsp->ulpContext);
2806 }
2807 break;
2808 }
2809
2810 if (free_saveq) {
2811 list_for_each_entry_safe(rspiocbp, next_iocb,
2812 &saveq->list, list) {
2813 list_del(&rspiocbp->list);
2814 __lpfc_sli_release_iocbq(phba, rspiocbp);
2815 }
2816 __lpfc_sli_release_iocbq(phba, saveq);
2817 }
2818 rspiocbp = NULL;
2819 }
2820 spin_unlock_irqrestore(&phba->hbalock, iflag);
2821 return rspiocbp;
2822}
2823
2824/**
2825 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002826 * @phba: Pointer to HBA context object.
2827 * @pring: Pointer to driver SLI ring object.
2828 * @mask: Host attention register mask for this ring.
2829 *
James Smart3772a992009-05-22 14:50:54 -04002830 * This routine wraps the actual slow_ring event process routine from the
2831 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002832 **/
James Smart3772a992009-05-22 14:50:54 -04002833void
James Smart2e0fef82007-06-17 19:56:36 -05002834lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2835 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002836{
James Smart3772a992009-05-22 14:50:54 -04002837 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2838}
2839
2840/**
2841 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2842 * @phba: Pointer to HBA context object.
2843 * @pring: Pointer to driver SLI ring object.
2844 * @mask: Host attention register mask for this ring.
2845 *
2846 * This function is called from the worker thread when there is a ring event
2847 * for non-fcp rings. The caller does not hold any lock. The function will
2848 * remove each response iocb in the response ring and calls the handle
2849 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2850 **/
2851static void
2852lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2853 struct lpfc_sli_ring *pring, uint32_t mask)
2854{
James Smart34b02dc2008-08-24 21:49:55 -04002855 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002856 IOCB_t *entry;
2857 IOCB_t *irsp = NULL;
2858 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002859 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002860 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002861 uint32_t status;
dea31012005-04-17 16:05:31 -05002862
James Smart34b02dc2008-08-24 21:49:55 -04002863 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002864 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002865 pring->stats.iocb_event++;
2866
dea31012005-04-17 16:05:31 -05002867 /*
2868 * The next available response entry should never exceed the maximum
2869 * entries. If it does, treat it as an adapter hardware error.
2870 */
2871 portRspMax = pring->numRiocb;
2872 portRspPut = le32_to_cpu(pgp->rspPutInx);
2873 if (portRspPut >= portRspMax) {
2874 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002875 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002876 * rsp ring <portRspMax>
2877 */
James Smarted957682007-06-17 19:56:37 -05002878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002879 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002880 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002881 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002882
James Smart2e0fef82007-06-17 19:56:36 -05002883 phba->link_state = LPFC_HBA_ERROR;
2884 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002885
2886 phba->work_hs = HS_FFER3;
2887 lpfc_handle_eratt(phba);
2888
James Smart3772a992009-05-22 14:50:54 -04002889 return;
dea31012005-04-17 16:05:31 -05002890 }
2891
2892 rmb();
dea31012005-04-17 16:05:31 -05002893 while (pring->rspidx != portRspPut) {
2894 /*
2895 * Build a completion list and call the appropriate handler.
2896 * The process is to get the next available response iocb, get
2897 * a free iocb from the list, copy the response data into the
2898 * free iocb, insert to the continuation list, and update the
2899 * next response index to slim. This process makes response
2900 * iocb's in the ring available to DMA as fast as possible but
2901 * pays a penalty for a copy operation. Since the iocb is
2902 * only 32 bytes, this penalty is considered small relative to
2903 * the PCI reads for register values and a slim write. When
2904 * the ulpLe field is set, the entire Command has been
2905 * received.
2906 */
James Smarted957682007-06-17 19:56:37 -05002907 entry = lpfc_resp_iocb(phba, pring);
2908
James Smart858c9f62007-06-17 19:56:39 -05002909 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002910 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002911 if (rspiocbp == NULL) {
2912 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002913 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002914 break;
2915 }
2916
James Smarted957682007-06-17 19:56:37 -05002917 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2918 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002919 irsp = &rspiocbp->iocb;
2920
2921 if (++pring->rspidx >= portRspMax)
2922 pring->rspidx = 0;
2923
James Smarta58cbd52007-08-02 11:09:43 -04002924 if (pring->ringno == LPFC_ELS_RING) {
2925 lpfc_debugfs_slow_ring_trc(phba,
2926 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2927 *(((uint32_t *) irsp) + 4),
2928 *(((uint32_t *) irsp) + 6),
2929 *(((uint32_t *) irsp) + 7));
2930 }
2931
James Smarted957682007-06-17 19:56:37 -05002932 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002933
James Smart3772a992009-05-22 14:50:54 -04002934 spin_unlock_irqrestore(&phba->hbalock, iflag);
2935 /* Handle the response IOCB */
2936 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2937 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002938
2939 /*
2940 * If the port response put pointer has not been updated, sync
2941 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2942 * response put pointer.
2943 */
2944 if (pring->rspidx == portRspPut) {
2945 portRspPut = le32_to_cpu(pgp->rspPutInx);
2946 }
2947 } /* while (pring->rspidx != portRspPut) */
2948
James Smart92d7f7b2007-06-17 19:56:38 -05002949 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002950 /* At least one response entry has been freed */
2951 pring->stats.iocb_rsp_full++;
2952 /* SET RxRE_RSP in Chip Att register */
2953 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2954 writel(status, phba->CAregaddr);
2955 readl(phba->CAregaddr); /* flush */
2956 }
2957 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2958 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2959 pring->stats.iocb_cmd_empty++;
2960
2961 /* Force update of the local copy of cmdGetInx */
2962 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2963 lpfc_sli_resume_iocb(phba, pring);
2964
2965 if ((pring->lpfc_sli_cmd_available))
2966 (pring->lpfc_sli_cmd_available) (phba, pring);
2967
2968 }
2969
James Smart2e0fef82007-06-17 19:56:36 -05002970 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002971 return;
dea31012005-04-17 16:05:31 -05002972}
2973
James Smarte59058c2008-08-24 21:49:00 -04002974/**
James Smart4f774512009-05-22 14:52:35 -04002975 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2976 * @phba: Pointer to HBA context object.
2977 * @pring: Pointer to driver SLI ring object.
2978 * @mask: Host attention register mask for this ring.
2979 *
2980 * This function is called from the worker thread when there is a pending
2981 * ELS response iocb on the driver internal slow-path response iocb worker
2982 * queue. The caller does not hold any lock. The function will remove each
2983 * response iocb from the response worker queue and calls the handle
2984 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2985 **/
2986static void
2987lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2988 struct lpfc_sli_ring *pring, uint32_t mask)
2989{
2990 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002991 struct hbq_dmabuf *dmabuf;
2992 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002993 unsigned long iflag;
2994
James Smart45ed1192009-10-02 15:17:02 -04002995 spin_lock_irqsave(&phba->hbalock, iflag);
2996 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2997 spin_unlock_irqrestore(&phba->hbalock, iflag);
2998 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002999 /* Get the response iocb from the head of work queue */
3000 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003001 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003002 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003003 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003004
3005 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3006 case CQE_CODE_COMPL_WQE:
3007 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3008 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003009 /* Translate ELS WCQE to response IOCBQ */
3010 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3011 irspiocbq);
3012 if (irspiocbq)
3013 lpfc_sli_sp_handle_rspiocb(phba, pring,
3014 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003015 break;
3016 case CQE_CODE_RECEIVE:
3017 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3018 cq_event);
3019 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3020 break;
3021 default:
3022 break;
3023 }
James Smart4f774512009-05-22 14:52:35 -04003024 }
3025}
3026
3027/**
James Smart3621a712009-04-06 18:47:14 -04003028 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003029 * @phba: Pointer to HBA context object.
3030 * @pring: Pointer to driver SLI ring object.
3031 *
3032 * This function aborts all iocbs in the given ring and frees all the iocb
3033 * objects in txq. This function issues an abort iocb for all the iocb commands
3034 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3035 * the return of this function. The caller is not required to hold any locks.
3036 **/
James Smart2e0fef82007-06-17 19:56:36 -05003037void
dea31012005-04-17 16:05:31 -05003038lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3039{
James Smart2534ba72007-04-25 09:52:20 -04003040 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003041 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003042
James Smart92d7f7b2007-06-17 19:56:38 -05003043 if (pring->ringno == LPFC_ELS_RING) {
3044 lpfc_fabric_abort_hba(phba);
3045 }
3046
dea31012005-04-17 16:05:31 -05003047 /* Error everything on txq and txcmplq
3048 * First do the txq.
3049 */
James Smart2e0fef82007-06-17 19:56:36 -05003050 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003051 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003052 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003053
3054 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003055 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3056 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3057
James Smart2e0fef82007-06-17 19:56:36 -05003058 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003059
James Smarta257bf92009-04-06 18:48:10 -04003060 /* Cancel all the IOCBs from the completions list */
3061 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3062 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003063}
3064
James Smarte59058c2008-08-24 21:49:00 -04003065/**
James Smart3621a712009-04-06 18:47:14 -04003066 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003067 * @phba: Pointer to HBA context object.
3068 *
3069 * This function flushes all iocbs in the fcp ring and frees all the iocb
3070 * objects in txq and txcmplq. This function will not issue abort iocbs
3071 * for all the iocb commands in txcmplq, they will just be returned with
3072 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3073 * slot has been permanently disabled.
3074 **/
3075void
3076lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3077{
3078 LIST_HEAD(txq);
3079 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003080 struct lpfc_sli *psli = &phba->sli;
3081 struct lpfc_sli_ring *pring;
3082
3083 /* Currently, only one fcp ring */
3084 pring = &psli->ring[psli->fcp_ring];
3085
3086 spin_lock_irq(&phba->hbalock);
3087 /* Retrieve everything on txq */
3088 list_splice_init(&pring->txq, &txq);
3089 pring->txq_cnt = 0;
3090
3091 /* Retrieve everything on the txcmplq */
3092 list_splice_init(&pring->txcmplq, &txcmplq);
3093 pring->txcmplq_cnt = 0;
3094 spin_unlock_irq(&phba->hbalock);
3095
3096 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003097 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3098 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003099
3100 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003101 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3102 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003103}
3104
3105/**
James Smart3772a992009-05-22 14:50:54 -04003106 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003107 * @phba: Pointer to HBA context object.
3108 * @mask: Bit mask to be checked.
3109 *
3110 * This function reads the host status register and compares
3111 * with the provided bit mask to check if HBA completed
3112 * the restart. This function will wait in a loop for the
3113 * HBA to complete restart. If the HBA does not restart within
3114 * 15 iterations, the function will reset the HBA again. The
3115 * function returns 1 when HBA fail to restart otherwise returns
3116 * zero.
3117 **/
James Smart3772a992009-05-22 14:50:54 -04003118static int
3119lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003120{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003121 uint32_t status;
3122 int i = 0;
3123 int retval = 0;
dea31012005-04-17 16:05:31 -05003124
Jamie Wellnitz41415862006-02-28 19:25:27 -05003125 /* Read the HBA Host Status Register */
3126 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003127
Jamie Wellnitz41415862006-02-28 19:25:27 -05003128 /*
3129 * Check status register every 100ms for 5 retries, then every
3130 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3131 * every 2.5 sec for 4.
3132 * Break our of the loop if errors occurred during init.
3133 */
3134 while (((status & mask) != mask) &&
3135 !(status & HS_FFERM) &&
3136 i++ < 20) {
dea31012005-04-17 16:05:31 -05003137
Jamie Wellnitz41415862006-02-28 19:25:27 -05003138 if (i <= 5)
3139 msleep(10);
3140 else if (i <= 10)
3141 msleep(500);
3142 else
3143 msleep(2500);
dea31012005-04-17 16:05:31 -05003144
Jamie Wellnitz41415862006-02-28 19:25:27 -05003145 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003146 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003147 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003148 lpfc_sli_brdrestart(phba);
3149 }
3150 /* Read the HBA Host Status Register */
3151 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003152 }
dea31012005-04-17 16:05:31 -05003153
Jamie Wellnitz41415862006-02-28 19:25:27 -05003154 /* Check to see if any errors occurred during init */
3155 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3157 "2751 Adapter failed to restart, "
3158 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3159 status,
3160 readl(phba->MBslimaddr + 0xa8),
3161 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003162 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003163 retval = 1;
3164 }
dea31012005-04-17 16:05:31 -05003165
Jamie Wellnitz41415862006-02-28 19:25:27 -05003166 return retval;
dea31012005-04-17 16:05:31 -05003167}
3168
James Smartda0436e2009-05-22 14:51:39 -04003169/**
3170 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3171 * @phba: Pointer to HBA context object.
3172 * @mask: Bit mask to be checked.
3173 *
3174 * This function checks the host status register to check if HBA is
3175 * ready. This function will wait in a loop for the HBA to be ready
3176 * If the HBA is not ready , the function will will reset the HBA PCI
3177 * function again. The function returns 1 when HBA fail to be ready
3178 * otherwise returns zero.
3179 **/
3180static int
3181lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3182{
3183 uint32_t status;
3184 int retval = 0;
3185
3186 /* Read the HBA Host Status Register */
3187 status = lpfc_sli4_post_status_check(phba);
3188
3189 if (status) {
3190 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3191 lpfc_sli_brdrestart(phba);
3192 status = lpfc_sli4_post_status_check(phba);
3193 }
3194
3195 /* Check to see if any errors occurred during init */
3196 if (status) {
3197 phba->link_state = LPFC_HBA_ERROR;
3198 retval = 1;
3199 } else
3200 phba->sli4_hba.intr_enable = 0;
3201
3202 return retval;
3203}
3204
3205/**
3206 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3207 * @phba: Pointer to HBA context object.
3208 * @mask: Bit mask to be checked.
3209 *
3210 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3211 * from the API jump table function pointer from the lpfc_hba struct.
3212 **/
3213int
3214lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3215{
3216 return phba->lpfc_sli_brdready(phba, mask);
3217}
3218
James Smart92908312006-03-07 15:04:13 -05003219#define BARRIER_TEST_PATTERN (0xdeadbeef)
3220
James Smarte59058c2008-08-24 21:49:00 -04003221/**
James Smart3621a712009-04-06 18:47:14 -04003222 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003223 * @phba: Pointer to HBA context object.
3224 *
3225 * This function is called before resetting an HBA. This
3226 * function requests HBA to quiesce DMAs before a reset.
3227 **/
James Smart2e0fef82007-06-17 19:56:36 -05003228void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003229{
James Smart65a29c12006-07-06 15:50:50 -04003230 uint32_t __iomem *resp_buf;
3231 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003232 volatile uint32_t mbox;
3233 uint32_t hc_copy;
3234 int i;
3235 uint8_t hdrtype;
3236
3237 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3238 if (hdrtype != 0x80 ||
3239 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3240 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3241 return;
3242
3243 /*
3244 * Tell the other part of the chip to suspend temporarily all
3245 * its DMA activity.
3246 */
James Smart65a29c12006-07-06 15:50:50 -04003247 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003248
3249 /* Disable the error attention */
3250 hc_copy = readl(phba->HCregaddr);
3251 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3252 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003253 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003254
3255 if (readl(phba->HAregaddr) & HA_ERATT) {
3256 /* Clear Chip error bit */
3257 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003258 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003259 }
3260
3261 mbox = 0;
3262 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3263 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3264
3265 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003266 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003267 writel(mbox, mbox_buf);
3268
3269 for (i = 0;
3270 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3271 mdelay(1);
3272
3273 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003274 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003275 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003276 goto restore_hc;
3277 else
3278 goto clear_errat;
3279 }
3280
3281 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3282 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3283 mdelay(1);
3284
3285clear_errat:
3286
3287 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3288 mdelay(1);
3289
3290 if (readl(phba->HAregaddr) & HA_ERATT) {
3291 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003292 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003293 }
3294
3295restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003296 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003297 writel(hc_copy, phba->HCregaddr);
3298 readl(phba->HCregaddr); /* flush */
3299}
3300
James Smarte59058c2008-08-24 21:49:00 -04003301/**
James Smart3621a712009-04-06 18:47:14 -04003302 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003303 * @phba: Pointer to HBA context object.
3304 *
3305 * This function issues a kill_board mailbox command and waits for
3306 * the error attention interrupt. This function is called for stopping
3307 * the firmware processing. The caller is not required to hold any
3308 * locks. This function calls lpfc_hba_down_post function to free
3309 * any pending commands after the kill. The function will return 1 when it
3310 * fails to kill the board else will return 0.
3311 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003312int
James Smart2e0fef82007-06-17 19:56:36 -05003313lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003314{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003315 struct lpfc_sli *psli;
3316 LPFC_MBOXQ_t *pmb;
3317 uint32_t status;
3318 uint32_t ha_copy;
3319 int retval;
3320 int i = 0;
3321
3322 psli = &phba->sli;
3323
3324 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003325 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003326 "0329 Kill HBA Data: x%x x%x\n",
3327 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003328
James Smart98c9ea52007-10-27 13:37:33 -04003329 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3330 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003331 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003332
3333 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003334 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003335 status = readl(phba->HCregaddr);
3336 status &= ~HC_ERINT_ENA;
3337 writel(status, phba->HCregaddr);
3338 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003339 phba->link_flag |= LS_IGNORE_ERATT;
3340 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003341
3342 lpfc_kill_board(phba, pmb);
3343 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3344 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3345
3346 if (retval != MBX_SUCCESS) {
3347 if (retval != MBX_BUSY)
3348 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3350 "2752 KILL_BOARD command failed retval %d\n",
3351 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003352 spin_lock_irq(&phba->hbalock);
3353 phba->link_flag &= ~LS_IGNORE_ERATT;
3354 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003355 return 1;
3356 }
3357
James Smartf4b4c682009-05-22 14:53:12 -04003358 spin_lock_irq(&phba->hbalock);
3359 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3360 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003361
Jamie Wellnitz41415862006-02-28 19:25:27 -05003362 mempool_free(pmb, phba->mbox_mem_pool);
3363
3364 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3365 * attention every 100ms for 3 seconds. If we don't get ERATT after
3366 * 3 seconds we still set HBA_ERROR state because the status of the
3367 * board is now undefined.
3368 */
3369 ha_copy = readl(phba->HAregaddr);
3370
3371 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3372 mdelay(100);
3373 ha_copy = readl(phba->HAregaddr);
3374 }
3375
3376 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003377 if (ha_copy & HA_ERATT) {
3378 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003379 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003380 }
James Smart2e0fef82007-06-17 19:56:36 -05003381 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003382 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003383 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003384 phba->link_flag &= ~LS_IGNORE_ERATT;
3385 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003386
Jamie Wellnitz41415862006-02-28 19:25:27 -05003387 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003388 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003389
James Smart2e0fef82007-06-17 19:56:36 -05003390 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003391}
3392
James Smarte59058c2008-08-24 21:49:00 -04003393/**
James Smart3772a992009-05-22 14:50:54 -04003394 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003395 * @phba: Pointer to HBA context object.
3396 *
3397 * This function resets the HBA by writing HC_INITFF to the control
3398 * register. After the HBA resets, this function resets all the iocb ring
3399 * indices. This function disables PCI layer parity checking during
3400 * the reset.
3401 * This function returns 0 always.
3402 * The caller is not required to hold any locks.
3403 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003404int
James Smart2e0fef82007-06-17 19:56:36 -05003405lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003406{
3407 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003408 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003409 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003410 int i;
dea31012005-04-17 16:05:31 -05003411
Jamie Wellnitz41415862006-02-28 19:25:27 -05003412 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003413
Jamie Wellnitz41415862006-02-28 19:25:27 -05003414 /* Reset HBA */
3415 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003416 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003417 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003418
3419 /* perform board reset */
3420 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003421 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003422 phba->pport->fc_myDID = 0;
3423 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003424
Jamie Wellnitz41415862006-02-28 19:25:27 -05003425 /* Turn off parity checking and serr during the physical reset */
3426 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3427 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3428 (cfg_value &
3429 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3430
James Smart3772a992009-05-22 14:50:54 -04003431 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3432
Jamie Wellnitz41415862006-02-28 19:25:27 -05003433 /* Now toggle INITFF bit in the Host Control Register */
3434 writel(HC_INITFF, phba->HCregaddr);
3435 mdelay(1);
3436 readl(phba->HCregaddr); /* flush */
3437 writel(0, phba->HCregaddr);
3438 readl(phba->HCregaddr); /* flush */
3439
3440 /* Restore PCI cmd register */
3441 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003442
3443 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003444 for (i = 0; i < psli->num_rings; i++) {
3445 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003446 pring->flag = 0;
3447 pring->rspidx = 0;
3448 pring->next_cmdidx = 0;
3449 pring->local_getidx = 0;
3450 pring->cmdidx = 0;
3451 pring->missbufcnt = 0;
3452 }
dea31012005-04-17 16:05:31 -05003453
James Smart2e0fef82007-06-17 19:56:36 -05003454 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003455 return 0;
3456}
3457
James Smarte59058c2008-08-24 21:49:00 -04003458/**
James Smartda0436e2009-05-22 14:51:39 -04003459 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3460 * @phba: Pointer to HBA context object.
3461 *
3462 * This function resets a SLI4 HBA. This function disables PCI layer parity
3463 * checking during resets the device. The caller is not required to hold
3464 * any locks.
3465 *
3466 * This function returns 0 always.
3467 **/
3468int
3469lpfc_sli4_brdreset(struct lpfc_hba *phba)
3470{
3471 struct lpfc_sli *psli = &phba->sli;
3472 uint16_t cfg_value;
3473 uint8_t qindx;
3474
3475 /* Reset HBA */
3476 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3477 "0295 Reset HBA Data: x%x x%x\n",
3478 phba->pport->port_state, psli->sli_flag);
3479
3480 /* perform board reset */
3481 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003482 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003483 phba->pport->fc_myDID = 0;
3484 phba->pport->fc_prevDID = 0;
3485
3486 /* Turn off parity checking and serr during the physical reset */
3487 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3488 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3489 (cfg_value &
3490 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3491
3492 spin_lock_irq(&phba->hbalock);
3493 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3494 phba->fcf.fcf_flag = 0;
3495 /* Clean up the child queue list for the CQs */
3496 list_del_init(&phba->sli4_hba.mbx_wq->list);
3497 list_del_init(&phba->sli4_hba.els_wq->list);
3498 list_del_init(&phba->sli4_hba.hdr_rq->list);
3499 list_del_init(&phba->sli4_hba.dat_rq->list);
3500 list_del_init(&phba->sli4_hba.mbx_cq->list);
3501 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003502 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3503 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3504 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3505 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3506 spin_unlock_irq(&phba->hbalock);
3507
3508 /* Now physically reset the device */
3509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3510 "0389 Performing PCI function reset!\n");
3511 /* Perform FCoE PCI function reset */
3512 lpfc_pci_function_reset(phba);
3513
3514 return 0;
3515}
3516
3517/**
3518 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003519 * @phba: Pointer to HBA context object.
3520 *
3521 * This function is called in the SLI initialization code path to
3522 * restart the HBA. The caller is not required to hold any lock.
3523 * This function writes MBX_RESTART mailbox command to the SLIM and
3524 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3525 * function to free any pending commands. The function enables
3526 * POST only during the first initialization. The function returns zero.
3527 * The function does not guarantee completion of MBX_RESTART mailbox
3528 * command before the return of this function.
3529 **/
James Smartda0436e2009-05-22 14:51:39 -04003530static int
3531lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532{
3533 MAILBOX_t *mb;
3534 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535 volatile uint32_t word0;
3536 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003537 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003538
James Smart2e0fef82007-06-17 19:56:36 -05003539 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003540
James Smart0d878412009-10-02 15:16:56 -04003541 /* Take PCIe device Advanced Error Reporting (AER) state */
3542 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3543
Jamie Wellnitz41415862006-02-28 19:25:27 -05003544 psli = &phba->sli;
3545
3546 /* Restart HBA */
3547 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003548 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003549 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003550
3551 word0 = 0;
3552 mb = (MAILBOX_t *) &word0;
3553 mb->mbxCommand = MBX_RESTART;
3554 mb->mbxHc = 1;
3555
James Smart92908312006-03-07 15:04:13 -05003556 lpfc_reset_barrier(phba);
3557
Jamie Wellnitz41415862006-02-28 19:25:27 -05003558 to_slim = phba->MBslimaddr;
3559 writel(*(uint32_t *) mb, to_slim);
3560 readl(to_slim); /* flush */
3561
3562 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003563 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003564 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003565 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003566 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003567 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003568 writel(*(uint32_t *) mb, to_slim);
3569 readl(to_slim); /* flush */
3570
3571 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003572 phba->pport->stopped = 0;
3573 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003574 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003575 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003576
James Smart64ba8812006-08-02 15:24:34 -04003577 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3578 psli->stats_start = get_seconds();
3579
James Smarteaf15d52008-12-04 22:39:29 -05003580 /* Give the INITFF and Post time to settle. */
3581 mdelay(100);
dea31012005-04-17 16:05:31 -05003582
James Smart0d878412009-10-02 15:16:56 -04003583 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3584 if (hba_aer_enabled)
3585 pci_disable_pcie_error_reporting(phba->pcidev);
3586
Jamie Wellnitz41415862006-02-28 19:25:27 -05003587 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003588
3589 return 0;
3590}
3591
James Smarte59058c2008-08-24 21:49:00 -04003592/**
James Smartda0436e2009-05-22 14:51:39 -04003593 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3594 * @phba: Pointer to HBA context object.
3595 *
3596 * This function is called in the SLI initialization code path to restart
3597 * a SLI4 HBA. The caller is not required to hold any lock.
3598 * At the end of the function, it calls lpfc_hba_down_post function to
3599 * free any pending commands.
3600 **/
3601static int
3602lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3603{
3604 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003605 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003606
3607 /* Restart HBA */
3608 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3609 "0296 Restart HBA Data: x%x x%x\n",
3610 phba->pport->port_state, psli->sli_flag);
3611
James Smart75baf692010-06-08 18:31:21 -04003612 /* Take PCIe device Advanced Error Reporting (AER) state */
3613 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3614
James Smartda0436e2009-05-22 14:51:39 -04003615 lpfc_sli4_brdreset(phba);
3616
3617 spin_lock_irq(&phba->hbalock);
3618 phba->pport->stopped = 0;
3619 phba->link_state = LPFC_INIT_START;
3620 phba->hba_flag = 0;
3621 spin_unlock_irq(&phba->hbalock);
3622
3623 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3624 psli->stats_start = get_seconds();
3625
James Smart75baf692010-06-08 18:31:21 -04003626 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3627 if (hba_aer_enabled)
3628 pci_disable_pcie_error_reporting(phba->pcidev);
3629
James Smartda0436e2009-05-22 14:51:39 -04003630 lpfc_hba_down_post(phba);
3631
3632 return 0;
3633}
3634
3635/**
3636 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3637 * @phba: Pointer to HBA context object.
3638 *
3639 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3640 * API jump table function pointer from the lpfc_hba struct.
3641**/
3642int
3643lpfc_sli_brdrestart(struct lpfc_hba *phba)
3644{
3645 return phba->lpfc_sli_brdrestart(phba);
3646}
3647
3648/**
James Smart3621a712009-04-06 18:47:14 -04003649 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003650 * @phba: Pointer to HBA context object.
3651 *
3652 * This function is called after a HBA restart to wait for successful
3653 * restart of the HBA. Successful restart of the HBA is indicated by
3654 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3655 * iteration, the function will restart the HBA again. The function returns
3656 * zero if HBA successfully restarted else returns negative error code.
3657 **/
dea31012005-04-17 16:05:31 -05003658static int
3659lpfc_sli_chipset_init(struct lpfc_hba *phba)
3660{
3661 uint32_t status, i = 0;
3662
3663 /* Read the HBA Host Status Register */
3664 status = readl(phba->HSregaddr);
3665
3666 /* Check status register to see what current state is */
3667 i = 0;
3668 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3669
James Smartdcf2a4e2010-09-29 11:18:53 -04003670 /* Check every 10ms for 10 retries, then every 100ms for 90
3671 * retries, then every 1 sec for 50 retires for a total of
3672 * ~60 seconds before reset the board again and check every
3673 * 1 sec for 50 retries. The up to 60 seconds before the
3674 * board ready is required by the Falcon FIPS zeroization
3675 * complete, and any reset the board in between shall cause
3676 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05003677 */
James Smartdcf2a4e2010-09-29 11:18:53 -04003678 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05003679 /* Adapter failed to init, timeout, status reg
3680 <status> */
James Smarted957682007-06-17 19:56:37 -05003681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003682 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003683 "timeout, status reg x%x, "
3684 "FW Data: A8 x%x AC x%x\n", status,
3685 readl(phba->MBslimaddr + 0xa8),
3686 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003687 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003688 return -ETIMEDOUT;
3689 }
3690
3691 /* Check to see if any errors occurred during init */
3692 if (status & HS_FFERM) {
3693 /* ERROR: During chipset initialization */
3694 /* Adapter failed to init, chipset, status reg
3695 <status> */
James Smarted957682007-06-17 19:56:37 -05003696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003697 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003698 "chipset, status reg x%x, "
3699 "FW Data: A8 x%x AC x%x\n", status,
3700 readl(phba->MBslimaddr + 0xa8),
3701 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003702 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003703 return -EIO;
3704 }
3705
James Smartdcf2a4e2010-09-29 11:18:53 -04003706 if (i <= 10)
dea31012005-04-17 16:05:31 -05003707 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04003708 else if (i <= 100)
3709 msleep(100);
3710 else
3711 msleep(1000);
dea31012005-04-17 16:05:31 -05003712
James Smartdcf2a4e2010-09-29 11:18:53 -04003713 if (i == 150) {
3714 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003715 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003716 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003717 }
3718 /* Read the HBA Host Status Register */
3719 status = readl(phba->HSregaddr);
3720 }
3721
3722 /* Check to see if any errors occurred during init */
3723 if (status & HS_FFERM) {
3724 /* ERROR: During chipset initialization */
3725 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003727 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003728 "status reg x%x, "
3729 "FW Data: A8 x%x AC x%x\n", status,
3730 readl(phba->MBslimaddr + 0xa8),
3731 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003732 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003733 return -EIO;
3734 }
3735
3736 /* Clear all interrupt enable conditions */
3737 writel(0, phba->HCregaddr);
3738 readl(phba->HCregaddr); /* flush */
3739
3740 /* setup host attn register */
3741 writel(0xffffffff, phba->HAregaddr);
3742 readl(phba->HAregaddr); /* flush */
3743 return 0;
3744}
3745
James Smarte59058c2008-08-24 21:49:00 -04003746/**
James Smart3621a712009-04-06 18:47:14 -04003747 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003748 *
3749 * This function calculates and returns the number of HBQs required to be
3750 * configured.
3751 **/
James Smart78b2d852007-08-02 11:10:21 -04003752int
James Smarted957682007-06-17 19:56:37 -05003753lpfc_sli_hbq_count(void)
3754{
James Smart92d7f7b2007-06-17 19:56:38 -05003755 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003756}
3757
James Smarte59058c2008-08-24 21:49:00 -04003758/**
James Smart3621a712009-04-06 18:47:14 -04003759 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003760 *
3761 * This function adds the number of hbq entries in every HBQ to get
3762 * the total number of hbq entries required for the HBA and returns
3763 * the total count.
3764 **/
James Smarted957682007-06-17 19:56:37 -05003765static int
3766lpfc_sli_hbq_entry_count(void)
3767{
3768 int hbq_count = lpfc_sli_hbq_count();
3769 int count = 0;
3770 int i;
3771
3772 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003773 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003774 return count;
3775}
3776
James Smarte59058c2008-08-24 21:49:00 -04003777/**
James Smart3621a712009-04-06 18:47:14 -04003778 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003779 *
3780 * This function calculates amount of memory required for all hbq entries
3781 * to be configured and returns the total memory required.
3782 **/
dea31012005-04-17 16:05:31 -05003783int
James Smarted957682007-06-17 19:56:37 -05003784lpfc_sli_hbq_size(void)
3785{
3786 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3787}
3788
James Smarte59058c2008-08-24 21:49:00 -04003789/**
James Smart3621a712009-04-06 18:47:14 -04003790 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003791 * @phba: Pointer to HBA context object.
3792 *
3793 * This function is called during the SLI initialization to configure
3794 * all the HBQs and post buffers to the HBQ. The caller is not
3795 * required to hold any locks. This function will return zero if successful
3796 * else it will return negative error code.
3797 **/
James Smarted957682007-06-17 19:56:37 -05003798static int
3799lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3800{
3801 int hbq_count = lpfc_sli_hbq_count();
3802 LPFC_MBOXQ_t *pmb;
3803 MAILBOX_t *pmbox;
3804 uint32_t hbqno;
3805 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003806
James Smart92d7f7b2007-06-17 19:56:38 -05003807 /* Get a Mailbox buffer to setup mailbox
3808 * commands for HBA initialization
3809 */
James Smarted957682007-06-17 19:56:37 -05003810 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3811
3812 if (!pmb)
3813 return -ENOMEM;
3814
James Smart04c68492009-05-22 14:52:52 -04003815 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003816
3817 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3818 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003819 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003820
3821 hbq_entry_index = 0;
3822 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3823 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3824 phba->hbqs[hbqno].hbqPutIdx = 0;
3825 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3826 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003827 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003828 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3829 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003830 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3831
3832 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3833 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3834 mbxStatus <status>, ring <num> */
3835
3836 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003837 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003838 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003839 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003840 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003841 pmbox->mbxStatus, hbqno);
3842
3843 phba->link_state = LPFC_HBA_ERROR;
3844 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04003845 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003846 }
3847 }
3848 phba->hbq_count = hbq_count;
3849
James Smarted957682007-06-17 19:56:37 -05003850 mempool_free(pmb, phba->mbox_mem_pool);
3851
James Smart92d7f7b2007-06-17 19:56:38 -05003852 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003853 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3854 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003855 return 0;
3856}
3857
James Smarte59058c2008-08-24 21:49:00 -04003858/**
James Smart4f774512009-05-22 14:52:35 -04003859 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3860 * @phba: Pointer to HBA context object.
3861 *
3862 * This function is called during the SLI initialization to configure
3863 * all the HBQs and post buffers to the HBQ. The caller is not
3864 * required to hold any locks. This function will return zero if successful
3865 * else it will return negative error code.
3866 **/
3867static int
3868lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3869{
3870 phba->hbq_in_use = 1;
3871 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3872 phba->hbq_count = 1;
3873 /* Initially populate or replenish the HBQs */
3874 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3875 return 0;
3876}
3877
3878/**
James Smart3621a712009-04-06 18:47:14 -04003879 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003880 * @phba: Pointer to HBA context object.
3881 * @sli_mode: sli mode - 2/3
3882 *
3883 * This function is called by the sli intialization code path
3884 * to issue config_port mailbox command. This function restarts the
3885 * HBA firmware and issues a config_port mailbox command to configure
3886 * the SLI interface in the sli mode specified by sli_mode
3887 * variable. The caller is not required to hold any locks.
3888 * The function returns 0 if successful, else returns negative error
3889 * code.
3890 **/
James Smart93996272008-08-24 21:50:30 -04003891int
3892lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003893{
3894 LPFC_MBOXQ_t *pmb;
3895 uint32_t resetcount = 0, rc = 0, done = 0;
3896
3897 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3898 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003899 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003900 return -ENOMEM;
3901 }
3902
James Smarted957682007-06-17 19:56:37 -05003903 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003904 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003905 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003906 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003907 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003908 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003909 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003910 rc = lpfc_sli_chipset_init(phba);
3911 if (rc)
3912 break;
3913
James Smart2e0fef82007-06-17 19:56:36 -05003914 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003915 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003916 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003917 resetcount++;
3918
James Smarted957682007-06-17 19:56:37 -05003919 /* Call pre CONFIG_PORT mailbox command initialization. A
3920 * value of 0 means the call was successful. Any other
3921 * nonzero value is a failure, but if ERESTART is returned,
3922 * the driver may reset the HBA and try again.
3923 */
dea31012005-04-17 16:05:31 -05003924 rc = lpfc_config_port_prep(phba);
3925 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003926 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003927 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003928 } else if (rc)
dea31012005-04-17 16:05:31 -05003929 break;
James Smart2e0fef82007-06-17 19:56:36 -05003930 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003931 lpfc_config_port(phba, pmb);
3932 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003933 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3934 LPFC_SLI3_HBQ_ENABLED |
3935 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04003936 LPFC_SLI3_BG_ENABLED |
3937 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003938 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003940 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003941 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003942 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003943 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003944 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003945 spin_unlock_irq(&phba->hbalock);
3946 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003947 } else {
3948 /* Allow asynchronous mailbox command to go through */
3949 spin_lock_irq(&phba->hbalock);
3950 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3951 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003952 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003953 }
dea31012005-04-17 16:05:31 -05003954 }
James Smarted957682007-06-17 19:56:37 -05003955 if (!done) {
3956 rc = -EINVAL;
3957 goto do_prep_failed;
3958 }
James Smart04c68492009-05-22 14:52:52 -04003959 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3960 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003961 rc = -ENXIO;
3962 goto do_prep_failed;
3963 }
James Smart04c68492009-05-22 14:52:52 -04003964 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003965 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003966 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3967 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3968 phba->max_vpi : phba->max_vports;
3969
James Smart34b02dc2008-08-24 21:49:55 -04003970 } else
3971 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04003972 phba->fips_level = 0;
3973 phba->fips_spec_rev = 0;
3974 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04003975 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04003976 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
3977 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
3978 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3979 "2850 Security Crypto Active. FIPS x%d "
3980 "(Spec Rev: x%d)",
3981 phba->fips_level, phba->fips_spec_rev);
3982 }
3983 if (pmb->u.mb.un.varCfgPort.sec_err) {
3984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3985 "2856 Config Port Security Crypto "
3986 "Error: x%x ",
3987 pmb->u.mb.un.varCfgPort.sec_err);
3988 }
James Smart04c68492009-05-22 14:52:52 -04003989 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003990 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003991 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003992 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04003993
3994 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3995 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05003996
3997 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003998 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003999 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4000 else
4001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4002 "0443 Adapter did not grant "
4003 "BlockGuard\n");
4004 }
James Smart34b02dc2008-08-24 21:49:55 -04004005 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004006 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004007 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004008 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004009 }
James Smart92d7f7b2007-06-17 19:56:38 -05004010do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004011 mempool_free(pmb, phba->mbox_mem_pool);
4012 return rc;
4013}
4014
James Smarte59058c2008-08-24 21:49:00 -04004015
4016/**
James Smart3621a712009-04-06 18:47:14 -04004017 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004018 * @phba: Pointer to HBA context object.
4019 *
4020 * This function is the main SLI intialization function. This function
4021 * is called by the HBA intialization code, HBA reset code and HBA
4022 * error attention handler code. Caller is not required to hold any
4023 * locks. This function issues config_port mailbox command to configure
4024 * the SLI, setup iocb rings and HBQ rings. In the end the function
4025 * calls the config_port_post function to issue init_link mailbox
4026 * command and to start the discovery. The function will return zero
4027 * if successful, else it will return negative error code.
4028 **/
James Smarted957682007-06-17 19:56:37 -05004029int
4030lpfc_sli_hba_setup(struct lpfc_hba *phba)
4031{
4032 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004033 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004034
4035 switch (lpfc_sli_mode) {
4036 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004037 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004039 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004040 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004041 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004042 break;
4043 }
James Smarted957682007-06-17 19:56:37 -05004044 mode = 2;
4045 break;
4046 case 0:
4047 case 3:
4048 break;
4049 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004051 "1819 Unrecognized lpfc_sli_mode "
4052 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004053
4054 break;
4055 }
4056
James Smart93996272008-08-24 21:50:30 -04004057 rc = lpfc_sli_config_port(phba, mode);
4058
James Smarted957682007-06-17 19:56:37 -05004059 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004061 "1820 Unable to select SLI-3. "
4062 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004063 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004064 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004065 if (rc)
dea31012005-04-17 16:05:31 -05004066 goto lpfc_sli_hba_setup_error;
4067
James Smart0d878412009-10-02 15:16:56 -04004068 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4069 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4070 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4071 if (!rc) {
4072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4073 "2709 This device supports "
4074 "Advanced Error Reporting (AER)\n");
4075 spin_lock_irq(&phba->hbalock);
4076 phba->hba_flag |= HBA_AER_ENABLED;
4077 spin_unlock_irq(&phba->hbalock);
4078 } else {
4079 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4080 "2708 This device does not support "
4081 "Advanced Error Reporting (AER)\n");
4082 phba->cfg_aer_support = 0;
4083 }
4084 }
4085
James Smarted957682007-06-17 19:56:37 -05004086 if (phba->sli_rev == 3) {
4087 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4088 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004089 } else {
4090 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4091 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004092 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004093 }
4094
4095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004096 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4097 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004098 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004099
4100 if (rc)
4101 goto lpfc_sli_hba_setup_error;
4102
James Smart93996272008-08-24 21:50:30 -04004103 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004104 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4105 rc = lpfc_sli_hbq_setup(phba);
4106 if (rc)
4107 goto lpfc_sli_hba_setup_error;
4108 }
James Smart04c68492009-05-22 14:52:52 -04004109 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004110 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004111 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004112
4113 rc = lpfc_config_port_post(phba);
4114 if (rc)
4115 goto lpfc_sli_hba_setup_error;
4116
James Smarted957682007-06-17 19:56:37 -05004117 return rc;
4118
James Smart92d7f7b2007-06-17 19:56:38 -05004119lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004120 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004122 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004123 return rc;
4124}
4125
James Smartda0436e2009-05-22 14:51:39 -04004126/**
4127 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4128 * @phba: Pointer to HBA context object.
4129 * @mboxq: mailbox pointer.
4130 * This function issue a dump mailbox command to read config region
4131 * 23 and parse the records in the region and populate driver
4132 * data structure.
4133 **/
4134static int
4135lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4136 LPFC_MBOXQ_t *mboxq)
4137{
4138 struct lpfc_dmabuf *mp;
4139 struct lpfc_mqe *mqe;
4140 uint32_t data_length;
4141 int rc;
4142
4143 /* Program the default value of vlan_id and fc_map */
4144 phba->valid_vlan = 0;
4145 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4146 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4147 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4148
4149 mqe = &mboxq->u.mqe;
4150 if (lpfc_dump_fcoe_param(phba, mboxq))
4151 return -ENOMEM;
4152
4153 mp = (struct lpfc_dmabuf *) mboxq->context1;
4154 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4155
4156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4157 "(%d):2571 Mailbox cmd x%x Status x%x "
4158 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4159 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4160 "CQ: x%x x%x x%x x%x\n",
4161 mboxq->vport ? mboxq->vport->vpi : 0,
4162 bf_get(lpfc_mqe_command, mqe),
4163 bf_get(lpfc_mqe_status, mqe),
4164 mqe->un.mb_words[0], mqe->un.mb_words[1],
4165 mqe->un.mb_words[2], mqe->un.mb_words[3],
4166 mqe->un.mb_words[4], mqe->un.mb_words[5],
4167 mqe->un.mb_words[6], mqe->un.mb_words[7],
4168 mqe->un.mb_words[8], mqe->un.mb_words[9],
4169 mqe->un.mb_words[10], mqe->un.mb_words[11],
4170 mqe->un.mb_words[12], mqe->un.mb_words[13],
4171 mqe->un.mb_words[14], mqe->un.mb_words[15],
4172 mqe->un.mb_words[16], mqe->un.mb_words[50],
4173 mboxq->mcqe.word0,
4174 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4175 mboxq->mcqe.trailer);
4176
4177 if (rc) {
4178 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4179 kfree(mp);
4180 return -EIO;
4181 }
4182 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004183 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004184 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4185 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004186 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004187 }
James Smartda0436e2009-05-22 14:51:39 -04004188
4189 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4190 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4191 kfree(mp);
4192 return 0;
4193}
4194
4195/**
4196 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4197 * @phba: pointer to lpfc hba data structure.
4198 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4199 * @vpd: pointer to the memory to hold resulting port vpd data.
4200 * @vpd_size: On input, the number of bytes allocated to @vpd.
4201 * On output, the number of data bytes in @vpd.
4202 *
4203 * This routine executes a READ_REV SLI4 mailbox command. In
4204 * addition, this routine gets the port vpd data.
4205 *
4206 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004207 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004208 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004209 **/
4210static int
4211lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4212 uint8_t *vpd, uint32_t *vpd_size)
4213{
4214 int rc = 0;
4215 uint32_t dma_size;
4216 struct lpfc_dmabuf *dmabuf;
4217 struct lpfc_mqe *mqe;
4218
4219 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4220 if (!dmabuf)
4221 return -ENOMEM;
4222
4223 /*
4224 * Get a DMA buffer for the vpd data resulting from the READ_REV
4225 * mailbox command.
4226 */
4227 dma_size = *vpd_size;
4228 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4229 dma_size,
4230 &dmabuf->phys,
4231 GFP_KERNEL);
4232 if (!dmabuf->virt) {
4233 kfree(dmabuf);
4234 return -ENOMEM;
4235 }
4236 memset(dmabuf->virt, 0, dma_size);
4237
4238 /*
4239 * The SLI4 implementation of READ_REV conflicts at word1,
4240 * bits 31:16 and SLI4 adds vpd functionality not present
4241 * in SLI3. This code corrects the conflicts.
4242 */
4243 lpfc_read_rev(phba, mboxq);
4244 mqe = &mboxq->u.mqe;
4245 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4246 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4247 mqe->un.read_rev.word1 &= 0x0000FFFF;
4248 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4249 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4250
4251 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4252 if (rc) {
4253 dma_free_coherent(&phba->pcidev->dev, dma_size,
4254 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004255 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004256 return -EIO;
4257 }
4258
James Smartda0436e2009-05-22 14:51:39 -04004259 /*
4260 * The available vpd length cannot be bigger than the
4261 * DMA buffer passed to the port. Catch the less than
4262 * case and update the caller's size.
4263 */
4264 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4265 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4266
James Smartd7c47992010-06-08 18:31:54 -04004267 memcpy(vpd, dmabuf->virt, *vpd_size);
4268
James Smartda0436e2009-05-22 14:51:39 -04004269 dma_free_coherent(&phba->pcidev->dev, dma_size,
4270 dmabuf->virt, dmabuf->phys);
4271 kfree(dmabuf);
4272 return 0;
4273}
4274
4275/**
4276 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4277 * @phba: pointer to lpfc hba data structure.
4278 *
4279 * This routine is called to explicitly arm the SLI4 device's completion and
4280 * event queues
4281 **/
4282static void
4283lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4284{
4285 uint8_t fcp_eqidx;
4286
4287 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4288 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004289 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4290 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4291 LPFC_QUEUE_REARM);
4292 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4293 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4294 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4295 LPFC_QUEUE_REARM);
4296}
4297
4298/**
4299 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4300 * @phba: Pointer to HBA context object.
4301 *
4302 * This function is the main SLI4 device intialization PCI function. This
4303 * function is called by the HBA intialization code, HBA reset code and
4304 * HBA error attention handler code. Caller is not required to hold any
4305 * locks.
4306 **/
4307int
4308lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4309{
4310 int rc;
4311 LPFC_MBOXQ_t *mboxq;
4312 struct lpfc_mqe *mqe;
4313 uint8_t *vpd;
4314 uint32_t vpd_size;
4315 uint32_t ftr_rsp = 0;
4316 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4317 struct lpfc_vport *vport = phba->pport;
4318 struct lpfc_dmabuf *mp;
4319
4320 /* Perform a PCI function reset to start from clean */
4321 rc = lpfc_pci_function_reset(phba);
4322 if (unlikely(rc))
4323 return -ENODEV;
4324
4325 /* Check the HBA Host Status Register for readyness */
4326 rc = lpfc_sli4_post_status_check(phba);
4327 if (unlikely(rc))
4328 return -ENODEV;
4329 else {
4330 spin_lock_irq(&phba->hbalock);
4331 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4332 spin_unlock_irq(&phba->hbalock);
4333 }
4334
4335 /*
4336 * Allocate a single mailbox container for initializing the
4337 * port.
4338 */
4339 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4340 if (!mboxq)
4341 return -ENOMEM;
4342
4343 /*
4344 * Continue initialization with default values even if driver failed
4345 * to read FCoE param config regions
4346 */
4347 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004349 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004350
4351 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004352 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004353 vpd = kzalloc(vpd_size, GFP_KERNEL);
4354 if (!vpd) {
4355 rc = -ENOMEM;
4356 goto out_free_mbox;
4357 }
4358
4359 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4360 if (unlikely(rc))
4361 goto out_free_vpd;
4362
4363 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004364 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4365 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4366 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004367
4368 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4369 LPFC_DCBX_CEE_MODE)
4370 phba->hba_flag |= HBA_FIP_SUPPORT;
4371 else
4372 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4373
James Smartf1126682009-06-10 17:22:44 -04004374 if (phba->sli_rev != LPFC_SLI_REV4 ||
4375 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4377 "0376 READ_REV Error. SLI Level %d "
4378 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004379 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004380 rc = -EIO;
4381 goto out_free_vpd;
4382 }
James Smartda0436e2009-05-22 14:51:39 -04004383 /*
4384 * Evaluate the read rev and vpd data. Populate the driver
4385 * state with the results. If this routine fails, the failure
4386 * is not fatal as the driver will use generic values.
4387 */
4388 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4389 if (unlikely(!rc)) {
4390 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4391 "0377 Error %d parsing vpd. "
4392 "Using defaults.\n", rc);
4393 rc = 0;
4394 }
4395
James Smartf1126682009-06-10 17:22:44 -04004396 /* Save information as VPD data */
4397 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4398 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4399 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4400 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4401 &mqe->un.read_rev);
4402 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4403 &mqe->un.read_rev);
4404 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4405 &mqe->un.read_rev);
4406 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4407 &mqe->un.read_rev);
4408 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4409 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4410 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4411 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4412 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4413 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4414 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4415 "(%d):0380 READ_REV Status x%x "
4416 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4417 mboxq->vport ? mboxq->vport->vpi : 0,
4418 bf_get(lpfc_mqe_status, mqe),
4419 phba->vpd.rev.opFwName,
4420 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4421 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004422
4423 /*
4424 * Discover the port's supported feature set and match it against the
4425 * hosts requests.
4426 */
4427 lpfc_request_features(phba, mboxq);
4428 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4429 if (unlikely(rc)) {
4430 rc = -EIO;
4431 goto out_free_vpd;
4432 }
4433
4434 /*
4435 * The port must support FCP initiator mode as this is the
4436 * only mode running in the host.
4437 */
4438 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4439 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4440 "0378 No support for fcpi mode.\n");
4441 ftr_rsp++;
4442 }
4443
4444 /*
4445 * If the port cannot support the host's requested features
4446 * then turn off the global config parameters to disable the
4447 * feature in the driver. This is not a fatal error.
4448 */
4449 if ((phba->cfg_enable_bg) &&
4450 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4451 ftr_rsp++;
4452
4453 if (phba->max_vpi && phba->cfg_enable_npiv &&
4454 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4455 ftr_rsp++;
4456
4457 if (ftr_rsp) {
4458 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4459 "0379 Feature Mismatch Data: x%08x %08x "
4460 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4461 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4462 phba->cfg_enable_npiv, phba->max_vpi);
4463 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4464 phba->cfg_enable_bg = 0;
4465 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4466 phba->cfg_enable_npiv = 0;
4467 }
4468
4469 /* These SLI3 features are assumed in SLI4 */
4470 spin_lock_irq(&phba->hbalock);
4471 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4472 spin_unlock_irq(&phba->hbalock);
4473
4474 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004475 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4476 if (rc) {
4477 phba->link_state = LPFC_HBA_ERROR;
4478 rc = -ENOMEM;
4479 goto out_free_vpd;
4480 }
4481
James Smartda0436e2009-05-22 14:51:39 -04004482 mboxq->vport = vport;
4483 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4484 mp = (struct lpfc_dmabuf *) mboxq->context1;
4485 if (rc == MBX_SUCCESS) {
4486 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4487 rc = 0;
4488 }
4489
4490 /*
4491 * This memory was allocated by the lpfc_read_sparam routine. Release
4492 * it to the mbuf pool.
4493 */
4494 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4495 kfree(mp);
4496 mboxq->context1 = NULL;
4497 if (unlikely(rc)) {
4498 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4499 "0382 READ_SPARAM command failed "
4500 "status %d, mbxStatus x%x\n",
4501 rc, bf_get(lpfc_mqe_status, mqe));
4502 phba->link_state = LPFC_HBA_ERROR;
4503 rc = -EIO;
4504 goto out_free_vpd;
4505 }
4506
4507 if (phba->cfg_soft_wwnn)
4508 u64_to_wwn(phba->cfg_soft_wwnn,
4509 vport->fc_sparam.nodeName.u.wwn);
4510 if (phba->cfg_soft_wwpn)
4511 u64_to_wwn(phba->cfg_soft_wwpn,
4512 vport->fc_sparam.portName.u.wwn);
4513 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4514 sizeof(struct lpfc_name));
4515 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4516 sizeof(struct lpfc_name));
4517
4518 /* Update the fc_host data structures with new wwn. */
4519 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4520 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4521
4522 /* Register SGL pool to the device using non-embedded mailbox command */
4523 rc = lpfc_sli4_post_sgl_list(phba);
4524 if (unlikely(rc)) {
4525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004526 "0582 Error %d during sgl post operation\n",
4527 rc);
James Smartda0436e2009-05-22 14:51:39 -04004528 rc = -ENODEV;
4529 goto out_free_vpd;
4530 }
4531
4532 /* Register SCSI SGL pool to the device */
4533 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4534 if (unlikely(rc)) {
4535 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004536 "0383 Error %d during scsi sgl post "
4537 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004538 /* Some Scsi buffers were moved to the abort scsi list */
4539 /* A pci function reset will repost them */
4540 rc = -ENODEV;
4541 goto out_free_vpd;
4542 }
4543
4544 /* Post the rpi header region to the device. */
4545 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4546 if (unlikely(rc)) {
4547 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4548 "0393 Error %d during rpi post operation\n",
4549 rc);
4550 rc = -ENODEV;
4551 goto out_free_vpd;
4552 }
James Smartda0436e2009-05-22 14:51:39 -04004553
4554 /* Set up all the queues to the device */
4555 rc = lpfc_sli4_queue_setup(phba);
4556 if (unlikely(rc)) {
4557 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4558 "0381 Error %d during queue setup.\n ", rc);
4559 goto out_stop_timers;
4560 }
4561
4562 /* Arm the CQs and then EQs on device */
4563 lpfc_sli4_arm_cqeq_intr(phba);
4564
4565 /* Indicate device interrupt mode */
4566 phba->sli4_hba.intr_enable = 1;
4567
4568 /* Allow asynchronous mailbox command to go through */
4569 spin_lock_irq(&phba->hbalock);
4570 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4571 spin_unlock_irq(&phba->hbalock);
4572
4573 /* Post receive buffers to the device */
4574 lpfc_sli4_rb_setup(phba);
4575
James Smartfc2b9892010-02-26 14:15:29 -05004576 /* Reset HBA FCF states after HBA reset */
4577 phba->fcf.fcf_flag = 0;
4578 phba->fcf.current_rec.flag = 0;
4579
James Smartda0436e2009-05-22 14:51:39 -04004580 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004581 mod_timer(&vport->els_tmofunc,
4582 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004583
4584 /* Start heart beat timer */
4585 mod_timer(&phba->hb_tmofunc,
4586 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4587 phba->hb_outstanding = 0;
4588 phba->last_completion_time = jiffies;
4589
4590 /* Start error attention (ERATT) polling timer */
4591 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4592
James Smart75baf692010-06-08 18:31:21 -04004593 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4594 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4595 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4596 if (!rc) {
4597 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4598 "2829 This device supports "
4599 "Advanced Error Reporting (AER)\n");
4600 spin_lock_irq(&phba->hbalock);
4601 phba->hba_flag |= HBA_AER_ENABLED;
4602 spin_unlock_irq(&phba->hbalock);
4603 } else {
4604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4605 "2830 This device does not support "
4606 "Advanced Error Reporting (AER)\n");
4607 phba->cfg_aer_support = 0;
4608 }
4609 }
4610
James Smartda0436e2009-05-22 14:51:39 -04004611 /*
4612 * The port is ready, set the host's link state to LINK_DOWN
4613 * in preparation for link interrupts.
4614 */
4615 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4616 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4617 lpfc_set_loopback_flag(phba);
4618 /* Change driver state to LPFC_LINK_DOWN right before init link */
4619 spin_lock_irq(&phba->hbalock);
4620 phba->link_state = LPFC_LINK_DOWN;
4621 spin_unlock_irq(&phba->hbalock);
4622 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4623 if (unlikely(rc != MBX_NOT_FINISHED)) {
4624 kfree(vpd);
4625 return 0;
4626 } else
4627 rc = -EIO;
4628
4629 /* Unset all the queues set up in this routine when error out */
4630 if (rc)
4631 lpfc_sli4_queue_unset(phba);
4632
4633out_stop_timers:
4634 if (rc)
4635 lpfc_stop_hba_timers(phba);
4636out_free_vpd:
4637 kfree(vpd);
4638out_free_mbox:
4639 mempool_free(mboxq, phba->mbox_mem_pool);
4640 return rc;
4641}
James Smarte59058c2008-08-24 21:49:00 -04004642
4643/**
James Smart3621a712009-04-06 18:47:14 -04004644 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004645 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004646 *
James Smarte59058c2008-08-24 21:49:00 -04004647 * This is the callback function for mailbox timer. The mailbox
4648 * timer is armed when a new mailbox command is issued and the timer
4649 * is deleted when the mailbox complete. The function is called by
4650 * the kernel timer code when a mailbox does not complete within
4651 * expected time. This function wakes up the worker thread to
4652 * process the mailbox timeout and returns. All the processing is
4653 * done by the worker thread function lpfc_mbox_timeout_handler.
4654 **/
dea31012005-04-17 16:05:31 -05004655void
4656lpfc_mbox_timeout(unsigned long ptr)
4657{
James Smart92d7f7b2007-06-17 19:56:38 -05004658 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004659 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004660 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004661
James Smart2e0fef82007-06-17 19:56:36 -05004662 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004663 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004664 if (!tmo_posted)
4665 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4666 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4667
James Smart5e9d9b82008-06-14 22:52:53 -04004668 if (!tmo_posted)
4669 lpfc_worker_wake_up(phba);
4670 return;
dea31012005-04-17 16:05:31 -05004671}
4672
James Smarte59058c2008-08-24 21:49:00 -04004673
4674/**
James Smart3621a712009-04-06 18:47:14 -04004675 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004676 * @phba: Pointer to HBA context object.
4677 *
4678 * This function is called from worker thread when a mailbox command times out.
4679 * The caller is not required to hold any locks. This function will reset the
4680 * HBA and recover all the pending commands.
4681 **/
dea31012005-04-17 16:05:31 -05004682void
4683lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4684{
James Smart2e0fef82007-06-17 19:56:36 -05004685 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004686 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004687 struct lpfc_sli *psli = &phba->sli;
4688 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004689
James Smarta257bf92009-04-06 18:48:10 -04004690 /* Check the pmbox pointer first. There is a race condition
4691 * between the mbox timeout handler getting executed in the
4692 * worklist and the mailbox actually completing. When this
4693 * race condition occurs, the mbox_active will be NULL.
4694 */
4695 spin_lock_irq(&phba->hbalock);
4696 if (pmbox == NULL) {
4697 lpfc_printf_log(phba, KERN_WARNING,
4698 LOG_MBOX | LOG_SLI,
4699 "0353 Active Mailbox cleared - mailbox timeout "
4700 "exiting\n");
4701 spin_unlock_irq(&phba->hbalock);
4702 return;
4703 }
4704
dea31012005-04-17 16:05:31 -05004705 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004706 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004707 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004708 mb->mbxCommand,
4709 phba->pport->port_state,
4710 phba->sli.sli_flag,
4711 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004712 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004713
James Smart1dcb58e2007-04-25 09:51:30 -04004714 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4715 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4716 * it to fail all oustanding SCSI IO.
4717 */
James Smart2e0fef82007-06-17 19:56:36 -05004718 spin_lock_irq(&phba->pport->work_port_lock);
4719 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4720 spin_unlock_irq(&phba->pport->work_port_lock);
4721 spin_lock_irq(&phba->hbalock);
4722 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004723 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004724 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004725
4726 pring = &psli->ring[psli->fcp_ring];
4727 lpfc_sli_abort_iocb_ring(phba, pring);
4728
4729 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004730 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004731
4732 /* Reset the HBA device */
4733 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004734}
4735
James Smarte59058c2008-08-24 21:49:00 -04004736/**
James Smart3772a992009-05-22 14:50:54 -04004737 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004738 * @phba: Pointer to HBA context object.
4739 * @pmbox: Pointer to mailbox object.
4740 * @flag: Flag indicating how the mailbox need to be processed.
4741 *
4742 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004743 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4744 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004745 * The mailbox command can be submitted in polling mode, in which case
4746 * this function will wait in a polling loop for the completion of the
4747 * mailbox.
4748 * If the mailbox is submitted in no_wait mode (not polling) the
4749 * function will submit the command and returns immediately without waiting
4750 * for the mailbox completion. The no_wait is supported only when HBA
4751 * is in SLI2/SLI3 mode - interrupts are enabled.
4752 * The SLI interface allows only one mailbox pending at a time. If the
4753 * mailbox is issued in polling mode and there is already a mailbox
4754 * pending, then the function will return an error. If the mailbox is issued
4755 * in NO_WAIT mode and there is a mailbox pending already, the function
4756 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4757 * The sli layer owns the mailbox object until the completion of mailbox
4758 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4759 * return codes the caller owns the mailbox command after the return of
4760 * the function.
4761 **/
James Smart3772a992009-05-22 14:50:54 -04004762static int
4763lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4764 uint32_t flag)
dea31012005-04-17 16:05:31 -05004765{
dea31012005-04-17 16:05:31 -05004766 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004767 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004768 uint32_t status, evtctr;
4769 uint32_t ha_copy;
4770 int i;
James Smart09372822008-01-11 01:52:54 -05004771 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004772 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004773 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004774 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004775 int processing_queue = 0;
4776
4777 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4778 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004779 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004780 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004781 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4782 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4783 return MBX_SUCCESS;
4784 }
James Smart58da1ff2008-04-07 10:15:56 -04004785 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004786 pmbox = lpfc_mbox_get(phba);
4787 if (!pmbox) {
4788 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4789 return MBX_SUCCESS;
4790 }
4791 }
dea31012005-04-17 16:05:31 -05004792
James Smarted957682007-06-17 19:56:37 -05004793 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004794 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004795 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004797 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004798 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004799 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004800 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004801 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004802 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004803 }
4804 }
4805
Linas Vepstas8d63f372007-02-14 14:28:36 -06004806 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004807 if (unlikely(pci_channel_offline(phba->pcidev))) {
4808 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4809 goto out_not_finished;
4810 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004811
James Smarta257bf92009-04-06 18:48:10 -04004812 /* If HBA has a deferred error attention, fail the iocb. */
4813 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4814 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4815 goto out_not_finished;
4816 }
4817
dea31012005-04-17 16:05:31 -05004818 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004819
James Smart3772a992009-05-22 14:50:54 -04004820 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004821 status = MBX_SUCCESS;
4822
James Smart2e0fef82007-06-17 19:56:36 -05004823 if (phba->link_state == LPFC_HBA_ERROR) {
4824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004825
4826 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4828 "(%d):0311 Mailbox command x%x cannot "
4829 "issue Data: x%x x%x\n",
4830 pmbox->vport ? pmbox->vport->vpi : 0,
4831 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004832 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004833 }
4834
James Smart92908312006-03-07 15:04:13 -05004835 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4836 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004838 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4839 "(%d):2528 Mailbox command x%x cannot "
4840 "issue Data: x%x x%x\n",
4841 pmbox->vport ? pmbox->vport->vpi : 0,
4842 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004843 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004844 }
4845
dea31012005-04-17 16:05:31 -05004846 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4847 /* Polling for a mbox command when another one is already active
4848 * is not allowed in SLI. Also, the driver must have established
4849 * SLI2 mode to queue and process multiple mbox commands.
4850 */
4851
4852 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004853 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004854
4855 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004856 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4857 "(%d):2529 Mailbox command x%x "
4858 "cannot issue Data: x%x x%x\n",
4859 pmbox->vport ? pmbox->vport->vpi : 0,
4860 pmbox->u.mb.mbxCommand,
4861 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004862 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004863 }
4864
James Smart3772a992009-05-22 14:50:54 -04004865 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004866 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004867 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004868 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4869 "(%d):2530 Mailbox command x%x "
4870 "cannot issue Data: x%x x%x\n",
4871 pmbox->vport ? pmbox->vport->vpi : 0,
4872 pmbox->u.mb.mbxCommand,
4873 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004874 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004875 }
4876
dea31012005-04-17 16:05:31 -05004877 /* Another mailbox command is still being processed, queue this
4878 * command to be processed later.
4879 */
4880 lpfc_mbox_put(phba, pmbox);
4881
4882 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004883 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004884 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004885 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004886 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4887 mb->mbxCommand, phba->pport->port_state,
4888 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004889
4890 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004891 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004892
James Smart858c9f62007-06-17 19:56:39 -05004893 if (pmbox->vport) {
4894 lpfc_debugfs_disc_trc(pmbox->vport,
4895 LPFC_DISC_TRC_MBOX_VPORT,
4896 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4897 (uint32_t)mb->mbxCommand,
4898 mb->un.varWords[0], mb->un.varWords[1]);
4899 }
4900 else {
4901 lpfc_debugfs_disc_trc(phba->pport,
4902 LPFC_DISC_TRC_MBOX,
4903 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4904 (uint32_t)mb->mbxCommand,
4905 mb->un.varWords[0], mb->un.varWords[1]);
4906 }
4907
James Smart2e0fef82007-06-17 19:56:36 -05004908 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004909 }
4910
dea31012005-04-17 16:05:31 -05004911 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4912
4913 /* If we are not polling, we MUST be in SLI2 mode */
4914 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004915 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004916 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004917 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004918 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004919 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004920 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4921 "(%d):2531 Mailbox command x%x "
4922 "cannot issue Data: x%x x%x\n",
4923 pmbox->vport ? pmbox->vport->vpi : 0,
4924 pmbox->u.mb.mbxCommand,
4925 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004926 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004927 }
4928 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004929 mod_timer(&psli->mbox_tmo, (jiffies +
4930 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004931 }
4932
4933 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004934 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004935 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004936 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004937 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004938 mb->mbxCommand, phba->pport->port_state,
4939 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004940
James Smart858c9f62007-06-17 19:56:39 -05004941 if (mb->mbxCommand != MBX_HEARTBEAT) {
4942 if (pmbox->vport) {
4943 lpfc_debugfs_disc_trc(pmbox->vport,
4944 LPFC_DISC_TRC_MBOX_VPORT,
4945 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4946 (uint32_t)mb->mbxCommand,
4947 mb->un.varWords[0], mb->un.varWords[1]);
4948 }
4949 else {
4950 lpfc_debugfs_disc_trc(phba->pport,
4951 LPFC_DISC_TRC_MBOX,
4952 "MBOX Send: cmd:x%x mb:x%x x%x",
4953 (uint32_t)mb->mbxCommand,
4954 mb->un.varWords[0], mb->un.varWords[1]);
4955 }
4956 }
4957
dea31012005-04-17 16:05:31 -05004958 psli->slistat.mbox_cmd++;
4959 evtctr = psli->slistat.mbox_event;
4960
4961 /* next set own bit for the adapter and copy over command word */
4962 mb->mbxOwner = OWN_CHIP;
4963
James Smart3772a992009-05-22 14:50:54 -04004964 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004965 /* Populate mbox extension offset word. */
4966 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4967 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4968 = (uint8_t *)phba->mbox_ext
4969 - (uint8_t *)phba->mbox;
4970 }
4971
4972 /* Copy the mailbox extension data */
4973 if (pmbox->in_ext_byte_len && pmbox->context2) {
4974 lpfc_sli_pcimem_bcopy(pmbox->context2,
4975 (uint8_t *)phba->mbox_ext,
4976 pmbox->in_ext_byte_len);
4977 }
4978 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004979 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004980 } else {
James Smart7a470272010-03-15 11:25:20 -04004981 /* Populate mbox extension offset word. */
4982 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4983 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4984 = MAILBOX_HBA_EXT_OFFSET;
4985
4986 /* Copy the mailbox extension data */
4987 if (pmbox->in_ext_byte_len && pmbox->context2) {
4988 lpfc_memcpy_to_slim(phba->MBslimaddr +
4989 MAILBOX_HBA_EXT_OFFSET,
4990 pmbox->context2, pmbox->in_ext_byte_len);
4991
4992 }
James Smart92908312006-03-07 15:04:13 -05004993 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004994 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004995 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004996 }
4997
4998 /* First copy mbox command data to HBA SLIM, skip past first
4999 word */
5000 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5001 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5002 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5003
5004 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005005 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005006 to_slim = phba->MBslimaddr;
5007 writel(ldata, to_slim);
5008 readl(to_slim); /* flush */
5009
5010 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5011 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005012 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005013 }
5014 }
5015
5016 wmb();
dea31012005-04-17 16:05:31 -05005017
5018 switch (flag) {
5019 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005020 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005021 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005022 /* Interrupt board to do it */
5023 writel(CA_MBATT, phba->CAregaddr);
5024 readl(phba->CAregaddr); /* flush */
5025 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005026 break;
5027
5028 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005029 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005030 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005031 /* Interrupt board to do it */
5032 writel(CA_MBATT, phba->CAregaddr);
5033 readl(phba->CAregaddr); /* flush */
5034
James Smart3772a992009-05-22 14:50:54 -04005035 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005036 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005037 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005038 word0 = le32_to_cpu(word0);
5039 } else {
5040 /* First read mbox status word */
5041 word0 = readl(phba->MBslimaddr);
5042 }
5043
5044 /* Read the HBA Host Attention Register */
5045 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005046 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5047 mb->mbxCommand) *
5048 1000) + jiffies;
5049 i = 0;
dea31012005-04-17 16:05:31 -05005050 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005051 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5052 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005053 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005054 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005055 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005056 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005057 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005058 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005059 }
5060
5061 /* Check if we took a mbox interrupt while we were
5062 polling */
5063 if (((word0 & OWN_CHIP) != OWN_CHIP)
5064 && (evtctr != psli->slistat.mbox_event))
5065 break;
5066
James Smart09372822008-01-11 01:52:54 -05005067 if (i++ > 10) {
5068 spin_unlock_irqrestore(&phba->hbalock,
5069 drvr_flag);
5070 msleep(1);
5071 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5072 }
dea31012005-04-17 16:05:31 -05005073
James Smart3772a992009-05-22 14:50:54 -04005074 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005075 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005076 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005077 word0 = le32_to_cpu(word0);
5078 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5079 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005080 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005081 /* Check real SLIM for any errors */
5082 slimword0 = readl(phba->MBslimaddr);
5083 slimmb = (MAILBOX_t *) & slimword0;
5084 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5085 && slimmb->mbxStatus) {
5086 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005087 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005088 word0 = slimword0;
5089 }
5090 }
5091 } else {
5092 /* First copy command data */
5093 word0 = readl(phba->MBslimaddr);
5094 }
5095 /* Read the HBA Host Attention Register */
5096 ha_copy = readl(phba->HAregaddr);
5097 }
5098
James Smart3772a992009-05-22 14:50:54 -04005099 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005100 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005101 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005102 /* Copy the mailbox extension data */
5103 if (pmbox->out_ext_byte_len && pmbox->context2) {
5104 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5105 pmbox->context2,
5106 pmbox->out_ext_byte_len);
5107 }
dea31012005-04-17 16:05:31 -05005108 } else {
5109 /* First copy command data */
5110 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5111 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005112 /* Copy the mailbox extension data */
5113 if (pmbox->out_ext_byte_len && pmbox->context2) {
5114 lpfc_memcpy_from_slim(pmbox->context2,
5115 phba->MBslimaddr +
5116 MAILBOX_HBA_EXT_OFFSET,
5117 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005118 }
5119 }
5120
5121 writel(HA_MBATT, phba->HAregaddr);
5122 readl(phba->HAregaddr); /* flush */
5123
5124 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5125 status = mb->mbxStatus;
5126 }
5127
James Smart2e0fef82007-06-17 19:56:36 -05005128 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5129 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005130
5131out_not_finished:
5132 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005133 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005134 lpfc_mbox_cmpl_put(phba, pmbox);
5135 }
5136 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005137}
5138
James Smarte59058c2008-08-24 21:49:00 -04005139/**
James Smartf1126682009-06-10 17:22:44 -04005140 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5141 * @phba: Pointer to HBA context object.
5142 *
5143 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5144 * the driver internal pending mailbox queue. It will then try to wait out the
5145 * possible outstanding mailbox command before return.
5146 *
5147 * Returns:
5148 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5149 * the outstanding mailbox command timed out.
5150 **/
5151static int
5152lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5153{
5154 struct lpfc_sli *psli = &phba->sli;
5155 uint8_t actcmd = MBX_HEARTBEAT;
5156 int rc = 0;
5157 unsigned long timeout;
5158
5159 /* Mark the asynchronous mailbox command posting as blocked */
5160 spin_lock_irq(&phba->hbalock);
5161 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5162 if (phba->sli.mbox_active)
5163 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5164 spin_unlock_irq(&phba->hbalock);
5165 /* Determine how long we might wait for the active mailbox
5166 * command to be gracefully completed by firmware.
5167 */
5168 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5169 jiffies;
5170 /* Wait for the outstnading mailbox command to complete */
5171 while (phba->sli.mbox_active) {
5172 /* Check active mailbox complete status every 2ms */
5173 msleep(2);
5174 if (time_after(jiffies, timeout)) {
5175 /* Timeout, marked the outstanding cmd not complete */
5176 rc = 1;
5177 break;
5178 }
5179 }
5180
5181 /* Can not cleanly block async mailbox command, fails it */
5182 if (rc) {
5183 spin_lock_irq(&phba->hbalock);
5184 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5185 spin_unlock_irq(&phba->hbalock);
5186 }
5187 return rc;
5188}
5189
5190/**
5191 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5192 * @phba: Pointer to HBA context object.
5193 *
5194 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5195 * commands from the driver internal pending mailbox queue. It makes sure
5196 * that there is no outstanding mailbox command before resuming posting
5197 * asynchronous mailbox commands. If, for any reason, there is outstanding
5198 * mailbox command, it will try to wait it out before resuming asynchronous
5199 * mailbox command posting.
5200 **/
5201static void
5202lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5203{
5204 struct lpfc_sli *psli = &phba->sli;
5205
5206 spin_lock_irq(&phba->hbalock);
5207 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5208 /* Asynchronous mailbox posting is not blocked, do nothing */
5209 spin_unlock_irq(&phba->hbalock);
5210 return;
5211 }
5212
5213 /* Outstanding synchronous mailbox command is guaranteed to be done,
5214 * successful or timeout, after timing-out the outstanding mailbox
5215 * command shall always be removed, so just unblock posting async
5216 * mailbox command and resume
5217 */
5218 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5219 spin_unlock_irq(&phba->hbalock);
5220
5221 /* wake up worker thread to post asynchronlous mailbox command */
5222 lpfc_worker_wake_up(phba);
5223}
5224
5225/**
James Smartda0436e2009-05-22 14:51:39 -04005226 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5227 * @phba: Pointer to HBA context object.
5228 * @mboxq: Pointer to mailbox object.
5229 *
5230 * The function posts a mailbox to the port. The mailbox is expected
5231 * to be comletely filled in and ready for the port to operate on it.
5232 * This routine executes a synchronous completion operation on the
5233 * mailbox by polling for its completion.
5234 *
5235 * The caller must not be holding any locks when calling this routine.
5236 *
5237 * Returns:
5238 * MBX_SUCCESS - mailbox posted successfully
5239 * Any of the MBX error values.
5240 **/
5241static int
5242lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5243{
5244 int rc = MBX_SUCCESS;
5245 unsigned long iflag;
5246 uint32_t db_ready;
5247 uint32_t mcqe_status;
5248 uint32_t mbx_cmnd;
5249 unsigned long timeout;
5250 struct lpfc_sli *psli = &phba->sli;
5251 struct lpfc_mqe *mb = &mboxq->u.mqe;
5252 struct lpfc_bmbx_create *mbox_rgn;
5253 struct dma_address *dma_address;
5254 struct lpfc_register bmbx_reg;
5255
5256 /*
5257 * Only one mailbox can be active to the bootstrap mailbox region
5258 * at a time and there is no queueing provided.
5259 */
5260 spin_lock_irqsave(&phba->hbalock, iflag);
5261 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5262 spin_unlock_irqrestore(&phba->hbalock, iflag);
5263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5264 "(%d):2532 Mailbox command x%x (x%x) "
5265 "cannot issue Data: x%x x%x\n",
5266 mboxq->vport ? mboxq->vport->vpi : 0,
5267 mboxq->u.mb.mbxCommand,
5268 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5269 psli->sli_flag, MBX_POLL);
5270 return MBXERR_ERROR;
5271 }
5272 /* The server grabs the token and owns it until release */
5273 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5274 phba->sli.mbox_active = mboxq;
5275 spin_unlock_irqrestore(&phba->hbalock, iflag);
5276
5277 /*
5278 * Initialize the bootstrap memory region to avoid stale data areas
5279 * in the mailbox post. Then copy the caller's mailbox contents to
5280 * the bmbx mailbox region.
5281 */
5282 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5283 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5284 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5285 sizeof(struct lpfc_mqe));
5286
5287 /* Post the high mailbox dma address to the port and wait for ready. */
5288 dma_address = &phba->sli4_hba.bmbx.dma_address;
5289 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5290
5291 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5292 * 1000) + jiffies;
5293 do {
5294 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5295 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5296 if (!db_ready)
5297 msleep(2);
5298
5299 if (time_after(jiffies, timeout)) {
5300 rc = MBXERR_ERROR;
5301 goto exit;
5302 }
5303 } while (!db_ready);
5304
5305 /* Post the low mailbox dma address to the port. */
5306 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5307 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5308 * 1000) + jiffies;
5309 do {
5310 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5311 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5312 if (!db_ready)
5313 msleep(2);
5314
5315 if (time_after(jiffies, timeout)) {
5316 rc = MBXERR_ERROR;
5317 goto exit;
5318 }
5319 } while (!db_ready);
5320
5321 /*
5322 * Read the CQ to ensure the mailbox has completed.
5323 * If so, update the mailbox status so that the upper layers
5324 * can complete the request normally.
5325 */
5326 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5327 sizeof(struct lpfc_mqe));
5328 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5329 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5330 sizeof(struct lpfc_mcqe));
5331 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5332
5333 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5334 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5335 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5336 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005337 } else
5338 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005339
5340 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5341 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5342 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5343 " x%x x%x CQ: x%x x%x x%x x%x\n",
5344 mboxq->vport ? mboxq->vport->vpi : 0,
5345 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5346 bf_get(lpfc_mqe_status, mb),
5347 mb->un.mb_words[0], mb->un.mb_words[1],
5348 mb->un.mb_words[2], mb->un.mb_words[3],
5349 mb->un.mb_words[4], mb->un.mb_words[5],
5350 mb->un.mb_words[6], mb->un.mb_words[7],
5351 mb->un.mb_words[8], mb->un.mb_words[9],
5352 mb->un.mb_words[10], mb->un.mb_words[11],
5353 mb->un.mb_words[12], mboxq->mcqe.word0,
5354 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5355 mboxq->mcqe.trailer);
5356exit:
5357 /* We are holding the token, no needed for lock when release */
5358 spin_lock_irqsave(&phba->hbalock, iflag);
5359 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5360 phba->sli.mbox_active = NULL;
5361 spin_unlock_irqrestore(&phba->hbalock, iflag);
5362 return rc;
5363}
5364
5365/**
5366 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5367 * @phba: Pointer to HBA context object.
5368 * @pmbox: Pointer to mailbox object.
5369 * @flag: Flag indicating how the mailbox need to be processed.
5370 *
5371 * This function is called by discovery code and HBA management code to submit
5372 * a mailbox command to firmware with SLI-4 interface spec.
5373 *
5374 * Return codes the caller owns the mailbox command after the return of the
5375 * function.
5376 **/
5377static int
5378lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5379 uint32_t flag)
5380{
5381 struct lpfc_sli *psli = &phba->sli;
5382 unsigned long iflags;
5383 int rc;
5384
James Smart8fa38512009-07-19 10:01:03 -04005385 rc = lpfc_mbox_dev_check(phba);
5386 if (unlikely(rc)) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5388 "(%d):2544 Mailbox command x%x (x%x) "
5389 "cannot issue Data: x%x x%x\n",
5390 mboxq->vport ? mboxq->vport->vpi : 0,
5391 mboxq->u.mb.mbxCommand,
5392 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5393 psli->sli_flag, flag);
5394 goto out_not_finished;
5395 }
5396
James Smartda0436e2009-05-22 14:51:39 -04005397 /* Detect polling mode and jump to a handler */
5398 if (!phba->sli4_hba.intr_enable) {
5399 if (flag == MBX_POLL)
5400 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5401 else
5402 rc = -EIO;
5403 if (rc != MBX_SUCCESS)
5404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5405 "(%d):2541 Mailbox command x%x "
5406 "(x%x) cannot issue Data: x%x x%x\n",
5407 mboxq->vport ? mboxq->vport->vpi : 0,
5408 mboxq->u.mb.mbxCommand,
5409 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5410 psli->sli_flag, flag);
5411 return rc;
5412 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005413 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5414 "(%d):2542 Try to issue mailbox command "
5415 "x%x (x%x) synchronously ahead of async"
5416 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005417 mboxq->vport ? mboxq->vport->vpi : 0,
5418 mboxq->u.mb.mbxCommand,
5419 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5420 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005421 /* Try to block the asynchronous mailbox posting */
5422 rc = lpfc_sli4_async_mbox_block(phba);
5423 if (!rc) {
5424 /* Successfully blocked, now issue sync mbox cmd */
5425 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5426 if (rc != MBX_SUCCESS)
5427 lpfc_printf_log(phba, KERN_ERR,
5428 LOG_MBOX | LOG_SLI,
5429 "(%d):2597 Mailbox command "
5430 "x%x (x%x) cannot issue "
5431 "Data: x%x x%x\n",
5432 mboxq->vport ?
5433 mboxq->vport->vpi : 0,
5434 mboxq->u.mb.mbxCommand,
5435 lpfc_sli4_mbox_opcode_get(phba,
5436 mboxq),
5437 psli->sli_flag, flag);
5438 /* Unblock the async mailbox posting afterward */
5439 lpfc_sli4_async_mbox_unblock(phba);
5440 }
5441 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005442 }
5443
5444 /* Now, interrupt mode asynchrous mailbox command */
5445 rc = lpfc_mbox_cmd_check(phba, mboxq);
5446 if (rc) {
5447 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5448 "(%d):2543 Mailbox command x%x (x%x) "
5449 "cannot issue Data: x%x x%x\n",
5450 mboxq->vport ? mboxq->vport->vpi : 0,
5451 mboxq->u.mb.mbxCommand,
5452 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5453 psli->sli_flag, flag);
5454 goto out_not_finished;
5455 }
James Smartda0436e2009-05-22 14:51:39 -04005456
5457 /* Put the mailbox command to the driver internal FIFO */
5458 psli->slistat.mbox_busy++;
5459 spin_lock_irqsave(&phba->hbalock, iflags);
5460 lpfc_mbox_put(phba, mboxq);
5461 spin_unlock_irqrestore(&phba->hbalock, iflags);
5462 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5463 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5464 "x%x (x%x) x%x x%x x%x\n",
5465 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5466 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5467 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5468 phba->pport->port_state,
5469 psli->sli_flag, MBX_NOWAIT);
5470 /* Wake up worker thread to transport mailbox command from head */
5471 lpfc_worker_wake_up(phba);
5472
5473 return MBX_BUSY;
5474
5475out_not_finished:
5476 return MBX_NOT_FINISHED;
5477}
5478
5479/**
5480 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5481 * @phba: Pointer to HBA context object.
5482 *
5483 * This function is called by worker thread to send a mailbox command to
5484 * SLI4 HBA firmware.
5485 *
5486 **/
5487int
5488lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5489{
5490 struct lpfc_sli *psli = &phba->sli;
5491 LPFC_MBOXQ_t *mboxq;
5492 int rc = MBX_SUCCESS;
5493 unsigned long iflags;
5494 struct lpfc_mqe *mqe;
5495 uint32_t mbx_cmnd;
5496
5497 /* Check interrupt mode before post async mailbox command */
5498 if (unlikely(!phba->sli4_hba.intr_enable))
5499 return MBX_NOT_FINISHED;
5500
5501 /* Check for mailbox command service token */
5502 spin_lock_irqsave(&phba->hbalock, iflags);
5503 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5504 spin_unlock_irqrestore(&phba->hbalock, iflags);
5505 return MBX_NOT_FINISHED;
5506 }
5507 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5508 spin_unlock_irqrestore(&phba->hbalock, iflags);
5509 return MBX_NOT_FINISHED;
5510 }
5511 if (unlikely(phba->sli.mbox_active)) {
5512 spin_unlock_irqrestore(&phba->hbalock, iflags);
5513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5514 "0384 There is pending active mailbox cmd\n");
5515 return MBX_NOT_FINISHED;
5516 }
5517 /* Take the mailbox command service token */
5518 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5519
5520 /* Get the next mailbox command from head of queue */
5521 mboxq = lpfc_mbox_get(phba);
5522
5523 /* If no more mailbox command waiting for post, we're done */
5524 if (!mboxq) {
5525 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5526 spin_unlock_irqrestore(&phba->hbalock, iflags);
5527 return MBX_SUCCESS;
5528 }
5529 phba->sli.mbox_active = mboxq;
5530 spin_unlock_irqrestore(&phba->hbalock, iflags);
5531
5532 /* Check device readiness for posting mailbox command */
5533 rc = lpfc_mbox_dev_check(phba);
5534 if (unlikely(rc))
5535 /* Driver clean routine will clean up pending mailbox */
5536 goto out_not_finished;
5537
5538 /* Prepare the mbox command to be posted */
5539 mqe = &mboxq->u.mqe;
5540 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5541
5542 /* Start timer for the mbox_tmo and log some mailbox post messages */
5543 mod_timer(&psli->mbox_tmo, (jiffies +
5544 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5545
5546 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5547 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5548 "x%x x%x\n",
5549 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5550 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5551 phba->pport->port_state, psli->sli_flag);
5552
5553 if (mbx_cmnd != MBX_HEARTBEAT) {
5554 if (mboxq->vport) {
5555 lpfc_debugfs_disc_trc(mboxq->vport,
5556 LPFC_DISC_TRC_MBOX_VPORT,
5557 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5558 mbx_cmnd, mqe->un.mb_words[0],
5559 mqe->un.mb_words[1]);
5560 } else {
5561 lpfc_debugfs_disc_trc(phba->pport,
5562 LPFC_DISC_TRC_MBOX,
5563 "MBOX Send: cmd:x%x mb:x%x x%x",
5564 mbx_cmnd, mqe->un.mb_words[0],
5565 mqe->un.mb_words[1]);
5566 }
5567 }
5568 psli->slistat.mbox_cmd++;
5569
5570 /* Post the mailbox command to the port */
5571 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5572 if (rc != MBX_SUCCESS) {
5573 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5574 "(%d):2533 Mailbox command x%x (x%x) "
5575 "cannot issue Data: x%x x%x\n",
5576 mboxq->vport ? mboxq->vport->vpi : 0,
5577 mboxq->u.mb.mbxCommand,
5578 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5579 psli->sli_flag, MBX_NOWAIT);
5580 goto out_not_finished;
5581 }
5582
5583 return rc;
5584
5585out_not_finished:
5586 spin_lock_irqsave(&phba->hbalock, iflags);
5587 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5588 __lpfc_mbox_cmpl_put(phba, mboxq);
5589 /* Release the token */
5590 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5591 phba->sli.mbox_active = NULL;
5592 spin_unlock_irqrestore(&phba->hbalock, iflags);
5593
5594 return MBX_NOT_FINISHED;
5595}
5596
5597/**
5598 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5599 * @phba: Pointer to HBA context object.
5600 * @pmbox: Pointer to mailbox object.
5601 * @flag: Flag indicating how the mailbox need to be processed.
5602 *
5603 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5604 * the API jump table function pointer from the lpfc_hba struct.
5605 *
5606 * Return codes the caller owns the mailbox command after the return of the
5607 * function.
5608 **/
5609int
5610lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5611{
5612 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5613}
5614
5615/**
5616 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5617 * @phba: The hba struct for which this call is being executed.
5618 * @dev_grp: The HBA PCI-Device group number.
5619 *
5620 * This routine sets up the mbox interface API function jump table in @phba
5621 * struct.
5622 * Returns: 0 - success, -ENODEV - failure.
5623 **/
5624int
5625lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5626{
5627
5628 switch (dev_grp) {
5629 case LPFC_PCI_DEV_LP:
5630 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5631 phba->lpfc_sli_handle_slow_ring_event =
5632 lpfc_sli_handle_slow_ring_event_s3;
5633 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5634 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5635 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5636 break;
5637 case LPFC_PCI_DEV_OC:
5638 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5639 phba->lpfc_sli_handle_slow_ring_event =
5640 lpfc_sli_handle_slow_ring_event_s4;
5641 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5642 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5643 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5644 break;
5645 default:
5646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5647 "1420 Invalid HBA PCI-device group: 0x%x\n",
5648 dev_grp);
5649 return -ENODEV;
5650 break;
5651 }
5652 return 0;
5653}
5654
5655/**
James Smart3621a712009-04-06 18:47:14 -04005656 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005657 * @phba: Pointer to HBA context object.
5658 * @pring: Pointer to driver SLI ring object.
5659 * @piocb: Pointer to address of newly added command iocb.
5660 *
5661 * This function is called with hbalock held to add a command
5662 * iocb to the txq when SLI layer cannot submit the command iocb
5663 * to the ring.
5664 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04005665void
James Smart92d7f7b2007-06-17 19:56:38 -05005666__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005667 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005668{
5669 /* Insert the caller's iocb in the txq tail for later processing. */
5670 list_add_tail(&piocb->list, &pring->txq);
5671 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005672}
5673
James Smarte59058c2008-08-24 21:49:00 -04005674/**
James Smart3621a712009-04-06 18:47:14 -04005675 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005676 * @phba: Pointer to HBA context object.
5677 * @pring: Pointer to driver SLI ring object.
5678 * @piocb: Pointer to address of newly added command iocb.
5679 *
5680 * This function is called with hbalock held before a new
5681 * iocb is submitted to the firmware. This function checks
5682 * txq to flush the iocbs in txq to Firmware before
5683 * submitting new iocbs to the Firmware.
5684 * If there are iocbs in the txq which need to be submitted
5685 * to firmware, lpfc_sli_next_iocb returns the first element
5686 * of the txq after dequeuing it from txq.
5687 * If there is no iocb in the txq then the function will return
5688 * *piocb and *piocb is set to NULL. Caller needs to check
5689 * *piocb to find if there are more commands in the txq.
5690 **/
dea31012005-04-17 16:05:31 -05005691static struct lpfc_iocbq *
5692lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005693 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005694{
5695 struct lpfc_iocbq * nextiocb;
5696
5697 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5698 if (!nextiocb) {
5699 nextiocb = *piocb;
5700 *piocb = NULL;
5701 }
5702
5703 return nextiocb;
5704}
5705
James Smarte59058c2008-08-24 21:49:00 -04005706/**
James Smart3772a992009-05-22 14:50:54 -04005707 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005708 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005709 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005710 * @piocb: Pointer to command iocb.
5711 * @flag: Flag indicating if this command can be put into txq.
5712 *
James Smart3772a992009-05-22 14:50:54 -04005713 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5714 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5715 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5716 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5717 * this function allows only iocbs for posting buffers. This function finds
5718 * next available slot in the command ring and posts the command to the
5719 * available slot and writes the port attention register to request HBA start
5720 * processing new iocb. If there is no slot available in the ring and
5721 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5722 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005723 *
James Smart3772a992009-05-22 14:50:54 -04005724 * This function is called with hbalock held. The function will return success
5725 * after it successfully submit the iocb to firmware or after adding to the
5726 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005727 **/
James Smart98c9ea52007-10-27 13:37:33 -04005728static int
James Smart3772a992009-05-22 14:50:54 -04005729__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005730 struct lpfc_iocbq *piocb, uint32_t flag)
5731{
5732 struct lpfc_iocbq *nextiocb;
5733 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005734 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005735
James Smart92d7f7b2007-06-17 19:56:38 -05005736 if (piocb->iocb_cmpl && (!piocb->vport) &&
5737 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5738 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5739 lpfc_printf_log(phba, KERN_ERR,
5740 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005741 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005742 piocb->iocb.ulpCommand);
5743 dump_stack();
5744 return IOCB_ERROR;
5745 }
5746
5747
Linas Vepstas8d63f372007-02-14 14:28:36 -06005748 /* If the PCI channel is in offline state, do not post iocbs. */
5749 if (unlikely(pci_channel_offline(phba->pcidev)))
5750 return IOCB_ERROR;
5751
James Smarta257bf92009-04-06 18:48:10 -04005752 /* If HBA has a deferred error attention, fail the iocb. */
5753 if (unlikely(phba->hba_flag & DEFER_ERATT))
5754 return IOCB_ERROR;
5755
dea31012005-04-17 16:05:31 -05005756 /*
5757 * We should never get an IOCB if we are in a < LINK_DOWN state
5758 */
James Smart2e0fef82007-06-17 19:56:36 -05005759 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005760 return IOCB_ERROR;
5761
5762 /*
5763 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005764 * outstanding event.
dea31012005-04-17 16:05:31 -05005765 */
James Smart0b727fe2007-10-27 13:37:25 -04005766 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005767 goto iocb_busy;
5768
James Smart2e0fef82007-06-17 19:56:36 -05005769 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005770 /*
James Smart2680eea2007-04-25 09:52:55 -04005771 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005772 * can be issued if the link is not up.
5773 */
5774 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005775 case CMD_GEN_REQUEST64_CR:
5776 case CMD_GEN_REQUEST64_CX:
5777 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5778 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005779 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005780 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5781 MENLO_TRANSPORT_TYPE))
5782
5783 goto iocb_busy;
5784 break;
dea31012005-04-17 16:05:31 -05005785 case CMD_QUE_RING_BUF_CN:
5786 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005787 /*
5788 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5789 * completion, iocb_cmpl MUST be 0.
5790 */
5791 if (piocb->iocb_cmpl)
5792 piocb->iocb_cmpl = NULL;
5793 /*FALLTHROUGH*/
5794 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005795 case CMD_CLOSE_XRI_CN:
5796 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005797 break;
5798 default:
5799 goto iocb_busy;
5800 }
5801
5802 /*
5803 * For FCP commands, we must be in a state where we can process link
5804 * attention events.
5805 */
5806 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005807 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005808 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005809 }
dea31012005-04-17 16:05:31 -05005810
dea31012005-04-17 16:05:31 -05005811 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5812 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5813 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5814
5815 if (iocb)
5816 lpfc_sli_update_ring(phba, pring);
5817 else
5818 lpfc_sli_update_full_ring(phba, pring);
5819
5820 if (!piocb)
5821 return IOCB_SUCCESS;
5822
5823 goto out_busy;
5824
5825 iocb_busy:
5826 pring->stats.iocb_cmd_delay++;
5827
5828 out_busy:
5829
5830 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005831 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005832 return IOCB_SUCCESS;
5833 }
5834
5835 return IOCB_BUSY;
5836}
5837
James Smart3772a992009-05-22 14:50:54 -04005838/**
James Smart4f774512009-05-22 14:52:35 -04005839 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5840 * @phba: Pointer to HBA context object.
5841 * @piocb: Pointer to command iocb.
5842 * @sglq: Pointer to the scatter gather queue object.
5843 *
5844 * This routine converts the bpl or bde that is in the IOCB
5845 * to a sgl list for the sli4 hardware. The physical address
5846 * of the bpl/bde is converted back to a virtual address.
5847 * If the IOCB contains a BPL then the list of BDE's is
5848 * converted to sli4_sge's. If the IOCB contains a single
5849 * BDE then it is converted to a single sli_sge.
5850 * The IOCB is still in cpu endianess so the contents of
5851 * the bpl can be used without byte swapping.
5852 *
5853 * Returns valid XRI = Success, NO_XRI = Failure.
5854**/
5855static uint16_t
5856lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5857 struct lpfc_sglq *sglq)
5858{
5859 uint16_t xritag = NO_XRI;
5860 struct ulp_bde64 *bpl = NULL;
5861 struct ulp_bde64 bde;
5862 struct sli4_sge *sgl = NULL;
5863 IOCB_t *icmd;
5864 int numBdes = 0;
5865 int i = 0;
5866
5867 if (!piocbq || !sglq)
5868 return xritag;
5869
5870 sgl = (struct sli4_sge *)sglq->sgl;
5871 icmd = &piocbq->iocb;
5872 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5873 numBdes = icmd->un.genreq64.bdl.bdeSize /
5874 sizeof(struct ulp_bde64);
5875 /* The addrHigh and addrLow fields within the IOCB
5876 * have not been byteswapped yet so there is no
5877 * need to swap them back.
5878 */
5879 bpl = (struct ulp_bde64 *)
5880 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5881
5882 if (!bpl)
5883 return xritag;
5884
5885 for (i = 0; i < numBdes; i++) {
5886 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005887 sgl->addr_hi = bpl->addrHigh;
5888 sgl->addr_lo = bpl->addrLow;
5889
James Smart4f774512009-05-22 14:52:35 -04005890 if ((i+1) == numBdes)
5891 bf_set(lpfc_sli4_sge_last, sgl, 1);
5892 else
5893 bf_set(lpfc_sli4_sge_last, sgl, 0);
5894 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005895 /* swap the size field back to the cpu so we
5896 * can assign it to the sgl.
5897 */
5898 bde.tus.w = le32_to_cpu(bpl->tus.w);
5899 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005900 bpl++;
5901 sgl++;
5902 }
5903 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5904 /* The addrHigh and addrLow fields of the BDE have not
5905 * been byteswapped yet so they need to be swapped
5906 * before putting them in the sgl.
5907 */
5908 sgl->addr_hi =
5909 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5910 sgl->addr_lo =
5911 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005912 bf_set(lpfc_sli4_sge_last, sgl, 1);
5913 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005914 sgl->sge_len =
5915 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005916 }
5917 return sglq->sli4_xritag;
5918}
5919
5920/**
5921 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5922 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005923 *
James Smarta93ff372010-10-22 11:06:08 -04005924 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005925 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5926 * held.
James Smart4f774512009-05-22 14:52:35 -04005927 *
5928 * Return: index into SLI4 fast-path FCP queue index.
5929 **/
5930static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005931lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005932{
James Smart8fa38512009-07-19 10:01:03 -04005933 ++phba->fcp_qidx;
5934 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5935 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005936
James Smart8fa38512009-07-19 10:01:03 -04005937 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005938}
5939
5940/**
5941 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5942 * @phba: Pointer to HBA context object.
5943 * @piocb: Pointer to command iocb.
5944 * @wqe: Pointer to the work queue entry.
5945 *
5946 * This routine converts the iocb command to its Work Queue Entry
5947 * equivalent. The wqe pointer should not have any fields set when
5948 * this routine is called because it will memcpy over them.
5949 * This routine does not set the CQ_ID or the WQEC bits in the
5950 * wqe.
5951 *
5952 * Returns: 0 = Success, IOCB_ERROR = Failure.
5953 **/
5954static int
5955lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5956 union lpfc_wqe *wqe)
5957{
James Smart5ffc2662009-11-18 15:39:44 -05005958 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005959 uint8_t ct = 0;
5960 uint32_t fip;
5961 uint32_t abort_tag;
5962 uint8_t command_type = ELS_COMMAND_NON_FIP;
5963 uint8_t cmnd;
5964 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04005965 uint16_t abrt_iotag;
5966 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04005967 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005968 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005969 int numBdes, i;
5970 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005971
James Smart45ed1192009-10-02 15:17:02 -04005972 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005973 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005974 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005975 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005976 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005977 command_type = ELS_COMMAND_FIP;
5978 else
5979 command_type = ELS_COMMAND_NON_FIP;
5980
James Smart4f774512009-05-22 14:52:35 -04005981 /* Some of the fields are in the right position already */
5982 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5983 abort_tag = (uint32_t) iocbq->iotag;
5984 xritag = iocbq->sli4_xritag;
5985 wqe->words[7] = 0; /* The ct field has moved so reset */
5986 /* words0-2 bpl convert bde */
5987 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005988 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5989 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005990 bpl = (struct ulp_bde64 *)
5991 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5992 if (!bpl)
5993 return IOCB_ERROR;
5994
5995 /* Should already be byte swapped. */
5996 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5997 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5998 /* swap the size field back to the cpu so we
5999 * can assign it to the sgl.
6000 */
6001 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006002 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6003 total_len = 0;
6004 for (i = 0; i < numBdes; i++) {
6005 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6006 total_len += bde.tus.f.bdeSize;
6007 }
James Smart4f774512009-05-22 14:52:35 -04006008 } else
James Smart5ffc2662009-11-18 15:39:44 -05006009 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006010
6011 iocbq->iocb.ulpIoTag = iocbq->iotag;
6012 cmnd = iocbq->iocb.ulpCommand;
6013
6014 switch (iocbq->iocb.ulpCommand) {
6015 case CMD_ELS_REQUEST64_CR:
6016 if (!iocbq->iocb.ulpLe) {
6017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6018 "2007 Only Limited Edition cmd Format"
6019 " supported 0x%x\n",
6020 iocbq->iocb.ulpCommand);
6021 return IOCB_ERROR;
6022 }
James Smart5ffc2662009-11-18 15:39:44 -05006023 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006024 /* Els_reguest64 has a TMO */
6025 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6026 iocbq->iocb.ulpTimeout);
6027 /* Need a VF for word 4 set the vf bit*/
6028 bf_set(els_req64_vf, &wqe->els_req, 0);
6029 /* And a VFID for word 12 */
6030 bf_set(els_req64_vfid, &wqe->els_req, 0);
6031 /*
6032 * Set ct field to 3, indicates that the context_tag field
6033 * contains the FCFI and remote N_Port_ID is
6034 * in word 5.
6035 */
6036
6037 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6038 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6039 iocbq->iocb.ulpContext);
6040
James Smart4f774512009-05-22 14:52:35 -04006041 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6042 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6043 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006044
6045 if (command_type == ELS_COMMAND_FIP) {
6046 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6047 >> LPFC_FIP_ELS_ID_SHIFT);
6048 }
6049 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6050
James Smart4f774512009-05-22 14:52:35 -04006051 break;
James Smart5ffc2662009-11-18 15:39:44 -05006052 case CMD_XMIT_SEQUENCE64_CX:
6053 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6054 iocbq->iocb.un.ulpWord[3]);
6055 wqe->generic.word3 = 0;
6056 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006057 /* The entire sequence is transmitted for this IOCB */
6058 xmit_len = total_len;
6059 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006060 case CMD_XMIT_SEQUENCE64_CR:
6061 /* word3 iocb=io_tag32 wqe=payload_offset */
6062 /* payload offset used for multilpe outstanding
6063 * sequences on the same exchange
6064 */
6065 wqe->words[3] = 0;
6066 /* word4 relative_offset memcpy */
6067 /* word5 r_ctl/df_ctl memcpy */
6068 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006069 wqe->xmit_sequence.xmit_len = xmit_len;
6070 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006071 break;
6072 case CMD_XMIT_BCAST64_CN:
6073 /* word3 iocb=iotag32 wqe=payload_len */
6074 wqe->words[3] = 0; /* no definition for this in wqe */
6075 /* word4 iocb=rsvd wqe=rsvd */
6076 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6077 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6078 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6079 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6080 break;
6081 case CMD_FCP_IWRITE64_CR:
6082 command_type = FCP_COMMAND_DATA_OUT;
6083 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6084 * confusing.
6085 * word3 is payload_len: byte offset to the sgl entry for the
6086 * fcp_command.
6087 * word4 is total xfer len, same as the IOCB->ulpParameter.
6088 * word5 is initial xfer len 0 = wait for xfer-ready
6089 */
6090
6091 /* Always wait for xfer-ready before sending data */
6092 wqe->fcp_iwrite.initial_xfer_len = 0;
6093 /* word 4 (xfer length) should have been set on the memcpy */
6094
6095 /* allow write to fall through to read */
6096 case CMD_FCP_IREAD64_CR:
6097 /* FCP_CMD is always the 1st sgl entry */
6098 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05006099 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04006100
6101 /* word 4 (xfer length) should have been set on the memcpy */
6102
6103 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6104 iocbq->iocb.ulpFCP2Rcvy);
6105 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6106 /* The XC bit and the XS bit are similar. The driver never
6107 * tracked whether or not the exchange was previouslly open.
6108 * XC = Exchange create, 0 is create. 1 is already open.
6109 * XS = link cmd: 1 do not close the exchange after command.
6110 * XS = 0 close exchange when command completes.
6111 * The only time we would not set the XC bit is when the XS bit
6112 * is set and we are sending our 2nd or greater command on
6113 * this exchange.
6114 */
James Smart4f774512009-05-22 14:52:35 -04006115 /* Always open the exchange */
6116 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6117
6118 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6119 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006120 break;
6121 case CMD_FCP_ICMND64_CR:
6122 /* Always open the exchange */
6123 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6124
6125 wqe->words[4] = 0;
6126 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6127 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006128 break;
6129 case CMD_GEN_REQUEST64_CR:
6130 /* word3 command length is described as byte offset to the
6131 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6132 * sgl[0] = cmnd
6133 * sgl[1] = rsp.
6134 *
6135 */
James Smart5ffc2662009-11-18 15:39:44 -05006136 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006137 /* Word4 parameter copied in the memcpy */
6138 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6139 /* word6 context tag copied in memcpy */
6140 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6141 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6143 "2015 Invalid CT %x command 0x%x\n",
6144 ct, iocbq->iocb.ulpCommand);
6145 return IOCB_ERROR;
6146 }
6147 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6148 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6149 iocbq->iocb.ulpTimeout);
6150
6151 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6152 command_type = OTHER_COMMAND;
6153 break;
6154 case CMD_XMIT_ELS_RSP64_CX:
6155 /* words0-2 BDE memcpy */
6156 /* word3 iocb=iotag32 wqe=rsvd */
6157 wqe->words[3] = 0;
6158 /* word4 iocb=did wge=rsvd. */
6159 wqe->words[4] = 0;
6160 /* word5 iocb=rsvd wge=did */
6161 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6162 iocbq->iocb.un.elsreq64.remoteID);
6163
6164 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6165 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6166
6167 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6168 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6169 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6170 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6171 iocbq->vport->vpi + phba->vpi_base);
6172 command_type = OTHER_COMMAND;
6173 break;
6174 case CMD_CLOSE_XRI_CN:
6175 case CMD_ABORT_XRI_CN:
6176 case CMD_ABORT_XRI_CX:
6177 /* words 0-2 memcpy should be 0 rserved */
6178 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006179 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6180 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6181 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6182 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6183 } else
6184 fip = 0;
6185
6186 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006187 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006188 * The link is down, or the command was ELS_FIP
6189 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006190 * on the wire.
6191 */
6192 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6193 else
6194 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6195 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006196 wqe->words[5] = 0;
6197 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6198 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6199 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006200 /*
6201 * The abort handler will send us CMD_ABORT_XRI_CN or
6202 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6203 */
6204 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6205 cmnd = CMD_ABORT_XRI_CX;
6206 command_type = OTHER_COMMAND;
6207 xritag = 0;
6208 break;
James Smart6669f9b2009-10-02 15:16:45 -04006209 case CMD_XMIT_BLS_RSP64_CX:
6210 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6211 * we re-construct this WQE here based on information in
6212 * iocbq from scratch.
6213 */
6214 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006215 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006216 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006217 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6218 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6219 LPFC_ABTS_UNSOL_INT) {
6220 /* ABTS sent by initiator to CT exchange, the
6221 * RX_ID field will be filled with the newly
6222 * allocated responder XRI.
6223 */
6224 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6225 iocbq->sli4_xritag);
6226 } else {
6227 /* ABTS sent by responder to CT exchange, the
6228 * RX_ID field will be filled with the responder
6229 * RX_ID from ABTS.
6230 */
6231 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6232 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6233 }
James Smart6669f9b2009-10-02 15:16:45 -04006234 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6235 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6236 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6237 iocbq->iocb.ulpContext);
6238 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6239 command_type = OTHER_COMMAND;
6240 break;
James Smart4f774512009-05-22 14:52:35 -04006241 case CMD_XRI_ABORTED_CX:
6242 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6243 /* words0-2 are all 0's no bde */
6244 /* word3 and word4 are rsvrd */
6245 wqe->words[3] = 0;
6246 wqe->words[4] = 0;
6247 /* word5 iocb=rsvd wge=did */
6248 /* There is no remote port id in the IOCB? */
6249 /* Let this fall through and fail */
6250 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6251 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6252 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6253 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6254 default:
6255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6256 "2014 Invalid command 0x%x\n",
6257 iocbq->iocb.ulpCommand);
6258 return IOCB_ERROR;
6259 break;
6260
6261 }
6262 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6263 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6264 wqe->generic.abort_tag = abort_tag;
6265 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6266 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6267 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6268 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6269
6270 return 0;
6271}
6272
6273/**
6274 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6275 * @phba: Pointer to HBA context object.
6276 * @ring_number: SLI ring number to issue iocb on.
6277 * @piocb: Pointer to command iocb.
6278 * @flag: Flag indicating if this command can be put into txq.
6279 *
6280 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6281 * an iocb command to an HBA with SLI-4 interface spec.
6282 *
6283 * This function is called with hbalock held. The function will return success
6284 * after it successfully submit the iocb to firmware or after adding to the
6285 * txq.
6286 **/
6287static int
6288__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6289 struct lpfc_iocbq *piocb, uint32_t flag)
6290{
6291 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006292 union lpfc_wqe wqe;
6293 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006294
6295 if (piocb->sli4_xritag == NO_XRI) {
6296 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006297 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006298 sglq = NULL;
6299 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006300 if (pring->txq_cnt) {
6301 if (!(flag & SLI_IOCB_RET_IOCB)) {
6302 __lpfc_sli_ringtx_put(phba,
6303 pring, piocb);
6304 return IOCB_SUCCESS;
6305 } else {
6306 return IOCB_BUSY;
6307 }
6308 } else {
James Smart4f774512009-05-22 14:52:35 -04006309 sglq = __lpfc_sli_get_sglq(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04006310 if (!sglq) {
6311 if (!(flag & SLI_IOCB_RET_IOCB)) {
6312 __lpfc_sli_ringtx_put(phba,
6313 pring,
6314 piocb);
6315 return IOCB_SUCCESS;
6316 } else
6317 return IOCB_BUSY;
6318 }
6319 }
James Smart4f774512009-05-22 14:52:35 -04006320 }
6321 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6322 sglq = NULL; /* These IO's already have an XRI and
6323 * a mapped sgl.
6324 */
6325 } else {
6326 /* This is a continuation of a commandi,(CX) so this
6327 * sglq is on the active list
6328 */
6329 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6330 if (!sglq)
6331 return IOCB_ERROR;
6332 }
6333
6334 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006335 piocb->sli4_xritag = sglq->sli4_xritag;
6336
6337 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006338 return IOCB_ERROR;
6339 }
6340
6341 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6342 return IOCB_ERROR;
6343
James Smart341af102010-01-26 23:07:37 -05006344 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6345 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006346 /*
6347 * For FCP command IOCB, get a new WQ index to distribute
6348 * WQE across the WQsr. On the other hand, for abort IOCB,
6349 * it carries the same WQ index to the original command
6350 * IOCB.
6351 */
James Smart341af102010-01-26 23:07:37 -05006352 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006353 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6354 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6355 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006356 return IOCB_ERROR;
6357 } else {
6358 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6359 return IOCB_ERROR;
6360 }
6361 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6362
6363 return 0;
6364}
6365
6366/**
James Smart3772a992009-05-22 14:50:54 -04006367 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6368 *
6369 * This routine wraps the actual lockless version for issusing IOCB function
6370 * pointer from the lpfc_hba struct.
6371 *
6372 * Return codes:
6373 * IOCB_ERROR - Error
6374 * IOCB_SUCCESS - Success
6375 * IOCB_BUSY - Busy
6376 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006377int
James Smart3772a992009-05-22 14:50:54 -04006378__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6379 struct lpfc_iocbq *piocb, uint32_t flag)
6380{
6381 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6382}
6383
6384/**
6385 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6386 * @phba: The hba struct for which this call is being executed.
6387 * @dev_grp: The HBA PCI-Device group number.
6388 *
6389 * This routine sets up the SLI interface API function jump table in @phba
6390 * struct.
6391 * Returns: 0 - success, -ENODEV - failure.
6392 **/
6393int
6394lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6395{
6396
6397 switch (dev_grp) {
6398 case LPFC_PCI_DEV_LP:
6399 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6400 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6401 break;
James Smart4f774512009-05-22 14:52:35 -04006402 case LPFC_PCI_DEV_OC:
6403 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6404 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6405 break;
James Smart3772a992009-05-22 14:50:54 -04006406 default:
6407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6408 "1419 Invalid HBA PCI-device group: 0x%x\n",
6409 dev_grp);
6410 return -ENODEV;
6411 break;
6412 }
6413 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6414 return 0;
6415}
James Smart92d7f7b2007-06-17 19:56:38 -05006416
James Smarte59058c2008-08-24 21:49:00 -04006417/**
James Smart3621a712009-04-06 18:47:14 -04006418 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006419 * @phba: Pointer to HBA context object.
6420 * @pring: Pointer to driver SLI ring object.
6421 * @piocb: Pointer to command iocb.
6422 * @flag: Flag indicating if this command can be put into txq.
6423 *
6424 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6425 * function. This function gets the hbalock and calls
6426 * __lpfc_sli_issue_iocb function and will return the error returned
6427 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6428 * functions which do not hold hbalock.
6429 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006430int
James Smart3772a992009-05-22 14:50:54 -04006431lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006432 struct lpfc_iocbq *piocb, uint32_t flag)
6433{
6434 unsigned long iflags;
6435 int rc;
6436
6437 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006438 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006439 spin_unlock_irqrestore(&phba->hbalock, iflags);
6440
6441 return rc;
6442}
6443
James Smarte59058c2008-08-24 21:49:00 -04006444/**
James Smart3621a712009-04-06 18:47:14 -04006445 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006446 * @phba: Pointer to HBA context object.
6447 *
6448 * This function is called while driver attaches with the
6449 * HBA to setup the extra ring. The extra ring is used
6450 * only when driver needs to support target mode functionality
6451 * or IP over FC functionalities.
6452 *
6453 * This function is called with no lock held.
6454 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006455static int
6456lpfc_extra_ring_setup( struct lpfc_hba *phba)
6457{
6458 struct lpfc_sli *psli;
6459 struct lpfc_sli_ring *pring;
6460
6461 psli = &phba->sli;
6462
6463 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006464
6465 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006466 pring = &psli->ring[psli->fcp_ring];
6467 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6468 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6469 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6470 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6471
James Smarta4bc3372006-12-02 13:34:16 -05006472 /* and give them to the extra ring */
6473 pring = &psli->ring[psli->extra_ring];
6474
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006475 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6476 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6477 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6478 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6479
6480 /* Setup default profile for this ring */
6481 pring->iotag_max = 4096;
6482 pring->num_mask = 1;
6483 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006484 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6485 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006486 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6487 return 0;
6488}
6489
James Smarte59058c2008-08-24 21:49:00 -04006490/**
James Smart3621a712009-04-06 18:47:14 -04006491 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006492 * @phba: Pointer to HBA context object.
6493 * @pring: Pointer to driver SLI ring object.
6494 * @iocbq: Pointer to iocb object.
6495 *
6496 * This function is called by the slow ring event handler
6497 * function when there is an ASYNC event iocb in the ring.
6498 * This function is called with no lock held.
6499 * Currently this function handles only temperature related
6500 * ASYNC events. The function decodes the temperature sensor
6501 * event message and posts events for the management applications.
6502 **/
James Smart98c9ea52007-10-27 13:37:33 -04006503static void
James Smart57127f12007-10-27 13:37:05 -04006504lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6505 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6506{
6507 IOCB_t *icmd;
6508 uint16_t evt_code;
6509 uint16_t temp;
6510 struct temp_event temp_event_data;
6511 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006512 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006513
6514 icmd = &iocbq->iocb;
6515 evt_code = icmd->un.asyncstat.evt_code;
6516 temp = icmd->ulpContext;
6517
6518 if ((evt_code != ASYNC_TEMP_WARN) &&
6519 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006520 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006521 lpfc_printf_log(phba,
6522 KERN_ERR,
6523 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006524 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006525 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006526 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6527 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6528 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6529 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006530 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006531 icmd->un.asyncstat.evt_code,
6532 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6533 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6534 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6535 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6536
James Smart57127f12007-10-27 13:37:05 -04006537 return;
6538 }
6539 temp_event_data.data = (uint32_t)temp;
6540 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6541 if (evt_code == ASYNC_TEMP_WARN) {
6542 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6543 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006544 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006545 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006546 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006547 "corrective action. temperature : %d Celsius\n",
6548 temp);
6549 }
6550 if (evt_code == ASYNC_TEMP_SAFE) {
6551 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6552 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006553 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006554 LOG_TEMP,
6555 "0340 Adapter temperature is OK now. "
6556 "temperature : %d Celsius\n",
6557 temp);
6558 }
6559
6560 /* Send temperature change event to applications */
6561 shost = lpfc_shost_from_vport(phba->pport);
6562 fc_host_post_vendor_event(shost, fc_get_event_number(),
6563 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006564 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006565
6566}
6567
6568
James Smarte59058c2008-08-24 21:49:00 -04006569/**
James Smart3621a712009-04-06 18:47:14 -04006570 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006571 * @phba: Pointer to HBA context object.
6572 *
6573 * lpfc_sli_setup sets up rings of the SLI interface with
6574 * number of iocbs per ring and iotags. This function is
6575 * called while driver attach to the HBA and before the
6576 * interrupts are enabled. So there is no need for locking.
6577 *
6578 * This function always returns 0.
6579 **/
dea31012005-04-17 16:05:31 -05006580int
6581lpfc_sli_setup(struct lpfc_hba *phba)
6582{
James Smarted957682007-06-17 19:56:37 -05006583 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006584 struct lpfc_sli *psli = &phba->sli;
6585 struct lpfc_sli_ring *pring;
6586
6587 psli->num_rings = MAX_CONFIGURED_RINGS;
6588 psli->sli_flag = 0;
6589 psli->fcp_ring = LPFC_FCP_RING;
6590 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006591 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006592
James Bottomley604a3e32005-10-29 10:28:33 -05006593 psli->iocbq_lookup = NULL;
6594 psli->iocbq_lookup_len = 0;
6595 psli->last_iotag = 0;
6596
dea31012005-04-17 16:05:31 -05006597 for (i = 0; i < psli->num_rings; i++) {
6598 pring = &psli->ring[i];
6599 switch (i) {
6600 case LPFC_FCP_RING: /* ring 0 - FCP */
6601 /* numCiocb and numRiocb are used in config_port */
6602 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6603 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6604 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6605 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6606 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6607 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006608 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006609 SLI3_IOCB_CMD_SIZE :
6610 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006611 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006612 SLI3_IOCB_RSP_SIZE :
6613 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006614 pring->iotag_ctr = 0;
6615 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006616 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006617 pring->fast_iotag = pring->iotag_max;
6618 pring->num_mask = 0;
6619 break;
James Smarta4bc3372006-12-02 13:34:16 -05006620 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006621 /* numCiocb and numRiocb are used in config_port */
6622 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6623 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006624 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006625 SLI3_IOCB_CMD_SIZE :
6626 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006627 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006628 SLI3_IOCB_RSP_SIZE :
6629 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006630 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006631 pring->num_mask = 0;
6632 break;
6633 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6634 /* numCiocb and numRiocb are used in config_port */
6635 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6636 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006637 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006638 SLI3_IOCB_CMD_SIZE :
6639 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006640 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006641 SLI3_IOCB_RSP_SIZE :
6642 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006643 pring->fast_iotag = 0;
6644 pring->iotag_ctr = 0;
6645 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006646 pring->lpfc_sli_rcv_async_status =
6647 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006648 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006649 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006650 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6651 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006652 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006653 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006654 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006655 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6656 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006657 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006658 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006659 pring->prt[2].profile = 0; /* Mask 2 */
6660 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006661 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006662 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006663 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006664 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006665 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006666 pring->prt[3].profile = 0; /* Mask 3 */
6667 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006668 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006669 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006670 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006671 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006672 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006673 /* abort unsolicited sequence */
6674 pring->prt[4].profile = 0; /* Mask 4 */
6675 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6676 pring->prt[4].type = FC_TYPE_BLS;
6677 pring->prt[4].lpfc_sli_rcv_unsol_event =
6678 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006679 break;
6680 }
James Smarted957682007-06-17 19:56:37 -05006681 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006682 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006683 }
James Smarted957682007-06-17 19:56:37 -05006684 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006685 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006686 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6687 "SLI2 SLIM Data: x%x x%lx\n",
6688 phba->brd_no, totiocbsize,
6689 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006690 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006691 if (phba->cfg_multi_ring_support == 2)
6692 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006693
6694 return 0;
6695}
6696
James Smarte59058c2008-08-24 21:49:00 -04006697/**
James Smart3621a712009-04-06 18:47:14 -04006698 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006699 * @phba: Pointer to HBA context object.
6700 *
6701 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6702 * ring. This function also initializes ring indices of each ring.
6703 * This function is called during the initialization of the SLI
6704 * interface of an HBA.
6705 * This function is called with no lock held and always returns
6706 * 1.
6707 **/
dea31012005-04-17 16:05:31 -05006708int
James Smart2e0fef82007-06-17 19:56:36 -05006709lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006710{
6711 struct lpfc_sli *psli;
6712 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006713 int i;
dea31012005-04-17 16:05:31 -05006714
6715 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006716 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006717 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006718 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006719 /* Initialize list headers for txq and txcmplq as double linked lists */
6720 for (i = 0; i < psli->num_rings; i++) {
6721 pring = &psli->ring[i];
6722 pring->ringno = i;
6723 pring->next_cmdidx = 0;
6724 pring->local_getidx = 0;
6725 pring->cmdidx = 0;
6726 INIT_LIST_HEAD(&pring->txq);
6727 INIT_LIST_HEAD(&pring->txcmplq);
6728 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006729 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006730 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006731 }
James Smart2e0fef82007-06-17 19:56:36 -05006732 spin_unlock_irq(&phba->hbalock);
6733 return 1;
dea31012005-04-17 16:05:31 -05006734}
6735
James Smarte59058c2008-08-24 21:49:00 -04006736/**
James Smart04c68492009-05-22 14:52:52 -04006737 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6738 * @phba: Pointer to HBA context object.
6739 *
6740 * This routine flushes the mailbox command subsystem. It will unconditionally
6741 * flush all the mailbox commands in the three possible stages in the mailbox
6742 * command sub-system: pending mailbox command queue; the outstanding mailbox
6743 * command; and completed mailbox command queue. It is caller's responsibility
6744 * to make sure that the driver is in the proper state to flush the mailbox
6745 * command sub-system. Namely, the posting of mailbox commands into the
6746 * pending mailbox command queue from the various clients must be stopped;
6747 * either the HBA is in a state that it will never works on the outstanding
6748 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6749 * mailbox command has been completed.
6750 **/
6751static void
6752lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6753{
6754 LIST_HEAD(completions);
6755 struct lpfc_sli *psli = &phba->sli;
6756 LPFC_MBOXQ_t *pmb;
6757 unsigned long iflag;
6758
6759 /* Flush all the mailbox commands in the mbox system */
6760 spin_lock_irqsave(&phba->hbalock, iflag);
6761 /* The pending mailbox command queue */
6762 list_splice_init(&phba->sli.mboxq, &completions);
6763 /* The outstanding active mailbox command */
6764 if (psli->mbox_active) {
6765 list_add_tail(&psli->mbox_active->list, &completions);
6766 psli->mbox_active = NULL;
6767 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6768 }
6769 /* The completed mailbox command queue */
6770 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6771 spin_unlock_irqrestore(&phba->hbalock, iflag);
6772
6773 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6774 while (!list_empty(&completions)) {
6775 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6776 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6777 if (pmb->mbox_cmpl)
6778 pmb->mbox_cmpl(phba, pmb);
6779 }
6780}
6781
6782/**
James Smart3621a712009-04-06 18:47:14 -04006783 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006784 * @vport: Pointer to virtual port object.
6785 *
6786 * lpfc_sli_host_down is called to clean up the resources
6787 * associated with a vport before destroying virtual
6788 * port data structures.
6789 * This function does following operations:
6790 * - Free discovery resources associated with this virtual
6791 * port.
6792 * - Free iocbs associated with this virtual port in
6793 * the txq.
6794 * - Send abort for all iocb commands associated with this
6795 * vport in txcmplq.
6796 *
6797 * This function is called with no lock held and always returns 1.
6798 **/
dea31012005-04-17 16:05:31 -05006799int
James Smart92d7f7b2007-06-17 19:56:38 -05006800lpfc_sli_host_down(struct lpfc_vport *vport)
6801{
James Smart858c9f62007-06-17 19:56:39 -05006802 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006803 struct lpfc_hba *phba = vport->phba;
6804 struct lpfc_sli *psli = &phba->sli;
6805 struct lpfc_sli_ring *pring;
6806 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006807 int i;
6808 unsigned long flags = 0;
6809 uint16_t prev_pring_flag;
6810
6811 lpfc_cleanup_discovery_resources(vport);
6812
6813 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006814 for (i = 0; i < psli->num_rings; i++) {
6815 pring = &psli->ring[i];
6816 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006817 /* Only slow rings */
6818 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006819 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006820 /* Set the lpfc data pending flag */
6821 set_bit(LPFC_DATA_READY, &phba->data_flags);
6822 }
James Smart92d7f7b2007-06-17 19:56:38 -05006823 /*
6824 * Error everything on the txq since these iocbs have not been
6825 * given to the FW yet.
6826 */
James Smart92d7f7b2007-06-17 19:56:38 -05006827 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6828 if (iocb->vport != vport)
6829 continue;
James Smart858c9f62007-06-17 19:56:39 -05006830 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006831 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006832 }
6833
6834 /* Next issue ABTS for everything on the txcmplq */
6835 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6836 list) {
6837 if (iocb->vport != vport)
6838 continue;
6839 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6840 }
6841
6842 pring->flag = prev_pring_flag;
6843 }
6844
6845 spin_unlock_irqrestore(&phba->hbalock, flags);
6846
James Smarta257bf92009-04-06 18:48:10 -04006847 /* Cancel all the IOCBs from the completions list */
6848 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6849 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006850 return 1;
6851}
6852
James Smarte59058c2008-08-24 21:49:00 -04006853/**
James Smart3621a712009-04-06 18:47:14 -04006854 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006855 * @phba: Pointer to HBA context object.
6856 *
6857 * This function cleans up all iocb, buffers, mailbox commands
6858 * while shutting down the HBA. This function is called with no
6859 * lock held and always returns 1.
6860 * This function does the following to cleanup driver resources:
6861 * - Free discovery resources for each virtual port
6862 * - Cleanup any pending fabric iocbs
6863 * - Iterate through the iocb txq and free each entry
6864 * in the list.
6865 * - Free up any buffer posted to the HBA
6866 * - Free mailbox commands in the mailbox queue.
6867 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006868int
James Smart2e0fef82007-06-17 19:56:36 -05006869lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006870{
James Smart2534ba72007-04-25 09:52:20 -04006871 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006872 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006873 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006874 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006875 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006876 int i;
6877
6878 /* Shutdown the mailbox command sub-system */
6879 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006880
dea31012005-04-17 16:05:31 -05006881 lpfc_hba_down_prep(phba);
6882
James Smart92d7f7b2007-06-17 19:56:38 -05006883 lpfc_fabric_abort_hba(phba);
6884
James Smart2e0fef82007-06-17 19:56:36 -05006885 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006886 for (i = 0; i < psli->num_rings; i++) {
6887 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006888 /* Only slow rings */
6889 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006890 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006891 /* Set the lpfc data pending flag */
6892 set_bit(LPFC_DATA_READY, &phba->data_flags);
6893 }
dea31012005-04-17 16:05:31 -05006894
6895 /*
6896 * Error everything on the txq since these iocbs have not been
6897 * given to the FW yet.
6898 */
James Smart2534ba72007-04-25 09:52:20 -04006899 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006900 pring->txq_cnt = 0;
6901
dea31012005-04-17 16:05:31 -05006902 }
James Smart2e0fef82007-06-17 19:56:36 -05006903 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006904
James Smarta257bf92009-04-06 18:48:10 -04006905 /* Cancel all the IOCBs from the completions list */
6906 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6907 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006908
James Smart0ff10d42008-01-11 01:52:36 -05006909 spin_lock_irqsave(&phba->hbalock, flags);
6910 list_splice_init(&phba->elsbuf, &completions);
6911 phba->elsbuf_cnt = 0;
6912 phba->elsbuf_prev_cnt = 0;
6913 spin_unlock_irqrestore(&phba->hbalock, flags);
6914
6915 while (!list_empty(&completions)) {
6916 list_remove_head(&completions, buf_ptr,
6917 struct lpfc_dmabuf, list);
6918 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6919 kfree(buf_ptr);
6920 }
6921
dea31012005-04-17 16:05:31 -05006922 /* Return any active mbox cmds */
6923 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006924
James Smartda0436e2009-05-22 14:51:39 -04006925 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006926 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006927 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006928
James Smartda0436e2009-05-22 14:51:39 -04006929 return 1;
6930}
James Smart92d7f7b2007-06-17 19:56:38 -05006931
James Smartda0436e2009-05-22 14:51:39 -04006932/**
James Smart3621a712009-04-06 18:47:14 -04006933 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006934 * @srcp: Source memory pointer.
6935 * @destp: Destination memory pointer.
6936 * @cnt: Number of words required to be copied.
6937 *
6938 * This function is used for copying data between driver memory
6939 * and the SLI memory. This function also changes the endianness
6940 * of each word if native endianness is different from SLI
6941 * endianness. This function can be called with or without
6942 * lock.
6943 **/
dea31012005-04-17 16:05:31 -05006944void
6945lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6946{
6947 uint32_t *src = srcp;
6948 uint32_t *dest = destp;
6949 uint32_t ldata;
6950 int i;
6951
6952 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6953 ldata = *src;
6954 ldata = le32_to_cpu(ldata);
6955 *dest = ldata;
6956 src++;
6957 dest++;
6958 }
6959}
6960
James Smarte59058c2008-08-24 21:49:00 -04006961
6962/**
James Smarta0c87cb2009-07-19 10:01:10 -04006963 * lpfc_sli_bemem_bcopy - SLI memory copy function
6964 * @srcp: Source memory pointer.
6965 * @destp: Destination memory pointer.
6966 * @cnt: Number of words required to be copied.
6967 *
6968 * This function is used for copying data between a data structure
6969 * with big endian representation to local endianness.
6970 * This function can be called with or without lock.
6971 **/
6972void
6973lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6974{
6975 uint32_t *src = srcp;
6976 uint32_t *dest = destp;
6977 uint32_t ldata;
6978 int i;
6979
6980 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6981 ldata = *src;
6982 ldata = be32_to_cpu(ldata);
6983 *dest = ldata;
6984 src++;
6985 dest++;
6986 }
6987}
6988
6989/**
James Smart3621a712009-04-06 18:47:14 -04006990 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006991 * @phba: Pointer to HBA context object.
6992 * @pring: Pointer to driver SLI ring object.
6993 * @mp: Pointer to driver buffer object.
6994 *
6995 * This function is called with no lock held.
6996 * It always return zero after adding the buffer to the postbufq
6997 * buffer list.
6998 **/
dea31012005-04-17 16:05:31 -05006999int
James Smart2e0fef82007-06-17 19:56:36 -05007000lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7001 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007002{
7003 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7004 later */
James Smart2e0fef82007-06-17 19:56:36 -05007005 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007006 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007007 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007008 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007009 return 0;
7010}
7011
James Smarte59058c2008-08-24 21:49:00 -04007012/**
James Smart3621a712009-04-06 18:47:14 -04007013 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007014 * @phba: Pointer to HBA context object.
7015 *
7016 * When HBQ is enabled, buffers are searched based on tags. This function
7017 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7018 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7019 * does not conflict with tags of buffer posted for unsolicited events.
7020 * The function returns the allocated tag. The function is called with
7021 * no locks held.
7022 **/
James Smart76bb24e2007-10-27 13:38:00 -04007023uint32_t
7024lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7025{
7026 spin_lock_irq(&phba->hbalock);
7027 phba->buffer_tag_count++;
7028 /*
7029 * Always set the QUE_BUFTAG_BIT to distiguish between
7030 * a tag assigned by HBQ.
7031 */
7032 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7033 spin_unlock_irq(&phba->hbalock);
7034 return phba->buffer_tag_count;
7035}
7036
James Smarte59058c2008-08-24 21:49:00 -04007037/**
James Smart3621a712009-04-06 18:47:14 -04007038 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007039 * @phba: Pointer to HBA context object.
7040 * @pring: Pointer to driver SLI ring object.
7041 * @tag: Buffer tag.
7042 *
7043 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7044 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7045 * iocb is posted to the response ring with the tag of the buffer.
7046 * This function searches the pring->postbufq list using the tag
7047 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7048 * iocb. If the buffer is found then lpfc_dmabuf object of the
7049 * buffer is returned to the caller else NULL is returned.
7050 * This function is called with no lock held.
7051 **/
James Smart76bb24e2007-10-27 13:38:00 -04007052struct lpfc_dmabuf *
7053lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7054 uint32_t tag)
7055{
7056 struct lpfc_dmabuf *mp, *next_mp;
7057 struct list_head *slp = &pring->postbufq;
7058
7059 /* Search postbufq, from the begining, looking for a match on tag */
7060 spin_lock_irq(&phba->hbalock);
7061 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7062 if (mp->buffer_tag == tag) {
7063 list_del_init(&mp->list);
7064 pring->postbufq_cnt--;
7065 spin_unlock_irq(&phba->hbalock);
7066 return mp;
7067 }
7068 }
7069
7070 spin_unlock_irq(&phba->hbalock);
7071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007072 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007073 "ring %d Data x%lx x%p x%p x%x\n",
7074 pring->ringno, (unsigned long) tag,
7075 slp->next, slp->prev, pring->postbufq_cnt);
7076
7077 return NULL;
7078}
dea31012005-04-17 16:05:31 -05007079
James Smarte59058c2008-08-24 21:49:00 -04007080/**
James Smart3621a712009-04-06 18:47:14 -04007081 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007082 * @phba: Pointer to HBA context object.
7083 * @pring: Pointer to driver SLI ring object.
7084 * @phys: DMA address of the buffer.
7085 *
7086 * This function searches the buffer list using the dma_address
7087 * of unsolicited event to find the driver's lpfc_dmabuf object
7088 * corresponding to the dma_address. The function returns the
7089 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7090 * This function is called by the ct and els unsolicited event
7091 * handlers to get the buffer associated with the unsolicited
7092 * event.
7093 *
7094 * This function is called with no lock held.
7095 **/
dea31012005-04-17 16:05:31 -05007096struct lpfc_dmabuf *
7097lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7098 dma_addr_t phys)
7099{
7100 struct lpfc_dmabuf *mp, *next_mp;
7101 struct list_head *slp = &pring->postbufq;
7102
7103 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007104 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007105 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7106 if (mp->phys == phys) {
7107 list_del_init(&mp->list);
7108 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007109 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007110 return mp;
7111 }
7112 }
7113
James Smart2e0fef82007-06-17 19:56:36 -05007114 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007116 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007117 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007118 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007119 slp->next, slp->prev, pring->postbufq_cnt);
7120 return NULL;
7121}
7122
James Smarte59058c2008-08-24 21:49:00 -04007123/**
James Smart3621a712009-04-06 18:47:14 -04007124 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007125 * @phba: Pointer to HBA context object.
7126 * @cmdiocb: Pointer to driver command iocb object.
7127 * @rspiocb: Pointer to driver response iocb object.
7128 *
7129 * This function is the completion handler for the abort iocbs for
7130 * ELS commands. This function is called from the ELS ring event
7131 * handler with no lock held. This function frees memory resources
7132 * associated with the abort iocb.
7133 **/
dea31012005-04-17 16:05:31 -05007134static void
James Smart2e0fef82007-06-17 19:56:36 -05007135lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7136 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007137{
James Smart2e0fef82007-06-17 19:56:36 -05007138 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007139 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007140 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007141 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7142
7143 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007144
7145 if (irsp->ulpStatus) {
7146 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7147 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7148
James Smart2e0fef82007-06-17 19:56:36 -05007149 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007150 if (phba->sli_rev < LPFC_SLI_REV4) {
7151 if (abort_iotag != 0 &&
7152 abort_iotag <= phba->sli.last_iotag)
7153 abort_iocb =
7154 phba->sli.iocbq_lookup[abort_iotag];
7155 } else
7156 /* For sli4 the abort_tag is the XRI,
7157 * so the abort routine puts the iotag of the iocb
7158 * being aborted in the context field of the abort
7159 * IOCB.
7160 */
7161 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007162
James Smart2680eea2007-04-25 09:52:55 -04007163 /*
James Smart58da1ff2008-04-07 10:15:56 -04007164 * If the iocb is not found in Firmware queue the iocb
7165 * might have completed already. Do not free it again.
7166 */
James Smart9b379602008-04-07 10:16:00 -04007167 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007168 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7169 spin_unlock_irq(&phba->hbalock);
7170 lpfc_sli_release_iocbq(phba, cmdiocb);
7171 return;
7172 }
7173 /* For SLI4 the ulpContext field for abort IOCB
7174 * holds the iotag of the IOCB being aborted so
7175 * the local abort_context needs to be reset to
7176 * match the aborted IOCBs ulpContext.
7177 */
7178 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7179 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007180 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007181
7182 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7183 "0327 Cannot abort els iocb %p "
7184 "with tag %x context %x, abort status %x, "
7185 "abort code %x\n",
7186 abort_iocb, abort_iotag, abort_context,
7187 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007188 /*
James Smart2680eea2007-04-25 09:52:55 -04007189 * make sure we have the right iocbq before taking it
7190 * off the txcmplq and try to call completion routine.
7191 */
James Smart2e0fef82007-06-17 19:56:36 -05007192 if (!abort_iocb ||
7193 abort_iocb->iocb.ulpContext != abort_context ||
7194 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7195 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007196 else if (phba->sli_rev < LPFC_SLI_REV4) {
7197 /*
7198 * leave the SLI4 aborted command on the txcmplq
7199 * list and the command complete WCQE's XB bit
7200 * will tell whether the SGL (XRI) can be released
7201 * immediately or to the aborted SGL list for the
7202 * following abort XRI from the HBA.
7203 */
James Smart92d7f7b2007-06-17 19:56:38 -05007204 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007205 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7206 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7207 pring->txcmplq_cnt--;
7208 }
James Smart2680eea2007-04-25 09:52:55 -04007209
James Smart0ff10d42008-01-11 01:52:36 -05007210 /* Firmware could still be in progress of DMAing
7211 * payload, so don't free data buffer till after
7212 * a hbeat.
7213 */
7214 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007215 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007216 spin_unlock_irq(&phba->hbalock);
7217
James Smart92d7f7b2007-06-17 19:56:38 -05007218 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007219 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007220 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007221 } else
7222 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007223 }
7224
James Bottomley604a3e32005-10-29 10:28:33 -05007225 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007226 return;
7227}
7228
James Smarte59058c2008-08-24 21:49:00 -04007229/**
James Smart3621a712009-04-06 18:47:14 -04007230 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007231 * @phba: Pointer to HBA context object.
7232 * @cmdiocb: Pointer to driver command iocb object.
7233 * @rspiocb: Pointer to driver response iocb object.
7234 *
7235 * The function is called from SLI ring event handler with no
7236 * lock held. This function is the completion handler for ELS commands
7237 * which are aborted. The function frees memory resources used for
7238 * the aborted ELS commands.
7239 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007240static void
7241lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7242 struct lpfc_iocbq *rspiocb)
7243{
7244 IOCB_t *irsp = &rspiocb->iocb;
7245
7246 /* ELS cmd tag <ulpIoTag> completes */
7247 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007248 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007249 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007250 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007251 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007252 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7253 lpfc_ct_free_iocb(phba, cmdiocb);
7254 else
7255 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007256 return;
7257}
7258
James Smarte59058c2008-08-24 21:49:00 -04007259/**
James Smart5af5eee2010-10-22 11:06:38 -04007260 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007261 * @phba: Pointer to HBA context object.
7262 * @pring: Pointer to driver SLI ring object.
7263 * @cmdiocb: Pointer to driver command iocb object.
7264 *
James Smart5af5eee2010-10-22 11:06:38 -04007265 * This function issues an abort iocb for the provided command iocb down to
7266 * the port. Other than the case the outstanding command iocb is an abort
7267 * request, this function issues abort out unconditionally. This function is
7268 * called with hbalock held. The function returns 0 when it fails due to
7269 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007270 **/
James Smart5af5eee2010-10-22 11:06:38 -04007271static int
7272lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007273 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007274{
James Smart2e0fef82007-06-17 19:56:36 -05007275 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007276 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007277 IOCB_t *icmd = NULL;
7278 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007279 int retval;
James Smart07951072007-04-25 09:51:38 -04007280
James Smart92d7f7b2007-06-17 19:56:38 -05007281 /*
7282 * There are certain command types we don't want to abort. And we
7283 * don't want to abort commands that are already in the process of
7284 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007285 */
7286 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007287 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007288 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7289 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007290 return 0;
7291
dea31012005-04-17 16:05:31 -05007292 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007293 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007294 if (abtsiocbp == NULL)
7295 return 0;
dea31012005-04-17 16:05:31 -05007296
James Smart07951072007-04-25 09:51:38 -04007297 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007298 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007299 */
7300 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7301
dea31012005-04-17 16:05:31 -05007302 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007303 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7304 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007305 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007306 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007307 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7308 }
James Smartda0436e2009-05-22 14:51:39 -04007309 else
7310 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007311 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007312 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007313
James Smart5ffc2662009-11-18 15:39:44 -05007314 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7315 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007316 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7317 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007318
James Smart2e0fef82007-06-17 19:56:36 -05007319 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007320 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7321 else
7322 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7323
7324 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007325
James Smarte8b62012007-08-02 11:10:09 -04007326 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7327 "0339 Abort xri x%x, original iotag x%x, "
7328 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007329 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007330 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007331 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007332 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007333
James Smartd7c255b2008-08-24 21:50:00 -04007334 if (retval)
7335 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007336
7337 /*
7338 * Caller to this routine should check for IOCB_ERROR
7339 * and handle it properly. This routine no longer removes
7340 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7341 */
7342 return retval;
7343}
7344
7345/**
7346 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7347 * @phba: Pointer to HBA context object.
7348 * @pring: Pointer to driver SLI ring object.
7349 * @cmdiocb: Pointer to driver command iocb object.
7350 *
7351 * This function issues an abort iocb for the provided command iocb. In case
7352 * of unloading, the abort iocb will not be issued to commands on the ELS
7353 * ring. Instead, the callback function shall be changed to those commands
7354 * so that nothing happens when them finishes. This function is called with
7355 * hbalock held. The function returns 0 when the command iocb is an abort
7356 * request.
7357 **/
7358int
7359lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7360 struct lpfc_iocbq *cmdiocb)
7361{
7362 struct lpfc_vport *vport = cmdiocb->vport;
7363 int retval = IOCB_ERROR;
7364 IOCB_t *icmd = NULL;
7365
7366 /*
7367 * There are certain command types we don't want to abort. And we
7368 * don't want to abort commands that are already in the process of
7369 * being aborted.
7370 */
7371 icmd = &cmdiocb->iocb;
7372 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7373 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7374 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7375 return 0;
7376
7377 /*
7378 * If we're unloading, don't abort iocb on the ELS ring, but change
7379 * the callback so that nothing happens when it finishes.
7380 */
7381 if ((vport->load_flag & FC_UNLOADING) &&
7382 (pring->ringno == LPFC_ELS_RING)) {
7383 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7384 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7385 else
7386 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7387 goto abort_iotag_exit;
7388 }
7389
7390 /* Now, we try to issue the abort to the cmdiocb out */
7391 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7392
James Smart07951072007-04-25 09:51:38 -04007393abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007394 /*
7395 * Caller to this routine should check for IOCB_ERROR
7396 * and handle it properly. This routine no longer removes
7397 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007398 */
James Smart2e0fef82007-06-17 19:56:36 -05007399 return retval;
dea31012005-04-17 16:05:31 -05007400}
7401
James Smarte59058c2008-08-24 21:49:00 -04007402/**
James Smart5af5eee2010-10-22 11:06:38 -04007403 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7404 * @phba: Pointer to HBA context object.
7405 * @pring: Pointer to driver SLI ring object.
7406 *
7407 * This function aborts all iocbs in the given ring and frees all the iocb
7408 * objects in txq. This function issues abort iocbs unconditionally for all
7409 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7410 * to complete before the return of this function. The caller is not required
7411 * to hold any locks.
7412 **/
7413static void
7414lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7415{
7416 LIST_HEAD(completions);
7417 struct lpfc_iocbq *iocb, *next_iocb;
7418
7419 if (pring->ringno == LPFC_ELS_RING)
7420 lpfc_fabric_abort_hba(phba);
7421
7422 spin_lock_irq(&phba->hbalock);
7423
7424 /* Take off all the iocbs on txq for cancelling */
7425 list_splice_init(&pring->txq, &completions);
7426 pring->txq_cnt = 0;
7427
7428 /* Next issue ABTS for everything on the txcmplq */
7429 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7430 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7431
7432 spin_unlock_irq(&phba->hbalock);
7433
7434 /* Cancel all the IOCBs from the completions list */
7435 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7436 IOERR_SLI_ABORTED);
7437}
7438
7439/**
7440 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7441 * @phba: pointer to lpfc HBA data structure.
7442 *
7443 * This routine will abort all pending and outstanding iocbs to an HBA.
7444 **/
7445void
7446lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7447{
7448 struct lpfc_sli *psli = &phba->sli;
7449 struct lpfc_sli_ring *pring;
7450 int i;
7451
7452 for (i = 0; i < psli->num_rings; i++) {
7453 pring = &psli->ring[i];
7454 lpfc_sli_iocb_ring_abort(phba, pring);
7455 }
7456}
7457
7458/**
James Smart3621a712009-04-06 18:47:14 -04007459 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007460 * @iocbq: Pointer to driver iocb object.
7461 * @vport: Pointer to driver virtual port object.
7462 * @tgt_id: SCSI ID of the target.
7463 * @lun_id: LUN ID of the scsi device.
7464 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7465 *
James Smart3621a712009-04-06 18:47:14 -04007466 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007467 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7468 * 0 if the filtering criteria is met for the given iocb and will return
7469 * 1 if the filtering criteria is not met.
7470 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7471 * given iocb is for the SCSI device specified by vport, tgt_id and
7472 * lun_id parameter.
7473 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7474 * given iocb is for the SCSI target specified by vport and tgt_id
7475 * parameters.
7476 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7477 * given iocb is for the SCSI host associated with the given vport.
7478 * This function is called with no locks held.
7479 **/
dea31012005-04-17 16:05:31 -05007480static int
James Smart51ef4c22007-08-02 11:10:31 -04007481lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7482 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007483 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007484{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007485 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007486 int rc = 1;
7487
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007488 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7489 return rc;
7490
James Smart51ef4c22007-08-02 11:10:31 -04007491 if (iocbq->vport != vport)
7492 return rc;
7493
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007494 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007495
James Smart495a7142008-06-14 22:52:59 -04007496 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007497 return rc;
7498
7499 switch (ctx_cmd) {
7500 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007501 if ((lpfc_cmd->rdata->pnode) &&
7502 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7503 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007504 rc = 0;
7505 break;
7506 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007507 if ((lpfc_cmd->rdata->pnode) &&
7508 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007509 rc = 0;
7510 break;
dea31012005-04-17 16:05:31 -05007511 case LPFC_CTX_HOST:
7512 rc = 0;
7513 break;
7514 default:
7515 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007516 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007517 break;
7518 }
7519
7520 return rc;
7521}
7522
James Smarte59058c2008-08-24 21:49:00 -04007523/**
James Smart3621a712009-04-06 18:47:14 -04007524 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007525 * @vport: Pointer to virtual port.
7526 * @tgt_id: SCSI ID of the target.
7527 * @lun_id: LUN ID of the scsi device.
7528 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7529 *
7530 * This function returns number of FCP commands pending for the vport.
7531 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7532 * commands pending on the vport associated with SCSI device specified
7533 * by tgt_id and lun_id parameters.
7534 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7535 * commands pending on the vport associated with SCSI target specified
7536 * by tgt_id parameter.
7537 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7538 * commands pending on the vport.
7539 * This function returns the number of iocbs which satisfy the filter.
7540 * This function is called without any lock held.
7541 **/
dea31012005-04-17 16:05:31 -05007542int
James Smart51ef4c22007-08-02 11:10:31 -04007543lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7544 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007545{
James Smart51ef4c22007-08-02 11:10:31 -04007546 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007547 struct lpfc_iocbq *iocbq;
7548 int sum, i;
dea31012005-04-17 16:05:31 -05007549
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007550 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7551 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007552
James Smart51ef4c22007-08-02 11:10:31 -04007553 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7554 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007555 sum++;
dea31012005-04-17 16:05:31 -05007556 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007557
dea31012005-04-17 16:05:31 -05007558 return sum;
7559}
7560
James Smarte59058c2008-08-24 21:49:00 -04007561/**
James Smart3621a712009-04-06 18:47:14 -04007562 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007563 * @phba: Pointer to HBA context object
7564 * @cmdiocb: Pointer to command iocb object.
7565 * @rspiocb: Pointer to response iocb object.
7566 *
7567 * This function is called when an aborted FCP iocb completes. This
7568 * function is called by the ring event handler with no lock held.
7569 * This function frees the iocb.
7570 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007571void
James Smart2e0fef82007-06-17 19:56:36 -05007572lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7573 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007574{
James Bottomley604a3e32005-10-29 10:28:33 -05007575 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007576 return;
7577}
7578
James Smarte59058c2008-08-24 21:49:00 -04007579/**
James Smart3621a712009-04-06 18:47:14 -04007580 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007581 * @vport: Pointer to virtual port.
7582 * @pring: Pointer to driver SLI ring object.
7583 * @tgt_id: SCSI ID of the target.
7584 * @lun_id: LUN ID of the scsi device.
7585 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7586 *
7587 * This function sends an abort command for every SCSI command
7588 * associated with the given virtual port pending on the ring
7589 * filtered by lpfc_sli_validate_fcp_iocb function.
7590 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7591 * FCP iocbs associated with lun specified by tgt_id and lun_id
7592 * parameters
7593 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7594 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7595 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7596 * FCP iocbs associated with virtual port.
7597 * This function returns number of iocbs it failed to abort.
7598 * This function is called with no locks held.
7599 **/
dea31012005-04-17 16:05:31 -05007600int
James Smart51ef4c22007-08-02 11:10:31 -04007601lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7602 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007603{
James Smart51ef4c22007-08-02 11:10:31 -04007604 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007605 struct lpfc_iocbq *iocbq;
7606 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007607 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007608 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007609 int i;
dea31012005-04-17 16:05:31 -05007610
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007611 for (i = 1; i <= phba->sli.last_iotag; i++) {
7612 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007613
James Smart51ef4c22007-08-02 11:10:31 -04007614 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007615 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007616 continue;
7617
7618 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007619 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007620 if (abtsiocb == NULL) {
7621 errcnt++;
7622 continue;
7623 }
dea31012005-04-17 16:05:31 -05007624
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007625 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007626 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7627 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007628 if (phba->sli_rev == LPFC_SLI_REV4)
7629 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7630 else
7631 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007632 abtsiocb->iocb.ulpLe = 1;
7633 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007634 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007635
James Smart5ffc2662009-11-18 15:39:44 -05007636 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7637 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007638 if (iocbq->iocb_flag & LPFC_IO_FCP)
7639 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007640
James Smart2e0fef82007-06-17 19:56:36 -05007641 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007642 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7643 else
7644 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7645
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007646 /* Setup callback routine and issue the command. */
7647 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007648 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7649 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007650 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007651 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007652 errcnt++;
7653 continue;
7654 }
7655 }
7656
7657 return errcnt;
7658}
7659
James Smarte59058c2008-08-24 21:49:00 -04007660/**
James Smart3621a712009-04-06 18:47:14 -04007661 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007662 * @phba: Pointer to HBA context object.
7663 * @cmdiocbq: Pointer to command iocb.
7664 * @rspiocbq: Pointer to response iocb.
7665 *
7666 * This function is the completion handler for iocbs issued using
7667 * lpfc_sli_issue_iocb_wait function. This function is called by the
7668 * ring event handler function without any lock held. This function
7669 * can be called from both worker thread context and interrupt
7670 * context. This function also can be called from other thread which
7671 * cleans up the SLI layer objects.
7672 * This function copy the contents of the response iocb to the
7673 * response iocb memory object provided by the caller of
7674 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7675 * sleeps for the iocb completion.
7676 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007677static void
7678lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7679 struct lpfc_iocbq *cmdiocbq,
7680 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007681{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007682 wait_queue_head_t *pdone_q;
7683 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007684 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007685
James Smart2e0fef82007-06-17 19:56:36 -05007686 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007687 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7688 if (cmdiocbq->context2 && rspiocbq)
7689 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7690 &rspiocbq->iocb, sizeof(IOCB_t));
7691
James Smart0f65ff62010-02-26 14:14:23 -05007692 /* Set the exchange busy flag for task management commands */
7693 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7694 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7695 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7696 cur_iocbq);
7697 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7698 }
7699
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007700 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007701 if (pdone_q)
7702 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007703 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007704 return;
7705}
7706
James Smarte59058c2008-08-24 21:49:00 -04007707/**
James Smartd11e31d2009-06-10 17:23:06 -04007708 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7709 * @phba: Pointer to HBA context object..
7710 * @piocbq: Pointer to command iocb.
7711 * @flag: Flag to test.
7712 *
7713 * This routine grabs the hbalock and then test the iocb_flag to
7714 * see if the passed in flag is set.
7715 * Returns:
7716 * 1 if flag is set.
7717 * 0 if flag is not set.
7718 **/
7719static int
7720lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7721 struct lpfc_iocbq *piocbq, uint32_t flag)
7722{
7723 unsigned long iflags;
7724 int ret;
7725
7726 spin_lock_irqsave(&phba->hbalock, iflags);
7727 ret = piocbq->iocb_flag & flag;
7728 spin_unlock_irqrestore(&phba->hbalock, iflags);
7729 return ret;
7730
7731}
7732
7733/**
James Smart3621a712009-04-06 18:47:14 -04007734 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007735 * @phba: Pointer to HBA context object..
7736 * @pring: Pointer to sli ring.
7737 * @piocb: Pointer to command iocb.
7738 * @prspiocbq: Pointer to response iocb.
7739 * @timeout: Timeout in number of seconds.
7740 *
7741 * This function issues the iocb to firmware and waits for the
7742 * iocb to complete. If the iocb command is not
7743 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7744 * Caller should not free the iocb resources if this function
7745 * returns IOCB_TIMEDOUT.
7746 * The function waits for the iocb completion using an
7747 * non-interruptible wait.
7748 * This function will sleep while waiting for iocb completion.
7749 * So, this function should not be called from any context which
7750 * does not allow sleeping. Due to the same reason, this function
7751 * cannot be called with interrupt disabled.
7752 * This function assumes that the iocb completions occur while
7753 * this function sleep. So, this function cannot be called from
7754 * the thread which process iocb completion for this ring.
7755 * This function clears the iocb_flag of the iocb object before
7756 * issuing the iocb and the iocb completion handler sets this
7757 * flag and wakes this thread when the iocb completes.
7758 * The contents of the response iocb will be copied to prspiocbq
7759 * by the completion handler when the command completes.
7760 * This function returns IOCB_SUCCESS when success.
7761 * This function is called with no lock held.
7762 **/
dea31012005-04-17 16:05:31 -05007763int
James Smart2e0fef82007-06-17 19:56:36 -05007764lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007765 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007766 struct lpfc_iocbq *piocb,
7767 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007768 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007769{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007770 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007771 long timeleft, timeout_req = 0;
7772 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007773 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04007774 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05007775 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007776 * If the caller has provided a response iocbq buffer, then context2
7777 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007778 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007779 if (prspiocbq) {
7780 if (piocb->context2)
7781 return IOCB_ERROR;
7782 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007783 }
7784
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007785 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7786 piocb->context_un.wait_queue = &done_q;
7787 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007788
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007789 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7790 creg_val = readl(phba->HCregaddr);
7791 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7792 writel(creg_val, phba->HCregaddr);
7793 readl(phba->HCregaddr); /* flush */
7794 }
7795
James Smart2a9bf3d2010-06-07 15:24:45 -04007796 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7797 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007798 if (retval == IOCB_SUCCESS) {
7799 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007800 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007801 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007802 timeout_req);
dea31012005-04-17 16:05:31 -05007803
James Smart7054a602007-04-25 09:52:34 -04007804 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7805 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007806 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007807 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007809 "0338 IOCB wait timeout error - no "
7810 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007811 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007812 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007813 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007814 "0330 IOCB wake NOT set, "
7815 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007816 timeout, (timeleft / jiffies));
7817 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007818 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007819 } else if (retval == IOCB_BUSY) {
7820 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7821 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7822 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7823 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007824 } else {
7825 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007826 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007827 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007828 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007829 }
7830
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007831 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7832 creg_val = readl(phba->HCregaddr);
7833 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7834 writel(creg_val, phba->HCregaddr);
7835 readl(phba->HCregaddr); /* flush */
7836 }
7837
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007838 if (prspiocbq)
7839 piocb->context2 = NULL;
7840
7841 piocb->context_un.wait_queue = NULL;
7842 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007843 return retval;
7844}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007845
James Smarte59058c2008-08-24 21:49:00 -04007846/**
James Smart3621a712009-04-06 18:47:14 -04007847 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007848 * @phba: Pointer to HBA context object.
7849 * @pmboxq: Pointer to driver mailbox object.
7850 * @timeout: Timeout in number of seconds.
7851 *
7852 * This function issues the mailbox to firmware and waits for the
7853 * mailbox command to complete. If the mailbox command is not
7854 * completed within timeout seconds, it returns MBX_TIMEOUT.
7855 * The function waits for the mailbox completion using an
7856 * interruptible wait. If the thread is woken up due to a
7857 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7858 * should not free the mailbox resources, if this function returns
7859 * MBX_TIMEOUT.
7860 * This function will sleep while waiting for mailbox completion.
7861 * So, this function should not be called from any context which
7862 * does not allow sleeping. Due to the same reason, this function
7863 * cannot be called with interrupt disabled.
7864 * This function assumes that the mailbox completion occurs while
7865 * this function sleep. So, this function cannot be called from
7866 * the worker thread which processes mailbox completion.
7867 * This function is called in the context of HBA management
7868 * applications.
7869 * This function returns MBX_SUCCESS when successful.
7870 * This function is called with no lock held.
7871 **/
dea31012005-04-17 16:05:31 -05007872int
James Smart2e0fef82007-06-17 19:56:36 -05007873lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007874 uint32_t timeout)
7875{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007876 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007877 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007878 unsigned long flag;
dea31012005-04-17 16:05:31 -05007879
7880 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007881 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007882 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007883
James Smart495a7142008-06-14 22:52:59 -04007884 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007885 /* setup wake call as IOCB callback */
7886 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7887 /* setup context field to pass wait_queue pointer to wake function */
7888 pmboxq->context1 = &done_q;
7889
dea31012005-04-17 16:05:31 -05007890 /* now issue the command */
7891 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7892
7893 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007894 wait_event_interruptible_timeout(done_q,
7895 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7896 timeout * HZ);
7897
James Smart858c9f62007-06-17 19:56:39 -05007898 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007899 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007900 /*
7901 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7902 * else do not free the resources.
7903 */
James Smartd7c47992010-06-08 18:31:54 -04007904 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05007905 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04007906 lpfc_sli4_swap_str(phba, pmboxq);
7907 } else {
James Smart7054a602007-04-25 09:52:34 -04007908 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007909 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7910 }
7911 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007912 }
7913
dea31012005-04-17 16:05:31 -05007914 return retval;
7915}
7916
James Smarte59058c2008-08-24 21:49:00 -04007917/**
James Smart3772a992009-05-22 14:50:54 -04007918 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007919 * @phba: Pointer to HBA context.
7920 *
James Smart3772a992009-05-22 14:50:54 -04007921 * This function is called to shutdown the driver's mailbox sub-system.
7922 * It first marks the mailbox sub-system is in a block state to prevent
7923 * the asynchronous mailbox command from issued off the pending mailbox
7924 * command queue. If the mailbox command sub-system shutdown is due to
7925 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7926 * the mailbox sub-system flush routine to forcefully bring down the
7927 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7928 * as with offline or HBA function reset), this routine will wait for the
7929 * outstanding mailbox command to complete before invoking the mailbox
7930 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007931 **/
James Smart3772a992009-05-22 14:50:54 -04007932void
7933lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007934{
James Smart3772a992009-05-22 14:50:54 -04007935 struct lpfc_sli *psli = &phba->sli;
7936 uint8_t actcmd = MBX_HEARTBEAT;
7937 unsigned long timeout;
7938
7939 spin_lock_irq(&phba->hbalock);
7940 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7941 spin_unlock_irq(&phba->hbalock);
7942
7943 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7944 spin_lock_irq(&phba->hbalock);
7945 if (phba->sli.mbox_active)
7946 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7947 spin_unlock_irq(&phba->hbalock);
7948 /* Determine how long we might wait for the active mailbox
7949 * command to be gracefully completed by firmware.
7950 */
7951 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7952 1000) + jiffies;
7953 while (phba->sli.mbox_active) {
7954 /* Check active mailbox complete status every 2ms */
7955 msleep(2);
7956 if (time_after(jiffies, timeout))
7957 /* Timeout, let the mailbox flush routine to
7958 * forcefully release active mailbox command
7959 */
7960 break;
7961 }
7962 }
7963 lpfc_sli_mbox_sys_flush(phba);
7964}
7965
7966/**
7967 * lpfc_sli_eratt_read - read sli-3 error attention events
7968 * @phba: Pointer to HBA context.
7969 *
7970 * This function is called to read the SLI3 device error attention registers
7971 * for possible error attention events. The caller must hold the hostlock
7972 * with spin_lock_irq().
7973 *
7974 * This fucntion returns 1 when there is Error Attention in the Host Attention
7975 * Register and returns 0 otherwise.
7976 **/
7977static int
7978lpfc_sli_eratt_read(struct lpfc_hba *phba)
7979{
James Smarted957682007-06-17 19:56:37 -05007980 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007981
James Smart3772a992009-05-22 14:50:54 -04007982 /* Read chip Host Attention (HA) register */
7983 ha_copy = readl(phba->HAregaddr);
7984 if (ha_copy & HA_ERATT) {
7985 /* Read host status register to retrieve error event */
7986 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007987
James Smart3772a992009-05-22 14:50:54 -04007988 /* Check if there is a deferred error condition is active */
7989 if ((HS_FFER1 & phba->work_hs) &&
7990 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04007991 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007992 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007993 /* Clear all interrupt enable conditions */
7994 writel(0, phba->HCregaddr);
7995 readl(phba->HCregaddr);
7996 }
7997
7998 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007999 phba->work_ha |= HA_ERATT;
8000 /* Indicate polling handles this ERATT */
8001 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008002 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008003 }
James Smart3772a992009-05-22 14:50:54 -04008004 return 0;
James Smartb4c02652006-07-06 15:50:43 -04008005}
8006
James Smarte59058c2008-08-24 21:49:00 -04008007/**
James Smartda0436e2009-05-22 14:51:39 -04008008 * lpfc_sli4_eratt_read - read sli-4 error attention events
8009 * @phba: Pointer to HBA context.
8010 *
8011 * This function is called to read the SLI4 device error attention registers
8012 * for possible error attention events. The caller must hold the hostlock
8013 * with spin_lock_irq().
8014 *
8015 * This fucntion returns 1 when there is Error Attention in the Host Attention
8016 * Register and returns 0 otherwise.
8017 **/
8018static int
8019lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8020{
8021 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04008022
8023 /* For now, use the SLI4 device internal unrecoverable error
8024 * registers for error attention. This can be changed later.
8025 */
James Smarta747c9c2009-11-18 15:41:10 -05008026 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
8027 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
8028 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8029 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8031 "1423 HBA Unrecoverable error: "
8032 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8033 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
8034 uerr_sta_lo, uerr_sta_hi,
8035 phba->sli4_hba.ue_mask_lo,
8036 phba->sli4_hba.ue_mask_hi);
8037 phba->work_status[0] = uerr_sta_lo;
8038 phba->work_status[1] = uerr_sta_hi;
8039 /* Set the driver HA work bitmap */
8040 phba->work_ha |= HA_ERATT;
8041 /* Indicate polling handles this ERATT */
8042 phba->hba_flag |= HBA_ERATT_HANDLED;
8043 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008044 }
8045 return 0;
8046}
8047
8048/**
James Smart3621a712009-04-06 18:47:14 -04008049 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008050 * @phba: Pointer to HBA context.
8051 *
James Smart3772a992009-05-22 14:50:54 -04008052 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008053 * error attention register bit for error attention events.
8054 *
8055 * This fucntion returns 1 when there is Error Attention in the Host Attention
8056 * Register and returns 0 otherwise.
8057 **/
8058int
8059lpfc_sli_check_eratt(struct lpfc_hba *phba)
8060{
8061 uint32_t ha_copy;
8062
8063 /* If somebody is waiting to handle an eratt, don't process it
8064 * here. The brdkill function will do this.
8065 */
8066 if (phba->link_flag & LS_IGNORE_ERATT)
8067 return 0;
8068
8069 /* Check if interrupt handler handles this ERATT */
8070 spin_lock_irq(&phba->hbalock);
8071 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8072 /* Interrupt handler has handled ERATT */
8073 spin_unlock_irq(&phba->hbalock);
8074 return 0;
8075 }
8076
James Smarta257bf92009-04-06 18:48:10 -04008077 /*
8078 * If there is deferred error attention, do not check for error
8079 * attention
8080 */
8081 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8082 spin_unlock_irq(&phba->hbalock);
8083 return 0;
8084 }
8085
James Smart3772a992009-05-22 14:50:54 -04008086 /* If PCI channel is offline, don't process it */
8087 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008088 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008089 return 0;
8090 }
8091
8092 switch (phba->sli_rev) {
8093 case LPFC_SLI_REV2:
8094 case LPFC_SLI_REV3:
8095 /* Read chip Host Attention (HA) register */
8096 ha_copy = lpfc_sli_eratt_read(phba);
8097 break;
James Smartda0436e2009-05-22 14:51:39 -04008098 case LPFC_SLI_REV4:
8099 /* Read devcie Uncoverable Error (UERR) registers */
8100 ha_copy = lpfc_sli4_eratt_read(phba);
8101 break;
James Smart3772a992009-05-22 14:50:54 -04008102 default:
8103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8104 "0299 Invalid SLI revision (%d)\n",
8105 phba->sli_rev);
8106 ha_copy = 0;
8107 break;
James Smart93996272008-08-24 21:50:30 -04008108 }
8109 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008110
8111 return ha_copy;
8112}
8113
8114/**
8115 * lpfc_intr_state_check - Check device state for interrupt handling
8116 * @phba: Pointer to HBA context.
8117 *
8118 * This inline routine checks whether a device or its PCI slot is in a state
8119 * that the interrupt should be handled.
8120 *
8121 * This function returns 0 if the device or the PCI slot is in a state that
8122 * interrupt should be handled, otherwise -EIO.
8123 */
8124static inline int
8125lpfc_intr_state_check(struct lpfc_hba *phba)
8126{
8127 /* If the pci channel is offline, ignore all the interrupts */
8128 if (unlikely(pci_channel_offline(phba->pcidev)))
8129 return -EIO;
8130
8131 /* Update device level interrupt statistics */
8132 phba->sli.slistat.sli_intr++;
8133
8134 /* Ignore all interrupts during initialization. */
8135 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8136 return -EIO;
8137
James Smart93996272008-08-24 21:50:30 -04008138 return 0;
8139}
8140
8141/**
James Smart3772a992009-05-22 14:50:54 -04008142 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008143 * @irq: Interrupt number.
8144 * @dev_id: The device context pointer.
8145 *
James Smart93996272008-08-24 21:50:30 -04008146 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008147 * service routine when device with SLI-3 interface spec is enabled with
8148 * MSI-X multi-message interrupt mode and there are slow-path events in
8149 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8150 * interrupt mode, this function is called as part of the device-level
8151 * interrupt handler. When the PCI slot is in error recovery or the HBA
8152 * is undergoing initialization, the interrupt handler will not process
8153 * the interrupt. The link attention and ELS ring attention events are
8154 * handled by the worker thread. The interrupt handler signals the worker
8155 * thread and returns for these events. This function is called without
8156 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008157 * structures.
8158 *
8159 * This function returns IRQ_HANDLED when interrupt is handled else it
8160 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008161 **/
dea31012005-04-17 16:05:31 -05008162irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008163lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008164{
James Smart2e0fef82007-06-17 19:56:36 -05008165 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008166 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008167 uint32_t work_ha_copy;
8168 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008169 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008170 uint32_t control;
8171
James Smart92d7f7b2007-06-17 19:56:38 -05008172 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008173 struct lpfc_vport *vport;
8174 struct lpfc_nodelist *ndlp;
8175 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008176 LPFC_MBOXQ_t *pmb;
8177 int rc;
8178
dea31012005-04-17 16:05:31 -05008179 /*
8180 * Get the driver's phba structure from the dev_id and
8181 * assume the HBA is not interrupting.
8182 */
James Smart93996272008-08-24 21:50:30 -04008183 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008184
8185 if (unlikely(!phba))
8186 return IRQ_NONE;
8187
dea31012005-04-17 16:05:31 -05008188 /*
James Smart93996272008-08-24 21:50:30 -04008189 * Stuff needs to be attented to when this function is invoked as an
8190 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008191 */
James Smart93996272008-08-24 21:50:30 -04008192 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008193 /* Check device state for handling interrupt */
8194 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008195 return IRQ_NONE;
8196 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008197 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008198 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008199 /* If somebody is waiting to handle an eratt don't process it
8200 * here. The brdkill function will do this.
8201 */
8202 if (phba->link_flag & LS_IGNORE_ERATT)
8203 ha_copy &= ~HA_ERATT;
8204 /* Check the need for handling ERATT in interrupt handler */
8205 if (ha_copy & HA_ERATT) {
8206 if (phba->hba_flag & HBA_ERATT_HANDLED)
8207 /* ERATT polling has handled ERATT */
8208 ha_copy &= ~HA_ERATT;
8209 else
8210 /* Indicate interrupt handler handles ERATT */
8211 phba->hba_flag |= HBA_ERATT_HANDLED;
8212 }
James Smarta257bf92009-04-06 18:48:10 -04008213
8214 /*
8215 * If there is deferred error attention, do not check for any
8216 * interrupt.
8217 */
8218 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008219 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008220 return IRQ_NONE;
8221 }
8222
James Smart93996272008-08-24 21:50:30 -04008223 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008224 hc_copy = readl(phba->HCregaddr);
8225 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8226 HC_LAINT_ENA | HC_ERINT_ENA),
8227 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008228 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8229 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008230 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008231 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008232 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008233 } else
8234 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008235
dea31012005-04-17 16:05:31 -05008236 work_ha_copy = ha_copy & phba->work_ha_mask;
8237
James Smart93996272008-08-24 21:50:30 -04008238 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008239 if (work_ha_copy & HA_LATT) {
8240 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8241 /*
8242 * Turn off Link Attention interrupts
8243 * until CLEAR_LA done
8244 */
James Smart5b75da22008-12-04 22:39:35 -05008245 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008246 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8247 control = readl(phba->HCregaddr);
8248 control &= ~HC_LAINT_ENA;
8249 writel(control, phba->HCregaddr);
8250 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008251 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008252 }
8253 else
8254 work_ha_copy &= ~HA_LATT;
8255 }
8256
James Smart93996272008-08-24 21:50:30 -04008257 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008258 /*
8259 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8260 * the only slow ring.
8261 */
8262 status = (work_ha_copy &
8263 (HA_RXMASK << (4*LPFC_ELS_RING)));
8264 status >>= (4*LPFC_ELS_RING);
8265 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008266 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008267 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008268
8269 lpfc_debugfs_slow_ring_trc(phba,
8270 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8271 control, status,
8272 (uint32_t)phba->sli.slistat.sli_intr);
8273
James Smart858c9f62007-06-17 19:56:39 -05008274 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008275 lpfc_debugfs_slow_ring_trc(phba,
8276 "ISR Disable ring:"
8277 "pwork:x%x hawork:x%x wait:x%x",
8278 phba->work_ha, work_ha_copy,
8279 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008280 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008281
James Smart858c9f62007-06-17 19:56:39 -05008282 control &=
8283 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008284 writel(control, phba->HCregaddr);
8285 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008286 }
James Smarta58cbd52007-08-02 11:09:43 -04008287 else {
8288 lpfc_debugfs_slow_ring_trc(phba,
8289 "ISR slow ring: pwork:"
8290 "x%x hawork:x%x wait:x%x",
8291 phba->work_ha, work_ha_copy,
8292 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008293 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008294 }
James Smart5b75da22008-12-04 22:39:35 -05008295 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008296 }
8297 }
James Smart5b75da22008-12-04 22:39:35 -05008298 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008299 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008300 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008301 /*
8302 * Check if there is a deferred error condition
8303 * is active
8304 */
8305 if ((HS_FFER1 & phba->work_hs) &&
8306 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008307 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8308 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008309 phba->hba_flag |= DEFER_ERATT;
8310 /* Clear all interrupt enable conditions */
8311 writel(0, phba->HCregaddr);
8312 readl(phba->HCregaddr);
8313 }
8314 }
8315
James Smart93996272008-08-24 21:50:30 -04008316 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008317 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008318 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008319 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008320 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008321
8322 /* First check out the status word */
8323 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8324 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008325 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008326 /*
8327 * Stray Mailbox Interrupt, mbxCommand <cmd>
8328 * mbxStatus <status>
8329 */
James Smart09372822008-01-11 01:52:54 -05008330 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008331 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008332 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008333 "Interrupt mbxCommand x%x "
8334 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008335 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008336 pmbox->mbxCommand,
8337 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008338 /* clear mailbox attention bit */
8339 work_ha_copy &= ~HA_MBATT;
8340 } else {
James Smart97eab632008-04-07 10:16:05 -04008341 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008342 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008343 phba->last_completion_time = jiffies;
8344 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008345 if (pmb->mbox_cmpl) {
8346 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8347 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008348 if (pmb->out_ext_byte_len &&
8349 pmb->context2)
8350 lpfc_sli_pcimem_bcopy(
8351 phba->mbox_ext,
8352 pmb->context2,
8353 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008354 }
James Smart09372822008-01-11 01:52:54 -05008355 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8356 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8357
8358 lpfc_debugfs_disc_trc(vport,
8359 LPFC_DISC_TRC_MBOX_VPORT,
8360 "MBOX dflt rpi: : "
8361 "status:x%x rpi:x%x",
8362 (uint32_t)pmbox->mbxStatus,
8363 pmbox->un.varWords[0], 0);
8364
8365 if (!pmbox->mbxStatus) {
8366 mp = (struct lpfc_dmabuf *)
8367 (pmb->context1);
8368 ndlp = (struct lpfc_nodelist *)
8369 pmb->context2;
8370
8371 /* Reg_LOGIN of dflt RPI was
8372 * successful. new lets get
8373 * rid of the RPI using the
8374 * same mbox buffer.
8375 */
8376 lpfc_unreg_login(phba,
8377 vport->vpi,
8378 pmbox->un.varWords[0],
8379 pmb);
8380 pmb->mbox_cmpl =
8381 lpfc_mbx_cmpl_dflt_rpi;
8382 pmb->context1 = mp;
8383 pmb->context2 = ndlp;
8384 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008385 rc = lpfc_sli_issue_mbox(phba,
8386 pmb,
8387 MBX_NOWAIT);
8388 if (rc != MBX_BUSY)
8389 lpfc_printf_log(phba,
8390 KERN_ERR,
8391 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008392 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008393 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008394 if (rc != MBX_NOT_FINISHED)
8395 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008396 }
8397 }
James Smart5b75da22008-12-04 22:39:35 -05008398 spin_lock_irqsave(
8399 &phba->pport->work_port_lock,
8400 iflag);
James Smart09372822008-01-11 01:52:54 -05008401 phba->pport->work_port_events &=
8402 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008403 spin_unlock_irqrestore(
8404 &phba->pport->work_port_lock,
8405 iflag);
James Smart09372822008-01-11 01:52:54 -05008406 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008407 }
James Smart97eab632008-04-07 10:16:05 -04008408 } else
James Smart5b75da22008-12-04 22:39:35 -05008409 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008410
James Smart92d7f7b2007-06-17 19:56:38 -05008411 if ((work_ha_copy & HA_MBATT) &&
8412 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008413send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008414 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008415 do {
8416 rc = lpfc_sli_issue_mbox(phba, NULL,
8417 MBX_NOWAIT);
8418 } while (rc == MBX_NOT_FINISHED);
8419 if (rc != MBX_SUCCESS)
8420 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8421 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008422 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008423 }
8424
James Smart5b75da22008-12-04 22:39:35 -05008425 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008426 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008427 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008428 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008429 }
James Smart93996272008-08-24 21:50:30 -04008430 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008431
James Smart3772a992009-05-22 14:50:54 -04008432} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008433
8434/**
James Smart3772a992009-05-22 14:50:54 -04008435 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008436 * @irq: Interrupt number.
8437 * @dev_id: The device context pointer.
8438 *
8439 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008440 * service routine when device with SLI-3 interface spec is enabled with
8441 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8442 * ring event in the HBA. However, when the device is enabled with either
8443 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8444 * device-level interrupt handler. When the PCI slot is in error recovery
8445 * or the HBA is undergoing initialization, the interrupt handler will not
8446 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8447 * the intrrupt context. This function is called without any lock held.
8448 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008449 *
8450 * This function returns IRQ_HANDLED when interrupt is handled else it
8451 * returns IRQ_NONE.
8452 **/
8453irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008454lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008455{
8456 struct lpfc_hba *phba;
8457 uint32_t ha_copy;
8458 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008459 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008460
8461 /* Get the driver's phba structure from the dev_id and
8462 * assume the HBA is not interrupting.
8463 */
8464 phba = (struct lpfc_hba *) dev_id;
8465
8466 if (unlikely(!phba))
8467 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008468
8469 /*
James Smart93996272008-08-24 21:50:30 -04008470 * Stuff needs to be attented to when this function is invoked as an
8471 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008472 */
James Smart93996272008-08-24 21:50:30 -04008473 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008474 /* Check device state for handling interrupt */
8475 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008476 return IRQ_NONE;
8477 /* Need to read HA REG for FCP ring and other ring events */
8478 ha_copy = readl(phba->HAregaddr);
8479 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008480 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008481 /*
8482 * If there is deferred error attention, do not check for
8483 * any interrupt.
8484 */
8485 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008486 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008487 return IRQ_NONE;
8488 }
James Smart93996272008-08-24 21:50:30 -04008489 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8490 phba->HAregaddr);
8491 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008492 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008493 } else
8494 ha_copy = phba->ha_copy;
8495
8496 /*
8497 * Process all events on FCP ring. Take the optimized path for FCP IO.
8498 */
8499 ha_copy &= ~(phba->work_ha_mask);
8500
8501 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008502 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008503 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008504 lpfc_sli_handle_fast_ring_event(phba,
8505 &phba->sli.ring[LPFC_FCP_RING],
8506 status);
James Smarta4bc3372006-12-02 13:34:16 -05008507
8508 if (phba->cfg_multi_ring_support == 2) {
8509 /*
James Smart93996272008-08-24 21:50:30 -04008510 * Process all events on extra ring. Take the optimized path
8511 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008512 */
James Smart93996272008-08-24 21:50:30 -04008513 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008514 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008515 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008516 lpfc_sli_handle_fast_ring_event(phba,
8517 &phba->sli.ring[LPFC_EXTRA_RING],
8518 status);
8519 }
8520 }
dea31012005-04-17 16:05:31 -05008521 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008522} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008523
James Smart93996272008-08-24 21:50:30 -04008524/**
James Smart3772a992009-05-22 14:50:54 -04008525 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008526 * @irq: Interrupt number.
8527 * @dev_id: The device context pointer.
8528 *
James Smart3772a992009-05-22 14:50:54 -04008529 * This function is the HBA device-level interrupt handler to device with
8530 * SLI-3 interface spec, called from the PCI layer when either MSI or
8531 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8532 * requires driver attention. This function invokes the slow-path interrupt
8533 * attention handling function and fast-path interrupt attention handling
8534 * function in turn to process the relevant HBA attention events. This
8535 * function is called without any lock held. It gets the hbalock to access
8536 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008537 *
8538 * This function returns IRQ_HANDLED when interrupt is handled, else it
8539 * returns IRQ_NONE.
8540 **/
8541irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008542lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008543{
8544 struct lpfc_hba *phba;
8545 irqreturn_t sp_irq_rc, fp_irq_rc;
8546 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008547 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008548
8549 /*
8550 * Get the driver's phba structure from the dev_id and
8551 * assume the HBA is not interrupting.
8552 */
8553 phba = (struct lpfc_hba *) dev_id;
8554
8555 if (unlikely(!phba))
8556 return IRQ_NONE;
8557
James Smart3772a992009-05-22 14:50:54 -04008558 /* Check device state for handling interrupt */
8559 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008560 return IRQ_NONE;
8561
8562 spin_lock(&phba->hbalock);
8563 phba->ha_copy = readl(phba->HAregaddr);
8564 if (unlikely(!phba->ha_copy)) {
8565 spin_unlock(&phba->hbalock);
8566 return IRQ_NONE;
8567 } else if (phba->ha_copy & HA_ERATT) {
8568 if (phba->hba_flag & HBA_ERATT_HANDLED)
8569 /* ERATT polling has handled ERATT */
8570 phba->ha_copy &= ~HA_ERATT;
8571 else
8572 /* Indicate interrupt handler handles ERATT */
8573 phba->hba_flag |= HBA_ERATT_HANDLED;
8574 }
8575
James Smarta257bf92009-04-06 18:48:10 -04008576 /*
8577 * If there is deferred error attention, do not check for any interrupt.
8578 */
8579 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02008580 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04008581 return IRQ_NONE;
8582 }
8583
James Smart93996272008-08-24 21:50:30 -04008584 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008585 hc_copy = readl(phba->HCregaddr);
8586 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8587 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8588 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008589 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008590 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008591 readl(phba->HAregaddr); /* flush */
8592 spin_unlock(&phba->hbalock);
8593
8594 /*
8595 * Invokes slow-path host attention interrupt handling as appropriate.
8596 */
8597
8598 /* status of events with mailbox and link attention */
8599 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8600
8601 /* status of events with ELS ring */
8602 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8603 status2 >>= (4*LPFC_ELS_RING);
8604
8605 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008606 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008607 else
8608 sp_irq_rc = IRQ_NONE;
8609
8610 /*
8611 * Invoke fast-path host attention interrupt handling as appropriate.
8612 */
8613
8614 /* status of events with FCP ring */
8615 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8616 status1 >>= (4*LPFC_FCP_RING);
8617
8618 /* status of events with extra ring */
8619 if (phba->cfg_multi_ring_support == 2) {
8620 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8621 status2 >>= (4*LPFC_EXTRA_RING);
8622 } else
8623 status2 = 0;
8624
8625 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008626 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008627 else
8628 fp_irq_rc = IRQ_NONE;
8629
8630 /* Return device-level interrupt handling status */
8631 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008632} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008633
8634/**
8635 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8636 * @phba: pointer to lpfc hba data structure.
8637 *
8638 * This routine is invoked by the worker thread to process all the pending
8639 * SLI4 FCP abort XRI events.
8640 **/
8641void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8642{
8643 struct lpfc_cq_event *cq_event;
8644
8645 /* First, declare the fcp xri abort event has been handled */
8646 spin_lock_irq(&phba->hbalock);
8647 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8648 spin_unlock_irq(&phba->hbalock);
8649 /* Now, handle all the fcp xri abort events */
8650 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8651 /* Get the first event from the head of the event queue */
8652 spin_lock_irq(&phba->hbalock);
8653 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8654 cq_event, struct lpfc_cq_event, list);
8655 spin_unlock_irq(&phba->hbalock);
8656 /* Notify aborted XRI for FCP work queue */
8657 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8658 /* Free the event processed back to the free pool */
8659 lpfc_sli4_cq_event_release(phba, cq_event);
8660 }
8661}
8662
8663/**
8664 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8665 * @phba: pointer to lpfc hba data structure.
8666 *
8667 * This routine is invoked by the worker thread to process all the pending
8668 * SLI4 els abort xri events.
8669 **/
8670void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8671{
8672 struct lpfc_cq_event *cq_event;
8673
8674 /* First, declare the els xri abort event has been handled */
8675 spin_lock_irq(&phba->hbalock);
8676 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8677 spin_unlock_irq(&phba->hbalock);
8678 /* Now, handle all the els xri abort events */
8679 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8680 /* Get the first event from the head of the event queue */
8681 spin_lock_irq(&phba->hbalock);
8682 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8683 cq_event, struct lpfc_cq_event, list);
8684 spin_unlock_irq(&phba->hbalock);
8685 /* Notify aborted XRI for ELS work queue */
8686 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8687 /* Free the event processed back to the free pool */
8688 lpfc_sli4_cq_event_release(phba, cq_event);
8689 }
8690}
8691
James Smart341af102010-01-26 23:07:37 -05008692/**
8693 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8694 * @phba: pointer to lpfc hba data structure
8695 * @pIocbIn: pointer to the rspiocbq
8696 * @pIocbOut: pointer to the cmdiocbq
8697 * @wcqe: pointer to the complete wcqe
8698 *
8699 * This routine transfers the fields of a command iocbq to a response iocbq
8700 * by copying all the IOCB fields from command iocbq and transferring the
8701 * completion status information from the complete wcqe.
8702 **/
James Smart4f774512009-05-22 14:52:35 -04008703static void
James Smart341af102010-01-26 23:07:37 -05008704lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8705 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008706 struct lpfc_iocbq *pIocbOut,
8707 struct lpfc_wcqe_complete *wcqe)
8708{
James Smart341af102010-01-26 23:07:37 -05008709 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008710 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8711
8712 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8713 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008714 /* Map WCQE parameters into irspiocb parameters */
8715 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8716 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8717 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8718 pIocbIn->iocb.un.fcpi.fcpi_parm =
8719 pIocbOut->iocb.un.fcpi.fcpi_parm -
8720 wcqe->total_data_placed;
8721 else
8722 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008723 else {
James Smart4f774512009-05-22 14:52:35 -04008724 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008725 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8726 }
James Smart341af102010-01-26 23:07:37 -05008727
8728 /* Pick up HBA exchange busy condition */
8729 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8730 spin_lock_irqsave(&phba->hbalock, iflags);
8731 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8732 spin_unlock_irqrestore(&phba->hbalock, iflags);
8733 }
James Smart4f774512009-05-22 14:52:35 -04008734}
8735
8736/**
James Smart45ed1192009-10-02 15:17:02 -04008737 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8738 * @phba: Pointer to HBA context object.
8739 * @wcqe: Pointer to work-queue completion queue entry.
8740 *
8741 * This routine handles an ELS work-queue completion event and construct
8742 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8743 * discovery engine to handle.
8744 *
8745 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8746 **/
8747static struct lpfc_iocbq *
8748lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8749 struct lpfc_iocbq *irspiocbq)
8750{
8751 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8752 struct lpfc_iocbq *cmdiocbq;
8753 struct lpfc_wcqe_complete *wcqe;
8754 unsigned long iflags;
8755
8756 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8757 spin_lock_irqsave(&phba->hbalock, iflags);
8758 pring->stats.iocb_event++;
8759 /* Look up the ELS command IOCB and create pseudo response IOCB */
8760 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8761 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8762 spin_unlock_irqrestore(&phba->hbalock, iflags);
8763
8764 if (unlikely(!cmdiocbq)) {
8765 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8766 "0386 ELS complete with no corresponding "
8767 "cmdiocb: iotag (%d)\n",
8768 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8769 lpfc_sli_release_iocbq(phba, irspiocbq);
8770 return NULL;
8771 }
8772
8773 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008774 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008775
8776 return irspiocbq;
8777}
8778
8779/**
James Smart04c68492009-05-22 14:52:52 -04008780 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8781 * @phba: Pointer to HBA context object.
8782 * @cqe: Pointer to mailbox completion queue entry.
8783 *
8784 * This routine process a mailbox completion queue entry with asynchrous
8785 * event.
8786 *
8787 * Return: true if work posted to worker thread, otherwise false.
8788 **/
8789static bool
8790lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8791{
8792 struct lpfc_cq_event *cq_event;
8793 unsigned long iflags;
8794
8795 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8796 "0392 Async Event: word0:x%x, word1:x%x, "
8797 "word2:x%x, word3:x%x\n", mcqe->word0,
8798 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8799
8800 /* Allocate a new internal CQ_EVENT entry */
8801 cq_event = lpfc_sli4_cq_event_alloc(phba);
8802 if (!cq_event) {
8803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8804 "0394 Failed to allocate CQ_EVENT entry\n");
8805 return false;
8806 }
8807
8808 /* Move the CQE into an asynchronous event entry */
8809 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8810 spin_lock_irqsave(&phba->hbalock, iflags);
8811 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8812 /* Set the async event flag */
8813 phba->hba_flag |= ASYNC_EVENT;
8814 spin_unlock_irqrestore(&phba->hbalock, iflags);
8815
8816 return true;
8817}
8818
8819/**
8820 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8821 * @phba: Pointer to HBA context object.
8822 * @cqe: Pointer to mailbox completion queue entry.
8823 *
8824 * This routine process a mailbox completion queue entry with mailbox
8825 * completion event.
8826 *
8827 * Return: true if work posted to worker thread, otherwise false.
8828 **/
8829static bool
8830lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8831{
8832 uint32_t mcqe_status;
8833 MAILBOX_t *mbox, *pmbox;
8834 struct lpfc_mqe *mqe;
8835 struct lpfc_vport *vport;
8836 struct lpfc_nodelist *ndlp;
8837 struct lpfc_dmabuf *mp;
8838 unsigned long iflags;
8839 LPFC_MBOXQ_t *pmb;
8840 bool workposted = false;
8841 int rc;
8842
8843 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8844 if (!bf_get(lpfc_trailer_completed, mcqe))
8845 goto out_no_mqe_complete;
8846
8847 /* Get the reference to the active mbox command */
8848 spin_lock_irqsave(&phba->hbalock, iflags);
8849 pmb = phba->sli.mbox_active;
8850 if (unlikely(!pmb)) {
8851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8852 "1832 No pending MBOX command to handle\n");
8853 spin_unlock_irqrestore(&phba->hbalock, iflags);
8854 goto out_no_mqe_complete;
8855 }
8856 spin_unlock_irqrestore(&phba->hbalock, iflags);
8857 mqe = &pmb->u.mqe;
8858 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8859 mbox = phba->mbox;
8860 vport = pmb->vport;
8861
8862 /* Reset heartbeat timer */
8863 phba->last_completion_time = jiffies;
8864 del_timer(&phba->sli.mbox_tmo);
8865
8866 /* Move mbox data to caller's mailbox region, do endian swapping */
8867 if (pmb->mbox_cmpl && mbox)
8868 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8869 /* Set the mailbox status with SLI4 range 0x4000 */
8870 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8871 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8872 bf_set(lpfc_mqe_status, mqe,
8873 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8874
8875 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8876 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8877 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8878 "MBOX dflt rpi: status:x%x rpi:x%x",
8879 mcqe_status,
8880 pmbox->un.varWords[0], 0);
8881 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8882 mp = (struct lpfc_dmabuf *)(pmb->context1);
8883 ndlp = (struct lpfc_nodelist *)pmb->context2;
8884 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8885 * RID of the PPI using the same mbox buffer.
8886 */
8887 lpfc_unreg_login(phba, vport->vpi,
8888 pmbox->un.varWords[0], pmb);
8889 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8890 pmb->context1 = mp;
8891 pmb->context2 = ndlp;
8892 pmb->vport = vport;
8893 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8894 if (rc != MBX_BUSY)
8895 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8896 LOG_SLI, "0385 rc should "
8897 "have been MBX_BUSY\n");
8898 if (rc != MBX_NOT_FINISHED)
8899 goto send_current_mbox;
8900 }
8901 }
8902 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8903 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8904 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8905
8906 /* There is mailbox completion work to do */
8907 spin_lock_irqsave(&phba->hbalock, iflags);
8908 __lpfc_mbox_cmpl_put(phba, pmb);
8909 phba->work_ha |= HA_MBATT;
8910 spin_unlock_irqrestore(&phba->hbalock, iflags);
8911 workposted = true;
8912
8913send_current_mbox:
8914 spin_lock_irqsave(&phba->hbalock, iflags);
8915 /* Release the mailbox command posting token */
8916 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8917 /* Setting active mailbox pointer need to be in sync to flag clear */
8918 phba->sli.mbox_active = NULL;
8919 spin_unlock_irqrestore(&phba->hbalock, iflags);
8920 /* Wake up worker thread to post the next pending mailbox command */
8921 lpfc_worker_wake_up(phba);
8922out_no_mqe_complete:
8923 if (bf_get(lpfc_trailer_consumed, mcqe))
8924 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8925 return workposted;
8926}
8927
8928/**
8929 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8930 * @phba: Pointer to HBA context object.
8931 * @cqe: Pointer to mailbox completion queue entry.
8932 *
8933 * This routine process a mailbox completion queue entry, it invokes the
8934 * proper mailbox complete handling or asynchrous event handling routine
8935 * according to the MCQE's async bit.
8936 *
8937 * Return: true if work posted to worker thread, otherwise false.
8938 **/
8939static bool
8940lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8941{
8942 struct lpfc_mcqe mcqe;
8943 bool workposted;
8944
8945 /* Copy the mailbox MCQE and convert endian order as needed */
8946 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8947
8948 /* Invoke the proper event handling routine */
8949 if (!bf_get(lpfc_trailer_async, &mcqe))
8950 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8951 else
8952 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8953 return workposted;
8954}
8955
8956/**
James Smart4f774512009-05-22 14:52:35 -04008957 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8958 * @phba: Pointer to HBA context object.
8959 * @wcqe: Pointer to work-queue completion queue entry.
8960 *
8961 * This routine handles an ELS work-queue completion event.
8962 *
8963 * Return: true if work posted to worker thread, otherwise false.
8964 **/
8965static bool
8966lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8967 struct lpfc_wcqe_complete *wcqe)
8968{
James Smart4f774512009-05-22 14:52:35 -04008969 struct lpfc_iocbq *irspiocbq;
8970 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04008971 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04008972
James Smart45ed1192009-10-02 15:17:02 -04008973 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008974 irspiocbq = lpfc_sli_get_iocbq(phba);
8975 if (!irspiocbq) {
8976 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04008977 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8978 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8979 pring->txq_cnt, phba->iocb_cnt,
8980 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8981 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04008982 return false;
James Smart4f774512009-05-22 14:52:35 -04008983 }
James Smart4f774512009-05-22 14:52:35 -04008984
James Smart45ed1192009-10-02 15:17:02 -04008985 /* Save off the slow-path queue event for work thread to process */
8986 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008987 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008988 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008989 &phba->sli4_hba.sp_queue_event);
8990 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008991 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008992
James Smart45ed1192009-10-02 15:17:02 -04008993 return true;
James Smart4f774512009-05-22 14:52:35 -04008994}
8995
8996/**
8997 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8998 * @phba: Pointer to HBA context object.
8999 * @wcqe: Pointer to work-queue completion queue entry.
9000 *
9001 * This routine handles slow-path WQ entry comsumed event by invoking the
9002 * proper WQ release routine to the slow-path WQ.
9003 **/
9004static void
9005lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9006 struct lpfc_wcqe_release *wcqe)
9007{
9008 /* Check for the slow-path ELS work queue */
9009 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9010 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9011 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9012 else
9013 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9014 "2579 Slow-path wqe consume event carries "
9015 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9016 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9017 phba->sli4_hba.els_wq->queue_id);
9018}
9019
9020/**
9021 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9022 * @phba: Pointer to HBA context object.
9023 * @cq: Pointer to a WQ completion queue.
9024 * @wcqe: Pointer to work-queue completion queue entry.
9025 *
9026 * This routine handles an XRI abort event.
9027 *
9028 * Return: true if work posted to worker thread, otherwise false.
9029 **/
9030static bool
9031lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9032 struct lpfc_queue *cq,
9033 struct sli4_wcqe_xri_aborted *wcqe)
9034{
9035 bool workposted = false;
9036 struct lpfc_cq_event *cq_event;
9037 unsigned long iflags;
9038
9039 /* Allocate a new internal CQ_EVENT entry */
9040 cq_event = lpfc_sli4_cq_event_alloc(phba);
9041 if (!cq_event) {
9042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9043 "0602 Failed to allocate CQ_EVENT entry\n");
9044 return false;
9045 }
9046
9047 /* Move the CQE into the proper xri abort event list */
9048 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9049 switch (cq->subtype) {
9050 case LPFC_FCP:
9051 spin_lock_irqsave(&phba->hbalock, iflags);
9052 list_add_tail(&cq_event->list,
9053 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9054 /* Set the fcp xri abort event flag */
9055 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9056 spin_unlock_irqrestore(&phba->hbalock, iflags);
9057 workposted = true;
9058 break;
9059 case LPFC_ELS:
9060 spin_lock_irqsave(&phba->hbalock, iflags);
9061 list_add_tail(&cq_event->list,
9062 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9063 /* Set the els xri abort event flag */
9064 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9065 spin_unlock_irqrestore(&phba->hbalock, iflags);
9066 workposted = true;
9067 break;
9068 default:
9069 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9070 "0603 Invalid work queue CQE subtype (x%x)\n",
9071 cq->subtype);
9072 workposted = false;
9073 break;
9074 }
9075 return workposted;
9076}
9077
9078/**
James Smart4d9ab992009-10-02 15:16:39 -04009079 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009080 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009081 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009082 *
James Smart4d9ab992009-10-02 15:16:39 -04009083 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009084 *
9085 * Return: true if work posted to worker thread, otherwise false.
9086 **/
9087static bool
James Smart4d9ab992009-10-02 15:16:39 -04009088lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9089{
9090 bool workposted = false;
9091 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9092 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9093 struct hbq_dmabuf *dma_buf;
9094 uint32_t status;
9095 unsigned long iflags;
9096
James Smart4d9ab992009-10-02 15:16:39 -04009097 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9098 goto out;
9099
9100 status = bf_get(lpfc_rcqe_status, rcqe);
9101 switch (status) {
9102 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9104 "2537 Receive Frame Truncated!!\n");
9105 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009106 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009107 spin_lock_irqsave(&phba->hbalock, iflags);
9108 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9109 if (!dma_buf) {
9110 spin_unlock_irqrestore(&phba->hbalock, iflags);
9111 goto out;
9112 }
9113 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9114 /* save off the frame for the word thread to process */
9115 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009116 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009117 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009118 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009119 spin_unlock_irqrestore(&phba->hbalock, iflags);
9120 workposted = true;
9121 break;
9122 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9123 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9124 /* Post more buffers if possible */
9125 spin_lock_irqsave(&phba->hbalock, iflags);
9126 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9127 spin_unlock_irqrestore(&phba->hbalock, iflags);
9128 workposted = true;
9129 break;
9130 }
9131out:
9132 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009133}
9134
9135/**
9136 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9137 * @phba: Pointer to HBA context object.
9138 * @cq: Pointer to the completion queue.
9139 * @wcqe: Pointer to a completion queue entry.
9140 *
9141 * This routine process a slow-path work-queue or recieve queue completion queue
9142 * entry.
9143 *
9144 * Return: true if work posted to worker thread, otherwise false.
9145 **/
9146static bool
9147lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009148 struct lpfc_cqe *cqe)
9149{
James Smart45ed1192009-10-02 15:17:02 -04009150 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009151 bool workposted = false;
9152
9153 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009154 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009155
9156 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009157 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009158 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009159 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009160 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009161 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009162 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009163 break;
9164 case CQE_CODE_RELEASE_WQE:
9165 /* Process the WQ release event */
9166 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009167 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009168 break;
9169 case CQE_CODE_XRI_ABORTED:
9170 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009171 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009172 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009173 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009174 break;
James Smart4d9ab992009-10-02 15:16:39 -04009175 case CQE_CODE_RECEIVE:
9176 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009177 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009178 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009179 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009180 break;
James Smart4f774512009-05-22 14:52:35 -04009181 default:
9182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9183 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009184 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009185 break;
9186 }
9187 return workposted;
9188}
9189
9190/**
James Smart4f774512009-05-22 14:52:35 -04009191 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9192 * @phba: Pointer to HBA context object.
9193 * @eqe: Pointer to fast-path event queue entry.
9194 *
9195 * This routine process a event queue entry from the slow-path event queue.
9196 * It will check the MajorCode and MinorCode to determine this is for a
9197 * completion event on a completion queue, if not, an error shall be logged
9198 * and just return. Otherwise, it will get to the corresponding completion
9199 * queue and process all the entries on that completion queue, rearm the
9200 * completion queue, and then return.
9201 *
9202 **/
9203static void
9204lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9205{
9206 struct lpfc_queue *cq = NULL, *childq, *speq;
9207 struct lpfc_cqe *cqe;
9208 bool workposted = false;
9209 int ecount = 0;
9210 uint16_t cqid;
9211
James Smartcb5172e2010-03-15 11:25:07 -04009212 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009213 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9214 "0359 Not a valid slow-path completion "
9215 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009216 bf_get_le32(lpfc_eqe_major_code, eqe),
9217 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009218 return;
9219 }
9220
9221 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009222 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009223
9224 /* Search for completion queue pointer matching this cqid */
9225 speq = phba->sli4_hba.sp_eq;
9226 list_for_each_entry(childq, &speq->child_list, list) {
9227 if (childq->queue_id == cqid) {
9228 cq = childq;
9229 break;
9230 }
9231 }
9232 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009233 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9235 "0365 Slow-path CQ identifier "
9236 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009237 return;
9238 }
9239
9240 /* Process all the entries to the CQ */
9241 switch (cq->type) {
9242 case LPFC_MCQ:
9243 while ((cqe = lpfc_sli4_cq_get(cq))) {
9244 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9245 if (!(++ecount % LPFC_GET_QE_REL_INT))
9246 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9247 }
9248 break;
9249 case LPFC_WCQ:
9250 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009251 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009252 if (!(++ecount % LPFC_GET_QE_REL_INT))
9253 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9254 }
9255 break;
9256 default:
9257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9258 "0370 Invalid completion queue type (%d)\n",
9259 cq->type);
9260 return;
9261 }
9262
9263 /* Catch the no cq entry condition, log an error */
9264 if (unlikely(ecount == 0))
9265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9266 "0371 No entry from the CQ: identifier "
9267 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9268
9269 /* In any case, flash and re-arm the RCQ */
9270 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9271
9272 /* wake up worker thread if there are works to be done */
9273 if (workposted)
9274 lpfc_worker_wake_up(phba);
9275}
9276
9277/**
9278 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9279 * @eqe: Pointer to fast-path completion queue entry.
9280 *
9281 * This routine process a fast-path work queue completion entry from fast-path
9282 * event queue for FCP command response completion.
9283 **/
9284static void
9285lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9286 struct lpfc_wcqe_complete *wcqe)
9287{
9288 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9289 struct lpfc_iocbq *cmdiocbq;
9290 struct lpfc_iocbq irspiocbq;
9291 unsigned long iflags;
9292
9293 spin_lock_irqsave(&phba->hbalock, iflags);
9294 pring->stats.iocb_event++;
9295 spin_unlock_irqrestore(&phba->hbalock, iflags);
9296
9297 /* Check for response status */
9298 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9299 /* If resource errors reported from HBA, reduce queue
9300 * depth of the SCSI device.
9301 */
9302 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9303 IOSTAT_LOCAL_REJECT) &&
9304 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9305 phba->lpfc_rampdown_queue_depth(phba);
9306 }
9307 /* Log the error status */
9308 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9309 "0373 FCP complete error: status=x%x, "
9310 "hw_status=x%x, total_data_specified=%d, "
9311 "parameter=x%x, word3=x%x\n",
9312 bf_get(lpfc_wcqe_c_status, wcqe),
9313 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9314 wcqe->total_data_placed, wcqe->parameter,
9315 wcqe->word3);
9316 }
9317
9318 /* Look up the FCP command IOCB and create pseudo response IOCB */
9319 spin_lock_irqsave(&phba->hbalock, iflags);
9320 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9321 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9322 spin_unlock_irqrestore(&phba->hbalock, iflags);
9323 if (unlikely(!cmdiocbq)) {
9324 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9325 "0374 FCP complete with no corresponding "
9326 "cmdiocb: iotag (%d)\n",
9327 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9328 return;
9329 }
9330 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9332 "0375 FCP cmdiocb not callback function "
9333 "iotag: (%d)\n",
9334 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9335 return;
9336 }
9337
9338 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009339 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009340
James Smart0f65ff62010-02-26 14:14:23 -05009341 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9342 spin_lock_irqsave(&phba->hbalock, iflags);
9343 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9344 spin_unlock_irqrestore(&phba->hbalock, iflags);
9345 }
9346
James Smart4f774512009-05-22 14:52:35 -04009347 /* Pass the cmd_iocb and the rsp state to the upper layer */
9348 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9349}
9350
9351/**
9352 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9353 * @phba: Pointer to HBA context object.
9354 * @cq: Pointer to completion queue.
9355 * @wcqe: Pointer to work-queue completion queue entry.
9356 *
9357 * This routine handles an fast-path WQ entry comsumed event by invoking the
9358 * proper WQ release routine to the slow-path WQ.
9359 **/
9360static void
9361lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9362 struct lpfc_wcqe_release *wcqe)
9363{
9364 struct lpfc_queue *childwq;
9365 bool wqid_matched = false;
9366 uint16_t fcp_wqid;
9367
9368 /* Check for fast-path FCP work queue release */
9369 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9370 list_for_each_entry(childwq, &cq->child_list, list) {
9371 if (childwq->queue_id == fcp_wqid) {
9372 lpfc_sli4_wq_release(childwq,
9373 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9374 wqid_matched = true;
9375 break;
9376 }
9377 }
9378 /* Report warning log message if no match found */
9379 if (wqid_matched != true)
9380 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9381 "2580 Fast-path wqe consume event carries "
9382 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9383}
9384
9385/**
9386 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9387 * @cq: Pointer to the completion queue.
9388 * @eqe: Pointer to fast-path completion queue entry.
9389 *
9390 * This routine process a fast-path work queue completion entry from fast-path
9391 * event queue for FCP command response completion.
9392 **/
9393static int
9394lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9395 struct lpfc_cqe *cqe)
9396{
9397 struct lpfc_wcqe_release wcqe;
9398 bool workposted = false;
9399
9400 /* Copy the work queue CQE and convert endian order if needed */
9401 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9402
9403 /* Check and process for different type of WCQE and dispatch */
9404 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9405 case CQE_CODE_COMPL_WQE:
9406 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009407 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009408 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9409 (struct lpfc_wcqe_complete *)&wcqe);
9410 break;
9411 case CQE_CODE_RELEASE_WQE:
9412 /* Process the WQ release event */
9413 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9414 (struct lpfc_wcqe_release *)&wcqe);
9415 break;
9416 case CQE_CODE_XRI_ABORTED:
9417 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009418 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009419 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9420 (struct sli4_wcqe_xri_aborted *)&wcqe);
9421 break;
9422 default:
9423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9424 "0144 Not a valid WCQE code: x%x\n",
9425 bf_get(lpfc_wcqe_c_code, &wcqe));
9426 break;
9427 }
9428 return workposted;
9429}
9430
9431/**
9432 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9433 * @phba: Pointer to HBA context object.
9434 * @eqe: Pointer to fast-path event queue entry.
9435 *
9436 * This routine process a event queue entry from the fast-path event queue.
9437 * It will check the MajorCode and MinorCode to determine this is for a
9438 * completion event on a completion queue, if not, an error shall be logged
9439 * and just return. Otherwise, it will get to the corresponding completion
9440 * queue and process all the entries on the completion queue, rearm the
9441 * completion queue, and then return.
9442 **/
9443static void
9444lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9445 uint32_t fcp_cqidx)
9446{
9447 struct lpfc_queue *cq;
9448 struct lpfc_cqe *cqe;
9449 bool workposted = false;
9450 uint16_t cqid;
9451 int ecount = 0;
9452
James Smartcb5172e2010-03-15 11:25:07 -04009453 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009454 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9455 "0366 Not a valid fast-path completion "
9456 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009457 bf_get_le32(lpfc_eqe_major_code, eqe),
9458 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009459 return;
9460 }
9461
9462 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9463 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009464 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9466 "0367 Fast-path completion queue "
9467 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009468 return;
9469 }
9470
9471 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009472 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009473 if (unlikely(cqid != cq->queue_id)) {
9474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9475 "0368 Miss-matched fast-path completion "
9476 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9477 cqid, cq->queue_id);
9478 return;
9479 }
9480
9481 /* Process all the entries to the CQ */
9482 while ((cqe = lpfc_sli4_cq_get(cq))) {
9483 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9484 if (!(++ecount % LPFC_GET_QE_REL_INT))
9485 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9486 }
9487
9488 /* Catch the no cq entry condition */
9489 if (unlikely(ecount == 0))
9490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9491 "0369 No entry from fast-path completion "
9492 "queue fcpcqid=%d\n", cq->queue_id);
9493
9494 /* In any case, flash and re-arm the CQ */
9495 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9496
9497 /* wake up worker thread if there are works to be done */
9498 if (workposted)
9499 lpfc_worker_wake_up(phba);
9500}
9501
9502static void
9503lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9504{
9505 struct lpfc_eqe *eqe;
9506
9507 /* walk all the EQ entries and drop on the floor */
9508 while ((eqe = lpfc_sli4_eq_get(eq)))
9509 ;
9510
9511 /* Clear and re-arm the EQ */
9512 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9513}
9514
9515/**
9516 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9517 * @irq: Interrupt number.
9518 * @dev_id: The device context pointer.
9519 *
9520 * This function is directly called from the PCI layer as an interrupt
9521 * service routine when device with SLI-4 interface spec is enabled with
9522 * MSI-X multi-message interrupt mode and there are slow-path events in
9523 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9524 * interrupt mode, this function is called as part of the device-level
9525 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9526 * undergoing initialization, the interrupt handler will not process the
9527 * interrupt. The link attention and ELS ring attention events are handled
9528 * by the worker thread. The interrupt handler signals the worker thread
9529 * and returns for these events. This function is called without any lock
9530 * held. It gets the hbalock to access and update SLI data structures.
9531 *
9532 * This function returns IRQ_HANDLED when interrupt is handled else it
9533 * returns IRQ_NONE.
9534 **/
9535irqreturn_t
9536lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9537{
9538 struct lpfc_hba *phba;
9539 struct lpfc_queue *speq;
9540 struct lpfc_eqe *eqe;
9541 unsigned long iflag;
9542 int ecount = 0;
9543
9544 /*
9545 * Get the driver's phba structure from the dev_id
9546 */
9547 phba = (struct lpfc_hba *)dev_id;
9548
9549 if (unlikely(!phba))
9550 return IRQ_NONE;
9551
9552 /* Get to the EQ struct associated with this vector */
9553 speq = phba->sli4_hba.sp_eq;
9554
9555 /* Check device state for handling interrupt */
9556 if (unlikely(lpfc_intr_state_check(phba))) {
9557 /* Check again for link_state with lock held */
9558 spin_lock_irqsave(&phba->hbalock, iflag);
9559 if (phba->link_state < LPFC_LINK_DOWN)
9560 /* Flush, clear interrupt, and rearm the EQ */
9561 lpfc_sli4_eq_flush(phba, speq);
9562 spin_unlock_irqrestore(&phba->hbalock, iflag);
9563 return IRQ_NONE;
9564 }
9565
9566 /*
9567 * Process all the event on FCP slow-path EQ
9568 */
9569 while ((eqe = lpfc_sli4_eq_get(speq))) {
9570 lpfc_sli4_sp_handle_eqe(phba, eqe);
9571 if (!(++ecount % LPFC_GET_QE_REL_INT))
9572 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9573 }
9574
9575 /* Always clear and re-arm the slow-path EQ */
9576 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9577
9578 /* Catch the no cq entry condition */
9579 if (unlikely(ecount == 0)) {
9580 if (phba->intr_type == MSIX)
9581 /* MSI-X treated interrupt served as no EQ share INT */
9582 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9583 "0357 MSI-X interrupt with no EQE\n");
9584 else
9585 /* Non MSI-X treated on interrupt as EQ share INT */
9586 return IRQ_NONE;
9587 }
9588
9589 return IRQ_HANDLED;
9590} /* lpfc_sli4_sp_intr_handler */
9591
9592/**
9593 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9594 * @irq: Interrupt number.
9595 * @dev_id: The device context pointer.
9596 *
9597 * This function is directly called from the PCI layer as an interrupt
9598 * service routine when device with SLI-4 interface spec is enabled with
9599 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9600 * ring event in the HBA. However, when the device is enabled with either
9601 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9602 * device-level interrupt handler. When the PCI slot is in error recovery
9603 * or the HBA is undergoing initialization, the interrupt handler will not
9604 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9605 * the intrrupt context. This function is called without any lock held.
9606 * It gets the hbalock to access and update SLI data structures. Note that,
9607 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9608 * equal to that of FCP CQ index.
9609 *
9610 * This function returns IRQ_HANDLED when interrupt is handled else it
9611 * returns IRQ_NONE.
9612 **/
9613irqreturn_t
9614lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9615{
9616 struct lpfc_hba *phba;
9617 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9618 struct lpfc_queue *fpeq;
9619 struct lpfc_eqe *eqe;
9620 unsigned long iflag;
9621 int ecount = 0;
9622 uint32_t fcp_eqidx;
9623
9624 /* Get the driver's phba structure from the dev_id */
9625 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9626 phba = fcp_eq_hdl->phba;
9627 fcp_eqidx = fcp_eq_hdl->idx;
9628
9629 if (unlikely(!phba))
9630 return IRQ_NONE;
9631
9632 /* Get to the EQ struct associated with this vector */
9633 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9634
9635 /* Check device state for handling interrupt */
9636 if (unlikely(lpfc_intr_state_check(phba))) {
9637 /* Check again for link_state with lock held */
9638 spin_lock_irqsave(&phba->hbalock, iflag);
9639 if (phba->link_state < LPFC_LINK_DOWN)
9640 /* Flush, clear interrupt, and rearm the EQ */
9641 lpfc_sli4_eq_flush(phba, fpeq);
9642 spin_unlock_irqrestore(&phba->hbalock, iflag);
9643 return IRQ_NONE;
9644 }
9645
9646 /*
9647 * Process all the event on FCP fast-path EQ
9648 */
9649 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9650 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9651 if (!(++ecount % LPFC_GET_QE_REL_INT))
9652 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9653 }
9654
9655 /* Always clear and re-arm the fast-path EQ */
9656 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9657
9658 if (unlikely(ecount == 0)) {
9659 if (phba->intr_type == MSIX)
9660 /* MSI-X treated interrupt served as no EQ share INT */
9661 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9662 "0358 MSI-X interrupt with no EQE\n");
9663 else
9664 /* Non MSI-X treated on interrupt as EQ share INT */
9665 return IRQ_NONE;
9666 }
9667
9668 return IRQ_HANDLED;
9669} /* lpfc_sli4_fp_intr_handler */
9670
9671/**
9672 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9673 * @irq: Interrupt number.
9674 * @dev_id: The device context pointer.
9675 *
9676 * This function is the device-level interrupt handler to device with SLI-4
9677 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9678 * interrupt mode is enabled and there is an event in the HBA which requires
9679 * driver attention. This function invokes the slow-path interrupt attention
9680 * handling function and fast-path interrupt attention handling function in
9681 * turn to process the relevant HBA attention events. This function is called
9682 * without any lock held. It gets the hbalock to access and update SLI data
9683 * structures.
9684 *
9685 * This function returns IRQ_HANDLED when interrupt is handled, else it
9686 * returns IRQ_NONE.
9687 **/
9688irqreturn_t
9689lpfc_sli4_intr_handler(int irq, void *dev_id)
9690{
9691 struct lpfc_hba *phba;
9692 irqreturn_t sp_irq_rc, fp_irq_rc;
9693 bool fp_handled = false;
9694 uint32_t fcp_eqidx;
9695
9696 /* Get the driver's phba structure from the dev_id */
9697 phba = (struct lpfc_hba *)dev_id;
9698
9699 if (unlikely(!phba))
9700 return IRQ_NONE;
9701
9702 /*
9703 * Invokes slow-path host attention interrupt handling as appropriate.
9704 */
9705 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9706
9707 /*
9708 * Invoke fast-path host attention interrupt handling as appropriate.
9709 */
9710 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9711 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9712 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9713 if (fp_irq_rc == IRQ_HANDLED)
9714 fp_handled |= true;
9715 }
9716
9717 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9718} /* lpfc_sli4_intr_handler */
9719
9720/**
9721 * lpfc_sli4_queue_free - free a queue structure and associated memory
9722 * @queue: The queue structure to free.
9723 *
9724 * This function frees a queue structure and the DMAable memeory used for
9725 * the host resident queue. This function must be called after destroying the
9726 * queue on the HBA.
9727 **/
9728void
9729lpfc_sli4_queue_free(struct lpfc_queue *queue)
9730{
9731 struct lpfc_dmabuf *dmabuf;
9732
9733 if (!queue)
9734 return;
9735
9736 while (!list_empty(&queue->page_list)) {
9737 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9738 list);
James Smart49198b32010-04-06 15:04:33 -04009739 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009740 dmabuf->virt, dmabuf->phys);
9741 kfree(dmabuf);
9742 }
9743 kfree(queue);
9744 return;
9745}
9746
9747/**
9748 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9749 * @phba: The HBA that this queue is being created on.
9750 * @entry_size: The size of each queue entry for this queue.
9751 * @entry count: The number of entries that this queue will handle.
9752 *
9753 * This function allocates a queue structure and the DMAable memory used for
9754 * the host resident queue. This function must be called before creating the
9755 * queue on the HBA.
9756 **/
9757struct lpfc_queue *
9758lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9759 uint32_t entry_count)
9760{
9761 struct lpfc_queue *queue;
9762 struct lpfc_dmabuf *dmabuf;
9763 int x, total_qe_count;
9764 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009765 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009766
James Smartcb5172e2010-03-15 11:25:07 -04009767 if (!phba->sli4_hba.pc_sli4_params.supported)
9768 hw_page_size = SLI4_PAGE_SIZE;
9769
James Smart4f774512009-05-22 14:52:35 -04009770 queue = kzalloc(sizeof(struct lpfc_queue) +
9771 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9772 if (!queue)
9773 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009774 queue->page_count = (ALIGN(entry_size * entry_count,
9775 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009776 INIT_LIST_HEAD(&queue->list);
9777 INIT_LIST_HEAD(&queue->page_list);
9778 INIT_LIST_HEAD(&queue->child_list);
9779 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9780 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9781 if (!dmabuf)
9782 goto out_fail;
9783 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009784 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009785 GFP_KERNEL);
9786 if (!dmabuf->virt) {
9787 kfree(dmabuf);
9788 goto out_fail;
9789 }
James Smartcb5172e2010-03-15 11:25:07 -04009790 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009791 dmabuf->buffer_tag = x;
9792 list_add_tail(&dmabuf->list, &queue->page_list);
9793 /* initialize queue's entry array */
9794 dma_pointer = dmabuf->virt;
9795 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009796 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009797 total_qe_count++, dma_pointer += entry_size) {
9798 queue->qe[total_qe_count].address = dma_pointer;
9799 }
9800 }
9801 queue->entry_size = entry_size;
9802 queue->entry_count = entry_count;
9803 queue->phba = phba;
9804
9805 return queue;
9806out_fail:
9807 lpfc_sli4_queue_free(queue);
9808 return NULL;
9809}
9810
9811/**
9812 * lpfc_eq_create - Create an Event Queue on the HBA
9813 * @phba: HBA structure that indicates port to create a queue on.
9814 * @eq: The queue structure to use to create the event queue.
9815 * @imax: The maximum interrupt per second limit.
9816 *
9817 * This function creates an event queue, as detailed in @eq, on a port,
9818 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9819 *
9820 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9821 * is used to get the entry count and entry size that are necessary to
9822 * determine the number of pages to allocate and use for this queue. This
9823 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9824 * event queue. This function is asynchronous and will wait for the mailbox
9825 * command to finish before continuing.
9826 *
9827 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -04009828 * memory this function will return -ENOMEM. If the queue create mailbox command
9829 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -04009830 **/
9831uint32_t
9832lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9833{
9834 struct lpfc_mbx_eq_create *eq_create;
9835 LPFC_MBOXQ_t *mbox;
9836 int rc, length, status = 0;
9837 struct lpfc_dmabuf *dmabuf;
9838 uint32_t shdr_status, shdr_add_status;
9839 union lpfc_sli4_cfg_shdr *shdr;
9840 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009841 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9842
9843 if (!phba->sli4_hba.pc_sli4_params.supported)
9844 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009845
9846 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9847 if (!mbox)
9848 return -ENOMEM;
9849 length = (sizeof(struct lpfc_mbx_eq_create) -
9850 sizeof(struct lpfc_sli4_cfg_mhdr));
9851 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9852 LPFC_MBOX_OPCODE_EQ_CREATE,
9853 length, LPFC_SLI4_MBX_EMBED);
9854 eq_create = &mbox->u.mqe.un.eq_create;
9855 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9856 eq->page_count);
9857 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9858 LPFC_EQE_SIZE);
9859 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9860 /* Calculate delay multiper from maximum interrupt per second */
9861 dmult = LPFC_DMULT_CONST/imax - 1;
9862 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9863 dmult);
9864 switch (eq->entry_count) {
9865 default:
9866 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9867 "0360 Unsupported EQ count. (%d)\n",
9868 eq->entry_count);
9869 if (eq->entry_count < 256)
9870 return -EINVAL;
9871 /* otherwise default to smallest count (drop through) */
9872 case 256:
9873 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9874 LPFC_EQ_CNT_256);
9875 break;
9876 case 512:
9877 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9878 LPFC_EQ_CNT_512);
9879 break;
9880 case 1024:
9881 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9882 LPFC_EQ_CNT_1024);
9883 break;
9884 case 2048:
9885 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9886 LPFC_EQ_CNT_2048);
9887 break;
9888 case 4096:
9889 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9890 LPFC_EQ_CNT_4096);
9891 break;
9892 }
9893 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009894 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009895 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9896 putPaddrLow(dmabuf->phys);
9897 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9898 putPaddrHigh(dmabuf->phys);
9899 }
9900 mbox->vport = phba->pport;
9901 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9902 mbox->context1 = NULL;
9903 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9904 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9905 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9906 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9907 if (shdr_status || shdr_add_status || rc) {
9908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9909 "2500 EQ_CREATE mailbox failed with "
9910 "status x%x add_status x%x, mbx status x%x\n",
9911 shdr_status, shdr_add_status, rc);
9912 status = -ENXIO;
9913 }
9914 eq->type = LPFC_EQ;
9915 eq->subtype = LPFC_NONE;
9916 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9917 if (eq->queue_id == 0xFFFF)
9918 status = -ENXIO;
9919 eq->host_index = 0;
9920 eq->hba_index = 0;
9921
James Smart8fa38512009-07-19 10:01:03 -04009922 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009923 return status;
9924}
9925
9926/**
9927 * lpfc_cq_create - Create a Completion Queue on the HBA
9928 * @phba: HBA structure that indicates port to create a queue on.
9929 * @cq: The queue structure to use to create the completion queue.
9930 * @eq: The event queue to bind this completion queue to.
9931 *
9932 * This function creates a completion queue, as detailed in @wq, on a port,
9933 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9934 *
9935 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9936 * is used to get the entry count and entry size that are necessary to
9937 * determine the number of pages to allocate and use for this queue. The @eq
9938 * is used to indicate which event queue to bind this completion queue to. This
9939 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9940 * completion queue. This function is asynchronous and will wait for the mailbox
9941 * command to finish before continuing.
9942 *
9943 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -04009944 * memory this function will return -ENOMEM. If the queue create mailbox command
9945 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -04009946 **/
9947uint32_t
9948lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9949 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9950{
9951 struct lpfc_mbx_cq_create *cq_create;
9952 struct lpfc_dmabuf *dmabuf;
9953 LPFC_MBOXQ_t *mbox;
9954 int rc, length, status = 0;
9955 uint32_t shdr_status, shdr_add_status;
9956 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009957 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9958
9959 if (!phba->sli4_hba.pc_sli4_params.supported)
9960 hw_page_size = SLI4_PAGE_SIZE;
9961
James Smart4f774512009-05-22 14:52:35 -04009962
9963 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9964 if (!mbox)
9965 return -ENOMEM;
9966 length = (sizeof(struct lpfc_mbx_cq_create) -
9967 sizeof(struct lpfc_sli4_cfg_mhdr));
9968 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9969 LPFC_MBOX_OPCODE_CQ_CREATE,
9970 length, LPFC_SLI4_MBX_EMBED);
9971 cq_create = &mbox->u.mqe.un.cq_create;
9972 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9973 cq->page_count);
9974 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9975 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9976 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9977 switch (cq->entry_count) {
9978 default:
9979 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9980 "0361 Unsupported CQ count. (%d)\n",
9981 cq->entry_count);
9982 if (cq->entry_count < 256)
9983 return -EINVAL;
9984 /* otherwise default to smallest count (drop through) */
9985 case 256:
9986 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9987 LPFC_CQ_CNT_256);
9988 break;
9989 case 512:
9990 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9991 LPFC_CQ_CNT_512);
9992 break;
9993 case 1024:
9994 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9995 LPFC_CQ_CNT_1024);
9996 break;
9997 }
9998 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009999 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010000 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10001 putPaddrLow(dmabuf->phys);
10002 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10003 putPaddrHigh(dmabuf->phys);
10004 }
10005 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10006
10007 /* The IOCTL status is embedded in the mailbox subheader. */
10008 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10009 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10010 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10011 if (shdr_status || shdr_add_status || rc) {
10012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10013 "2501 CQ_CREATE mailbox failed with "
10014 "status x%x add_status x%x, mbx status x%x\n",
10015 shdr_status, shdr_add_status, rc);
10016 status = -ENXIO;
10017 goto out;
10018 }
10019 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10020 if (cq->queue_id == 0xFFFF) {
10021 status = -ENXIO;
10022 goto out;
10023 }
10024 /* link the cq onto the parent eq child list */
10025 list_add_tail(&cq->list, &eq->child_list);
10026 /* Set up completion queue's type and subtype */
10027 cq->type = type;
10028 cq->subtype = subtype;
10029 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10030 cq->host_index = 0;
10031 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010032
James Smart8fa38512009-07-19 10:01:03 -040010033out:
10034 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010035 return status;
10036}
10037
10038/**
James Smartb19a0612010-04-06 14:48:51 -040010039 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010040 * @phba: HBA structure that indicates port to create a queue on.
10041 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010042 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10043 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010044 *
James Smartb19a0612010-04-06 14:48:51 -040010045 * This function provides failback (fb) functionality when the
10046 * mq_create_ext fails on older FW generations. It's purpose is identical
10047 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010048 *
James Smartb19a0612010-04-06 14:48:51 -040010049 * This routine cannot fail as all attributes were previously accessed and
10050 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010051 **/
James Smartb19a0612010-04-06 14:48:51 -040010052static void
10053lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10054 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010055{
10056 struct lpfc_mbx_mq_create *mq_create;
10057 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010058 int length;
James Smart04c68492009-05-22 14:52:52 -040010059
James Smart04c68492009-05-22 14:52:52 -040010060 length = (sizeof(struct lpfc_mbx_mq_create) -
10061 sizeof(struct lpfc_sli4_cfg_mhdr));
10062 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10063 LPFC_MBOX_OPCODE_MQ_CREATE,
10064 length, LPFC_SLI4_MBX_EMBED);
10065 mq_create = &mbox->u.mqe.un.mq_create;
10066 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010067 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010068 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010069 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010070 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10071 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010072 case 16:
10073 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10074 LPFC_MQ_CNT_16);
10075 break;
10076 case 32:
10077 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10078 LPFC_MQ_CNT_32);
10079 break;
10080 case 64:
10081 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10082 LPFC_MQ_CNT_64);
10083 break;
10084 case 128:
10085 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10086 LPFC_MQ_CNT_128);
10087 break;
10088 }
10089 list_for_each_entry(dmabuf, &mq->page_list, list) {
10090 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010091 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010092 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010093 putPaddrHigh(dmabuf->phys);
10094 }
10095}
10096
10097/**
10098 * lpfc_mq_create - Create a mailbox Queue on the HBA
10099 * @phba: HBA structure that indicates port to create a queue on.
10100 * @mq: The queue structure to use to create the mailbox queue.
10101 * @cq: The completion queue to associate with this cq.
10102 * @subtype: The queue's subtype.
10103 *
10104 * This function creates a mailbox queue, as detailed in @mq, on a port,
10105 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10106 *
10107 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10108 * is used to get the entry count and entry size that are necessary to
10109 * determine the number of pages to allocate and use for this queue. This
10110 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10111 * mailbox queue. This function is asynchronous and will wait for the mailbox
10112 * command to finish before continuing.
10113 *
10114 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010115 * memory this function will return -ENOMEM. If the queue create mailbox command
10116 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010117 **/
10118int32_t
10119lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10120 struct lpfc_queue *cq, uint32_t subtype)
10121{
10122 struct lpfc_mbx_mq_create *mq_create;
10123 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10124 struct lpfc_dmabuf *dmabuf;
10125 LPFC_MBOXQ_t *mbox;
10126 int rc, length, status = 0;
10127 uint32_t shdr_status, shdr_add_status;
10128 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010129 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010130
James Smart49198b32010-04-06 15:04:33 -040010131 if (!phba->sli4_hba.pc_sli4_params.supported)
10132 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010133
10134 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10135 if (!mbox)
10136 return -ENOMEM;
10137 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10138 sizeof(struct lpfc_sli4_cfg_mhdr));
10139 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10140 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10141 length, LPFC_SLI4_MBX_EMBED);
10142
10143 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10144 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10145 mq->page_count);
10146 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10147 1);
10148 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10149 &mq_create_ext->u.request, 1);
10150 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10151 &mq_create_ext->u.request, 1);
10152 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10153 cq->queue_id);
10154 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10155 switch (mq->entry_count) {
10156 default:
10157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10158 "0362 Unsupported MQ count. (%d)\n",
10159 mq->entry_count);
10160 if (mq->entry_count < 16)
10161 return -EINVAL;
10162 /* otherwise default to smallest count (drop through) */
10163 case 16:
10164 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10165 LPFC_MQ_CNT_16);
10166 break;
10167 case 32:
10168 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10169 LPFC_MQ_CNT_32);
10170 break;
10171 case 64:
10172 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10173 LPFC_MQ_CNT_64);
10174 break;
10175 case 128:
10176 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10177 LPFC_MQ_CNT_128);
10178 break;
10179 }
10180 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010181 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010182 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10183 putPaddrLow(dmabuf->phys);
10184 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010185 putPaddrHigh(dmabuf->phys);
10186 }
10187 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010188 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10189 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10190 &mq_create_ext->u.response);
10191 if (rc != MBX_SUCCESS) {
10192 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10193 "2795 MQ_CREATE_EXT failed with "
10194 "status x%x. Failback to MQ_CREATE.\n",
10195 rc);
10196 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10197 mq_create = &mbox->u.mqe.un.mq_create;
10198 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10199 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10200 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10201 &mq_create->u.response);
10202 }
10203
James Smart04c68492009-05-22 14:52:52 -040010204 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010205 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10206 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10207 if (shdr_status || shdr_add_status || rc) {
10208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10209 "2502 MQ_CREATE mailbox failed with "
10210 "status x%x add_status x%x, mbx status x%x\n",
10211 shdr_status, shdr_add_status, rc);
10212 status = -ENXIO;
10213 goto out;
10214 }
James Smart04c68492009-05-22 14:52:52 -040010215 if (mq->queue_id == 0xFFFF) {
10216 status = -ENXIO;
10217 goto out;
10218 }
10219 mq->type = LPFC_MQ;
10220 mq->subtype = subtype;
10221 mq->host_index = 0;
10222 mq->hba_index = 0;
10223
10224 /* link the mq onto the parent cq child list */
10225 list_add_tail(&mq->list, &cq->child_list);
10226out:
James Smart8fa38512009-07-19 10:01:03 -040010227 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010228 return status;
10229}
10230
10231/**
James Smart4f774512009-05-22 14:52:35 -040010232 * lpfc_wq_create - Create a Work Queue on the HBA
10233 * @phba: HBA structure that indicates port to create a queue on.
10234 * @wq: The queue structure to use to create the work queue.
10235 * @cq: The completion queue to bind this work queue to.
10236 * @subtype: The subtype of the work queue indicating its functionality.
10237 *
10238 * This function creates a work queue, as detailed in @wq, on a port, described
10239 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10240 *
10241 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10242 * is used to get the entry count and entry size that are necessary to
10243 * determine the number of pages to allocate and use for this queue. The @cq
10244 * is used to indicate which completion queue to bind this work queue to. This
10245 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10246 * work queue. This function is asynchronous and will wait for the mailbox
10247 * command to finish before continuing.
10248 *
10249 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010250 * memory this function will return -ENOMEM. If the queue create mailbox command
10251 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010252 **/
10253uint32_t
10254lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10255 struct lpfc_queue *cq, uint32_t subtype)
10256{
10257 struct lpfc_mbx_wq_create *wq_create;
10258 struct lpfc_dmabuf *dmabuf;
10259 LPFC_MBOXQ_t *mbox;
10260 int rc, length, status = 0;
10261 uint32_t shdr_status, shdr_add_status;
10262 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010263 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10264
10265 if (!phba->sli4_hba.pc_sli4_params.supported)
10266 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010267
10268 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10269 if (!mbox)
10270 return -ENOMEM;
10271 length = (sizeof(struct lpfc_mbx_wq_create) -
10272 sizeof(struct lpfc_sli4_cfg_mhdr));
10273 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10274 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10275 length, LPFC_SLI4_MBX_EMBED);
10276 wq_create = &mbox->u.mqe.un.wq_create;
10277 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10278 wq->page_count);
10279 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10280 cq->queue_id);
10281 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010282 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010283 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10284 putPaddrLow(dmabuf->phys);
10285 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10286 putPaddrHigh(dmabuf->phys);
10287 }
10288 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10289 /* The IOCTL status is embedded in the mailbox subheader. */
10290 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10291 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10292 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10293 if (shdr_status || shdr_add_status || rc) {
10294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10295 "2503 WQ_CREATE mailbox failed with "
10296 "status x%x add_status x%x, mbx status x%x\n",
10297 shdr_status, shdr_add_status, rc);
10298 status = -ENXIO;
10299 goto out;
10300 }
10301 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10302 if (wq->queue_id == 0xFFFF) {
10303 status = -ENXIO;
10304 goto out;
10305 }
10306 wq->type = LPFC_WQ;
10307 wq->subtype = subtype;
10308 wq->host_index = 0;
10309 wq->hba_index = 0;
10310
10311 /* link the wq onto the parent cq child list */
10312 list_add_tail(&wq->list, &cq->child_list);
10313out:
James Smart8fa38512009-07-19 10:01:03 -040010314 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010315 return status;
10316}
10317
10318/**
10319 * lpfc_rq_create - Create a Receive Queue on the HBA
10320 * @phba: HBA structure that indicates port to create a queue on.
10321 * @hrq: The queue structure to use to create the header receive queue.
10322 * @drq: The queue structure to use to create the data receive queue.
10323 * @cq: The completion queue to bind this work queue to.
10324 *
10325 * This function creates a receive buffer queue pair , as detailed in @hrq and
10326 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10327 * to the HBA.
10328 *
10329 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10330 * struct is used to get the entry count that is necessary to determine the
10331 * number of pages to use for this queue. The @cq is used to indicate which
10332 * completion queue to bind received buffers that are posted to these queues to.
10333 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10334 * receive queue pair. This function is asynchronous and will wait for the
10335 * mailbox command to finish before continuing.
10336 *
10337 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010338 * memory this function will return -ENOMEM. If the queue create mailbox command
10339 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010340 **/
10341uint32_t
10342lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10343 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10344{
10345 struct lpfc_mbx_rq_create *rq_create;
10346 struct lpfc_dmabuf *dmabuf;
10347 LPFC_MBOXQ_t *mbox;
10348 int rc, length, status = 0;
10349 uint32_t shdr_status, shdr_add_status;
10350 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010351 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10352
10353 if (!phba->sli4_hba.pc_sli4_params.supported)
10354 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010355
10356 if (hrq->entry_count != drq->entry_count)
10357 return -EINVAL;
10358 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10359 if (!mbox)
10360 return -ENOMEM;
10361 length = (sizeof(struct lpfc_mbx_rq_create) -
10362 sizeof(struct lpfc_sli4_cfg_mhdr));
10363 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10364 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10365 length, LPFC_SLI4_MBX_EMBED);
10366 rq_create = &mbox->u.mqe.un.rq_create;
10367 switch (hrq->entry_count) {
10368 default:
10369 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10370 "2535 Unsupported RQ count. (%d)\n",
10371 hrq->entry_count);
10372 if (hrq->entry_count < 512)
10373 return -EINVAL;
10374 /* otherwise default to smallest count (drop through) */
10375 case 512:
10376 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10377 LPFC_RQ_RING_SIZE_512);
10378 break;
10379 case 1024:
10380 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10381 LPFC_RQ_RING_SIZE_1024);
10382 break;
10383 case 2048:
10384 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10385 LPFC_RQ_RING_SIZE_2048);
10386 break;
10387 case 4096:
10388 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10389 LPFC_RQ_RING_SIZE_4096);
10390 break;
10391 }
10392 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10393 cq->queue_id);
10394 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10395 hrq->page_count);
10396 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10397 LPFC_HDR_BUF_SIZE);
10398 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010399 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010400 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10401 putPaddrLow(dmabuf->phys);
10402 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10403 putPaddrHigh(dmabuf->phys);
10404 }
10405 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10406 /* The IOCTL status is embedded in the mailbox subheader. */
10407 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10408 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10409 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10410 if (shdr_status || shdr_add_status || rc) {
10411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10412 "2504 RQ_CREATE mailbox failed with "
10413 "status x%x add_status x%x, mbx status x%x\n",
10414 shdr_status, shdr_add_status, rc);
10415 status = -ENXIO;
10416 goto out;
10417 }
10418 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10419 if (hrq->queue_id == 0xFFFF) {
10420 status = -ENXIO;
10421 goto out;
10422 }
10423 hrq->type = LPFC_HRQ;
10424 hrq->subtype = subtype;
10425 hrq->host_index = 0;
10426 hrq->hba_index = 0;
10427
10428 /* now create the data queue */
10429 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10430 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10431 length, LPFC_SLI4_MBX_EMBED);
10432 switch (drq->entry_count) {
10433 default:
10434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10435 "2536 Unsupported RQ count. (%d)\n",
10436 drq->entry_count);
10437 if (drq->entry_count < 512)
10438 return -EINVAL;
10439 /* otherwise default to smallest count (drop through) */
10440 case 512:
10441 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10442 LPFC_RQ_RING_SIZE_512);
10443 break;
10444 case 1024:
10445 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10446 LPFC_RQ_RING_SIZE_1024);
10447 break;
10448 case 2048:
10449 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10450 LPFC_RQ_RING_SIZE_2048);
10451 break;
10452 case 4096:
10453 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10454 LPFC_RQ_RING_SIZE_4096);
10455 break;
10456 }
10457 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10458 cq->queue_id);
10459 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10460 drq->page_count);
10461 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10462 LPFC_DATA_BUF_SIZE);
10463 list_for_each_entry(dmabuf, &drq->page_list, list) {
10464 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10465 putPaddrLow(dmabuf->phys);
10466 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10467 putPaddrHigh(dmabuf->phys);
10468 }
10469 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10470 /* The IOCTL status is embedded in the mailbox subheader. */
10471 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10472 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10473 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10474 if (shdr_status || shdr_add_status || rc) {
10475 status = -ENXIO;
10476 goto out;
10477 }
10478 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10479 if (drq->queue_id == 0xFFFF) {
10480 status = -ENXIO;
10481 goto out;
10482 }
10483 drq->type = LPFC_DRQ;
10484 drq->subtype = subtype;
10485 drq->host_index = 0;
10486 drq->hba_index = 0;
10487
10488 /* link the header and data RQs onto the parent cq child list */
10489 list_add_tail(&hrq->list, &cq->child_list);
10490 list_add_tail(&drq->list, &cq->child_list);
10491
10492out:
James Smart8fa38512009-07-19 10:01:03 -040010493 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010494 return status;
10495}
10496
10497/**
10498 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10499 * @eq: The queue structure associated with the queue to destroy.
10500 *
10501 * This function destroys a queue, as detailed in @eq by sending an mailbox
10502 * command, specific to the type of queue, to the HBA.
10503 *
10504 * The @eq struct is used to get the queue ID of the queue to destroy.
10505 *
10506 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010507 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010508 **/
10509uint32_t
10510lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10511{
10512 LPFC_MBOXQ_t *mbox;
10513 int rc, length, status = 0;
10514 uint32_t shdr_status, shdr_add_status;
10515 union lpfc_sli4_cfg_shdr *shdr;
10516
10517 if (!eq)
10518 return -ENODEV;
10519 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10520 if (!mbox)
10521 return -ENOMEM;
10522 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10523 sizeof(struct lpfc_sli4_cfg_mhdr));
10524 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10525 LPFC_MBOX_OPCODE_EQ_DESTROY,
10526 length, LPFC_SLI4_MBX_EMBED);
10527 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10528 eq->queue_id);
10529 mbox->vport = eq->phba->pport;
10530 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10531
10532 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10533 /* The IOCTL status is embedded in the mailbox subheader. */
10534 shdr = (union lpfc_sli4_cfg_shdr *)
10535 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10536 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10537 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10538 if (shdr_status || shdr_add_status || rc) {
10539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10540 "2505 EQ_DESTROY mailbox failed with "
10541 "status x%x add_status x%x, mbx status x%x\n",
10542 shdr_status, shdr_add_status, rc);
10543 status = -ENXIO;
10544 }
10545
10546 /* Remove eq from any list */
10547 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010548 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010549 return status;
10550}
10551
10552/**
10553 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10554 * @cq: The queue structure associated with the queue to destroy.
10555 *
10556 * This function destroys a queue, as detailed in @cq by sending an mailbox
10557 * command, specific to the type of queue, to the HBA.
10558 *
10559 * The @cq struct is used to get the queue ID of the queue to destroy.
10560 *
10561 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010562 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010563 **/
10564uint32_t
10565lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10566{
10567 LPFC_MBOXQ_t *mbox;
10568 int rc, length, status = 0;
10569 uint32_t shdr_status, shdr_add_status;
10570 union lpfc_sli4_cfg_shdr *shdr;
10571
10572 if (!cq)
10573 return -ENODEV;
10574 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10575 if (!mbox)
10576 return -ENOMEM;
10577 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10578 sizeof(struct lpfc_sli4_cfg_mhdr));
10579 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10580 LPFC_MBOX_OPCODE_CQ_DESTROY,
10581 length, LPFC_SLI4_MBX_EMBED);
10582 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10583 cq->queue_id);
10584 mbox->vport = cq->phba->pport;
10585 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10586 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10587 /* The IOCTL status is embedded in the mailbox subheader. */
10588 shdr = (union lpfc_sli4_cfg_shdr *)
10589 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10590 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10591 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10592 if (shdr_status || shdr_add_status || rc) {
10593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10594 "2506 CQ_DESTROY mailbox failed with "
10595 "status x%x add_status x%x, mbx status x%x\n",
10596 shdr_status, shdr_add_status, rc);
10597 status = -ENXIO;
10598 }
10599 /* Remove cq from any list */
10600 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010601 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010602 return status;
10603}
10604
10605/**
James Smart04c68492009-05-22 14:52:52 -040010606 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10607 * @qm: The queue structure associated with the queue to destroy.
10608 *
10609 * This function destroys a queue, as detailed in @mq by sending an mailbox
10610 * command, specific to the type of queue, to the HBA.
10611 *
10612 * The @mq struct is used to get the queue ID of the queue to destroy.
10613 *
10614 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010615 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040010616 **/
10617uint32_t
10618lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10619{
10620 LPFC_MBOXQ_t *mbox;
10621 int rc, length, status = 0;
10622 uint32_t shdr_status, shdr_add_status;
10623 union lpfc_sli4_cfg_shdr *shdr;
10624
10625 if (!mq)
10626 return -ENODEV;
10627 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10628 if (!mbox)
10629 return -ENOMEM;
10630 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10631 sizeof(struct lpfc_sli4_cfg_mhdr));
10632 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10633 LPFC_MBOX_OPCODE_MQ_DESTROY,
10634 length, LPFC_SLI4_MBX_EMBED);
10635 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10636 mq->queue_id);
10637 mbox->vport = mq->phba->pport;
10638 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10639 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10640 /* The IOCTL status is embedded in the mailbox subheader. */
10641 shdr = (union lpfc_sli4_cfg_shdr *)
10642 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10643 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10644 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10645 if (shdr_status || shdr_add_status || rc) {
10646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10647 "2507 MQ_DESTROY mailbox failed with "
10648 "status x%x add_status x%x, mbx status x%x\n",
10649 shdr_status, shdr_add_status, rc);
10650 status = -ENXIO;
10651 }
10652 /* Remove mq from any list */
10653 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010654 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010655 return status;
10656}
10657
10658/**
James Smart4f774512009-05-22 14:52:35 -040010659 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10660 * @wq: The queue structure associated with the queue to destroy.
10661 *
10662 * This function destroys a queue, as detailed in @wq by sending an mailbox
10663 * command, specific to the type of queue, to the HBA.
10664 *
10665 * The @wq struct is used to get the queue ID of the queue to destroy.
10666 *
10667 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010668 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010669 **/
10670uint32_t
10671lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10672{
10673 LPFC_MBOXQ_t *mbox;
10674 int rc, length, status = 0;
10675 uint32_t shdr_status, shdr_add_status;
10676 union lpfc_sli4_cfg_shdr *shdr;
10677
10678 if (!wq)
10679 return -ENODEV;
10680 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10681 if (!mbox)
10682 return -ENOMEM;
10683 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10684 sizeof(struct lpfc_sli4_cfg_mhdr));
10685 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10686 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10687 length, LPFC_SLI4_MBX_EMBED);
10688 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10689 wq->queue_id);
10690 mbox->vport = wq->phba->pport;
10691 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10692 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10693 shdr = (union lpfc_sli4_cfg_shdr *)
10694 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10695 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10696 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10697 if (shdr_status || shdr_add_status || rc) {
10698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10699 "2508 WQ_DESTROY mailbox failed with "
10700 "status x%x add_status x%x, mbx status x%x\n",
10701 shdr_status, shdr_add_status, rc);
10702 status = -ENXIO;
10703 }
10704 /* Remove wq from any list */
10705 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010706 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010707 return status;
10708}
10709
10710/**
10711 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10712 * @rq: The queue structure associated with the queue to destroy.
10713 *
10714 * This function destroys a queue, as detailed in @rq by sending an mailbox
10715 * command, specific to the type of queue, to the HBA.
10716 *
10717 * The @rq struct is used to get the queue ID of the queue to destroy.
10718 *
10719 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010720 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010721 **/
10722uint32_t
10723lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10724 struct lpfc_queue *drq)
10725{
10726 LPFC_MBOXQ_t *mbox;
10727 int rc, length, status = 0;
10728 uint32_t shdr_status, shdr_add_status;
10729 union lpfc_sli4_cfg_shdr *shdr;
10730
10731 if (!hrq || !drq)
10732 return -ENODEV;
10733 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10734 if (!mbox)
10735 return -ENOMEM;
10736 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10737 sizeof(struct mbox_header));
10738 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10739 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10740 length, LPFC_SLI4_MBX_EMBED);
10741 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10742 hrq->queue_id);
10743 mbox->vport = hrq->phba->pport;
10744 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10745 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10746 /* The IOCTL status is embedded in the mailbox subheader. */
10747 shdr = (union lpfc_sli4_cfg_shdr *)
10748 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10749 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10750 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10751 if (shdr_status || shdr_add_status || rc) {
10752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10753 "2509 RQ_DESTROY mailbox failed with "
10754 "status x%x add_status x%x, mbx status x%x\n",
10755 shdr_status, shdr_add_status, rc);
10756 if (rc != MBX_TIMEOUT)
10757 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10758 return -ENXIO;
10759 }
10760 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10761 drq->queue_id);
10762 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10763 shdr = (union lpfc_sli4_cfg_shdr *)
10764 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10765 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10766 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10767 if (shdr_status || shdr_add_status || rc) {
10768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10769 "2510 RQ_DESTROY mailbox failed with "
10770 "status x%x add_status x%x, mbx status x%x\n",
10771 shdr_status, shdr_add_status, rc);
10772 status = -ENXIO;
10773 }
10774 list_del_init(&hrq->list);
10775 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010776 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010777 return status;
10778}
10779
10780/**
10781 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10782 * @phba: The virtual port for which this call being executed.
10783 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10784 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10785 * @xritag: the xritag that ties this io to the SGL pages.
10786 *
10787 * This routine will post the sgl pages for the IO that has the xritag
10788 * that is in the iocbq structure. The xritag is assigned during iocbq
10789 * creation and persists for as long as the driver is loaded.
10790 * if the caller has fewer than 256 scatter gather segments to map then
10791 * pdma_phys_addr1 should be 0.
10792 * If the caller needs to map more than 256 scatter gather segment then
10793 * pdma_phys_addr1 should be a valid physical address.
10794 * physical address for SGLs must be 64 byte aligned.
10795 * If you are going to map 2 SGL's then the first one must have 256 entries
10796 * the second sgl can have between 1 and 256 entries.
10797 *
10798 * Return codes:
10799 * 0 - Success
10800 * -ENXIO, -ENOMEM - Failure
10801 **/
10802int
10803lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10804 dma_addr_t pdma_phys_addr0,
10805 dma_addr_t pdma_phys_addr1,
10806 uint16_t xritag)
10807{
10808 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10809 LPFC_MBOXQ_t *mbox;
10810 int rc;
10811 uint32_t shdr_status, shdr_add_status;
10812 union lpfc_sli4_cfg_shdr *shdr;
10813
10814 if (xritag == NO_XRI) {
10815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10816 "0364 Invalid param:\n");
10817 return -EINVAL;
10818 }
10819
10820 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10821 if (!mbox)
10822 return -ENOMEM;
10823
10824 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10825 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10826 sizeof(struct lpfc_mbx_post_sgl_pages) -
10827 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10828
10829 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10830 &mbox->u.mqe.un.post_sgl_pages;
10831 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10832 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10833
10834 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10835 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10836 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10837 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10838
10839 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10840 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10841 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10842 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10843 if (!phba->sli4_hba.intr_enable)
10844 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10845 else
10846 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10847 /* The IOCTL status is embedded in the mailbox subheader. */
10848 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10849 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10850 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10851 if (rc != MBX_TIMEOUT)
10852 mempool_free(mbox, phba->mbox_mem_pool);
10853 if (shdr_status || shdr_add_status || rc) {
10854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10855 "2511 POST_SGL mailbox failed with "
10856 "status x%x add_status x%x, mbx status x%x\n",
10857 shdr_status, shdr_add_status, rc);
10858 rc = -ENXIO;
10859 }
10860 return 0;
10861}
James Smart4f774512009-05-22 14:52:35 -040010862
10863/**
10864 * lpfc_sli4_next_xritag - Get an xritag for the io
10865 * @phba: Pointer to HBA context object.
10866 *
10867 * This function gets an xritag for the iocb. If there is no unused xritag
10868 * it will return 0xffff.
10869 * The function returns the allocated xritag if successful, else returns zero.
10870 * Zero is not a valid xritag.
10871 * The caller is not required to hold any lock.
10872 **/
10873uint16_t
10874lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10875{
10876 uint16_t xritag;
10877
10878 spin_lock_irq(&phba->hbalock);
10879 xritag = phba->sli4_hba.next_xri;
10880 if ((xritag != (uint16_t) -1) && xritag <
10881 (phba->sli4_hba.max_cfg_param.max_xri
10882 + phba->sli4_hba.max_cfg_param.xri_base)) {
10883 phba->sli4_hba.next_xri++;
10884 phba->sli4_hba.max_cfg_param.xri_used++;
10885 spin_unlock_irq(&phba->hbalock);
10886 return xritag;
10887 }
10888 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010889 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010890 "2004 Failed to allocate XRI.last XRITAG is %d"
10891 " Max XRI is %d, Used XRI is %d\n",
10892 phba->sli4_hba.next_xri,
10893 phba->sli4_hba.max_cfg_param.max_xri,
10894 phba->sli4_hba.max_cfg_param.xri_used);
10895 return -1;
10896}
10897
10898/**
10899 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10900 * @phba: pointer to lpfc hba data structure.
10901 *
10902 * This routine is invoked to post a block of driver's sgl pages to the
10903 * HBA using non-embedded mailbox command. No Lock is held. This routine
10904 * is only called when the driver is loading and after all IO has been
10905 * stopped.
10906 **/
10907int
10908lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10909{
10910 struct lpfc_sglq *sglq_entry;
10911 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10912 struct sgl_page_pairs *sgl_pg_pairs;
10913 void *viraddr;
10914 LPFC_MBOXQ_t *mbox;
10915 uint32_t reqlen, alloclen, pg_pairs;
10916 uint32_t mbox_tmo;
10917 uint16_t xritag_start = 0;
10918 int els_xri_cnt, rc = 0;
10919 uint32_t shdr_status, shdr_add_status;
10920 union lpfc_sli4_cfg_shdr *shdr;
10921
10922 /* The number of sgls to be posted */
10923 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10924
10925 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10926 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010927 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010928 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10929 "2559 Block sgl registration required DMA "
10930 "size (%d) great than a page\n", reqlen);
10931 return -ENOMEM;
10932 }
10933 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10934 if (!mbox) {
10935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10936 "2560 Failed to allocate mbox cmd memory\n");
10937 return -ENOMEM;
10938 }
10939
10940 /* Allocate DMA memory and set up the non-embedded mailbox command */
10941 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10942 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10943 LPFC_SLI4_MBX_NEMBED);
10944
10945 if (alloclen < reqlen) {
10946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10947 "0285 Allocated DMA memory size (%d) is "
10948 "less than the requested DMA memory "
10949 "size (%d)\n", alloclen, reqlen);
10950 lpfc_sli4_mbox_cmd_free(phba, mbox);
10951 return -ENOMEM;
10952 }
James Smart4f774512009-05-22 14:52:35 -040010953 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010954 viraddr = mbox->sge_array->addr[0];
10955
10956 /* Set up the SGL pages in the non-embedded DMA pages */
10957 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10958 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10959
10960 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10961 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10962 /* Set up the sge entry */
10963 sgl_pg_pairs->sgl_pg0_addr_lo =
10964 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10965 sgl_pg_pairs->sgl_pg0_addr_hi =
10966 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10967 sgl_pg_pairs->sgl_pg1_addr_lo =
10968 cpu_to_le32(putPaddrLow(0));
10969 sgl_pg_pairs->sgl_pg1_addr_hi =
10970 cpu_to_le32(putPaddrHigh(0));
10971 /* Keep the first xritag on the list */
10972 if (pg_pairs == 0)
10973 xritag_start = sglq_entry->sli4_xritag;
10974 sgl_pg_pairs++;
10975 }
10976 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010977 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010978 /* Perform endian conversion if necessary */
10979 sgl->word0 = cpu_to_le32(sgl->word0);
10980
10981 if (!phba->sli4_hba.intr_enable)
10982 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10983 else {
10984 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10985 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10986 }
10987 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10988 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10989 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10990 if (rc != MBX_TIMEOUT)
10991 lpfc_sli4_mbox_cmd_free(phba, mbox);
10992 if (shdr_status || shdr_add_status || rc) {
10993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10994 "2513 POST_SGL_BLOCK mailbox command failed "
10995 "status x%x add_status x%x mbx status x%x\n",
10996 shdr_status, shdr_add_status, rc);
10997 rc = -ENXIO;
10998 }
10999 return rc;
11000}
11001
11002/**
11003 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11004 * @phba: pointer to lpfc hba data structure.
11005 * @sblist: pointer to scsi buffer list.
11006 * @count: number of scsi buffers on the list.
11007 *
11008 * This routine is invoked to post a block of @count scsi sgl pages from a
11009 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11010 * No Lock is held.
11011 *
11012 **/
11013int
11014lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11015 int cnt)
11016{
11017 struct lpfc_scsi_buf *psb;
11018 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11019 struct sgl_page_pairs *sgl_pg_pairs;
11020 void *viraddr;
11021 LPFC_MBOXQ_t *mbox;
11022 uint32_t reqlen, alloclen, pg_pairs;
11023 uint32_t mbox_tmo;
11024 uint16_t xritag_start = 0;
11025 int rc = 0;
11026 uint32_t shdr_status, shdr_add_status;
11027 dma_addr_t pdma_phys_bpl1;
11028 union lpfc_sli4_cfg_shdr *shdr;
11029
11030 /* Calculate the requested length of the dma memory */
11031 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11032 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011033 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011034 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11035 "0217 Block sgl registration required DMA "
11036 "size (%d) great than a page\n", reqlen);
11037 return -ENOMEM;
11038 }
11039 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11040 if (!mbox) {
11041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11042 "0283 Failed to allocate mbox cmd memory\n");
11043 return -ENOMEM;
11044 }
11045
11046 /* Allocate DMA memory and set up the non-embedded mailbox command */
11047 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11048 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11049 LPFC_SLI4_MBX_NEMBED);
11050
11051 if (alloclen < reqlen) {
11052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11053 "2561 Allocated DMA memory size (%d) is "
11054 "less than the requested DMA memory "
11055 "size (%d)\n", alloclen, reqlen);
11056 lpfc_sli4_mbox_cmd_free(phba, mbox);
11057 return -ENOMEM;
11058 }
James Smart4f774512009-05-22 14:52:35 -040011059 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011060 viraddr = mbox->sge_array->addr[0];
11061
11062 /* Set up the SGL pages in the non-embedded DMA pages */
11063 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11064 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11065
11066 pg_pairs = 0;
11067 list_for_each_entry(psb, sblist, list) {
11068 /* Set up the sge entry */
11069 sgl_pg_pairs->sgl_pg0_addr_lo =
11070 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11071 sgl_pg_pairs->sgl_pg0_addr_hi =
11072 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11073 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11074 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11075 else
11076 pdma_phys_bpl1 = 0;
11077 sgl_pg_pairs->sgl_pg1_addr_lo =
11078 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11079 sgl_pg_pairs->sgl_pg1_addr_hi =
11080 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11081 /* Keep the first xritag on the list */
11082 if (pg_pairs == 0)
11083 xritag_start = psb->cur_iocbq.sli4_xritag;
11084 sgl_pg_pairs++;
11085 pg_pairs++;
11086 }
11087 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11088 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11089 /* Perform endian conversion if necessary */
11090 sgl->word0 = cpu_to_le32(sgl->word0);
11091
11092 if (!phba->sli4_hba.intr_enable)
11093 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11094 else {
11095 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11096 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11097 }
11098 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11099 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11100 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11101 if (rc != MBX_TIMEOUT)
11102 lpfc_sli4_mbox_cmd_free(phba, mbox);
11103 if (shdr_status || shdr_add_status || rc) {
11104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11105 "2564 POST_SGL_BLOCK mailbox command failed "
11106 "status x%x add_status x%x mbx status x%x\n",
11107 shdr_status, shdr_add_status, rc);
11108 rc = -ENXIO;
11109 }
11110 return rc;
11111}
11112
11113/**
11114 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11115 * @phba: pointer to lpfc_hba struct that the frame was received on
11116 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11117 *
11118 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11119 * valid type of frame that the LPFC driver will handle. This function will
11120 * return a zero if the frame is a valid frame or a non zero value when the
11121 * frame does not pass the check.
11122 **/
11123static int
11124lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11125{
11126 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11127 char *type_names[] = FC_TYPE_NAMES_INIT;
11128 struct fc_vft_header *fc_vft_hdr;
11129
11130 switch (fc_hdr->fh_r_ctl) {
11131 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11132 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11133 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11134 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11135 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11136 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11137 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11138 case FC_RCTL_DD_CMD_STATUS: /* command status */
11139 case FC_RCTL_ELS_REQ: /* extended link services request */
11140 case FC_RCTL_ELS_REP: /* extended link services reply */
11141 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11142 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11143 case FC_RCTL_BA_NOP: /* basic link service NOP */
11144 case FC_RCTL_BA_ABTS: /* basic link service abort */
11145 case FC_RCTL_BA_RMC: /* remove connection */
11146 case FC_RCTL_BA_ACC: /* basic accept */
11147 case FC_RCTL_BA_RJT: /* basic reject */
11148 case FC_RCTL_BA_PRMT:
11149 case FC_RCTL_ACK_1: /* acknowledge_1 */
11150 case FC_RCTL_ACK_0: /* acknowledge_0 */
11151 case FC_RCTL_P_RJT: /* port reject */
11152 case FC_RCTL_F_RJT: /* fabric reject */
11153 case FC_RCTL_P_BSY: /* port busy */
11154 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11155 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11156 case FC_RCTL_LCR: /* link credit reset */
11157 case FC_RCTL_END: /* end */
11158 break;
11159 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11160 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11161 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11162 return lpfc_fc_frame_check(phba, fc_hdr);
11163 default:
11164 goto drop;
11165 }
11166 switch (fc_hdr->fh_type) {
11167 case FC_TYPE_BLS:
11168 case FC_TYPE_ELS:
11169 case FC_TYPE_FCP:
11170 case FC_TYPE_CT:
11171 break;
11172 case FC_TYPE_IP:
11173 case FC_TYPE_ILS:
11174 default:
11175 goto drop;
11176 }
11177 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11178 "2538 Received frame rctl:%s type:%s\n",
11179 rctl_names[fc_hdr->fh_r_ctl],
11180 type_names[fc_hdr->fh_type]);
11181 return 0;
11182drop:
11183 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11184 "2539 Dropped frame rctl:%s type:%s\n",
11185 rctl_names[fc_hdr->fh_r_ctl],
11186 type_names[fc_hdr->fh_type]);
11187 return 1;
11188}
11189
11190/**
11191 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11192 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11193 *
11194 * This function processes the FC header to retrieve the VFI from the VF
11195 * header, if one exists. This function will return the VFI if one exists
11196 * or 0 if no VSAN Header exists.
11197 **/
11198static uint32_t
11199lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11200{
11201 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11202
11203 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11204 return 0;
11205 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11206}
11207
11208/**
11209 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11210 * @phba: Pointer to the HBA structure to search for the vport on
11211 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11212 * @fcfi: The FC Fabric ID that the frame came from
11213 *
11214 * This function searches the @phba for a vport that matches the content of the
11215 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11216 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11217 * returns the matching vport pointer or NULL if unable to match frame to a
11218 * vport.
11219 **/
11220static struct lpfc_vport *
11221lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11222 uint16_t fcfi)
11223{
11224 struct lpfc_vport **vports;
11225 struct lpfc_vport *vport = NULL;
11226 int i;
11227 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11228 fc_hdr->fh_d_id[1] << 8 |
11229 fc_hdr->fh_d_id[2]);
11230
11231 vports = lpfc_create_vport_work_array(phba);
11232 if (vports != NULL)
11233 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11234 if (phba->fcf.fcfi == fcfi &&
11235 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11236 vports[i]->fc_myDID == did) {
11237 vport = vports[i];
11238 break;
11239 }
11240 }
11241 lpfc_destroy_vport_work_array(phba, vports);
11242 return vport;
11243}
11244
11245/**
James Smart45ed1192009-10-02 15:17:02 -040011246 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11247 * @vport: The vport to work on.
11248 *
11249 * This function updates the receive sequence time stamp for this vport. The
11250 * receive sequence time stamp indicates the time that the last frame of the
11251 * the sequence that has been idle for the longest amount of time was received.
11252 * the driver uses this time stamp to indicate if any received sequences have
11253 * timed out.
11254 **/
11255void
11256lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11257{
11258 struct lpfc_dmabuf *h_buf;
11259 struct hbq_dmabuf *dmabuf = NULL;
11260
11261 /* get the oldest sequence on the rcv list */
11262 h_buf = list_get_first(&vport->rcv_buffer_list,
11263 struct lpfc_dmabuf, list);
11264 if (!h_buf)
11265 return;
11266 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11267 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11268}
11269
11270/**
11271 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11272 * @vport: The vport that the received sequences were sent to.
11273 *
11274 * This function cleans up all outstanding received sequences. This is called
11275 * by the driver when a link event or user action invalidates all the received
11276 * sequences.
11277 **/
11278void
11279lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11280{
11281 struct lpfc_dmabuf *h_buf, *hnext;
11282 struct lpfc_dmabuf *d_buf, *dnext;
11283 struct hbq_dmabuf *dmabuf = NULL;
11284
11285 /* start with the oldest sequence on the rcv list */
11286 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11287 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11288 list_del_init(&dmabuf->hbuf.list);
11289 list_for_each_entry_safe(d_buf, dnext,
11290 &dmabuf->dbuf.list, list) {
11291 list_del_init(&d_buf->list);
11292 lpfc_in_buf_free(vport->phba, d_buf);
11293 }
11294 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11295 }
11296}
11297
11298/**
11299 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11300 * @vport: The vport that the received sequences were sent to.
11301 *
11302 * This function determines whether any received sequences have timed out by
11303 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11304 * indicates that there is at least one timed out sequence this routine will
11305 * go through the received sequences one at a time from most inactive to most
11306 * active to determine which ones need to be cleaned up. Once it has determined
11307 * that a sequence needs to be cleaned up it will simply free up the resources
11308 * without sending an abort.
11309 **/
11310void
11311lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11312{
11313 struct lpfc_dmabuf *h_buf, *hnext;
11314 struct lpfc_dmabuf *d_buf, *dnext;
11315 struct hbq_dmabuf *dmabuf = NULL;
11316 unsigned long timeout;
11317 int abort_count = 0;
11318
11319 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11320 vport->rcv_buffer_time_stamp);
11321 if (list_empty(&vport->rcv_buffer_list) ||
11322 time_before(jiffies, timeout))
11323 return;
11324 /* start with the oldest sequence on the rcv list */
11325 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11326 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11327 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11328 dmabuf->time_stamp);
11329 if (time_before(jiffies, timeout))
11330 break;
11331 abort_count++;
11332 list_del_init(&dmabuf->hbuf.list);
11333 list_for_each_entry_safe(d_buf, dnext,
11334 &dmabuf->dbuf.list, list) {
11335 list_del_init(&d_buf->list);
11336 lpfc_in_buf_free(vport->phba, d_buf);
11337 }
11338 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11339 }
11340 if (abort_count)
11341 lpfc_update_rcv_time_stamp(vport);
11342}
11343
11344/**
James Smart4f774512009-05-22 14:52:35 -040011345 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11346 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11347 *
11348 * This function searches through the existing incomplete sequences that have
11349 * been sent to this @vport. If the frame matches one of the incomplete
11350 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11351 * make up that sequence. If no sequence is found that matches this frame then
11352 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11353 * This function returns a pointer to the first dmabuf in the sequence list that
11354 * the frame was linked to.
11355 **/
11356static struct hbq_dmabuf *
11357lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11358{
11359 struct fc_frame_header *new_hdr;
11360 struct fc_frame_header *temp_hdr;
11361 struct lpfc_dmabuf *d_buf;
11362 struct lpfc_dmabuf *h_buf;
11363 struct hbq_dmabuf *seq_dmabuf = NULL;
11364 struct hbq_dmabuf *temp_dmabuf = NULL;
11365
James Smart4d9ab992009-10-02 15:16:39 -040011366 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011367 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011368 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11369 /* Use the hdr_buf to find the sequence that this frame belongs to */
11370 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11371 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11372 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11373 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11374 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11375 continue;
11376 /* found a pending sequence that matches this frame */
11377 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11378 break;
11379 }
11380 if (!seq_dmabuf) {
11381 /*
11382 * This indicates first frame received for this sequence.
11383 * Queue the buffer on the vport's rcv_buffer_list.
11384 */
11385 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011386 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011387 return dmabuf;
11388 }
11389 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011390 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11391 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011392 list_del_init(&seq_dmabuf->hbuf.list);
11393 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11394 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011395 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011396 return dmabuf;
11397 }
James Smart45ed1192009-10-02 15:17:02 -040011398 /* move this sequence to the tail to indicate a young sequence */
11399 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11400 seq_dmabuf->time_stamp = jiffies;
11401 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011402 if (list_empty(&seq_dmabuf->dbuf.list)) {
11403 temp_hdr = dmabuf->hbuf.virt;
11404 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11405 return seq_dmabuf;
11406 }
James Smart4f774512009-05-22 14:52:35 -040011407 /* find the correct place in the sequence to insert this frame */
11408 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11409 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11410 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11411 /*
11412 * If the frame's sequence count is greater than the frame on
11413 * the list then insert the frame right after this frame
11414 */
James Smarteeead812009-12-21 17:01:23 -050011415 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11416 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011417 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11418 return seq_dmabuf;
11419 }
11420 }
11421 return NULL;
11422}
11423
11424/**
James Smart6669f9b2009-10-02 15:16:45 -040011425 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11426 * @vport: pointer to a vitural port
11427 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11428 *
11429 * This function tries to abort from the partially assembed sequence, described
11430 * by the information from basic abbort @dmabuf. It checks to see whether such
11431 * partially assembled sequence held by the driver. If so, it shall free up all
11432 * the frames from the partially assembled sequence.
11433 *
11434 * Return
11435 * true -- if there is matching partially assembled sequence present and all
11436 * the frames freed with the sequence;
11437 * false -- if there is no matching partially assembled sequence present so
11438 * nothing got aborted in the lower layer driver
11439 **/
11440static bool
11441lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11442 struct hbq_dmabuf *dmabuf)
11443{
11444 struct fc_frame_header *new_hdr;
11445 struct fc_frame_header *temp_hdr;
11446 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11447 struct hbq_dmabuf *seq_dmabuf = NULL;
11448
11449 /* Use the hdr_buf to find the sequence that matches this frame */
11450 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11451 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11452 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11453 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11454 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11455 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11456 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11457 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11458 continue;
11459 /* found a pending sequence that matches this frame */
11460 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11461 break;
11462 }
11463
11464 /* Free up all the frames from the partially assembled sequence */
11465 if (seq_dmabuf) {
11466 list_for_each_entry_safe(d_buf, n_buf,
11467 &seq_dmabuf->dbuf.list, list) {
11468 list_del_init(&d_buf->list);
11469 lpfc_in_buf_free(vport->phba, d_buf);
11470 }
11471 return true;
11472 }
11473 return false;
11474}
11475
11476/**
11477 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11478 * @phba: Pointer to HBA context object.
11479 * @cmd_iocbq: pointer to the command iocbq structure.
11480 * @rsp_iocbq: pointer to the response iocbq structure.
11481 *
11482 * This function handles the sequence abort accept iocb command complete
11483 * event. It properly releases the memory allocated to the sequence abort
11484 * accept iocb.
11485 **/
11486static void
11487lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11488 struct lpfc_iocbq *cmd_iocbq,
11489 struct lpfc_iocbq *rsp_iocbq)
11490{
11491 if (cmd_iocbq)
11492 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11493}
11494
11495/**
11496 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11497 * @phba: Pointer to HBA context object.
11498 * @fc_hdr: pointer to a FC frame header.
11499 *
11500 * This function sends a basic accept to a previous unsol sequence abort
11501 * event after aborting the sequence handling.
11502 **/
11503static void
11504lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11505 struct fc_frame_header *fc_hdr)
11506{
11507 struct lpfc_iocbq *ctiocb = NULL;
11508 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011509 uint16_t oxid, rxid;
11510 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011511 IOCB_t *icmd;
11512
11513 if (!lpfc_is_link_up(phba))
11514 return;
11515
11516 sid = sli4_sid_from_fc_hdr(fc_hdr);
11517 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011518 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011519
11520 ndlp = lpfc_findnode_did(phba->pport, sid);
11521 if (!ndlp) {
11522 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11523 "1268 Find ndlp returned NULL for oxid:x%x "
11524 "SID:x%x\n", oxid, sid);
11525 return;
11526 }
11527
11528 /* Allocate buffer for acc iocb */
11529 ctiocb = lpfc_sli_get_iocbq(phba);
11530 if (!ctiocb)
11531 return;
11532
James Smart5ffc2662009-11-18 15:39:44 -050011533 /* Extract the F_CTL field from FC_HDR */
11534 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11535
James Smart6669f9b2009-10-02 15:16:45 -040011536 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011537 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011538 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011539 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11540 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11541 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11542
11543 /* Fill in the rest of iocb fields */
11544 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11545 icmd->ulpBdeCount = 0;
11546 icmd->ulpLe = 1;
11547 icmd->ulpClass = CLASS3;
11548 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011549
James Smart6669f9b2009-10-02 15:16:45 -040011550 ctiocb->iocb_cmpl = NULL;
11551 ctiocb->vport = phba->pport;
11552 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11553
James Smart5ffc2662009-11-18 15:39:44 -050011554 if (fctl & FC_FC_EX_CTX) {
11555 /* ABTS sent by responder to CT exchange, construction
11556 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11557 * field and RX_ID from ABTS for RX_ID field.
11558 */
11559 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11560 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11561 ctiocb->sli4_xritag = oxid;
11562 } else {
11563 /* ABTS sent by initiator to CT exchange, construction
11564 * of BA_ACC will need to allocate a new XRI as for the
11565 * XRI_TAG and RX_ID fields.
11566 */
11567 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11568 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11569 ctiocb->sli4_xritag = NO_XRI;
11570 }
11571 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11572
James Smart6669f9b2009-10-02 15:16:45 -040011573 /* Xmit CT abts accept on exchange <xid> */
11574 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11575 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11576 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11577 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11578}
11579
11580/**
11581 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11582 * @vport: Pointer to the vport on which this sequence was received
11583 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11584 *
11585 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11586 * receive sequence is only partially assembed by the driver, it shall abort
11587 * the partially assembled frames for the sequence. Otherwise, if the
11588 * unsolicited receive sequence has been completely assembled and passed to
11589 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11590 * unsolicited sequence has been aborted. After that, it will issue a basic
11591 * accept to accept the abort.
11592 **/
11593void
11594lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11595 struct hbq_dmabuf *dmabuf)
11596{
11597 struct lpfc_hba *phba = vport->phba;
11598 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011599 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011600 bool abts_par;
11601
James Smart6669f9b2009-10-02 15:16:45 -040011602 /* Make a copy of fc_hdr before the dmabuf being released */
11603 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011604 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011605
James Smart5ffc2662009-11-18 15:39:44 -050011606 if (fctl & FC_FC_EX_CTX) {
11607 /*
11608 * ABTS sent by responder to exchange, just free the buffer
11609 */
James Smart6669f9b2009-10-02 15:16:45 -040011610 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011611 } else {
11612 /*
11613 * ABTS sent by initiator to exchange, need to do cleanup
11614 */
11615 /* Try to abort partially assembled seq */
11616 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11617
11618 /* Send abort to ULP if partially seq abort failed */
11619 if (abts_par == false)
11620 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11621 else
11622 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11623 }
James Smart6669f9b2009-10-02 15:16:45 -040011624 /* Send basic accept (BA_ACC) to the abort requester */
11625 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11626}
11627
11628/**
James Smart4f774512009-05-22 14:52:35 -040011629 * lpfc_seq_complete - Indicates if a sequence is complete
11630 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11631 *
11632 * This function checks the sequence, starting with the frame described by
11633 * @dmabuf, to see if all the frames associated with this sequence are present.
11634 * the frames associated with this sequence are linked to the @dmabuf using the
11635 * dbuf list. This function looks for two major things. 1) That the first frame
11636 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11637 * set. 3) That there are no holes in the sequence count. The function will
11638 * return 1 when the sequence is complete, otherwise it will return 0.
11639 **/
11640static int
11641lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11642{
11643 struct fc_frame_header *hdr;
11644 struct lpfc_dmabuf *d_buf;
11645 struct hbq_dmabuf *seq_dmabuf;
11646 uint32_t fctl;
11647 int seq_count = 0;
11648
11649 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11650 /* make sure first fame of sequence has a sequence count of zero */
11651 if (hdr->fh_seq_cnt != seq_count)
11652 return 0;
11653 fctl = (hdr->fh_f_ctl[0] << 16 |
11654 hdr->fh_f_ctl[1] << 8 |
11655 hdr->fh_f_ctl[2]);
11656 /* If last frame of sequence we can return success. */
11657 if (fctl & FC_FC_END_SEQ)
11658 return 1;
11659 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11660 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11661 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11662 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011663 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011664 return 0;
11665 fctl = (hdr->fh_f_ctl[0] << 16 |
11666 hdr->fh_f_ctl[1] << 8 |
11667 hdr->fh_f_ctl[2]);
11668 /* If last frame of sequence we can return success. */
11669 if (fctl & FC_FC_END_SEQ)
11670 return 1;
11671 }
11672 return 0;
11673}
11674
11675/**
11676 * lpfc_prep_seq - Prep sequence for ULP processing
11677 * @vport: Pointer to the vport on which this sequence was received
11678 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11679 *
11680 * This function takes a sequence, described by a list of frames, and creates
11681 * a list of iocbq structures to describe the sequence. This iocbq list will be
11682 * used to issue to the generic unsolicited sequence handler. This routine
11683 * returns a pointer to the first iocbq in the list. If the function is unable
11684 * to allocate an iocbq then it throw out the received frames that were not
11685 * able to be described and return a pointer to the first iocbq. If unable to
11686 * allocate any iocbqs (including the first) this function will return NULL.
11687 **/
11688static struct lpfc_iocbq *
11689lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11690{
11691 struct lpfc_dmabuf *d_buf, *n_buf;
11692 struct lpfc_iocbq *first_iocbq, *iocbq;
11693 struct fc_frame_header *fc_hdr;
11694 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011695 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011696
11697 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11698 /* remove from receive buffer list */
11699 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011700 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011701 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011702 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011703 /* Get an iocbq struct to fill in. */
11704 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11705 if (first_iocbq) {
11706 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011707 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011708 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11709 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11710 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11711 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11712 vport->vpi + vport->phba->vpi_base;
11713 /* put the first buffer into the first IOCBq */
11714 first_iocbq->context2 = &seq_dmabuf->dbuf;
11715 first_iocbq->context3 = NULL;
11716 first_iocbq->iocb.ulpBdeCount = 1;
11717 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11718 LPFC_DATA_BUF_SIZE;
11719 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011720 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011721 bf_get(lpfc_rcqe_length,
11722 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011723 }
11724 iocbq = first_iocbq;
11725 /*
11726 * Each IOCBq can have two Buffers assigned, so go through the list
11727 * of buffers for this sequence and save two buffers in each IOCBq
11728 */
11729 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11730 if (!iocbq) {
11731 lpfc_in_buf_free(vport->phba, d_buf);
11732 continue;
11733 }
11734 if (!iocbq->context3) {
11735 iocbq->context3 = d_buf;
11736 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011737 pbde = (struct ulp_bde64 *)
11738 &iocbq->iocb.unsli3.sli3Words[4];
11739 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011740 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011741 bf_get(lpfc_rcqe_length,
11742 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011743 } else {
11744 iocbq = lpfc_sli_get_iocbq(vport->phba);
11745 if (!iocbq) {
11746 if (first_iocbq) {
11747 first_iocbq->iocb.ulpStatus =
11748 IOSTAT_FCP_RSP_ERROR;
11749 first_iocbq->iocb.un.ulpWord[4] =
11750 IOERR_NO_RESOURCES;
11751 }
11752 lpfc_in_buf_free(vport->phba, d_buf);
11753 continue;
11754 }
11755 iocbq->context2 = d_buf;
11756 iocbq->context3 = NULL;
11757 iocbq->iocb.ulpBdeCount = 1;
11758 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11759 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011760 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011761 bf_get(lpfc_rcqe_length,
11762 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011763 iocbq->iocb.un.rcvels.remoteID = sid;
11764 list_add_tail(&iocbq->list, &first_iocbq->list);
11765 }
11766 }
11767 return first_iocbq;
11768}
11769
James Smart6669f9b2009-10-02 15:16:45 -040011770static void
11771lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11772 struct hbq_dmabuf *seq_dmabuf)
11773{
11774 struct fc_frame_header *fc_hdr;
11775 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11776 struct lpfc_hba *phba = vport->phba;
11777
11778 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11779 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11780 if (!iocbq) {
11781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11782 "2707 Ring %d handler: Failed to allocate "
11783 "iocb Rctl x%x Type x%x received\n",
11784 LPFC_ELS_RING,
11785 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11786 return;
11787 }
11788 if (!lpfc_complete_unsol_iocb(phba,
11789 &phba->sli.ring[LPFC_ELS_RING],
11790 iocbq, fc_hdr->fh_r_ctl,
11791 fc_hdr->fh_type))
11792 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11793 "2540 Ring %d handler: unexpected Rctl "
11794 "x%x Type x%x received\n",
11795 LPFC_ELS_RING,
11796 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11797
11798 /* Free iocb created in lpfc_prep_seq */
11799 list_for_each_entry_safe(curr_iocb, next_iocb,
11800 &iocbq->list, list) {
11801 list_del_init(&curr_iocb->list);
11802 lpfc_sli_release_iocbq(phba, curr_iocb);
11803 }
11804 lpfc_sli_release_iocbq(phba, iocbq);
11805}
11806
James Smart4f774512009-05-22 14:52:35 -040011807/**
11808 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11809 * @phba: Pointer to HBA context object.
11810 *
11811 * This function is called with no lock held. This function processes all
11812 * the received buffers and gives it to upper layers when a received buffer
11813 * indicates that it is the final frame in the sequence. The interrupt
11814 * service routine processes received buffers at interrupt contexts and adds
11815 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11816 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11817 * appropriate receive function when the final frame in a sequence is received.
11818 **/
James Smart4d9ab992009-10-02 15:16:39 -040011819void
11820lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11821 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011822{
James Smart4d9ab992009-10-02 15:16:39 -040011823 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011824 struct fc_frame_header *fc_hdr;
11825 struct lpfc_vport *vport;
11826 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011827
James Smart4f774512009-05-22 14:52:35 -040011828 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011829 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11830 /* check to see if this a valid type of frame */
11831 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11832 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11833 return;
11834 }
11835 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11836 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011837 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011838 /* throw out the frame */
11839 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11840 return;
11841 }
James Smart6669f9b2009-10-02 15:16:45 -040011842 /* Handle the basic abort sequence (BA_ABTS) event */
11843 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11844 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11845 return;
11846 }
11847
James Smart4d9ab992009-10-02 15:16:39 -040011848 /* Link this frame */
11849 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11850 if (!seq_dmabuf) {
11851 /* unable to add frame to vport - throw it out */
11852 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11853 return;
11854 }
11855 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011856 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011857 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011858
James Smart6669f9b2009-10-02 15:16:45 -040011859 /* Send the complete sequence to the upper layer protocol */
11860 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011861}
James Smart6fb120a2009-05-22 14:52:59 -040011862
11863/**
11864 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11865 * @phba: pointer to lpfc hba data structure.
11866 *
11867 * This routine is invoked to post rpi header templates to the
11868 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011869 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11870 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011871 *
11872 * This routine does not require any locks. It's usage is expected
11873 * to be driver load or reset recovery when the driver is
11874 * sequential.
11875 *
11876 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011877 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040011878 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040011879 * When this error occurs, the driver is not guaranteed
11880 * to have any rpi regions posted to the device and
11881 * must either attempt to repost the regions or take a
11882 * fatal error.
11883 **/
11884int
11885lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11886{
11887 struct lpfc_rpi_hdr *rpi_page;
11888 uint32_t rc = 0;
11889
11890 /* Post all rpi memory regions to the port. */
11891 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11892 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11893 if (rc != MBX_SUCCESS) {
11894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11895 "2008 Error %d posting all rpi "
11896 "headers\n", rc);
11897 rc = -EIO;
11898 break;
11899 }
11900 }
11901
11902 return rc;
11903}
11904
11905/**
11906 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11907 * @phba: pointer to lpfc hba data structure.
11908 * @rpi_page: pointer to the rpi memory region.
11909 *
11910 * This routine is invoked to post a single rpi header to the
11911 * HBA consistent with the SLI-4 interface spec. This memory region
11912 * maps up to 64 rpi context regions.
11913 *
11914 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011915 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040011916 * -ENOMEM - No available memory
11917 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040011918 **/
11919int
11920lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11921{
11922 LPFC_MBOXQ_t *mboxq;
11923 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11924 uint32_t rc = 0;
11925 uint32_t mbox_tmo;
11926 uint32_t shdr_status, shdr_add_status;
11927 union lpfc_sli4_cfg_shdr *shdr;
11928
11929 /* The port is notified of the header region via a mailbox command. */
11930 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11931 if (!mboxq) {
11932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11933 "2001 Unable to allocate memory for issuing "
11934 "SLI_CONFIG_SPECIAL mailbox command\n");
11935 return -ENOMEM;
11936 }
11937
11938 /* Post all rpi memory regions to the port. */
11939 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11940 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11941 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11942 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11943 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11944 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11945 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11946 hdr_tmpl, rpi_page->page_count);
11947 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11948 rpi_page->start_rpi);
11949 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11950 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011951 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011952 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11953 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11954 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11955 if (rc != MBX_TIMEOUT)
11956 mempool_free(mboxq, phba->mbox_mem_pool);
11957 if (shdr_status || shdr_add_status || rc) {
11958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11959 "2514 POST_RPI_HDR mailbox failed with "
11960 "status x%x add_status x%x, mbx status x%x\n",
11961 shdr_status, shdr_add_status, rc);
11962 rc = -ENXIO;
11963 }
11964 return rc;
11965}
11966
11967/**
11968 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11969 * @phba: pointer to lpfc hba data structure.
11970 *
11971 * This routine is invoked to post rpi header templates to the
11972 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011973 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11974 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011975 *
11976 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011977 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011978 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11979 **/
11980int
11981lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11982{
11983 int rpi;
11984 uint16_t max_rpi, rpi_base, rpi_limit;
11985 uint16_t rpi_remaining;
11986 struct lpfc_rpi_hdr *rpi_hdr;
11987
11988 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11989 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11990 rpi_limit = phba->sli4_hba.next_rpi;
11991
11992 /*
11993 * The valid rpi range is not guaranteed to be zero-based. Start
11994 * the search at the rpi_base as reported by the port.
11995 */
11996 spin_lock_irq(&phba->hbalock);
11997 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11998 if (rpi >= rpi_limit || rpi < rpi_base)
11999 rpi = LPFC_RPI_ALLOC_ERROR;
12000 else {
12001 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12002 phba->sli4_hba.max_cfg_param.rpi_used++;
12003 phba->sli4_hba.rpi_count++;
12004 }
12005
12006 /*
12007 * Don't try to allocate more rpi header regions if the device limit
12008 * on available rpis max has been exhausted.
12009 */
12010 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12011 (phba->sli4_hba.rpi_count >= max_rpi)) {
12012 spin_unlock_irq(&phba->hbalock);
12013 return rpi;
12014 }
12015
12016 /*
12017 * If the driver is running low on rpi resources, allocate another
12018 * page now. Note that the next_rpi value is used because
12019 * it represents how many are actually in use whereas max_rpi notes
12020 * how many are supported max by the device.
12021 */
12022 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12023 phba->sli4_hba.rpi_count;
12024 spin_unlock_irq(&phba->hbalock);
12025 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12026 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12027 if (!rpi_hdr) {
12028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12029 "2002 Error Could not grow rpi "
12030 "count\n");
12031 } else {
12032 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12033 }
12034 }
12035
12036 return rpi;
12037}
12038
12039/**
12040 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12041 * @phba: pointer to lpfc hba data structure.
12042 *
12043 * This routine is invoked to release an rpi to the pool of
12044 * available rpis maintained by the driver.
12045 **/
12046void
James Smartd7c47992010-06-08 18:31:54 -040012047__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12048{
12049 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12050 phba->sli4_hba.rpi_count--;
12051 phba->sli4_hba.max_cfg_param.rpi_used--;
12052 }
12053}
12054
12055/**
12056 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12057 * @phba: pointer to lpfc hba data structure.
12058 *
12059 * This routine is invoked to release an rpi to the pool of
12060 * available rpis maintained by the driver.
12061 **/
12062void
James Smart6fb120a2009-05-22 14:52:59 -040012063lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12064{
12065 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012066 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012067 spin_unlock_irq(&phba->hbalock);
12068}
12069
12070/**
12071 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12072 * @phba: pointer to lpfc hba data structure.
12073 *
12074 * This routine is invoked to remove the memory region that
12075 * provided rpi via a bitmask.
12076 **/
12077void
12078lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12079{
12080 kfree(phba->sli4_hba.rpi_bmask);
12081}
12082
12083/**
12084 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12085 * @phba: pointer to lpfc hba data structure.
12086 *
12087 * This routine is invoked to remove the memory region that
12088 * provided rpi via a bitmask.
12089 **/
12090int
12091lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12092{
12093 LPFC_MBOXQ_t *mboxq;
12094 struct lpfc_hba *phba = ndlp->phba;
12095 int rc;
12096
12097 /* The port is notified of the header region via a mailbox command. */
12098 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12099 if (!mboxq)
12100 return -ENOMEM;
12101
12102 /* Post all rpi memory regions to the port. */
12103 lpfc_resume_rpi(mboxq, ndlp);
12104 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12105 if (rc == MBX_NOT_FINISHED) {
12106 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12107 "2010 Resume RPI Mailbox failed "
12108 "status %d, mbxStatus x%x\n", rc,
12109 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12110 mempool_free(mboxq, phba->mbox_mem_pool);
12111 return -EIO;
12112 }
12113 return 0;
12114}
12115
12116/**
12117 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12118 * @phba: pointer to lpfc hba data structure.
12119 * @vpi: vpi value to activate with the port.
12120 *
12121 * This routine is invoked to activate a vpi with the
12122 * port when the host intends to use vports with a
12123 * nonzero vpi.
12124 *
12125 * Returns:
12126 * 0 success
12127 * -Evalue otherwise
12128 **/
12129int
12130lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12131{
12132 LPFC_MBOXQ_t *mboxq;
12133 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012134 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012135 uint32_t mbox_tmo;
12136
12137 if (vpi == 0)
12138 return -EINVAL;
12139 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12140 if (!mboxq)
12141 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040012142 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012143 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12144 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012145 if (rc != MBX_SUCCESS) {
12146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12147 "2022 INIT VPI Mailbox failed "
12148 "status %d, mbxStatus x%x\n", rc,
12149 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012150 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012151 }
James Smart6a9c52c2009-10-02 15:16:51 -040012152 if (rc != MBX_TIMEOUT)
12153 mempool_free(mboxq, phba->mbox_mem_pool);
12154
12155 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012156}
12157
12158/**
12159 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12160 * @phba: pointer to lpfc hba data structure.
12161 * @mboxq: Pointer to mailbox object.
12162 *
12163 * This routine is invoked to manually add a single FCF record. The caller
12164 * must pass a completely initialized FCF_Record. This routine takes
12165 * care of the nonembedded mailbox operations.
12166 **/
12167static void
12168lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12169{
12170 void *virt_addr;
12171 union lpfc_sli4_cfg_shdr *shdr;
12172 uint32_t shdr_status, shdr_add_status;
12173
12174 virt_addr = mboxq->sge_array->addr[0];
12175 /* The IOCTL status is embedded in the mailbox subheader. */
12176 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12177 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12178 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12179
12180 if ((shdr_status || shdr_add_status) &&
12181 (shdr_status != STATUS_FCF_IN_USE))
12182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12183 "2558 ADD_FCF_RECORD mailbox failed with "
12184 "status x%x add_status x%x\n",
12185 shdr_status, shdr_add_status);
12186
12187 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12188}
12189
12190/**
12191 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12192 * @phba: pointer to lpfc hba data structure.
12193 * @fcf_record: pointer to the initialized fcf record to add.
12194 *
12195 * This routine is invoked to manually add a single FCF record. The caller
12196 * must pass a completely initialized FCF_Record. This routine takes
12197 * care of the nonembedded mailbox operations.
12198 **/
12199int
12200lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12201{
12202 int rc = 0;
12203 LPFC_MBOXQ_t *mboxq;
12204 uint8_t *bytep;
12205 void *virt_addr;
12206 dma_addr_t phys_addr;
12207 struct lpfc_mbx_sge sge;
12208 uint32_t alloc_len, req_len;
12209 uint32_t fcfindex;
12210
12211 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12212 if (!mboxq) {
12213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12214 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12215 return -ENOMEM;
12216 }
12217
12218 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12219 sizeof(uint32_t);
12220
12221 /* Allocate DMA memory and set up the non-embedded mailbox command */
12222 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12223 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12224 req_len, LPFC_SLI4_MBX_NEMBED);
12225 if (alloc_len < req_len) {
12226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12227 "2523 Allocated DMA memory size (x%x) is "
12228 "less than the requested DMA memory "
12229 "size (x%x)\n", alloc_len, req_len);
12230 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12231 return -ENOMEM;
12232 }
12233
12234 /*
12235 * Get the first SGE entry from the non-embedded DMA memory. This
12236 * routine only uses a single SGE.
12237 */
12238 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12239 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012240 virt_addr = mboxq->sge_array->addr[0];
12241 /*
12242 * Configure the FCF record for FCFI 0. This is the driver's
12243 * hardcoded default and gets used in nonFIP mode.
12244 */
12245 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12246 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12247 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12248
12249 /*
12250 * Copy the fcf_index and the FCF Record Data. The data starts after
12251 * the FCoE header plus word10. The data copy needs to be endian
12252 * correct.
12253 */
12254 bytep += sizeof(uint32_t);
12255 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12256 mboxq->vport = phba->pport;
12257 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12258 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12259 if (rc == MBX_NOT_FINISHED) {
12260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12261 "2515 ADD_FCF_RECORD mailbox failed with "
12262 "status 0x%x\n", rc);
12263 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12264 rc = -EIO;
12265 } else
12266 rc = 0;
12267
12268 return rc;
12269}
12270
12271/**
12272 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12273 * @phba: pointer to lpfc hba data structure.
12274 * @fcf_record: pointer to the fcf record to write the default data.
12275 * @fcf_index: FCF table entry index.
12276 *
12277 * This routine is invoked to build the driver's default FCF record. The
12278 * values used are hardcoded. This routine handles memory initialization.
12279 *
12280 **/
12281void
12282lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12283 struct fcf_record *fcf_record,
12284 uint16_t fcf_index)
12285{
12286 memset(fcf_record, 0, sizeof(struct fcf_record));
12287 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12288 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12289 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12290 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12291 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12292 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12293 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12294 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12295 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12296 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12297 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12298 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12299 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012300 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012301 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12302 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12303 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12304 /* Set the VLAN bit map */
12305 if (phba->valid_vlan) {
12306 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12307 = 1 << (phba->vlan_id % 8);
12308 }
12309}
12310
12311/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012312 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012313 * @phba: pointer to lpfc hba data structure.
12314 * @fcf_index: FCF table entry offset.
12315 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012316 * This routine is invoked to scan the entire FCF table by reading FCF
12317 * record and processing it one at a time starting from the @fcf_index
12318 * for initial FCF discovery or fast FCF failover rediscovery.
12319 *
12320 * Return 0 if the mailbox command is submitted sucessfully, none 0
12321 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012322 **/
12323int
James Smart0c9ab6f2010-02-26 14:15:57 -050012324lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012325{
12326 int rc = 0, error;
12327 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012328
James Smart32b97932009-07-19 10:01:21 -040012329 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012330 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12331 if (!mboxq) {
12332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12333 "2000 Failed to allocate mbox for "
12334 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012335 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012336 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012337 }
James Smartecfd03c2010-02-12 14:41:27 -050012338 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012339 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012340 if (rc) {
12341 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012342 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012343 }
James Smartecfd03c2010-02-12 14:41:27 -050012344 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012345 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012346 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040012347
12348 spin_lock_irq(&phba->hbalock);
12349 phba->hba_flag |= FCF_TS_INPROG;
12350 spin_unlock_irq(&phba->hbalock);
12351
James Smart6fb120a2009-05-22 14:52:59 -040012352 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012353 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012354 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012355 else {
James Smart38b92ef2010-08-04 16:11:39 -040012356 /* Reset eligible FCF count for new scan */
12357 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040012358 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040012359 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012360 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012361fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012362 if (error) {
12363 if (mboxq)
12364 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040012365 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040012366 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040012367 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040012368 spin_unlock_irq(&phba->hbalock);
12369 }
James Smart6fb120a2009-05-22 14:52:59 -040012370 return error;
12371}
James Smarta0c87cb2009-07-19 10:01:10 -040012372
12373/**
James Smarta93ff372010-10-22 11:06:08 -040012374 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050012375 * @phba: pointer to lpfc hba data structure.
12376 * @fcf_index: FCF table entry offset.
12377 *
12378 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040012379 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050012380 *
12381 * Return 0 if the mailbox command is submitted sucessfully, none 0
12382 * otherwise.
12383 **/
12384int
12385lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12386{
12387 int rc = 0, error;
12388 LPFC_MBOXQ_t *mboxq;
12389
12390 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12391 if (!mboxq) {
12392 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12393 "2763 Failed to allocate mbox for "
12394 "READ_FCF cmd\n");
12395 error = -ENOMEM;
12396 goto fail_fcf_read;
12397 }
12398 /* Construct the read FCF record mailbox command */
12399 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12400 if (rc) {
12401 error = -EINVAL;
12402 goto fail_fcf_read;
12403 }
12404 /* Issue the mailbox command asynchronously */
12405 mboxq->vport = phba->pport;
12406 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12407 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12408 if (rc == MBX_NOT_FINISHED)
12409 error = -EIO;
12410 else
12411 error = 0;
12412
12413fail_fcf_read:
12414 if (error && mboxq)
12415 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12416 return error;
12417}
12418
12419/**
12420 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12421 * @phba: pointer to lpfc hba data structure.
12422 * @fcf_index: FCF table entry offset.
12423 *
12424 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040012425 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050012426 *
12427 * Return 0 if the mailbox command is submitted sucessfully, none 0
12428 * otherwise.
12429 **/
12430int
12431lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12432{
12433 int rc = 0, error;
12434 LPFC_MBOXQ_t *mboxq;
12435
12436 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12437 if (!mboxq) {
12438 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12439 "2758 Failed to allocate mbox for "
12440 "READ_FCF cmd\n");
12441 error = -ENOMEM;
12442 goto fail_fcf_read;
12443 }
12444 /* Construct the read FCF record mailbox command */
12445 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12446 if (rc) {
12447 error = -EINVAL;
12448 goto fail_fcf_read;
12449 }
12450 /* Issue the mailbox command asynchronously */
12451 mboxq->vport = phba->pport;
12452 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12453 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12454 if (rc == MBX_NOT_FINISHED)
12455 error = -EIO;
12456 else
12457 error = 0;
12458
12459fail_fcf_read:
12460 if (error && mboxq)
12461 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12462 return error;
12463}
12464
12465/**
12466 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12467 * @phba: pointer to lpfc hba data structure.
12468 *
12469 * This routine is to get the next eligible FCF record index in a round
12470 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040012471 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050012472 * shall be returned, otherwise, the next eligible FCF record's index
12473 * shall be returned.
12474 **/
12475uint16_t
12476lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12477{
12478 uint16_t next_fcf_index;
12479
James Smart3804dc82010-07-14 15:31:37 -040012480 /* Search start from next bit of currently registered FCF index */
12481 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12482 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050012483 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12484 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040012485 next_fcf_index);
12486
James Smart0c9ab6f2010-02-26 14:15:57 -050012487 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12488 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12489 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12490 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050012491
James Smart3804dc82010-07-14 15:31:37 -040012492 /* Check roundrobin failover list empty condition */
12493 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12494 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12495 "2844 No roundrobin failover FCF available\n");
12496 return LPFC_FCOE_FCF_NEXT_NONE;
12497 }
12498
James Smart3804dc82010-07-14 15:31:37 -040012499 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012500 "2845 Get next roundrobin failover FCF (x%x)\n",
12501 next_fcf_index);
12502
James Smart0c9ab6f2010-02-26 14:15:57 -050012503 return next_fcf_index;
12504}
12505
12506/**
12507 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12508 * @phba: pointer to lpfc hba data structure.
12509 *
12510 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012511 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012512 * does not go beyond the range of the driver allocated bmask dimension
12513 * before setting the bit.
12514 *
12515 * Returns 0 if the index bit successfully set, otherwise, it returns
12516 * -EINVAL.
12517 **/
12518int
12519lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12520{
12521 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12522 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012523 "2610 FCF (x%x) reached driver's book "
12524 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012525 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12526 return -EINVAL;
12527 }
12528 /* Set the eligible FCF record index bmask */
12529 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12530
James Smart3804dc82010-07-14 15:31:37 -040012531 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012532 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040012533 "bmask\n", fcf_index);
12534
James Smart0c9ab6f2010-02-26 14:15:57 -050012535 return 0;
12536}
12537
12538/**
James Smart3804dc82010-07-14 15:31:37 -040012539 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050012540 * @phba: pointer to lpfc hba data structure.
12541 *
12542 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012543 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012544 * does not go beyond the range of the driver allocated bmask dimension
12545 * before clearing the bit.
12546 **/
12547void
12548lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12549{
12550 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12551 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012552 "2762 FCF (x%x) reached driver's book "
12553 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012554 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12555 return;
12556 }
12557 /* Clear the eligible FCF record index bmask */
12558 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040012559
12560 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012561 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040012562 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050012563}
12564
12565/**
James Smartecfd03c2010-02-12 14:41:27 -050012566 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12567 * @phba: pointer to lpfc hba data structure.
12568 *
12569 * This routine is the completion routine for the rediscover FCF table mailbox
12570 * command. If the mailbox command returned failure, it will try to stop the
12571 * FCF rediscover wait timer.
12572 **/
12573void
12574lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12575{
12576 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12577 uint32_t shdr_status, shdr_add_status;
12578
12579 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12580
12581 shdr_status = bf_get(lpfc_mbox_hdr_status,
12582 &redisc_fcf->header.cfg_shdr.response);
12583 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12584 &redisc_fcf->header.cfg_shdr.response);
12585 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012586 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012587 "2746 Requesting for FCF rediscovery failed "
12588 "status x%x add_status x%x\n",
12589 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012590 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012591 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012592 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012593 spin_unlock_irq(&phba->hbalock);
12594 /*
12595 * CVL event triggered FCF rediscover request failed,
12596 * last resort to re-try current registered FCF entry.
12597 */
12598 lpfc_retry_pport_discovery(phba);
12599 } else {
12600 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012601 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012602 spin_unlock_irq(&phba->hbalock);
12603 /*
12604 * DEAD FCF event triggered FCF rediscover request
12605 * failed, last resort to fail over as a link down
12606 * to FCF registration.
12607 */
12608 lpfc_sli4_fcf_dead_failthrough(phba);
12609 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012610 } else {
12611 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012612 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050012613 /*
12614 * Start FCF rediscovery wait timer for pending FCF
12615 * before rescan FCF record table.
12616 */
12617 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012618 }
James Smartecfd03c2010-02-12 14:41:27 -050012619
12620 mempool_free(mbox, phba->mbox_mem_pool);
12621}
12622
12623/**
James Smart3804dc82010-07-14 15:31:37 -040012624 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050012625 * @phba: pointer to lpfc hba data structure.
12626 *
12627 * This routine is invoked to request for rediscovery of the entire FCF table
12628 * by the port.
12629 **/
12630int
12631lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12632{
12633 LPFC_MBOXQ_t *mbox;
12634 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12635 int rc, length;
12636
James Smart0c9ab6f2010-02-26 14:15:57 -050012637 /* Cancel retry delay timers to all vports before FCF rediscover */
12638 lpfc_cancel_all_vport_retry_delay_timer(phba);
12639
James Smartecfd03c2010-02-12 14:41:27 -050012640 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12641 if (!mbox) {
12642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12643 "2745 Failed to allocate mbox for "
12644 "requesting FCF rediscover.\n");
12645 return -ENOMEM;
12646 }
12647
12648 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12649 sizeof(struct lpfc_sli4_cfg_mhdr));
12650 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12651 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12652 length, LPFC_SLI4_MBX_EMBED);
12653
12654 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12655 /* Set count to 0 for invalidating the entire FCF database */
12656 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12657
12658 /* Issue the mailbox command asynchronously */
12659 mbox->vport = phba->pport;
12660 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12661 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12662
12663 if (rc == MBX_NOT_FINISHED) {
12664 mempool_free(mbox, phba->mbox_mem_pool);
12665 return -EIO;
12666 }
12667 return 0;
12668}
12669
12670/**
James Smartfc2b9892010-02-26 14:15:29 -050012671 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12672 * @phba: pointer to lpfc hba data structure.
12673 *
12674 * This function is the failover routine as a last resort to the FCF DEAD
12675 * event when driver failed to perform fast FCF failover.
12676 **/
12677void
12678lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12679{
12680 uint32_t link_state;
12681
12682 /*
12683 * Last resort as FCF DEAD event failover will treat this as
12684 * a link down, but save the link state because we don't want
12685 * it to be changed to Link Down unless it is already down.
12686 */
12687 link_state = phba->link_state;
12688 lpfc_linkdown(phba);
12689 phba->link_state = link_state;
12690
12691 /* Unregister FCF if no devices connected to it */
12692 lpfc_unregister_unused_fcf(phba);
12693}
12694
12695/**
James Smarta0c87cb2009-07-19 10:01:10 -040012696 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12697 * @phba: pointer to lpfc hba data structure.
12698 *
12699 * This function read region 23 and parse TLV for port status to
12700 * decide if the user disaled the port. If the TLV indicates the
12701 * port is disabled, the hba_flag is set accordingly.
12702 **/
12703void
12704lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12705{
12706 LPFC_MBOXQ_t *pmb = NULL;
12707 MAILBOX_t *mb;
12708 uint8_t *rgn23_data = NULL;
12709 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12710 int rc;
12711
12712 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12713 if (!pmb) {
12714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12715 "2600 lpfc_sli_read_serdes_param failed to"
12716 " allocate mailbox memory\n");
12717 goto out;
12718 }
12719 mb = &pmb->u.mb;
12720
12721 /* Get adapter Region 23 data */
12722 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12723 if (!rgn23_data)
12724 goto out;
12725
12726 do {
12727 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12728 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12729
12730 if (rc != MBX_SUCCESS) {
12731 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12732 "2601 lpfc_sli_read_link_ste failed to"
12733 " read config region 23 rc 0x%x Status 0x%x\n",
12734 rc, mb->mbxStatus);
12735 mb->un.varDmp.word_cnt = 0;
12736 }
12737 /*
12738 * dump mem may return a zero when finished or we got a
12739 * mailbox error, either way we are done.
12740 */
12741 if (mb->un.varDmp.word_cnt == 0)
12742 break;
12743 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12744 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12745
12746 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12747 rgn23_data + offset,
12748 mb->un.varDmp.word_cnt);
12749 offset += mb->un.varDmp.word_cnt;
12750 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12751
12752 data_size = offset;
12753 offset = 0;
12754
12755 if (!data_size)
12756 goto out;
12757
12758 /* Check the region signature first */
12759 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12761 "2619 Config region 23 has bad signature\n");
12762 goto out;
12763 }
12764 offset += 4;
12765
12766 /* Check the data structure version */
12767 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12769 "2620 Config region 23 has bad version\n");
12770 goto out;
12771 }
12772 offset += 4;
12773
12774 /* Parse TLV entries in the region */
12775 while (offset < data_size) {
12776 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12777 break;
12778 /*
12779 * If the TLV is not driver specific TLV or driver id is
12780 * not linux driver id, skip the record.
12781 */
12782 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12783 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12784 (rgn23_data[offset + 3] != 0)) {
12785 offset += rgn23_data[offset + 1] * 4 + 4;
12786 continue;
12787 }
12788
12789 /* Driver found a driver specific TLV in the config region */
12790 sub_tlv_len = rgn23_data[offset + 1] * 4;
12791 offset += 4;
12792 tlv_offset = 0;
12793
12794 /*
12795 * Search for configured port state sub-TLV.
12796 */
12797 while ((offset < data_size) &&
12798 (tlv_offset < sub_tlv_len)) {
12799 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12800 offset += 4;
12801 tlv_offset += 4;
12802 break;
12803 }
12804 if (rgn23_data[offset] != PORT_STE_TYPE) {
12805 offset += rgn23_data[offset + 1] * 4 + 4;
12806 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12807 continue;
12808 }
12809
12810 /* This HBA contains PORT_STE configured */
12811 if (!rgn23_data[offset + 2])
12812 phba->hba_flag |= LINK_DISABLED;
12813
12814 goto out;
12815 }
12816 }
12817out:
12818 if (pmb)
12819 mempool_free(pmb, phba->mbox_mem_pool);
12820 kfree(rgn23_data);
12821 return;
12822}
James Smart695a8142010-01-26 23:08:03 -050012823
12824/**
12825 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12826 * @vport: pointer to vport data structure.
12827 *
12828 * This function iterate through the mailboxq and clean up all REG_LOGIN
12829 * and REG_VPI mailbox commands associated with the vport. This function
12830 * is called when driver want to restart discovery of the vport due to
12831 * a Clear Virtual Link event.
12832 **/
12833void
12834lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12835{
12836 struct lpfc_hba *phba = vport->phba;
12837 LPFC_MBOXQ_t *mb, *nextmb;
12838 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040012839 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040012840 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040012841 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040012842 LIST_HEAD(mbox_cmd_list);
James Smart695a8142010-01-26 23:08:03 -050012843
James Smartd439d282010-09-29 11:18:45 -040012844 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050012845 spin_lock_irq(&phba->hbalock);
12846 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12847 if (mb->vport != vport)
12848 continue;
12849
12850 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12851 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12852 continue;
12853
James Smartd439d282010-09-29 11:18:45 -040012854 list_del(&mb->list);
12855 list_add_tail(&mb->list, &mbox_cmd_list);
12856 }
12857 /* Clean up active mailbox command with the vport */
12858 mb = phba->sli.mbox_active;
12859 if (mb && (mb->vport == vport)) {
12860 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12861 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12862 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12863 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12864 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
12865 /* Put reference count for delayed processing */
12866 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
12867 /* Unregister the RPI when mailbox complete */
12868 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12869 }
12870 }
12871 spin_unlock_irq(&phba->hbalock);
12872
12873 /* Release the cleaned-up mailbox commands */
12874 while (!list_empty(&mbox_cmd_list)) {
12875 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050012876 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smartd7c47992010-06-08 18:31:54 -040012877 if (phba->sli_rev == LPFC_SLI_REV4)
12878 __lpfc_sli4_free_rpi(phba,
12879 mb->u.mb.un.varRegLogin.rpi);
James Smart695a8142010-01-26 23:08:03 -050012880 mp = (struct lpfc_dmabuf *) (mb->context1);
12881 if (mp) {
12882 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12883 kfree(mp);
12884 }
James Smart78730cf2010-04-06 15:06:30 -040012885 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040012886 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040012887 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012888 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040012889 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012890 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040012891 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040012892 }
James Smart695a8142010-01-26 23:08:03 -050012893 }
James Smart695a8142010-01-26 23:08:03 -050012894 mempool_free(mb, phba->mbox_mem_pool);
12895 }
James Smartd439d282010-09-29 11:18:45 -040012896
12897 /* Release the ndlp with the cleaned-up active mailbox command */
12898 if (act_mbx_ndlp) {
12899 spin_lock(shost->host_lock);
12900 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12901 spin_unlock(shost->host_lock);
12902 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050012903 }
James Smart695a8142010-01-26 23:08:03 -050012904}
12905
James Smart2a9bf3d2010-06-07 15:24:45 -040012906/**
12907 * lpfc_drain_txq - Drain the txq
12908 * @phba: Pointer to HBA context object.
12909 *
12910 * This function attempt to submit IOCBs on the txq
12911 * to the adapter. For SLI4 adapters, the txq contains
12912 * ELS IOCBs that have been deferred because the there
12913 * are no SGLs. This congestion can occur with large
12914 * vport counts during node discovery.
12915 **/
12916
12917uint32_t
12918lpfc_drain_txq(struct lpfc_hba *phba)
12919{
12920 LIST_HEAD(completions);
12921 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12922 struct lpfc_iocbq *piocbq = 0;
12923 unsigned long iflags = 0;
12924 char *fail_msg = NULL;
12925 struct lpfc_sglq *sglq;
12926 union lpfc_wqe wqe;
12927
12928 spin_lock_irqsave(&phba->hbalock, iflags);
12929 if (pring->txq_cnt > pring->txq_max)
12930 pring->txq_max = pring->txq_cnt;
12931
12932 spin_unlock_irqrestore(&phba->hbalock, iflags);
12933
12934 while (pring->txq_cnt) {
12935 spin_lock_irqsave(&phba->hbalock, iflags);
12936
12937 sglq = __lpfc_sli_get_sglq(phba);
12938 if (!sglq) {
12939 spin_unlock_irqrestore(&phba->hbalock, iflags);
12940 break;
12941 } else {
12942 piocbq = lpfc_sli_ringtx_get(phba, pring);
12943 if (!piocbq) {
12944 /* The txq_cnt out of sync. This should
12945 * never happen
12946 */
12947 sglq = __lpfc_clear_active_sglq(phba,
12948 sglq->sli4_xritag);
12949 spin_unlock_irqrestore(&phba->hbalock, iflags);
12950 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12951 "2823 txq empty and txq_cnt is %d\n ",
12952 pring->txq_cnt);
12953 break;
12954 }
12955 }
12956
12957 /* The xri and iocb resources secured,
12958 * attempt to issue request
12959 */
12960 piocbq->sli4_xritag = sglq->sli4_xritag;
12961 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12962 fail_msg = "to convert bpl to sgl";
12963 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12964 fail_msg = "to convert iocb to wqe";
12965 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12966 fail_msg = " - Wq is full";
12967 else
12968 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12969
12970 if (fail_msg) {
12971 /* Failed means we can't issue and need to cancel */
12972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12973 "2822 IOCB failed %s iotag 0x%x "
12974 "xri 0x%x\n",
12975 fail_msg,
12976 piocbq->iotag, piocbq->sli4_xritag);
12977 list_add_tail(&piocbq->list, &completions);
12978 }
12979 spin_unlock_irqrestore(&phba->hbalock, iflags);
12980 }
12981
James Smart2a9bf3d2010-06-07 15:24:45 -040012982 /* Cancel all the IOCBs that cannot be issued */
12983 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12984 IOERR_SLI_ABORTED);
12985
12986 return pring->txq_cnt;
12987}