blob: d6f3b83f32194ec33e339f9a5ff70478674fc5ec [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040068static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
James Smartf0d9bcc2010-10-22 11:07:09 -040098 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart4f774512009-05-22 14:52:35 -040099
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
James Smarte59058c2008-08-24 21:49:00 -0400408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
James Smarted957682007-06-17 19:56:37 -0500418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
James Smarte59058c2008-08-24 21:49:00 -0400425/**
James Smart3621a712009-04-06 18:47:14 -0400426 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
James Smarted957682007-06-17 19:56:37 -0500435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
James Smarte59058c2008-08-24 21:49:00 -0400442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
James Smart2e0fef82007-06-17 19:56:36 -0500451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400458
459 if (iocbq)
460 phba->iocb_cnt++;
461 if (phba->iocb_cnt > phba->iocb_max)
462 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400463 return iocbq;
464}
465
James Smarte59058c2008-08-24 21:49:00 -0400466/**
James Smartda0436e2009-05-22 14:51:39 -0400467 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468 * @phba: Pointer to HBA context object.
469 * @xritag: XRI value.
470 *
471 * This function clears the sglq pointer from the array of acive
472 * sglq's. The xritag that is passed in is used to index into the
473 * array. Before the xritag can be used it needs to be adjusted
474 * by subtracting the xribase.
475 *
476 * Returns sglq ponter = success, NULL = Failure.
477 **/
478static struct lpfc_sglq *
479__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480{
481 uint16_t adj_xri;
482 struct lpfc_sglq *sglq;
483 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 return NULL;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
James Smart0f65ff62010-02-26 14:14:23 -0500503struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
506 uint16_t adj_xri;
507 struct lpfc_sglq *sglq;
508 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 return NULL;
511 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 return sglq;
513}
514
515/**
James Smart19ca7602010-11-20 23:11:55 -0500516 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
517 * @phba: Pointer to HBA context object.
518 * @ndlp: nodelist pointer for this target.
519 * @xritag: xri used in this exchange.
520 * @rxid: Remote Exchange ID.
521 * @send_rrq: Flag used to determine if we should send rrq els cmd.
522 *
523 * This function is called with hbalock held.
524 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
525 * rrq struct and adds it to the active_rrq_list.
526 *
527 * returns 0 for rrq slot for this xri
528 * < 0 Were not able to get rrq mem or invalid parameter.
529 **/
530static int
531__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
532 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
533{
534 uint16_t adj_xri;
535 struct lpfc_node_rrq *rrq;
536 int empty;
537
538 /*
539 * set the active bit even if there is no mem available.
540 */
541 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
542 if (!ndlp)
543 return -EINVAL;
544 if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
545 return -EINVAL;
546 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
547 if (rrq) {
548 rrq->send_rrq = send_rrq;
549 rrq->xritag = xritag;
550 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
551 rrq->ndlp = ndlp;
552 rrq->nlp_DID = ndlp->nlp_DID;
553 rrq->vport = ndlp->vport;
554 rrq->rxid = rxid;
555 empty = list_empty(&phba->active_rrq_list);
556 if (phba->cfg_enable_rrq && send_rrq)
557 /*
558 * We need the xri before we can add this to the
559 * phba active rrq list.
560 */
561 rrq->send_rrq = send_rrq;
562 else
563 rrq->send_rrq = 0;
564 list_add_tail(&rrq->list, &phba->active_rrq_list);
565 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
566 phba->hba_flag |= HBA_RRQ_ACTIVE;
567 if (empty)
568 lpfc_worker_wake_up(phba);
569 }
570 return 0;
571 }
572 return -ENOMEM;
573}
574
575/**
576 * __lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
577 * @phba: Pointer to HBA context object.
578 * @xritag: xri used in this exchange.
579 * @rrq: The RRQ to be cleared.
580 *
581 * This function is called with hbalock held. This function
582 **/
583static void
584__lpfc_clr_rrq_active(struct lpfc_hba *phba,
585 uint16_t xritag,
586 struct lpfc_node_rrq *rrq)
587{
588 uint16_t adj_xri;
589 struct lpfc_nodelist *ndlp;
590
591 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
592
593 /* The target DID could have been swapped (cable swap)
594 * we should use the ndlp from the findnode if it is
595 * available.
596 */
597 if (!ndlp)
598 ndlp = rrq->ndlp;
599
600 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
601 if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
602 rrq->send_rrq = 0;
603 rrq->xritag = 0;
604 rrq->rrq_stop_time = 0;
605 }
606 mempool_free(rrq, phba->rrq_pool);
607}
608
609/**
610 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
611 * @phba: Pointer to HBA context object.
612 *
613 * This function is called with hbalock held. This function
614 * Checks if stop_time (ratov from setting rrq active) has
615 * been reached, if it has and the send_rrq flag is set then
616 * it will call lpfc_send_rrq. If the send_rrq flag is not set
617 * then it will just call the routine to clear the rrq and
618 * free the rrq resource.
619 * The timer is set to the next rrq that is going to expire before
620 * leaving the routine.
621 *
622 **/
623void
624lpfc_handle_rrq_active(struct lpfc_hba *phba)
625{
626 struct lpfc_node_rrq *rrq;
627 struct lpfc_node_rrq *nextrrq;
628 unsigned long next_time;
629 unsigned long iflags;
630
631 spin_lock_irqsave(&phba->hbalock, iflags);
632 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
633 next_time = jiffies + HZ * (phba->fc_ratov + 1);
634 list_for_each_entry_safe(rrq, nextrrq,
635 &phba->active_rrq_list, list) {
636 if (time_after(jiffies, rrq->rrq_stop_time)) {
637 list_del(&rrq->list);
638 if (!rrq->send_rrq)
639 /* this call will free the rrq */
640 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
641 else {
642 /* if we send the rrq then the completion handler
643 * will clear the bit in the xribitmap.
644 */
645 spin_unlock_irqrestore(&phba->hbalock, iflags);
646 if (lpfc_send_rrq(phba, rrq)) {
647 lpfc_clr_rrq_active(phba, rrq->xritag,
648 rrq);
649 }
650 spin_lock_irqsave(&phba->hbalock, iflags);
651 }
652 } else if (time_before(rrq->rrq_stop_time, next_time))
653 next_time = rrq->rrq_stop_time;
654 }
655 spin_unlock_irqrestore(&phba->hbalock, iflags);
656 if (!list_empty(&phba->active_rrq_list))
657 mod_timer(&phba->rrq_tmr, next_time);
658}
659
660/**
661 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
662 * @vport: Pointer to vport context object.
663 * @xri: The xri used in the exchange.
664 * @did: The targets DID for this exchange.
665 *
666 * returns NULL = rrq not found in the phba->active_rrq_list.
667 * rrq = rrq for this xri and target.
668 **/
669struct lpfc_node_rrq *
670lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
671{
672 struct lpfc_hba *phba = vport->phba;
673 struct lpfc_node_rrq *rrq;
674 struct lpfc_node_rrq *nextrrq;
675 unsigned long iflags;
676
677 if (phba->sli_rev != LPFC_SLI_REV4)
678 return NULL;
679 spin_lock_irqsave(&phba->hbalock, iflags);
680 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
681 if (rrq->vport == vport && rrq->xritag == xri &&
682 rrq->nlp_DID == did){
683 list_del(&rrq->list);
684 spin_unlock_irqrestore(&phba->hbalock, iflags);
685 return rrq;
686 }
687 }
688 spin_unlock_irqrestore(&phba->hbalock, iflags);
689 return NULL;
690}
691
692/**
693 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
694 * @vport: Pointer to vport context object.
695 *
696 * Remove all active RRQs for this vport from the phba->active_rrq_list and
697 * clear the rrq.
698 **/
699void
700lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport)
701
702{
703 struct lpfc_hba *phba = vport->phba;
704 struct lpfc_node_rrq *rrq;
705 struct lpfc_node_rrq *nextrrq;
706 unsigned long iflags;
707
708 if (phba->sli_rev != LPFC_SLI_REV4)
709 return;
710 spin_lock_irqsave(&phba->hbalock, iflags);
711 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
712 if (rrq->vport == vport) {
713 list_del(&rrq->list);
714 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
715 }
716 }
717 spin_unlock_irqrestore(&phba->hbalock, iflags);
718}
719
720/**
721 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
722 * @phba: Pointer to HBA context object.
723 *
724 * Remove all rrqs from the phba->active_rrq_list and free them by
725 * calling __lpfc_clr_active_rrq
726 *
727 **/
728void
729lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
730{
731 struct lpfc_node_rrq *rrq;
732 struct lpfc_node_rrq *nextrrq;
733 unsigned long next_time;
734 unsigned long iflags;
735
736 if (phba->sli_rev != LPFC_SLI_REV4)
737 return;
738 spin_lock_irqsave(&phba->hbalock, iflags);
739 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
740 next_time = jiffies + HZ * (phba->fc_ratov * 2);
741 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
742 list_del(&rrq->list);
743 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
744 }
745 spin_unlock_irqrestore(&phba->hbalock, iflags);
746 if (!list_empty(&phba->active_rrq_list))
747 mod_timer(&phba->rrq_tmr, next_time);
748}
749
750
751/**
752 * __lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
753 * @phba: Pointer to HBA context object.
754 * @ndlp: Targets nodelist pointer for this exchange.
755 * @xritag the xri in the bitmap to test.
756 *
757 * This function is called with hbalock held. This function
758 * returns 0 = rrq not active for this xri
759 * 1 = rrq is valid for this xri.
760 **/
761static int
762__lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
763 uint16_t xritag)
764{
765 uint16_t adj_xri;
766
767 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
768 if (!ndlp)
769 return 0;
770 if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
771 return 1;
772 else
773 return 0;
774}
775
776/**
777 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
778 * @phba: Pointer to HBA context object.
779 * @ndlp: nodelist pointer for this target.
780 * @xritag: xri used in this exchange.
781 * @rxid: Remote Exchange ID.
782 * @send_rrq: Flag used to determine if we should send rrq els cmd.
783 *
784 * This function takes the hbalock.
785 * The active bit is always set in the active rrq xri_bitmap even
786 * if there is no slot avaiable for the other rrq information.
787 *
788 * returns 0 rrq actived for this xri
789 * < 0 No memory or invalid ndlp.
790 **/
791int
792lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
793 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
794{
795 int ret;
796 unsigned long iflags;
797
798 spin_lock_irqsave(&phba->hbalock, iflags);
799 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
800 spin_unlock_irqrestore(&phba->hbalock, iflags);
801 return ret;
802}
803
804/**
805 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
806 * @phba: Pointer to HBA context object.
807 * @xritag: xri used in this exchange.
808 * @rrq: The RRQ to be cleared.
809 *
810 * This function is takes the hbalock.
811 **/
812void
813lpfc_clr_rrq_active(struct lpfc_hba *phba,
814 uint16_t xritag,
815 struct lpfc_node_rrq *rrq)
816{
817 unsigned long iflags;
818
819 spin_lock_irqsave(&phba->hbalock, iflags);
820 __lpfc_clr_rrq_active(phba, xritag, rrq);
821 spin_unlock_irqrestore(&phba->hbalock, iflags);
822 return;
823}
824
825
826
827/**
828 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
829 * @phba: Pointer to HBA context object.
830 * @ndlp: Targets nodelist pointer for this exchange.
831 * @xritag the xri in the bitmap to test.
832 *
833 * This function takes the hbalock.
834 * returns 0 = rrq not active for this xri
835 * 1 = rrq is valid for this xri.
836 **/
837int
838lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
839 uint16_t xritag)
840{
841 int ret;
842 unsigned long iflags;
843
844 spin_lock_irqsave(&phba->hbalock, iflags);
845 ret = __lpfc_test_rrq_active(phba, ndlp, xritag);
846 spin_unlock_irqrestore(&phba->hbalock, iflags);
847 return ret;
848}
849
850/**
James Smartda0436e2009-05-22 14:51:39 -0400851 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
852 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500853 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400854 *
855 * This function is called with hbalock held. This function
856 * Gets a new driver sglq object from the sglq list. If the
857 * list is not empty then it is successful, it returns pointer to the newly
858 * allocated sglq object else it returns NULL.
859 **/
860static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500861__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400862{
863 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
864 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500865 struct lpfc_sglq *start_sglq = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400866 uint16_t adj_xri;
James Smart19ca7602010-11-20 23:11:55 -0500867 struct lpfc_scsi_buf *lpfc_cmd;
868 struct lpfc_nodelist *ndlp;
869 int found = 0;
870
871 if (piocbq->iocb_flag & LPFC_IO_FCP) {
872 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
873 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500874 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
875 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500876 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500877 else
878 ndlp = piocbq->context1;
879
James Smartda0436e2009-05-22 14:51:39 -0400880 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500881 start_sglq = sglq;
882 while (!found) {
883 if (!sglq)
884 return NULL;
885 adj_xri = sglq->sli4_xritag -
886 phba->sli4_hba.max_cfg_param.xri_base;
887 if (__lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
888 /* This xri has an rrq outstanding for this DID.
889 * put it back in the list and get another xri.
890 */
891 list_add_tail(&sglq->list, lpfc_sgl_list);
892 sglq = NULL;
893 list_remove_head(lpfc_sgl_list, sglq,
894 struct lpfc_sglq, list);
895 if (sglq == start_sglq) {
896 sglq = NULL;
897 break;
898 } else
899 continue;
900 }
901 sglq->ndlp = ndlp;
902 found = 1;
903 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
904 sglq->state = SGL_ALLOCATED;
905 }
James Smartda0436e2009-05-22 14:51:39 -0400906 return sglq;
907}
908
909/**
James Smart3621a712009-04-06 18:47:14 -0400910 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400911 * @phba: Pointer to HBA context object.
912 *
913 * This function is called with no lock held. This function
914 * allocates a new driver iocb object from the iocb pool. If the
915 * allocation is successful, it returns pointer to the newly
916 * allocated iocb object else it returns NULL.
917 **/
James Smart2e0fef82007-06-17 19:56:36 -0500918struct lpfc_iocbq *
919lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500920{
James Smart2e0fef82007-06-17 19:56:36 -0500921 struct lpfc_iocbq * iocbq = NULL;
922 unsigned long iflags;
923
924 spin_lock_irqsave(&phba->hbalock, iflags);
925 iocbq = __lpfc_sli_get_iocbq(phba);
926 spin_unlock_irqrestore(&phba->hbalock, iflags);
927 return iocbq;
928}
929
James Smarte59058c2008-08-24 21:49:00 -0400930/**
James Smart4f774512009-05-22 14:52:35 -0400931 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
932 * @phba: Pointer to HBA context object.
933 * @iocbq: Pointer to driver iocb object.
934 *
935 * This function is called with hbalock held to release driver
936 * iocb object to the iocb pool. The iotag in the iocb object
937 * does not change for each use of the iocb object. This function
938 * clears all other fields of the iocb object when it is freed.
939 * The sqlq structure that holds the xritag and phys and virtual
940 * mappings for the scatter gather list is retrieved from the
941 * active array of sglq. The get of the sglq pointer also clears
942 * the entry in the array. If the status of the IO indiactes that
943 * this IO was aborted then the sglq entry it put on the
944 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
945 * IO has good status or fails for any other reason then the sglq
946 * entry is added to the free list (lpfc_sgl_list).
947 **/
948static void
949__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
950{
951 struct lpfc_sglq *sglq;
952 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400953 unsigned long iflag = 0;
954 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400955
956 if (iocbq->sli4_xritag == NO_XRI)
957 sglq = NULL;
958 else
959 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
960 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500961 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
962 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400963 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
964 iflag);
965 list_add(&sglq->list,
966 &phba->sli4_hba.lpfc_abts_els_sgl_list);
967 spin_unlock_irqrestore(
968 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500969 } else {
970 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500971 sglq->ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -0400972 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400973
974 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400975 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400976 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500977 }
James Smart4f774512009-05-22 14:52:35 -0400978 }
979
980
981 /*
982 * Clean all volatile data fields, preserve iotag and node struct.
983 */
984 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985 iocbq->sli4_xritag = NO_XRI;
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987}
988
James Smart2a9bf3d2010-06-07 15:24:45 -0400989
James Smart4f774512009-05-22 14:52:35 -0400990/**
James Smart3772a992009-05-22 14:50:54 -0400991 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
992 * @phba: Pointer to HBA context object.
993 * @iocbq: Pointer to driver iocb object.
994 *
995 * This function is called with hbalock held to release driver
996 * iocb object to the iocb pool. The iotag in the iocb object
997 * does not change for each use of the iocb object. This function
998 * clears all other fields of the iocb object when it is freed.
999 **/
1000static void
1001__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1002{
1003 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1004
1005 /*
1006 * Clean all volatile data fields, preserve iotag and node struct.
1007 */
1008 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1009 iocbq->sli4_xritag = NO_XRI;
1010 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1011}
1012
1013/**
James Smart3621a712009-04-06 18:47:14 -04001014 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001015 * @phba: Pointer to HBA context object.
1016 * @iocbq: Pointer to driver iocb object.
1017 *
1018 * This function is called with hbalock held to release driver
1019 * iocb object to the iocb pool. The iotag in the iocb object
1020 * does not change for each use of the iocb object. This function
1021 * clears all other fields of the iocb object when it is freed.
1022 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001023static void
James Smart2e0fef82007-06-17 19:56:36 -05001024__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1025{
James Smart3772a992009-05-22 14:50:54 -04001026 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001027 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001028}
1029
James Smarte59058c2008-08-24 21:49:00 -04001030/**
James Smart3621a712009-04-06 18:47:14 -04001031 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001032 * @phba: Pointer to HBA context object.
1033 * @iocbq: Pointer to driver iocb object.
1034 *
1035 * This function is called with no lock held to release the iocb to
1036 * iocb pool.
1037 **/
James Smart2e0fef82007-06-17 19:56:36 -05001038void
1039lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1040{
1041 unsigned long iflags;
1042
1043 /*
1044 * Clean all volatile data fields, preserve iotag and node struct.
1045 */
1046 spin_lock_irqsave(&phba->hbalock, iflags);
1047 __lpfc_sli_release_iocbq(phba, iocbq);
1048 spin_unlock_irqrestore(&phba->hbalock, iflags);
1049}
1050
James Smarte59058c2008-08-24 21:49:00 -04001051/**
James Smarta257bf92009-04-06 18:48:10 -04001052 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1053 * @phba: Pointer to HBA context object.
1054 * @iocblist: List of IOCBs.
1055 * @ulpstatus: ULP status in IOCB command field.
1056 * @ulpWord4: ULP word-4 in IOCB command field.
1057 *
1058 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1059 * on the list by invoking the complete callback function associated with the
1060 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1061 * fields.
1062 **/
1063void
1064lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1065 uint32_t ulpstatus, uint32_t ulpWord4)
1066{
1067 struct lpfc_iocbq *piocb;
1068
1069 while (!list_empty(iocblist)) {
1070 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1071
1072 if (!piocb->iocb_cmpl)
1073 lpfc_sli_release_iocbq(phba, piocb);
1074 else {
1075 piocb->iocb.ulpStatus = ulpstatus;
1076 piocb->iocb.un.ulpWord[4] = ulpWord4;
1077 (piocb->iocb_cmpl) (phba, piocb, piocb);
1078 }
1079 }
1080 return;
1081}
1082
1083/**
James Smart3621a712009-04-06 18:47:14 -04001084 * lpfc_sli_iocb_cmd_type - Get the iocb type
1085 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001086 *
1087 * This function is called by ring event handler function to get the iocb type.
1088 * This function translates the iocb command to an iocb command type used to
1089 * decide the final disposition of each completed IOCB.
1090 * The function returns
1091 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1092 * LPFC_SOL_IOCB if it is a solicited iocb completion
1093 * LPFC_ABORT_IOCB if it is an abort iocb
1094 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1095 *
1096 * The caller is not required to hold any lock.
1097 **/
dea31012005-04-17 16:05:31 -05001098static lpfc_iocb_type
1099lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1100{
1101 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1102
1103 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1104 return 0;
1105
1106 switch (iocb_cmnd) {
1107 case CMD_XMIT_SEQUENCE_CR:
1108 case CMD_XMIT_SEQUENCE_CX:
1109 case CMD_XMIT_BCAST_CN:
1110 case CMD_XMIT_BCAST_CX:
1111 case CMD_ELS_REQUEST_CR:
1112 case CMD_ELS_REQUEST_CX:
1113 case CMD_CREATE_XRI_CR:
1114 case CMD_CREATE_XRI_CX:
1115 case CMD_GET_RPI_CN:
1116 case CMD_XMIT_ELS_RSP_CX:
1117 case CMD_GET_RPI_CR:
1118 case CMD_FCP_IWRITE_CR:
1119 case CMD_FCP_IWRITE_CX:
1120 case CMD_FCP_IREAD_CR:
1121 case CMD_FCP_IREAD_CX:
1122 case CMD_FCP_ICMND_CR:
1123 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001124 case CMD_FCP_TSEND_CX:
1125 case CMD_FCP_TRSP_CX:
1126 case CMD_FCP_TRECEIVE_CX:
1127 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001128 case CMD_ADAPTER_MSG:
1129 case CMD_ADAPTER_DUMP:
1130 case CMD_XMIT_SEQUENCE64_CR:
1131 case CMD_XMIT_SEQUENCE64_CX:
1132 case CMD_XMIT_BCAST64_CN:
1133 case CMD_XMIT_BCAST64_CX:
1134 case CMD_ELS_REQUEST64_CR:
1135 case CMD_ELS_REQUEST64_CX:
1136 case CMD_FCP_IWRITE64_CR:
1137 case CMD_FCP_IWRITE64_CX:
1138 case CMD_FCP_IREAD64_CR:
1139 case CMD_FCP_IREAD64_CX:
1140 case CMD_FCP_ICMND64_CR:
1141 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001142 case CMD_FCP_TSEND64_CX:
1143 case CMD_FCP_TRSP64_CX:
1144 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001145 case CMD_GEN_REQUEST64_CR:
1146 case CMD_GEN_REQUEST64_CX:
1147 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001148 case DSSCMD_IWRITE64_CR:
1149 case DSSCMD_IWRITE64_CX:
1150 case DSSCMD_IREAD64_CR:
1151 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001152 type = LPFC_SOL_IOCB;
1153 break;
1154 case CMD_ABORT_XRI_CN:
1155 case CMD_ABORT_XRI_CX:
1156 case CMD_CLOSE_XRI_CN:
1157 case CMD_CLOSE_XRI_CX:
1158 case CMD_XRI_ABORTED_CX:
1159 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001160 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001161 type = LPFC_ABORT_IOCB;
1162 break;
1163 case CMD_RCV_SEQUENCE_CX:
1164 case CMD_RCV_ELS_REQ_CX:
1165 case CMD_RCV_SEQUENCE64_CX:
1166 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001167 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001168 case CMD_IOCB_RCV_SEQ64_CX:
1169 case CMD_IOCB_RCV_ELS64_CX:
1170 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001171 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001172 type = LPFC_UNSOL_IOCB;
1173 break;
James Smart3163f722008-02-08 18:50:25 -05001174 case CMD_IOCB_XMIT_MSEQ64_CR:
1175 case CMD_IOCB_XMIT_MSEQ64_CX:
1176 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1177 case CMD_IOCB_RCV_ELS_LIST64_CX:
1178 case CMD_IOCB_CLOSE_EXTENDED_CN:
1179 case CMD_IOCB_ABORT_EXTENDED_CN:
1180 case CMD_IOCB_RET_HBQE64_CN:
1181 case CMD_IOCB_FCP_IBIDIR64_CR:
1182 case CMD_IOCB_FCP_IBIDIR64_CX:
1183 case CMD_IOCB_FCP_ITASKMGT64_CX:
1184 case CMD_IOCB_LOGENTRY_CN:
1185 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1186 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001187 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001188 type = LPFC_UNKNOWN_IOCB;
1189 break;
dea31012005-04-17 16:05:31 -05001190 default:
1191 type = LPFC_UNKNOWN_IOCB;
1192 break;
1193 }
1194
1195 return type;
1196}
1197
James Smarte59058c2008-08-24 21:49:00 -04001198/**
James Smart3621a712009-04-06 18:47:14 -04001199 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001200 * @phba: Pointer to HBA context object.
1201 *
1202 * This function is called from SLI initialization code
1203 * to configure every ring of the HBA's SLI interface. The
1204 * caller is not required to hold any lock. This function issues
1205 * a config_ring mailbox command for each ring.
1206 * This function returns zero if successful else returns a negative
1207 * error code.
1208 **/
dea31012005-04-17 16:05:31 -05001209static int
James Smarted957682007-06-17 19:56:37 -05001210lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001211{
1212 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001213 LPFC_MBOXQ_t *pmb;
1214 MAILBOX_t *pmbox;
1215 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001216
James Smarted957682007-06-17 19:56:37 -05001217 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1218 if (!pmb)
1219 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001220 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001221 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001222 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001223 lpfc_config_ring(phba, i, pmb);
1224 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1225 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001227 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001228 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1229 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001230 rc, pmbox->mbxCommand,
1231 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001232 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001233 ret = -ENXIO;
1234 break;
dea31012005-04-17 16:05:31 -05001235 }
1236 }
James Smarted957682007-06-17 19:56:37 -05001237 mempool_free(pmb, phba->mbox_mem_pool);
1238 return ret;
dea31012005-04-17 16:05:31 -05001239}
1240
James Smarte59058c2008-08-24 21:49:00 -04001241/**
James Smart3621a712009-04-06 18:47:14 -04001242 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001243 * @phba: Pointer to HBA context object.
1244 * @pring: Pointer to driver SLI ring object.
1245 * @piocb: Pointer to the driver iocb object.
1246 *
1247 * This function is called with hbalock held. The function adds the
1248 * new iocb to txcmplq of the given ring. This function always returns
1249 * 0. If this function is called for ELS ring, this function checks if
1250 * there is a vport associated with the ELS command. This function also
1251 * starts els_tmofunc timer if this is an ELS command.
1252 **/
dea31012005-04-17 16:05:31 -05001253static int
James Smart2e0fef82007-06-17 19:56:36 -05001254lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1255 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001256{
dea31012005-04-17 16:05:31 -05001257 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001258 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001259 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001260 if (pring->txcmplq_cnt > pring->txcmplq_max)
1261 pring->txcmplq_max = pring->txcmplq_cnt;
1262
James Smart92d7f7b2007-06-17 19:56:38 -05001263 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1264 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1265 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1266 if (!piocb->vport)
1267 BUG();
1268 else
1269 mod_timer(&piocb->vport->els_tmofunc,
1270 jiffies + HZ * (phba->fc_ratov << 1));
1271 }
1272
dea31012005-04-17 16:05:31 -05001273
James Smart2e0fef82007-06-17 19:56:36 -05001274 return 0;
dea31012005-04-17 16:05:31 -05001275}
1276
James Smarte59058c2008-08-24 21:49:00 -04001277/**
James Smart3621a712009-04-06 18:47:14 -04001278 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001279 * @phba: Pointer to HBA context object.
1280 * @pring: Pointer to driver SLI ring object.
1281 *
1282 * This function is called with hbalock held to get next
1283 * iocb in txq of the given ring. If there is any iocb in
1284 * the txq, the function returns first iocb in the list after
1285 * removing the iocb from the list, else it returns NULL.
1286 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001287struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001288lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001289{
dea31012005-04-17 16:05:31 -05001290 struct lpfc_iocbq *cmd_iocb;
1291
James Smart858c9f62007-06-17 19:56:39 -05001292 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1293 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001294 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001295 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001296}
1297
James Smarte59058c2008-08-24 21:49:00 -04001298/**
James Smart3621a712009-04-06 18:47:14 -04001299 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001300 * @phba: Pointer to HBA context object.
1301 * @pring: Pointer to driver SLI ring object.
1302 *
1303 * This function is called with hbalock held and the caller must post the
1304 * iocb without releasing the lock. If the caller releases the lock,
1305 * iocb slot returned by the function is not guaranteed to be available.
1306 * The function returns pointer to the next available iocb slot if there
1307 * is available slot in the ring, else it returns NULL.
1308 * If the get index of the ring is ahead of the put index, the function
1309 * will post an error attention event to the worker thread to take the
1310 * HBA to offline state.
1311 **/
dea31012005-04-17 16:05:31 -05001312static IOCB_t *
1313lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1314{
James Smart34b02dc2008-08-24 21:49:55 -04001315 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001316 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001317 if ((pring->next_cmdidx == pring->cmdidx) &&
1318 (++pring->next_cmdidx >= max_cmd_idx))
1319 pring->next_cmdidx = 0;
1320
1321 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1322
1323 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1324
1325 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1326 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001327 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001328 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001329 pring->ringno,
dea31012005-04-17 16:05:31 -05001330 pring->local_getidx, max_cmd_idx);
1331
James Smart2e0fef82007-06-17 19:56:36 -05001332 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001333 /*
1334 * All error attention handlers are posted to
1335 * worker thread
1336 */
1337 phba->work_ha |= HA_ERATT;
1338 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001339
James Smart5e9d9b82008-06-14 22:52:53 -04001340 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001341
1342 return NULL;
1343 }
1344
1345 if (pring->local_getidx == pring->next_cmdidx)
1346 return NULL;
1347 }
1348
James Smarted957682007-06-17 19:56:37 -05001349 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001350}
1351
James Smarte59058c2008-08-24 21:49:00 -04001352/**
James Smart3621a712009-04-06 18:47:14 -04001353 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001354 * @phba: Pointer to HBA context object.
1355 * @iocbq: Pointer to driver iocb object.
1356 *
1357 * This function gets an iotag for the iocb. If there is no unused iotag and
1358 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1359 * array and assigns a new iotag.
1360 * The function returns the allocated iotag if successful, else returns zero.
1361 * Zero is not a valid iotag.
1362 * The caller is not required to hold any lock.
1363 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001364uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001365lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001366{
James Smart2e0fef82007-06-17 19:56:36 -05001367 struct lpfc_iocbq **new_arr;
1368 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001369 size_t new_len;
1370 struct lpfc_sli *psli = &phba->sli;
1371 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001372
James Smart2e0fef82007-06-17 19:56:36 -05001373 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001374 iotag = psli->last_iotag;
1375 if(++iotag < psli->iocbq_lookup_len) {
1376 psli->last_iotag = iotag;
1377 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001378 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001379 iocbq->iotag = iotag;
1380 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001381 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001382 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1383 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001384 spin_unlock_irq(&phba->hbalock);
1385 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001386 GFP_KERNEL);
1387 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001388 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001389 old_arr = psli->iocbq_lookup;
1390 if (new_len <= psli->iocbq_lookup_len) {
1391 /* highly unprobable case */
1392 kfree(new_arr);
1393 iotag = psli->last_iotag;
1394 if(++iotag < psli->iocbq_lookup_len) {
1395 psli->last_iotag = iotag;
1396 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001397 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001398 iocbq->iotag = iotag;
1399 return iotag;
1400 }
James Smart2e0fef82007-06-17 19:56:36 -05001401 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001402 return 0;
1403 }
1404 if (psli->iocbq_lookup)
1405 memcpy(new_arr, old_arr,
1406 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001407 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001408 psli->iocbq_lookup = new_arr;
1409 psli->iocbq_lookup_len = new_len;
1410 psli->last_iotag = iotag;
1411 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001412 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001413 iocbq->iotag = iotag;
1414 kfree(old_arr);
1415 return iotag;
1416 }
James Smart8f6d98d2006-08-01 07:34:00 -04001417 } else
James Smart2e0fef82007-06-17 19:56:36 -05001418 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001419
James Smartbc739052010-08-04 16:11:18 -04001420 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001421 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1422 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001423
James Bottomley604a3e32005-10-29 10:28:33 -05001424 return 0;
dea31012005-04-17 16:05:31 -05001425}
1426
James Smarte59058c2008-08-24 21:49:00 -04001427/**
James Smart3621a712009-04-06 18:47:14 -04001428 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001429 * @phba: Pointer to HBA context object.
1430 * @pring: Pointer to driver SLI ring object.
1431 * @iocb: Pointer to iocb slot in the ring.
1432 * @nextiocb: Pointer to driver iocb object which need to be
1433 * posted to firmware.
1434 *
1435 * This function is called with hbalock held to post a new iocb to
1436 * the firmware. This function copies the new iocb to ring iocb slot and
1437 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1438 * a completion call back for this iocb else the function will free the
1439 * iocb object.
1440 **/
dea31012005-04-17 16:05:31 -05001441static void
1442lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1443 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1444{
1445 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001446 * Set up an iotag
dea31012005-04-17 16:05:31 -05001447 */
James Bottomley604a3e32005-10-29 10:28:33 -05001448 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001449
James Smarte2a0a9d2008-12-04 22:40:02 -05001450
James Smarta58cbd52007-08-02 11:09:43 -04001451 if (pring->ringno == LPFC_ELS_RING) {
1452 lpfc_debugfs_slow_ring_trc(phba,
1453 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1454 *(((uint32_t *) &nextiocb->iocb) + 4),
1455 *(((uint32_t *) &nextiocb->iocb) + 6),
1456 *(((uint32_t *) &nextiocb->iocb) + 7));
1457 }
1458
dea31012005-04-17 16:05:31 -05001459 /*
1460 * Issue iocb command to adapter
1461 */
James Smart92d7f7b2007-06-17 19:56:38 -05001462 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001463 wmb();
1464 pring->stats.iocb_cmd++;
1465
1466 /*
1467 * If there is no completion routine to call, we can release the
1468 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1469 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1470 */
1471 if (nextiocb->iocb_cmpl)
1472 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001473 else
James Smart2e0fef82007-06-17 19:56:36 -05001474 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001475
1476 /*
1477 * Let the HBA know what IOCB slot will be the next one the
1478 * driver will put a command into.
1479 */
1480 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001481 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001482}
1483
James Smarte59058c2008-08-24 21:49:00 -04001484/**
James Smart3621a712009-04-06 18:47:14 -04001485 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001486 * @phba: Pointer to HBA context object.
1487 * @pring: Pointer to driver SLI ring object.
1488 *
1489 * The caller is not required to hold any lock for calling this function.
1490 * This function updates the chip attention bits for the ring to inform firmware
1491 * that there are pending work to be done for this ring and requests an
1492 * interrupt when there is space available in the ring. This function is
1493 * called when the driver is unable to post more iocbs to the ring due
1494 * to unavailability of space in the ring.
1495 **/
dea31012005-04-17 16:05:31 -05001496static void
James Smart2e0fef82007-06-17 19:56:36 -05001497lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001498{
1499 int ringno = pring->ringno;
1500
1501 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1502
1503 wmb();
1504
1505 /*
1506 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1507 * The HBA will tell us when an IOCB entry is available.
1508 */
1509 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1510 readl(phba->CAregaddr); /* flush */
1511
1512 pring->stats.iocb_cmd_full++;
1513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart3621a712009-04-06 18:47:14 -04001516 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 *
1520 * This function updates the chip attention register bit for the
1521 * given ring to inform HBA that there is more work to be done
1522 * in this ring. The caller is not required to hold any lock.
1523 **/
dea31012005-04-17 16:05:31 -05001524static void
James Smart2e0fef82007-06-17 19:56:36 -05001525lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001526{
1527 int ringno = pring->ringno;
1528
1529 /*
1530 * Tell the HBA that there is work to do in this ring.
1531 */
James Smart34b02dc2008-08-24 21:49:55 -04001532 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1533 wmb();
1534 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1535 readl(phba->CAregaddr); /* flush */
1536 }
dea31012005-04-17 16:05:31 -05001537}
1538
James Smarte59058c2008-08-24 21:49:00 -04001539/**
James Smart3621a712009-04-06 18:47:14 -04001540 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001541 * @phba: Pointer to HBA context object.
1542 * @pring: Pointer to driver SLI ring object.
1543 *
1544 * This function is called with hbalock held to post pending iocbs
1545 * in the txq to the firmware. This function is called when driver
1546 * detects space available in the ring.
1547 **/
dea31012005-04-17 16:05:31 -05001548static void
James Smart2e0fef82007-06-17 19:56:36 -05001549lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001550{
1551 IOCB_t *iocb;
1552 struct lpfc_iocbq *nextiocb;
1553
1554 /*
1555 * Check to see if:
1556 * (a) there is anything on the txq to send
1557 * (b) link is up
1558 * (c) link attention events can be processed (fcp ring only)
1559 * (d) IOCB processing is not blocked by the outstanding mbox command.
1560 */
1561 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001562 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001563 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001564 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001565
1566 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1567 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1568 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1569
1570 if (iocb)
1571 lpfc_sli_update_ring(phba, pring);
1572 else
1573 lpfc_sli_update_full_ring(phba, pring);
1574 }
1575
1576 return;
1577}
1578
James Smarte59058c2008-08-24 21:49:00 -04001579/**
James Smart3621a712009-04-06 18:47:14 -04001580 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001581 * @phba: Pointer to HBA context object.
1582 * @hbqno: HBQ number.
1583 *
1584 * This function is called with hbalock held to get the next
1585 * available slot for the given HBQ. If there is free slot
1586 * available for the HBQ it will return pointer to the next available
1587 * HBQ entry else it will return NULL.
1588 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001589static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001590lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1591{
1592 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1593
1594 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1595 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1596 hbqp->next_hbqPutIdx = 0;
1597
1598 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001599 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001600 uint32_t getidx = le32_to_cpu(raw_index);
1601
1602 hbqp->local_hbqGetIdx = getidx;
1603
1604 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1605 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001606 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001607 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001608 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001609 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001610 hbqp->entry_count);
1611
1612 phba->link_state = LPFC_HBA_ERROR;
1613 return NULL;
1614 }
1615
1616 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1617 return NULL;
1618 }
1619
James Smart51ef4c22007-08-02 11:10:31 -04001620 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1621 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001622}
1623
James Smarte59058c2008-08-24 21:49:00 -04001624/**
James Smart3621a712009-04-06 18:47:14 -04001625 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001626 * @phba: Pointer to HBA context object.
1627 *
1628 * This function is called with no lock held to free all the
1629 * hbq buffers while uninitializing the SLI interface. It also
1630 * frees the HBQ buffers returned by the firmware but not yet
1631 * processed by the upper layers.
1632 **/
James Smarted957682007-06-17 19:56:37 -05001633void
1634lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1635{
James Smart92d7f7b2007-06-17 19:56:38 -05001636 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1637 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001638 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001639 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001640 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001641
James Smart51ef4c22007-08-02 11:10:31 -04001642 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001643 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001644 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001645 for (i = 0; i < hbq_count; ++i) {
1646 list_for_each_entry_safe(dmabuf, next_dmabuf,
1647 &phba->hbqs[i].hbq_buffer_list, list) {
1648 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1649 list_del(&hbq_buf->dbuf.list);
1650 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1651 }
James Smarta8adb832007-10-27 13:37:53 -04001652 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001653 }
James Smart3163f722008-02-08 18:50:25 -05001654 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001655 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1656 list) {
James Smart3163f722008-02-08 18:50:25 -05001657 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1658 list_del(&hbq_buf->dbuf.list);
1659 if (hbq_buf->tag == -1) {
1660 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1661 (phba, hbq_buf);
1662 } else {
1663 hbqno = hbq_buf->tag >> 16;
1664 if (hbqno >= LPFC_MAX_HBQS)
1665 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1666 (phba, hbq_buf);
1667 else
1668 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1669 hbq_buf);
1670 }
1671 }
1672
1673 /* Mark the HBQs not in use */
1674 phba->hbq_in_use = 0;
1675 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001676}
1677
James Smarte59058c2008-08-24 21:49:00 -04001678/**
James Smart3621a712009-04-06 18:47:14 -04001679 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001680 * @phba: Pointer to HBA context object.
1681 * @hbqno: HBQ number.
1682 * @hbq_buf: Pointer to HBQ buffer.
1683 *
1684 * This function is called with the hbalock held to post a
1685 * hbq buffer to the firmware. If the function finds an empty
1686 * slot in the HBQ, it will post the buffer. The function will return
1687 * pointer to the hbq entry if it successfully post the buffer
1688 * else it will return NULL.
1689 **/
James Smart3772a992009-05-22 14:50:54 -04001690static int
James Smarted957682007-06-17 19:56:37 -05001691lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001692 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001693{
James Smart3772a992009-05-22 14:50:54 -04001694 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1695}
1696
1697/**
1698 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1699 * @phba: Pointer to HBA context object.
1700 * @hbqno: HBQ number.
1701 * @hbq_buf: Pointer to HBQ buffer.
1702 *
1703 * This function is called with the hbalock held to post a hbq buffer to the
1704 * firmware. If the function finds an empty slot in the HBQ, it will post the
1705 * buffer and place it on the hbq_buffer_list. The function will return zero if
1706 * it successfully post the buffer else it will return an error.
1707 **/
1708static int
1709lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1710 struct hbq_dmabuf *hbq_buf)
1711{
James Smarted957682007-06-17 19:56:37 -05001712 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001713 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001714
1715 /* Get next HBQ entry slot to use */
1716 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1717 if (hbqe) {
1718 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1719
James Smart92d7f7b2007-06-17 19:56:38 -05001720 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1721 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001722 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001723 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001724 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1725 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1726 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001727 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1728 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001729 /* flush */
James Smarted957682007-06-17 19:56:37 -05001730 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001731 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001732 return 0;
1733 } else
1734 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001735}
1736
James Smart4f774512009-05-22 14:52:35 -04001737/**
1738 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1739 * @phba: Pointer to HBA context object.
1740 * @hbqno: HBQ number.
1741 * @hbq_buf: Pointer to HBQ buffer.
1742 *
1743 * This function is called with the hbalock held to post an RQE to the SLI4
1744 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1745 * the hbq_buffer_list and return zero, otherwise it will return an error.
1746 **/
1747static int
1748lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1749 struct hbq_dmabuf *hbq_buf)
1750{
1751 int rc;
1752 struct lpfc_rqe hrqe;
1753 struct lpfc_rqe drqe;
1754
1755 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1756 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1757 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1758 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1759 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1760 &hrqe, &drqe);
1761 if (rc < 0)
1762 return rc;
1763 hbq_buf->tag = rc;
1764 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1765 return 0;
1766}
1767
James Smarte59058c2008-08-24 21:49:00 -04001768/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001769static struct lpfc_hbq_init lpfc_els_hbq = {
1770 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001771 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001772 .mask_count = 0,
1773 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001774 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001775 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001776 .init_count = 40,
1777 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001778};
James Smarted957682007-06-17 19:56:37 -05001779
James Smarte59058c2008-08-24 21:49:00 -04001780/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001781static struct lpfc_hbq_init lpfc_extra_hbq = {
1782 .rn = 1,
1783 .entry_count = 200,
1784 .mask_count = 0,
1785 .profile = 0,
1786 .ring_mask = (1 << LPFC_EXTRA_RING),
1787 .buffer_count = 0,
1788 .init_count = 0,
1789 .add_count = 5,
1790};
1791
James Smarte59058c2008-08-24 21:49:00 -04001792/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001793struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001794 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001795 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001796};
1797
James Smarte59058c2008-08-24 21:49:00 -04001798/**
James Smart3621a712009-04-06 18:47:14 -04001799 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001800 * @phba: Pointer to HBA context object.
1801 * @hbqno: HBQ number.
1802 * @count: Number of HBQ buffers to be posted.
1803 *
James Smartd7c255b2008-08-24 21:50:00 -04001804 * This function is called with no lock held to post more hbq buffers to the
1805 * given HBQ. The function returns the number of HBQ buffers successfully
1806 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001807 **/
James Smart311464e2007-08-02 11:10:37 -04001808static int
James Smart92d7f7b2007-06-17 19:56:38 -05001809lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1810{
James Smartd7c255b2008-08-24 21:50:00 -04001811 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001812 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001813 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001814 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001815 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001816 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001817
James Smartd7c255b2008-08-24 21:50:00 -04001818 if ((phba->hbqs[hbqno].buffer_count + count) >
1819 lpfc_hbq_defs[hbqno]->entry_count)
1820 count = lpfc_hbq_defs[hbqno]->entry_count -
1821 phba->hbqs[hbqno].buffer_count;
1822 if (!count)
1823 return 0;
1824 /* Allocate HBQ entries */
1825 for (i = 0; i < count; i++) {
1826 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1827 if (!hbq_buffer)
1828 break;
1829 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1830 }
James Smart3163f722008-02-08 18:50:25 -05001831 /* Check whether HBQ is still in use */
1832 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001833 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001834 goto err;
1835 while (!list_empty(&hbq_buf_list)) {
1836 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1837 dbuf.list);
1838 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1839 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001840 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001841 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001842 posted++;
1843 } else
James Smart51ef4c22007-08-02 11:10:31 -04001844 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001845 }
James Smart3163f722008-02-08 18:50:25 -05001846 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001847 return posted;
1848err:
1849 spin_unlock_irqrestore(&phba->hbalock, flags);
1850 while (!list_empty(&hbq_buf_list)) {
1851 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1852 dbuf.list);
1853 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1854 }
James Smart92d7f7b2007-06-17 19:56:38 -05001855 return 0;
James Smarted957682007-06-17 19:56:37 -05001856}
1857
James Smarte59058c2008-08-24 21:49:00 -04001858/**
James Smart3621a712009-04-06 18:47:14 -04001859 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001860 * @phba: Pointer to HBA context object.
1861 * @qno: HBQ number.
1862 *
1863 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001864 * is called with no lock held. The function returns the number of HBQ entries
1865 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001866 **/
James Smarted957682007-06-17 19:56:37 -05001867int
James Smart92d7f7b2007-06-17 19:56:38 -05001868lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001869{
James Smartdef9c7a2009-12-21 17:02:28 -05001870 if (phba->sli_rev == LPFC_SLI_REV4)
1871 return 0;
1872 else
1873 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1874 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001875}
1876
James Smarte59058c2008-08-24 21:49:00 -04001877/**
James Smart3621a712009-04-06 18:47:14 -04001878 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001879 * @phba: Pointer to HBA context object.
1880 * @qno: HBQ queue number.
1881 *
1882 * This function is called from SLI initialization code path with
1883 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001884 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001885 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001886static int
James Smart92d7f7b2007-06-17 19:56:38 -05001887lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001888{
James Smartdef9c7a2009-12-21 17:02:28 -05001889 if (phba->sli_rev == LPFC_SLI_REV4)
1890 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1891 lpfc_hbq_defs[qno]->entry_count);
1892 else
1893 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1894 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001895}
1896
James Smarte59058c2008-08-24 21:49:00 -04001897/**
James Smart3772a992009-05-22 14:50:54 -04001898 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1899 * @phba: Pointer to HBA context object.
1900 * @hbqno: HBQ number.
1901 *
1902 * This function removes the first hbq buffer on an hbq list and returns a
1903 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1904 **/
1905static struct hbq_dmabuf *
1906lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1907{
1908 struct lpfc_dmabuf *d_buf;
1909
1910 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1911 if (!d_buf)
1912 return NULL;
1913 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1914}
1915
1916/**
James Smart3621a712009-04-06 18:47:14 -04001917 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001918 * @phba: Pointer to HBA context object.
1919 * @tag: Tag of the hbq buffer.
1920 *
1921 * This function is called with hbalock held. This function searches
1922 * for the hbq buffer associated with the given tag in the hbq buffer
1923 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1924 * it returns NULL.
1925 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001926static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001927lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1928{
James Smart92d7f7b2007-06-17 19:56:38 -05001929 struct lpfc_dmabuf *d_buf;
1930 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001931 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001932
James Smart51ef4c22007-08-02 11:10:31 -04001933 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001934 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001935 return NULL;
1936
James Smart3772a992009-05-22 14:50:54 -04001937 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001938 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001939 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001940 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001941 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001942 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001943 }
1944 }
James Smart3772a992009-05-22 14:50:54 -04001945 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001947 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001948 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001949 return NULL;
James Smarted957682007-06-17 19:56:37 -05001950}
1951
James Smarte59058c2008-08-24 21:49:00 -04001952/**
James Smart3621a712009-04-06 18:47:14 -04001953 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001954 * @phba: Pointer to HBA context object.
1955 * @hbq_buffer: Pointer to HBQ buffer.
1956 *
1957 * This function is called with hbalock. This function gives back
1958 * the hbq buffer to firmware. If the HBQ does not have space to
1959 * post the buffer, it will free the buffer.
1960 **/
James Smarted957682007-06-17 19:56:37 -05001961void
James Smart51ef4c22007-08-02 11:10:31 -04001962lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001963{
1964 uint32_t hbqno;
1965
James Smart51ef4c22007-08-02 11:10:31 -04001966 if (hbq_buffer) {
1967 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001968 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001969 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001970 }
1971}
1972
James Smarte59058c2008-08-24 21:49:00 -04001973/**
James Smart3621a712009-04-06 18:47:14 -04001974 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001975 * @mbxCommand: mailbox command code.
1976 *
1977 * This function is called by the mailbox event handler function to verify
1978 * that the completed mailbox command is a legitimate mailbox command. If the
1979 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1980 * and the mailbox event handler will take the HBA offline.
1981 **/
dea31012005-04-17 16:05:31 -05001982static int
1983lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1984{
1985 uint8_t ret;
1986
1987 switch (mbxCommand) {
1988 case MBX_LOAD_SM:
1989 case MBX_READ_NV:
1990 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001991 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001992 case MBX_RUN_BIU_DIAG:
1993 case MBX_INIT_LINK:
1994 case MBX_DOWN_LINK:
1995 case MBX_CONFIG_LINK:
1996 case MBX_CONFIG_RING:
1997 case MBX_RESET_RING:
1998 case MBX_READ_CONFIG:
1999 case MBX_READ_RCONFIG:
2000 case MBX_READ_SPARM:
2001 case MBX_READ_STATUS:
2002 case MBX_READ_RPI:
2003 case MBX_READ_XRI:
2004 case MBX_READ_REV:
2005 case MBX_READ_LNK_STAT:
2006 case MBX_REG_LOGIN:
2007 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002008 case MBX_CLEAR_LA:
2009 case MBX_DUMP_MEMORY:
2010 case MBX_DUMP_CONTEXT:
2011 case MBX_RUN_DIAGS:
2012 case MBX_RESTART:
2013 case MBX_UPDATE_CFG:
2014 case MBX_DOWN_LOAD:
2015 case MBX_DEL_LD_ENTRY:
2016 case MBX_RUN_PROGRAM:
2017 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002018 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002019 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002020 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002021 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002022 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002023 case MBX_LOAD_AREA:
2024 case MBX_RUN_BIU_DIAG64:
2025 case MBX_CONFIG_PORT:
2026 case MBX_READ_SPARM64:
2027 case MBX_READ_RPI64:
2028 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002029 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002030 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002031 case MBX_SET_DEBUG:
2032 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002033 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002034 case MBX_REG_VPI:
2035 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002036 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002037 case MBX_PORT_CAPABILITIES:
2038 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002039 case MBX_SLI4_CONFIG:
2040 case MBX_SLI4_REQ_FTRS:
2041 case MBX_REG_FCFI:
2042 case MBX_UNREG_FCFI:
2043 case MBX_REG_VFI:
2044 case MBX_UNREG_VFI:
2045 case MBX_INIT_VPI:
2046 case MBX_INIT_VFI:
2047 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002048 case MBX_READ_EVENT_LOG_STATUS:
2049 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002050 case MBX_SECURITY_MGMT:
2051 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002052 ret = mbxCommand;
2053 break;
2054 default:
2055 ret = MBX_SHUTDOWN;
2056 break;
2057 }
James Smart2e0fef82007-06-17 19:56:36 -05002058 return ret;
dea31012005-04-17 16:05:31 -05002059}
James Smarte59058c2008-08-24 21:49:00 -04002060
2061/**
James Smart3621a712009-04-06 18:47:14 -04002062 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002063 * @phba: Pointer to HBA context object.
2064 * @pmboxq: Pointer to mailbox command.
2065 *
2066 * This is completion handler function for mailbox commands issued from
2067 * lpfc_sli_issue_mbox_wait function. This function is called by the
2068 * mailbox event handler function with no lock held. This function
2069 * will wake up thread waiting on the wait queue pointed by context1
2070 * of the mailbox.
2071 **/
James Smart04c68492009-05-22 14:52:52 -04002072void
James Smart2e0fef82007-06-17 19:56:36 -05002073lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002074{
2075 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002076 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002077
2078 /*
2079 * If pdone_q is empty, the driver thread gave up waiting and
2080 * continued running.
2081 */
James Smart7054a602007-04-25 09:52:34 -04002082 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002083 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002084 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2085 if (pdone_q)
2086 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002087 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002088 return;
2089}
2090
James Smarte59058c2008-08-24 21:49:00 -04002091
2092/**
James Smart3621a712009-04-06 18:47:14 -04002093 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002094 * @phba: Pointer to HBA context object.
2095 * @pmb: Pointer to mailbox object.
2096 *
2097 * This function is the default mailbox completion handler. It
2098 * frees the memory resources associated with the completed mailbox
2099 * command. If the completed command is a REG_LOGIN mailbox command,
2100 * this function will issue a UREG_LOGIN to re-claim the RPI.
2101 **/
dea31012005-04-17 16:05:31 -05002102void
James Smart2e0fef82007-06-17 19:56:36 -05002103lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002104{
James Smartd439d282010-09-29 11:18:45 -04002105 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002106 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002107 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002108 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002109 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002110 int rc;
2111
dea31012005-04-17 16:05:31 -05002112 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002113
dea31012005-04-17 16:05:31 -05002114 if (mp) {
2115 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2116 kfree(mp);
2117 }
James Smart7054a602007-04-25 09:52:34 -04002118
James Smart04c68492009-05-22 14:52:52 -04002119 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
James Smart5af5eee2010-10-22 11:06:38 -04002120 (phba->sli_rev == LPFC_SLI_REV4) &&
2121 (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
James Smart04c68492009-05-22 14:52:52 -04002122 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
2123
James Smart7054a602007-04-25 09:52:34 -04002124 /*
2125 * If a REG_LOGIN succeeded after node is destroyed or node
2126 * is in re-discovery driver need to cleanup the RPI.
2127 */
James Smart2e0fef82007-06-17 19:56:36 -05002128 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002129 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2130 !pmb->u.mb.mbxStatus) {
2131 rpi = pmb->u.mb.un.varWords[0];
2132 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2133 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002134 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002135 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2136 if (rc != MBX_NOT_FINISHED)
2137 return;
2138 }
2139
James Smart695a8142010-01-26 23:08:03 -05002140 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2141 !(phba->pport->load_flag & FC_UNLOADING) &&
2142 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002143 shost = lpfc_shost_from_vport(vport);
2144 spin_lock_irq(shost->host_lock);
2145 vport->vpi_state |= LPFC_VPI_REGISTERED;
2146 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2147 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002148 }
2149
James Smartd439d282010-09-29 11:18:45 -04002150 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2151 ndlp = (struct lpfc_nodelist *)pmb->context2;
2152 lpfc_nlp_put(ndlp);
2153 pmb->context2 = NULL;
2154 }
2155
James Smartdcf2a4e2010-09-29 11:18:53 -04002156 /* Check security permission status on INIT_LINK mailbox command */
2157 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2158 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2159 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2160 "2860 SLI authentication is required "
2161 "for INIT_LINK but has not done yet\n");
2162
James Smart04c68492009-05-22 14:52:52 -04002163 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2164 lpfc_sli4_mbox_cmd_free(phba, pmb);
2165 else
2166 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002167}
2168
James Smarte59058c2008-08-24 21:49:00 -04002169/**
James Smart3621a712009-04-06 18:47:14 -04002170 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002171 * @phba: Pointer to HBA context object.
2172 *
2173 * This function is called with no lock held. This function processes all
2174 * the completed mailbox commands and gives it to upper layers. The interrupt
2175 * service routine processes mailbox completion interrupt and adds completed
2176 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2177 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2178 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2179 * function returns the mailbox commands to the upper layer by calling the
2180 * completion handler function of each mailbox.
2181 **/
dea31012005-04-17 16:05:31 -05002182int
James Smart2e0fef82007-06-17 19:56:36 -05002183lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002184{
James Smart92d7f7b2007-06-17 19:56:38 -05002185 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002186 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002187 int rc;
2188 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002189
2190 phba->sli.slistat.mbox_event++;
2191
James Smart92d7f7b2007-06-17 19:56:38 -05002192 /* Get all completed mailboxe buffers into the cmplq */
2193 spin_lock_irq(&phba->hbalock);
2194 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2195 spin_unlock_irq(&phba->hbalock);
2196
dea31012005-04-17 16:05:31 -05002197 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002198 do {
2199 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2200 if (pmb == NULL)
2201 break;
2202
James Smart04c68492009-05-22 14:52:52 -04002203 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002204
James Smart858c9f62007-06-17 19:56:39 -05002205 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2206 if (pmb->vport) {
2207 lpfc_debugfs_disc_trc(pmb->vport,
2208 LPFC_DISC_TRC_MBOX_VPORT,
2209 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2210 (uint32_t)pmbox->mbxCommand,
2211 pmbox->un.varWords[0],
2212 pmbox->un.varWords[1]);
2213 }
2214 else {
2215 lpfc_debugfs_disc_trc(phba->pport,
2216 LPFC_DISC_TRC_MBOX,
2217 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2218 (uint32_t)pmbox->mbxCommand,
2219 pmbox->un.varWords[0],
2220 pmbox->un.varWords[1]);
2221 }
2222 }
2223
dea31012005-04-17 16:05:31 -05002224 /*
2225 * It is a fatal error if unknown mbox command completion.
2226 */
2227 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2228 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002229 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002230 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002231 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04002232 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002233 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002234 pmbox->mbxCommand,
2235 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002236 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002237 phba->work_hs = HS_FFER3;
2238 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002239 continue;
dea31012005-04-17 16:05:31 -05002240 }
2241
dea31012005-04-17 16:05:31 -05002242 if (pmbox->mbxStatus) {
2243 phba->sli.slistat.mbox_stat_err++;
2244 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2245 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002246 lpfc_printf_log(phba, KERN_INFO,
2247 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002248 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002249 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002250 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002251 pmb->vport ? pmb->vport->vpi :0,
2252 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002253 lpfc_sli4_mbox_opcode_get(phba,
2254 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002255 pmbox->mbxStatus,
2256 pmbox->un.varWords[0],
2257 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002258 pmbox->mbxStatus = 0;
2259 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002261 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002262 continue;
dea31012005-04-17 16:05:31 -05002263 }
2264 }
2265
2266 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002267 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002268 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002269 "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 -05002270 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002271 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002272 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002273 pmb->mbox_cmpl,
2274 *((uint32_t *) pmbox),
2275 pmbox->un.varWords[0],
2276 pmbox->un.varWords[1],
2277 pmbox->un.varWords[2],
2278 pmbox->un.varWords[3],
2279 pmbox->un.varWords[4],
2280 pmbox->un.varWords[5],
2281 pmbox->un.varWords[6],
2282 pmbox->un.varWords[7]);
2283
James Smart92d7f7b2007-06-17 19:56:38 -05002284 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002285 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002286 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002287 return 0;
dea31012005-04-17 16:05:31 -05002288}
James Smart92d7f7b2007-06-17 19:56:38 -05002289
James Smarte59058c2008-08-24 21:49:00 -04002290/**
James Smart3621a712009-04-06 18:47:14 -04002291 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002292 * @phba: Pointer to HBA context object.
2293 * @pring: Pointer to driver SLI ring object.
2294 * @tag: buffer tag.
2295 *
2296 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2297 * is set in the tag the buffer is posted for a particular exchange,
2298 * the function will return the buffer without replacing the buffer.
2299 * If the buffer is for unsolicited ELS or CT traffic, this function
2300 * returns the buffer and also posts another buffer to the firmware.
2301 **/
James Smart76bb24e2007-10-27 13:38:00 -04002302static struct lpfc_dmabuf *
2303lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002304 struct lpfc_sli_ring *pring,
2305 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002306{
James Smart9f1e1b52008-12-04 22:39:40 -05002307 struct hbq_dmabuf *hbq_entry;
2308
James Smart76bb24e2007-10-27 13:38:00 -04002309 if (tag & QUE_BUFTAG_BIT)
2310 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002311 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2312 if (!hbq_entry)
2313 return NULL;
2314 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002315}
James Smart57127f12007-10-27 13:37:05 -04002316
James Smart3772a992009-05-22 14:50:54 -04002317/**
2318 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2319 * @phba: Pointer to HBA context object.
2320 * @pring: Pointer to driver SLI ring object.
2321 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2322 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2323 * @fch_type: the type for the first frame of the sequence.
2324 *
2325 * This function is called with no lock held. This function uses the r_ctl and
2326 * type of the received sequence to find the correct callback function to call
2327 * to process the sequence.
2328 **/
2329static int
2330lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2331 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2332 uint32_t fch_type)
2333{
2334 int i;
2335
2336 /* unSolicited Responses */
2337 if (pring->prt[0].profile) {
2338 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2339 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2340 saveq);
2341 return 1;
2342 }
2343 /* We must search, based on rctl / type
2344 for the right routine */
2345 for (i = 0; i < pring->num_mask; i++) {
2346 if ((pring->prt[i].rctl == fch_r_ctl) &&
2347 (pring->prt[i].type == fch_type)) {
2348 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2349 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2350 (phba, pring, saveq);
2351 return 1;
2352 }
2353 }
2354 return 0;
2355}
James Smarte59058c2008-08-24 21:49:00 -04002356
2357/**
James Smart3621a712009-04-06 18:47:14 -04002358 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002359 * @phba: Pointer to HBA context object.
2360 * @pring: Pointer to driver SLI ring object.
2361 * @saveq: Pointer to the unsolicited iocb.
2362 *
2363 * This function is called with no lock held by the ring event handler
2364 * when there is an unsolicited iocb posted to the response ring by the
2365 * firmware. This function gets the buffer associated with the iocbs
2366 * and calls the event handler for the ring. This function handles both
2367 * qring buffers and hbq buffers.
2368 * When the function returns 1 the caller can free the iocb object otherwise
2369 * upper layer functions will free the iocb objects.
2370 **/
dea31012005-04-17 16:05:31 -05002371static int
2372lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2373 struct lpfc_iocbq *saveq)
2374{
2375 IOCB_t * irsp;
2376 WORD5 * w5p;
2377 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002378 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002379 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002380 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002381
2382 match = 0;
2383 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002384
2385 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2386 if (pring->lpfc_sli_rcv_async_status)
2387 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2388 else
2389 lpfc_printf_log(phba,
2390 KERN_WARNING,
2391 LOG_SLI,
2392 "0316 Ring %d handler: unexpected "
2393 "ASYNC_STATUS iocb received evt_code "
2394 "0x%x\n",
2395 pring->ringno,
2396 irsp->un.asyncstat.evt_code);
2397 return 1;
2398 }
2399
James Smart3163f722008-02-08 18:50:25 -05002400 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2401 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2402 if (irsp->ulpBdeCount > 0) {
2403 dmzbuf = lpfc_sli_get_buff(phba, pring,
2404 irsp->un.ulpWord[3]);
2405 lpfc_in_buf_free(phba, dmzbuf);
2406 }
2407
2408 if (irsp->ulpBdeCount > 1) {
2409 dmzbuf = lpfc_sli_get_buff(phba, pring,
2410 irsp->unsli3.sli3Words[3]);
2411 lpfc_in_buf_free(phba, dmzbuf);
2412 }
2413
2414 if (irsp->ulpBdeCount > 2) {
2415 dmzbuf = lpfc_sli_get_buff(phba, pring,
2416 irsp->unsli3.sli3Words[7]);
2417 lpfc_in_buf_free(phba, dmzbuf);
2418 }
2419
2420 return 1;
2421 }
2422
James Smart92d7f7b2007-06-17 19:56:38 -05002423 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002424 if (irsp->ulpBdeCount != 0) {
2425 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002426 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002427 if (!saveq->context2)
2428 lpfc_printf_log(phba,
2429 KERN_ERR,
2430 LOG_SLI,
2431 "0341 Ring %d Cannot find buffer for "
2432 "an unsolicited iocb. tag 0x%x\n",
2433 pring->ringno,
2434 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002435 }
2436 if (irsp->ulpBdeCount == 2) {
2437 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002438 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002439 if (!saveq->context3)
2440 lpfc_printf_log(phba,
2441 KERN_ERR,
2442 LOG_SLI,
2443 "0342 Ring %d Cannot find buffer for an"
2444 " unsolicited iocb. tag 0x%x\n",
2445 pring->ringno,
2446 irsp->unsli3.sli3Words[7]);
2447 }
2448 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002449 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002450 if (irsp->ulpBdeCount != 0) {
2451 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2452 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002453 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002454 lpfc_printf_log(phba,
2455 KERN_ERR,
2456 LOG_SLI,
2457 "0343 Ring %d Cannot find "
2458 "buffer for an unsolicited iocb"
2459 ". tag 0x%x\n", pring->ringno,
2460 irsp->un.ulpWord[3]);
2461 }
2462 if (irsp->ulpBdeCount == 2) {
2463 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2464 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002465 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002466 lpfc_printf_log(phba,
2467 KERN_ERR,
2468 LOG_SLI,
2469 "0344 Ring %d Cannot find "
2470 "buffer for an unsolicited "
2471 "iocb. tag 0x%x\n",
2472 pring->ringno,
2473 irsp->unsli3.sli3Words[7]);
2474 }
2475 }
James Smart92d7f7b2007-06-17 19:56:38 -05002476 }
James Smart9c2face2008-01-11 01:53:18 -05002477 if (irsp->ulpBdeCount != 0 &&
2478 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2479 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2480 int found = 0;
2481
2482 /* search continue save q for same XRI */
2483 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2484 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2485 list_add_tail(&saveq->list, &iocbq->list);
2486 found = 1;
2487 break;
2488 }
2489 }
2490 if (!found)
2491 list_add_tail(&saveq->clist,
2492 &pring->iocb_continue_saveq);
2493 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2494 list_del_init(&iocbq->clist);
2495 saveq = iocbq;
2496 irsp = &(saveq->iocb);
2497 } else
2498 return 0;
2499 }
2500 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2501 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2502 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002503 Rctl = FC_RCTL_ELS_REQ;
2504 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002505 } else {
2506 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2507 Rctl = w5p->hcsw.Rctl;
2508 Type = w5p->hcsw.Type;
2509
2510 /* Firmware Workaround */
2511 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2512 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2513 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002514 Rctl = FC_RCTL_ELS_REQ;
2515 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002516 w5p->hcsw.Rctl = Rctl;
2517 w5p->hcsw.Type = Type;
2518 }
2519 }
James Smart92d7f7b2007-06-17 19:56:38 -05002520
James Smart3772a992009-05-22 14:50:54 -04002521 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002522 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002523 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002524 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002525 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002526
James Smart92d7f7b2007-06-17 19:56:38 -05002527 return 1;
dea31012005-04-17 16:05:31 -05002528}
2529
James Smarte59058c2008-08-24 21:49:00 -04002530/**
James Smart3621a712009-04-06 18:47:14 -04002531 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002532 * @phba: Pointer to HBA context object.
2533 * @pring: Pointer to driver SLI ring object.
2534 * @prspiocb: Pointer to response iocb object.
2535 *
2536 * This function looks up the iocb_lookup table to get the command iocb
2537 * corresponding to the given response iocb using the iotag of the
2538 * response iocb. This function is called with the hbalock held.
2539 * This function returns the command iocb object if it finds the command
2540 * iocb else returns NULL.
2541 **/
dea31012005-04-17 16:05:31 -05002542static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002543lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2544 struct lpfc_sli_ring *pring,
2545 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002546{
dea31012005-04-17 16:05:31 -05002547 struct lpfc_iocbq *cmd_iocb = NULL;
2548 uint16_t iotag;
2549
James Bottomley604a3e32005-10-29 10:28:33 -05002550 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002551
James Bottomley604a3e32005-10-29 10:28:33 -05002552 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2553 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002554 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002555 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2556 pring->txcmplq_cnt--;
2557 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2558 }
James Bottomley604a3e32005-10-29 10:28:33 -05002559 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002560 }
2561
dea31012005-04-17 16:05:31 -05002562 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002563 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002564 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002565 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002566 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002567 return NULL;
2568}
2569
James Smarte59058c2008-08-24 21:49:00 -04002570/**
James Smart3772a992009-05-22 14:50:54 -04002571 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2572 * @phba: Pointer to HBA context object.
2573 * @pring: Pointer to driver SLI ring object.
2574 * @iotag: IOCB tag.
2575 *
2576 * This function looks up the iocb_lookup table to get the command iocb
2577 * corresponding to the given iotag. This function is called with the
2578 * hbalock held.
2579 * This function returns the command iocb object if it finds the command
2580 * iocb else returns NULL.
2581 **/
2582static struct lpfc_iocbq *
2583lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2584 struct lpfc_sli_ring *pring, uint16_t iotag)
2585{
2586 struct lpfc_iocbq *cmd_iocb;
2587
2588 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2589 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2590 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002591 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2592 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2593 pring->txcmplq_cnt--;
2594 }
James Smart3772a992009-05-22 14:50:54 -04002595 return cmd_iocb;
2596 }
2597
2598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2599 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2600 iotag, phba->sli.last_iotag);
2601 return NULL;
2602}
2603
2604/**
James Smart3621a712009-04-06 18:47:14 -04002605 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002606 * @phba: Pointer to HBA context object.
2607 * @pring: Pointer to driver SLI ring object.
2608 * @saveq: Pointer to the response iocb to be processed.
2609 *
2610 * This function is called by the ring event handler for non-fcp
2611 * rings when there is a new response iocb in the response ring.
2612 * The caller is not required to hold any locks. This function
2613 * gets the command iocb associated with the response iocb and
2614 * calls the completion handler for the command iocb. If there
2615 * is no completion handler, the function will free the resources
2616 * associated with command iocb. If the response iocb is for
2617 * an already aborted command iocb, the status of the completion
2618 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2619 * This function always returns 1.
2620 **/
dea31012005-04-17 16:05:31 -05002621static int
James Smart2e0fef82007-06-17 19:56:36 -05002622lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002623 struct lpfc_iocbq *saveq)
2624{
James Smart2e0fef82007-06-17 19:56:36 -05002625 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002626 int rc = 1;
2627 unsigned long iflag;
2628
2629 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002630 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002631 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002632 spin_unlock_irqrestore(&phba->hbalock, iflag);
2633
dea31012005-04-17 16:05:31 -05002634 if (cmdiocbp) {
2635 if (cmdiocbp->iocb_cmpl) {
2636 /*
James Smartea2151b2008-09-07 11:52:10 -04002637 * If an ELS command failed send an event to mgmt
2638 * application.
2639 */
2640 if (saveq->iocb.ulpStatus &&
2641 (pring->ringno == LPFC_ELS_RING) &&
2642 (cmdiocbp->iocb.ulpCommand ==
2643 CMD_ELS_REQUEST64_CR))
2644 lpfc_send_els_failure_event(phba,
2645 cmdiocbp, saveq);
2646
2647 /*
dea31012005-04-17 16:05:31 -05002648 * Post all ELS completions to the worker thread.
2649 * All other are passed to the completion callback.
2650 */
2651 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002652 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2653 (cmdiocbp->iocb_flag &
2654 LPFC_DRIVER_ABORTED)) {
2655 spin_lock_irqsave(&phba->hbalock,
2656 iflag);
James Smart07951072007-04-25 09:51:38 -04002657 cmdiocbp->iocb_flag &=
2658 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002659 spin_unlock_irqrestore(&phba->hbalock,
2660 iflag);
James Smart07951072007-04-25 09:51:38 -04002661 saveq->iocb.ulpStatus =
2662 IOSTAT_LOCAL_REJECT;
2663 saveq->iocb.un.ulpWord[4] =
2664 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002665
2666 /* Firmware could still be in progress
2667 * of DMAing payload, so don't free data
2668 * buffer till after a hbeat.
2669 */
James Smart341af102010-01-26 23:07:37 -05002670 spin_lock_irqsave(&phba->hbalock,
2671 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002672 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002673 spin_unlock_irqrestore(&phba->hbalock,
2674 iflag);
2675 }
James Smart0f65ff62010-02-26 14:14:23 -05002676 if (phba->sli_rev == LPFC_SLI_REV4) {
2677 if (saveq->iocb_flag &
2678 LPFC_EXCHANGE_BUSY) {
2679 /* Set cmdiocb flag for the
2680 * exchange busy so sgl (xri)
2681 * will not be released until
2682 * the abort xri is received
2683 * from hba.
2684 */
2685 spin_lock_irqsave(
2686 &phba->hbalock, iflag);
2687 cmdiocbp->iocb_flag |=
2688 LPFC_EXCHANGE_BUSY;
2689 spin_unlock_irqrestore(
2690 &phba->hbalock, iflag);
2691 }
2692 if (cmdiocbp->iocb_flag &
2693 LPFC_DRIVER_ABORTED) {
2694 /*
2695 * Clear LPFC_DRIVER_ABORTED
2696 * bit in case it was driver
2697 * initiated abort.
2698 */
2699 spin_lock_irqsave(
2700 &phba->hbalock, iflag);
2701 cmdiocbp->iocb_flag &=
2702 ~LPFC_DRIVER_ABORTED;
2703 spin_unlock_irqrestore(
2704 &phba->hbalock, iflag);
2705 cmdiocbp->iocb.ulpStatus =
2706 IOSTAT_LOCAL_REJECT;
2707 cmdiocbp->iocb.un.ulpWord[4] =
2708 IOERR_ABORT_REQUESTED;
2709 /*
2710 * For SLI4, irsiocb contains
2711 * NO_XRI in sli_xritag, it
2712 * shall not affect releasing
2713 * sgl (xri) process.
2714 */
2715 saveq->iocb.ulpStatus =
2716 IOSTAT_LOCAL_REJECT;
2717 saveq->iocb.un.ulpWord[4] =
2718 IOERR_SLI_ABORTED;
2719 spin_lock_irqsave(
2720 &phba->hbalock, iflag);
2721 saveq->iocb_flag |=
2722 LPFC_DELAY_MEM_FREE;
2723 spin_unlock_irqrestore(
2724 &phba->hbalock, iflag);
2725 }
James Smart07951072007-04-25 09:51:38 -04002726 }
dea31012005-04-17 16:05:31 -05002727 }
James Smart2e0fef82007-06-17 19:56:36 -05002728 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002729 } else
2730 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002731 } else {
2732 /*
2733 * Unknown initiating command based on the response iotag.
2734 * This could be the case on the ELS ring because of
2735 * lpfc_els_abort().
2736 */
2737 if (pring->ringno != LPFC_ELS_RING) {
2738 /*
2739 * Ring <ringno> handler: unexpected completion IoTag
2740 * <IoTag>
2741 */
James Smarta257bf92009-04-06 18:48:10 -04002742 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002743 "0322 Ring %d handler: "
2744 "unexpected completion IoTag x%x "
2745 "Data: x%x x%x x%x x%x\n",
2746 pring->ringno,
2747 saveq->iocb.ulpIoTag,
2748 saveq->iocb.ulpStatus,
2749 saveq->iocb.un.ulpWord[4],
2750 saveq->iocb.ulpCommand,
2751 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002752 }
2753 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002754
dea31012005-04-17 16:05:31 -05002755 return rc;
2756}
2757
James Smarte59058c2008-08-24 21:49:00 -04002758/**
James Smart3621a712009-04-06 18:47:14 -04002759 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002760 * @phba: Pointer to HBA context object.
2761 * @pring: Pointer to driver SLI ring object.
2762 *
2763 * This function is called from the iocb ring event handlers when
2764 * put pointer is ahead of the get pointer for a ring. This function signal
2765 * an error attention condition to the worker thread and the worker
2766 * thread will transition the HBA to offline state.
2767 **/
James Smart2e0fef82007-06-17 19:56:36 -05002768static void
2769lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002770{
James Smart34b02dc2008-08-24 21:49:55 -04002771 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002772 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002773 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002774 * rsp ring <portRspMax>
2775 */
2776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002777 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002778 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002779 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002780 pring->numRiocb);
2781
James Smart2e0fef82007-06-17 19:56:36 -05002782 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002783
2784 /*
2785 * All error attention handlers are posted to
2786 * worker thread
2787 */
2788 phba->work_ha |= HA_ERATT;
2789 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002790
James Smart5e9d9b82008-06-14 22:52:53 -04002791 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002792
2793 return;
2794}
2795
James Smarte59058c2008-08-24 21:49:00 -04002796/**
James Smart3621a712009-04-06 18:47:14 -04002797 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002798 * @ptr: Pointer to address of HBA context object.
2799 *
2800 * This function is invoked by the Error Attention polling timer when the
2801 * timer times out. It will check the SLI Error Attention register for
2802 * possible attention events. If so, it will post an Error Attention event
2803 * and wake up worker thread to process it. Otherwise, it will set up the
2804 * Error Attention polling timer for the next poll.
2805 **/
2806void lpfc_poll_eratt(unsigned long ptr)
2807{
2808 struct lpfc_hba *phba;
2809 uint32_t eratt = 0;
2810
2811 phba = (struct lpfc_hba *)ptr;
2812
2813 /* Check chip HA register for error event */
2814 eratt = lpfc_sli_check_eratt(phba);
2815
2816 if (eratt)
2817 /* Tell the worker thread there is work to do */
2818 lpfc_worker_wake_up(phba);
2819 else
2820 /* Restart the timer for next eratt poll */
2821 mod_timer(&phba->eratt_poll, jiffies +
2822 HZ * LPFC_ERATT_POLL_INTERVAL);
2823 return;
2824}
2825
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002826
James Smarte59058c2008-08-24 21:49:00 -04002827/**
James Smart3621a712009-04-06 18:47:14 -04002828 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002829 * @phba: Pointer to HBA context object.
2830 * @pring: Pointer to driver SLI ring object.
2831 * @mask: Host attention register mask for this ring.
2832 *
2833 * This function is called from the interrupt context when there is a ring
2834 * event for the fcp ring. The caller does not hold any lock.
2835 * The function processes each response iocb in the response ring until it
2836 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2837 * LE bit set. The function will call the completion handler of the command iocb
2838 * if the response iocb indicates a completion for a command iocb or it is
2839 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2840 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002841 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002842 * to check it explicitly.
2843 */
2844int
James Smart2e0fef82007-06-17 19:56:36 -05002845lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2846 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002847{
James Smart34b02dc2008-08-24 21:49:55 -04002848 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002849 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002850 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002851 struct lpfc_iocbq *cmdiocbq = NULL;
2852 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002853 uint32_t status;
2854 uint32_t portRspPut, portRspMax;
2855 int rc = 1;
2856 lpfc_iocb_type type;
2857 unsigned long iflag;
2858 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002859
James Smart2e0fef82007-06-17 19:56:36 -05002860 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002861 pring->stats.iocb_event++;
2862
dea31012005-04-17 16:05:31 -05002863 /*
2864 * The next available response entry should never exceed the maximum
2865 * entries. If it does, treat it as an adapter hardware error.
2866 */
2867 portRspMax = pring->numRiocb;
2868 portRspPut = le32_to_cpu(pgp->rspPutInx);
2869 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002870 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002871 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002872 return 1;
2873 }
James Smart45ed1192009-10-02 15:17:02 -04002874 if (phba->fcp_ring_in_use) {
2875 spin_unlock_irqrestore(&phba->hbalock, iflag);
2876 return 1;
2877 } else
2878 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002879
2880 rmb();
2881 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002882 /*
2883 * Fetch an entry off the ring and copy it into a local data
2884 * structure. The copy involves a byte-swap since the
2885 * network byte order and pci byte orders are different.
2886 */
James Smarted957682007-06-17 19:56:37 -05002887 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002888 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002889
2890 if (++pring->rspidx >= portRspMax)
2891 pring->rspidx = 0;
2892
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002893 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2894 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002895 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002896 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002897 irsp = &rspiocbq.iocb;
2898
dea31012005-04-17 16:05:31 -05002899 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2900 pring->stats.iocb_rsp++;
2901 rsp_cmpl++;
2902
2903 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002904 /*
2905 * If resource errors reported from HBA, reduce
2906 * queuedepths of the SCSI device.
2907 */
2908 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2909 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2910 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002911 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002912 spin_lock_irqsave(&phba->hbalock, iflag);
2913 }
2914
dea31012005-04-17 16:05:31 -05002915 /* Rsp ring <ringno> error: IOCB */
2916 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002917 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002918 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002919 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002920 irsp->un.ulpWord[0],
2921 irsp->un.ulpWord[1],
2922 irsp->un.ulpWord[2],
2923 irsp->un.ulpWord[3],
2924 irsp->un.ulpWord[4],
2925 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002926 *(uint32_t *)&irsp->un1,
2927 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002928 }
2929
2930 switch (type) {
2931 case LPFC_ABORT_IOCB:
2932 case LPFC_SOL_IOCB:
2933 /*
2934 * Idle exchange closed via ABTS from port. No iocb
2935 * resources need to be recovered.
2936 */
2937 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002938 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002939 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002940 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002941 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002942 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002943 break;
2944 }
2945
James Bottomley604a3e32005-10-29 10:28:33 -05002946 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2947 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002948 if (unlikely(!cmdiocbq))
2949 break;
2950 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2951 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2952 if (cmdiocbq->iocb_cmpl) {
2953 spin_unlock_irqrestore(&phba->hbalock, iflag);
2954 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2955 &rspiocbq);
2956 spin_lock_irqsave(&phba->hbalock, iflag);
2957 }
dea31012005-04-17 16:05:31 -05002958 break;
James Smarta4bc3372006-12-02 13:34:16 -05002959 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002960 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002961 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002962 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002963 break;
dea31012005-04-17 16:05:31 -05002964 default:
2965 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2966 char adaptermsg[LPFC_MAX_ADPTMSG];
2967 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2968 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2969 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002970 dev_warn(&((phba->pcidev)->dev),
2971 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002972 phba->brd_no, adaptermsg);
2973 } else {
2974 /* Unknown IOCB command */
2975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002976 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002977 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002978 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002979 irsp->ulpStatus,
2980 irsp->ulpIoTag,
2981 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002982 }
2983 break;
2984 }
2985
2986 /*
2987 * The response IOCB has been processed. Update the ring
2988 * pointer in SLIM. If the port response put pointer has not
2989 * been updated, sync the pgp->rspPutInx and fetch the new port
2990 * response put pointer.
2991 */
James Smarted957682007-06-17 19:56:37 -05002992 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002993
2994 if (pring->rspidx == portRspPut)
2995 portRspPut = le32_to_cpu(pgp->rspPutInx);
2996 }
2997
2998 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2999 pring->stats.iocb_rsp_full++;
3000 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3001 writel(status, phba->CAregaddr);
3002 readl(phba->CAregaddr);
3003 }
3004 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3005 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3006 pring->stats.iocb_cmd_empty++;
3007
3008 /* Force update of the local copy of cmdGetInx */
3009 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3010 lpfc_sli_resume_iocb(phba, pring);
3011
3012 if ((pring->lpfc_sli_cmd_available))
3013 (pring->lpfc_sli_cmd_available) (phba, pring);
3014
3015 }
3016
James Smart45ed1192009-10-02 15:17:02 -04003017 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003019 return rc;
3020}
3021
James Smarte59058c2008-08-24 21:49:00 -04003022/**
James Smart3772a992009-05-22 14:50:54 -04003023 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3024 * @phba: Pointer to HBA context object.
3025 * @pring: Pointer to driver SLI ring object.
3026 * @rspiocbp: Pointer to driver response IOCB object.
3027 *
3028 * This function is called from the worker thread when there is a slow-path
3029 * response IOCB to process. This function chains all the response iocbs until
3030 * seeing the iocb with the LE bit set. The function will call
3031 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3032 * completion of a command iocb. The function will call the
3033 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3034 * The function frees the resources or calls the completion handler if this
3035 * iocb is an abort completion. The function returns NULL when the response
3036 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3037 * this function shall chain the iocb on to the iocb_continueq and return the
3038 * response iocb passed in.
3039 **/
3040static struct lpfc_iocbq *
3041lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3042 struct lpfc_iocbq *rspiocbp)
3043{
3044 struct lpfc_iocbq *saveq;
3045 struct lpfc_iocbq *cmdiocbp;
3046 struct lpfc_iocbq *next_iocb;
3047 IOCB_t *irsp = NULL;
3048 uint32_t free_saveq;
3049 uint8_t iocb_cmd_type;
3050 lpfc_iocb_type type;
3051 unsigned long iflag;
3052 int rc;
3053
3054 spin_lock_irqsave(&phba->hbalock, iflag);
3055 /* First add the response iocb to the countinueq list */
3056 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3057 pring->iocb_continueq_cnt++;
3058
3059 /* Now, determine whetehr the list is completed for processing */
3060 irsp = &rspiocbp->iocb;
3061 if (irsp->ulpLe) {
3062 /*
3063 * By default, the driver expects to free all resources
3064 * associated with this iocb completion.
3065 */
3066 free_saveq = 1;
3067 saveq = list_get_first(&pring->iocb_continueq,
3068 struct lpfc_iocbq, list);
3069 irsp = &(saveq->iocb);
3070 list_del_init(&pring->iocb_continueq);
3071 pring->iocb_continueq_cnt = 0;
3072
3073 pring->stats.iocb_rsp++;
3074
3075 /*
3076 * If resource errors reported from HBA, reduce
3077 * queuedepths of the SCSI device.
3078 */
3079 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3080 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3081 spin_unlock_irqrestore(&phba->hbalock, iflag);
3082 phba->lpfc_rampdown_queue_depth(phba);
3083 spin_lock_irqsave(&phba->hbalock, iflag);
3084 }
3085
3086 if (irsp->ulpStatus) {
3087 /* Rsp ring <ringno> error: IOCB */
3088 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3089 "0328 Rsp Ring %d error: "
3090 "IOCB Data: "
3091 "x%x x%x x%x x%x "
3092 "x%x x%x x%x x%x "
3093 "x%x x%x x%x x%x "
3094 "x%x x%x x%x x%x\n",
3095 pring->ringno,
3096 irsp->un.ulpWord[0],
3097 irsp->un.ulpWord[1],
3098 irsp->un.ulpWord[2],
3099 irsp->un.ulpWord[3],
3100 irsp->un.ulpWord[4],
3101 irsp->un.ulpWord[5],
3102 *(((uint32_t *) irsp) + 6),
3103 *(((uint32_t *) irsp) + 7),
3104 *(((uint32_t *) irsp) + 8),
3105 *(((uint32_t *) irsp) + 9),
3106 *(((uint32_t *) irsp) + 10),
3107 *(((uint32_t *) irsp) + 11),
3108 *(((uint32_t *) irsp) + 12),
3109 *(((uint32_t *) irsp) + 13),
3110 *(((uint32_t *) irsp) + 14),
3111 *(((uint32_t *) irsp) + 15));
3112 }
3113
3114 /*
3115 * Fetch the IOCB command type and call the correct completion
3116 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3117 * get freed back to the lpfc_iocb_list by the discovery
3118 * kernel thread.
3119 */
3120 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3121 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3122 switch (type) {
3123 case LPFC_SOL_IOCB:
3124 spin_unlock_irqrestore(&phba->hbalock, iflag);
3125 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3126 spin_lock_irqsave(&phba->hbalock, iflag);
3127 break;
3128
3129 case LPFC_UNSOL_IOCB:
3130 spin_unlock_irqrestore(&phba->hbalock, iflag);
3131 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3132 spin_lock_irqsave(&phba->hbalock, iflag);
3133 if (!rc)
3134 free_saveq = 0;
3135 break;
3136
3137 case LPFC_ABORT_IOCB:
3138 cmdiocbp = NULL;
3139 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3140 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3141 saveq);
3142 if (cmdiocbp) {
3143 /* Call the specified completion routine */
3144 if (cmdiocbp->iocb_cmpl) {
3145 spin_unlock_irqrestore(&phba->hbalock,
3146 iflag);
3147 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3148 saveq);
3149 spin_lock_irqsave(&phba->hbalock,
3150 iflag);
3151 } else
3152 __lpfc_sli_release_iocbq(phba,
3153 cmdiocbp);
3154 }
3155 break;
3156
3157 case LPFC_UNKNOWN_IOCB:
3158 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3159 char adaptermsg[LPFC_MAX_ADPTMSG];
3160 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3161 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3162 MAX_MSG_DATA);
3163 dev_warn(&((phba->pcidev)->dev),
3164 "lpfc%d: %s\n",
3165 phba->brd_no, adaptermsg);
3166 } else {
3167 /* Unknown IOCB command */
3168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3169 "0335 Unknown IOCB "
3170 "command Data: x%x "
3171 "x%x x%x x%x\n",
3172 irsp->ulpCommand,
3173 irsp->ulpStatus,
3174 irsp->ulpIoTag,
3175 irsp->ulpContext);
3176 }
3177 break;
3178 }
3179
3180 if (free_saveq) {
3181 list_for_each_entry_safe(rspiocbp, next_iocb,
3182 &saveq->list, list) {
3183 list_del(&rspiocbp->list);
3184 __lpfc_sli_release_iocbq(phba, rspiocbp);
3185 }
3186 __lpfc_sli_release_iocbq(phba, saveq);
3187 }
3188 rspiocbp = NULL;
3189 }
3190 spin_unlock_irqrestore(&phba->hbalock, iflag);
3191 return rspiocbp;
3192}
3193
3194/**
3195 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003196 * @phba: Pointer to HBA context object.
3197 * @pring: Pointer to driver SLI ring object.
3198 * @mask: Host attention register mask for this ring.
3199 *
James Smart3772a992009-05-22 14:50:54 -04003200 * This routine wraps the actual slow_ring event process routine from the
3201 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003202 **/
James Smart3772a992009-05-22 14:50:54 -04003203void
James Smart2e0fef82007-06-17 19:56:36 -05003204lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3205 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003206{
James Smart3772a992009-05-22 14:50:54 -04003207 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3208}
3209
3210/**
3211 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3212 * @phba: Pointer to HBA context object.
3213 * @pring: Pointer to driver SLI ring object.
3214 * @mask: Host attention register mask for this ring.
3215 *
3216 * This function is called from the worker thread when there is a ring event
3217 * for non-fcp rings. The caller does not hold any lock. The function will
3218 * remove each response iocb in the response ring and calls the handle
3219 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3220 **/
3221static void
3222lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3223 struct lpfc_sli_ring *pring, uint32_t mask)
3224{
James Smart34b02dc2008-08-24 21:49:55 -04003225 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003226 IOCB_t *entry;
3227 IOCB_t *irsp = NULL;
3228 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003229 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003230 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003231 uint32_t status;
dea31012005-04-17 16:05:31 -05003232
James Smart34b02dc2008-08-24 21:49:55 -04003233 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003234 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003235 pring->stats.iocb_event++;
3236
dea31012005-04-17 16:05:31 -05003237 /*
3238 * The next available response entry should never exceed the maximum
3239 * entries. If it does, treat it as an adapter hardware error.
3240 */
3241 portRspMax = pring->numRiocb;
3242 portRspPut = le32_to_cpu(pgp->rspPutInx);
3243 if (portRspPut >= portRspMax) {
3244 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003245 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003246 * rsp ring <portRspMax>
3247 */
James Smarted957682007-06-17 19:56:37 -05003248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003249 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003250 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003251 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003252
James Smart2e0fef82007-06-17 19:56:36 -05003253 phba->link_state = LPFC_HBA_ERROR;
3254 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003255
3256 phba->work_hs = HS_FFER3;
3257 lpfc_handle_eratt(phba);
3258
James Smart3772a992009-05-22 14:50:54 -04003259 return;
dea31012005-04-17 16:05:31 -05003260 }
3261
3262 rmb();
dea31012005-04-17 16:05:31 -05003263 while (pring->rspidx != portRspPut) {
3264 /*
3265 * Build a completion list and call the appropriate handler.
3266 * The process is to get the next available response iocb, get
3267 * a free iocb from the list, copy the response data into the
3268 * free iocb, insert to the continuation list, and update the
3269 * next response index to slim. This process makes response
3270 * iocb's in the ring available to DMA as fast as possible but
3271 * pays a penalty for a copy operation. Since the iocb is
3272 * only 32 bytes, this penalty is considered small relative to
3273 * the PCI reads for register values and a slim write. When
3274 * the ulpLe field is set, the entire Command has been
3275 * received.
3276 */
James Smarted957682007-06-17 19:56:37 -05003277 entry = lpfc_resp_iocb(phba, pring);
3278
James Smart858c9f62007-06-17 19:56:39 -05003279 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003280 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003281 if (rspiocbp == NULL) {
3282 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003283 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003284 break;
3285 }
3286
James Smarted957682007-06-17 19:56:37 -05003287 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3288 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003289 irsp = &rspiocbp->iocb;
3290
3291 if (++pring->rspidx >= portRspMax)
3292 pring->rspidx = 0;
3293
James Smarta58cbd52007-08-02 11:09:43 -04003294 if (pring->ringno == LPFC_ELS_RING) {
3295 lpfc_debugfs_slow_ring_trc(phba,
3296 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3297 *(((uint32_t *) irsp) + 4),
3298 *(((uint32_t *) irsp) + 6),
3299 *(((uint32_t *) irsp) + 7));
3300 }
3301
James Smarted957682007-06-17 19:56:37 -05003302 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003303
James Smart3772a992009-05-22 14:50:54 -04003304 spin_unlock_irqrestore(&phba->hbalock, iflag);
3305 /* Handle the response IOCB */
3306 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3307 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003308
3309 /*
3310 * If the port response put pointer has not been updated, sync
3311 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3312 * response put pointer.
3313 */
3314 if (pring->rspidx == portRspPut) {
3315 portRspPut = le32_to_cpu(pgp->rspPutInx);
3316 }
3317 } /* while (pring->rspidx != portRspPut) */
3318
James Smart92d7f7b2007-06-17 19:56:38 -05003319 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003320 /* At least one response entry has been freed */
3321 pring->stats.iocb_rsp_full++;
3322 /* SET RxRE_RSP in Chip Att register */
3323 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3324 writel(status, phba->CAregaddr);
3325 readl(phba->CAregaddr); /* flush */
3326 }
3327 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3328 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3329 pring->stats.iocb_cmd_empty++;
3330
3331 /* Force update of the local copy of cmdGetInx */
3332 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3333 lpfc_sli_resume_iocb(phba, pring);
3334
3335 if ((pring->lpfc_sli_cmd_available))
3336 (pring->lpfc_sli_cmd_available) (phba, pring);
3337
3338 }
3339
James Smart2e0fef82007-06-17 19:56:36 -05003340 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003341 return;
dea31012005-04-17 16:05:31 -05003342}
3343
James Smarte59058c2008-08-24 21:49:00 -04003344/**
James Smart4f774512009-05-22 14:52:35 -04003345 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3346 * @phba: Pointer to HBA context object.
3347 * @pring: Pointer to driver SLI ring object.
3348 * @mask: Host attention register mask for this ring.
3349 *
3350 * This function is called from the worker thread when there is a pending
3351 * ELS response iocb on the driver internal slow-path response iocb worker
3352 * queue. The caller does not hold any lock. The function will remove each
3353 * response iocb from the response worker queue and calls the handle
3354 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3355 **/
3356static void
3357lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3358 struct lpfc_sli_ring *pring, uint32_t mask)
3359{
3360 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003361 struct hbq_dmabuf *dmabuf;
3362 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003363 unsigned long iflag;
3364
James Smart45ed1192009-10-02 15:17:02 -04003365 spin_lock_irqsave(&phba->hbalock, iflag);
3366 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3367 spin_unlock_irqrestore(&phba->hbalock, iflag);
3368 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003369 /* Get the response iocb from the head of work queue */
3370 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003371 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003372 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003373 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003374
3375 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3376 case CQE_CODE_COMPL_WQE:
3377 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3378 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003379 /* Translate ELS WCQE to response IOCBQ */
3380 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3381 irspiocbq);
3382 if (irspiocbq)
3383 lpfc_sli_sp_handle_rspiocb(phba, pring,
3384 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003385 break;
3386 case CQE_CODE_RECEIVE:
3387 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3388 cq_event);
3389 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3390 break;
3391 default:
3392 break;
3393 }
James Smart4f774512009-05-22 14:52:35 -04003394 }
3395}
3396
3397/**
James Smart3621a712009-04-06 18:47:14 -04003398 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003399 * @phba: Pointer to HBA context object.
3400 * @pring: Pointer to driver SLI ring object.
3401 *
3402 * This function aborts all iocbs in the given ring and frees all the iocb
3403 * objects in txq. This function issues an abort iocb for all the iocb commands
3404 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3405 * the return of this function. The caller is not required to hold any locks.
3406 **/
James Smart2e0fef82007-06-17 19:56:36 -05003407void
dea31012005-04-17 16:05:31 -05003408lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3409{
James Smart2534ba72007-04-25 09:52:20 -04003410 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003411 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003412
James Smart92d7f7b2007-06-17 19:56:38 -05003413 if (pring->ringno == LPFC_ELS_RING) {
3414 lpfc_fabric_abort_hba(phba);
3415 }
3416
dea31012005-04-17 16:05:31 -05003417 /* Error everything on txq and txcmplq
3418 * First do the txq.
3419 */
James Smart2e0fef82007-06-17 19:56:36 -05003420 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003421 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003422 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003423
3424 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003425 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3426 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3427
James Smart2e0fef82007-06-17 19:56:36 -05003428 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003429
James Smarta257bf92009-04-06 18:48:10 -04003430 /* Cancel all the IOCBs from the completions list */
3431 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3432 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003433}
3434
James Smarte59058c2008-08-24 21:49:00 -04003435/**
James Smart3621a712009-04-06 18:47:14 -04003436 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003437 * @phba: Pointer to HBA context object.
3438 *
3439 * This function flushes all iocbs in the fcp ring and frees all the iocb
3440 * objects in txq and txcmplq. This function will not issue abort iocbs
3441 * for all the iocb commands in txcmplq, they will just be returned with
3442 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3443 * slot has been permanently disabled.
3444 **/
3445void
3446lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3447{
3448 LIST_HEAD(txq);
3449 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003450 struct lpfc_sli *psli = &phba->sli;
3451 struct lpfc_sli_ring *pring;
3452
3453 /* Currently, only one fcp ring */
3454 pring = &psli->ring[psli->fcp_ring];
3455
3456 spin_lock_irq(&phba->hbalock);
3457 /* Retrieve everything on txq */
3458 list_splice_init(&pring->txq, &txq);
3459 pring->txq_cnt = 0;
3460
3461 /* Retrieve everything on the txcmplq */
3462 list_splice_init(&pring->txcmplq, &txcmplq);
3463 pring->txcmplq_cnt = 0;
3464 spin_unlock_irq(&phba->hbalock);
3465
3466 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003467 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3468 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003469
3470 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003471 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3472 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003473}
3474
3475/**
James Smart3772a992009-05-22 14:50:54 -04003476 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003477 * @phba: Pointer to HBA context object.
3478 * @mask: Bit mask to be checked.
3479 *
3480 * This function reads the host status register and compares
3481 * with the provided bit mask to check if HBA completed
3482 * the restart. This function will wait in a loop for the
3483 * HBA to complete restart. If the HBA does not restart within
3484 * 15 iterations, the function will reset the HBA again. The
3485 * function returns 1 when HBA fail to restart otherwise returns
3486 * zero.
3487 **/
James Smart3772a992009-05-22 14:50:54 -04003488static int
3489lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003490{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003491 uint32_t status;
3492 int i = 0;
3493 int retval = 0;
dea31012005-04-17 16:05:31 -05003494
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 /* Read the HBA Host Status Register */
3496 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003497
Jamie Wellnitz41415862006-02-28 19:25:27 -05003498 /*
3499 * Check status register every 100ms for 5 retries, then every
3500 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3501 * every 2.5 sec for 4.
3502 * Break our of the loop if errors occurred during init.
3503 */
3504 while (((status & mask) != mask) &&
3505 !(status & HS_FFERM) &&
3506 i++ < 20) {
dea31012005-04-17 16:05:31 -05003507
Jamie Wellnitz41415862006-02-28 19:25:27 -05003508 if (i <= 5)
3509 msleep(10);
3510 else if (i <= 10)
3511 msleep(500);
3512 else
3513 msleep(2500);
dea31012005-04-17 16:05:31 -05003514
Jamie Wellnitz41415862006-02-28 19:25:27 -05003515 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003516 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003517 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003518 lpfc_sli_brdrestart(phba);
3519 }
3520 /* Read the HBA Host Status Register */
3521 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003522 }
dea31012005-04-17 16:05:31 -05003523
Jamie Wellnitz41415862006-02-28 19:25:27 -05003524 /* Check to see if any errors occurred during init */
3525 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3527 "2751 Adapter failed to restart, "
3528 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3529 status,
3530 readl(phba->MBslimaddr + 0xa8),
3531 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003532 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003533 retval = 1;
3534 }
dea31012005-04-17 16:05:31 -05003535
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536 return retval;
dea31012005-04-17 16:05:31 -05003537}
3538
James Smartda0436e2009-05-22 14:51:39 -04003539/**
3540 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3541 * @phba: Pointer to HBA context object.
3542 * @mask: Bit mask to be checked.
3543 *
3544 * This function checks the host status register to check if HBA is
3545 * ready. This function will wait in a loop for the HBA to be ready
3546 * If the HBA is not ready , the function will will reset the HBA PCI
3547 * function again. The function returns 1 when HBA fail to be ready
3548 * otherwise returns zero.
3549 **/
3550static int
3551lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3552{
3553 uint32_t status;
3554 int retval = 0;
3555
3556 /* Read the HBA Host Status Register */
3557 status = lpfc_sli4_post_status_check(phba);
3558
3559 if (status) {
3560 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3561 lpfc_sli_brdrestart(phba);
3562 status = lpfc_sli4_post_status_check(phba);
3563 }
3564
3565 /* Check to see if any errors occurred during init */
3566 if (status) {
3567 phba->link_state = LPFC_HBA_ERROR;
3568 retval = 1;
3569 } else
3570 phba->sli4_hba.intr_enable = 0;
3571
3572 return retval;
3573}
3574
3575/**
3576 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3577 * @phba: Pointer to HBA context object.
3578 * @mask: Bit mask to be checked.
3579 *
3580 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3581 * from the API jump table function pointer from the lpfc_hba struct.
3582 **/
3583int
3584lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3585{
3586 return phba->lpfc_sli_brdready(phba, mask);
3587}
3588
James Smart92908312006-03-07 15:04:13 -05003589#define BARRIER_TEST_PATTERN (0xdeadbeef)
3590
James Smarte59058c2008-08-24 21:49:00 -04003591/**
James Smart3621a712009-04-06 18:47:14 -04003592 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003593 * @phba: Pointer to HBA context object.
3594 *
3595 * This function is called before resetting an HBA. This
3596 * function requests HBA to quiesce DMAs before a reset.
3597 **/
James Smart2e0fef82007-06-17 19:56:36 -05003598void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003599{
James Smart65a29c12006-07-06 15:50:50 -04003600 uint32_t __iomem *resp_buf;
3601 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003602 volatile uint32_t mbox;
3603 uint32_t hc_copy;
3604 int i;
3605 uint8_t hdrtype;
3606
3607 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3608 if (hdrtype != 0x80 ||
3609 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3610 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3611 return;
3612
3613 /*
3614 * Tell the other part of the chip to suspend temporarily all
3615 * its DMA activity.
3616 */
James Smart65a29c12006-07-06 15:50:50 -04003617 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003618
3619 /* Disable the error attention */
3620 hc_copy = readl(phba->HCregaddr);
3621 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3622 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003623 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003624
3625 if (readl(phba->HAregaddr) & HA_ERATT) {
3626 /* Clear Chip error bit */
3627 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003628 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003629 }
3630
3631 mbox = 0;
3632 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3633 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3634
3635 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003636 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003637 writel(mbox, mbox_buf);
3638
3639 for (i = 0;
3640 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3641 mdelay(1);
3642
3643 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003644 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003645 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003646 goto restore_hc;
3647 else
3648 goto clear_errat;
3649 }
3650
3651 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3652 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3653 mdelay(1);
3654
3655clear_errat:
3656
3657 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3658 mdelay(1);
3659
3660 if (readl(phba->HAregaddr) & HA_ERATT) {
3661 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003662 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003663 }
3664
3665restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003666 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003667 writel(hc_copy, phba->HCregaddr);
3668 readl(phba->HCregaddr); /* flush */
3669}
3670
James Smarte59058c2008-08-24 21:49:00 -04003671/**
James Smart3621a712009-04-06 18:47:14 -04003672 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003673 * @phba: Pointer to HBA context object.
3674 *
3675 * This function issues a kill_board mailbox command and waits for
3676 * the error attention interrupt. This function is called for stopping
3677 * the firmware processing. The caller is not required to hold any
3678 * locks. This function calls lpfc_hba_down_post function to free
3679 * any pending commands after the kill. The function will return 1 when it
3680 * fails to kill the board else will return 0.
3681 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003682int
James Smart2e0fef82007-06-17 19:56:36 -05003683lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003684{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003685 struct lpfc_sli *psli;
3686 LPFC_MBOXQ_t *pmb;
3687 uint32_t status;
3688 uint32_t ha_copy;
3689 int retval;
3690 int i = 0;
3691
3692 psli = &phba->sli;
3693
3694 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003695 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003696 "0329 Kill HBA Data: x%x x%x\n",
3697 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003698
James Smart98c9ea52007-10-27 13:37:33 -04003699 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3700 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003701 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003702
3703 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003704 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003705 status = readl(phba->HCregaddr);
3706 status &= ~HC_ERINT_ENA;
3707 writel(status, phba->HCregaddr);
3708 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003709 phba->link_flag |= LS_IGNORE_ERATT;
3710 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003711
3712 lpfc_kill_board(phba, pmb);
3713 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3714 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3715
3716 if (retval != MBX_SUCCESS) {
3717 if (retval != MBX_BUSY)
3718 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3720 "2752 KILL_BOARD command failed retval %d\n",
3721 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003722 spin_lock_irq(&phba->hbalock);
3723 phba->link_flag &= ~LS_IGNORE_ERATT;
3724 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003725 return 1;
3726 }
3727
James Smartf4b4c682009-05-22 14:53:12 -04003728 spin_lock_irq(&phba->hbalock);
3729 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3730 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003731
Jamie Wellnitz41415862006-02-28 19:25:27 -05003732 mempool_free(pmb, phba->mbox_mem_pool);
3733
3734 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3735 * attention every 100ms for 3 seconds. If we don't get ERATT after
3736 * 3 seconds we still set HBA_ERROR state because the status of the
3737 * board is now undefined.
3738 */
3739 ha_copy = readl(phba->HAregaddr);
3740
3741 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3742 mdelay(100);
3743 ha_copy = readl(phba->HAregaddr);
3744 }
3745
3746 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003747 if (ha_copy & HA_ERATT) {
3748 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003749 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003750 }
James Smart2e0fef82007-06-17 19:56:36 -05003751 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003752 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003753 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003754 phba->link_flag &= ~LS_IGNORE_ERATT;
3755 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003756
Jamie Wellnitz41415862006-02-28 19:25:27 -05003757 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003758 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759
James Smart2e0fef82007-06-17 19:56:36 -05003760 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003761}
3762
James Smarte59058c2008-08-24 21:49:00 -04003763/**
James Smart3772a992009-05-22 14:50:54 -04003764 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003765 * @phba: Pointer to HBA context object.
3766 *
3767 * This function resets the HBA by writing HC_INITFF to the control
3768 * register. After the HBA resets, this function resets all the iocb ring
3769 * indices. This function disables PCI layer parity checking during
3770 * the reset.
3771 * This function returns 0 always.
3772 * The caller is not required to hold any locks.
3773 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003774int
James Smart2e0fef82007-06-17 19:56:36 -05003775lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003776{
3777 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003778 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003780 int i;
dea31012005-04-17 16:05:31 -05003781
Jamie Wellnitz41415862006-02-28 19:25:27 -05003782 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003783
Jamie Wellnitz41415862006-02-28 19:25:27 -05003784 /* Reset HBA */
3785 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003786 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003787 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003788
3789 /* perform board reset */
3790 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003791 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003792 phba->pport->fc_myDID = 0;
3793 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003794
Jamie Wellnitz41415862006-02-28 19:25:27 -05003795 /* Turn off parity checking and serr during the physical reset */
3796 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3797 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3798 (cfg_value &
3799 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3800
James Smart3772a992009-05-22 14:50:54 -04003801 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3802
Jamie Wellnitz41415862006-02-28 19:25:27 -05003803 /* Now toggle INITFF bit in the Host Control Register */
3804 writel(HC_INITFF, phba->HCregaddr);
3805 mdelay(1);
3806 readl(phba->HCregaddr); /* flush */
3807 writel(0, phba->HCregaddr);
3808 readl(phba->HCregaddr); /* flush */
3809
3810 /* Restore PCI cmd register */
3811 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003812
3813 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814 for (i = 0; i < psli->num_rings; i++) {
3815 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003816 pring->flag = 0;
3817 pring->rspidx = 0;
3818 pring->next_cmdidx = 0;
3819 pring->local_getidx = 0;
3820 pring->cmdidx = 0;
3821 pring->missbufcnt = 0;
3822 }
dea31012005-04-17 16:05:31 -05003823
James Smart2e0fef82007-06-17 19:56:36 -05003824 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 return 0;
3826}
3827
James Smarte59058c2008-08-24 21:49:00 -04003828/**
James Smartda0436e2009-05-22 14:51:39 -04003829 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3830 * @phba: Pointer to HBA context object.
3831 *
3832 * This function resets a SLI4 HBA. This function disables PCI layer parity
3833 * checking during resets the device. The caller is not required to hold
3834 * any locks.
3835 *
3836 * This function returns 0 always.
3837 **/
3838int
3839lpfc_sli4_brdreset(struct lpfc_hba *phba)
3840{
3841 struct lpfc_sli *psli = &phba->sli;
3842 uint16_t cfg_value;
3843 uint8_t qindx;
3844
3845 /* Reset HBA */
3846 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3847 "0295 Reset HBA Data: x%x x%x\n",
3848 phba->pport->port_state, psli->sli_flag);
3849
3850 /* perform board reset */
3851 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003852 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003853 phba->pport->fc_myDID = 0;
3854 phba->pport->fc_prevDID = 0;
3855
James Smartda0436e2009-05-22 14:51:39 -04003856 spin_lock_irq(&phba->hbalock);
3857 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3858 phba->fcf.fcf_flag = 0;
3859 /* Clean up the child queue list for the CQs */
3860 list_del_init(&phba->sli4_hba.mbx_wq->list);
3861 list_del_init(&phba->sli4_hba.els_wq->list);
3862 list_del_init(&phba->sli4_hba.hdr_rq->list);
3863 list_del_init(&phba->sli4_hba.dat_rq->list);
3864 list_del_init(&phba->sli4_hba.mbx_cq->list);
3865 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003866 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3867 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3868 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3869 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3870 spin_unlock_irq(&phba->hbalock);
3871
3872 /* Now physically reset the device */
3873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3874 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003875
3876 /* Turn off parity checking and serr during the physical reset */
3877 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3878 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3879 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3880
James Smartda0436e2009-05-22 14:51:39 -04003881 /* Perform FCoE PCI function reset */
3882 lpfc_pci_function_reset(phba);
3883
James Smartbe858b62010-12-15 17:57:20 -05003884 /* Restore PCI cmd register */
3885 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3886
James Smartda0436e2009-05-22 14:51:39 -04003887 return 0;
3888}
3889
3890/**
3891 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003892 * @phba: Pointer to HBA context object.
3893 *
3894 * This function is called in the SLI initialization code path to
3895 * restart the HBA. The caller is not required to hold any lock.
3896 * This function writes MBX_RESTART mailbox command to the SLIM and
3897 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3898 * function to free any pending commands. The function enables
3899 * POST only during the first initialization. The function returns zero.
3900 * The function does not guarantee completion of MBX_RESTART mailbox
3901 * command before the return of this function.
3902 **/
James Smartda0436e2009-05-22 14:51:39 -04003903static int
3904lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003905{
3906 MAILBOX_t *mb;
3907 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908 volatile uint32_t word0;
3909 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003910 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003911
James Smart2e0fef82007-06-17 19:56:36 -05003912 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003913
James Smart0d878412009-10-02 15:16:56 -04003914 /* Take PCIe device Advanced Error Reporting (AER) state */
3915 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3916
Jamie Wellnitz41415862006-02-28 19:25:27 -05003917 psli = &phba->sli;
3918
3919 /* Restart HBA */
3920 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003921 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003922 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003923
3924 word0 = 0;
3925 mb = (MAILBOX_t *) &word0;
3926 mb->mbxCommand = MBX_RESTART;
3927 mb->mbxHc = 1;
3928
James Smart92908312006-03-07 15:04:13 -05003929 lpfc_reset_barrier(phba);
3930
Jamie Wellnitz41415862006-02-28 19:25:27 -05003931 to_slim = phba->MBslimaddr;
3932 writel(*(uint32_t *) mb, to_slim);
3933 readl(to_slim); /* flush */
3934
3935 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003936 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003937 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003938 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003939 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003940 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003941 writel(*(uint32_t *) mb, to_slim);
3942 readl(to_slim); /* flush */
3943
3944 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003945 phba->pport->stopped = 0;
3946 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003947 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003948 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949
James Smart64ba8812006-08-02 15:24:34 -04003950 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3951 psli->stats_start = get_seconds();
3952
James Smarteaf15d52008-12-04 22:39:29 -05003953 /* Give the INITFF and Post time to settle. */
3954 mdelay(100);
dea31012005-04-17 16:05:31 -05003955
James Smart0d878412009-10-02 15:16:56 -04003956 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3957 if (hba_aer_enabled)
3958 pci_disable_pcie_error_reporting(phba->pcidev);
3959
Jamie Wellnitz41415862006-02-28 19:25:27 -05003960 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003961
3962 return 0;
3963}
3964
James Smarte59058c2008-08-24 21:49:00 -04003965/**
James Smartda0436e2009-05-22 14:51:39 -04003966 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3967 * @phba: Pointer to HBA context object.
3968 *
3969 * This function is called in the SLI initialization code path to restart
3970 * a SLI4 HBA. The caller is not required to hold any lock.
3971 * At the end of the function, it calls lpfc_hba_down_post function to
3972 * free any pending commands.
3973 **/
3974static int
3975lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3976{
3977 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003978 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003979
3980 /* Restart HBA */
3981 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3982 "0296 Restart HBA Data: x%x x%x\n",
3983 phba->pport->port_state, psli->sli_flag);
3984
James Smart75baf692010-06-08 18:31:21 -04003985 /* Take PCIe device Advanced Error Reporting (AER) state */
3986 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3987
James Smartda0436e2009-05-22 14:51:39 -04003988 lpfc_sli4_brdreset(phba);
3989
3990 spin_lock_irq(&phba->hbalock);
3991 phba->pport->stopped = 0;
3992 phba->link_state = LPFC_INIT_START;
3993 phba->hba_flag = 0;
3994 spin_unlock_irq(&phba->hbalock);
3995
3996 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3997 psli->stats_start = get_seconds();
3998
James Smart75baf692010-06-08 18:31:21 -04003999 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4000 if (hba_aer_enabled)
4001 pci_disable_pcie_error_reporting(phba->pcidev);
4002
James Smartda0436e2009-05-22 14:51:39 -04004003 lpfc_hba_down_post(phba);
4004
4005 return 0;
4006}
4007
4008/**
4009 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4010 * @phba: Pointer to HBA context object.
4011 *
4012 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4013 * API jump table function pointer from the lpfc_hba struct.
4014**/
4015int
4016lpfc_sli_brdrestart(struct lpfc_hba *phba)
4017{
4018 return phba->lpfc_sli_brdrestart(phba);
4019}
4020
4021/**
James Smart3621a712009-04-06 18:47:14 -04004022 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004023 * @phba: Pointer to HBA context object.
4024 *
4025 * This function is called after a HBA restart to wait for successful
4026 * restart of the HBA. Successful restart of the HBA is indicated by
4027 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4028 * iteration, the function will restart the HBA again. The function returns
4029 * zero if HBA successfully restarted else returns negative error code.
4030 **/
dea31012005-04-17 16:05:31 -05004031static int
4032lpfc_sli_chipset_init(struct lpfc_hba *phba)
4033{
4034 uint32_t status, i = 0;
4035
4036 /* Read the HBA Host Status Register */
4037 status = readl(phba->HSregaddr);
4038
4039 /* Check status register to see what current state is */
4040 i = 0;
4041 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4042
James Smartdcf2a4e2010-09-29 11:18:53 -04004043 /* Check every 10ms for 10 retries, then every 100ms for 90
4044 * retries, then every 1 sec for 50 retires for a total of
4045 * ~60 seconds before reset the board again and check every
4046 * 1 sec for 50 retries. The up to 60 seconds before the
4047 * board ready is required by the Falcon FIPS zeroization
4048 * complete, and any reset the board in between shall cause
4049 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004050 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004051 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004052 /* Adapter failed to init, timeout, status reg
4053 <status> */
James Smarted957682007-06-17 19:56:37 -05004054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004055 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004056 "timeout, status reg x%x, "
4057 "FW Data: A8 x%x AC x%x\n", status,
4058 readl(phba->MBslimaddr + 0xa8),
4059 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004060 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004061 return -ETIMEDOUT;
4062 }
4063
4064 /* Check to see if any errors occurred during init */
4065 if (status & HS_FFERM) {
4066 /* ERROR: During chipset initialization */
4067 /* Adapter failed to init, chipset, status reg
4068 <status> */
James Smarted957682007-06-17 19:56:37 -05004069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004070 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004071 "chipset, status reg x%x, "
4072 "FW Data: A8 x%x AC x%x\n", status,
4073 readl(phba->MBslimaddr + 0xa8),
4074 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004075 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004076 return -EIO;
4077 }
4078
James Smartdcf2a4e2010-09-29 11:18:53 -04004079 if (i <= 10)
dea31012005-04-17 16:05:31 -05004080 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004081 else if (i <= 100)
4082 msleep(100);
4083 else
4084 msleep(1000);
dea31012005-04-17 16:05:31 -05004085
James Smartdcf2a4e2010-09-29 11:18:53 -04004086 if (i == 150) {
4087 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004088 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004089 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004090 }
4091 /* Read the HBA Host Status Register */
4092 status = readl(phba->HSregaddr);
4093 }
4094
4095 /* Check to see if any errors occurred during init */
4096 if (status & HS_FFERM) {
4097 /* ERROR: During chipset initialization */
4098 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004100 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004101 "status reg x%x, "
4102 "FW Data: A8 x%x AC x%x\n", status,
4103 readl(phba->MBslimaddr + 0xa8),
4104 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004105 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004106 return -EIO;
4107 }
4108
4109 /* Clear all interrupt enable conditions */
4110 writel(0, phba->HCregaddr);
4111 readl(phba->HCregaddr); /* flush */
4112
4113 /* setup host attn register */
4114 writel(0xffffffff, phba->HAregaddr);
4115 readl(phba->HAregaddr); /* flush */
4116 return 0;
4117}
4118
James Smarte59058c2008-08-24 21:49:00 -04004119/**
James Smart3621a712009-04-06 18:47:14 -04004120 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004121 *
4122 * This function calculates and returns the number of HBQs required to be
4123 * configured.
4124 **/
James Smart78b2d852007-08-02 11:10:21 -04004125int
James Smarted957682007-06-17 19:56:37 -05004126lpfc_sli_hbq_count(void)
4127{
James Smart92d7f7b2007-06-17 19:56:38 -05004128 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004129}
4130
James Smarte59058c2008-08-24 21:49:00 -04004131/**
James Smart3621a712009-04-06 18:47:14 -04004132 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004133 *
4134 * This function adds the number of hbq entries in every HBQ to get
4135 * the total number of hbq entries required for the HBA and returns
4136 * the total count.
4137 **/
James Smarted957682007-06-17 19:56:37 -05004138static int
4139lpfc_sli_hbq_entry_count(void)
4140{
4141 int hbq_count = lpfc_sli_hbq_count();
4142 int count = 0;
4143 int i;
4144
4145 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004146 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004147 return count;
4148}
4149
James Smarte59058c2008-08-24 21:49:00 -04004150/**
James Smart3621a712009-04-06 18:47:14 -04004151 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004152 *
4153 * This function calculates amount of memory required for all hbq entries
4154 * to be configured and returns the total memory required.
4155 **/
dea31012005-04-17 16:05:31 -05004156int
James Smarted957682007-06-17 19:56:37 -05004157lpfc_sli_hbq_size(void)
4158{
4159 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4160}
4161
James Smarte59058c2008-08-24 21:49:00 -04004162/**
James Smart3621a712009-04-06 18:47:14 -04004163 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004164 * @phba: Pointer to HBA context object.
4165 *
4166 * This function is called during the SLI initialization to configure
4167 * all the HBQs and post buffers to the HBQ. The caller is not
4168 * required to hold any locks. This function will return zero if successful
4169 * else it will return negative error code.
4170 **/
James Smarted957682007-06-17 19:56:37 -05004171static int
4172lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4173{
4174 int hbq_count = lpfc_sli_hbq_count();
4175 LPFC_MBOXQ_t *pmb;
4176 MAILBOX_t *pmbox;
4177 uint32_t hbqno;
4178 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004179
James Smart92d7f7b2007-06-17 19:56:38 -05004180 /* Get a Mailbox buffer to setup mailbox
4181 * commands for HBA initialization
4182 */
James Smarted957682007-06-17 19:56:37 -05004183 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4184
4185 if (!pmb)
4186 return -ENOMEM;
4187
James Smart04c68492009-05-22 14:52:52 -04004188 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004189
4190 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4191 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004192 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004193
4194 hbq_entry_index = 0;
4195 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4196 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4197 phba->hbqs[hbqno].hbqPutIdx = 0;
4198 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4199 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004200 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004201 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4202 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004203 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4204
4205 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4206 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4207 mbxStatus <status>, ring <num> */
4208
4209 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004210 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004211 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004212 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004213 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004214 pmbox->mbxStatus, hbqno);
4215
4216 phba->link_state = LPFC_HBA_ERROR;
4217 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004218 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004219 }
4220 }
4221 phba->hbq_count = hbq_count;
4222
James Smarted957682007-06-17 19:56:37 -05004223 mempool_free(pmb, phba->mbox_mem_pool);
4224
James Smart92d7f7b2007-06-17 19:56:38 -05004225 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004226 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4227 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004228 return 0;
4229}
4230
James Smarte59058c2008-08-24 21:49:00 -04004231/**
James Smart4f774512009-05-22 14:52:35 -04004232 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4233 * @phba: Pointer to HBA context object.
4234 *
4235 * This function is called during the SLI initialization to configure
4236 * all the HBQs and post buffers to the HBQ. The caller is not
4237 * required to hold any locks. This function will return zero if successful
4238 * else it will return negative error code.
4239 **/
4240static int
4241lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4242{
4243 phba->hbq_in_use = 1;
4244 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4245 phba->hbq_count = 1;
4246 /* Initially populate or replenish the HBQs */
4247 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4248 return 0;
4249}
4250
4251/**
James Smart3621a712009-04-06 18:47:14 -04004252 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004253 * @phba: Pointer to HBA context object.
4254 * @sli_mode: sli mode - 2/3
4255 *
4256 * This function is called by the sli intialization code path
4257 * to issue config_port mailbox command. This function restarts the
4258 * HBA firmware and issues a config_port mailbox command to configure
4259 * the SLI interface in the sli mode specified by sli_mode
4260 * variable. The caller is not required to hold any locks.
4261 * The function returns 0 if successful, else returns negative error
4262 * code.
4263 **/
James Smart93996272008-08-24 21:50:30 -04004264int
4265lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004266{
4267 LPFC_MBOXQ_t *pmb;
4268 uint32_t resetcount = 0, rc = 0, done = 0;
4269
4270 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4271 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004272 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004273 return -ENOMEM;
4274 }
4275
James Smarted957682007-06-17 19:56:37 -05004276 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004277 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004278 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004279 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004280 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004281 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004282 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004283 rc = lpfc_sli_chipset_init(phba);
4284 if (rc)
4285 break;
4286
James Smart2e0fef82007-06-17 19:56:36 -05004287 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004288 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004289 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004290 resetcount++;
4291
James Smarted957682007-06-17 19:56:37 -05004292 /* Call pre CONFIG_PORT mailbox command initialization. A
4293 * value of 0 means the call was successful. Any other
4294 * nonzero value is a failure, but if ERESTART is returned,
4295 * the driver may reset the HBA and try again.
4296 */
dea31012005-04-17 16:05:31 -05004297 rc = lpfc_config_port_prep(phba);
4298 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004299 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004300 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004301 } else if (rc)
dea31012005-04-17 16:05:31 -05004302 break;
James Smart2e0fef82007-06-17 19:56:36 -05004303 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004304 lpfc_config_port(phba, pmb);
4305 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004306 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4307 LPFC_SLI3_HBQ_ENABLED |
4308 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004309 LPFC_SLI3_BG_ENABLED |
4310 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004311 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004313 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004314 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004315 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004316 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004317 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004318 spin_unlock_irq(&phba->hbalock);
4319 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004320 } else {
4321 /* Allow asynchronous mailbox command to go through */
4322 spin_lock_irq(&phba->hbalock);
4323 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4324 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004325 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004326 }
dea31012005-04-17 16:05:31 -05004327 }
James Smarted957682007-06-17 19:56:37 -05004328 if (!done) {
4329 rc = -EINVAL;
4330 goto do_prep_failed;
4331 }
James Smart04c68492009-05-22 14:52:52 -04004332 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4333 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004334 rc = -ENXIO;
4335 goto do_prep_failed;
4336 }
James Smart04c68492009-05-22 14:52:52 -04004337 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004338 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004339 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4340 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4341 phba->max_vpi : phba->max_vports;
4342
James Smart34b02dc2008-08-24 21:49:55 -04004343 } else
4344 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004345 phba->fips_level = 0;
4346 phba->fips_spec_rev = 0;
4347 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004348 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004349 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4350 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4351 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4352 "2850 Security Crypto Active. FIPS x%d "
4353 "(Spec Rev: x%d)",
4354 phba->fips_level, phba->fips_spec_rev);
4355 }
4356 if (pmb->u.mb.un.varCfgPort.sec_err) {
4357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4358 "2856 Config Port Security Crypto "
4359 "Error: x%x ",
4360 pmb->u.mb.un.varCfgPort.sec_err);
4361 }
James Smart04c68492009-05-22 14:52:52 -04004362 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004363 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004364 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004365 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004366
4367 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4368 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004369
4370 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004371 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004372 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4373 else
4374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4375 "0443 Adapter did not grant "
4376 "BlockGuard\n");
4377 }
James Smart34b02dc2008-08-24 21:49:55 -04004378 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004379 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004380 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004381 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004382 }
James Smart92d7f7b2007-06-17 19:56:38 -05004383do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004384 mempool_free(pmb, phba->mbox_mem_pool);
4385 return rc;
4386}
4387
James Smarte59058c2008-08-24 21:49:00 -04004388
4389/**
James Smart3621a712009-04-06 18:47:14 -04004390 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004391 * @phba: Pointer to HBA context object.
4392 *
4393 * This function is the main SLI intialization function. This function
4394 * is called by the HBA intialization code, HBA reset code and HBA
4395 * error attention handler code. Caller is not required to hold any
4396 * locks. This function issues config_port mailbox command to configure
4397 * the SLI, setup iocb rings and HBQ rings. In the end the function
4398 * calls the config_port_post function to issue init_link mailbox
4399 * command and to start the discovery. The function will return zero
4400 * if successful, else it will return negative error code.
4401 **/
James Smarted957682007-06-17 19:56:37 -05004402int
4403lpfc_sli_hba_setup(struct lpfc_hba *phba)
4404{
4405 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004406 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004407
4408 switch (lpfc_sli_mode) {
4409 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004410 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004412 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004413 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004414 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004415 break;
4416 }
James Smarted957682007-06-17 19:56:37 -05004417 mode = 2;
4418 break;
4419 case 0:
4420 case 3:
4421 break;
4422 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004424 "1819 Unrecognized lpfc_sli_mode "
4425 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004426
4427 break;
4428 }
4429
James Smart93996272008-08-24 21:50:30 -04004430 rc = lpfc_sli_config_port(phba, mode);
4431
James Smarted957682007-06-17 19:56:37 -05004432 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004434 "1820 Unable to select SLI-3. "
4435 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004436 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004437 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004438 if (rc)
dea31012005-04-17 16:05:31 -05004439 goto lpfc_sli_hba_setup_error;
4440
James Smart0d878412009-10-02 15:16:56 -04004441 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4442 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4443 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4444 if (!rc) {
4445 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4446 "2709 This device supports "
4447 "Advanced Error Reporting (AER)\n");
4448 spin_lock_irq(&phba->hbalock);
4449 phba->hba_flag |= HBA_AER_ENABLED;
4450 spin_unlock_irq(&phba->hbalock);
4451 } else {
4452 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4453 "2708 This device does not support "
4454 "Advanced Error Reporting (AER)\n");
4455 phba->cfg_aer_support = 0;
4456 }
4457 }
4458
James Smarted957682007-06-17 19:56:37 -05004459 if (phba->sli_rev == 3) {
4460 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4461 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004462 } else {
4463 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4464 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004465 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004466 }
4467
4468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004469 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4470 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004471 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004472
4473 if (rc)
4474 goto lpfc_sli_hba_setup_error;
4475
James Smart93996272008-08-24 21:50:30 -04004476 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004477 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4478 rc = lpfc_sli_hbq_setup(phba);
4479 if (rc)
4480 goto lpfc_sli_hba_setup_error;
4481 }
James Smart04c68492009-05-22 14:52:52 -04004482 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004483 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004484 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004485
4486 rc = lpfc_config_port_post(phba);
4487 if (rc)
4488 goto lpfc_sli_hba_setup_error;
4489
James Smarted957682007-06-17 19:56:37 -05004490 return rc;
4491
James Smart92d7f7b2007-06-17 19:56:38 -05004492lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004493 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004495 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004496 return rc;
4497}
4498
James Smartda0436e2009-05-22 14:51:39 -04004499/**
4500 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4501 * @phba: Pointer to HBA context object.
4502 * @mboxq: mailbox pointer.
4503 * This function issue a dump mailbox command to read config region
4504 * 23 and parse the records in the region and populate driver
4505 * data structure.
4506 **/
4507static int
4508lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4509 LPFC_MBOXQ_t *mboxq)
4510{
4511 struct lpfc_dmabuf *mp;
4512 struct lpfc_mqe *mqe;
4513 uint32_t data_length;
4514 int rc;
4515
4516 /* Program the default value of vlan_id and fc_map */
4517 phba->valid_vlan = 0;
4518 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4519 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4520 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4521
4522 mqe = &mboxq->u.mqe;
4523 if (lpfc_dump_fcoe_param(phba, mboxq))
4524 return -ENOMEM;
4525
4526 mp = (struct lpfc_dmabuf *) mboxq->context1;
4527 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4528
4529 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4530 "(%d):2571 Mailbox cmd x%x Status x%x "
4531 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4532 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4533 "CQ: x%x x%x x%x x%x\n",
4534 mboxq->vport ? mboxq->vport->vpi : 0,
4535 bf_get(lpfc_mqe_command, mqe),
4536 bf_get(lpfc_mqe_status, mqe),
4537 mqe->un.mb_words[0], mqe->un.mb_words[1],
4538 mqe->un.mb_words[2], mqe->un.mb_words[3],
4539 mqe->un.mb_words[4], mqe->un.mb_words[5],
4540 mqe->un.mb_words[6], mqe->un.mb_words[7],
4541 mqe->un.mb_words[8], mqe->un.mb_words[9],
4542 mqe->un.mb_words[10], mqe->un.mb_words[11],
4543 mqe->un.mb_words[12], mqe->un.mb_words[13],
4544 mqe->un.mb_words[14], mqe->un.mb_words[15],
4545 mqe->un.mb_words[16], mqe->un.mb_words[50],
4546 mboxq->mcqe.word0,
4547 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4548 mboxq->mcqe.trailer);
4549
4550 if (rc) {
4551 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4552 kfree(mp);
4553 return -EIO;
4554 }
4555 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004556 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004557 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4558 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004559 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004560 }
James Smartda0436e2009-05-22 14:51:39 -04004561
4562 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4563 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4564 kfree(mp);
4565 return 0;
4566}
4567
4568/**
4569 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4570 * @phba: pointer to lpfc hba data structure.
4571 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4572 * @vpd: pointer to the memory to hold resulting port vpd data.
4573 * @vpd_size: On input, the number of bytes allocated to @vpd.
4574 * On output, the number of data bytes in @vpd.
4575 *
4576 * This routine executes a READ_REV SLI4 mailbox command. In
4577 * addition, this routine gets the port vpd data.
4578 *
4579 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004580 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004581 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004582 **/
4583static int
4584lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4585 uint8_t *vpd, uint32_t *vpd_size)
4586{
4587 int rc = 0;
4588 uint32_t dma_size;
4589 struct lpfc_dmabuf *dmabuf;
4590 struct lpfc_mqe *mqe;
4591
4592 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4593 if (!dmabuf)
4594 return -ENOMEM;
4595
4596 /*
4597 * Get a DMA buffer for the vpd data resulting from the READ_REV
4598 * mailbox command.
4599 */
4600 dma_size = *vpd_size;
4601 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4602 dma_size,
4603 &dmabuf->phys,
4604 GFP_KERNEL);
4605 if (!dmabuf->virt) {
4606 kfree(dmabuf);
4607 return -ENOMEM;
4608 }
4609 memset(dmabuf->virt, 0, dma_size);
4610
4611 /*
4612 * The SLI4 implementation of READ_REV conflicts at word1,
4613 * bits 31:16 and SLI4 adds vpd functionality not present
4614 * in SLI3. This code corrects the conflicts.
4615 */
4616 lpfc_read_rev(phba, mboxq);
4617 mqe = &mboxq->u.mqe;
4618 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4619 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4620 mqe->un.read_rev.word1 &= 0x0000FFFF;
4621 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4622 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4623
4624 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4625 if (rc) {
4626 dma_free_coherent(&phba->pcidev->dev, dma_size,
4627 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004628 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004629 return -EIO;
4630 }
4631
James Smartda0436e2009-05-22 14:51:39 -04004632 /*
4633 * The available vpd length cannot be bigger than the
4634 * DMA buffer passed to the port. Catch the less than
4635 * case and update the caller's size.
4636 */
4637 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4638 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4639
James Smartd7c47992010-06-08 18:31:54 -04004640 memcpy(vpd, dmabuf->virt, *vpd_size);
4641
James Smartda0436e2009-05-22 14:51:39 -04004642 dma_free_coherent(&phba->pcidev->dev, dma_size,
4643 dmabuf->virt, dmabuf->phys);
4644 kfree(dmabuf);
4645 return 0;
4646}
4647
4648/**
4649 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4650 * @phba: pointer to lpfc hba data structure.
4651 *
4652 * This routine is called to explicitly arm the SLI4 device's completion and
4653 * event queues
4654 **/
4655static void
4656lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4657{
4658 uint8_t fcp_eqidx;
4659
4660 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4661 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004662 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4663 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4664 LPFC_QUEUE_REARM);
4665 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4666 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4667 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4668 LPFC_QUEUE_REARM);
4669}
4670
4671/**
4672 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4673 * @phba: Pointer to HBA context object.
4674 *
4675 * This function is the main SLI4 device intialization PCI function. This
4676 * function is called by the HBA intialization code, HBA reset code and
4677 * HBA error attention handler code. Caller is not required to hold any
4678 * locks.
4679 **/
4680int
4681lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4682{
4683 int rc;
4684 LPFC_MBOXQ_t *mboxq;
4685 struct lpfc_mqe *mqe;
4686 uint8_t *vpd;
4687 uint32_t vpd_size;
4688 uint32_t ftr_rsp = 0;
4689 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4690 struct lpfc_vport *vport = phba->pport;
4691 struct lpfc_dmabuf *mp;
4692
James Smart2fcee4b2010-12-15 17:57:46 -05004693 /*
4694 * TODO: Why does this routine execute these task in a different
4695 * order from probe?
4696 */
James Smartda0436e2009-05-22 14:51:39 -04004697 /* Perform a PCI function reset to start from clean */
4698 rc = lpfc_pci_function_reset(phba);
4699 if (unlikely(rc))
4700 return -ENODEV;
4701
4702 /* Check the HBA Host Status Register for readyness */
4703 rc = lpfc_sli4_post_status_check(phba);
4704 if (unlikely(rc))
4705 return -ENODEV;
4706 else {
4707 spin_lock_irq(&phba->hbalock);
4708 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4709 spin_unlock_irq(&phba->hbalock);
4710 }
4711
4712 /*
4713 * Allocate a single mailbox container for initializing the
4714 * port.
4715 */
4716 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4717 if (!mboxq)
4718 return -ENOMEM;
4719
4720 /*
4721 * Continue initialization with default values even if driver failed
4722 * to read FCoE param config regions
4723 */
4724 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4725 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004726 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004727
4728 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004729 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004730 vpd = kzalloc(vpd_size, GFP_KERNEL);
4731 if (!vpd) {
4732 rc = -ENOMEM;
4733 goto out_free_mbox;
4734 }
4735
4736 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05004737 if (unlikely(rc)) {
4738 kfree(vpd);
4739 goto out_free_mbox;
4740 }
James Smartda0436e2009-05-22 14:51:39 -04004741 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004742 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4743 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05004744 phba->hba_flag |= HBA_FCOE_MODE;
4745 else
4746 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04004747
4748 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4749 LPFC_DCBX_CEE_MODE)
4750 phba->hba_flag |= HBA_FIP_SUPPORT;
4751 else
4752 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4753
James Smartf1126682009-06-10 17:22:44 -04004754 if (phba->sli_rev != LPFC_SLI_REV4 ||
James Smart76a95d72010-11-20 23:11:48 -05004755 !(phba->hba_flag & HBA_FCOE_MODE)) {
James Smartda0436e2009-05-22 14:51:39 -04004756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4757 "0376 READ_REV Error. SLI Level %d "
4758 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05004759 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04004760 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004761 kfree(vpd);
4762 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004763 }
James Smartda0436e2009-05-22 14:51:39 -04004764 /*
4765 * Evaluate the read rev and vpd data. Populate the driver
4766 * state with the results. If this routine fails, the failure
4767 * is not fatal as the driver will use generic values.
4768 */
4769 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4770 if (unlikely(!rc)) {
4771 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4772 "0377 Error %d parsing vpd. "
4773 "Using defaults.\n", rc);
4774 rc = 0;
4775 }
James Smart76a95d72010-11-20 23:11:48 -05004776 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04004777
James Smartf1126682009-06-10 17:22:44 -04004778 /* Save information as VPD data */
4779 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4780 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4781 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4782 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4783 &mqe->un.read_rev);
4784 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4785 &mqe->un.read_rev);
4786 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4787 &mqe->un.read_rev);
4788 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4789 &mqe->un.read_rev);
4790 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4791 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4792 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4793 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4794 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4795 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4796 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4797 "(%d):0380 READ_REV Status x%x "
4798 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4799 mboxq->vport ? mboxq->vport->vpi : 0,
4800 bf_get(lpfc_mqe_status, mqe),
4801 phba->vpd.rev.opFwName,
4802 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4803 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004804
4805 /*
4806 * Discover the port's supported feature set and match it against the
4807 * hosts requests.
4808 */
4809 lpfc_request_features(phba, mboxq);
4810 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4811 if (unlikely(rc)) {
4812 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004813 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004814 }
4815
4816 /*
4817 * The port must support FCP initiator mode as this is the
4818 * only mode running in the host.
4819 */
4820 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4821 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4822 "0378 No support for fcpi mode.\n");
4823 ftr_rsp++;
4824 }
4825
4826 /*
4827 * If the port cannot support the host's requested features
4828 * then turn off the global config parameters to disable the
4829 * feature in the driver. This is not a fatal error.
4830 */
4831 if ((phba->cfg_enable_bg) &&
4832 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4833 ftr_rsp++;
4834
4835 if (phba->max_vpi && phba->cfg_enable_npiv &&
4836 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4837 ftr_rsp++;
4838
4839 if (ftr_rsp) {
4840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4841 "0379 Feature Mismatch Data: x%08x %08x "
4842 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4843 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4844 phba->cfg_enable_npiv, phba->max_vpi);
4845 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4846 phba->cfg_enable_bg = 0;
4847 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4848 phba->cfg_enable_npiv = 0;
4849 }
4850
4851 /* These SLI3 features are assumed in SLI4 */
4852 spin_lock_irq(&phba->hbalock);
4853 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4854 spin_unlock_irq(&phba->hbalock);
4855
4856 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004857 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4858 if (rc) {
4859 phba->link_state = LPFC_HBA_ERROR;
4860 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05004861 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05004862 }
4863
James Smartda0436e2009-05-22 14:51:39 -04004864 mboxq->vport = vport;
4865 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4866 mp = (struct lpfc_dmabuf *) mboxq->context1;
4867 if (rc == MBX_SUCCESS) {
4868 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4869 rc = 0;
4870 }
4871
4872 /*
4873 * This memory was allocated by the lpfc_read_sparam routine. Release
4874 * it to the mbuf pool.
4875 */
4876 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4877 kfree(mp);
4878 mboxq->context1 = NULL;
4879 if (unlikely(rc)) {
4880 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4881 "0382 READ_SPARAM command failed "
4882 "status %d, mbxStatus x%x\n",
4883 rc, bf_get(lpfc_mqe_status, mqe));
4884 phba->link_state = LPFC_HBA_ERROR;
4885 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004886 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004887 }
4888
4889 if (phba->cfg_soft_wwnn)
4890 u64_to_wwn(phba->cfg_soft_wwnn,
4891 vport->fc_sparam.nodeName.u.wwn);
4892 if (phba->cfg_soft_wwpn)
4893 u64_to_wwn(phba->cfg_soft_wwpn,
4894 vport->fc_sparam.portName.u.wwn);
4895 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4896 sizeof(struct lpfc_name));
4897 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4898 sizeof(struct lpfc_name));
4899
4900 /* Update the fc_host data structures with new wwn. */
4901 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4902 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4903
4904 /* Register SGL pool to the device using non-embedded mailbox command */
4905 rc = lpfc_sli4_post_sgl_list(phba);
4906 if (unlikely(rc)) {
4907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004908 "0582 Error %d during sgl post operation\n",
4909 rc);
James Smartda0436e2009-05-22 14:51:39 -04004910 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004911 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004912 }
4913
4914 /* Register SCSI SGL pool to the device */
4915 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4916 if (unlikely(rc)) {
4917 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004918 "0383 Error %d during scsi sgl post "
4919 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004920 /* Some Scsi buffers were moved to the abort scsi list */
4921 /* A pci function reset will repost them */
4922 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004923 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004924 }
4925
4926 /* Post the rpi header region to the device. */
4927 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4928 if (unlikely(rc)) {
4929 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4930 "0393 Error %d during rpi post operation\n",
4931 rc);
4932 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004933 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004934 }
James Smartda0436e2009-05-22 14:51:39 -04004935
4936 /* Set up all the queues to the device */
4937 rc = lpfc_sli4_queue_setup(phba);
4938 if (unlikely(rc)) {
4939 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4940 "0381 Error %d during queue setup.\n ", rc);
4941 goto out_stop_timers;
4942 }
4943
4944 /* Arm the CQs and then EQs on device */
4945 lpfc_sli4_arm_cqeq_intr(phba);
4946
4947 /* Indicate device interrupt mode */
4948 phba->sli4_hba.intr_enable = 1;
4949
4950 /* Allow asynchronous mailbox command to go through */
4951 spin_lock_irq(&phba->hbalock);
4952 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4953 spin_unlock_irq(&phba->hbalock);
4954
4955 /* Post receive buffers to the device */
4956 lpfc_sli4_rb_setup(phba);
4957
James Smartfc2b9892010-02-26 14:15:29 -05004958 /* Reset HBA FCF states after HBA reset */
4959 phba->fcf.fcf_flag = 0;
4960 phba->fcf.current_rec.flag = 0;
4961
James Smartda0436e2009-05-22 14:51:39 -04004962 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004963 mod_timer(&vport->els_tmofunc,
4964 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004965
4966 /* Start heart beat timer */
4967 mod_timer(&phba->hb_tmofunc,
4968 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4969 phba->hb_outstanding = 0;
4970 phba->last_completion_time = jiffies;
4971
4972 /* Start error attention (ERATT) polling timer */
4973 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4974
James Smart75baf692010-06-08 18:31:21 -04004975 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4976 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4977 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4978 if (!rc) {
4979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4980 "2829 This device supports "
4981 "Advanced Error Reporting (AER)\n");
4982 spin_lock_irq(&phba->hbalock);
4983 phba->hba_flag |= HBA_AER_ENABLED;
4984 spin_unlock_irq(&phba->hbalock);
4985 } else {
4986 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4987 "2830 This device does not support "
4988 "Advanced Error Reporting (AER)\n");
4989 phba->cfg_aer_support = 0;
4990 }
4991 }
4992
James Smart76a95d72010-11-20 23:11:48 -05004993 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4994 /*
4995 * The FC Port needs to register FCFI (index 0)
4996 */
4997 lpfc_reg_fcfi(phba, mboxq);
4998 mboxq->vport = phba->pport;
4999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000 if (rc == MBX_SUCCESS)
5001 rc = 0;
5002 else
5003 goto out_unset_queue;
5004 }
James Smartda0436e2009-05-22 14:51:39 -04005005 /*
5006 * The port is ready, set the host's link state to LINK_DOWN
5007 * in preparation for link interrupts.
5008 */
James Smartda0436e2009-05-22 14:51:39 -04005009 spin_lock_irq(&phba->hbalock);
5010 phba->link_state = LPFC_LINK_DOWN;
5011 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05005012 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5013out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04005014 /* Unset all the queues set up in this routine when error out */
5015 if (rc)
5016 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005017out_stop_timers:
5018 if (rc)
5019 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04005020out_free_mbox:
5021 mempool_free(mboxq, phba->mbox_mem_pool);
5022 return rc;
5023}
James Smarte59058c2008-08-24 21:49:00 -04005024
5025/**
James Smart3621a712009-04-06 18:47:14 -04005026 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04005027 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05005028 *
James Smarte59058c2008-08-24 21:49:00 -04005029 * This is the callback function for mailbox timer. The mailbox
5030 * timer is armed when a new mailbox command is issued and the timer
5031 * is deleted when the mailbox complete. The function is called by
5032 * the kernel timer code when a mailbox does not complete within
5033 * expected time. This function wakes up the worker thread to
5034 * process the mailbox timeout and returns. All the processing is
5035 * done by the worker thread function lpfc_mbox_timeout_handler.
5036 **/
dea31012005-04-17 16:05:31 -05005037void
5038lpfc_mbox_timeout(unsigned long ptr)
5039{
James Smart92d7f7b2007-06-17 19:56:38 -05005040 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05005041 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05005042 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005043
James Smart2e0fef82007-06-17 19:56:36 -05005044 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005045 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005046 if (!tmo_posted)
5047 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5048 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5049
James Smart5e9d9b82008-06-14 22:52:53 -04005050 if (!tmo_posted)
5051 lpfc_worker_wake_up(phba);
5052 return;
dea31012005-04-17 16:05:31 -05005053}
5054
James Smarte59058c2008-08-24 21:49:00 -04005055
5056/**
James Smart3621a712009-04-06 18:47:14 -04005057 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04005058 * @phba: Pointer to HBA context object.
5059 *
5060 * This function is called from worker thread when a mailbox command times out.
5061 * The caller is not required to hold any locks. This function will reset the
5062 * HBA and recover all the pending commands.
5063 **/
dea31012005-04-17 16:05:31 -05005064void
5065lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5066{
James Smart2e0fef82007-06-17 19:56:36 -05005067 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04005068 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04005069 struct lpfc_sli *psli = &phba->sli;
5070 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005071
James Smarta257bf92009-04-06 18:48:10 -04005072 /* Check the pmbox pointer first. There is a race condition
5073 * between the mbox timeout handler getting executed in the
5074 * worklist and the mailbox actually completing. When this
5075 * race condition occurs, the mbox_active will be NULL.
5076 */
5077 spin_lock_irq(&phba->hbalock);
5078 if (pmbox == NULL) {
5079 lpfc_printf_log(phba, KERN_WARNING,
5080 LOG_MBOX | LOG_SLI,
5081 "0353 Active Mailbox cleared - mailbox timeout "
5082 "exiting\n");
5083 spin_unlock_irq(&phba->hbalock);
5084 return;
5085 }
5086
dea31012005-04-17 16:05:31 -05005087 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05005088 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005089 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005090 mb->mbxCommand,
5091 phba->pport->port_state,
5092 phba->sli.sli_flag,
5093 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04005094 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005095
James Smart1dcb58e2007-04-25 09:51:30 -04005096 /* Setting state unknown so lpfc_sli_abort_iocb_ring
5097 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5098 * it to fail all oustanding SCSI IO.
5099 */
James Smart2e0fef82007-06-17 19:56:36 -05005100 spin_lock_irq(&phba->pport->work_port_lock);
5101 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5102 spin_unlock_irq(&phba->pport->work_port_lock);
5103 spin_lock_irq(&phba->hbalock);
5104 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04005105 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005106 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04005107
5108 pring = &psli->ring[psli->fcp_ring];
5109 lpfc_sli_abort_iocb_ring(phba, pring);
5110
5111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005112 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04005113
5114 /* Reset the HBA device */
5115 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05005116}
5117
James Smarte59058c2008-08-24 21:49:00 -04005118/**
James Smart3772a992009-05-22 14:50:54 -04005119 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04005120 * @phba: Pointer to HBA context object.
5121 * @pmbox: Pointer to mailbox object.
5122 * @flag: Flag indicating how the mailbox need to be processed.
5123 *
5124 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04005125 * to submit a mailbox command to firmware with SLI-3 interface spec. This
5126 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04005127 * The mailbox command can be submitted in polling mode, in which case
5128 * this function will wait in a polling loop for the completion of the
5129 * mailbox.
5130 * If the mailbox is submitted in no_wait mode (not polling) the
5131 * function will submit the command and returns immediately without waiting
5132 * for the mailbox completion. The no_wait is supported only when HBA
5133 * is in SLI2/SLI3 mode - interrupts are enabled.
5134 * The SLI interface allows only one mailbox pending at a time. If the
5135 * mailbox is issued in polling mode and there is already a mailbox
5136 * pending, then the function will return an error. If the mailbox is issued
5137 * in NO_WAIT mode and there is a mailbox pending already, the function
5138 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5139 * The sli layer owns the mailbox object until the completion of mailbox
5140 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5141 * return codes the caller owns the mailbox command after the return of
5142 * the function.
5143 **/
James Smart3772a992009-05-22 14:50:54 -04005144static int
5145lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5146 uint32_t flag)
dea31012005-04-17 16:05:31 -05005147{
dea31012005-04-17 16:05:31 -05005148 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05005149 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005150 uint32_t status, evtctr;
5151 uint32_t ha_copy;
5152 int i;
James Smart09372822008-01-11 01:52:54 -05005153 unsigned long timeout;
dea31012005-04-17 16:05:31 -05005154 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04005155 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05005156 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04005157 int processing_queue = 0;
5158
5159 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5160 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04005161 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04005162 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04005163 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5164 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5165 return MBX_SUCCESS;
5166 }
James Smart58da1ff2008-04-07 10:15:56 -04005167 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04005168 pmbox = lpfc_mbox_get(phba);
5169 if (!pmbox) {
5170 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5171 return MBX_SUCCESS;
5172 }
5173 }
dea31012005-04-17 16:05:31 -05005174
James Smarted957682007-06-17 19:56:37 -05005175 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05005176 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05005177 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04005178 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05005179 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005180 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005181 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04005182 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05005183 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04005184 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05005185 }
5186 }
5187
Linas Vepstas8d63f372007-02-14 14:28:36 -06005188 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04005189 if (unlikely(pci_channel_offline(phba->pcidev))) {
5190 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5191 goto out_not_finished;
5192 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06005193
James Smarta257bf92009-04-06 18:48:10 -04005194 /* If HBA has a deferred error attention, fail the iocb. */
5195 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5196 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5197 goto out_not_finished;
5198 }
5199
dea31012005-04-17 16:05:31 -05005200 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05005201
James Smart3772a992009-05-22 14:50:54 -04005202 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05005203 status = MBX_SUCCESS;
5204
James Smart2e0fef82007-06-17 19:56:36 -05005205 if (phba->link_state == LPFC_HBA_ERROR) {
5206 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005207
5208 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5210 "(%d):0311 Mailbox command x%x cannot "
5211 "issue Data: x%x x%x\n",
5212 pmbox->vport ? pmbox->vport->vpi : 0,
5213 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005214 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005215 }
5216
James Smart92908312006-03-07 15:04:13 -05005217 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
5218 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05005219 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04005220 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5221 "(%d):2528 Mailbox command x%x cannot "
5222 "issue Data: x%x x%x\n",
5223 pmbox->vport ? pmbox->vport->vpi : 0,
5224 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005225 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05005226 }
5227
dea31012005-04-17 16:05:31 -05005228 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5229 /* Polling for a mbox command when another one is already active
5230 * is not allowed in SLI. Also, the driver must have established
5231 * SLI2 mode to queue and process multiple mbox commands.
5232 */
5233
5234 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05005235 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005236
5237 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5239 "(%d):2529 Mailbox command x%x "
5240 "cannot issue Data: x%x x%x\n",
5241 pmbox->vport ? pmbox->vport->vpi : 0,
5242 pmbox->u.mb.mbxCommand,
5243 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005244 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005245 }
5246
James Smart3772a992009-05-22 14:50:54 -04005247 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005248 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005249 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005250 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5251 "(%d):2530 Mailbox command x%x "
5252 "cannot issue Data: x%x x%x\n",
5253 pmbox->vport ? pmbox->vport->vpi : 0,
5254 pmbox->u.mb.mbxCommand,
5255 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005256 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005257 }
5258
dea31012005-04-17 16:05:31 -05005259 /* Another mailbox command is still being processed, queue this
5260 * command to be processed later.
5261 */
5262 lpfc_mbox_put(phba, pmbox);
5263
5264 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05005265 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005266 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05005267 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005268 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5269 mb->mbxCommand, phba->pport->port_state,
5270 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005271
5272 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05005273 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005274
James Smart858c9f62007-06-17 19:56:39 -05005275 if (pmbox->vport) {
5276 lpfc_debugfs_disc_trc(pmbox->vport,
5277 LPFC_DISC_TRC_MBOX_VPORT,
5278 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
5279 (uint32_t)mb->mbxCommand,
5280 mb->un.varWords[0], mb->un.varWords[1]);
5281 }
5282 else {
5283 lpfc_debugfs_disc_trc(phba->pport,
5284 LPFC_DISC_TRC_MBOX,
5285 "MBOX Bsy: cmd:x%x mb:x%x x%x",
5286 (uint32_t)mb->mbxCommand,
5287 mb->un.varWords[0], mb->un.varWords[1]);
5288 }
5289
James Smart2e0fef82007-06-17 19:56:36 -05005290 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05005291 }
5292
dea31012005-04-17 16:05:31 -05005293 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5294
5295 /* If we are not polling, we MUST be in SLI2 mode */
5296 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04005297 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05005298 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05005299 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005300 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005301 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005302 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5303 "(%d):2531 Mailbox command x%x "
5304 "cannot issue Data: x%x x%x\n",
5305 pmbox->vport ? pmbox->vport->vpi : 0,
5306 pmbox->u.mb.mbxCommand,
5307 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005308 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005309 }
5310 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04005311 mod_timer(&psli->mbox_tmo, (jiffies +
5312 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05005313 }
5314
5315 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05005316 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005317 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005318 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005319 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05005320 mb->mbxCommand, phba->pport->port_state,
5321 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005322
James Smart858c9f62007-06-17 19:56:39 -05005323 if (mb->mbxCommand != MBX_HEARTBEAT) {
5324 if (pmbox->vport) {
5325 lpfc_debugfs_disc_trc(pmbox->vport,
5326 LPFC_DISC_TRC_MBOX_VPORT,
5327 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5328 (uint32_t)mb->mbxCommand,
5329 mb->un.varWords[0], mb->un.varWords[1]);
5330 }
5331 else {
5332 lpfc_debugfs_disc_trc(phba->pport,
5333 LPFC_DISC_TRC_MBOX,
5334 "MBOX Send: cmd:x%x mb:x%x x%x",
5335 (uint32_t)mb->mbxCommand,
5336 mb->un.varWords[0], mb->un.varWords[1]);
5337 }
5338 }
5339
dea31012005-04-17 16:05:31 -05005340 psli->slistat.mbox_cmd++;
5341 evtctr = psli->slistat.mbox_event;
5342
5343 /* next set own bit for the adapter and copy over command word */
5344 mb->mbxOwner = OWN_CHIP;
5345
James Smart3772a992009-05-22 14:50:54 -04005346 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04005347 /* Populate mbox extension offset word. */
5348 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5349 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5350 = (uint8_t *)phba->mbox_ext
5351 - (uint8_t *)phba->mbox;
5352 }
5353
5354 /* Copy the mailbox extension data */
5355 if (pmbox->in_ext_byte_len && pmbox->context2) {
5356 lpfc_sli_pcimem_bcopy(pmbox->context2,
5357 (uint8_t *)phba->mbox_ext,
5358 pmbox->in_ext_byte_len);
5359 }
5360 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04005361 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005362 } else {
James Smart7a470272010-03-15 11:25:20 -04005363 /* Populate mbox extension offset word. */
5364 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5365 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5366 = MAILBOX_HBA_EXT_OFFSET;
5367
5368 /* Copy the mailbox extension data */
5369 if (pmbox->in_ext_byte_len && pmbox->context2) {
5370 lpfc_memcpy_to_slim(phba->MBslimaddr +
5371 MAILBOX_HBA_EXT_OFFSET,
5372 pmbox->context2, pmbox->in_ext_byte_len);
5373
5374 }
James Smart92908312006-03-07 15:04:13 -05005375 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05005376 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04005377 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005378 }
5379
5380 /* First copy mbox command data to HBA SLIM, skip past first
5381 word */
5382 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5383 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5384 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5385
5386 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005387 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005388 to_slim = phba->MBslimaddr;
5389 writel(ldata, to_slim);
5390 readl(to_slim); /* flush */
5391
5392 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5393 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005394 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005395 }
5396 }
5397
5398 wmb();
dea31012005-04-17 16:05:31 -05005399
5400 switch (flag) {
5401 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005402 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005403 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005404 /* Interrupt board to do it */
5405 writel(CA_MBATT, phba->CAregaddr);
5406 readl(phba->CAregaddr); /* flush */
5407 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005408 break;
5409
5410 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005411 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005412 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005413 /* Interrupt board to do it */
5414 writel(CA_MBATT, phba->CAregaddr);
5415 readl(phba->CAregaddr); /* flush */
5416
James Smart3772a992009-05-22 14:50:54 -04005417 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005418 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005419 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005420 word0 = le32_to_cpu(word0);
5421 } else {
5422 /* First read mbox status word */
5423 word0 = readl(phba->MBslimaddr);
5424 }
5425
5426 /* Read the HBA Host Attention Register */
5427 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005428 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5429 mb->mbxCommand) *
5430 1000) + jiffies;
5431 i = 0;
dea31012005-04-17 16:05:31 -05005432 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005433 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5434 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005435 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005436 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005437 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005438 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005439 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005440 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005441 }
5442
5443 /* Check if we took a mbox interrupt while we were
5444 polling */
5445 if (((word0 & OWN_CHIP) != OWN_CHIP)
5446 && (evtctr != psli->slistat.mbox_event))
5447 break;
5448
James Smart09372822008-01-11 01:52:54 -05005449 if (i++ > 10) {
5450 spin_unlock_irqrestore(&phba->hbalock,
5451 drvr_flag);
5452 msleep(1);
5453 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5454 }
dea31012005-04-17 16:05:31 -05005455
James Smart3772a992009-05-22 14:50:54 -04005456 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005457 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005458 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005459 word0 = le32_to_cpu(word0);
5460 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5461 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005462 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005463 /* Check real SLIM for any errors */
5464 slimword0 = readl(phba->MBslimaddr);
5465 slimmb = (MAILBOX_t *) & slimword0;
5466 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5467 && slimmb->mbxStatus) {
5468 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005469 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005470 word0 = slimword0;
5471 }
5472 }
5473 } else {
5474 /* First copy command data */
5475 word0 = readl(phba->MBslimaddr);
5476 }
5477 /* Read the HBA Host Attention Register */
5478 ha_copy = readl(phba->HAregaddr);
5479 }
5480
James Smart3772a992009-05-22 14:50:54 -04005481 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005482 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005483 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005484 /* Copy the mailbox extension data */
5485 if (pmbox->out_ext_byte_len && pmbox->context2) {
5486 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5487 pmbox->context2,
5488 pmbox->out_ext_byte_len);
5489 }
dea31012005-04-17 16:05:31 -05005490 } else {
5491 /* First copy command data */
5492 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5493 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005494 /* Copy the mailbox extension data */
5495 if (pmbox->out_ext_byte_len && pmbox->context2) {
5496 lpfc_memcpy_from_slim(pmbox->context2,
5497 phba->MBslimaddr +
5498 MAILBOX_HBA_EXT_OFFSET,
5499 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005500 }
5501 }
5502
5503 writel(HA_MBATT, phba->HAregaddr);
5504 readl(phba->HAregaddr); /* flush */
5505
5506 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5507 status = mb->mbxStatus;
5508 }
5509
James Smart2e0fef82007-06-17 19:56:36 -05005510 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5511 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005512
5513out_not_finished:
5514 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005515 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005516 lpfc_mbox_cmpl_put(phba, pmbox);
5517 }
5518 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005519}
5520
James Smarte59058c2008-08-24 21:49:00 -04005521/**
James Smartf1126682009-06-10 17:22:44 -04005522 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5523 * @phba: Pointer to HBA context object.
5524 *
5525 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5526 * the driver internal pending mailbox queue. It will then try to wait out the
5527 * possible outstanding mailbox command before return.
5528 *
5529 * Returns:
5530 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5531 * the outstanding mailbox command timed out.
5532 **/
5533static int
5534lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5535{
5536 struct lpfc_sli *psli = &phba->sli;
5537 uint8_t actcmd = MBX_HEARTBEAT;
5538 int rc = 0;
5539 unsigned long timeout;
5540
5541 /* Mark the asynchronous mailbox command posting as blocked */
5542 spin_lock_irq(&phba->hbalock);
5543 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5544 if (phba->sli.mbox_active)
5545 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5546 spin_unlock_irq(&phba->hbalock);
5547 /* Determine how long we might wait for the active mailbox
5548 * command to be gracefully completed by firmware.
5549 */
5550 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5551 jiffies;
5552 /* Wait for the outstnading mailbox command to complete */
5553 while (phba->sli.mbox_active) {
5554 /* Check active mailbox complete status every 2ms */
5555 msleep(2);
5556 if (time_after(jiffies, timeout)) {
5557 /* Timeout, marked the outstanding cmd not complete */
5558 rc = 1;
5559 break;
5560 }
5561 }
5562
5563 /* Can not cleanly block async mailbox command, fails it */
5564 if (rc) {
5565 spin_lock_irq(&phba->hbalock);
5566 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5567 spin_unlock_irq(&phba->hbalock);
5568 }
5569 return rc;
5570}
5571
5572/**
5573 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5574 * @phba: Pointer to HBA context object.
5575 *
5576 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5577 * commands from the driver internal pending mailbox queue. It makes sure
5578 * that there is no outstanding mailbox command before resuming posting
5579 * asynchronous mailbox commands. If, for any reason, there is outstanding
5580 * mailbox command, it will try to wait it out before resuming asynchronous
5581 * mailbox command posting.
5582 **/
5583static void
5584lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5585{
5586 struct lpfc_sli *psli = &phba->sli;
5587
5588 spin_lock_irq(&phba->hbalock);
5589 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5590 /* Asynchronous mailbox posting is not blocked, do nothing */
5591 spin_unlock_irq(&phba->hbalock);
5592 return;
5593 }
5594
5595 /* Outstanding synchronous mailbox command is guaranteed to be done,
5596 * successful or timeout, after timing-out the outstanding mailbox
5597 * command shall always be removed, so just unblock posting async
5598 * mailbox command and resume
5599 */
5600 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5601 spin_unlock_irq(&phba->hbalock);
5602
5603 /* wake up worker thread to post asynchronlous mailbox command */
5604 lpfc_worker_wake_up(phba);
5605}
5606
5607/**
James Smartda0436e2009-05-22 14:51:39 -04005608 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5609 * @phba: Pointer to HBA context object.
5610 * @mboxq: Pointer to mailbox object.
5611 *
5612 * The function posts a mailbox to the port. The mailbox is expected
5613 * to be comletely filled in and ready for the port to operate on it.
5614 * This routine executes a synchronous completion operation on the
5615 * mailbox by polling for its completion.
5616 *
5617 * The caller must not be holding any locks when calling this routine.
5618 *
5619 * Returns:
5620 * MBX_SUCCESS - mailbox posted successfully
5621 * Any of the MBX error values.
5622 **/
5623static int
5624lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5625{
5626 int rc = MBX_SUCCESS;
5627 unsigned long iflag;
5628 uint32_t db_ready;
5629 uint32_t mcqe_status;
5630 uint32_t mbx_cmnd;
5631 unsigned long timeout;
5632 struct lpfc_sli *psli = &phba->sli;
5633 struct lpfc_mqe *mb = &mboxq->u.mqe;
5634 struct lpfc_bmbx_create *mbox_rgn;
5635 struct dma_address *dma_address;
5636 struct lpfc_register bmbx_reg;
5637
5638 /*
5639 * Only one mailbox can be active to the bootstrap mailbox region
5640 * at a time and there is no queueing provided.
5641 */
5642 spin_lock_irqsave(&phba->hbalock, iflag);
5643 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5644 spin_unlock_irqrestore(&phba->hbalock, iflag);
5645 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5646 "(%d):2532 Mailbox command x%x (x%x) "
5647 "cannot issue Data: x%x x%x\n",
5648 mboxq->vport ? mboxq->vport->vpi : 0,
5649 mboxq->u.mb.mbxCommand,
5650 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5651 psli->sli_flag, MBX_POLL);
5652 return MBXERR_ERROR;
5653 }
5654 /* The server grabs the token and owns it until release */
5655 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5656 phba->sli.mbox_active = mboxq;
5657 spin_unlock_irqrestore(&phba->hbalock, iflag);
5658
5659 /*
5660 * Initialize the bootstrap memory region to avoid stale data areas
5661 * in the mailbox post. Then copy the caller's mailbox contents to
5662 * the bmbx mailbox region.
5663 */
5664 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5665 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5666 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5667 sizeof(struct lpfc_mqe));
5668
5669 /* Post the high mailbox dma address to the port and wait for ready. */
5670 dma_address = &phba->sli4_hba.bmbx.dma_address;
5671 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5672
5673 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5674 * 1000) + jiffies;
5675 do {
5676 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5677 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5678 if (!db_ready)
5679 msleep(2);
5680
5681 if (time_after(jiffies, timeout)) {
5682 rc = MBXERR_ERROR;
5683 goto exit;
5684 }
5685 } while (!db_ready);
5686
5687 /* Post the low mailbox dma address to the port. */
5688 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5689 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5690 * 1000) + jiffies;
5691 do {
5692 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5693 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5694 if (!db_ready)
5695 msleep(2);
5696
5697 if (time_after(jiffies, timeout)) {
5698 rc = MBXERR_ERROR;
5699 goto exit;
5700 }
5701 } while (!db_ready);
5702
5703 /*
5704 * Read the CQ to ensure the mailbox has completed.
5705 * If so, update the mailbox status so that the upper layers
5706 * can complete the request normally.
5707 */
5708 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5709 sizeof(struct lpfc_mqe));
5710 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5711 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5712 sizeof(struct lpfc_mcqe));
5713 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5714
5715 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5716 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5717 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5718 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005719 } else
5720 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005721
5722 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5723 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5724 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5725 " x%x x%x CQ: x%x x%x x%x x%x\n",
5726 mboxq->vport ? mboxq->vport->vpi : 0,
5727 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5728 bf_get(lpfc_mqe_status, mb),
5729 mb->un.mb_words[0], mb->un.mb_words[1],
5730 mb->un.mb_words[2], mb->un.mb_words[3],
5731 mb->un.mb_words[4], mb->un.mb_words[5],
5732 mb->un.mb_words[6], mb->un.mb_words[7],
5733 mb->un.mb_words[8], mb->un.mb_words[9],
5734 mb->un.mb_words[10], mb->un.mb_words[11],
5735 mb->un.mb_words[12], mboxq->mcqe.word0,
5736 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5737 mboxq->mcqe.trailer);
5738exit:
5739 /* We are holding the token, no needed for lock when release */
5740 spin_lock_irqsave(&phba->hbalock, iflag);
5741 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5742 phba->sli.mbox_active = NULL;
5743 spin_unlock_irqrestore(&phba->hbalock, iflag);
5744 return rc;
5745}
5746
5747/**
5748 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5749 * @phba: Pointer to HBA context object.
5750 * @pmbox: Pointer to mailbox object.
5751 * @flag: Flag indicating how the mailbox need to be processed.
5752 *
5753 * This function is called by discovery code and HBA management code to submit
5754 * a mailbox command to firmware with SLI-4 interface spec.
5755 *
5756 * Return codes the caller owns the mailbox command after the return of the
5757 * function.
5758 **/
5759static int
5760lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5761 uint32_t flag)
5762{
5763 struct lpfc_sli *psli = &phba->sli;
5764 unsigned long iflags;
5765 int rc;
5766
James Smart8fa38512009-07-19 10:01:03 -04005767 rc = lpfc_mbox_dev_check(phba);
5768 if (unlikely(rc)) {
5769 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5770 "(%d):2544 Mailbox command x%x (x%x) "
5771 "cannot issue Data: x%x x%x\n",
5772 mboxq->vport ? mboxq->vport->vpi : 0,
5773 mboxq->u.mb.mbxCommand,
5774 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5775 psli->sli_flag, flag);
5776 goto out_not_finished;
5777 }
5778
James Smartda0436e2009-05-22 14:51:39 -04005779 /* Detect polling mode and jump to a handler */
5780 if (!phba->sli4_hba.intr_enable) {
5781 if (flag == MBX_POLL)
5782 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5783 else
5784 rc = -EIO;
5785 if (rc != MBX_SUCCESS)
5786 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5787 "(%d):2541 Mailbox command x%x "
5788 "(x%x) cannot issue Data: x%x x%x\n",
5789 mboxq->vport ? mboxq->vport->vpi : 0,
5790 mboxq->u.mb.mbxCommand,
5791 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5792 psli->sli_flag, flag);
5793 return rc;
5794 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005795 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5796 "(%d):2542 Try to issue mailbox command "
5797 "x%x (x%x) synchronously ahead of async"
5798 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005799 mboxq->vport ? mboxq->vport->vpi : 0,
5800 mboxq->u.mb.mbxCommand,
5801 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5802 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005803 /* Try to block the asynchronous mailbox posting */
5804 rc = lpfc_sli4_async_mbox_block(phba);
5805 if (!rc) {
5806 /* Successfully blocked, now issue sync mbox cmd */
5807 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5808 if (rc != MBX_SUCCESS)
5809 lpfc_printf_log(phba, KERN_ERR,
5810 LOG_MBOX | LOG_SLI,
5811 "(%d):2597 Mailbox command "
5812 "x%x (x%x) cannot issue "
5813 "Data: x%x x%x\n",
5814 mboxq->vport ?
5815 mboxq->vport->vpi : 0,
5816 mboxq->u.mb.mbxCommand,
5817 lpfc_sli4_mbox_opcode_get(phba,
5818 mboxq),
5819 psli->sli_flag, flag);
5820 /* Unblock the async mailbox posting afterward */
5821 lpfc_sli4_async_mbox_unblock(phba);
5822 }
5823 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005824 }
5825
5826 /* Now, interrupt mode asynchrous mailbox command */
5827 rc = lpfc_mbox_cmd_check(phba, mboxq);
5828 if (rc) {
5829 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5830 "(%d):2543 Mailbox command x%x (x%x) "
5831 "cannot issue Data: x%x x%x\n",
5832 mboxq->vport ? mboxq->vport->vpi : 0,
5833 mboxq->u.mb.mbxCommand,
5834 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5835 psli->sli_flag, flag);
5836 goto out_not_finished;
5837 }
James Smartda0436e2009-05-22 14:51:39 -04005838
5839 /* Put the mailbox command to the driver internal FIFO */
5840 psli->slistat.mbox_busy++;
5841 spin_lock_irqsave(&phba->hbalock, iflags);
5842 lpfc_mbox_put(phba, mboxq);
5843 spin_unlock_irqrestore(&phba->hbalock, iflags);
5844 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5845 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5846 "x%x (x%x) x%x x%x x%x\n",
5847 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5848 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5849 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5850 phba->pport->port_state,
5851 psli->sli_flag, MBX_NOWAIT);
5852 /* Wake up worker thread to transport mailbox command from head */
5853 lpfc_worker_wake_up(phba);
5854
5855 return MBX_BUSY;
5856
5857out_not_finished:
5858 return MBX_NOT_FINISHED;
5859}
5860
5861/**
5862 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5863 * @phba: Pointer to HBA context object.
5864 *
5865 * This function is called by worker thread to send a mailbox command to
5866 * SLI4 HBA firmware.
5867 *
5868 **/
5869int
5870lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5871{
5872 struct lpfc_sli *psli = &phba->sli;
5873 LPFC_MBOXQ_t *mboxq;
5874 int rc = MBX_SUCCESS;
5875 unsigned long iflags;
5876 struct lpfc_mqe *mqe;
5877 uint32_t mbx_cmnd;
5878
5879 /* Check interrupt mode before post async mailbox command */
5880 if (unlikely(!phba->sli4_hba.intr_enable))
5881 return MBX_NOT_FINISHED;
5882
5883 /* Check for mailbox command service token */
5884 spin_lock_irqsave(&phba->hbalock, iflags);
5885 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5886 spin_unlock_irqrestore(&phba->hbalock, iflags);
5887 return MBX_NOT_FINISHED;
5888 }
5889 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5890 spin_unlock_irqrestore(&phba->hbalock, iflags);
5891 return MBX_NOT_FINISHED;
5892 }
5893 if (unlikely(phba->sli.mbox_active)) {
5894 spin_unlock_irqrestore(&phba->hbalock, iflags);
5895 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5896 "0384 There is pending active mailbox cmd\n");
5897 return MBX_NOT_FINISHED;
5898 }
5899 /* Take the mailbox command service token */
5900 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5901
5902 /* Get the next mailbox command from head of queue */
5903 mboxq = lpfc_mbox_get(phba);
5904
5905 /* If no more mailbox command waiting for post, we're done */
5906 if (!mboxq) {
5907 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5908 spin_unlock_irqrestore(&phba->hbalock, iflags);
5909 return MBX_SUCCESS;
5910 }
5911 phba->sli.mbox_active = mboxq;
5912 spin_unlock_irqrestore(&phba->hbalock, iflags);
5913
5914 /* Check device readiness for posting mailbox command */
5915 rc = lpfc_mbox_dev_check(phba);
5916 if (unlikely(rc))
5917 /* Driver clean routine will clean up pending mailbox */
5918 goto out_not_finished;
5919
5920 /* Prepare the mbox command to be posted */
5921 mqe = &mboxq->u.mqe;
5922 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5923
5924 /* Start timer for the mbox_tmo and log some mailbox post messages */
5925 mod_timer(&psli->mbox_tmo, (jiffies +
5926 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5927
5928 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5929 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5930 "x%x x%x\n",
5931 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5932 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5933 phba->pport->port_state, psli->sli_flag);
5934
5935 if (mbx_cmnd != MBX_HEARTBEAT) {
5936 if (mboxq->vport) {
5937 lpfc_debugfs_disc_trc(mboxq->vport,
5938 LPFC_DISC_TRC_MBOX_VPORT,
5939 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5940 mbx_cmnd, mqe->un.mb_words[0],
5941 mqe->un.mb_words[1]);
5942 } else {
5943 lpfc_debugfs_disc_trc(phba->pport,
5944 LPFC_DISC_TRC_MBOX,
5945 "MBOX Send: cmd:x%x mb:x%x x%x",
5946 mbx_cmnd, mqe->un.mb_words[0],
5947 mqe->un.mb_words[1]);
5948 }
5949 }
5950 psli->slistat.mbox_cmd++;
5951
5952 /* Post the mailbox command to the port */
5953 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5954 if (rc != MBX_SUCCESS) {
5955 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5956 "(%d):2533 Mailbox command x%x (x%x) "
5957 "cannot issue Data: x%x x%x\n",
5958 mboxq->vport ? mboxq->vport->vpi : 0,
5959 mboxq->u.mb.mbxCommand,
5960 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5961 psli->sli_flag, MBX_NOWAIT);
5962 goto out_not_finished;
5963 }
5964
5965 return rc;
5966
5967out_not_finished:
5968 spin_lock_irqsave(&phba->hbalock, iflags);
5969 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5970 __lpfc_mbox_cmpl_put(phba, mboxq);
5971 /* Release the token */
5972 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5973 phba->sli.mbox_active = NULL;
5974 spin_unlock_irqrestore(&phba->hbalock, iflags);
5975
5976 return MBX_NOT_FINISHED;
5977}
5978
5979/**
5980 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5981 * @phba: Pointer to HBA context object.
5982 * @pmbox: Pointer to mailbox object.
5983 * @flag: Flag indicating how the mailbox need to be processed.
5984 *
5985 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5986 * the API jump table function pointer from the lpfc_hba struct.
5987 *
5988 * Return codes the caller owns the mailbox command after the return of the
5989 * function.
5990 **/
5991int
5992lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5993{
5994 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5995}
5996
5997/**
5998 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5999 * @phba: The hba struct for which this call is being executed.
6000 * @dev_grp: The HBA PCI-Device group number.
6001 *
6002 * This routine sets up the mbox interface API function jump table in @phba
6003 * struct.
6004 * Returns: 0 - success, -ENODEV - failure.
6005 **/
6006int
6007lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6008{
6009
6010 switch (dev_grp) {
6011 case LPFC_PCI_DEV_LP:
6012 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6013 phba->lpfc_sli_handle_slow_ring_event =
6014 lpfc_sli_handle_slow_ring_event_s3;
6015 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6016 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6017 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6018 break;
6019 case LPFC_PCI_DEV_OC:
6020 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6021 phba->lpfc_sli_handle_slow_ring_event =
6022 lpfc_sli_handle_slow_ring_event_s4;
6023 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6024 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6025 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6026 break;
6027 default:
6028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6029 "1420 Invalid HBA PCI-device group: 0x%x\n",
6030 dev_grp);
6031 return -ENODEV;
6032 break;
6033 }
6034 return 0;
6035}
6036
6037/**
James Smart3621a712009-04-06 18:47:14 -04006038 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04006039 * @phba: Pointer to HBA context object.
6040 * @pring: Pointer to driver SLI ring object.
6041 * @piocb: Pointer to address of newly added command iocb.
6042 *
6043 * This function is called with hbalock held to add a command
6044 * iocb to the txq when SLI layer cannot submit the command iocb
6045 * to the ring.
6046 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006047void
James Smart92d7f7b2007-06-17 19:56:38 -05006048__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006049 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05006050{
6051 /* Insert the caller's iocb in the txq tail for later processing. */
6052 list_add_tail(&piocb->list, &pring->txq);
6053 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05006054}
6055
James Smarte59058c2008-08-24 21:49:00 -04006056/**
James Smart3621a712009-04-06 18:47:14 -04006057 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04006058 * @phba: Pointer to HBA context object.
6059 * @pring: Pointer to driver SLI ring object.
6060 * @piocb: Pointer to address of newly added command iocb.
6061 *
6062 * This function is called with hbalock held before a new
6063 * iocb is submitted to the firmware. This function checks
6064 * txq to flush the iocbs in txq to Firmware before
6065 * submitting new iocbs to the Firmware.
6066 * If there are iocbs in the txq which need to be submitted
6067 * to firmware, lpfc_sli_next_iocb returns the first element
6068 * of the txq after dequeuing it from txq.
6069 * If there is no iocb in the txq then the function will return
6070 * *piocb and *piocb is set to NULL. Caller needs to check
6071 * *piocb to find if there are more commands in the txq.
6072 **/
dea31012005-04-17 16:05:31 -05006073static struct lpfc_iocbq *
6074lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006075 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05006076{
6077 struct lpfc_iocbq * nextiocb;
6078
6079 nextiocb = lpfc_sli_ringtx_get(phba, pring);
6080 if (!nextiocb) {
6081 nextiocb = *piocb;
6082 *piocb = NULL;
6083 }
6084
6085 return nextiocb;
6086}
6087
James Smarte59058c2008-08-24 21:49:00 -04006088/**
James Smart3772a992009-05-22 14:50:54 -04006089 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006090 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04006091 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04006092 * @piocb: Pointer to command iocb.
6093 * @flag: Flag indicating if this command can be put into txq.
6094 *
James Smart3772a992009-05-22 14:50:54 -04006095 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6096 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6097 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6098 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6099 * this function allows only iocbs for posting buffers. This function finds
6100 * next available slot in the command ring and posts the command to the
6101 * available slot and writes the port attention register to request HBA start
6102 * processing new iocb. If there is no slot available in the ring and
6103 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6104 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04006105 *
James Smart3772a992009-05-22 14:50:54 -04006106 * This function is called with hbalock held. The function will return success
6107 * after it successfully submit the iocb to firmware or after adding to the
6108 * txq.
James Smarte59058c2008-08-24 21:49:00 -04006109 **/
James Smart98c9ea52007-10-27 13:37:33 -04006110static int
James Smart3772a992009-05-22 14:50:54 -04006111__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05006112 struct lpfc_iocbq *piocb, uint32_t flag)
6113{
6114 struct lpfc_iocbq *nextiocb;
6115 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04006116 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05006117
James Smart92d7f7b2007-06-17 19:56:38 -05006118 if (piocb->iocb_cmpl && (!piocb->vport) &&
6119 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6120 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6121 lpfc_printf_log(phba, KERN_ERR,
6122 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006123 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006124 piocb->iocb.ulpCommand);
6125 dump_stack();
6126 return IOCB_ERROR;
6127 }
6128
6129
Linas Vepstas8d63f372007-02-14 14:28:36 -06006130 /* If the PCI channel is in offline state, do not post iocbs. */
6131 if (unlikely(pci_channel_offline(phba->pcidev)))
6132 return IOCB_ERROR;
6133
James Smarta257bf92009-04-06 18:48:10 -04006134 /* If HBA has a deferred error attention, fail the iocb. */
6135 if (unlikely(phba->hba_flag & DEFER_ERATT))
6136 return IOCB_ERROR;
6137
dea31012005-04-17 16:05:31 -05006138 /*
6139 * We should never get an IOCB if we are in a < LINK_DOWN state
6140 */
James Smart2e0fef82007-06-17 19:56:36 -05006141 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05006142 return IOCB_ERROR;
6143
6144 /*
6145 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04006146 * outstanding event.
dea31012005-04-17 16:05:31 -05006147 */
James Smart0b727fe2007-10-27 13:37:25 -04006148 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05006149 goto iocb_busy;
6150
James Smart2e0fef82007-06-17 19:56:36 -05006151 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05006152 /*
James Smart2680eea2007-04-25 09:52:55 -04006153 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05006154 * can be issued if the link is not up.
6155 */
6156 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04006157 case CMD_GEN_REQUEST64_CR:
6158 case CMD_GEN_REQUEST64_CX:
6159 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6160 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04006161 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04006162 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6163 MENLO_TRANSPORT_TYPE))
6164
6165 goto iocb_busy;
6166 break;
dea31012005-04-17 16:05:31 -05006167 case CMD_QUE_RING_BUF_CN:
6168 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05006169 /*
6170 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6171 * completion, iocb_cmpl MUST be 0.
6172 */
6173 if (piocb->iocb_cmpl)
6174 piocb->iocb_cmpl = NULL;
6175 /*FALLTHROUGH*/
6176 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04006177 case CMD_CLOSE_XRI_CN:
6178 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05006179 break;
6180 default:
6181 goto iocb_busy;
6182 }
6183
6184 /*
6185 * For FCP commands, we must be in a state where we can process link
6186 * attention events.
6187 */
6188 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05006189 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05006190 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05006191 }
dea31012005-04-17 16:05:31 -05006192
dea31012005-04-17 16:05:31 -05006193 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6194 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6195 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6196
6197 if (iocb)
6198 lpfc_sli_update_ring(phba, pring);
6199 else
6200 lpfc_sli_update_full_ring(phba, pring);
6201
6202 if (!piocb)
6203 return IOCB_SUCCESS;
6204
6205 goto out_busy;
6206
6207 iocb_busy:
6208 pring->stats.iocb_cmd_delay++;
6209
6210 out_busy:
6211
6212 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05006213 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05006214 return IOCB_SUCCESS;
6215 }
6216
6217 return IOCB_BUSY;
6218}
6219
James Smart3772a992009-05-22 14:50:54 -04006220/**
James Smart4f774512009-05-22 14:52:35 -04006221 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6222 * @phba: Pointer to HBA context object.
6223 * @piocb: Pointer to command iocb.
6224 * @sglq: Pointer to the scatter gather queue object.
6225 *
6226 * This routine converts the bpl or bde that is in the IOCB
6227 * to a sgl list for the sli4 hardware. The physical address
6228 * of the bpl/bde is converted back to a virtual address.
6229 * If the IOCB contains a BPL then the list of BDE's is
6230 * converted to sli4_sge's. If the IOCB contains a single
6231 * BDE then it is converted to a single sli_sge.
6232 * The IOCB is still in cpu endianess so the contents of
6233 * the bpl can be used without byte swapping.
6234 *
6235 * Returns valid XRI = Success, NO_XRI = Failure.
6236**/
6237static uint16_t
6238lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6239 struct lpfc_sglq *sglq)
6240{
6241 uint16_t xritag = NO_XRI;
6242 struct ulp_bde64 *bpl = NULL;
6243 struct ulp_bde64 bde;
6244 struct sli4_sge *sgl = NULL;
6245 IOCB_t *icmd;
6246 int numBdes = 0;
6247 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05006248 uint32_t offset = 0; /* accumulated offset in the sg request list */
6249 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04006250
6251 if (!piocbq || !sglq)
6252 return xritag;
6253
6254 sgl = (struct sli4_sge *)sglq->sgl;
6255 icmd = &piocbq->iocb;
6256 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6257 numBdes = icmd->un.genreq64.bdl.bdeSize /
6258 sizeof(struct ulp_bde64);
6259 /* The addrHigh and addrLow fields within the IOCB
6260 * have not been byteswapped yet so there is no
6261 * need to swap them back.
6262 */
6263 bpl = (struct ulp_bde64 *)
6264 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6265
6266 if (!bpl)
6267 return xritag;
6268
6269 for (i = 0; i < numBdes; i++) {
6270 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05006271 sgl->addr_hi = bpl->addrHigh;
6272 sgl->addr_lo = bpl->addrLow;
6273
James Smart4f774512009-05-22 14:52:35 -04006274 if ((i+1) == numBdes)
6275 bf_set(lpfc_sli4_sge_last, sgl, 1);
6276 else
6277 bf_set(lpfc_sli4_sge_last, sgl, 0);
6278 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006279 /* swap the size field back to the cpu so we
6280 * can assign it to the sgl.
6281 */
6282 bde.tus.w = le32_to_cpu(bpl->tus.w);
6283 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05006284 /* The offsets in the sgl need to be accumulated
6285 * separately for the request and reply lists.
6286 * The request is always first, the reply follows.
6287 */
6288 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6289 /* add up the reply sg entries */
6290 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6291 inbound++;
6292 /* first inbound? reset the offset */
6293 if (inbound == 1)
6294 offset = 0;
6295 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6296 offset += bde.tus.f.bdeSize;
6297 }
James Smart4f774512009-05-22 14:52:35 -04006298 bpl++;
6299 sgl++;
6300 }
6301 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6302 /* The addrHigh and addrLow fields of the BDE have not
6303 * been byteswapped yet so they need to be swapped
6304 * before putting them in the sgl.
6305 */
6306 sgl->addr_hi =
6307 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6308 sgl->addr_lo =
6309 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04006310 bf_set(lpfc_sli4_sge_last, sgl, 1);
6311 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006312 sgl->sge_len =
6313 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04006314 }
6315 return sglq->sli4_xritag;
6316}
6317
6318/**
6319 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6320 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04006321 *
James Smarta93ff372010-10-22 11:06:08 -04006322 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04006323 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6324 * held.
James Smart4f774512009-05-22 14:52:35 -04006325 *
6326 * Return: index into SLI4 fast-path FCP queue index.
6327 **/
6328static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04006329lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04006330{
James Smart8fa38512009-07-19 10:01:03 -04006331 ++phba->fcp_qidx;
6332 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6333 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04006334
James Smart8fa38512009-07-19 10:01:03 -04006335 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04006336}
6337
6338/**
6339 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6340 * @phba: Pointer to HBA context object.
6341 * @piocb: Pointer to command iocb.
6342 * @wqe: Pointer to the work queue entry.
6343 *
6344 * This routine converts the iocb command to its Work Queue Entry
6345 * equivalent. The wqe pointer should not have any fields set when
6346 * this routine is called because it will memcpy over them.
6347 * This routine does not set the CQ_ID or the WQEC bits in the
6348 * wqe.
6349 *
6350 * Returns: 0 = Success, IOCB_ERROR = Failure.
6351 **/
6352static int
6353lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6354 union lpfc_wqe *wqe)
6355{
James Smart5ffc2662009-11-18 15:39:44 -05006356 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04006357 uint8_t ct = 0;
6358 uint32_t fip;
6359 uint32_t abort_tag;
6360 uint8_t command_type = ELS_COMMAND_NON_FIP;
6361 uint8_t cmnd;
6362 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04006363 uint16_t abrt_iotag;
6364 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04006365 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04006366 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05006367 int numBdes, i;
6368 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04006369
James Smart45ed1192009-10-02 15:17:02 -04006370 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04006371 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04006372 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04006373 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05006374 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04006375 command_type = ELS_COMMAND_FIP;
6376 else
6377 command_type = ELS_COMMAND_NON_FIP;
6378
James Smart4f774512009-05-22 14:52:35 -04006379 /* Some of the fields are in the right position already */
6380 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6381 abort_tag = (uint32_t) iocbq->iotag;
6382 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04006383 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04006384 /* words0-2 bpl convert bde */
6385 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05006386 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6387 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04006388 bpl = (struct ulp_bde64 *)
6389 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6390 if (!bpl)
6391 return IOCB_ERROR;
6392
6393 /* Should already be byte swapped. */
6394 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6395 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6396 /* swap the size field back to the cpu so we
6397 * can assign it to the sgl.
6398 */
6399 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006400 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6401 total_len = 0;
6402 for (i = 0; i < numBdes; i++) {
6403 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6404 total_len += bde.tus.f.bdeSize;
6405 }
James Smart4f774512009-05-22 14:52:35 -04006406 } else
James Smart5ffc2662009-11-18 15:39:44 -05006407 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006408
6409 iocbq->iocb.ulpIoTag = iocbq->iotag;
6410 cmnd = iocbq->iocb.ulpCommand;
6411
6412 switch (iocbq->iocb.ulpCommand) {
6413 case CMD_ELS_REQUEST64_CR:
6414 if (!iocbq->iocb.ulpLe) {
6415 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6416 "2007 Only Limited Edition cmd Format"
6417 " supported 0x%x\n",
6418 iocbq->iocb.ulpCommand);
6419 return IOCB_ERROR;
6420 }
James Smart5ffc2662009-11-18 15:39:44 -05006421 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006422 /* Els_reguest64 has a TMO */
6423 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6424 iocbq->iocb.ulpTimeout);
6425 /* Need a VF for word 4 set the vf bit*/
6426 bf_set(els_req64_vf, &wqe->els_req, 0);
6427 /* And a VFID for word 12 */
6428 bf_set(els_req64_vfid, &wqe->els_req, 0);
6429 /*
6430 * Set ct field to 3, indicates that the context_tag field
6431 * contains the FCFI and remote N_Port_ID is
6432 * in word 5.
6433 */
James Smart4f774512009-05-22 14:52:35 -04006434 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04006435 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6436 iocbq->iocb.ulpContext);
6437 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6438 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006439 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006440 if (command_type == ELS_COMMAND_FIP) {
6441 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6442 >> LPFC_FIP_ELS_ID_SHIFT);
6443 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006444 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6445 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6446 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6447 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6448 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6449 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006450 break;
James Smart5ffc2662009-11-18 15:39:44 -05006451 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04006452 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6453 iocbq->iocb.un.ulpWord[3]);
6454 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6455 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006456 /* The entire sequence is transmitted for this IOCB */
6457 xmit_len = total_len;
6458 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006459 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006460 /* word3 iocb=io_tag32 wqe=reserved */
6461 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04006462 /* word4 relative_offset memcpy */
6463 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006464 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6465 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6466 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6467 LPFC_WQE_IOD_WRITE);
6468 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6469 LPFC_WQE_LENLOC_WORD12);
6470 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006471 wqe->xmit_sequence.xmit_len = xmit_len;
6472 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006473 break;
6474 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04006475 /* word3 iocb=iotag32 wqe=seq_payload_len */
6476 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006477 /* word4 iocb=rsvd wqe=rsvd */
6478 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6479 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04006480 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006481 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04006482 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6483 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6484 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6485 LPFC_WQE_LENLOC_WORD3);
6486 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006487 break;
6488 case CMD_FCP_IWRITE64_CR:
6489 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04006490 /* word3 iocb=iotag wqe=payload_offset_len */
6491 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6492 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05006493 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04006494 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6495 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6496 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6497 iocbq->iocb.ulpFCP2Rcvy);
6498 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6499 /* Always open the exchange */
6500 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6501 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6502 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6503 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6504 LPFC_WQE_LENLOC_WORD4);
6505 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6506 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6507 break;
6508 case CMD_FCP_IREAD64_CR:
6509 /* word3 iocb=iotag wqe=payload_offset_len */
6510 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6511 wqe->fcp_iread.payload_offset_len =
6512 xmit_len + sizeof(struct fcp_rsp);
6513 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6514 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6515 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6516 iocbq->iocb.ulpFCP2Rcvy);
6517 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04006518 /* Always open the exchange */
6519 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04006520 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6521 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6522 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6523 LPFC_WQE_LENLOC_WORD4);
6524 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6525 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6526 break;
James Smartf1126682009-06-10 17:22:44 -04006527 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006528 /* word3 iocb=IO_TAG wqe=reserved */
6529 wqe->fcp_icmd.rsrvd3 = 0;
6530 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04006531 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04006532 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6533 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6534 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6535 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6536 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6537 LPFC_WQE_LENLOC_NONE);
6538 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006539 break;
6540 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05006541 /* For this command calculate the xmit length of the
6542 * request bde.
6543 */
6544 xmit_len = 0;
6545 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6546 sizeof(struct ulp_bde64);
6547 for (i = 0; i < numBdes; i++) {
6548 if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6549 break;
6550 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6551 xmit_len += bde.tus.f.bdeSize;
6552 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006553 /* word3 iocb=IO_TAG wqe=request_payload_len */
6554 wqe->gen_req.request_payload_len = xmit_len;
6555 /* word4 iocb=parameter wqe=relative_offset memcpy */
6556 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04006557 /* word6 context tag copied in memcpy */
6558 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6559 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6561 "2015 Invalid CT %x command 0x%x\n",
6562 ct, iocbq->iocb.ulpCommand);
6563 return IOCB_ERROR;
6564 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006565 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6566 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6567 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6568 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6569 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6570 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6571 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6572 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006573 command_type = OTHER_COMMAND;
6574 break;
6575 case CMD_XMIT_ELS_RSP64_CX:
6576 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006577 /* word3 iocb=iotag32 wqe=response_payload_len */
6578 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006579 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04006580 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04006581 /* word5 iocb=rsvd wge=did */
6582 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6583 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04006584 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6585 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6586 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6587 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6588 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04006589 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04006590 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006591 iocbq->vport->vpi + phba->vpi_base);
James Smartf0d9bcc2010-10-22 11:07:09 -04006592 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6593 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6594 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6595 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6596 LPFC_WQE_LENLOC_WORD3);
6597 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006598 command_type = OTHER_COMMAND;
6599 break;
6600 case CMD_CLOSE_XRI_CN:
6601 case CMD_ABORT_XRI_CN:
6602 case CMD_ABORT_XRI_CX:
6603 /* words 0-2 memcpy should be 0 rserved */
6604 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006605 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6606 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6607 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6608 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6609 } else
6610 fip = 0;
6611
6612 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006613 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006614 * The link is down, or the command was ELS_FIP
6615 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006616 * on the wire.
6617 */
6618 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6619 else
6620 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6621 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04006622 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6623 wqe->abort_cmd.rsrvd5 = 0;
6624 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006625 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6626 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006627 /*
6628 * The abort handler will send us CMD_ABORT_XRI_CN or
6629 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6630 */
James Smartf0d9bcc2010-10-22 11:07:09 -04006631 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6632 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6633 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6634 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04006635 cmnd = CMD_ABORT_XRI_CX;
6636 command_type = OTHER_COMMAND;
6637 xritag = 0;
6638 break;
James Smart6669f9b2009-10-02 15:16:45 -04006639 case CMD_XMIT_BLS_RSP64_CX:
6640 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6641 * we re-construct this WQE here based on information in
6642 * iocbq from scratch.
6643 */
6644 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006645 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006646 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006647 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6648 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6649 LPFC_ABTS_UNSOL_INT) {
6650 /* ABTS sent by initiator to CT exchange, the
6651 * RX_ID field will be filled with the newly
6652 * allocated responder XRI.
6653 */
6654 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6655 iocbq->sli4_xritag);
6656 } else {
6657 /* ABTS sent by responder to CT exchange, the
6658 * RX_ID field will be filled with the responder
6659 * RX_ID from ABTS.
6660 */
6661 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6662 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6663 }
James Smart6669f9b2009-10-02 15:16:45 -04006664 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6665 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6666 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6667 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04006668 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6669 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6670 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04006671 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6672 command_type = OTHER_COMMAND;
6673 break;
James Smart4f774512009-05-22 14:52:35 -04006674 case CMD_XRI_ABORTED_CX:
6675 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04006676 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6677 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6678 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6679 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6680 default:
6681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6682 "2014 Invalid command 0x%x\n",
6683 iocbq->iocb.ulpCommand);
6684 return IOCB_ERROR;
6685 break;
James Smart4f774512009-05-22 14:52:35 -04006686 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006687 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6688 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6689 wqe->generic.wqe_com.abort_tag = abort_tag;
6690 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6691 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6692 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6693 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04006694 return 0;
6695}
6696
6697/**
6698 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6699 * @phba: Pointer to HBA context object.
6700 * @ring_number: SLI ring number to issue iocb on.
6701 * @piocb: Pointer to command iocb.
6702 * @flag: Flag indicating if this command can be put into txq.
6703 *
6704 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6705 * an iocb command to an HBA with SLI-4 interface spec.
6706 *
6707 * This function is called with hbalock held. The function will return success
6708 * after it successfully submit the iocb to firmware or after adding to the
6709 * txq.
6710 **/
6711static int
6712__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6713 struct lpfc_iocbq *piocb, uint32_t flag)
6714{
6715 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006716 union lpfc_wqe wqe;
6717 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006718
6719 if (piocb->sli4_xritag == NO_XRI) {
6720 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006721 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006722 sglq = NULL;
6723 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006724 if (pring->txq_cnt) {
6725 if (!(flag & SLI_IOCB_RET_IOCB)) {
6726 __lpfc_sli_ringtx_put(phba,
6727 pring, piocb);
6728 return IOCB_SUCCESS;
6729 } else {
6730 return IOCB_BUSY;
6731 }
6732 } else {
James Smart19ca7602010-11-20 23:11:55 -05006733 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04006734 if (!sglq) {
6735 if (!(flag & SLI_IOCB_RET_IOCB)) {
6736 __lpfc_sli_ringtx_put(phba,
6737 pring,
6738 piocb);
6739 return IOCB_SUCCESS;
6740 } else
6741 return IOCB_BUSY;
6742 }
6743 }
James Smart4f774512009-05-22 14:52:35 -04006744 }
6745 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6746 sglq = NULL; /* These IO's already have an XRI and
6747 * a mapped sgl.
6748 */
6749 } else {
6750 /* This is a continuation of a commandi,(CX) so this
6751 * sglq is on the active list
6752 */
6753 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6754 if (!sglq)
6755 return IOCB_ERROR;
6756 }
6757
6758 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006759 piocb->sli4_xritag = sglq->sli4_xritag;
6760
6761 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006762 return IOCB_ERROR;
6763 }
6764
6765 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6766 return IOCB_ERROR;
6767
James Smart341af102010-01-26 23:07:37 -05006768 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6769 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006770 /*
6771 * For FCP command IOCB, get a new WQ index to distribute
6772 * WQE across the WQsr. On the other hand, for abort IOCB,
6773 * it carries the same WQ index to the original command
6774 * IOCB.
6775 */
James Smart341af102010-01-26 23:07:37 -05006776 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006777 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6778 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6779 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006780 return IOCB_ERROR;
6781 } else {
6782 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6783 return IOCB_ERROR;
6784 }
6785 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6786
6787 return 0;
6788}
6789
6790/**
James Smart3772a992009-05-22 14:50:54 -04006791 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6792 *
6793 * This routine wraps the actual lockless version for issusing IOCB function
6794 * pointer from the lpfc_hba struct.
6795 *
6796 * Return codes:
6797 * IOCB_ERROR - Error
6798 * IOCB_SUCCESS - Success
6799 * IOCB_BUSY - Busy
6800 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006801int
James Smart3772a992009-05-22 14:50:54 -04006802__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6803 struct lpfc_iocbq *piocb, uint32_t flag)
6804{
6805 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6806}
6807
6808/**
6809 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6810 * @phba: The hba struct for which this call is being executed.
6811 * @dev_grp: The HBA PCI-Device group number.
6812 *
6813 * This routine sets up the SLI interface API function jump table in @phba
6814 * struct.
6815 * Returns: 0 - success, -ENODEV - failure.
6816 **/
6817int
6818lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6819{
6820
6821 switch (dev_grp) {
6822 case LPFC_PCI_DEV_LP:
6823 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6824 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6825 break;
James Smart4f774512009-05-22 14:52:35 -04006826 case LPFC_PCI_DEV_OC:
6827 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6828 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6829 break;
James Smart3772a992009-05-22 14:50:54 -04006830 default:
6831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6832 "1419 Invalid HBA PCI-device group: 0x%x\n",
6833 dev_grp);
6834 return -ENODEV;
6835 break;
6836 }
6837 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6838 return 0;
6839}
James Smart92d7f7b2007-06-17 19:56:38 -05006840
James Smarte59058c2008-08-24 21:49:00 -04006841/**
James Smart3621a712009-04-06 18:47:14 -04006842 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006843 * @phba: Pointer to HBA context object.
6844 * @pring: Pointer to driver SLI ring object.
6845 * @piocb: Pointer to command iocb.
6846 * @flag: Flag indicating if this command can be put into txq.
6847 *
6848 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6849 * function. This function gets the hbalock and calls
6850 * __lpfc_sli_issue_iocb function and will return the error returned
6851 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6852 * functions which do not hold hbalock.
6853 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006854int
James Smart3772a992009-05-22 14:50:54 -04006855lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006856 struct lpfc_iocbq *piocb, uint32_t flag)
6857{
6858 unsigned long iflags;
6859 int rc;
6860
6861 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006862 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006863 spin_unlock_irqrestore(&phba->hbalock, iflags);
6864
6865 return rc;
6866}
6867
James Smarte59058c2008-08-24 21:49:00 -04006868/**
James Smart3621a712009-04-06 18:47:14 -04006869 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006870 * @phba: Pointer to HBA context object.
6871 *
6872 * This function is called while driver attaches with the
6873 * HBA to setup the extra ring. The extra ring is used
6874 * only when driver needs to support target mode functionality
6875 * or IP over FC functionalities.
6876 *
6877 * This function is called with no lock held.
6878 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006879static int
6880lpfc_extra_ring_setup( struct lpfc_hba *phba)
6881{
6882 struct lpfc_sli *psli;
6883 struct lpfc_sli_ring *pring;
6884
6885 psli = &phba->sli;
6886
6887 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006888
6889 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006890 pring = &psli->ring[psli->fcp_ring];
6891 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6892 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6893 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6894 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6895
James Smarta4bc3372006-12-02 13:34:16 -05006896 /* and give them to the extra ring */
6897 pring = &psli->ring[psli->extra_ring];
6898
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006899 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6900 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6901 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6902 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6903
6904 /* Setup default profile for this ring */
6905 pring->iotag_max = 4096;
6906 pring->num_mask = 1;
6907 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006908 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6909 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006910 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6911 return 0;
6912}
6913
James Smarte59058c2008-08-24 21:49:00 -04006914/**
James Smart3621a712009-04-06 18:47:14 -04006915 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006916 * @phba: Pointer to HBA context object.
6917 * @pring: Pointer to driver SLI ring object.
6918 * @iocbq: Pointer to iocb object.
6919 *
6920 * This function is called by the slow ring event handler
6921 * function when there is an ASYNC event iocb in the ring.
6922 * This function is called with no lock held.
6923 * Currently this function handles only temperature related
6924 * ASYNC events. The function decodes the temperature sensor
6925 * event message and posts events for the management applications.
6926 **/
James Smart98c9ea52007-10-27 13:37:33 -04006927static void
James Smart57127f12007-10-27 13:37:05 -04006928lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6929 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6930{
6931 IOCB_t *icmd;
6932 uint16_t evt_code;
6933 uint16_t temp;
6934 struct temp_event temp_event_data;
6935 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006936 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006937
6938 icmd = &iocbq->iocb;
6939 evt_code = icmd->un.asyncstat.evt_code;
6940 temp = icmd->ulpContext;
6941
6942 if ((evt_code != ASYNC_TEMP_WARN) &&
6943 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006944 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006945 lpfc_printf_log(phba,
6946 KERN_ERR,
6947 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006948 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006949 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006950 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6951 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6952 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6953 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006954 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006955 icmd->un.asyncstat.evt_code,
6956 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6957 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6958 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6959 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6960
James Smart57127f12007-10-27 13:37:05 -04006961 return;
6962 }
6963 temp_event_data.data = (uint32_t)temp;
6964 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6965 if (evt_code == ASYNC_TEMP_WARN) {
6966 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6967 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006968 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006969 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006970 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006971 "corrective action. temperature : %d Celsius\n",
6972 temp);
6973 }
6974 if (evt_code == ASYNC_TEMP_SAFE) {
6975 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6976 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006977 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006978 LOG_TEMP,
6979 "0340 Adapter temperature is OK now. "
6980 "temperature : %d Celsius\n",
6981 temp);
6982 }
6983
6984 /* Send temperature change event to applications */
6985 shost = lpfc_shost_from_vport(phba->pport);
6986 fc_host_post_vendor_event(shost, fc_get_event_number(),
6987 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006988 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006989
6990}
6991
6992
James Smarte59058c2008-08-24 21:49:00 -04006993/**
James Smart3621a712009-04-06 18:47:14 -04006994 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006995 * @phba: Pointer to HBA context object.
6996 *
6997 * lpfc_sli_setup sets up rings of the SLI interface with
6998 * number of iocbs per ring and iotags. This function is
6999 * called while driver attach to the HBA and before the
7000 * interrupts are enabled. So there is no need for locking.
7001 *
7002 * This function always returns 0.
7003 **/
dea31012005-04-17 16:05:31 -05007004int
7005lpfc_sli_setup(struct lpfc_hba *phba)
7006{
James Smarted957682007-06-17 19:56:37 -05007007 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05007008 struct lpfc_sli *psli = &phba->sli;
7009 struct lpfc_sli_ring *pring;
7010
7011 psli->num_rings = MAX_CONFIGURED_RINGS;
7012 psli->sli_flag = 0;
7013 psli->fcp_ring = LPFC_FCP_RING;
7014 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05007015 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05007016
James Bottomley604a3e32005-10-29 10:28:33 -05007017 psli->iocbq_lookup = NULL;
7018 psli->iocbq_lookup_len = 0;
7019 psli->last_iotag = 0;
7020
dea31012005-04-17 16:05:31 -05007021 for (i = 0; i < psli->num_rings; i++) {
7022 pring = &psli->ring[i];
7023 switch (i) {
7024 case LPFC_FCP_RING: /* ring 0 - FCP */
7025 /* numCiocb and numRiocb are used in config_port */
7026 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7027 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7028 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7029 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7030 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7031 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007032 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007033 SLI3_IOCB_CMD_SIZE :
7034 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007035 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007036 SLI3_IOCB_RSP_SIZE :
7037 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007038 pring->iotag_ctr = 0;
7039 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05007040 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05007041 pring->fast_iotag = pring->iotag_max;
7042 pring->num_mask = 0;
7043 break;
James Smarta4bc3372006-12-02 13:34:16 -05007044 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05007045 /* numCiocb and numRiocb are used in config_port */
7046 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7047 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007048 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007049 SLI3_IOCB_CMD_SIZE :
7050 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007051 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007052 SLI3_IOCB_RSP_SIZE :
7053 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05007054 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05007055 pring->num_mask = 0;
7056 break;
7057 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
7058 /* numCiocb and numRiocb are used in config_port */
7059 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7060 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007061 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007062 SLI3_IOCB_CMD_SIZE :
7063 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007064 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007065 SLI3_IOCB_RSP_SIZE :
7066 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007067 pring->fast_iotag = 0;
7068 pring->iotag_ctr = 0;
7069 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04007070 pring->lpfc_sli_rcv_async_status =
7071 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04007072 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05007073 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04007074 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7075 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007076 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007077 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007078 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04007079 pring->prt[1].rctl = FC_RCTL_ELS_REP;
7080 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007081 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007082 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007083 pring->prt[2].profile = 0; /* Mask 2 */
7084 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04007085 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05007086 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007087 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007088 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007089 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05007090 pring->prt[3].profile = 0; /* Mask 3 */
7091 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04007092 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05007093 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007094 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007095 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007096 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04007097 /* abort unsolicited sequence */
7098 pring->prt[4].profile = 0; /* Mask 4 */
7099 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7100 pring->prt[4].type = FC_TYPE_BLS;
7101 pring->prt[4].lpfc_sli_rcv_unsol_event =
7102 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05007103 break;
7104 }
James Smarted957682007-06-17 19:56:37 -05007105 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05007106 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05007107 }
James Smarted957682007-06-17 19:56:37 -05007108 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05007109 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04007110 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7111 "SLI2 SLIM Data: x%x x%lx\n",
7112 phba->brd_no, totiocbsize,
7113 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05007114 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007115 if (phba->cfg_multi_ring_support == 2)
7116 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05007117
7118 return 0;
7119}
7120
James Smarte59058c2008-08-24 21:49:00 -04007121/**
James Smart3621a712009-04-06 18:47:14 -04007122 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04007123 * @phba: Pointer to HBA context object.
7124 *
7125 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7126 * ring. This function also initializes ring indices of each ring.
7127 * This function is called during the initialization of the SLI
7128 * interface of an HBA.
7129 * This function is called with no lock held and always returns
7130 * 1.
7131 **/
dea31012005-04-17 16:05:31 -05007132int
James Smart2e0fef82007-06-17 19:56:36 -05007133lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007134{
7135 struct lpfc_sli *psli;
7136 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05007137 int i;
dea31012005-04-17 16:05:31 -05007138
7139 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05007140 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007141 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05007142 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05007143 /* Initialize list headers for txq and txcmplq as double linked lists */
7144 for (i = 0; i < psli->num_rings; i++) {
7145 pring = &psli->ring[i];
7146 pring->ringno = i;
7147 pring->next_cmdidx = 0;
7148 pring->local_getidx = 0;
7149 pring->cmdidx = 0;
7150 INIT_LIST_HEAD(&pring->txq);
7151 INIT_LIST_HEAD(&pring->txcmplq);
7152 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05007153 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05007154 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05007155 }
James Smart2e0fef82007-06-17 19:56:36 -05007156 spin_unlock_irq(&phba->hbalock);
7157 return 1;
dea31012005-04-17 16:05:31 -05007158}
7159
James Smarte59058c2008-08-24 21:49:00 -04007160/**
James Smart04c68492009-05-22 14:52:52 -04007161 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7162 * @phba: Pointer to HBA context object.
7163 *
7164 * This routine flushes the mailbox command subsystem. It will unconditionally
7165 * flush all the mailbox commands in the three possible stages in the mailbox
7166 * command sub-system: pending mailbox command queue; the outstanding mailbox
7167 * command; and completed mailbox command queue. It is caller's responsibility
7168 * to make sure that the driver is in the proper state to flush the mailbox
7169 * command sub-system. Namely, the posting of mailbox commands into the
7170 * pending mailbox command queue from the various clients must be stopped;
7171 * either the HBA is in a state that it will never works on the outstanding
7172 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7173 * mailbox command has been completed.
7174 **/
7175static void
7176lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7177{
7178 LIST_HEAD(completions);
7179 struct lpfc_sli *psli = &phba->sli;
7180 LPFC_MBOXQ_t *pmb;
7181 unsigned long iflag;
7182
7183 /* Flush all the mailbox commands in the mbox system */
7184 spin_lock_irqsave(&phba->hbalock, iflag);
7185 /* The pending mailbox command queue */
7186 list_splice_init(&phba->sli.mboxq, &completions);
7187 /* The outstanding active mailbox command */
7188 if (psli->mbox_active) {
7189 list_add_tail(&psli->mbox_active->list, &completions);
7190 psli->mbox_active = NULL;
7191 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7192 }
7193 /* The completed mailbox command queue */
7194 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7195 spin_unlock_irqrestore(&phba->hbalock, iflag);
7196
7197 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7198 while (!list_empty(&completions)) {
7199 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7200 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7201 if (pmb->mbox_cmpl)
7202 pmb->mbox_cmpl(phba, pmb);
7203 }
7204}
7205
7206/**
James Smart3621a712009-04-06 18:47:14 -04007207 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04007208 * @vport: Pointer to virtual port object.
7209 *
7210 * lpfc_sli_host_down is called to clean up the resources
7211 * associated with a vport before destroying virtual
7212 * port data structures.
7213 * This function does following operations:
7214 * - Free discovery resources associated with this virtual
7215 * port.
7216 * - Free iocbs associated with this virtual port in
7217 * the txq.
7218 * - Send abort for all iocb commands associated with this
7219 * vport in txcmplq.
7220 *
7221 * This function is called with no lock held and always returns 1.
7222 **/
dea31012005-04-17 16:05:31 -05007223int
James Smart92d7f7b2007-06-17 19:56:38 -05007224lpfc_sli_host_down(struct lpfc_vport *vport)
7225{
James Smart858c9f62007-06-17 19:56:39 -05007226 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007227 struct lpfc_hba *phba = vport->phba;
7228 struct lpfc_sli *psli = &phba->sli;
7229 struct lpfc_sli_ring *pring;
7230 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05007231 int i;
7232 unsigned long flags = 0;
7233 uint16_t prev_pring_flag;
7234
7235 lpfc_cleanup_discovery_resources(vport);
7236
7237 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007238 for (i = 0; i < psli->num_rings; i++) {
7239 pring = &psli->ring[i];
7240 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04007241 /* Only slow rings */
7242 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007243 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007244 /* Set the lpfc data pending flag */
7245 set_bit(LPFC_DATA_READY, &phba->data_flags);
7246 }
James Smart92d7f7b2007-06-17 19:56:38 -05007247 /*
7248 * Error everything on the txq since these iocbs have not been
7249 * given to the FW yet.
7250 */
James Smart92d7f7b2007-06-17 19:56:38 -05007251 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7252 if (iocb->vport != vport)
7253 continue;
James Smart858c9f62007-06-17 19:56:39 -05007254 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007255 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05007256 }
7257
7258 /* Next issue ABTS for everything on the txcmplq */
7259 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7260 list) {
7261 if (iocb->vport != vport)
7262 continue;
7263 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7264 }
7265
7266 pring->flag = prev_pring_flag;
7267 }
7268
7269 spin_unlock_irqrestore(&phba->hbalock, flags);
7270
James Smarta257bf92009-04-06 18:48:10 -04007271 /* Cancel all the IOCBs from the completions list */
7272 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7273 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05007274 return 1;
7275}
7276
James Smarte59058c2008-08-24 21:49:00 -04007277/**
James Smart3621a712009-04-06 18:47:14 -04007278 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04007279 * @phba: Pointer to HBA context object.
7280 *
7281 * This function cleans up all iocb, buffers, mailbox commands
7282 * while shutting down the HBA. This function is called with no
7283 * lock held and always returns 1.
7284 * This function does the following to cleanup driver resources:
7285 * - Free discovery resources for each virtual port
7286 * - Cleanup any pending fabric iocbs
7287 * - Iterate through the iocb txq and free each entry
7288 * in the list.
7289 * - Free up any buffer posted to the HBA
7290 * - Free mailbox commands in the mailbox queue.
7291 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007292int
James Smart2e0fef82007-06-17 19:56:36 -05007293lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007294{
James Smart2534ba72007-04-25 09:52:20 -04007295 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05007296 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007297 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05007298 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05007299 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04007300 int i;
7301
7302 /* Shutdown the mailbox command sub-system */
7303 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05007304
dea31012005-04-17 16:05:31 -05007305 lpfc_hba_down_prep(phba);
7306
James Smart92d7f7b2007-06-17 19:56:38 -05007307 lpfc_fabric_abort_hba(phba);
7308
James Smart2e0fef82007-06-17 19:56:36 -05007309 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007310 for (i = 0; i < psli->num_rings; i++) {
7311 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04007312 /* Only slow rings */
7313 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007314 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007315 /* Set the lpfc data pending flag */
7316 set_bit(LPFC_DATA_READY, &phba->data_flags);
7317 }
dea31012005-04-17 16:05:31 -05007318
7319 /*
7320 * Error everything on the txq since these iocbs have not been
7321 * given to the FW yet.
7322 */
James Smart2534ba72007-04-25 09:52:20 -04007323 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05007324 pring->txq_cnt = 0;
7325
dea31012005-04-17 16:05:31 -05007326 }
James Smart2e0fef82007-06-17 19:56:36 -05007327 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007328
James Smarta257bf92009-04-06 18:48:10 -04007329 /* Cancel all the IOCBs from the completions list */
7330 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7331 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04007332
James Smart0ff10d42008-01-11 01:52:36 -05007333 spin_lock_irqsave(&phba->hbalock, flags);
7334 list_splice_init(&phba->elsbuf, &completions);
7335 phba->elsbuf_cnt = 0;
7336 phba->elsbuf_prev_cnt = 0;
7337 spin_unlock_irqrestore(&phba->hbalock, flags);
7338
7339 while (!list_empty(&completions)) {
7340 list_remove_head(&completions, buf_ptr,
7341 struct lpfc_dmabuf, list);
7342 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7343 kfree(buf_ptr);
7344 }
7345
dea31012005-04-17 16:05:31 -05007346 /* Return any active mbox cmds */
7347 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05007348
James Smartda0436e2009-05-22 14:51:39 -04007349 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007350 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04007351 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007352
James Smartda0436e2009-05-22 14:51:39 -04007353 return 1;
7354}
James Smart92d7f7b2007-06-17 19:56:38 -05007355
James Smartda0436e2009-05-22 14:51:39 -04007356/**
James Smart3621a712009-04-06 18:47:14 -04007357 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04007358 * @srcp: Source memory pointer.
7359 * @destp: Destination memory pointer.
7360 * @cnt: Number of words required to be copied.
7361 *
7362 * This function is used for copying data between driver memory
7363 * and the SLI memory. This function also changes the endianness
7364 * of each word if native endianness is different from SLI
7365 * endianness. This function can be called with or without
7366 * lock.
7367 **/
dea31012005-04-17 16:05:31 -05007368void
7369lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7370{
7371 uint32_t *src = srcp;
7372 uint32_t *dest = destp;
7373 uint32_t ldata;
7374 int i;
7375
7376 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7377 ldata = *src;
7378 ldata = le32_to_cpu(ldata);
7379 *dest = ldata;
7380 src++;
7381 dest++;
7382 }
7383}
7384
James Smarte59058c2008-08-24 21:49:00 -04007385
7386/**
James Smarta0c87cb2009-07-19 10:01:10 -04007387 * lpfc_sli_bemem_bcopy - SLI memory copy function
7388 * @srcp: Source memory pointer.
7389 * @destp: Destination memory pointer.
7390 * @cnt: Number of words required to be copied.
7391 *
7392 * This function is used for copying data between a data structure
7393 * with big endian representation to local endianness.
7394 * This function can be called with or without lock.
7395 **/
7396void
7397lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7398{
7399 uint32_t *src = srcp;
7400 uint32_t *dest = destp;
7401 uint32_t ldata;
7402 int i;
7403
7404 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7405 ldata = *src;
7406 ldata = be32_to_cpu(ldata);
7407 *dest = ldata;
7408 src++;
7409 dest++;
7410 }
7411}
7412
7413/**
James Smart3621a712009-04-06 18:47:14 -04007414 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04007415 * @phba: Pointer to HBA context object.
7416 * @pring: Pointer to driver SLI ring object.
7417 * @mp: Pointer to driver buffer object.
7418 *
7419 * This function is called with no lock held.
7420 * It always return zero after adding the buffer to the postbufq
7421 * buffer list.
7422 **/
dea31012005-04-17 16:05:31 -05007423int
James Smart2e0fef82007-06-17 19:56:36 -05007424lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7425 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007426{
7427 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7428 later */
James Smart2e0fef82007-06-17 19:56:36 -05007429 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007430 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007431 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007432 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007433 return 0;
7434}
7435
James Smarte59058c2008-08-24 21:49:00 -04007436/**
James Smart3621a712009-04-06 18:47:14 -04007437 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007438 * @phba: Pointer to HBA context object.
7439 *
7440 * When HBQ is enabled, buffers are searched based on tags. This function
7441 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7442 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7443 * does not conflict with tags of buffer posted for unsolicited events.
7444 * The function returns the allocated tag. The function is called with
7445 * no locks held.
7446 **/
James Smart76bb24e2007-10-27 13:38:00 -04007447uint32_t
7448lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7449{
7450 spin_lock_irq(&phba->hbalock);
7451 phba->buffer_tag_count++;
7452 /*
7453 * Always set the QUE_BUFTAG_BIT to distiguish between
7454 * a tag assigned by HBQ.
7455 */
7456 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7457 spin_unlock_irq(&phba->hbalock);
7458 return phba->buffer_tag_count;
7459}
7460
James Smarte59058c2008-08-24 21:49:00 -04007461/**
James Smart3621a712009-04-06 18:47:14 -04007462 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007463 * @phba: Pointer to HBA context object.
7464 * @pring: Pointer to driver SLI ring object.
7465 * @tag: Buffer tag.
7466 *
7467 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7468 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7469 * iocb is posted to the response ring with the tag of the buffer.
7470 * This function searches the pring->postbufq list using the tag
7471 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7472 * iocb. If the buffer is found then lpfc_dmabuf object of the
7473 * buffer is returned to the caller else NULL is returned.
7474 * This function is called with no lock held.
7475 **/
James Smart76bb24e2007-10-27 13:38:00 -04007476struct lpfc_dmabuf *
7477lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7478 uint32_t tag)
7479{
7480 struct lpfc_dmabuf *mp, *next_mp;
7481 struct list_head *slp = &pring->postbufq;
7482
7483 /* Search postbufq, from the begining, looking for a match on tag */
7484 spin_lock_irq(&phba->hbalock);
7485 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7486 if (mp->buffer_tag == tag) {
7487 list_del_init(&mp->list);
7488 pring->postbufq_cnt--;
7489 spin_unlock_irq(&phba->hbalock);
7490 return mp;
7491 }
7492 }
7493
7494 spin_unlock_irq(&phba->hbalock);
7495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007496 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007497 "ring %d Data x%lx x%p x%p x%x\n",
7498 pring->ringno, (unsigned long) tag,
7499 slp->next, slp->prev, pring->postbufq_cnt);
7500
7501 return NULL;
7502}
dea31012005-04-17 16:05:31 -05007503
James Smarte59058c2008-08-24 21:49:00 -04007504/**
James Smart3621a712009-04-06 18:47:14 -04007505 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007506 * @phba: Pointer to HBA context object.
7507 * @pring: Pointer to driver SLI ring object.
7508 * @phys: DMA address of the buffer.
7509 *
7510 * This function searches the buffer list using the dma_address
7511 * of unsolicited event to find the driver's lpfc_dmabuf object
7512 * corresponding to the dma_address. The function returns the
7513 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7514 * This function is called by the ct and els unsolicited event
7515 * handlers to get the buffer associated with the unsolicited
7516 * event.
7517 *
7518 * This function is called with no lock held.
7519 **/
dea31012005-04-17 16:05:31 -05007520struct lpfc_dmabuf *
7521lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7522 dma_addr_t phys)
7523{
7524 struct lpfc_dmabuf *mp, *next_mp;
7525 struct list_head *slp = &pring->postbufq;
7526
7527 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007528 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007529 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7530 if (mp->phys == phys) {
7531 list_del_init(&mp->list);
7532 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007533 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007534 return mp;
7535 }
7536 }
7537
James Smart2e0fef82007-06-17 19:56:36 -05007538 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007540 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007541 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007542 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007543 slp->next, slp->prev, pring->postbufq_cnt);
7544 return NULL;
7545}
7546
James Smarte59058c2008-08-24 21:49:00 -04007547/**
James Smart3621a712009-04-06 18:47:14 -04007548 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007549 * @phba: Pointer to HBA context object.
7550 * @cmdiocb: Pointer to driver command iocb object.
7551 * @rspiocb: Pointer to driver response iocb object.
7552 *
7553 * This function is the completion handler for the abort iocbs for
7554 * ELS commands. This function is called from the ELS ring event
7555 * handler with no lock held. This function frees memory resources
7556 * associated with the abort iocb.
7557 **/
dea31012005-04-17 16:05:31 -05007558static void
James Smart2e0fef82007-06-17 19:56:36 -05007559lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7560 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007561{
James Smart2e0fef82007-06-17 19:56:36 -05007562 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007563 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007564 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007565 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7566
7567 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007568
7569 if (irsp->ulpStatus) {
7570 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7571 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7572
James Smart2e0fef82007-06-17 19:56:36 -05007573 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007574 if (phba->sli_rev < LPFC_SLI_REV4) {
7575 if (abort_iotag != 0 &&
7576 abort_iotag <= phba->sli.last_iotag)
7577 abort_iocb =
7578 phba->sli.iocbq_lookup[abort_iotag];
7579 } else
7580 /* For sli4 the abort_tag is the XRI,
7581 * so the abort routine puts the iotag of the iocb
7582 * being aborted in the context field of the abort
7583 * IOCB.
7584 */
7585 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007586
James Smart2680eea2007-04-25 09:52:55 -04007587 /*
James Smart58da1ff2008-04-07 10:15:56 -04007588 * If the iocb is not found in Firmware queue the iocb
7589 * might have completed already. Do not free it again.
7590 */
James Smart9b379602008-04-07 10:16:00 -04007591 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007592 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7593 spin_unlock_irq(&phba->hbalock);
7594 lpfc_sli_release_iocbq(phba, cmdiocb);
7595 return;
7596 }
7597 /* For SLI4 the ulpContext field for abort IOCB
7598 * holds the iotag of the IOCB being aborted so
7599 * the local abort_context needs to be reset to
7600 * match the aborted IOCBs ulpContext.
7601 */
7602 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7603 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007604 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007605
7606 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7607 "0327 Cannot abort els iocb %p "
7608 "with tag %x context %x, abort status %x, "
7609 "abort code %x\n",
7610 abort_iocb, abort_iotag, abort_context,
7611 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007612 /*
James Smart2680eea2007-04-25 09:52:55 -04007613 * make sure we have the right iocbq before taking it
7614 * off the txcmplq and try to call completion routine.
7615 */
James Smart2e0fef82007-06-17 19:56:36 -05007616 if (!abort_iocb ||
7617 abort_iocb->iocb.ulpContext != abort_context ||
7618 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7619 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007620 else if (phba->sli_rev < LPFC_SLI_REV4) {
7621 /*
7622 * leave the SLI4 aborted command on the txcmplq
7623 * list and the command complete WCQE's XB bit
7624 * will tell whether the SGL (XRI) can be released
7625 * immediately or to the aborted SGL list for the
7626 * following abort XRI from the HBA.
7627 */
James Smart92d7f7b2007-06-17 19:56:38 -05007628 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007629 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7630 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7631 pring->txcmplq_cnt--;
7632 }
James Smart2680eea2007-04-25 09:52:55 -04007633
James Smart0ff10d42008-01-11 01:52:36 -05007634 /* Firmware could still be in progress of DMAing
7635 * payload, so don't free data buffer till after
7636 * a hbeat.
7637 */
7638 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007639 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007640 spin_unlock_irq(&phba->hbalock);
7641
James Smart92d7f7b2007-06-17 19:56:38 -05007642 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007643 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007644 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007645 } else
7646 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007647 }
7648
James Bottomley604a3e32005-10-29 10:28:33 -05007649 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007650 return;
7651}
7652
James Smarte59058c2008-08-24 21:49:00 -04007653/**
James Smart3621a712009-04-06 18:47:14 -04007654 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007655 * @phba: Pointer to HBA context object.
7656 * @cmdiocb: Pointer to driver command iocb object.
7657 * @rspiocb: Pointer to driver response iocb object.
7658 *
7659 * The function is called from SLI ring event handler with no
7660 * lock held. This function is the completion handler for ELS commands
7661 * which are aborted. The function frees memory resources used for
7662 * the aborted ELS commands.
7663 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007664static void
7665lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7666 struct lpfc_iocbq *rspiocb)
7667{
7668 IOCB_t *irsp = &rspiocb->iocb;
7669
7670 /* ELS cmd tag <ulpIoTag> completes */
7671 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007672 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007673 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007674 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007675 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007676 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7677 lpfc_ct_free_iocb(phba, cmdiocb);
7678 else
7679 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007680 return;
7681}
7682
James Smarte59058c2008-08-24 21:49:00 -04007683/**
James Smart5af5eee2010-10-22 11:06:38 -04007684 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007685 * @phba: Pointer to HBA context object.
7686 * @pring: Pointer to driver SLI ring object.
7687 * @cmdiocb: Pointer to driver command iocb object.
7688 *
James Smart5af5eee2010-10-22 11:06:38 -04007689 * This function issues an abort iocb for the provided command iocb down to
7690 * the port. Other than the case the outstanding command iocb is an abort
7691 * request, this function issues abort out unconditionally. This function is
7692 * called with hbalock held. The function returns 0 when it fails due to
7693 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007694 **/
James Smart5af5eee2010-10-22 11:06:38 -04007695static int
7696lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007697 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007698{
James Smart2e0fef82007-06-17 19:56:36 -05007699 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007700 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007701 IOCB_t *icmd = NULL;
7702 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007703 int retval;
James Smart07951072007-04-25 09:51:38 -04007704
James Smart92d7f7b2007-06-17 19:56:38 -05007705 /*
7706 * There are certain command types we don't want to abort. And we
7707 * don't want to abort commands that are already in the process of
7708 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007709 */
7710 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007711 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007712 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7713 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007714 return 0;
7715
dea31012005-04-17 16:05:31 -05007716 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007717 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007718 if (abtsiocbp == NULL)
7719 return 0;
dea31012005-04-17 16:05:31 -05007720
James Smart07951072007-04-25 09:51:38 -04007721 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007722 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007723 */
7724 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7725
dea31012005-04-17 16:05:31 -05007726 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007727 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7728 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007729 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007730 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007731 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7732 }
James Smartda0436e2009-05-22 14:51:39 -04007733 else
7734 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007735 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007736 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007737
James Smart5ffc2662009-11-18 15:39:44 -05007738 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7739 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007740 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7741 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007742
James Smart2e0fef82007-06-17 19:56:36 -05007743 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007744 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7745 else
7746 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7747
7748 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007749
James Smarte8b62012007-08-02 11:10:09 -04007750 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7751 "0339 Abort xri x%x, original iotag x%x, "
7752 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007753 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007754 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007755 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007756 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007757
James Smartd7c255b2008-08-24 21:50:00 -04007758 if (retval)
7759 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007760
7761 /*
7762 * Caller to this routine should check for IOCB_ERROR
7763 * and handle it properly. This routine no longer removes
7764 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7765 */
7766 return retval;
7767}
7768
7769/**
7770 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7771 * @phba: Pointer to HBA context object.
7772 * @pring: Pointer to driver SLI ring object.
7773 * @cmdiocb: Pointer to driver command iocb object.
7774 *
7775 * This function issues an abort iocb for the provided command iocb. In case
7776 * of unloading, the abort iocb will not be issued to commands on the ELS
7777 * ring. Instead, the callback function shall be changed to those commands
7778 * so that nothing happens when them finishes. This function is called with
7779 * hbalock held. The function returns 0 when the command iocb is an abort
7780 * request.
7781 **/
7782int
7783lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7784 struct lpfc_iocbq *cmdiocb)
7785{
7786 struct lpfc_vport *vport = cmdiocb->vport;
7787 int retval = IOCB_ERROR;
7788 IOCB_t *icmd = NULL;
7789
7790 /*
7791 * There are certain command types we don't want to abort. And we
7792 * don't want to abort commands that are already in the process of
7793 * being aborted.
7794 */
7795 icmd = &cmdiocb->iocb;
7796 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7797 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7798 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7799 return 0;
7800
7801 /*
7802 * If we're unloading, don't abort iocb on the ELS ring, but change
7803 * the callback so that nothing happens when it finishes.
7804 */
7805 if ((vport->load_flag & FC_UNLOADING) &&
7806 (pring->ringno == LPFC_ELS_RING)) {
7807 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7808 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7809 else
7810 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7811 goto abort_iotag_exit;
7812 }
7813
7814 /* Now, we try to issue the abort to the cmdiocb out */
7815 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7816
James Smart07951072007-04-25 09:51:38 -04007817abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007818 /*
7819 * Caller to this routine should check for IOCB_ERROR
7820 * and handle it properly. This routine no longer removes
7821 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007822 */
James Smart2e0fef82007-06-17 19:56:36 -05007823 return retval;
dea31012005-04-17 16:05:31 -05007824}
7825
James Smarte59058c2008-08-24 21:49:00 -04007826/**
James Smart5af5eee2010-10-22 11:06:38 -04007827 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7828 * @phba: Pointer to HBA context object.
7829 * @pring: Pointer to driver SLI ring object.
7830 *
7831 * This function aborts all iocbs in the given ring and frees all the iocb
7832 * objects in txq. This function issues abort iocbs unconditionally for all
7833 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7834 * to complete before the return of this function. The caller is not required
7835 * to hold any locks.
7836 **/
7837static void
7838lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7839{
7840 LIST_HEAD(completions);
7841 struct lpfc_iocbq *iocb, *next_iocb;
7842
7843 if (pring->ringno == LPFC_ELS_RING)
7844 lpfc_fabric_abort_hba(phba);
7845
7846 spin_lock_irq(&phba->hbalock);
7847
7848 /* Take off all the iocbs on txq for cancelling */
7849 list_splice_init(&pring->txq, &completions);
7850 pring->txq_cnt = 0;
7851
7852 /* Next issue ABTS for everything on the txcmplq */
7853 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7854 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7855
7856 spin_unlock_irq(&phba->hbalock);
7857
7858 /* Cancel all the IOCBs from the completions list */
7859 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7860 IOERR_SLI_ABORTED);
7861}
7862
7863/**
7864 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7865 * @phba: pointer to lpfc HBA data structure.
7866 *
7867 * This routine will abort all pending and outstanding iocbs to an HBA.
7868 **/
7869void
7870lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7871{
7872 struct lpfc_sli *psli = &phba->sli;
7873 struct lpfc_sli_ring *pring;
7874 int i;
7875
7876 for (i = 0; i < psli->num_rings; i++) {
7877 pring = &psli->ring[i];
7878 lpfc_sli_iocb_ring_abort(phba, pring);
7879 }
7880}
7881
7882/**
James Smart3621a712009-04-06 18:47:14 -04007883 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007884 * @iocbq: Pointer to driver iocb object.
7885 * @vport: Pointer to driver virtual port object.
7886 * @tgt_id: SCSI ID of the target.
7887 * @lun_id: LUN ID of the scsi device.
7888 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7889 *
James Smart3621a712009-04-06 18:47:14 -04007890 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007891 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7892 * 0 if the filtering criteria is met for the given iocb and will return
7893 * 1 if the filtering criteria is not met.
7894 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7895 * given iocb is for the SCSI device specified by vport, tgt_id and
7896 * lun_id parameter.
7897 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7898 * given iocb is for the SCSI target specified by vport and tgt_id
7899 * parameters.
7900 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7901 * given iocb is for the SCSI host associated with the given vport.
7902 * This function is called with no locks held.
7903 **/
dea31012005-04-17 16:05:31 -05007904static int
James Smart51ef4c22007-08-02 11:10:31 -04007905lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7906 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007907 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007908{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007909 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007910 int rc = 1;
7911
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007912 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7913 return rc;
7914
James Smart51ef4c22007-08-02 11:10:31 -04007915 if (iocbq->vport != vport)
7916 return rc;
7917
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007918 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007919
James Smart495a7142008-06-14 22:52:59 -04007920 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007921 return rc;
7922
7923 switch (ctx_cmd) {
7924 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007925 if ((lpfc_cmd->rdata->pnode) &&
7926 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7927 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007928 rc = 0;
7929 break;
7930 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007931 if ((lpfc_cmd->rdata->pnode) &&
7932 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007933 rc = 0;
7934 break;
dea31012005-04-17 16:05:31 -05007935 case LPFC_CTX_HOST:
7936 rc = 0;
7937 break;
7938 default:
7939 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007940 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007941 break;
7942 }
7943
7944 return rc;
7945}
7946
James Smarte59058c2008-08-24 21:49:00 -04007947/**
James Smart3621a712009-04-06 18:47:14 -04007948 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007949 * @vport: Pointer to virtual port.
7950 * @tgt_id: SCSI ID of the target.
7951 * @lun_id: LUN ID of the scsi device.
7952 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7953 *
7954 * This function returns number of FCP commands pending for the vport.
7955 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7956 * commands pending on the vport associated with SCSI device specified
7957 * by tgt_id and lun_id parameters.
7958 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7959 * commands pending on the vport associated with SCSI target specified
7960 * by tgt_id parameter.
7961 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7962 * commands pending on the vport.
7963 * This function returns the number of iocbs which satisfy the filter.
7964 * This function is called without any lock held.
7965 **/
dea31012005-04-17 16:05:31 -05007966int
James Smart51ef4c22007-08-02 11:10:31 -04007967lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7968 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007969{
James Smart51ef4c22007-08-02 11:10:31 -04007970 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007971 struct lpfc_iocbq *iocbq;
7972 int sum, i;
dea31012005-04-17 16:05:31 -05007973
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007974 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7975 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007976
James Smart51ef4c22007-08-02 11:10:31 -04007977 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7978 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007979 sum++;
dea31012005-04-17 16:05:31 -05007980 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007981
dea31012005-04-17 16:05:31 -05007982 return sum;
7983}
7984
James Smarte59058c2008-08-24 21:49:00 -04007985/**
James Smart3621a712009-04-06 18:47:14 -04007986 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007987 * @phba: Pointer to HBA context object
7988 * @cmdiocb: Pointer to command iocb object.
7989 * @rspiocb: Pointer to response iocb object.
7990 *
7991 * This function is called when an aborted FCP iocb completes. This
7992 * function is called by the ring event handler with no lock held.
7993 * This function frees the iocb.
7994 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007995void
James Smart2e0fef82007-06-17 19:56:36 -05007996lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7997 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007998{
James Bottomley604a3e32005-10-29 10:28:33 -05007999 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008000 return;
8001}
8002
James Smarte59058c2008-08-24 21:49:00 -04008003/**
James Smart3621a712009-04-06 18:47:14 -04008004 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04008005 * @vport: Pointer to virtual port.
8006 * @pring: Pointer to driver SLI ring object.
8007 * @tgt_id: SCSI ID of the target.
8008 * @lun_id: LUN ID of the scsi device.
8009 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8010 *
8011 * This function sends an abort command for every SCSI command
8012 * associated with the given virtual port pending on the ring
8013 * filtered by lpfc_sli_validate_fcp_iocb function.
8014 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8015 * FCP iocbs associated with lun specified by tgt_id and lun_id
8016 * parameters
8017 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8018 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8019 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8020 * FCP iocbs associated with virtual port.
8021 * This function returns number of iocbs it failed to abort.
8022 * This function is called with no locks held.
8023 **/
dea31012005-04-17 16:05:31 -05008024int
James Smart51ef4c22007-08-02 11:10:31 -04008025lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8026 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05008027{
James Smart51ef4c22007-08-02 11:10:31 -04008028 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008029 struct lpfc_iocbq *iocbq;
8030 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05008031 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05008032 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008033 int i;
dea31012005-04-17 16:05:31 -05008034
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008035 for (i = 1; i <= phba->sli.last_iotag; i++) {
8036 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008037
James Smart51ef4c22007-08-02 11:10:31 -04008038 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05008039 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05008040 continue;
8041
8042 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008043 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008044 if (abtsiocb == NULL) {
8045 errcnt++;
8046 continue;
8047 }
dea31012005-04-17 16:05:31 -05008048
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008049 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05008050 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8051 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04008052 if (phba->sli_rev == LPFC_SLI_REV4)
8053 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8054 else
8055 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008056 abtsiocb->iocb.ulpLe = 1;
8057 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05008058 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05008059
James Smart5ffc2662009-11-18 15:39:44 -05008060 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8061 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008062 if (iocbq->iocb_flag & LPFC_IO_FCP)
8063 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008064
James Smart2e0fef82007-06-17 19:56:36 -05008065 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05008066 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8067 else
8068 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8069
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008070 /* Setup callback routine and issue the command. */
8071 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04008072 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8073 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05008074 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05008075 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05008076 errcnt++;
8077 continue;
8078 }
8079 }
8080
8081 return errcnt;
8082}
8083
James Smarte59058c2008-08-24 21:49:00 -04008084/**
James Smart3621a712009-04-06 18:47:14 -04008085 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04008086 * @phba: Pointer to HBA context object.
8087 * @cmdiocbq: Pointer to command iocb.
8088 * @rspiocbq: Pointer to response iocb.
8089 *
8090 * This function is the completion handler for iocbs issued using
8091 * lpfc_sli_issue_iocb_wait function. This function is called by the
8092 * ring event handler function without any lock held. This function
8093 * can be called from both worker thread context and interrupt
8094 * context. This function also can be called from other thread which
8095 * cleans up the SLI layer objects.
8096 * This function copy the contents of the response iocb to the
8097 * response iocb memory object provided by the caller of
8098 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8099 * sleeps for the iocb completion.
8100 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008101static void
8102lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8103 struct lpfc_iocbq *cmdiocbq,
8104 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05008105{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008106 wait_queue_head_t *pdone_q;
8107 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05008108 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008109
James Smart2e0fef82007-06-17 19:56:36 -05008110 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008111 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8112 if (cmdiocbq->context2 && rspiocbq)
8113 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8114 &rspiocbq->iocb, sizeof(IOCB_t));
8115
James Smart0f65ff62010-02-26 14:14:23 -05008116 /* Set the exchange busy flag for task management commands */
8117 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8118 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8119 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8120 cur_iocbq);
8121 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8122 }
8123
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008124 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008125 if (pdone_q)
8126 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05008127 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05008128 return;
8129}
8130
James Smarte59058c2008-08-24 21:49:00 -04008131/**
James Smartd11e31d2009-06-10 17:23:06 -04008132 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8133 * @phba: Pointer to HBA context object..
8134 * @piocbq: Pointer to command iocb.
8135 * @flag: Flag to test.
8136 *
8137 * This routine grabs the hbalock and then test the iocb_flag to
8138 * see if the passed in flag is set.
8139 * Returns:
8140 * 1 if flag is set.
8141 * 0 if flag is not set.
8142 **/
8143static int
8144lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8145 struct lpfc_iocbq *piocbq, uint32_t flag)
8146{
8147 unsigned long iflags;
8148 int ret;
8149
8150 spin_lock_irqsave(&phba->hbalock, iflags);
8151 ret = piocbq->iocb_flag & flag;
8152 spin_unlock_irqrestore(&phba->hbalock, iflags);
8153 return ret;
8154
8155}
8156
8157/**
James Smart3621a712009-04-06 18:47:14 -04008158 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04008159 * @phba: Pointer to HBA context object..
8160 * @pring: Pointer to sli ring.
8161 * @piocb: Pointer to command iocb.
8162 * @prspiocbq: Pointer to response iocb.
8163 * @timeout: Timeout in number of seconds.
8164 *
8165 * This function issues the iocb to firmware and waits for the
8166 * iocb to complete. If the iocb command is not
8167 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8168 * Caller should not free the iocb resources if this function
8169 * returns IOCB_TIMEDOUT.
8170 * The function waits for the iocb completion using an
8171 * non-interruptible wait.
8172 * This function will sleep while waiting for iocb completion.
8173 * So, this function should not be called from any context which
8174 * does not allow sleeping. Due to the same reason, this function
8175 * cannot be called with interrupt disabled.
8176 * This function assumes that the iocb completions occur while
8177 * this function sleep. So, this function cannot be called from
8178 * the thread which process iocb completion for this ring.
8179 * This function clears the iocb_flag of the iocb object before
8180 * issuing the iocb and the iocb completion handler sets this
8181 * flag and wakes this thread when the iocb completes.
8182 * The contents of the response iocb will be copied to prspiocbq
8183 * by the completion handler when the command completes.
8184 * This function returns IOCB_SUCCESS when success.
8185 * This function is called with no lock held.
8186 **/
dea31012005-04-17 16:05:31 -05008187int
James Smart2e0fef82007-06-17 19:56:36 -05008188lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04008189 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05008190 struct lpfc_iocbq *piocb,
8191 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008192 uint32_t timeout)
dea31012005-04-17 16:05:31 -05008193{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008194 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008195 long timeleft, timeout_req = 0;
8196 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008197 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04008198 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05008199 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008200 * If the caller has provided a response iocbq buffer, then context2
8201 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05008202 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008203 if (prspiocbq) {
8204 if (piocb->context2)
8205 return IOCB_ERROR;
8206 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05008207 }
8208
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008209 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8210 piocb->context_un.wait_queue = &done_q;
8211 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05008212
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008213 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8214 creg_val = readl(phba->HCregaddr);
8215 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8216 writel(creg_val, phba->HCregaddr);
8217 readl(phba->HCregaddr); /* flush */
8218 }
8219
James Smart2a9bf3d2010-06-07 15:24:45 -04008220 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8221 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008222 if (retval == IOCB_SUCCESS) {
8223 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008224 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04008225 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008226 timeout_req);
dea31012005-04-17 16:05:31 -05008227
James Smart7054a602007-04-25 09:52:34 -04008228 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8229 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008230 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04008231 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008233 "0338 IOCB wait timeout error - no "
8234 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008235 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04008236 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008238 "0330 IOCB wake NOT set, "
8239 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008240 timeout, (timeleft / jiffies));
8241 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05008242 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008243 } else if (retval == IOCB_BUSY) {
8244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8245 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8246 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8247 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008248 } else {
8249 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008250 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008251 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008252 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05008253 }
8254
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008255 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8256 creg_val = readl(phba->HCregaddr);
8257 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8258 writel(creg_val, phba->HCregaddr);
8259 readl(phba->HCregaddr); /* flush */
8260 }
8261
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008262 if (prspiocbq)
8263 piocb->context2 = NULL;
8264
8265 piocb->context_un.wait_queue = NULL;
8266 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05008267 return retval;
8268}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008269
James Smarte59058c2008-08-24 21:49:00 -04008270/**
James Smart3621a712009-04-06 18:47:14 -04008271 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04008272 * @phba: Pointer to HBA context object.
8273 * @pmboxq: Pointer to driver mailbox object.
8274 * @timeout: Timeout in number of seconds.
8275 *
8276 * This function issues the mailbox to firmware and waits for the
8277 * mailbox command to complete. If the mailbox command is not
8278 * completed within timeout seconds, it returns MBX_TIMEOUT.
8279 * The function waits for the mailbox completion using an
8280 * interruptible wait. If the thread is woken up due to a
8281 * signal, MBX_TIMEOUT error is returned to the caller. Caller
8282 * should not free the mailbox resources, if this function returns
8283 * MBX_TIMEOUT.
8284 * This function will sleep while waiting for mailbox completion.
8285 * So, this function should not be called from any context which
8286 * does not allow sleeping. Due to the same reason, this function
8287 * cannot be called with interrupt disabled.
8288 * This function assumes that the mailbox completion occurs while
8289 * this function sleep. So, this function cannot be called from
8290 * the worker thread which processes mailbox completion.
8291 * This function is called in the context of HBA management
8292 * applications.
8293 * This function returns MBX_SUCCESS when successful.
8294 * This function is called with no lock held.
8295 **/
dea31012005-04-17 16:05:31 -05008296int
James Smart2e0fef82007-06-17 19:56:36 -05008297lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05008298 uint32_t timeout)
8299{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008300 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05008301 int retval;
James Smart858c9f62007-06-17 19:56:39 -05008302 unsigned long flag;
dea31012005-04-17 16:05:31 -05008303
8304 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04008305 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05008306 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008307
James Smart495a7142008-06-14 22:52:59 -04008308 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05008309 /* setup wake call as IOCB callback */
8310 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8311 /* setup context field to pass wait_queue pointer to wake function */
8312 pmboxq->context1 = &done_q;
8313
dea31012005-04-17 16:05:31 -05008314 /* now issue the command */
8315 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8316
8317 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04008318 wait_event_interruptible_timeout(done_q,
8319 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8320 timeout * HZ);
8321
James Smart858c9f62007-06-17 19:56:39 -05008322 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008323 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04008324 /*
8325 * if LPFC_MBX_WAKE flag is set the mailbox is completed
8326 * else do not free the resources.
8327 */
James Smartd7c47992010-06-08 18:31:54 -04008328 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05008329 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04008330 lpfc_sli4_swap_str(phba, pmboxq);
8331 } else {
James Smart7054a602007-04-25 09:52:34 -04008332 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05008333 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8334 }
8335 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008336 }
8337
dea31012005-04-17 16:05:31 -05008338 return retval;
8339}
8340
James Smarte59058c2008-08-24 21:49:00 -04008341/**
James Smart3772a992009-05-22 14:50:54 -04008342 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04008343 * @phba: Pointer to HBA context.
8344 *
James Smart3772a992009-05-22 14:50:54 -04008345 * This function is called to shutdown the driver's mailbox sub-system.
8346 * It first marks the mailbox sub-system is in a block state to prevent
8347 * the asynchronous mailbox command from issued off the pending mailbox
8348 * command queue. If the mailbox command sub-system shutdown is due to
8349 * HBA error conditions such as EEH or ERATT, this routine shall invoke
8350 * the mailbox sub-system flush routine to forcefully bring down the
8351 * mailbox sub-system. Otherwise, if it is due to normal condition (such
8352 * as with offline or HBA function reset), this routine will wait for the
8353 * outstanding mailbox command to complete before invoking the mailbox
8354 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04008355 **/
James Smart3772a992009-05-22 14:50:54 -04008356void
8357lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04008358{
James Smart3772a992009-05-22 14:50:54 -04008359 struct lpfc_sli *psli = &phba->sli;
8360 uint8_t actcmd = MBX_HEARTBEAT;
8361 unsigned long timeout;
8362
8363 spin_lock_irq(&phba->hbalock);
8364 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8365 spin_unlock_irq(&phba->hbalock);
8366
8367 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8368 spin_lock_irq(&phba->hbalock);
8369 if (phba->sli.mbox_active)
8370 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8371 spin_unlock_irq(&phba->hbalock);
8372 /* Determine how long we might wait for the active mailbox
8373 * command to be gracefully completed by firmware.
8374 */
8375 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8376 1000) + jiffies;
8377 while (phba->sli.mbox_active) {
8378 /* Check active mailbox complete status every 2ms */
8379 msleep(2);
8380 if (time_after(jiffies, timeout))
8381 /* Timeout, let the mailbox flush routine to
8382 * forcefully release active mailbox command
8383 */
8384 break;
8385 }
8386 }
8387 lpfc_sli_mbox_sys_flush(phba);
8388}
8389
8390/**
8391 * lpfc_sli_eratt_read - read sli-3 error attention events
8392 * @phba: Pointer to HBA context.
8393 *
8394 * This function is called to read the SLI3 device error attention registers
8395 * for possible error attention events. The caller must hold the hostlock
8396 * with spin_lock_irq().
8397 *
8398 * This fucntion returns 1 when there is Error Attention in the Host Attention
8399 * Register and returns 0 otherwise.
8400 **/
8401static int
8402lpfc_sli_eratt_read(struct lpfc_hba *phba)
8403{
James Smarted957682007-06-17 19:56:37 -05008404 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04008405
James Smart3772a992009-05-22 14:50:54 -04008406 /* Read chip Host Attention (HA) register */
8407 ha_copy = readl(phba->HAregaddr);
8408 if (ha_copy & HA_ERATT) {
8409 /* Read host status register to retrieve error event */
8410 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04008411
James Smart3772a992009-05-22 14:50:54 -04008412 /* Check if there is a deferred error condition is active */
8413 if ((HS_FFER1 & phba->work_hs) &&
8414 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008415 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04008416 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04008417 /* Clear all interrupt enable conditions */
8418 writel(0, phba->HCregaddr);
8419 readl(phba->HCregaddr);
8420 }
8421
8422 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04008423 phba->work_ha |= HA_ERATT;
8424 /* Indicate polling handles this ERATT */
8425 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008426 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008427 }
James Smart3772a992009-05-22 14:50:54 -04008428 return 0;
James Smartb4c02652006-07-06 15:50:43 -04008429}
8430
James Smarte59058c2008-08-24 21:49:00 -04008431/**
James Smartda0436e2009-05-22 14:51:39 -04008432 * lpfc_sli4_eratt_read - read sli-4 error attention events
8433 * @phba: Pointer to HBA context.
8434 *
8435 * This function is called to read the SLI4 device error attention registers
8436 * for possible error attention events. The caller must hold the hostlock
8437 * with spin_lock_irq().
8438 *
8439 * This fucntion returns 1 when there is Error Attention in the Host Attention
8440 * Register and returns 0 otherwise.
8441 **/
8442static int
8443lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8444{
8445 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05008446 uint32_t if_type, portsmphr;
8447 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04008448
James Smart2fcee4b2010-12-15 17:57:46 -05008449 /*
8450 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04008451 * registers for error attention. This can be changed later.
8452 */
James Smart2fcee4b2010-12-15 17:57:46 -05008453 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8454 switch (if_type) {
8455 case LPFC_SLI_INTF_IF_TYPE_0:
8456 uerr_sta_lo = readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8457 uerr_sta_hi = readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8458 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8459 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8461 "1423 HBA Unrecoverable error: "
8462 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8463 "ue_mask_lo_reg=0x%x, "
8464 "ue_mask_hi_reg=0x%x\n",
8465 uerr_sta_lo, uerr_sta_hi,
8466 phba->sli4_hba.ue_mask_lo,
8467 phba->sli4_hba.ue_mask_hi);
8468 phba->work_status[0] = uerr_sta_lo;
8469 phba->work_status[1] = uerr_sta_hi;
8470 phba->work_ha |= HA_ERATT;
8471 phba->hba_flag |= HBA_ERATT_HANDLED;
8472 return 1;
8473 }
8474 break;
8475 case LPFC_SLI_INTF_IF_TYPE_2:
8476 portstat_reg.word0 =
8477 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
8478 portsmphr = readl(phba->sli4_hba.PSMPHRregaddr);
8479 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8480 phba->work_status[0] =
8481 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8482 phba->work_status[1] =
8483 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8485 "2885 Port Error Detected: "
8486 "port status reg 0x%x, "
8487 "port smphr reg 0x%x, "
8488 "error 1=0x%x, error 2=0x%x\n",
8489 portstat_reg.word0,
8490 portsmphr,
8491 phba->work_status[0],
8492 phba->work_status[1]);
8493 phba->work_ha |= HA_ERATT;
8494 phba->hba_flag |= HBA_ERATT_HANDLED;
8495 return 1;
8496 }
8497 break;
8498 case LPFC_SLI_INTF_IF_TYPE_1:
8499 default:
James Smarta747c9c2009-11-18 15:41:10 -05008500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05008501 "2886 HBA Error Attention on unsupported "
8502 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05008503 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008504 }
James Smart2fcee4b2010-12-15 17:57:46 -05008505
James Smartda0436e2009-05-22 14:51:39 -04008506 return 0;
8507}
8508
8509/**
James Smart3621a712009-04-06 18:47:14 -04008510 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008511 * @phba: Pointer to HBA context.
8512 *
James Smart3772a992009-05-22 14:50:54 -04008513 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008514 * error attention register bit for error attention events.
8515 *
8516 * This fucntion returns 1 when there is Error Attention in the Host Attention
8517 * Register and returns 0 otherwise.
8518 **/
8519int
8520lpfc_sli_check_eratt(struct lpfc_hba *phba)
8521{
8522 uint32_t ha_copy;
8523
8524 /* If somebody is waiting to handle an eratt, don't process it
8525 * here. The brdkill function will do this.
8526 */
8527 if (phba->link_flag & LS_IGNORE_ERATT)
8528 return 0;
8529
8530 /* Check if interrupt handler handles this ERATT */
8531 spin_lock_irq(&phba->hbalock);
8532 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8533 /* Interrupt handler has handled ERATT */
8534 spin_unlock_irq(&phba->hbalock);
8535 return 0;
8536 }
8537
James Smarta257bf92009-04-06 18:48:10 -04008538 /*
8539 * If there is deferred error attention, do not check for error
8540 * attention
8541 */
8542 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8543 spin_unlock_irq(&phba->hbalock);
8544 return 0;
8545 }
8546
James Smart3772a992009-05-22 14:50:54 -04008547 /* If PCI channel is offline, don't process it */
8548 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008549 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008550 return 0;
8551 }
8552
8553 switch (phba->sli_rev) {
8554 case LPFC_SLI_REV2:
8555 case LPFC_SLI_REV3:
8556 /* Read chip Host Attention (HA) register */
8557 ha_copy = lpfc_sli_eratt_read(phba);
8558 break;
James Smartda0436e2009-05-22 14:51:39 -04008559 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05008560 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04008561 ha_copy = lpfc_sli4_eratt_read(phba);
8562 break;
James Smart3772a992009-05-22 14:50:54 -04008563 default:
8564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8565 "0299 Invalid SLI revision (%d)\n",
8566 phba->sli_rev);
8567 ha_copy = 0;
8568 break;
James Smart93996272008-08-24 21:50:30 -04008569 }
8570 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008571
8572 return ha_copy;
8573}
8574
8575/**
8576 * lpfc_intr_state_check - Check device state for interrupt handling
8577 * @phba: Pointer to HBA context.
8578 *
8579 * This inline routine checks whether a device or its PCI slot is in a state
8580 * that the interrupt should be handled.
8581 *
8582 * This function returns 0 if the device or the PCI slot is in a state that
8583 * interrupt should be handled, otherwise -EIO.
8584 */
8585static inline int
8586lpfc_intr_state_check(struct lpfc_hba *phba)
8587{
8588 /* If the pci channel is offline, ignore all the interrupts */
8589 if (unlikely(pci_channel_offline(phba->pcidev)))
8590 return -EIO;
8591
8592 /* Update device level interrupt statistics */
8593 phba->sli.slistat.sli_intr++;
8594
8595 /* Ignore all interrupts during initialization. */
8596 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8597 return -EIO;
8598
James Smart93996272008-08-24 21:50:30 -04008599 return 0;
8600}
8601
8602/**
James Smart3772a992009-05-22 14:50:54 -04008603 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008604 * @irq: Interrupt number.
8605 * @dev_id: The device context pointer.
8606 *
James Smart93996272008-08-24 21:50:30 -04008607 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008608 * service routine when device with SLI-3 interface spec is enabled with
8609 * MSI-X multi-message interrupt mode and there are slow-path events in
8610 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8611 * interrupt mode, this function is called as part of the device-level
8612 * interrupt handler. When the PCI slot is in error recovery or the HBA
8613 * is undergoing initialization, the interrupt handler will not process
8614 * the interrupt. The link attention and ELS ring attention events are
8615 * handled by the worker thread. The interrupt handler signals the worker
8616 * thread and returns for these events. This function is called without
8617 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008618 * structures.
8619 *
8620 * This function returns IRQ_HANDLED when interrupt is handled else it
8621 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008622 **/
dea31012005-04-17 16:05:31 -05008623irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008624lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008625{
James Smart2e0fef82007-06-17 19:56:36 -05008626 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008627 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008628 uint32_t work_ha_copy;
8629 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008630 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008631 uint32_t control;
8632
James Smart92d7f7b2007-06-17 19:56:38 -05008633 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008634 struct lpfc_vport *vport;
8635 struct lpfc_nodelist *ndlp;
8636 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008637 LPFC_MBOXQ_t *pmb;
8638 int rc;
8639
dea31012005-04-17 16:05:31 -05008640 /*
8641 * Get the driver's phba structure from the dev_id and
8642 * assume the HBA is not interrupting.
8643 */
James Smart93996272008-08-24 21:50:30 -04008644 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008645
8646 if (unlikely(!phba))
8647 return IRQ_NONE;
8648
dea31012005-04-17 16:05:31 -05008649 /*
James Smart93996272008-08-24 21:50:30 -04008650 * Stuff needs to be attented to when this function is invoked as an
8651 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008652 */
James Smart93996272008-08-24 21:50:30 -04008653 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008654 /* Check device state for handling interrupt */
8655 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008656 return IRQ_NONE;
8657 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008658 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008659 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008660 /* If somebody is waiting to handle an eratt don't process it
8661 * here. The brdkill function will do this.
8662 */
8663 if (phba->link_flag & LS_IGNORE_ERATT)
8664 ha_copy &= ~HA_ERATT;
8665 /* Check the need for handling ERATT in interrupt handler */
8666 if (ha_copy & HA_ERATT) {
8667 if (phba->hba_flag & HBA_ERATT_HANDLED)
8668 /* ERATT polling has handled ERATT */
8669 ha_copy &= ~HA_ERATT;
8670 else
8671 /* Indicate interrupt handler handles ERATT */
8672 phba->hba_flag |= HBA_ERATT_HANDLED;
8673 }
James Smarta257bf92009-04-06 18:48:10 -04008674
8675 /*
8676 * If there is deferred error attention, do not check for any
8677 * interrupt.
8678 */
8679 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008680 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008681 return IRQ_NONE;
8682 }
8683
James Smart93996272008-08-24 21:50:30 -04008684 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008685 hc_copy = readl(phba->HCregaddr);
8686 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8687 HC_LAINT_ENA | HC_ERINT_ENA),
8688 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008689 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8690 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008691 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008692 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008693 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008694 } else
8695 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008696
dea31012005-04-17 16:05:31 -05008697 work_ha_copy = ha_copy & phba->work_ha_mask;
8698
James Smart93996272008-08-24 21:50:30 -04008699 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008700 if (work_ha_copy & HA_LATT) {
8701 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8702 /*
8703 * Turn off Link Attention interrupts
8704 * until CLEAR_LA done
8705 */
James Smart5b75da22008-12-04 22:39:35 -05008706 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008707 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8708 control = readl(phba->HCregaddr);
8709 control &= ~HC_LAINT_ENA;
8710 writel(control, phba->HCregaddr);
8711 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008712 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008713 }
8714 else
8715 work_ha_copy &= ~HA_LATT;
8716 }
8717
James Smart93996272008-08-24 21:50:30 -04008718 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008719 /*
8720 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8721 * the only slow ring.
8722 */
8723 status = (work_ha_copy &
8724 (HA_RXMASK << (4*LPFC_ELS_RING)));
8725 status >>= (4*LPFC_ELS_RING);
8726 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008727 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008728 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008729
8730 lpfc_debugfs_slow_ring_trc(phba,
8731 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8732 control, status,
8733 (uint32_t)phba->sli.slistat.sli_intr);
8734
James Smart858c9f62007-06-17 19:56:39 -05008735 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008736 lpfc_debugfs_slow_ring_trc(phba,
8737 "ISR Disable ring:"
8738 "pwork:x%x hawork:x%x wait:x%x",
8739 phba->work_ha, work_ha_copy,
8740 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008741 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008742
James Smart858c9f62007-06-17 19:56:39 -05008743 control &=
8744 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008745 writel(control, phba->HCregaddr);
8746 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008747 }
James Smarta58cbd52007-08-02 11:09:43 -04008748 else {
8749 lpfc_debugfs_slow_ring_trc(phba,
8750 "ISR slow ring: pwork:"
8751 "x%x hawork:x%x wait:x%x",
8752 phba->work_ha, work_ha_copy,
8753 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008754 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008755 }
James Smart5b75da22008-12-04 22:39:35 -05008756 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008757 }
8758 }
James Smart5b75da22008-12-04 22:39:35 -05008759 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008760 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008761 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008762 /*
8763 * Check if there is a deferred error condition
8764 * is active
8765 */
8766 if ((HS_FFER1 & phba->work_hs) &&
8767 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008768 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8769 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008770 phba->hba_flag |= DEFER_ERATT;
8771 /* Clear all interrupt enable conditions */
8772 writel(0, phba->HCregaddr);
8773 readl(phba->HCregaddr);
8774 }
8775 }
8776
James Smart93996272008-08-24 21:50:30 -04008777 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008778 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008779 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008780 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008781 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008782
8783 /* First check out the status word */
8784 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8785 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008786 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008787 /*
8788 * Stray Mailbox Interrupt, mbxCommand <cmd>
8789 * mbxStatus <status>
8790 */
James Smart09372822008-01-11 01:52:54 -05008791 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008792 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008793 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008794 "Interrupt mbxCommand x%x "
8795 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008796 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008797 pmbox->mbxCommand,
8798 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008799 /* clear mailbox attention bit */
8800 work_ha_copy &= ~HA_MBATT;
8801 } else {
James Smart97eab632008-04-07 10:16:05 -04008802 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008804 phba->last_completion_time = jiffies;
8805 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008806 if (pmb->mbox_cmpl) {
8807 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8808 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008809 if (pmb->out_ext_byte_len &&
8810 pmb->context2)
8811 lpfc_sli_pcimem_bcopy(
8812 phba->mbox_ext,
8813 pmb->context2,
8814 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008815 }
James Smart09372822008-01-11 01:52:54 -05008816 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8817 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8818
8819 lpfc_debugfs_disc_trc(vport,
8820 LPFC_DISC_TRC_MBOX_VPORT,
8821 "MBOX dflt rpi: : "
8822 "status:x%x rpi:x%x",
8823 (uint32_t)pmbox->mbxStatus,
8824 pmbox->un.varWords[0], 0);
8825
8826 if (!pmbox->mbxStatus) {
8827 mp = (struct lpfc_dmabuf *)
8828 (pmb->context1);
8829 ndlp = (struct lpfc_nodelist *)
8830 pmb->context2;
8831
8832 /* Reg_LOGIN of dflt RPI was
8833 * successful. new lets get
8834 * rid of the RPI using the
8835 * same mbox buffer.
8836 */
8837 lpfc_unreg_login(phba,
8838 vport->vpi,
8839 pmbox->un.varWords[0],
8840 pmb);
8841 pmb->mbox_cmpl =
8842 lpfc_mbx_cmpl_dflt_rpi;
8843 pmb->context1 = mp;
8844 pmb->context2 = ndlp;
8845 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008846 rc = lpfc_sli_issue_mbox(phba,
8847 pmb,
8848 MBX_NOWAIT);
8849 if (rc != MBX_BUSY)
8850 lpfc_printf_log(phba,
8851 KERN_ERR,
8852 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008853 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008854 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008855 if (rc != MBX_NOT_FINISHED)
8856 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008857 }
8858 }
James Smart5b75da22008-12-04 22:39:35 -05008859 spin_lock_irqsave(
8860 &phba->pport->work_port_lock,
8861 iflag);
James Smart09372822008-01-11 01:52:54 -05008862 phba->pport->work_port_events &=
8863 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008864 spin_unlock_irqrestore(
8865 &phba->pport->work_port_lock,
8866 iflag);
James Smart09372822008-01-11 01:52:54 -05008867 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008868 }
James Smart97eab632008-04-07 10:16:05 -04008869 } else
James Smart5b75da22008-12-04 22:39:35 -05008870 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008871
James Smart92d7f7b2007-06-17 19:56:38 -05008872 if ((work_ha_copy & HA_MBATT) &&
8873 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008874send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008875 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008876 do {
8877 rc = lpfc_sli_issue_mbox(phba, NULL,
8878 MBX_NOWAIT);
8879 } while (rc == MBX_NOT_FINISHED);
8880 if (rc != MBX_SUCCESS)
8881 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8882 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008883 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008884 }
8885
James Smart5b75da22008-12-04 22:39:35 -05008886 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008887 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008888 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008889 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008890 }
James Smart93996272008-08-24 21:50:30 -04008891 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008892
James Smart3772a992009-05-22 14:50:54 -04008893} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008894
8895/**
James Smart3772a992009-05-22 14:50:54 -04008896 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008897 * @irq: Interrupt number.
8898 * @dev_id: The device context pointer.
8899 *
8900 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008901 * service routine when device with SLI-3 interface spec is enabled with
8902 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8903 * ring event in the HBA. However, when the device is enabled with either
8904 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8905 * device-level interrupt handler. When the PCI slot is in error recovery
8906 * or the HBA is undergoing initialization, the interrupt handler will not
8907 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8908 * the intrrupt context. This function is called without any lock held.
8909 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008910 *
8911 * This function returns IRQ_HANDLED when interrupt is handled else it
8912 * returns IRQ_NONE.
8913 **/
8914irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008915lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008916{
8917 struct lpfc_hba *phba;
8918 uint32_t ha_copy;
8919 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008920 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008921
8922 /* Get the driver's phba structure from the dev_id and
8923 * assume the HBA is not interrupting.
8924 */
8925 phba = (struct lpfc_hba *) dev_id;
8926
8927 if (unlikely(!phba))
8928 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008929
8930 /*
James Smart93996272008-08-24 21:50:30 -04008931 * Stuff needs to be attented to when this function is invoked as an
8932 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008933 */
James Smart93996272008-08-24 21:50:30 -04008934 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008935 /* Check device state for handling interrupt */
8936 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008937 return IRQ_NONE;
8938 /* Need to read HA REG for FCP ring and other ring events */
8939 ha_copy = readl(phba->HAregaddr);
8940 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008941 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008942 /*
8943 * If there is deferred error attention, do not check for
8944 * any interrupt.
8945 */
8946 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008947 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008948 return IRQ_NONE;
8949 }
James Smart93996272008-08-24 21:50:30 -04008950 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8951 phba->HAregaddr);
8952 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008953 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008954 } else
8955 ha_copy = phba->ha_copy;
8956
8957 /*
8958 * Process all events on FCP ring. Take the optimized path for FCP IO.
8959 */
8960 ha_copy &= ~(phba->work_ha_mask);
8961
8962 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008963 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008964 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008965 lpfc_sli_handle_fast_ring_event(phba,
8966 &phba->sli.ring[LPFC_FCP_RING],
8967 status);
James Smarta4bc3372006-12-02 13:34:16 -05008968
8969 if (phba->cfg_multi_ring_support == 2) {
8970 /*
James Smart93996272008-08-24 21:50:30 -04008971 * Process all events on extra ring. Take the optimized path
8972 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008973 */
James Smart93996272008-08-24 21:50:30 -04008974 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008975 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008976 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008977 lpfc_sli_handle_fast_ring_event(phba,
8978 &phba->sli.ring[LPFC_EXTRA_RING],
8979 status);
8980 }
8981 }
dea31012005-04-17 16:05:31 -05008982 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008983} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008984
James Smart93996272008-08-24 21:50:30 -04008985/**
James Smart3772a992009-05-22 14:50:54 -04008986 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008987 * @irq: Interrupt number.
8988 * @dev_id: The device context pointer.
8989 *
James Smart3772a992009-05-22 14:50:54 -04008990 * This function is the HBA device-level interrupt handler to device with
8991 * SLI-3 interface spec, called from the PCI layer when either MSI or
8992 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8993 * requires driver attention. This function invokes the slow-path interrupt
8994 * attention handling function and fast-path interrupt attention handling
8995 * function in turn to process the relevant HBA attention events. This
8996 * function is called without any lock held. It gets the hbalock to access
8997 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008998 *
8999 * This function returns IRQ_HANDLED when interrupt is handled, else it
9000 * returns IRQ_NONE.
9001 **/
9002irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009003lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04009004{
9005 struct lpfc_hba *phba;
9006 irqreturn_t sp_irq_rc, fp_irq_rc;
9007 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05009008 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04009009
9010 /*
9011 * Get the driver's phba structure from the dev_id and
9012 * assume the HBA is not interrupting.
9013 */
9014 phba = (struct lpfc_hba *) dev_id;
9015
9016 if (unlikely(!phba))
9017 return IRQ_NONE;
9018
James Smart3772a992009-05-22 14:50:54 -04009019 /* Check device state for handling interrupt */
9020 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009021 return IRQ_NONE;
9022
9023 spin_lock(&phba->hbalock);
9024 phba->ha_copy = readl(phba->HAregaddr);
9025 if (unlikely(!phba->ha_copy)) {
9026 spin_unlock(&phba->hbalock);
9027 return IRQ_NONE;
9028 } else if (phba->ha_copy & HA_ERATT) {
9029 if (phba->hba_flag & HBA_ERATT_HANDLED)
9030 /* ERATT polling has handled ERATT */
9031 phba->ha_copy &= ~HA_ERATT;
9032 else
9033 /* Indicate interrupt handler handles ERATT */
9034 phba->hba_flag |= HBA_ERATT_HANDLED;
9035 }
9036
James Smarta257bf92009-04-06 18:48:10 -04009037 /*
9038 * If there is deferred error attention, do not check for any interrupt.
9039 */
9040 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02009041 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04009042 return IRQ_NONE;
9043 }
9044
James Smart93996272008-08-24 21:50:30 -04009045 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05009046 hc_copy = readl(phba->HCregaddr);
9047 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9048 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9049 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009050 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009051 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009052 readl(phba->HAregaddr); /* flush */
9053 spin_unlock(&phba->hbalock);
9054
9055 /*
9056 * Invokes slow-path host attention interrupt handling as appropriate.
9057 */
9058
9059 /* status of events with mailbox and link attention */
9060 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9061
9062 /* status of events with ELS ring */
9063 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
9064 status2 >>= (4*LPFC_ELS_RING);
9065
9066 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009067 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009068 else
9069 sp_irq_rc = IRQ_NONE;
9070
9071 /*
9072 * Invoke fast-path host attention interrupt handling as appropriate.
9073 */
9074
9075 /* status of events with FCP ring */
9076 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9077 status1 >>= (4*LPFC_FCP_RING);
9078
9079 /* status of events with extra ring */
9080 if (phba->cfg_multi_ring_support == 2) {
9081 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9082 status2 >>= (4*LPFC_EXTRA_RING);
9083 } else
9084 status2 = 0;
9085
9086 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009087 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009088 else
9089 fp_irq_rc = IRQ_NONE;
9090
9091 /* Return device-level interrupt handling status */
9092 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04009093} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04009094
9095/**
9096 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9097 * @phba: pointer to lpfc hba data structure.
9098 *
9099 * This routine is invoked by the worker thread to process all the pending
9100 * SLI4 FCP abort XRI events.
9101 **/
9102void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9103{
9104 struct lpfc_cq_event *cq_event;
9105
9106 /* First, declare the fcp xri abort event has been handled */
9107 spin_lock_irq(&phba->hbalock);
9108 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9109 spin_unlock_irq(&phba->hbalock);
9110 /* Now, handle all the fcp xri abort events */
9111 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9112 /* Get the first event from the head of the event queue */
9113 spin_lock_irq(&phba->hbalock);
9114 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9115 cq_event, struct lpfc_cq_event, list);
9116 spin_unlock_irq(&phba->hbalock);
9117 /* Notify aborted XRI for FCP work queue */
9118 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9119 /* Free the event processed back to the free pool */
9120 lpfc_sli4_cq_event_release(phba, cq_event);
9121 }
9122}
9123
9124/**
9125 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9126 * @phba: pointer to lpfc hba data structure.
9127 *
9128 * This routine is invoked by the worker thread to process all the pending
9129 * SLI4 els abort xri events.
9130 **/
9131void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9132{
9133 struct lpfc_cq_event *cq_event;
9134
9135 /* First, declare the els xri abort event has been handled */
9136 spin_lock_irq(&phba->hbalock);
9137 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9138 spin_unlock_irq(&phba->hbalock);
9139 /* Now, handle all the els xri abort events */
9140 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9141 /* Get the first event from the head of the event queue */
9142 spin_lock_irq(&phba->hbalock);
9143 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9144 cq_event, struct lpfc_cq_event, list);
9145 spin_unlock_irq(&phba->hbalock);
9146 /* Notify aborted XRI for ELS work queue */
9147 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9148 /* Free the event processed back to the free pool */
9149 lpfc_sli4_cq_event_release(phba, cq_event);
9150 }
9151}
9152
James Smart341af102010-01-26 23:07:37 -05009153/**
9154 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9155 * @phba: pointer to lpfc hba data structure
9156 * @pIocbIn: pointer to the rspiocbq
9157 * @pIocbOut: pointer to the cmdiocbq
9158 * @wcqe: pointer to the complete wcqe
9159 *
9160 * This routine transfers the fields of a command iocbq to a response iocbq
9161 * by copying all the IOCB fields from command iocbq and transferring the
9162 * completion status information from the complete wcqe.
9163 **/
James Smart4f774512009-05-22 14:52:35 -04009164static void
James Smart341af102010-01-26 23:07:37 -05009165lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9166 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04009167 struct lpfc_iocbq *pIocbOut,
9168 struct lpfc_wcqe_complete *wcqe)
9169{
James Smart341af102010-01-26 23:07:37 -05009170 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04009171 size_t offset = offsetof(struct lpfc_iocbq, iocb);
9172
9173 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9174 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04009175 /* Map WCQE parameters into irspiocb parameters */
9176 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9177 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9178 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9179 pIocbIn->iocb.un.fcpi.fcpi_parm =
9180 pIocbOut->iocb.un.fcpi.fcpi_parm -
9181 wcqe->total_data_placed;
9182 else
9183 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009184 else {
James Smart4f774512009-05-22 14:52:35 -04009185 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009186 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9187 }
James Smart341af102010-01-26 23:07:37 -05009188
9189 /* Pick up HBA exchange busy condition */
9190 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9191 spin_lock_irqsave(&phba->hbalock, iflags);
9192 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9193 spin_unlock_irqrestore(&phba->hbalock, iflags);
9194 }
James Smart4f774512009-05-22 14:52:35 -04009195}
9196
9197/**
James Smart45ed1192009-10-02 15:17:02 -04009198 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9199 * @phba: Pointer to HBA context object.
9200 * @wcqe: Pointer to work-queue completion queue entry.
9201 *
9202 * This routine handles an ELS work-queue completion event and construct
9203 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9204 * discovery engine to handle.
9205 *
9206 * Return: Pointer to the receive IOCBQ, NULL otherwise.
9207 **/
9208static struct lpfc_iocbq *
9209lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9210 struct lpfc_iocbq *irspiocbq)
9211{
9212 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9213 struct lpfc_iocbq *cmdiocbq;
9214 struct lpfc_wcqe_complete *wcqe;
9215 unsigned long iflags;
9216
9217 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9218 spin_lock_irqsave(&phba->hbalock, iflags);
9219 pring->stats.iocb_event++;
9220 /* Look up the ELS command IOCB and create pseudo response IOCB */
9221 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9222 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9223 spin_unlock_irqrestore(&phba->hbalock, iflags);
9224
9225 if (unlikely(!cmdiocbq)) {
9226 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9227 "0386 ELS complete with no corresponding "
9228 "cmdiocb: iotag (%d)\n",
9229 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9230 lpfc_sli_release_iocbq(phba, irspiocbq);
9231 return NULL;
9232 }
9233
9234 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009235 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04009236
9237 return irspiocbq;
9238}
9239
9240/**
James Smart04c68492009-05-22 14:52:52 -04009241 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9242 * @phba: Pointer to HBA context object.
9243 * @cqe: Pointer to mailbox completion queue entry.
9244 *
9245 * This routine process a mailbox completion queue entry with asynchrous
9246 * event.
9247 *
9248 * Return: true if work posted to worker thread, otherwise false.
9249 **/
9250static bool
9251lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9252{
9253 struct lpfc_cq_event *cq_event;
9254 unsigned long iflags;
9255
9256 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9257 "0392 Async Event: word0:x%x, word1:x%x, "
9258 "word2:x%x, word3:x%x\n", mcqe->word0,
9259 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9260
9261 /* Allocate a new internal CQ_EVENT entry */
9262 cq_event = lpfc_sli4_cq_event_alloc(phba);
9263 if (!cq_event) {
9264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9265 "0394 Failed to allocate CQ_EVENT entry\n");
9266 return false;
9267 }
9268
9269 /* Move the CQE into an asynchronous event entry */
9270 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9271 spin_lock_irqsave(&phba->hbalock, iflags);
9272 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9273 /* Set the async event flag */
9274 phba->hba_flag |= ASYNC_EVENT;
9275 spin_unlock_irqrestore(&phba->hbalock, iflags);
9276
9277 return true;
9278}
9279
9280/**
9281 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9282 * @phba: Pointer to HBA context object.
9283 * @cqe: Pointer to mailbox completion queue entry.
9284 *
9285 * This routine process a mailbox completion queue entry with mailbox
9286 * completion event.
9287 *
9288 * Return: true if work posted to worker thread, otherwise false.
9289 **/
9290static bool
9291lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9292{
9293 uint32_t mcqe_status;
9294 MAILBOX_t *mbox, *pmbox;
9295 struct lpfc_mqe *mqe;
9296 struct lpfc_vport *vport;
9297 struct lpfc_nodelist *ndlp;
9298 struct lpfc_dmabuf *mp;
9299 unsigned long iflags;
9300 LPFC_MBOXQ_t *pmb;
9301 bool workposted = false;
9302 int rc;
9303
9304 /* If not a mailbox complete MCQE, out by checking mailbox consume */
9305 if (!bf_get(lpfc_trailer_completed, mcqe))
9306 goto out_no_mqe_complete;
9307
9308 /* Get the reference to the active mbox command */
9309 spin_lock_irqsave(&phba->hbalock, iflags);
9310 pmb = phba->sli.mbox_active;
9311 if (unlikely(!pmb)) {
9312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9313 "1832 No pending MBOX command to handle\n");
9314 spin_unlock_irqrestore(&phba->hbalock, iflags);
9315 goto out_no_mqe_complete;
9316 }
9317 spin_unlock_irqrestore(&phba->hbalock, iflags);
9318 mqe = &pmb->u.mqe;
9319 pmbox = (MAILBOX_t *)&pmb->u.mqe;
9320 mbox = phba->mbox;
9321 vport = pmb->vport;
9322
9323 /* Reset heartbeat timer */
9324 phba->last_completion_time = jiffies;
9325 del_timer(&phba->sli.mbox_tmo);
9326
9327 /* Move mbox data to caller's mailbox region, do endian swapping */
9328 if (pmb->mbox_cmpl && mbox)
9329 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9330 /* Set the mailbox status with SLI4 range 0x4000 */
9331 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9332 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9333 bf_set(lpfc_mqe_status, mqe,
9334 (LPFC_MBX_ERROR_RANGE | mcqe_status));
9335
9336 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9337 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9338 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9339 "MBOX dflt rpi: status:x%x rpi:x%x",
9340 mcqe_status,
9341 pmbox->un.varWords[0], 0);
9342 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9343 mp = (struct lpfc_dmabuf *)(pmb->context1);
9344 ndlp = (struct lpfc_nodelist *)pmb->context2;
9345 /* Reg_LOGIN of dflt RPI was successful. Now lets get
9346 * RID of the PPI using the same mbox buffer.
9347 */
9348 lpfc_unreg_login(phba, vport->vpi,
9349 pmbox->un.varWords[0], pmb);
9350 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9351 pmb->context1 = mp;
9352 pmb->context2 = ndlp;
9353 pmb->vport = vport;
9354 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9355 if (rc != MBX_BUSY)
9356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9357 LOG_SLI, "0385 rc should "
9358 "have been MBX_BUSY\n");
9359 if (rc != MBX_NOT_FINISHED)
9360 goto send_current_mbox;
9361 }
9362 }
9363 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9364 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9365 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9366
9367 /* There is mailbox completion work to do */
9368 spin_lock_irqsave(&phba->hbalock, iflags);
9369 __lpfc_mbox_cmpl_put(phba, pmb);
9370 phba->work_ha |= HA_MBATT;
9371 spin_unlock_irqrestore(&phba->hbalock, iflags);
9372 workposted = true;
9373
9374send_current_mbox:
9375 spin_lock_irqsave(&phba->hbalock, iflags);
9376 /* Release the mailbox command posting token */
9377 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9378 /* Setting active mailbox pointer need to be in sync to flag clear */
9379 phba->sli.mbox_active = NULL;
9380 spin_unlock_irqrestore(&phba->hbalock, iflags);
9381 /* Wake up worker thread to post the next pending mailbox command */
9382 lpfc_worker_wake_up(phba);
9383out_no_mqe_complete:
9384 if (bf_get(lpfc_trailer_consumed, mcqe))
9385 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9386 return workposted;
9387}
9388
9389/**
9390 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9391 * @phba: Pointer to HBA context object.
9392 * @cqe: Pointer to mailbox completion queue entry.
9393 *
9394 * This routine process a mailbox completion queue entry, it invokes the
9395 * proper mailbox complete handling or asynchrous event handling routine
9396 * according to the MCQE's async bit.
9397 *
9398 * Return: true if work posted to worker thread, otherwise false.
9399 **/
9400static bool
9401lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9402{
9403 struct lpfc_mcqe mcqe;
9404 bool workposted;
9405
9406 /* Copy the mailbox MCQE and convert endian order as needed */
9407 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9408
9409 /* Invoke the proper event handling routine */
9410 if (!bf_get(lpfc_trailer_async, &mcqe))
9411 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9412 else
9413 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9414 return workposted;
9415}
9416
9417/**
James Smart4f774512009-05-22 14:52:35 -04009418 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9419 * @phba: Pointer to HBA context object.
9420 * @wcqe: Pointer to work-queue completion queue entry.
9421 *
9422 * This routine handles an ELS work-queue completion event.
9423 *
9424 * Return: true if work posted to worker thread, otherwise false.
9425 **/
9426static bool
9427lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9428 struct lpfc_wcqe_complete *wcqe)
9429{
James Smart4f774512009-05-22 14:52:35 -04009430 struct lpfc_iocbq *irspiocbq;
9431 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04009432 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04009433
James Smart45ed1192009-10-02 15:17:02 -04009434 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04009435 irspiocbq = lpfc_sli_get_iocbq(phba);
9436 if (!irspiocbq) {
9437 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04009438 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9439 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9440 pring->txq_cnt, phba->iocb_cnt,
9441 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9442 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04009443 return false;
James Smart4f774512009-05-22 14:52:35 -04009444 }
James Smart4f774512009-05-22 14:52:35 -04009445
James Smart45ed1192009-10-02 15:17:02 -04009446 /* Save off the slow-path queue event for work thread to process */
9447 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04009448 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04009449 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009450 &phba->sli4_hba.sp_queue_event);
9451 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04009452 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04009453
James Smart45ed1192009-10-02 15:17:02 -04009454 return true;
James Smart4f774512009-05-22 14:52:35 -04009455}
9456
9457/**
9458 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9459 * @phba: Pointer to HBA context object.
9460 * @wcqe: Pointer to work-queue completion queue entry.
9461 *
9462 * This routine handles slow-path WQ entry comsumed event by invoking the
9463 * proper WQ release routine to the slow-path WQ.
9464 **/
9465static void
9466lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9467 struct lpfc_wcqe_release *wcqe)
9468{
9469 /* Check for the slow-path ELS work queue */
9470 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9471 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9472 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9473 else
9474 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9475 "2579 Slow-path wqe consume event carries "
9476 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9477 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9478 phba->sli4_hba.els_wq->queue_id);
9479}
9480
9481/**
9482 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9483 * @phba: Pointer to HBA context object.
9484 * @cq: Pointer to a WQ completion queue.
9485 * @wcqe: Pointer to work-queue completion queue entry.
9486 *
9487 * This routine handles an XRI abort event.
9488 *
9489 * Return: true if work posted to worker thread, otherwise false.
9490 **/
9491static bool
9492lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9493 struct lpfc_queue *cq,
9494 struct sli4_wcqe_xri_aborted *wcqe)
9495{
9496 bool workposted = false;
9497 struct lpfc_cq_event *cq_event;
9498 unsigned long iflags;
9499
9500 /* Allocate a new internal CQ_EVENT entry */
9501 cq_event = lpfc_sli4_cq_event_alloc(phba);
9502 if (!cq_event) {
9503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9504 "0602 Failed to allocate CQ_EVENT entry\n");
9505 return false;
9506 }
9507
9508 /* Move the CQE into the proper xri abort event list */
9509 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9510 switch (cq->subtype) {
9511 case LPFC_FCP:
9512 spin_lock_irqsave(&phba->hbalock, iflags);
9513 list_add_tail(&cq_event->list,
9514 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9515 /* Set the fcp xri abort event flag */
9516 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9517 spin_unlock_irqrestore(&phba->hbalock, iflags);
9518 workposted = true;
9519 break;
9520 case LPFC_ELS:
9521 spin_lock_irqsave(&phba->hbalock, iflags);
9522 list_add_tail(&cq_event->list,
9523 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9524 /* Set the els xri abort event flag */
9525 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9526 spin_unlock_irqrestore(&phba->hbalock, iflags);
9527 workposted = true;
9528 break;
9529 default:
9530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9531 "0603 Invalid work queue CQE subtype (x%x)\n",
9532 cq->subtype);
9533 workposted = false;
9534 break;
9535 }
9536 return workposted;
9537}
9538
9539/**
James Smart4d9ab992009-10-02 15:16:39 -04009540 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009541 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009542 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009543 *
James Smart4d9ab992009-10-02 15:16:39 -04009544 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009545 *
9546 * Return: true if work posted to worker thread, otherwise false.
9547 **/
9548static bool
James Smart4d9ab992009-10-02 15:16:39 -04009549lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9550{
9551 bool workposted = false;
9552 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9553 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9554 struct hbq_dmabuf *dma_buf;
9555 uint32_t status;
9556 unsigned long iflags;
9557
James Smart4d9ab992009-10-02 15:16:39 -04009558 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9559 goto out;
9560
9561 status = bf_get(lpfc_rcqe_status, rcqe);
9562 switch (status) {
9563 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9565 "2537 Receive Frame Truncated!!\n");
9566 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009567 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009568 spin_lock_irqsave(&phba->hbalock, iflags);
9569 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9570 if (!dma_buf) {
9571 spin_unlock_irqrestore(&phba->hbalock, iflags);
9572 goto out;
9573 }
9574 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9575 /* save off the frame for the word thread to process */
9576 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009577 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009578 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009579 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009580 spin_unlock_irqrestore(&phba->hbalock, iflags);
9581 workposted = true;
9582 break;
9583 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9584 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9585 /* Post more buffers if possible */
9586 spin_lock_irqsave(&phba->hbalock, iflags);
9587 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9588 spin_unlock_irqrestore(&phba->hbalock, iflags);
9589 workposted = true;
9590 break;
9591 }
9592out:
9593 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009594}
9595
9596/**
9597 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9598 * @phba: Pointer to HBA context object.
9599 * @cq: Pointer to the completion queue.
9600 * @wcqe: Pointer to a completion queue entry.
9601 *
9602 * This routine process a slow-path work-queue or recieve queue completion queue
9603 * entry.
9604 *
9605 * Return: true if work posted to worker thread, otherwise false.
9606 **/
9607static bool
9608lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009609 struct lpfc_cqe *cqe)
9610{
James Smart45ed1192009-10-02 15:17:02 -04009611 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009612 bool workposted = false;
9613
9614 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009615 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009616
9617 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009618 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009619 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009620 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009621 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009622 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009623 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009624 break;
9625 case CQE_CODE_RELEASE_WQE:
9626 /* Process the WQ release event */
9627 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009628 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009629 break;
9630 case CQE_CODE_XRI_ABORTED:
9631 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009632 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009633 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009634 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009635 break;
James Smart4d9ab992009-10-02 15:16:39 -04009636 case CQE_CODE_RECEIVE:
9637 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009638 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009639 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009640 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009641 break;
James Smart4f774512009-05-22 14:52:35 -04009642 default:
9643 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9644 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009645 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009646 break;
9647 }
9648 return workposted;
9649}
9650
9651/**
James Smart4f774512009-05-22 14:52:35 -04009652 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9653 * @phba: Pointer to HBA context object.
9654 * @eqe: Pointer to fast-path event queue entry.
9655 *
9656 * This routine process a event queue entry from the slow-path event queue.
9657 * It will check the MajorCode and MinorCode to determine this is for a
9658 * completion event on a completion queue, if not, an error shall be logged
9659 * and just return. Otherwise, it will get to the corresponding completion
9660 * queue and process all the entries on that completion queue, rearm the
9661 * completion queue, and then return.
9662 *
9663 **/
9664static void
9665lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9666{
9667 struct lpfc_queue *cq = NULL, *childq, *speq;
9668 struct lpfc_cqe *cqe;
9669 bool workposted = false;
9670 int ecount = 0;
9671 uint16_t cqid;
9672
James Smartcb5172e2010-03-15 11:25:07 -04009673 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009674 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9675 "0359 Not a valid slow-path completion "
9676 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009677 bf_get_le32(lpfc_eqe_major_code, eqe),
9678 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009679 return;
9680 }
9681
9682 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009683 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009684
9685 /* Search for completion queue pointer matching this cqid */
9686 speq = phba->sli4_hba.sp_eq;
9687 list_for_each_entry(childq, &speq->child_list, list) {
9688 if (childq->queue_id == cqid) {
9689 cq = childq;
9690 break;
9691 }
9692 }
9693 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009694 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9695 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9696 "0365 Slow-path CQ identifier "
9697 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009698 return;
9699 }
9700
9701 /* Process all the entries to the CQ */
9702 switch (cq->type) {
9703 case LPFC_MCQ:
9704 while ((cqe = lpfc_sli4_cq_get(cq))) {
9705 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9706 if (!(++ecount % LPFC_GET_QE_REL_INT))
9707 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9708 }
9709 break;
9710 case LPFC_WCQ:
9711 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009712 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009713 if (!(++ecount % LPFC_GET_QE_REL_INT))
9714 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9715 }
9716 break;
9717 default:
9718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9719 "0370 Invalid completion queue type (%d)\n",
9720 cq->type);
9721 return;
9722 }
9723
9724 /* Catch the no cq entry condition, log an error */
9725 if (unlikely(ecount == 0))
9726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9727 "0371 No entry from the CQ: identifier "
9728 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9729
9730 /* In any case, flash and re-arm the RCQ */
9731 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9732
9733 /* wake up worker thread if there are works to be done */
9734 if (workposted)
9735 lpfc_worker_wake_up(phba);
9736}
9737
9738/**
9739 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9740 * @eqe: Pointer to fast-path completion queue entry.
9741 *
9742 * This routine process a fast-path work queue completion entry from fast-path
9743 * event queue for FCP command response completion.
9744 **/
9745static void
9746lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9747 struct lpfc_wcqe_complete *wcqe)
9748{
9749 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9750 struct lpfc_iocbq *cmdiocbq;
9751 struct lpfc_iocbq irspiocbq;
9752 unsigned long iflags;
9753
9754 spin_lock_irqsave(&phba->hbalock, iflags);
9755 pring->stats.iocb_event++;
9756 spin_unlock_irqrestore(&phba->hbalock, iflags);
9757
9758 /* Check for response status */
9759 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9760 /* If resource errors reported from HBA, reduce queue
9761 * depth of the SCSI device.
9762 */
9763 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9764 IOSTAT_LOCAL_REJECT) &&
9765 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9766 phba->lpfc_rampdown_queue_depth(phba);
9767 }
9768 /* Log the error status */
9769 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9770 "0373 FCP complete error: status=x%x, "
9771 "hw_status=x%x, total_data_specified=%d, "
9772 "parameter=x%x, word3=x%x\n",
9773 bf_get(lpfc_wcqe_c_status, wcqe),
9774 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9775 wcqe->total_data_placed, wcqe->parameter,
9776 wcqe->word3);
9777 }
9778
9779 /* Look up the FCP command IOCB and create pseudo response IOCB */
9780 spin_lock_irqsave(&phba->hbalock, iflags);
9781 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9782 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9783 spin_unlock_irqrestore(&phba->hbalock, iflags);
9784 if (unlikely(!cmdiocbq)) {
9785 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9786 "0374 FCP complete with no corresponding "
9787 "cmdiocb: iotag (%d)\n",
9788 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9789 return;
9790 }
9791 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9792 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9793 "0375 FCP cmdiocb not callback function "
9794 "iotag: (%d)\n",
9795 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9796 return;
9797 }
9798
9799 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009800 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009801
James Smart0f65ff62010-02-26 14:14:23 -05009802 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9803 spin_lock_irqsave(&phba->hbalock, iflags);
9804 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9805 spin_unlock_irqrestore(&phba->hbalock, iflags);
9806 }
9807
James Smart4f774512009-05-22 14:52:35 -04009808 /* Pass the cmd_iocb and the rsp state to the upper layer */
9809 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9810}
9811
9812/**
9813 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9814 * @phba: Pointer to HBA context object.
9815 * @cq: Pointer to completion queue.
9816 * @wcqe: Pointer to work-queue completion queue entry.
9817 *
9818 * This routine handles an fast-path WQ entry comsumed event by invoking the
9819 * proper WQ release routine to the slow-path WQ.
9820 **/
9821static void
9822lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9823 struct lpfc_wcqe_release *wcqe)
9824{
9825 struct lpfc_queue *childwq;
9826 bool wqid_matched = false;
9827 uint16_t fcp_wqid;
9828
9829 /* Check for fast-path FCP work queue release */
9830 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9831 list_for_each_entry(childwq, &cq->child_list, list) {
9832 if (childwq->queue_id == fcp_wqid) {
9833 lpfc_sli4_wq_release(childwq,
9834 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9835 wqid_matched = true;
9836 break;
9837 }
9838 }
9839 /* Report warning log message if no match found */
9840 if (wqid_matched != true)
9841 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9842 "2580 Fast-path wqe consume event carries "
9843 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9844}
9845
9846/**
9847 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9848 * @cq: Pointer to the completion queue.
9849 * @eqe: Pointer to fast-path completion queue entry.
9850 *
9851 * This routine process a fast-path work queue completion entry from fast-path
9852 * event queue for FCP command response completion.
9853 **/
9854static int
9855lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9856 struct lpfc_cqe *cqe)
9857{
9858 struct lpfc_wcqe_release wcqe;
9859 bool workposted = false;
9860
9861 /* Copy the work queue CQE and convert endian order if needed */
9862 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9863
9864 /* Check and process for different type of WCQE and dispatch */
9865 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9866 case CQE_CODE_COMPL_WQE:
9867 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009868 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009869 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9870 (struct lpfc_wcqe_complete *)&wcqe);
9871 break;
9872 case CQE_CODE_RELEASE_WQE:
9873 /* Process the WQ release event */
9874 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9875 (struct lpfc_wcqe_release *)&wcqe);
9876 break;
9877 case CQE_CODE_XRI_ABORTED:
9878 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009879 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009880 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9881 (struct sli4_wcqe_xri_aborted *)&wcqe);
9882 break;
9883 default:
9884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9885 "0144 Not a valid WCQE code: x%x\n",
9886 bf_get(lpfc_wcqe_c_code, &wcqe));
9887 break;
9888 }
9889 return workposted;
9890}
9891
9892/**
9893 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9894 * @phba: Pointer to HBA context object.
9895 * @eqe: Pointer to fast-path event queue entry.
9896 *
9897 * This routine process a event queue entry from the fast-path event queue.
9898 * It will check the MajorCode and MinorCode to determine this is for a
9899 * completion event on a completion queue, if not, an error shall be logged
9900 * and just return. Otherwise, it will get to the corresponding completion
9901 * queue and process all the entries on the completion queue, rearm the
9902 * completion queue, and then return.
9903 **/
9904static void
9905lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9906 uint32_t fcp_cqidx)
9907{
9908 struct lpfc_queue *cq;
9909 struct lpfc_cqe *cqe;
9910 bool workposted = false;
9911 uint16_t cqid;
9912 int ecount = 0;
9913
James Smartcb5172e2010-03-15 11:25:07 -04009914 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9916 "0366 Not a valid fast-path completion "
9917 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009918 bf_get_le32(lpfc_eqe_major_code, eqe),
9919 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009920 return;
9921 }
9922
9923 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9924 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009925 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9927 "0367 Fast-path completion queue "
9928 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009929 return;
9930 }
9931
9932 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009933 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009934 if (unlikely(cqid != cq->queue_id)) {
9935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9936 "0368 Miss-matched fast-path completion "
9937 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9938 cqid, cq->queue_id);
9939 return;
9940 }
9941
9942 /* Process all the entries to the CQ */
9943 while ((cqe = lpfc_sli4_cq_get(cq))) {
9944 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9945 if (!(++ecount % LPFC_GET_QE_REL_INT))
9946 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9947 }
9948
9949 /* Catch the no cq entry condition */
9950 if (unlikely(ecount == 0))
9951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9952 "0369 No entry from fast-path completion "
9953 "queue fcpcqid=%d\n", cq->queue_id);
9954
9955 /* In any case, flash and re-arm the CQ */
9956 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9957
9958 /* wake up worker thread if there are works to be done */
9959 if (workposted)
9960 lpfc_worker_wake_up(phba);
9961}
9962
9963static void
9964lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9965{
9966 struct lpfc_eqe *eqe;
9967
9968 /* walk all the EQ entries and drop on the floor */
9969 while ((eqe = lpfc_sli4_eq_get(eq)))
9970 ;
9971
9972 /* Clear and re-arm the EQ */
9973 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9974}
9975
9976/**
9977 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9978 * @irq: Interrupt number.
9979 * @dev_id: The device context pointer.
9980 *
9981 * This function is directly called from the PCI layer as an interrupt
9982 * service routine when device with SLI-4 interface spec is enabled with
9983 * MSI-X multi-message interrupt mode and there are slow-path events in
9984 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9985 * interrupt mode, this function is called as part of the device-level
9986 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9987 * undergoing initialization, the interrupt handler will not process the
9988 * interrupt. The link attention and ELS ring attention events are handled
9989 * by the worker thread. The interrupt handler signals the worker thread
9990 * and returns for these events. This function is called without any lock
9991 * held. It gets the hbalock to access and update SLI data structures.
9992 *
9993 * This function returns IRQ_HANDLED when interrupt is handled else it
9994 * returns IRQ_NONE.
9995 **/
9996irqreturn_t
9997lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9998{
9999 struct lpfc_hba *phba;
10000 struct lpfc_queue *speq;
10001 struct lpfc_eqe *eqe;
10002 unsigned long iflag;
10003 int ecount = 0;
10004
10005 /*
10006 * Get the driver's phba structure from the dev_id
10007 */
10008 phba = (struct lpfc_hba *)dev_id;
10009
10010 if (unlikely(!phba))
10011 return IRQ_NONE;
10012
10013 /* Get to the EQ struct associated with this vector */
10014 speq = phba->sli4_hba.sp_eq;
10015
10016 /* Check device state for handling interrupt */
10017 if (unlikely(lpfc_intr_state_check(phba))) {
10018 /* Check again for link_state with lock held */
10019 spin_lock_irqsave(&phba->hbalock, iflag);
10020 if (phba->link_state < LPFC_LINK_DOWN)
10021 /* Flush, clear interrupt, and rearm the EQ */
10022 lpfc_sli4_eq_flush(phba, speq);
10023 spin_unlock_irqrestore(&phba->hbalock, iflag);
10024 return IRQ_NONE;
10025 }
10026
10027 /*
10028 * Process all the event on FCP slow-path EQ
10029 */
10030 while ((eqe = lpfc_sli4_eq_get(speq))) {
10031 lpfc_sli4_sp_handle_eqe(phba, eqe);
10032 if (!(++ecount % LPFC_GET_QE_REL_INT))
10033 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10034 }
10035
10036 /* Always clear and re-arm the slow-path EQ */
10037 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10038
10039 /* Catch the no cq entry condition */
10040 if (unlikely(ecount == 0)) {
10041 if (phba->intr_type == MSIX)
10042 /* MSI-X treated interrupt served as no EQ share INT */
10043 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10044 "0357 MSI-X interrupt with no EQE\n");
10045 else
10046 /* Non MSI-X treated on interrupt as EQ share INT */
10047 return IRQ_NONE;
10048 }
10049
10050 return IRQ_HANDLED;
10051} /* lpfc_sli4_sp_intr_handler */
10052
10053/**
10054 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10055 * @irq: Interrupt number.
10056 * @dev_id: The device context pointer.
10057 *
10058 * This function is directly called from the PCI layer as an interrupt
10059 * service routine when device with SLI-4 interface spec is enabled with
10060 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10061 * ring event in the HBA. However, when the device is enabled with either
10062 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10063 * device-level interrupt handler. When the PCI slot is in error recovery
10064 * or the HBA is undergoing initialization, the interrupt handler will not
10065 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10066 * the intrrupt context. This function is called without any lock held.
10067 * It gets the hbalock to access and update SLI data structures. Note that,
10068 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10069 * equal to that of FCP CQ index.
10070 *
10071 * This function returns IRQ_HANDLED when interrupt is handled else it
10072 * returns IRQ_NONE.
10073 **/
10074irqreturn_t
10075lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10076{
10077 struct lpfc_hba *phba;
10078 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10079 struct lpfc_queue *fpeq;
10080 struct lpfc_eqe *eqe;
10081 unsigned long iflag;
10082 int ecount = 0;
10083 uint32_t fcp_eqidx;
10084
10085 /* Get the driver's phba structure from the dev_id */
10086 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10087 phba = fcp_eq_hdl->phba;
10088 fcp_eqidx = fcp_eq_hdl->idx;
10089
10090 if (unlikely(!phba))
10091 return IRQ_NONE;
10092
10093 /* Get to the EQ struct associated with this vector */
10094 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10095
10096 /* Check device state for handling interrupt */
10097 if (unlikely(lpfc_intr_state_check(phba))) {
10098 /* Check again for link_state with lock held */
10099 spin_lock_irqsave(&phba->hbalock, iflag);
10100 if (phba->link_state < LPFC_LINK_DOWN)
10101 /* Flush, clear interrupt, and rearm the EQ */
10102 lpfc_sli4_eq_flush(phba, fpeq);
10103 spin_unlock_irqrestore(&phba->hbalock, iflag);
10104 return IRQ_NONE;
10105 }
10106
10107 /*
10108 * Process all the event on FCP fast-path EQ
10109 */
10110 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10111 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10112 if (!(++ecount % LPFC_GET_QE_REL_INT))
10113 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10114 }
10115
10116 /* Always clear and re-arm the fast-path EQ */
10117 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10118
10119 if (unlikely(ecount == 0)) {
10120 if (phba->intr_type == MSIX)
10121 /* MSI-X treated interrupt served as no EQ share INT */
10122 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10123 "0358 MSI-X interrupt with no EQE\n");
10124 else
10125 /* Non MSI-X treated on interrupt as EQ share INT */
10126 return IRQ_NONE;
10127 }
10128
10129 return IRQ_HANDLED;
10130} /* lpfc_sli4_fp_intr_handler */
10131
10132/**
10133 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10134 * @irq: Interrupt number.
10135 * @dev_id: The device context pointer.
10136 *
10137 * This function is the device-level interrupt handler to device with SLI-4
10138 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10139 * interrupt mode is enabled and there is an event in the HBA which requires
10140 * driver attention. This function invokes the slow-path interrupt attention
10141 * handling function and fast-path interrupt attention handling function in
10142 * turn to process the relevant HBA attention events. This function is called
10143 * without any lock held. It gets the hbalock to access and update SLI data
10144 * structures.
10145 *
10146 * This function returns IRQ_HANDLED when interrupt is handled, else it
10147 * returns IRQ_NONE.
10148 **/
10149irqreturn_t
10150lpfc_sli4_intr_handler(int irq, void *dev_id)
10151{
10152 struct lpfc_hba *phba;
10153 irqreturn_t sp_irq_rc, fp_irq_rc;
10154 bool fp_handled = false;
10155 uint32_t fcp_eqidx;
10156
10157 /* Get the driver's phba structure from the dev_id */
10158 phba = (struct lpfc_hba *)dev_id;
10159
10160 if (unlikely(!phba))
10161 return IRQ_NONE;
10162
10163 /*
10164 * Invokes slow-path host attention interrupt handling as appropriate.
10165 */
10166 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10167
10168 /*
10169 * Invoke fast-path host attention interrupt handling as appropriate.
10170 */
10171 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10172 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10173 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10174 if (fp_irq_rc == IRQ_HANDLED)
10175 fp_handled |= true;
10176 }
10177
10178 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10179} /* lpfc_sli4_intr_handler */
10180
10181/**
10182 * lpfc_sli4_queue_free - free a queue structure and associated memory
10183 * @queue: The queue structure to free.
10184 *
10185 * This function frees a queue structure and the DMAable memeory used for
10186 * the host resident queue. This function must be called after destroying the
10187 * queue on the HBA.
10188 **/
10189void
10190lpfc_sli4_queue_free(struct lpfc_queue *queue)
10191{
10192 struct lpfc_dmabuf *dmabuf;
10193
10194 if (!queue)
10195 return;
10196
10197 while (!list_empty(&queue->page_list)) {
10198 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10199 list);
James Smart49198b32010-04-06 15:04:33 -040010200 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040010201 dmabuf->virt, dmabuf->phys);
10202 kfree(dmabuf);
10203 }
10204 kfree(queue);
10205 return;
10206}
10207
10208/**
10209 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10210 * @phba: The HBA that this queue is being created on.
10211 * @entry_size: The size of each queue entry for this queue.
10212 * @entry count: The number of entries that this queue will handle.
10213 *
10214 * This function allocates a queue structure and the DMAable memory used for
10215 * the host resident queue. This function must be called before creating the
10216 * queue on the HBA.
10217 **/
10218struct lpfc_queue *
10219lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10220 uint32_t entry_count)
10221{
10222 struct lpfc_queue *queue;
10223 struct lpfc_dmabuf *dmabuf;
10224 int x, total_qe_count;
10225 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040010226 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040010227
James Smartcb5172e2010-03-15 11:25:07 -040010228 if (!phba->sli4_hba.pc_sli4_params.supported)
10229 hw_page_size = SLI4_PAGE_SIZE;
10230
James Smart4f774512009-05-22 14:52:35 -040010231 queue = kzalloc(sizeof(struct lpfc_queue) +
10232 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10233 if (!queue)
10234 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040010235 queue->page_count = (ALIGN(entry_size * entry_count,
10236 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040010237 INIT_LIST_HEAD(&queue->list);
10238 INIT_LIST_HEAD(&queue->page_list);
10239 INIT_LIST_HEAD(&queue->child_list);
10240 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10241 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10242 if (!dmabuf)
10243 goto out_fail;
10244 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040010245 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040010246 GFP_KERNEL);
10247 if (!dmabuf->virt) {
10248 kfree(dmabuf);
10249 goto out_fail;
10250 }
James Smartcb5172e2010-03-15 11:25:07 -040010251 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010252 dmabuf->buffer_tag = x;
10253 list_add_tail(&dmabuf->list, &queue->page_list);
10254 /* initialize queue's entry array */
10255 dma_pointer = dmabuf->virt;
10256 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040010257 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040010258 total_qe_count++, dma_pointer += entry_size) {
10259 queue->qe[total_qe_count].address = dma_pointer;
10260 }
10261 }
10262 queue->entry_size = entry_size;
10263 queue->entry_count = entry_count;
10264 queue->phba = phba;
10265
10266 return queue;
10267out_fail:
10268 lpfc_sli4_queue_free(queue);
10269 return NULL;
10270}
10271
10272/**
10273 * lpfc_eq_create - Create an Event Queue on the HBA
10274 * @phba: HBA structure that indicates port to create a queue on.
10275 * @eq: The queue structure to use to create the event queue.
10276 * @imax: The maximum interrupt per second limit.
10277 *
10278 * This function creates an event queue, as detailed in @eq, on a port,
10279 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10280 *
10281 * The @phba struct is used to send mailbox command to HBA. The @eq struct
10282 * is used to get the entry count and entry size that are necessary to
10283 * determine the number of pages to allocate and use for this queue. This
10284 * function will send the EQ_CREATE mailbox command to the HBA to setup the
10285 * event queue. This function is asynchronous and will wait for the mailbox
10286 * command to finish before continuing.
10287 *
10288 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010289 * memory this function will return -ENOMEM. If the queue create mailbox command
10290 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010291 **/
10292uint32_t
10293lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10294{
10295 struct lpfc_mbx_eq_create *eq_create;
10296 LPFC_MBOXQ_t *mbox;
10297 int rc, length, status = 0;
10298 struct lpfc_dmabuf *dmabuf;
10299 uint32_t shdr_status, shdr_add_status;
10300 union lpfc_sli4_cfg_shdr *shdr;
10301 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040010302 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10303
10304 if (!phba->sli4_hba.pc_sli4_params.supported)
10305 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010306
10307 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10308 if (!mbox)
10309 return -ENOMEM;
10310 length = (sizeof(struct lpfc_mbx_eq_create) -
10311 sizeof(struct lpfc_sli4_cfg_mhdr));
10312 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10313 LPFC_MBOX_OPCODE_EQ_CREATE,
10314 length, LPFC_SLI4_MBX_EMBED);
10315 eq_create = &mbox->u.mqe.un.eq_create;
10316 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10317 eq->page_count);
10318 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10319 LPFC_EQE_SIZE);
10320 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10321 /* Calculate delay multiper from maximum interrupt per second */
10322 dmult = LPFC_DMULT_CONST/imax - 1;
10323 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10324 dmult);
10325 switch (eq->entry_count) {
10326 default:
10327 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10328 "0360 Unsupported EQ count. (%d)\n",
10329 eq->entry_count);
10330 if (eq->entry_count < 256)
10331 return -EINVAL;
10332 /* otherwise default to smallest count (drop through) */
10333 case 256:
10334 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10335 LPFC_EQ_CNT_256);
10336 break;
10337 case 512:
10338 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10339 LPFC_EQ_CNT_512);
10340 break;
10341 case 1024:
10342 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10343 LPFC_EQ_CNT_1024);
10344 break;
10345 case 2048:
10346 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10347 LPFC_EQ_CNT_2048);
10348 break;
10349 case 4096:
10350 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10351 LPFC_EQ_CNT_4096);
10352 break;
10353 }
10354 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010355 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010356 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10357 putPaddrLow(dmabuf->phys);
10358 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10359 putPaddrHigh(dmabuf->phys);
10360 }
10361 mbox->vport = phba->pport;
10362 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10363 mbox->context1 = NULL;
10364 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10365 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10366 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10367 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10368 if (shdr_status || shdr_add_status || rc) {
10369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10370 "2500 EQ_CREATE mailbox failed with "
10371 "status x%x add_status x%x, mbx status x%x\n",
10372 shdr_status, shdr_add_status, rc);
10373 status = -ENXIO;
10374 }
10375 eq->type = LPFC_EQ;
10376 eq->subtype = LPFC_NONE;
10377 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10378 if (eq->queue_id == 0xFFFF)
10379 status = -ENXIO;
10380 eq->host_index = 0;
10381 eq->hba_index = 0;
10382
James Smart8fa38512009-07-19 10:01:03 -040010383 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010384 return status;
10385}
10386
10387/**
10388 * lpfc_cq_create - Create a Completion Queue on the HBA
10389 * @phba: HBA structure that indicates port to create a queue on.
10390 * @cq: The queue structure to use to create the completion queue.
10391 * @eq: The event queue to bind this completion queue to.
10392 *
10393 * This function creates a completion queue, as detailed in @wq, on a port,
10394 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10395 *
10396 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10397 * is used to get the entry count and entry size that are necessary to
10398 * determine the number of pages to allocate and use for this queue. The @eq
10399 * is used to indicate which event queue to bind this completion queue to. This
10400 * function will send the CQ_CREATE mailbox command to the HBA to setup the
10401 * completion queue. This function is asynchronous and will wait for the mailbox
10402 * command to finish before continuing.
10403 *
10404 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010405 * memory this function will return -ENOMEM. If the queue create mailbox command
10406 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010407 **/
10408uint32_t
10409lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10410 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10411{
10412 struct lpfc_mbx_cq_create *cq_create;
10413 struct lpfc_dmabuf *dmabuf;
10414 LPFC_MBOXQ_t *mbox;
10415 int rc, length, status = 0;
10416 uint32_t shdr_status, shdr_add_status;
10417 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010418 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10419
10420 if (!phba->sli4_hba.pc_sli4_params.supported)
10421 hw_page_size = SLI4_PAGE_SIZE;
10422
James Smart4f774512009-05-22 14:52:35 -040010423
10424 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10425 if (!mbox)
10426 return -ENOMEM;
10427 length = (sizeof(struct lpfc_mbx_cq_create) -
10428 sizeof(struct lpfc_sli4_cfg_mhdr));
10429 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10430 LPFC_MBOX_OPCODE_CQ_CREATE,
10431 length, LPFC_SLI4_MBX_EMBED);
10432 cq_create = &mbox->u.mqe.un.cq_create;
10433 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10434 cq->page_count);
10435 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10436 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10437 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10438 switch (cq->entry_count) {
10439 default:
10440 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10441 "0361 Unsupported CQ count. (%d)\n",
10442 cq->entry_count);
10443 if (cq->entry_count < 256)
10444 return -EINVAL;
10445 /* otherwise default to smallest count (drop through) */
10446 case 256:
10447 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10448 LPFC_CQ_CNT_256);
10449 break;
10450 case 512:
10451 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10452 LPFC_CQ_CNT_512);
10453 break;
10454 case 1024:
10455 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10456 LPFC_CQ_CNT_1024);
10457 break;
10458 }
10459 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010460 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010461 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10462 putPaddrLow(dmabuf->phys);
10463 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10464 putPaddrHigh(dmabuf->phys);
10465 }
10466 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10467
10468 /* The IOCTL status is embedded in the mailbox subheader. */
10469 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10470 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10471 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10472 if (shdr_status || shdr_add_status || rc) {
10473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10474 "2501 CQ_CREATE mailbox failed with "
10475 "status x%x add_status x%x, mbx status x%x\n",
10476 shdr_status, shdr_add_status, rc);
10477 status = -ENXIO;
10478 goto out;
10479 }
10480 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10481 if (cq->queue_id == 0xFFFF) {
10482 status = -ENXIO;
10483 goto out;
10484 }
10485 /* link the cq onto the parent eq child list */
10486 list_add_tail(&cq->list, &eq->child_list);
10487 /* Set up completion queue's type and subtype */
10488 cq->type = type;
10489 cq->subtype = subtype;
10490 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10491 cq->host_index = 0;
10492 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010493
James Smart8fa38512009-07-19 10:01:03 -040010494out:
10495 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010496 return status;
10497}
10498
10499/**
James Smartb19a0612010-04-06 14:48:51 -040010500 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010501 * @phba: HBA structure that indicates port to create a queue on.
10502 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010503 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10504 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010505 *
James Smartb19a0612010-04-06 14:48:51 -040010506 * This function provides failback (fb) functionality when the
10507 * mq_create_ext fails on older FW generations. It's purpose is identical
10508 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010509 *
James Smartb19a0612010-04-06 14:48:51 -040010510 * This routine cannot fail as all attributes were previously accessed and
10511 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010512 **/
James Smartb19a0612010-04-06 14:48:51 -040010513static void
10514lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10515 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010516{
10517 struct lpfc_mbx_mq_create *mq_create;
10518 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010519 int length;
James Smart04c68492009-05-22 14:52:52 -040010520
James Smart04c68492009-05-22 14:52:52 -040010521 length = (sizeof(struct lpfc_mbx_mq_create) -
10522 sizeof(struct lpfc_sli4_cfg_mhdr));
10523 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10524 LPFC_MBOX_OPCODE_MQ_CREATE,
10525 length, LPFC_SLI4_MBX_EMBED);
10526 mq_create = &mbox->u.mqe.un.mq_create;
10527 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010528 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010529 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010530 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010531 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10532 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010533 case 16:
10534 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10535 LPFC_MQ_CNT_16);
10536 break;
10537 case 32:
10538 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10539 LPFC_MQ_CNT_32);
10540 break;
10541 case 64:
10542 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10543 LPFC_MQ_CNT_64);
10544 break;
10545 case 128:
10546 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10547 LPFC_MQ_CNT_128);
10548 break;
10549 }
10550 list_for_each_entry(dmabuf, &mq->page_list, list) {
10551 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010552 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010553 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010554 putPaddrHigh(dmabuf->phys);
10555 }
10556}
10557
10558/**
10559 * lpfc_mq_create - Create a mailbox Queue on the HBA
10560 * @phba: HBA structure that indicates port to create a queue on.
10561 * @mq: The queue structure to use to create the mailbox queue.
10562 * @cq: The completion queue to associate with this cq.
10563 * @subtype: The queue's subtype.
10564 *
10565 * This function creates a mailbox queue, as detailed in @mq, on a port,
10566 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10567 *
10568 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10569 * is used to get the entry count and entry size that are necessary to
10570 * determine the number of pages to allocate and use for this queue. This
10571 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10572 * mailbox queue. This function is asynchronous and will wait for the mailbox
10573 * command to finish before continuing.
10574 *
10575 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010576 * memory this function will return -ENOMEM. If the queue create mailbox command
10577 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010578 **/
10579int32_t
10580lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10581 struct lpfc_queue *cq, uint32_t subtype)
10582{
10583 struct lpfc_mbx_mq_create *mq_create;
10584 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10585 struct lpfc_dmabuf *dmabuf;
10586 LPFC_MBOXQ_t *mbox;
10587 int rc, length, status = 0;
10588 uint32_t shdr_status, shdr_add_status;
10589 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010590 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010591
James Smart49198b32010-04-06 15:04:33 -040010592 if (!phba->sli4_hba.pc_sli4_params.supported)
10593 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010594
10595 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10596 if (!mbox)
10597 return -ENOMEM;
10598 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10599 sizeof(struct lpfc_sli4_cfg_mhdr));
10600 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10601 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10602 length, LPFC_SLI4_MBX_EMBED);
10603
10604 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart70f3c072010-12-15 17:57:33 -050010605 bf_set(lpfc_mbx_mq_create_ext_num_pages,
10606 &mq_create_ext->u.request, mq->page_count);
10607 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10608 &mq_create_ext->u.request, 1);
10609 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040010610 &mq_create_ext->u.request, 1);
10611 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10612 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050010613 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10614 &mq_create_ext->u.request, 1);
10615 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10616 &mq_create_ext->u.request, 1);
10617 bf_set(lpfc_mq_context_cq_id,
10618 &mq_create_ext->u.request.context, cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040010619 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10620 switch (mq->entry_count) {
10621 default:
10622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10623 "0362 Unsupported MQ count. (%d)\n",
10624 mq->entry_count);
10625 if (mq->entry_count < 16)
10626 return -EINVAL;
10627 /* otherwise default to smallest count (drop through) */
10628 case 16:
10629 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10630 LPFC_MQ_CNT_16);
10631 break;
10632 case 32:
10633 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10634 LPFC_MQ_CNT_32);
10635 break;
10636 case 64:
10637 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10638 LPFC_MQ_CNT_64);
10639 break;
10640 case 128:
10641 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10642 LPFC_MQ_CNT_128);
10643 break;
10644 }
10645 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010646 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010647 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10648 putPaddrLow(dmabuf->phys);
10649 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010650 putPaddrHigh(dmabuf->phys);
10651 }
10652 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010653 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10654 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10655 &mq_create_ext->u.response);
10656 if (rc != MBX_SUCCESS) {
10657 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10658 "2795 MQ_CREATE_EXT failed with "
10659 "status x%x. Failback to MQ_CREATE.\n",
10660 rc);
10661 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10662 mq_create = &mbox->u.mqe.un.mq_create;
10663 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10664 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10665 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10666 &mq_create->u.response);
10667 }
10668
James Smart04c68492009-05-22 14:52:52 -040010669 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010670 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10671 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10672 if (shdr_status || shdr_add_status || rc) {
10673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10674 "2502 MQ_CREATE mailbox failed with "
10675 "status x%x add_status x%x, mbx status x%x\n",
10676 shdr_status, shdr_add_status, rc);
10677 status = -ENXIO;
10678 goto out;
10679 }
James Smart04c68492009-05-22 14:52:52 -040010680 if (mq->queue_id == 0xFFFF) {
10681 status = -ENXIO;
10682 goto out;
10683 }
10684 mq->type = LPFC_MQ;
10685 mq->subtype = subtype;
10686 mq->host_index = 0;
10687 mq->hba_index = 0;
10688
10689 /* link the mq onto the parent cq child list */
10690 list_add_tail(&mq->list, &cq->child_list);
10691out:
James Smart8fa38512009-07-19 10:01:03 -040010692 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010693 return status;
10694}
10695
10696/**
James Smart4f774512009-05-22 14:52:35 -040010697 * lpfc_wq_create - Create a Work Queue on the HBA
10698 * @phba: HBA structure that indicates port to create a queue on.
10699 * @wq: The queue structure to use to create the work queue.
10700 * @cq: The completion queue to bind this work queue to.
10701 * @subtype: The subtype of the work queue indicating its functionality.
10702 *
10703 * This function creates a work queue, as detailed in @wq, on a port, described
10704 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10705 *
10706 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10707 * is used to get the entry count and entry size that are necessary to
10708 * determine the number of pages to allocate and use for this queue. The @cq
10709 * is used to indicate which completion queue to bind this work queue to. This
10710 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10711 * work queue. This function is asynchronous and will wait for the mailbox
10712 * command to finish before continuing.
10713 *
10714 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010715 * memory this function will return -ENOMEM. If the queue create mailbox command
10716 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010717 **/
10718uint32_t
10719lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10720 struct lpfc_queue *cq, uint32_t subtype)
10721{
10722 struct lpfc_mbx_wq_create *wq_create;
10723 struct lpfc_dmabuf *dmabuf;
10724 LPFC_MBOXQ_t *mbox;
10725 int rc, length, status = 0;
10726 uint32_t shdr_status, shdr_add_status;
10727 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010728 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10729
10730 if (!phba->sli4_hba.pc_sli4_params.supported)
10731 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010732
10733 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10734 if (!mbox)
10735 return -ENOMEM;
10736 length = (sizeof(struct lpfc_mbx_wq_create) -
10737 sizeof(struct lpfc_sli4_cfg_mhdr));
10738 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10739 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10740 length, LPFC_SLI4_MBX_EMBED);
10741 wq_create = &mbox->u.mqe.un.wq_create;
10742 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10743 wq->page_count);
10744 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10745 cq->queue_id);
10746 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010747 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010748 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10749 putPaddrLow(dmabuf->phys);
10750 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10751 putPaddrHigh(dmabuf->phys);
10752 }
10753 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10754 /* The IOCTL status is embedded in the mailbox subheader. */
10755 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10756 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10757 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10758 if (shdr_status || shdr_add_status || rc) {
10759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10760 "2503 WQ_CREATE mailbox failed with "
10761 "status x%x add_status x%x, mbx status x%x\n",
10762 shdr_status, shdr_add_status, rc);
10763 status = -ENXIO;
10764 goto out;
10765 }
10766 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10767 if (wq->queue_id == 0xFFFF) {
10768 status = -ENXIO;
10769 goto out;
10770 }
10771 wq->type = LPFC_WQ;
10772 wq->subtype = subtype;
10773 wq->host_index = 0;
10774 wq->hba_index = 0;
10775
10776 /* link the wq onto the parent cq child list */
10777 list_add_tail(&wq->list, &cq->child_list);
10778out:
James Smart8fa38512009-07-19 10:01:03 -040010779 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010780 return status;
10781}
10782
10783/**
10784 * lpfc_rq_create - Create a Receive Queue on the HBA
10785 * @phba: HBA structure that indicates port to create a queue on.
10786 * @hrq: The queue structure to use to create the header receive queue.
10787 * @drq: The queue structure to use to create the data receive queue.
10788 * @cq: The completion queue to bind this work queue to.
10789 *
10790 * This function creates a receive buffer queue pair , as detailed in @hrq and
10791 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10792 * to the HBA.
10793 *
10794 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10795 * struct is used to get the entry count that is necessary to determine the
10796 * number of pages to use for this queue. The @cq is used to indicate which
10797 * completion queue to bind received buffers that are posted to these queues to.
10798 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10799 * receive queue pair. This function is asynchronous and will wait for the
10800 * mailbox command to finish before continuing.
10801 *
10802 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010803 * memory this function will return -ENOMEM. If the queue create mailbox command
10804 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010805 **/
10806uint32_t
10807lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10808 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10809{
10810 struct lpfc_mbx_rq_create *rq_create;
10811 struct lpfc_dmabuf *dmabuf;
10812 LPFC_MBOXQ_t *mbox;
10813 int rc, length, status = 0;
10814 uint32_t shdr_status, shdr_add_status;
10815 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010816 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10817
10818 if (!phba->sli4_hba.pc_sli4_params.supported)
10819 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010820
10821 if (hrq->entry_count != drq->entry_count)
10822 return -EINVAL;
10823 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10824 if (!mbox)
10825 return -ENOMEM;
10826 length = (sizeof(struct lpfc_mbx_rq_create) -
10827 sizeof(struct lpfc_sli4_cfg_mhdr));
10828 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10829 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10830 length, LPFC_SLI4_MBX_EMBED);
10831 rq_create = &mbox->u.mqe.un.rq_create;
10832 switch (hrq->entry_count) {
10833 default:
10834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10835 "2535 Unsupported RQ count. (%d)\n",
10836 hrq->entry_count);
10837 if (hrq->entry_count < 512)
10838 return -EINVAL;
10839 /* otherwise default to smallest count (drop through) */
10840 case 512:
10841 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10842 LPFC_RQ_RING_SIZE_512);
10843 break;
10844 case 1024:
10845 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10846 LPFC_RQ_RING_SIZE_1024);
10847 break;
10848 case 2048:
10849 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10850 LPFC_RQ_RING_SIZE_2048);
10851 break;
10852 case 4096:
10853 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10854 LPFC_RQ_RING_SIZE_4096);
10855 break;
10856 }
10857 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10858 cq->queue_id);
10859 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10860 hrq->page_count);
10861 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10862 LPFC_HDR_BUF_SIZE);
10863 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010864 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010865 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10866 putPaddrLow(dmabuf->phys);
10867 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10868 putPaddrHigh(dmabuf->phys);
10869 }
10870 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10871 /* The IOCTL status is embedded in the mailbox subheader. */
10872 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10873 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10874 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10875 if (shdr_status || shdr_add_status || rc) {
10876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10877 "2504 RQ_CREATE mailbox failed with "
10878 "status x%x add_status x%x, mbx status x%x\n",
10879 shdr_status, shdr_add_status, rc);
10880 status = -ENXIO;
10881 goto out;
10882 }
10883 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10884 if (hrq->queue_id == 0xFFFF) {
10885 status = -ENXIO;
10886 goto out;
10887 }
10888 hrq->type = LPFC_HRQ;
10889 hrq->subtype = subtype;
10890 hrq->host_index = 0;
10891 hrq->hba_index = 0;
10892
10893 /* now create the data queue */
10894 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10895 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10896 length, LPFC_SLI4_MBX_EMBED);
10897 switch (drq->entry_count) {
10898 default:
10899 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10900 "2536 Unsupported RQ count. (%d)\n",
10901 drq->entry_count);
10902 if (drq->entry_count < 512)
10903 return -EINVAL;
10904 /* otherwise default to smallest count (drop through) */
10905 case 512:
10906 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10907 LPFC_RQ_RING_SIZE_512);
10908 break;
10909 case 1024:
10910 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10911 LPFC_RQ_RING_SIZE_1024);
10912 break;
10913 case 2048:
10914 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10915 LPFC_RQ_RING_SIZE_2048);
10916 break;
10917 case 4096:
10918 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10919 LPFC_RQ_RING_SIZE_4096);
10920 break;
10921 }
10922 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10923 cq->queue_id);
10924 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10925 drq->page_count);
10926 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10927 LPFC_DATA_BUF_SIZE);
10928 list_for_each_entry(dmabuf, &drq->page_list, list) {
10929 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10930 putPaddrLow(dmabuf->phys);
10931 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10932 putPaddrHigh(dmabuf->phys);
10933 }
10934 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10935 /* The IOCTL status is embedded in the mailbox subheader. */
10936 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10937 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10938 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10939 if (shdr_status || shdr_add_status || rc) {
10940 status = -ENXIO;
10941 goto out;
10942 }
10943 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10944 if (drq->queue_id == 0xFFFF) {
10945 status = -ENXIO;
10946 goto out;
10947 }
10948 drq->type = LPFC_DRQ;
10949 drq->subtype = subtype;
10950 drq->host_index = 0;
10951 drq->hba_index = 0;
10952
10953 /* link the header and data RQs onto the parent cq child list */
10954 list_add_tail(&hrq->list, &cq->child_list);
10955 list_add_tail(&drq->list, &cq->child_list);
10956
10957out:
James Smart8fa38512009-07-19 10:01:03 -040010958 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010959 return status;
10960}
10961
10962/**
10963 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10964 * @eq: The queue structure associated with the queue to destroy.
10965 *
10966 * This function destroys a queue, as detailed in @eq by sending an mailbox
10967 * command, specific to the type of queue, to the HBA.
10968 *
10969 * The @eq struct is used to get the queue ID of the queue to destroy.
10970 *
10971 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010972 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010973 **/
10974uint32_t
10975lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10976{
10977 LPFC_MBOXQ_t *mbox;
10978 int rc, length, status = 0;
10979 uint32_t shdr_status, shdr_add_status;
10980 union lpfc_sli4_cfg_shdr *shdr;
10981
10982 if (!eq)
10983 return -ENODEV;
10984 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10985 if (!mbox)
10986 return -ENOMEM;
10987 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10988 sizeof(struct lpfc_sli4_cfg_mhdr));
10989 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10990 LPFC_MBOX_OPCODE_EQ_DESTROY,
10991 length, LPFC_SLI4_MBX_EMBED);
10992 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10993 eq->queue_id);
10994 mbox->vport = eq->phba->pport;
10995 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10996
10997 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10998 /* The IOCTL status is embedded in the mailbox subheader. */
10999 shdr = (union lpfc_sli4_cfg_shdr *)
11000 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
11001 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11002 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11003 if (shdr_status || shdr_add_status || rc) {
11004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11005 "2505 EQ_DESTROY mailbox failed with "
11006 "status x%x add_status x%x, mbx status x%x\n",
11007 shdr_status, shdr_add_status, rc);
11008 status = -ENXIO;
11009 }
11010
11011 /* Remove eq from any list */
11012 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040011013 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011014 return status;
11015}
11016
11017/**
11018 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11019 * @cq: The queue structure associated with the queue to destroy.
11020 *
11021 * This function destroys a queue, as detailed in @cq by sending an mailbox
11022 * command, specific to the type of queue, to the HBA.
11023 *
11024 * The @cq struct is used to get the queue ID of the queue to destroy.
11025 *
11026 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011027 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011028 **/
11029uint32_t
11030lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11031{
11032 LPFC_MBOXQ_t *mbox;
11033 int rc, length, status = 0;
11034 uint32_t shdr_status, shdr_add_status;
11035 union lpfc_sli4_cfg_shdr *shdr;
11036
11037 if (!cq)
11038 return -ENODEV;
11039 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11040 if (!mbox)
11041 return -ENOMEM;
11042 length = (sizeof(struct lpfc_mbx_cq_destroy) -
11043 sizeof(struct lpfc_sli4_cfg_mhdr));
11044 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11045 LPFC_MBOX_OPCODE_CQ_DESTROY,
11046 length, LPFC_SLI4_MBX_EMBED);
11047 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11048 cq->queue_id);
11049 mbox->vport = cq->phba->pport;
11050 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11051 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11052 /* The IOCTL status is embedded in the mailbox subheader. */
11053 shdr = (union lpfc_sli4_cfg_shdr *)
11054 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11055 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11056 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11057 if (shdr_status || shdr_add_status || rc) {
11058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11059 "2506 CQ_DESTROY mailbox failed with "
11060 "status x%x add_status x%x, mbx status x%x\n",
11061 shdr_status, shdr_add_status, rc);
11062 status = -ENXIO;
11063 }
11064 /* Remove cq from any list */
11065 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040011066 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011067 return status;
11068}
11069
11070/**
James Smart04c68492009-05-22 14:52:52 -040011071 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11072 * @qm: The queue structure associated with the queue to destroy.
11073 *
11074 * This function destroys a queue, as detailed in @mq by sending an mailbox
11075 * command, specific to the type of queue, to the HBA.
11076 *
11077 * The @mq struct is used to get the queue ID of the queue to destroy.
11078 *
11079 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011080 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040011081 **/
11082uint32_t
11083lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11084{
11085 LPFC_MBOXQ_t *mbox;
11086 int rc, length, status = 0;
11087 uint32_t shdr_status, shdr_add_status;
11088 union lpfc_sli4_cfg_shdr *shdr;
11089
11090 if (!mq)
11091 return -ENODEV;
11092 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11093 if (!mbox)
11094 return -ENOMEM;
11095 length = (sizeof(struct lpfc_mbx_mq_destroy) -
11096 sizeof(struct lpfc_sli4_cfg_mhdr));
11097 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11098 LPFC_MBOX_OPCODE_MQ_DESTROY,
11099 length, LPFC_SLI4_MBX_EMBED);
11100 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11101 mq->queue_id);
11102 mbox->vport = mq->phba->pport;
11103 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11104 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11105 /* The IOCTL status is embedded in the mailbox subheader. */
11106 shdr = (union lpfc_sli4_cfg_shdr *)
11107 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11108 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11109 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11110 if (shdr_status || shdr_add_status || rc) {
11111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11112 "2507 MQ_DESTROY mailbox failed with "
11113 "status x%x add_status x%x, mbx status x%x\n",
11114 shdr_status, shdr_add_status, rc);
11115 status = -ENXIO;
11116 }
11117 /* Remove mq from any list */
11118 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040011119 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011120 return status;
11121}
11122
11123/**
James Smart4f774512009-05-22 14:52:35 -040011124 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11125 * @wq: The queue structure associated with the queue to destroy.
11126 *
11127 * This function destroys a queue, as detailed in @wq by sending an mailbox
11128 * command, specific to the type of queue, to the HBA.
11129 *
11130 * The @wq struct is used to get the queue ID of the queue to destroy.
11131 *
11132 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011133 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011134 **/
11135uint32_t
11136lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11137{
11138 LPFC_MBOXQ_t *mbox;
11139 int rc, length, status = 0;
11140 uint32_t shdr_status, shdr_add_status;
11141 union lpfc_sli4_cfg_shdr *shdr;
11142
11143 if (!wq)
11144 return -ENODEV;
11145 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11146 if (!mbox)
11147 return -ENOMEM;
11148 length = (sizeof(struct lpfc_mbx_wq_destroy) -
11149 sizeof(struct lpfc_sli4_cfg_mhdr));
11150 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11151 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11152 length, LPFC_SLI4_MBX_EMBED);
11153 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11154 wq->queue_id);
11155 mbox->vport = wq->phba->pport;
11156 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11157 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11158 shdr = (union lpfc_sli4_cfg_shdr *)
11159 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11160 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11161 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11162 if (shdr_status || shdr_add_status || rc) {
11163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11164 "2508 WQ_DESTROY mailbox failed with "
11165 "status x%x add_status x%x, mbx status x%x\n",
11166 shdr_status, shdr_add_status, rc);
11167 status = -ENXIO;
11168 }
11169 /* Remove wq from any list */
11170 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040011171 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011172 return status;
11173}
11174
11175/**
11176 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11177 * @rq: The queue structure associated with the queue to destroy.
11178 *
11179 * This function destroys a queue, as detailed in @rq by sending an mailbox
11180 * command, specific to the type of queue, to the HBA.
11181 *
11182 * The @rq struct is used to get the queue ID of the queue to destroy.
11183 *
11184 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011185 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011186 **/
11187uint32_t
11188lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11189 struct lpfc_queue *drq)
11190{
11191 LPFC_MBOXQ_t *mbox;
11192 int rc, length, status = 0;
11193 uint32_t shdr_status, shdr_add_status;
11194 union lpfc_sli4_cfg_shdr *shdr;
11195
11196 if (!hrq || !drq)
11197 return -ENODEV;
11198 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11199 if (!mbox)
11200 return -ENOMEM;
11201 length = (sizeof(struct lpfc_mbx_rq_destroy) -
11202 sizeof(struct mbox_header));
11203 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11204 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11205 length, LPFC_SLI4_MBX_EMBED);
11206 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11207 hrq->queue_id);
11208 mbox->vport = hrq->phba->pport;
11209 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11210 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11211 /* The IOCTL status is embedded in the mailbox subheader. */
11212 shdr = (union lpfc_sli4_cfg_shdr *)
11213 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11214 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11215 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11216 if (shdr_status || shdr_add_status || rc) {
11217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11218 "2509 RQ_DESTROY mailbox failed with "
11219 "status x%x add_status x%x, mbx status x%x\n",
11220 shdr_status, shdr_add_status, rc);
11221 if (rc != MBX_TIMEOUT)
11222 mempool_free(mbox, hrq->phba->mbox_mem_pool);
11223 return -ENXIO;
11224 }
11225 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11226 drq->queue_id);
11227 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11228 shdr = (union lpfc_sli4_cfg_shdr *)
11229 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11230 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11231 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11232 if (shdr_status || shdr_add_status || rc) {
11233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11234 "2510 RQ_DESTROY mailbox failed with "
11235 "status x%x add_status x%x, mbx status x%x\n",
11236 shdr_status, shdr_add_status, rc);
11237 status = -ENXIO;
11238 }
11239 list_del_init(&hrq->list);
11240 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040011241 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011242 return status;
11243}
11244
11245/**
11246 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11247 * @phba: The virtual port for which this call being executed.
11248 * @pdma_phys_addr0: Physical address of the 1st SGL page.
11249 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11250 * @xritag: the xritag that ties this io to the SGL pages.
11251 *
11252 * This routine will post the sgl pages for the IO that has the xritag
11253 * that is in the iocbq structure. The xritag is assigned during iocbq
11254 * creation and persists for as long as the driver is loaded.
11255 * if the caller has fewer than 256 scatter gather segments to map then
11256 * pdma_phys_addr1 should be 0.
11257 * If the caller needs to map more than 256 scatter gather segment then
11258 * pdma_phys_addr1 should be a valid physical address.
11259 * physical address for SGLs must be 64 byte aligned.
11260 * If you are going to map 2 SGL's then the first one must have 256 entries
11261 * the second sgl can have between 1 and 256 entries.
11262 *
11263 * Return codes:
11264 * 0 - Success
11265 * -ENXIO, -ENOMEM - Failure
11266 **/
11267int
11268lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11269 dma_addr_t pdma_phys_addr0,
11270 dma_addr_t pdma_phys_addr1,
11271 uint16_t xritag)
11272{
11273 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11274 LPFC_MBOXQ_t *mbox;
11275 int rc;
11276 uint32_t shdr_status, shdr_add_status;
11277 union lpfc_sli4_cfg_shdr *shdr;
11278
11279 if (xritag == NO_XRI) {
11280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11281 "0364 Invalid param:\n");
11282 return -EINVAL;
11283 }
11284
11285 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11286 if (!mbox)
11287 return -ENOMEM;
11288
11289 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11290 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11291 sizeof(struct lpfc_mbx_post_sgl_pages) -
11292 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11293
11294 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11295 &mbox->u.mqe.un.post_sgl_pages;
11296 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11297 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11298
11299 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11300 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11301 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11302 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11303
11304 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11305 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11306 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11307 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11308 if (!phba->sli4_hba.intr_enable)
11309 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11310 else
11311 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11312 /* The IOCTL status is embedded in the mailbox subheader. */
11313 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11314 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11315 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11316 if (rc != MBX_TIMEOUT)
11317 mempool_free(mbox, phba->mbox_mem_pool);
11318 if (shdr_status || shdr_add_status || rc) {
11319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11320 "2511 POST_SGL mailbox failed with "
11321 "status x%x add_status x%x, mbx status x%x\n",
11322 shdr_status, shdr_add_status, rc);
11323 rc = -ENXIO;
11324 }
11325 return 0;
11326}
James Smart4f774512009-05-22 14:52:35 -040011327
11328/**
11329 * lpfc_sli4_next_xritag - Get an xritag for the io
11330 * @phba: Pointer to HBA context object.
11331 *
11332 * This function gets an xritag for the iocb. If there is no unused xritag
11333 * it will return 0xffff.
11334 * The function returns the allocated xritag if successful, else returns zero.
11335 * Zero is not a valid xritag.
11336 * The caller is not required to hold any lock.
11337 **/
11338uint16_t
11339lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11340{
11341 uint16_t xritag;
11342
11343 spin_lock_irq(&phba->hbalock);
11344 xritag = phba->sli4_hba.next_xri;
11345 if ((xritag != (uint16_t) -1) && xritag <
11346 (phba->sli4_hba.max_cfg_param.max_xri
11347 + phba->sli4_hba.max_cfg_param.xri_base)) {
11348 phba->sli4_hba.next_xri++;
11349 phba->sli4_hba.max_cfg_param.xri_used++;
11350 spin_unlock_irq(&phba->hbalock);
11351 return xritag;
11352 }
11353 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040011354 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040011355 "2004 Failed to allocate XRI.last XRITAG is %d"
11356 " Max XRI is %d, Used XRI is %d\n",
11357 phba->sli4_hba.next_xri,
11358 phba->sli4_hba.max_cfg_param.max_xri,
11359 phba->sli4_hba.max_cfg_param.xri_used);
11360 return -1;
11361}
11362
11363/**
11364 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11365 * @phba: pointer to lpfc hba data structure.
11366 *
11367 * This routine is invoked to post a block of driver's sgl pages to the
11368 * HBA using non-embedded mailbox command. No Lock is held. This routine
11369 * is only called when the driver is loading and after all IO has been
11370 * stopped.
11371 **/
11372int
11373lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11374{
11375 struct lpfc_sglq *sglq_entry;
11376 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11377 struct sgl_page_pairs *sgl_pg_pairs;
11378 void *viraddr;
11379 LPFC_MBOXQ_t *mbox;
11380 uint32_t reqlen, alloclen, pg_pairs;
11381 uint32_t mbox_tmo;
11382 uint16_t xritag_start = 0;
11383 int els_xri_cnt, rc = 0;
11384 uint32_t shdr_status, shdr_add_status;
11385 union lpfc_sli4_cfg_shdr *shdr;
11386
11387 /* The number of sgls to be posted */
11388 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11389
11390 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11391 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011392 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011393 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11394 "2559 Block sgl registration required DMA "
11395 "size (%d) great than a page\n", reqlen);
11396 return -ENOMEM;
11397 }
11398 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11399 if (!mbox) {
11400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11401 "2560 Failed to allocate mbox cmd memory\n");
11402 return -ENOMEM;
11403 }
11404
11405 /* Allocate DMA memory and set up the non-embedded mailbox command */
11406 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11407 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11408 LPFC_SLI4_MBX_NEMBED);
11409
11410 if (alloclen < reqlen) {
11411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11412 "0285 Allocated DMA memory size (%d) is "
11413 "less than the requested DMA memory "
11414 "size (%d)\n", alloclen, reqlen);
11415 lpfc_sli4_mbox_cmd_free(phba, mbox);
11416 return -ENOMEM;
11417 }
James Smart4f774512009-05-22 14:52:35 -040011418 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011419 viraddr = mbox->sge_array->addr[0];
11420
11421 /* Set up the SGL pages in the non-embedded DMA pages */
11422 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11423 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11424
11425 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11426 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11427 /* Set up the sge entry */
11428 sgl_pg_pairs->sgl_pg0_addr_lo =
11429 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11430 sgl_pg_pairs->sgl_pg0_addr_hi =
11431 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11432 sgl_pg_pairs->sgl_pg1_addr_lo =
11433 cpu_to_le32(putPaddrLow(0));
11434 sgl_pg_pairs->sgl_pg1_addr_hi =
11435 cpu_to_le32(putPaddrHigh(0));
11436 /* Keep the first xritag on the list */
11437 if (pg_pairs == 0)
11438 xritag_start = sglq_entry->sli4_xritag;
11439 sgl_pg_pairs++;
11440 }
11441 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040011442 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040011443 /* Perform endian conversion if necessary */
11444 sgl->word0 = cpu_to_le32(sgl->word0);
11445
11446 if (!phba->sli4_hba.intr_enable)
11447 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11448 else {
11449 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11450 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11451 }
11452 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11453 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11454 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11455 if (rc != MBX_TIMEOUT)
11456 lpfc_sli4_mbox_cmd_free(phba, mbox);
11457 if (shdr_status || shdr_add_status || rc) {
11458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11459 "2513 POST_SGL_BLOCK mailbox command failed "
11460 "status x%x add_status x%x mbx status x%x\n",
11461 shdr_status, shdr_add_status, rc);
11462 rc = -ENXIO;
11463 }
11464 return rc;
11465}
11466
11467/**
11468 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11469 * @phba: pointer to lpfc hba data structure.
11470 * @sblist: pointer to scsi buffer list.
11471 * @count: number of scsi buffers on the list.
11472 *
11473 * This routine is invoked to post a block of @count scsi sgl pages from a
11474 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11475 * No Lock is held.
11476 *
11477 **/
11478int
11479lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11480 int cnt)
11481{
11482 struct lpfc_scsi_buf *psb;
11483 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11484 struct sgl_page_pairs *sgl_pg_pairs;
11485 void *viraddr;
11486 LPFC_MBOXQ_t *mbox;
11487 uint32_t reqlen, alloclen, pg_pairs;
11488 uint32_t mbox_tmo;
11489 uint16_t xritag_start = 0;
11490 int rc = 0;
11491 uint32_t shdr_status, shdr_add_status;
11492 dma_addr_t pdma_phys_bpl1;
11493 union lpfc_sli4_cfg_shdr *shdr;
11494
11495 /* Calculate the requested length of the dma memory */
11496 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11497 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011498 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011499 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11500 "0217 Block sgl registration required DMA "
11501 "size (%d) great than a page\n", reqlen);
11502 return -ENOMEM;
11503 }
11504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11505 if (!mbox) {
11506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11507 "0283 Failed to allocate mbox cmd memory\n");
11508 return -ENOMEM;
11509 }
11510
11511 /* Allocate DMA memory and set up the non-embedded mailbox command */
11512 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11513 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11514 LPFC_SLI4_MBX_NEMBED);
11515
11516 if (alloclen < reqlen) {
11517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11518 "2561 Allocated DMA memory size (%d) is "
11519 "less than the requested DMA memory "
11520 "size (%d)\n", alloclen, reqlen);
11521 lpfc_sli4_mbox_cmd_free(phba, mbox);
11522 return -ENOMEM;
11523 }
James Smart4f774512009-05-22 14:52:35 -040011524 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011525 viraddr = mbox->sge_array->addr[0];
11526
11527 /* Set up the SGL pages in the non-embedded DMA pages */
11528 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11529 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11530
11531 pg_pairs = 0;
11532 list_for_each_entry(psb, sblist, list) {
11533 /* Set up the sge entry */
11534 sgl_pg_pairs->sgl_pg0_addr_lo =
11535 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11536 sgl_pg_pairs->sgl_pg0_addr_hi =
11537 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11538 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11539 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11540 else
11541 pdma_phys_bpl1 = 0;
11542 sgl_pg_pairs->sgl_pg1_addr_lo =
11543 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11544 sgl_pg_pairs->sgl_pg1_addr_hi =
11545 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11546 /* Keep the first xritag on the list */
11547 if (pg_pairs == 0)
11548 xritag_start = psb->cur_iocbq.sli4_xritag;
11549 sgl_pg_pairs++;
11550 pg_pairs++;
11551 }
11552 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11553 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11554 /* Perform endian conversion if necessary */
11555 sgl->word0 = cpu_to_le32(sgl->word0);
11556
11557 if (!phba->sli4_hba.intr_enable)
11558 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11559 else {
11560 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11561 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11562 }
11563 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11566 if (rc != MBX_TIMEOUT)
11567 lpfc_sli4_mbox_cmd_free(phba, mbox);
11568 if (shdr_status || shdr_add_status || rc) {
11569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11570 "2564 POST_SGL_BLOCK mailbox command failed "
11571 "status x%x add_status x%x mbx status x%x\n",
11572 shdr_status, shdr_add_status, rc);
11573 rc = -ENXIO;
11574 }
11575 return rc;
11576}
11577
11578/**
11579 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11580 * @phba: pointer to lpfc_hba struct that the frame was received on
11581 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11582 *
11583 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11584 * valid type of frame that the LPFC driver will handle. This function will
11585 * return a zero if the frame is a valid frame or a non zero value when the
11586 * frame does not pass the check.
11587 **/
11588static int
11589lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11590{
11591 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11592 char *type_names[] = FC_TYPE_NAMES_INIT;
11593 struct fc_vft_header *fc_vft_hdr;
11594
11595 switch (fc_hdr->fh_r_ctl) {
11596 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11597 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11598 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11599 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11600 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11601 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11602 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11603 case FC_RCTL_DD_CMD_STATUS: /* command status */
11604 case FC_RCTL_ELS_REQ: /* extended link services request */
11605 case FC_RCTL_ELS_REP: /* extended link services reply */
11606 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11607 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11608 case FC_RCTL_BA_NOP: /* basic link service NOP */
11609 case FC_RCTL_BA_ABTS: /* basic link service abort */
11610 case FC_RCTL_BA_RMC: /* remove connection */
11611 case FC_RCTL_BA_ACC: /* basic accept */
11612 case FC_RCTL_BA_RJT: /* basic reject */
11613 case FC_RCTL_BA_PRMT:
11614 case FC_RCTL_ACK_1: /* acknowledge_1 */
11615 case FC_RCTL_ACK_0: /* acknowledge_0 */
11616 case FC_RCTL_P_RJT: /* port reject */
11617 case FC_RCTL_F_RJT: /* fabric reject */
11618 case FC_RCTL_P_BSY: /* port busy */
11619 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11620 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11621 case FC_RCTL_LCR: /* link credit reset */
11622 case FC_RCTL_END: /* end */
11623 break;
11624 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11625 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11626 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11627 return lpfc_fc_frame_check(phba, fc_hdr);
11628 default:
11629 goto drop;
11630 }
11631 switch (fc_hdr->fh_type) {
11632 case FC_TYPE_BLS:
11633 case FC_TYPE_ELS:
11634 case FC_TYPE_FCP:
11635 case FC_TYPE_CT:
11636 break;
11637 case FC_TYPE_IP:
11638 case FC_TYPE_ILS:
11639 default:
11640 goto drop;
11641 }
11642 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11643 "2538 Received frame rctl:%s type:%s\n",
11644 rctl_names[fc_hdr->fh_r_ctl],
11645 type_names[fc_hdr->fh_type]);
11646 return 0;
11647drop:
11648 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11649 "2539 Dropped frame rctl:%s type:%s\n",
11650 rctl_names[fc_hdr->fh_r_ctl],
11651 type_names[fc_hdr->fh_type]);
11652 return 1;
11653}
11654
11655/**
11656 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11657 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11658 *
11659 * This function processes the FC header to retrieve the VFI from the VF
11660 * header, if one exists. This function will return the VFI if one exists
11661 * or 0 if no VSAN Header exists.
11662 **/
11663static uint32_t
11664lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11665{
11666 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11667
11668 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11669 return 0;
11670 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11671}
11672
11673/**
11674 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11675 * @phba: Pointer to the HBA structure to search for the vport on
11676 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11677 * @fcfi: The FC Fabric ID that the frame came from
11678 *
11679 * This function searches the @phba for a vport that matches the content of the
11680 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11681 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11682 * returns the matching vport pointer or NULL if unable to match frame to a
11683 * vport.
11684 **/
11685static struct lpfc_vport *
11686lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11687 uint16_t fcfi)
11688{
11689 struct lpfc_vport **vports;
11690 struct lpfc_vport *vport = NULL;
11691 int i;
11692 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11693 fc_hdr->fh_d_id[1] << 8 |
11694 fc_hdr->fh_d_id[2]);
11695
11696 vports = lpfc_create_vport_work_array(phba);
11697 if (vports != NULL)
11698 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11699 if (phba->fcf.fcfi == fcfi &&
11700 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11701 vports[i]->fc_myDID == did) {
11702 vport = vports[i];
11703 break;
11704 }
11705 }
11706 lpfc_destroy_vport_work_array(phba, vports);
11707 return vport;
11708}
11709
11710/**
James Smart45ed1192009-10-02 15:17:02 -040011711 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11712 * @vport: The vport to work on.
11713 *
11714 * This function updates the receive sequence time stamp for this vport. The
11715 * receive sequence time stamp indicates the time that the last frame of the
11716 * the sequence that has been idle for the longest amount of time was received.
11717 * the driver uses this time stamp to indicate if any received sequences have
11718 * timed out.
11719 **/
11720void
11721lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11722{
11723 struct lpfc_dmabuf *h_buf;
11724 struct hbq_dmabuf *dmabuf = NULL;
11725
11726 /* get the oldest sequence on the rcv list */
11727 h_buf = list_get_first(&vport->rcv_buffer_list,
11728 struct lpfc_dmabuf, list);
11729 if (!h_buf)
11730 return;
11731 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11732 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11733}
11734
11735/**
11736 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11737 * @vport: The vport that the received sequences were sent to.
11738 *
11739 * This function cleans up all outstanding received sequences. This is called
11740 * by the driver when a link event or user action invalidates all the received
11741 * sequences.
11742 **/
11743void
11744lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11745{
11746 struct lpfc_dmabuf *h_buf, *hnext;
11747 struct lpfc_dmabuf *d_buf, *dnext;
11748 struct hbq_dmabuf *dmabuf = NULL;
11749
11750 /* start with the oldest sequence on the rcv list */
11751 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11752 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11753 list_del_init(&dmabuf->hbuf.list);
11754 list_for_each_entry_safe(d_buf, dnext,
11755 &dmabuf->dbuf.list, list) {
11756 list_del_init(&d_buf->list);
11757 lpfc_in_buf_free(vport->phba, d_buf);
11758 }
11759 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11760 }
11761}
11762
11763/**
11764 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11765 * @vport: The vport that the received sequences were sent to.
11766 *
11767 * This function determines whether any received sequences have timed out by
11768 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11769 * indicates that there is at least one timed out sequence this routine will
11770 * go through the received sequences one at a time from most inactive to most
11771 * active to determine which ones need to be cleaned up. Once it has determined
11772 * that a sequence needs to be cleaned up it will simply free up the resources
11773 * without sending an abort.
11774 **/
11775void
11776lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11777{
11778 struct lpfc_dmabuf *h_buf, *hnext;
11779 struct lpfc_dmabuf *d_buf, *dnext;
11780 struct hbq_dmabuf *dmabuf = NULL;
11781 unsigned long timeout;
11782 int abort_count = 0;
11783
11784 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11785 vport->rcv_buffer_time_stamp);
11786 if (list_empty(&vport->rcv_buffer_list) ||
11787 time_before(jiffies, timeout))
11788 return;
11789 /* start with the oldest sequence on the rcv list */
11790 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11791 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11792 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11793 dmabuf->time_stamp);
11794 if (time_before(jiffies, timeout))
11795 break;
11796 abort_count++;
11797 list_del_init(&dmabuf->hbuf.list);
11798 list_for_each_entry_safe(d_buf, dnext,
11799 &dmabuf->dbuf.list, list) {
11800 list_del_init(&d_buf->list);
11801 lpfc_in_buf_free(vport->phba, d_buf);
11802 }
11803 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11804 }
11805 if (abort_count)
11806 lpfc_update_rcv_time_stamp(vport);
11807}
11808
11809/**
James Smart4f774512009-05-22 14:52:35 -040011810 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11811 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11812 *
11813 * This function searches through the existing incomplete sequences that have
11814 * been sent to this @vport. If the frame matches one of the incomplete
11815 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11816 * make up that sequence. If no sequence is found that matches this frame then
11817 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11818 * This function returns a pointer to the first dmabuf in the sequence list that
11819 * the frame was linked to.
11820 **/
11821static struct hbq_dmabuf *
11822lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11823{
11824 struct fc_frame_header *new_hdr;
11825 struct fc_frame_header *temp_hdr;
11826 struct lpfc_dmabuf *d_buf;
11827 struct lpfc_dmabuf *h_buf;
11828 struct hbq_dmabuf *seq_dmabuf = NULL;
11829 struct hbq_dmabuf *temp_dmabuf = NULL;
11830
James Smart4d9ab992009-10-02 15:16:39 -040011831 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011832 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011833 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11834 /* Use the hdr_buf to find the sequence that this frame belongs to */
11835 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11836 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11837 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11838 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11839 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11840 continue;
11841 /* found a pending sequence that matches this frame */
11842 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11843 break;
11844 }
11845 if (!seq_dmabuf) {
11846 /*
11847 * This indicates first frame received for this sequence.
11848 * Queue the buffer on the vport's rcv_buffer_list.
11849 */
11850 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011851 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011852 return dmabuf;
11853 }
11854 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011855 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11856 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011857 list_del_init(&seq_dmabuf->hbuf.list);
11858 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11859 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011860 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011861 return dmabuf;
11862 }
James Smart45ed1192009-10-02 15:17:02 -040011863 /* move this sequence to the tail to indicate a young sequence */
11864 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11865 seq_dmabuf->time_stamp = jiffies;
11866 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011867 if (list_empty(&seq_dmabuf->dbuf.list)) {
11868 temp_hdr = dmabuf->hbuf.virt;
11869 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11870 return seq_dmabuf;
11871 }
James Smart4f774512009-05-22 14:52:35 -040011872 /* find the correct place in the sequence to insert this frame */
11873 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11874 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11875 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11876 /*
11877 * If the frame's sequence count is greater than the frame on
11878 * the list then insert the frame right after this frame
11879 */
James Smarteeead812009-12-21 17:01:23 -050011880 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11881 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011882 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11883 return seq_dmabuf;
11884 }
11885 }
11886 return NULL;
11887}
11888
11889/**
James Smart6669f9b2009-10-02 15:16:45 -040011890 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11891 * @vport: pointer to a vitural port
11892 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11893 *
11894 * This function tries to abort from the partially assembed sequence, described
11895 * by the information from basic abbort @dmabuf. It checks to see whether such
11896 * partially assembled sequence held by the driver. If so, it shall free up all
11897 * the frames from the partially assembled sequence.
11898 *
11899 * Return
11900 * true -- if there is matching partially assembled sequence present and all
11901 * the frames freed with the sequence;
11902 * false -- if there is no matching partially assembled sequence present so
11903 * nothing got aborted in the lower layer driver
11904 **/
11905static bool
11906lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11907 struct hbq_dmabuf *dmabuf)
11908{
11909 struct fc_frame_header *new_hdr;
11910 struct fc_frame_header *temp_hdr;
11911 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11912 struct hbq_dmabuf *seq_dmabuf = NULL;
11913
11914 /* Use the hdr_buf to find the sequence that matches this frame */
11915 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11916 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11917 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11918 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11919 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11920 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11921 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11922 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11923 continue;
11924 /* found a pending sequence that matches this frame */
11925 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11926 break;
11927 }
11928
11929 /* Free up all the frames from the partially assembled sequence */
11930 if (seq_dmabuf) {
11931 list_for_each_entry_safe(d_buf, n_buf,
11932 &seq_dmabuf->dbuf.list, list) {
11933 list_del_init(&d_buf->list);
11934 lpfc_in_buf_free(vport->phba, d_buf);
11935 }
11936 return true;
11937 }
11938 return false;
11939}
11940
11941/**
11942 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11943 * @phba: Pointer to HBA context object.
11944 * @cmd_iocbq: pointer to the command iocbq structure.
11945 * @rsp_iocbq: pointer to the response iocbq structure.
11946 *
11947 * This function handles the sequence abort accept iocb command complete
11948 * event. It properly releases the memory allocated to the sequence abort
11949 * accept iocb.
11950 **/
11951static void
11952lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11953 struct lpfc_iocbq *cmd_iocbq,
11954 struct lpfc_iocbq *rsp_iocbq)
11955{
11956 if (cmd_iocbq)
11957 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11958}
11959
11960/**
11961 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11962 * @phba: Pointer to HBA context object.
11963 * @fc_hdr: pointer to a FC frame header.
11964 *
11965 * This function sends a basic accept to a previous unsol sequence abort
11966 * event after aborting the sequence handling.
11967 **/
11968static void
11969lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11970 struct fc_frame_header *fc_hdr)
11971{
11972 struct lpfc_iocbq *ctiocb = NULL;
11973 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011974 uint16_t oxid, rxid;
11975 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011976 IOCB_t *icmd;
11977
11978 if (!lpfc_is_link_up(phba))
11979 return;
11980
11981 sid = sli4_sid_from_fc_hdr(fc_hdr);
11982 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011983 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011984
11985 ndlp = lpfc_findnode_did(phba->pport, sid);
11986 if (!ndlp) {
11987 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11988 "1268 Find ndlp returned NULL for oxid:x%x "
11989 "SID:x%x\n", oxid, sid);
11990 return;
11991 }
James Smart19ca7602010-11-20 23:11:55 -050011992 if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
11993 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
11994 + phba->sli4_hba.max_cfg_param.xri_base))
11995 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040011996
11997 /* Allocate buffer for acc iocb */
11998 ctiocb = lpfc_sli_get_iocbq(phba);
11999 if (!ctiocb)
12000 return;
12001
James Smart5ffc2662009-11-18 15:39:44 -050012002 /* Extract the F_CTL field from FC_HDR */
12003 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
12004
James Smart6669f9b2009-10-02 15:16:45 -040012005 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040012006 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050012007 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040012008 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12009 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
12010 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12011
12012 /* Fill in the rest of iocb fields */
12013 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12014 icmd->ulpBdeCount = 0;
12015 icmd->ulpLe = 1;
12016 icmd->ulpClass = CLASS3;
12017 icmd->ulpContext = ndlp->nlp_rpi;
James Smartbe858b62010-12-15 17:57:20 -050012018 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040012019
James Smart6669f9b2009-10-02 15:16:45 -040012020 ctiocb->iocb_cmpl = NULL;
12021 ctiocb->vport = phba->pport;
12022 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
12023
James Smart5ffc2662009-11-18 15:39:44 -050012024 if (fctl & FC_FC_EX_CTX) {
12025 /* ABTS sent by responder to CT exchange, construction
12026 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12027 * field and RX_ID from ABTS for RX_ID field.
12028 */
12029 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
12030 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
12031 ctiocb->sli4_xritag = oxid;
12032 } else {
12033 /* ABTS sent by initiator to CT exchange, construction
12034 * of BA_ACC will need to allocate a new XRI as for the
12035 * XRI_TAG and RX_ID fields.
12036 */
12037 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
12038 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
12039 ctiocb->sli4_xritag = NO_XRI;
12040 }
12041 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
12042
James Smart6669f9b2009-10-02 15:16:45 -040012043 /* Xmit CT abts accept on exchange <xid> */
12044 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12045 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
12046 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
12047 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12048}
12049
12050/**
12051 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12052 * @vport: Pointer to the vport on which this sequence was received
12053 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12054 *
12055 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12056 * receive sequence is only partially assembed by the driver, it shall abort
12057 * the partially assembled frames for the sequence. Otherwise, if the
12058 * unsolicited receive sequence has been completely assembled and passed to
12059 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12060 * unsolicited sequence has been aborted. After that, it will issue a basic
12061 * accept to accept the abort.
12062 **/
12063void
12064lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12065 struct hbq_dmabuf *dmabuf)
12066{
12067 struct lpfc_hba *phba = vport->phba;
12068 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050012069 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040012070 bool abts_par;
12071
James Smart6669f9b2009-10-02 15:16:45 -040012072 /* Make a copy of fc_hdr before the dmabuf being released */
12073 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050012074 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040012075
James Smart5ffc2662009-11-18 15:39:44 -050012076 if (fctl & FC_FC_EX_CTX) {
12077 /*
12078 * ABTS sent by responder to exchange, just free the buffer
12079 */
James Smart6669f9b2009-10-02 15:16:45 -040012080 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050012081 } else {
12082 /*
12083 * ABTS sent by initiator to exchange, need to do cleanup
12084 */
12085 /* Try to abort partially assembled seq */
12086 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12087
12088 /* Send abort to ULP if partially seq abort failed */
12089 if (abts_par == false)
12090 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12091 else
12092 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12093 }
James Smart6669f9b2009-10-02 15:16:45 -040012094 /* Send basic accept (BA_ACC) to the abort requester */
12095 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
12096}
12097
12098/**
James Smart4f774512009-05-22 14:52:35 -040012099 * lpfc_seq_complete - Indicates if a sequence is complete
12100 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12101 *
12102 * This function checks the sequence, starting with the frame described by
12103 * @dmabuf, to see if all the frames associated with this sequence are present.
12104 * the frames associated with this sequence are linked to the @dmabuf using the
12105 * dbuf list. This function looks for two major things. 1) That the first frame
12106 * has a sequence count of zero. 2) There is a frame with last frame of sequence
12107 * set. 3) That there are no holes in the sequence count. The function will
12108 * return 1 when the sequence is complete, otherwise it will return 0.
12109 **/
12110static int
12111lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12112{
12113 struct fc_frame_header *hdr;
12114 struct lpfc_dmabuf *d_buf;
12115 struct hbq_dmabuf *seq_dmabuf;
12116 uint32_t fctl;
12117 int seq_count = 0;
12118
12119 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12120 /* make sure first fame of sequence has a sequence count of zero */
12121 if (hdr->fh_seq_cnt != seq_count)
12122 return 0;
12123 fctl = (hdr->fh_f_ctl[0] << 16 |
12124 hdr->fh_f_ctl[1] << 8 |
12125 hdr->fh_f_ctl[2]);
12126 /* If last frame of sequence we can return success. */
12127 if (fctl & FC_FC_END_SEQ)
12128 return 1;
12129 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12130 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12131 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12132 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050012133 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040012134 return 0;
12135 fctl = (hdr->fh_f_ctl[0] << 16 |
12136 hdr->fh_f_ctl[1] << 8 |
12137 hdr->fh_f_ctl[2]);
12138 /* If last frame of sequence we can return success. */
12139 if (fctl & FC_FC_END_SEQ)
12140 return 1;
12141 }
12142 return 0;
12143}
12144
12145/**
12146 * lpfc_prep_seq - Prep sequence for ULP processing
12147 * @vport: Pointer to the vport on which this sequence was received
12148 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12149 *
12150 * This function takes a sequence, described by a list of frames, and creates
12151 * a list of iocbq structures to describe the sequence. This iocbq list will be
12152 * used to issue to the generic unsolicited sequence handler. This routine
12153 * returns a pointer to the first iocbq in the list. If the function is unable
12154 * to allocate an iocbq then it throw out the received frames that were not
12155 * able to be described and return a pointer to the first iocbq. If unable to
12156 * allocate any iocbqs (including the first) this function will return NULL.
12157 **/
12158static struct lpfc_iocbq *
12159lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12160{
12161 struct lpfc_dmabuf *d_buf, *n_buf;
12162 struct lpfc_iocbq *first_iocbq, *iocbq;
12163 struct fc_frame_header *fc_hdr;
12164 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050012165 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040012166
12167 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12168 /* remove from receive buffer list */
12169 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040012170 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012171 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040012172 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040012173 /* Get an iocbq struct to fill in. */
12174 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12175 if (first_iocbq) {
12176 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040012177 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040012178 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12179 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12180 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12181 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12182 vport->vpi + vport->phba->vpi_base;
12183 /* put the first buffer into the first IOCBq */
12184 first_iocbq->context2 = &seq_dmabuf->dbuf;
12185 first_iocbq->context3 = NULL;
12186 first_iocbq->iocb.ulpBdeCount = 1;
12187 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12188 LPFC_DATA_BUF_SIZE;
12189 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040012190 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012191 bf_get(lpfc_rcqe_length,
12192 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012193 }
12194 iocbq = first_iocbq;
12195 /*
12196 * Each IOCBq can have two Buffers assigned, so go through the list
12197 * of buffers for this sequence and save two buffers in each IOCBq
12198 */
12199 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12200 if (!iocbq) {
12201 lpfc_in_buf_free(vport->phba, d_buf);
12202 continue;
12203 }
12204 if (!iocbq->context3) {
12205 iocbq->context3 = d_buf;
12206 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050012207 pbde = (struct ulp_bde64 *)
12208 &iocbq->iocb.unsli3.sli3Words[4];
12209 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012210 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012211 bf_get(lpfc_rcqe_length,
12212 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012213 } else {
12214 iocbq = lpfc_sli_get_iocbq(vport->phba);
12215 if (!iocbq) {
12216 if (first_iocbq) {
12217 first_iocbq->iocb.ulpStatus =
12218 IOSTAT_FCP_RSP_ERROR;
12219 first_iocbq->iocb.un.ulpWord[4] =
12220 IOERR_NO_RESOURCES;
12221 }
12222 lpfc_in_buf_free(vport->phba, d_buf);
12223 continue;
12224 }
12225 iocbq->context2 = d_buf;
12226 iocbq->context3 = NULL;
12227 iocbq->iocb.ulpBdeCount = 1;
12228 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12229 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012230 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012231 bf_get(lpfc_rcqe_length,
12232 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012233 iocbq->iocb.un.rcvels.remoteID = sid;
12234 list_add_tail(&iocbq->list, &first_iocbq->list);
12235 }
12236 }
12237 return first_iocbq;
12238}
12239
James Smart6669f9b2009-10-02 15:16:45 -040012240static void
12241lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12242 struct hbq_dmabuf *seq_dmabuf)
12243{
12244 struct fc_frame_header *fc_hdr;
12245 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12246 struct lpfc_hba *phba = vport->phba;
12247
12248 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12249 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12250 if (!iocbq) {
12251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12252 "2707 Ring %d handler: Failed to allocate "
12253 "iocb Rctl x%x Type x%x received\n",
12254 LPFC_ELS_RING,
12255 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12256 return;
12257 }
12258 if (!lpfc_complete_unsol_iocb(phba,
12259 &phba->sli.ring[LPFC_ELS_RING],
12260 iocbq, fc_hdr->fh_r_ctl,
12261 fc_hdr->fh_type))
12262 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12263 "2540 Ring %d handler: unexpected Rctl "
12264 "x%x Type x%x received\n",
12265 LPFC_ELS_RING,
12266 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12267
12268 /* Free iocb created in lpfc_prep_seq */
12269 list_for_each_entry_safe(curr_iocb, next_iocb,
12270 &iocbq->list, list) {
12271 list_del_init(&curr_iocb->list);
12272 lpfc_sli_release_iocbq(phba, curr_iocb);
12273 }
12274 lpfc_sli_release_iocbq(phba, iocbq);
12275}
12276
James Smart4f774512009-05-22 14:52:35 -040012277/**
12278 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12279 * @phba: Pointer to HBA context object.
12280 *
12281 * This function is called with no lock held. This function processes all
12282 * the received buffers and gives it to upper layers when a received buffer
12283 * indicates that it is the final frame in the sequence. The interrupt
12284 * service routine processes received buffers at interrupt contexts and adds
12285 * received dma buffers to the rb_pend_list queue and signals the worker thread.
12286 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12287 * appropriate receive function when the final frame in a sequence is received.
12288 **/
James Smart4d9ab992009-10-02 15:16:39 -040012289void
12290lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12291 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040012292{
James Smart4d9ab992009-10-02 15:16:39 -040012293 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040012294 struct fc_frame_header *fc_hdr;
12295 struct lpfc_vport *vport;
12296 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040012297
James Smart4f774512009-05-22 14:52:35 -040012298 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040012299 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12300 /* check to see if this a valid type of frame */
12301 if (lpfc_fc_frame_check(phba, fc_hdr)) {
12302 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12303 return;
12304 }
12305 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12306 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050012307 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040012308 /* throw out the frame */
12309 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12310 return;
12311 }
James Smart6669f9b2009-10-02 15:16:45 -040012312 /* Handle the basic abort sequence (BA_ABTS) event */
12313 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12314 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12315 return;
12316 }
12317
James Smart4d9ab992009-10-02 15:16:39 -040012318 /* Link this frame */
12319 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12320 if (!seq_dmabuf) {
12321 /* unable to add frame to vport - throw it out */
12322 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12323 return;
12324 }
12325 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050012326 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040012327 return;
James Smartdef9c7a2009-12-21 17:02:28 -050012328
James Smart6669f9b2009-10-02 15:16:45 -040012329 /* Send the complete sequence to the upper layer protocol */
12330 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040012331}
James Smart6fb120a2009-05-22 14:52:59 -040012332
12333/**
12334 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12335 * @phba: pointer to lpfc hba data structure.
12336 *
12337 * This routine is invoked to post rpi header templates to the
12338 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012339 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12340 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012341 *
12342 * This routine does not require any locks. It's usage is expected
12343 * to be driver load or reset recovery when the driver is
12344 * sequential.
12345 *
12346 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012347 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012348 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012349 * When this error occurs, the driver is not guaranteed
12350 * to have any rpi regions posted to the device and
12351 * must either attempt to repost the regions or take a
12352 * fatal error.
12353 **/
12354int
12355lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12356{
12357 struct lpfc_rpi_hdr *rpi_page;
12358 uint32_t rc = 0;
12359
12360 /* Post all rpi memory regions to the port. */
12361 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12362 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12363 if (rc != MBX_SUCCESS) {
12364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12365 "2008 Error %d posting all rpi "
12366 "headers\n", rc);
12367 rc = -EIO;
12368 break;
12369 }
12370 }
12371
12372 return rc;
12373}
12374
12375/**
12376 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12377 * @phba: pointer to lpfc hba data structure.
12378 * @rpi_page: pointer to the rpi memory region.
12379 *
12380 * This routine is invoked to post a single rpi header to the
12381 * HBA consistent with the SLI-4 interface spec. This memory region
12382 * maps up to 64 rpi context regions.
12383 *
12384 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012385 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012386 * -ENOMEM - No available memory
12387 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012388 **/
12389int
12390lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12391{
12392 LPFC_MBOXQ_t *mboxq;
12393 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12394 uint32_t rc = 0;
12395 uint32_t mbox_tmo;
12396 uint32_t shdr_status, shdr_add_status;
12397 union lpfc_sli4_cfg_shdr *shdr;
12398
12399 /* The port is notified of the header region via a mailbox command. */
12400 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12401 if (!mboxq) {
12402 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12403 "2001 Unable to allocate memory for issuing "
12404 "SLI_CONFIG_SPECIAL mailbox command\n");
12405 return -ENOMEM;
12406 }
12407
12408 /* Post all rpi memory regions to the port. */
12409 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12410 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12411 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12412 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12413 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
12414 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
12415 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12416 hdr_tmpl, rpi_page->page_count);
12417 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12418 rpi_page->start_rpi);
12419 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12420 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040012421 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040012422 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12423 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12424 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12425 if (rc != MBX_TIMEOUT)
12426 mempool_free(mboxq, phba->mbox_mem_pool);
12427 if (shdr_status || shdr_add_status || rc) {
12428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12429 "2514 POST_RPI_HDR mailbox failed with "
12430 "status x%x add_status x%x, mbx status x%x\n",
12431 shdr_status, shdr_add_status, rc);
12432 rc = -ENXIO;
12433 }
12434 return rc;
12435}
12436
12437/**
12438 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12439 * @phba: pointer to lpfc hba data structure.
12440 *
12441 * This routine is invoked to post rpi header templates to the
12442 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012443 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12444 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012445 *
12446 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012447 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040012448 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12449 **/
12450int
12451lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12452{
12453 int rpi;
12454 uint16_t max_rpi, rpi_base, rpi_limit;
12455 uint16_t rpi_remaining;
12456 struct lpfc_rpi_hdr *rpi_hdr;
12457
12458 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12459 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12460 rpi_limit = phba->sli4_hba.next_rpi;
12461
12462 /*
12463 * The valid rpi range is not guaranteed to be zero-based. Start
12464 * the search at the rpi_base as reported by the port.
12465 */
12466 spin_lock_irq(&phba->hbalock);
12467 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12468 if (rpi >= rpi_limit || rpi < rpi_base)
12469 rpi = LPFC_RPI_ALLOC_ERROR;
12470 else {
12471 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12472 phba->sli4_hba.max_cfg_param.rpi_used++;
12473 phba->sli4_hba.rpi_count++;
12474 }
12475
12476 /*
12477 * Don't try to allocate more rpi header regions if the device limit
12478 * on available rpis max has been exhausted.
12479 */
12480 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12481 (phba->sli4_hba.rpi_count >= max_rpi)) {
12482 spin_unlock_irq(&phba->hbalock);
12483 return rpi;
12484 }
12485
12486 /*
12487 * If the driver is running low on rpi resources, allocate another
12488 * page now. Note that the next_rpi value is used because
12489 * it represents how many are actually in use whereas max_rpi notes
12490 * how many are supported max by the device.
12491 */
12492 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12493 phba->sli4_hba.rpi_count;
12494 spin_unlock_irq(&phba->hbalock);
12495 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12496 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12497 if (!rpi_hdr) {
12498 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12499 "2002 Error Could not grow rpi "
12500 "count\n");
12501 } else {
12502 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12503 }
12504 }
12505
12506 return rpi;
12507}
12508
12509/**
12510 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12511 * @phba: pointer to lpfc hba data structure.
12512 *
12513 * This routine is invoked to release an rpi to the pool of
12514 * available rpis maintained by the driver.
12515 **/
12516void
James Smartd7c47992010-06-08 18:31:54 -040012517__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12518{
12519 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12520 phba->sli4_hba.rpi_count--;
12521 phba->sli4_hba.max_cfg_param.rpi_used--;
12522 }
12523}
12524
12525/**
12526 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12527 * @phba: pointer to lpfc hba data structure.
12528 *
12529 * This routine is invoked to release an rpi to the pool of
12530 * available rpis maintained by the driver.
12531 **/
12532void
James Smart6fb120a2009-05-22 14:52:59 -040012533lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12534{
12535 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012536 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012537 spin_unlock_irq(&phba->hbalock);
12538}
12539
12540/**
12541 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12542 * @phba: pointer to lpfc hba data structure.
12543 *
12544 * This routine is invoked to remove the memory region that
12545 * provided rpi via a bitmask.
12546 **/
12547void
12548lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12549{
12550 kfree(phba->sli4_hba.rpi_bmask);
12551}
12552
12553/**
12554 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12555 * @phba: pointer to lpfc hba data structure.
12556 *
12557 * This routine is invoked to remove the memory region that
12558 * provided rpi via a bitmask.
12559 **/
12560int
12561lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12562{
12563 LPFC_MBOXQ_t *mboxq;
12564 struct lpfc_hba *phba = ndlp->phba;
12565 int rc;
12566
12567 /* The port is notified of the header region via a mailbox command. */
12568 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12569 if (!mboxq)
12570 return -ENOMEM;
12571
12572 /* Post all rpi memory regions to the port. */
12573 lpfc_resume_rpi(mboxq, ndlp);
12574 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12575 if (rc == MBX_NOT_FINISHED) {
12576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12577 "2010 Resume RPI Mailbox failed "
12578 "status %d, mbxStatus x%x\n", rc,
12579 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12580 mempool_free(mboxq, phba->mbox_mem_pool);
12581 return -EIO;
12582 }
12583 return 0;
12584}
12585
12586/**
12587 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050012588 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040012589 *
James Smart76a95d72010-11-20 23:11:48 -050012590 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040012591 *
12592 * Returns:
12593 * 0 success
12594 * -Evalue otherwise
12595 **/
12596int
James Smart76a95d72010-11-20 23:11:48 -050012597lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040012598{
12599 LPFC_MBOXQ_t *mboxq;
12600 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012601 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012602 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050012603 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040012604 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12605 if (!mboxq)
12606 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050012607 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012608 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12609 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012610 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050012611 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040012612 "2022 INIT VPI Mailbox failed "
12613 "status %d, mbxStatus x%x\n", rc,
12614 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012615 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012616 }
James Smart6a9c52c2009-10-02 15:16:51 -040012617 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050012618 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040012619
12620 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012621}
12622
12623/**
12624 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12625 * @phba: pointer to lpfc hba data structure.
12626 * @mboxq: Pointer to mailbox object.
12627 *
12628 * This routine is invoked to manually add a single FCF record. The caller
12629 * must pass a completely initialized FCF_Record. This routine takes
12630 * care of the nonembedded mailbox operations.
12631 **/
12632static void
12633lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12634{
12635 void *virt_addr;
12636 union lpfc_sli4_cfg_shdr *shdr;
12637 uint32_t shdr_status, shdr_add_status;
12638
12639 virt_addr = mboxq->sge_array->addr[0];
12640 /* The IOCTL status is embedded in the mailbox subheader. */
12641 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12642 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12643 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12644
12645 if ((shdr_status || shdr_add_status) &&
12646 (shdr_status != STATUS_FCF_IN_USE))
12647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12648 "2558 ADD_FCF_RECORD mailbox failed with "
12649 "status x%x add_status x%x\n",
12650 shdr_status, shdr_add_status);
12651
12652 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12653}
12654
12655/**
12656 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12657 * @phba: pointer to lpfc hba data structure.
12658 * @fcf_record: pointer to the initialized fcf record to add.
12659 *
12660 * This routine is invoked to manually add a single FCF record. The caller
12661 * must pass a completely initialized FCF_Record. This routine takes
12662 * care of the nonembedded mailbox operations.
12663 **/
12664int
12665lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12666{
12667 int rc = 0;
12668 LPFC_MBOXQ_t *mboxq;
12669 uint8_t *bytep;
12670 void *virt_addr;
12671 dma_addr_t phys_addr;
12672 struct lpfc_mbx_sge sge;
12673 uint32_t alloc_len, req_len;
12674 uint32_t fcfindex;
12675
12676 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12677 if (!mboxq) {
12678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12679 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12680 return -ENOMEM;
12681 }
12682
12683 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12684 sizeof(uint32_t);
12685
12686 /* Allocate DMA memory and set up the non-embedded mailbox command */
12687 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12688 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12689 req_len, LPFC_SLI4_MBX_NEMBED);
12690 if (alloc_len < req_len) {
12691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12692 "2523 Allocated DMA memory size (x%x) is "
12693 "less than the requested DMA memory "
12694 "size (x%x)\n", alloc_len, req_len);
12695 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12696 return -ENOMEM;
12697 }
12698
12699 /*
12700 * Get the first SGE entry from the non-embedded DMA memory. This
12701 * routine only uses a single SGE.
12702 */
12703 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12704 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012705 virt_addr = mboxq->sge_array->addr[0];
12706 /*
12707 * Configure the FCF record for FCFI 0. This is the driver's
12708 * hardcoded default and gets used in nonFIP mode.
12709 */
12710 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12711 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12712 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12713
12714 /*
12715 * Copy the fcf_index and the FCF Record Data. The data starts after
12716 * the FCoE header plus word10. The data copy needs to be endian
12717 * correct.
12718 */
12719 bytep += sizeof(uint32_t);
12720 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12721 mboxq->vport = phba->pport;
12722 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12723 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12724 if (rc == MBX_NOT_FINISHED) {
12725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12726 "2515 ADD_FCF_RECORD mailbox failed with "
12727 "status 0x%x\n", rc);
12728 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12729 rc = -EIO;
12730 } else
12731 rc = 0;
12732
12733 return rc;
12734}
12735
12736/**
12737 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12738 * @phba: pointer to lpfc hba data structure.
12739 * @fcf_record: pointer to the fcf record to write the default data.
12740 * @fcf_index: FCF table entry index.
12741 *
12742 * This routine is invoked to build the driver's default FCF record. The
12743 * values used are hardcoded. This routine handles memory initialization.
12744 *
12745 **/
12746void
12747lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12748 struct fcf_record *fcf_record,
12749 uint16_t fcf_index)
12750{
12751 memset(fcf_record, 0, sizeof(struct fcf_record));
12752 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12753 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12754 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12755 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12756 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12757 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12758 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12759 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12760 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12761 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12762 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12763 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12764 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012765 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012766 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12767 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12768 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12769 /* Set the VLAN bit map */
12770 if (phba->valid_vlan) {
12771 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12772 = 1 << (phba->vlan_id % 8);
12773 }
12774}
12775
12776/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012777 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012778 * @phba: pointer to lpfc hba data structure.
12779 * @fcf_index: FCF table entry offset.
12780 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012781 * This routine is invoked to scan the entire FCF table by reading FCF
12782 * record and processing it one at a time starting from the @fcf_index
12783 * for initial FCF discovery or fast FCF failover rediscovery.
12784 *
12785 * Return 0 if the mailbox command is submitted sucessfully, none 0
12786 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012787 **/
12788int
James Smart0c9ab6f2010-02-26 14:15:57 -050012789lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012790{
12791 int rc = 0, error;
12792 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012793
James Smart32b97932009-07-19 10:01:21 -040012794 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012795 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12796 if (!mboxq) {
12797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12798 "2000 Failed to allocate mbox for "
12799 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012800 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012801 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012802 }
James Smartecfd03c2010-02-12 14:41:27 -050012803 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012804 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012805 if (rc) {
12806 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012807 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012808 }
James Smartecfd03c2010-02-12 14:41:27 -050012809 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012810 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012811 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040012812
12813 spin_lock_irq(&phba->hbalock);
12814 phba->hba_flag |= FCF_TS_INPROG;
12815 spin_unlock_irq(&phba->hbalock);
12816
James Smart6fb120a2009-05-22 14:52:59 -040012817 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012818 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012819 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012820 else {
James Smart38b92ef2010-08-04 16:11:39 -040012821 /* Reset eligible FCF count for new scan */
12822 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040012823 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040012824 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012825 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012826fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012827 if (error) {
12828 if (mboxq)
12829 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040012830 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040012831 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040012832 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040012833 spin_unlock_irq(&phba->hbalock);
12834 }
James Smart6fb120a2009-05-22 14:52:59 -040012835 return error;
12836}
James Smarta0c87cb2009-07-19 10:01:10 -040012837
12838/**
James Smarta93ff372010-10-22 11:06:08 -040012839 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050012840 * @phba: pointer to lpfc hba data structure.
12841 * @fcf_index: FCF table entry offset.
12842 *
12843 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040012844 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050012845 *
12846 * Return 0 if the mailbox command is submitted sucessfully, none 0
12847 * otherwise.
12848 **/
12849int
12850lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12851{
12852 int rc = 0, error;
12853 LPFC_MBOXQ_t *mboxq;
12854
12855 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12856 if (!mboxq) {
12857 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12858 "2763 Failed to allocate mbox for "
12859 "READ_FCF cmd\n");
12860 error = -ENOMEM;
12861 goto fail_fcf_read;
12862 }
12863 /* Construct the read FCF record mailbox command */
12864 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12865 if (rc) {
12866 error = -EINVAL;
12867 goto fail_fcf_read;
12868 }
12869 /* Issue the mailbox command asynchronously */
12870 mboxq->vport = phba->pport;
12871 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12872 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12873 if (rc == MBX_NOT_FINISHED)
12874 error = -EIO;
12875 else
12876 error = 0;
12877
12878fail_fcf_read:
12879 if (error && mboxq)
12880 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12881 return error;
12882}
12883
12884/**
12885 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12886 * @phba: pointer to lpfc hba data structure.
12887 * @fcf_index: FCF table entry offset.
12888 *
12889 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040012890 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050012891 *
12892 * Return 0 if the mailbox command is submitted sucessfully, none 0
12893 * otherwise.
12894 **/
12895int
12896lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12897{
12898 int rc = 0, error;
12899 LPFC_MBOXQ_t *mboxq;
12900
12901 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12902 if (!mboxq) {
12903 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12904 "2758 Failed to allocate mbox for "
12905 "READ_FCF cmd\n");
12906 error = -ENOMEM;
12907 goto fail_fcf_read;
12908 }
12909 /* Construct the read FCF record mailbox command */
12910 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12911 if (rc) {
12912 error = -EINVAL;
12913 goto fail_fcf_read;
12914 }
12915 /* Issue the mailbox command asynchronously */
12916 mboxq->vport = phba->pport;
12917 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12918 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12919 if (rc == MBX_NOT_FINISHED)
12920 error = -EIO;
12921 else
12922 error = 0;
12923
12924fail_fcf_read:
12925 if (error && mboxq)
12926 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12927 return error;
12928}
12929
12930/**
12931 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12932 * @phba: pointer to lpfc hba data structure.
12933 *
12934 * This routine is to get the next eligible FCF record index in a round
12935 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040012936 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050012937 * shall be returned, otherwise, the next eligible FCF record's index
12938 * shall be returned.
12939 **/
12940uint16_t
12941lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12942{
12943 uint16_t next_fcf_index;
12944
James Smart3804dc82010-07-14 15:31:37 -040012945 /* Search start from next bit of currently registered FCF index */
12946 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12947 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050012948 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12949 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040012950 next_fcf_index);
12951
James Smart0c9ab6f2010-02-26 14:15:57 -050012952 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12953 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12954 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12955 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050012956
James Smart3804dc82010-07-14 15:31:37 -040012957 /* Check roundrobin failover list empty condition */
12958 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12959 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12960 "2844 No roundrobin failover FCF available\n");
12961 return LPFC_FCOE_FCF_NEXT_NONE;
12962 }
12963
James Smart3804dc82010-07-14 15:31:37 -040012964 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012965 "2845 Get next roundrobin failover FCF (x%x)\n",
12966 next_fcf_index);
12967
James Smart0c9ab6f2010-02-26 14:15:57 -050012968 return next_fcf_index;
12969}
12970
12971/**
12972 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12973 * @phba: pointer to lpfc hba data structure.
12974 *
12975 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012976 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012977 * does not go beyond the range of the driver allocated bmask dimension
12978 * before setting the bit.
12979 *
12980 * Returns 0 if the index bit successfully set, otherwise, it returns
12981 * -EINVAL.
12982 **/
12983int
12984lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12985{
12986 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12987 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012988 "2610 FCF (x%x) reached driver's book "
12989 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012990 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12991 return -EINVAL;
12992 }
12993 /* Set the eligible FCF record index bmask */
12994 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12995
James Smart3804dc82010-07-14 15:31:37 -040012996 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012997 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040012998 "bmask\n", fcf_index);
12999
James Smart0c9ab6f2010-02-26 14:15:57 -050013000 return 0;
13001}
13002
13003/**
James Smart3804dc82010-07-14 15:31:37 -040013004 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050013005 * @phba: pointer to lpfc hba data structure.
13006 *
13007 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040013008 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050013009 * does not go beyond the range of the driver allocated bmask dimension
13010 * before clearing the bit.
13011 **/
13012void
13013lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13014{
13015 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13016 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013017 "2762 FCF (x%x) reached driver's book "
13018 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050013019 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13020 return;
13021 }
13022 /* Clear the eligible FCF record index bmask */
13023 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040013024
13025 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013026 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040013027 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050013028}
13029
13030/**
James Smartecfd03c2010-02-12 14:41:27 -050013031 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13032 * @phba: pointer to lpfc hba data structure.
13033 *
13034 * This routine is the completion routine for the rediscover FCF table mailbox
13035 * command. If the mailbox command returned failure, it will try to stop the
13036 * FCF rediscover wait timer.
13037 **/
13038void
13039lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13040{
13041 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13042 uint32_t shdr_status, shdr_add_status;
13043
13044 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13045
13046 shdr_status = bf_get(lpfc_mbox_hdr_status,
13047 &redisc_fcf->header.cfg_shdr.response);
13048 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13049 &redisc_fcf->header.cfg_shdr.response);
13050 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050013051 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050013052 "2746 Requesting for FCF rediscovery failed "
13053 "status x%x add_status x%x\n",
13054 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050013055 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050013056 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013057 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013058 spin_unlock_irq(&phba->hbalock);
13059 /*
13060 * CVL event triggered FCF rediscover request failed,
13061 * last resort to re-try current registered FCF entry.
13062 */
13063 lpfc_retry_pport_discovery(phba);
13064 } else {
13065 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013066 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013067 spin_unlock_irq(&phba->hbalock);
13068 /*
13069 * DEAD FCF event triggered FCF rediscover request
13070 * failed, last resort to fail over as a link down
13071 * to FCF registration.
13072 */
13073 lpfc_sli4_fcf_dead_failthrough(phba);
13074 }
James Smart0c9ab6f2010-02-26 14:15:57 -050013075 } else {
13076 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013077 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050013078 /*
13079 * Start FCF rediscovery wait timer for pending FCF
13080 * before rescan FCF record table.
13081 */
13082 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050013083 }
James Smartecfd03c2010-02-12 14:41:27 -050013084
13085 mempool_free(mbox, phba->mbox_mem_pool);
13086}
13087
13088/**
James Smart3804dc82010-07-14 15:31:37 -040013089 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050013090 * @phba: pointer to lpfc hba data structure.
13091 *
13092 * This routine is invoked to request for rediscovery of the entire FCF table
13093 * by the port.
13094 **/
13095int
13096lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13097{
13098 LPFC_MBOXQ_t *mbox;
13099 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13100 int rc, length;
13101
James Smart0c9ab6f2010-02-26 14:15:57 -050013102 /* Cancel retry delay timers to all vports before FCF rediscover */
13103 lpfc_cancel_all_vport_retry_delay_timer(phba);
13104
James Smartecfd03c2010-02-12 14:41:27 -050013105 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13106 if (!mbox) {
13107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13108 "2745 Failed to allocate mbox for "
13109 "requesting FCF rediscover.\n");
13110 return -ENOMEM;
13111 }
13112
13113 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13114 sizeof(struct lpfc_sli4_cfg_mhdr));
13115 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13116 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13117 length, LPFC_SLI4_MBX_EMBED);
13118
13119 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13120 /* Set count to 0 for invalidating the entire FCF database */
13121 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13122
13123 /* Issue the mailbox command asynchronously */
13124 mbox->vport = phba->pport;
13125 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13126 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13127
13128 if (rc == MBX_NOT_FINISHED) {
13129 mempool_free(mbox, phba->mbox_mem_pool);
13130 return -EIO;
13131 }
13132 return 0;
13133}
13134
13135/**
James Smartfc2b9892010-02-26 14:15:29 -050013136 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13137 * @phba: pointer to lpfc hba data structure.
13138 *
13139 * This function is the failover routine as a last resort to the FCF DEAD
13140 * event when driver failed to perform fast FCF failover.
13141 **/
13142void
13143lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13144{
13145 uint32_t link_state;
13146
13147 /*
13148 * Last resort as FCF DEAD event failover will treat this as
13149 * a link down, but save the link state because we don't want
13150 * it to be changed to Link Down unless it is already down.
13151 */
13152 link_state = phba->link_state;
13153 lpfc_linkdown(phba);
13154 phba->link_state = link_state;
13155
13156 /* Unregister FCF if no devices connected to it */
13157 lpfc_unregister_unused_fcf(phba);
13158}
13159
13160/**
James Smarta0c87cb2009-07-19 10:01:10 -040013161 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13162 * @phba: pointer to lpfc hba data structure.
13163 *
13164 * This function read region 23 and parse TLV for port status to
13165 * decide if the user disaled the port. If the TLV indicates the
13166 * port is disabled, the hba_flag is set accordingly.
13167 **/
13168void
13169lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13170{
13171 LPFC_MBOXQ_t *pmb = NULL;
13172 MAILBOX_t *mb;
13173 uint8_t *rgn23_data = NULL;
13174 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13175 int rc;
13176
13177 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13178 if (!pmb) {
13179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13180 "2600 lpfc_sli_read_serdes_param failed to"
13181 " allocate mailbox memory\n");
13182 goto out;
13183 }
13184 mb = &pmb->u.mb;
13185
13186 /* Get adapter Region 23 data */
13187 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13188 if (!rgn23_data)
13189 goto out;
13190
13191 do {
13192 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13193 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13194
13195 if (rc != MBX_SUCCESS) {
13196 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13197 "2601 lpfc_sli_read_link_ste failed to"
13198 " read config region 23 rc 0x%x Status 0x%x\n",
13199 rc, mb->mbxStatus);
13200 mb->un.varDmp.word_cnt = 0;
13201 }
13202 /*
13203 * dump mem may return a zero when finished or we got a
13204 * mailbox error, either way we are done.
13205 */
13206 if (mb->un.varDmp.word_cnt == 0)
13207 break;
13208 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13209 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13210
13211 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13212 rgn23_data + offset,
13213 mb->un.varDmp.word_cnt);
13214 offset += mb->un.varDmp.word_cnt;
13215 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13216
13217 data_size = offset;
13218 offset = 0;
13219
13220 if (!data_size)
13221 goto out;
13222
13223 /* Check the region signature first */
13224 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13226 "2619 Config region 23 has bad signature\n");
13227 goto out;
13228 }
13229 offset += 4;
13230
13231 /* Check the data structure version */
13232 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13234 "2620 Config region 23 has bad version\n");
13235 goto out;
13236 }
13237 offset += 4;
13238
13239 /* Parse TLV entries in the region */
13240 while (offset < data_size) {
13241 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13242 break;
13243 /*
13244 * If the TLV is not driver specific TLV or driver id is
13245 * not linux driver id, skip the record.
13246 */
13247 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13248 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13249 (rgn23_data[offset + 3] != 0)) {
13250 offset += rgn23_data[offset + 1] * 4 + 4;
13251 continue;
13252 }
13253
13254 /* Driver found a driver specific TLV in the config region */
13255 sub_tlv_len = rgn23_data[offset + 1] * 4;
13256 offset += 4;
13257 tlv_offset = 0;
13258
13259 /*
13260 * Search for configured port state sub-TLV.
13261 */
13262 while ((offset < data_size) &&
13263 (tlv_offset < sub_tlv_len)) {
13264 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13265 offset += 4;
13266 tlv_offset += 4;
13267 break;
13268 }
13269 if (rgn23_data[offset] != PORT_STE_TYPE) {
13270 offset += rgn23_data[offset + 1] * 4 + 4;
13271 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13272 continue;
13273 }
13274
13275 /* This HBA contains PORT_STE configured */
13276 if (!rgn23_data[offset + 2])
13277 phba->hba_flag |= LINK_DISABLED;
13278
13279 goto out;
13280 }
13281 }
13282out:
13283 if (pmb)
13284 mempool_free(pmb, phba->mbox_mem_pool);
13285 kfree(rgn23_data);
13286 return;
13287}
James Smart695a8142010-01-26 23:08:03 -050013288
13289/**
13290 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13291 * @vport: pointer to vport data structure.
13292 *
13293 * This function iterate through the mailboxq and clean up all REG_LOGIN
13294 * and REG_VPI mailbox commands associated with the vport. This function
13295 * is called when driver want to restart discovery of the vport due to
13296 * a Clear Virtual Link event.
13297 **/
13298void
13299lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13300{
13301 struct lpfc_hba *phba = vport->phba;
13302 LPFC_MBOXQ_t *mb, *nextmb;
13303 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040013304 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040013305 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040013306 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040013307 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050013308 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050013309
James Smartd439d282010-09-29 11:18:45 -040013310 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050013311 spin_lock_irq(&phba->hbalock);
13312 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13313 if (mb->vport != vport)
13314 continue;
13315
13316 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13317 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13318 continue;
13319
James Smartd439d282010-09-29 11:18:45 -040013320 list_del(&mb->list);
13321 list_add_tail(&mb->list, &mbox_cmd_list);
13322 }
13323 /* Clean up active mailbox command with the vport */
13324 mb = phba->sli.mbox_active;
13325 if (mb && (mb->vport == vport)) {
13326 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13327 (mb->u.mb.mbxCommand == MBX_REG_VPI))
13328 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13329 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13330 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13331 /* Put reference count for delayed processing */
13332 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13333 /* Unregister the RPI when mailbox complete */
13334 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13335 }
13336 }
James Smart63e801c2010-11-20 23:14:19 -050013337 /* Cleanup any mailbox completions which are not yet processed */
13338 do {
13339 restart_loop = 0;
13340 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13341 /*
13342 * If this mailox is already processed or it is
13343 * for another vport ignore it.
13344 */
13345 if ((mb->vport != vport) ||
13346 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13347 continue;
13348
13349 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13350 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13351 continue;
13352
13353 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13354 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13355 ndlp = (struct lpfc_nodelist *)mb->context2;
13356 /* Unregister the RPI when mailbox complete */
13357 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13358 restart_loop = 1;
13359 spin_unlock_irq(&phba->hbalock);
13360 spin_lock(shost->host_lock);
13361 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13362 spin_unlock(shost->host_lock);
13363 spin_lock_irq(&phba->hbalock);
13364 break;
13365 }
13366 }
13367 } while (restart_loop);
13368
James Smartd439d282010-09-29 11:18:45 -040013369 spin_unlock_irq(&phba->hbalock);
13370
13371 /* Release the cleaned-up mailbox commands */
13372 while (!list_empty(&mbox_cmd_list)) {
13373 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050013374 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smartd7c47992010-06-08 18:31:54 -040013375 if (phba->sli_rev == LPFC_SLI_REV4)
13376 __lpfc_sli4_free_rpi(phba,
13377 mb->u.mb.un.varRegLogin.rpi);
James Smart695a8142010-01-26 23:08:03 -050013378 mp = (struct lpfc_dmabuf *) (mb->context1);
13379 if (mp) {
13380 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13381 kfree(mp);
13382 }
James Smart78730cf2010-04-06 15:06:30 -040013383 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040013384 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040013385 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013386 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040013387 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013388 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040013389 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040013390 }
James Smart695a8142010-01-26 23:08:03 -050013391 }
James Smart695a8142010-01-26 23:08:03 -050013392 mempool_free(mb, phba->mbox_mem_pool);
13393 }
James Smartd439d282010-09-29 11:18:45 -040013394
13395 /* Release the ndlp with the cleaned-up active mailbox command */
13396 if (act_mbx_ndlp) {
13397 spin_lock(shost->host_lock);
13398 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13399 spin_unlock(shost->host_lock);
13400 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050013401 }
James Smart695a8142010-01-26 23:08:03 -050013402}
13403
James Smart2a9bf3d2010-06-07 15:24:45 -040013404/**
13405 * lpfc_drain_txq - Drain the txq
13406 * @phba: Pointer to HBA context object.
13407 *
13408 * This function attempt to submit IOCBs on the txq
13409 * to the adapter. For SLI4 adapters, the txq contains
13410 * ELS IOCBs that have been deferred because the there
13411 * are no SGLs. This congestion can occur with large
13412 * vport counts during node discovery.
13413 **/
13414
13415uint32_t
13416lpfc_drain_txq(struct lpfc_hba *phba)
13417{
13418 LIST_HEAD(completions);
13419 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13420 struct lpfc_iocbq *piocbq = 0;
13421 unsigned long iflags = 0;
13422 char *fail_msg = NULL;
13423 struct lpfc_sglq *sglq;
13424 union lpfc_wqe wqe;
13425
13426 spin_lock_irqsave(&phba->hbalock, iflags);
13427 if (pring->txq_cnt > pring->txq_max)
13428 pring->txq_max = pring->txq_cnt;
13429
13430 spin_unlock_irqrestore(&phba->hbalock, iflags);
13431
13432 while (pring->txq_cnt) {
13433 spin_lock_irqsave(&phba->hbalock, iflags);
13434
James Smart19ca7602010-11-20 23:11:55 -050013435 piocbq = lpfc_sli_ringtx_get(phba, pring);
13436 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013437 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050013438 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013439 spin_unlock_irqrestore(&phba->hbalock, iflags);
13440 break;
13441 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040013442 if (!piocbq) {
13443 /* The txq_cnt out of sync. This should
13444 * never happen
13445 */
13446 sglq = __lpfc_clear_active_sglq(phba,
13447 sglq->sli4_xritag);
13448 spin_unlock_irqrestore(&phba->hbalock, iflags);
13449 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13450 "2823 txq empty and txq_cnt is %d\n ",
13451 pring->txq_cnt);
13452 break;
13453 }
13454 }
13455
13456 /* The xri and iocb resources secured,
13457 * attempt to issue request
13458 */
13459 piocbq->sli4_xritag = sglq->sli4_xritag;
13460 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13461 fail_msg = "to convert bpl to sgl";
13462 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13463 fail_msg = "to convert iocb to wqe";
13464 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13465 fail_msg = " - Wq is full";
13466 else
13467 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13468
13469 if (fail_msg) {
13470 /* Failed means we can't issue and need to cancel */
13471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13472 "2822 IOCB failed %s iotag 0x%x "
13473 "xri 0x%x\n",
13474 fail_msg,
13475 piocbq->iotag, piocbq->sli4_xritag);
13476 list_add_tail(&piocbq->list, &completions);
13477 }
13478 spin_unlock_irqrestore(&phba->hbalock, iflags);
13479 }
13480
James Smart2a9bf3d2010-06-07 15:24:45 -040013481 /* Cancel all the IOCBs that cannot be issued */
13482 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13483 IOERR_SLI_ABORTED);
13484
13485 return pring->txq_cnt;
13486}