blob: ae3cb0ab0ae4eb5003ac916aa5c68d546c4b4dff [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040068static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
James Smarte59058c2008-08-24 21:49:00 -0400408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
James Smarted957682007-06-17 19:56:37 -0500418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
James Smarte59058c2008-08-24 21:49:00 -0400425/**
James Smart3621a712009-04-06 18:47:14 -0400426 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
James Smarted957682007-06-17 19:56:37 -0500435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
James Smarte59058c2008-08-24 21:49:00 -0400442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
James Smart2e0fef82007-06-17 19:56:36 -0500451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400458
459 if (iocbq)
460 phba->iocb_cnt++;
461 if (phba->iocb_cnt > phba->iocb_max)
462 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400463 return iocbq;
464}
465
James Smarte59058c2008-08-24 21:49:00 -0400466/**
James Smartda0436e2009-05-22 14:51:39 -0400467 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468 * @phba: Pointer to HBA context object.
469 * @xritag: XRI value.
470 *
471 * This function clears the sglq pointer from the array of acive
472 * sglq's. The xritag that is passed in is used to index into the
473 * array. Before the xritag can be used it needs to be adjusted
474 * by subtracting the xribase.
475 *
476 * Returns sglq ponter = success, NULL = Failure.
477 **/
478static struct lpfc_sglq *
479__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480{
481 uint16_t adj_xri;
482 struct lpfc_sglq *sglq;
483 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 return NULL;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
James Smart0f65ff62010-02-26 14:14:23 -0500503struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
506 uint16_t adj_xri;
507 struct lpfc_sglq *sglq;
508 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 return NULL;
511 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 return sglq;
513}
514
515/**
516 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
517 * @phba: Pointer to HBA context object.
518 *
519 * This function is called with hbalock held. This function
520 * Gets a new driver sglq object from the sglq list. If the
521 * list is not empty then it is successful, it returns pointer to the newly
522 * allocated sglq object else it returns NULL.
523 **/
524static struct lpfc_sglq *
525__lpfc_sli_get_sglq(struct lpfc_hba *phba)
526{
527 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528 struct lpfc_sglq *sglq = NULL;
529 uint16_t adj_xri;
530 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400531 if (!sglq)
532 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400533 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
James Smart0f65ff62010-02-26 14:14:23 -0500535 sglq->state = SGL_ALLOCATED;
James Smartda0436e2009-05-22 14:51:39 -0400536 return sglq;
537}
538
539/**
James Smart3621a712009-04-06 18:47:14 -0400540 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400541 * @phba: Pointer to HBA context object.
542 *
543 * This function is called with no lock held. This function
544 * allocates a new driver iocb object from the iocb pool. If the
545 * allocation is successful, it returns pointer to the newly
546 * allocated iocb object else it returns NULL.
547 **/
James Smart2e0fef82007-06-17 19:56:36 -0500548struct lpfc_iocbq *
549lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500550{
James Smart2e0fef82007-06-17 19:56:36 -0500551 struct lpfc_iocbq * iocbq = NULL;
552 unsigned long iflags;
553
554 spin_lock_irqsave(&phba->hbalock, iflags);
555 iocbq = __lpfc_sli_get_iocbq(phba);
556 spin_unlock_irqrestore(&phba->hbalock, iflags);
557 return iocbq;
558}
559
James Smarte59058c2008-08-24 21:49:00 -0400560/**
James Smart4f774512009-05-22 14:52:35 -0400561 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562 * @phba: Pointer to HBA context object.
563 * @iocbq: Pointer to driver iocb object.
564 *
565 * This function is called with hbalock held to release driver
566 * iocb object to the iocb pool. The iotag in the iocb object
567 * does not change for each use of the iocb object. This function
568 * clears all other fields of the iocb object when it is freed.
569 * The sqlq structure that holds the xritag and phys and virtual
570 * mappings for the scatter gather list is retrieved from the
571 * active array of sglq. The get of the sglq pointer also clears
572 * the entry in the array. If the status of the IO indiactes that
573 * this IO was aborted then the sglq entry it put on the
574 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575 * IO has good status or fails for any other reason then the sglq
576 * entry is added to the free list (lpfc_sgl_list).
577 **/
578static void
579__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580{
581 struct lpfc_sglq *sglq;
582 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400583 unsigned long iflag = 0;
584 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400585
586 if (iocbq->sli4_xritag == NO_XRI)
587 sglq = NULL;
588 else
589 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500591 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400593 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594 iflag);
595 list_add(&sglq->list,
596 &phba->sli4_hba.lpfc_abts_els_sgl_list);
597 spin_unlock_irqrestore(
598 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500599 } else {
600 sglq->state = SGL_FREED;
James Smart4f774512009-05-22 14:52:35 -0400601 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400602
603 /* Check if TXQ queue needs to be serviced */
604 if (pring->txq_cnt) {
605 spin_lock_irqsave(
606 &phba->pport->work_port_lock, iflag);
607 phba->pport->work_port_events |=
608 WORKER_SERVICE_TXQ;
609 lpfc_worker_wake_up(phba);
610 spin_unlock_irqrestore(
611 &phba->pport->work_port_lock, iflag);
612 }
James Smart0f65ff62010-02-26 14:14:23 -0500613 }
James Smart4f774512009-05-22 14:52:35 -0400614 }
615
616
617 /*
618 * Clean all volatile data fields, preserve iotag and node struct.
619 */
620 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
621 iocbq->sli4_xritag = NO_XRI;
622 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
623}
624
James Smart2a9bf3d2010-06-07 15:24:45 -0400625
James Smart4f774512009-05-22 14:52:35 -0400626/**
James Smart3772a992009-05-22 14:50:54 -0400627 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
628 * @phba: Pointer to HBA context object.
629 * @iocbq: Pointer to driver iocb object.
630 *
631 * This function is called with hbalock held to release driver
632 * iocb object to the iocb pool. The iotag in the iocb object
633 * does not change for each use of the iocb object. This function
634 * clears all other fields of the iocb object when it is freed.
635 **/
636static void
637__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
638{
639 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
640
641 /*
642 * Clean all volatile data fields, preserve iotag and node struct.
643 */
644 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
645 iocbq->sli4_xritag = NO_XRI;
646 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
647}
648
649/**
James Smart3621a712009-04-06 18:47:14 -0400650 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400651 * @phba: Pointer to HBA context object.
652 * @iocbq: Pointer to driver iocb object.
653 *
654 * This function is called with hbalock held to release driver
655 * iocb object to the iocb pool. The iotag in the iocb object
656 * does not change for each use of the iocb object. This function
657 * clears all other fields of the iocb object when it is freed.
658 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100659static void
James Smart2e0fef82007-06-17 19:56:36 -0500660__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
661{
James Smart3772a992009-05-22 14:50:54 -0400662 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400663 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -0500664}
665
James Smarte59058c2008-08-24 21:49:00 -0400666/**
James Smart3621a712009-04-06 18:47:14 -0400667 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400668 * @phba: Pointer to HBA context object.
669 * @iocbq: Pointer to driver iocb object.
670 *
671 * This function is called with no lock held to release the iocb to
672 * iocb pool.
673 **/
James Smart2e0fef82007-06-17 19:56:36 -0500674void
675lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
676{
677 unsigned long iflags;
678
679 /*
680 * Clean all volatile data fields, preserve iotag and node struct.
681 */
682 spin_lock_irqsave(&phba->hbalock, iflags);
683 __lpfc_sli_release_iocbq(phba, iocbq);
684 spin_unlock_irqrestore(&phba->hbalock, iflags);
685}
686
James Smarte59058c2008-08-24 21:49:00 -0400687/**
James Smarta257bf92009-04-06 18:48:10 -0400688 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
689 * @phba: Pointer to HBA context object.
690 * @iocblist: List of IOCBs.
691 * @ulpstatus: ULP status in IOCB command field.
692 * @ulpWord4: ULP word-4 in IOCB command field.
693 *
694 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
695 * on the list by invoking the complete callback function associated with the
696 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
697 * fields.
698 **/
699void
700lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
701 uint32_t ulpstatus, uint32_t ulpWord4)
702{
703 struct lpfc_iocbq *piocb;
704
705 while (!list_empty(iocblist)) {
706 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
707
708 if (!piocb->iocb_cmpl)
709 lpfc_sli_release_iocbq(phba, piocb);
710 else {
711 piocb->iocb.ulpStatus = ulpstatus;
712 piocb->iocb.un.ulpWord[4] = ulpWord4;
713 (piocb->iocb_cmpl) (phba, piocb, piocb);
714 }
715 }
716 return;
717}
718
719/**
James Smart3621a712009-04-06 18:47:14 -0400720 * lpfc_sli_iocb_cmd_type - Get the iocb type
721 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400722 *
723 * This function is called by ring event handler function to get the iocb type.
724 * This function translates the iocb command to an iocb command type used to
725 * decide the final disposition of each completed IOCB.
726 * The function returns
727 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
728 * LPFC_SOL_IOCB if it is a solicited iocb completion
729 * LPFC_ABORT_IOCB if it is an abort iocb
730 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
731 *
732 * The caller is not required to hold any lock.
733 **/
dea31012005-04-17 16:05:31 -0500734static lpfc_iocb_type
735lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
736{
737 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
738
739 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
740 return 0;
741
742 switch (iocb_cmnd) {
743 case CMD_XMIT_SEQUENCE_CR:
744 case CMD_XMIT_SEQUENCE_CX:
745 case CMD_XMIT_BCAST_CN:
746 case CMD_XMIT_BCAST_CX:
747 case CMD_ELS_REQUEST_CR:
748 case CMD_ELS_REQUEST_CX:
749 case CMD_CREATE_XRI_CR:
750 case CMD_CREATE_XRI_CX:
751 case CMD_GET_RPI_CN:
752 case CMD_XMIT_ELS_RSP_CX:
753 case CMD_GET_RPI_CR:
754 case CMD_FCP_IWRITE_CR:
755 case CMD_FCP_IWRITE_CX:
756 case CMD_FCP_IREAD_CR:
757 case CMD_FCP_IREAD_CX:
758 case CMD_FCP_ICMND_CR:
759 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500760 case CMD_FCP_TSEND_CX:
761 case CMD_FCP_TRSP_CX:
762 case CMD_FCP_TRECEIVE_CX:
763 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500764 case CMD_ADAPTER_MSG:
765 case CMD_ADAPTER_DUMP:
766 case CMD_XMIT_SEQUENCE64_CR:
767 case CMD_XMIT_SEQUENCE64_CX:
768 case CMD_XMIT_BCAST64_CN:
769 case CMD_XMIT_BCAST64_CX:
770 case CMD_ELS_REQUEST64_CR:
771 case CMD_ELS_REQUEST64_CX:
772 case CMD_FCP_IWRITE64_CR:
773 case CMD_FCP_IWRITE64_CX:
774 case CMD_FCP_IREAD64_CR:
775 case CMD_FCP_IREAD64_CX:
776 case CMD_FCP_ICMND64_CR:
777 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500778 case CMD_FCP_TSEND64_CX:
779 case CMD_FCP_TRSP64_CX:
780 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500781 case CMD_GEN_REQUEST64_CR:
782 case CMD_GEN_REQUEST64_CX:
783 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400784 case DSSCMD_IWRITE64_CR:
785 case DSSCMD_IWRITE64_CX:
786 case DSSCMD_IREAD64_CR:
787 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500788 type = LPFC_SOL_IOCB;
789 break;
790 case CMD_ABORT_XRI_CN:
791 case CMD_ABORT_XRI_CX:
792 case CMD_CLOSE_XRI_CN:
793 case CMD_CLOSE_XRI_CX:
794 case CMD_XRI_ABORTED_CX:
795 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400796 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500797 type = LPFC_ABORT_IOCB;
798 break;
799 case CMD_RCV_SEQUENCE_CX:
800 case CMD_RCV_ELS_REQ_CX:
801 case CMD_RCV_SEQUENCE64_CX:
802 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400803 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500804 case CMD_IOCB_RCV_SEQ64_CX:
805 case CMD_IOCB_RCV_ELS64_CX:
806 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500807 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500808 type = LPFC_UNSOL_IOCB;
809 break;
James Smart3163f722008-02-08 18:50:25 -0500810 case CMD_IOCB_XMIT_MSEQ64_CR:
811 case CMD_IOCB_XMIT_MSEQ64_CX:
812 case CMD_IOCB_RCV_SEQ_LIST64_CX:
813 case CMD_IOCB_RCV_ELS_LIST64_CX:
814 case CMD_IOCB_CLOSE_EXTENDED_CN:
815 case CMD_IOCB_ABORT_EXTENDED_CN:
816 case CMD_IOCB_RET_HBQE64_CN:
817 case CMD_IOCB_FCP_IBIDIR64_CR:
818 case CMD_IOCB_FCP_IBIDIR64_CX:
819 case CMD_IOCB_FCP_ITASKMGT64_CX:
820 case CMD_IOCB_LOGENTRY_CN:
821 case CMD_IOCB_LOGENTRY_ASYNC_CN:
822 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700823 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500824 type = LPFC_UNKNOWN_IOCB;
825 break;
dea31012005-04-17 16:05:31 -0500826 default:
827 type = LPFC_UNKNOWN_IOCB;
828 break;
829 }
830
831 return type;
832}
833
James Smarte59058c2008-08-24 21:49:00 -0400834/**
James Smart3621a712009-04-06 18:47:14 -0400835 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400836 * @phba: Pointer to HBA context object.
837 *
838 * This function is called from SLI initialization code
839 * to configure every ring of the HBA's SLI interface. The
840 * caller is not required to hold any lock. This function issues
841 * a config_ring mailbox command for each ring.
842 * This function returns zero if successful else returns a negative
843 * error code.
844 **/
dea31012005-04-17 16:05:31 -0500845static int
James Smarted957682007-06-17 19:56:37 -0500846lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500847{
848 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500849 LPFC_MBOXQ_t *pmb;
850 MAILBOX_t *pmbox;
851 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500852
James Smarted957682007-06-17 19:56:37 -0500853 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
854 if (!pmb)
855 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400856 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500857 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500858 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500859 lpfc_config_ring(phba, i, pmb);
860 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
861 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400863 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500864 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
865 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400866 rc, pmbox->mbxCommand,
867 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500868 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500869 ret = -ENXIO;
870 break;
dea31012005-04-17 16:05:31 -0500871 }
872 }
James Smarted957682007-06-17 19:56:37 -0500873 mempool_free(pmb, phba->mbox_mem_pool);
874 return ret;
dea31012005-04-17 16:05:31 -0500875}
876
James Smarte59058c2008-08-24 21:49:00 -0400877/**
James Smart3621a712009-04-06 18:47:14 -0400878 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400879 * @phba: Pointer to HBA context object.
880 * @pring: Pointer to driver SLI ring object.
881 * @piocb: Pointer to the driver iocb object.
882 *
883 * This function is called with hbalock held. The function adds the
884 * new iocb to txcmplq of the given ring. This function always returns
885 * 0. If this function is called for ELS ring, this function checks if
886 * there is a vport associated with the ELS command. This function also
887 * starts els_tmofunc timer if this is an ELS command.
888 **/
dea31012005-04-17 16:05:31 -0500889static int
James Smart2e0fef82007-06-17 19:56:36 -0500890lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
891 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500892{
dea31012005-04-17 16:05:31 -0500893 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -0400894 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -0500895 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -0400896 if (pring->txcmplq_cnt > pring->txcmplq_max)
897 pring->txcmplq_max = pring->txcmplq_cnt;
898
James Smart92d7f7b2007-06-17 19:56:38 -0500899 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
900 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
901 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
902 if (!piocb->vport)
903 BUG();
904 else
905 mod_timer(&piocb->vport->els_tmofunc,
906 jiffies + HZ * (phba->fc_ratov << 1));
907 }
908
dea31012005-04-17 16:05:31 -0500909
James Smart2e0fef82007-06-17 19:56:36 -0500910 return 0;
dea31012005-04-17 16:05:31 -0500911}
912
James Smarte59058c2008-08-24 21:49:00 -0400913/**
James Smart3621a712009-04-06 18:47:14 -0400914 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400915 * @phba: Pointer to HBA context object.
916 * @pring: Pointer to driver SLI ring object.
917 *
918 * This function is called with hbalock held to get next
919 * iocb in txq of the given ring. If there is any iocb in
920 * the txq, the function returns first iocb in the list after
921 * removing the iocb from the list, else it returns NULL.
922 **/
James Smart2a9bf3d2010-06-07 15:24:45 -0400923struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500924lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500925{
dea31012005-04-17 16:05:31 -0500926 struct lpfc_iocbq *cmd_iocb;
927
James Smart858c9f62007-06-17 19:56:39 -0500928 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
929 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500930 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500931 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500932}
933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
James Smart3621a712009-04-06 18:47:14 -0400935 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400936 * @phba: Pointer to HBA context object.
937 * @pring: Pointer to driver SLI ring object.
938 *
939 * This function is called with hbalock held and the caller must post the
940 * iocb without releasing the lock. If the caller releases the lock,
941 * iocb slot returned by the function is not guaranteed to be available.
942 * The function returns pointer to the next available iocb slot if there
943 * is available slot in the ring, else it returns NULL.
944 * If the get index of the ring is ahead of the put index, the function
945 * will post an error attention event to the worker thread to take the
946 * HBA to offline state.
947 **/
dea31012005-04-17 16:05:31 -0500948static IOCB_t *
949lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
950{
James Smart34b02dc2008-08-24 21:49:55 -0400951 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500952 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500953 if ((pring->next_cmdidx == pring->cmdidx) &&
954 (++pring->next_cmdidx >= max_cmd_idx))
955 pring->next_cmdidx = 0;
956
957 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
958
959 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
960
961 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400963 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200964 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400965 pring->ringno,
dea31012005-04-17 16:05:31 -0500966 pring->local_getidx, max_cmd_idx);
967
James Smart2e0fef82007-06-17 19:56:36 -0500968 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500969 /*
970 * All error attention handlers are posted to
971 * worker thread
972 */
973 phba->work_ha |= HA_ERATT;
974 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500975
James Smart5e9d9b82008-06-14 22:52:53 -0400976 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500977
978 return NULL;
979 }
980
981 if (pring->local_getidx == pring->next_cmdidx)
982 return NULL;
983 }
984
James Smarted957682007-06-17 19:56:37 -0500985 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500986}
987
James Smarte59058c2008-08-24 21:49:00 -0400988/**
James Smart3621a712009-04-06 18:47:14 -0400989 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400990 * @phba: Pointer to HBA context object.
991 * @iocbq: Pointer to driver iocb object.
992 *
993 * This function gets an iotag for the iocb. If there is no unused iotag and
994 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
995 * array and assigns a new iotag.
996 * The function returns the allocated iotag if successful, else returns zero.
997 * Zero is not a valid iotag.
998 * The caller is not required to hold any lock.
999 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001000uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001001lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001002{
James Smart2e0fef82007-06-17 19:56:36 -05001003 struct lpfc_iocbq **new_arr;
1004 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001005 size_t new_len;
1006 struct lpfc_sli *psli = &phba->sli;
1007 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001008
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001010 iotag = psli->last_iotag;
1011 if(++iotag < psli->iocbq_lookup_len) {
1012 psli->last_iotag = iotag;
1013 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001014 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001015 iocbq->iotag = iotag;
1016 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001017 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001018 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1019 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001020 spin_unlock_irq(&phba->hbalock);
1021 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001022 GFP_KERNEL);
1023 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001024 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001025 old_arr = psli->iocbq_lookup;
1026 if (new_len <= psli->iocbq_lookup_len) {
1027 /* highly unprobable case */
1028 kfree(new_arr);
1029 iotag = psli->last_iotag;
1030 if(++iotag < psli->iocbq_lookup_len) {
1031 psli->last_iotag = iotag;
1032 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001033 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001034 iocbq->iotag = iotag;
1035 return iotag;
1036 }
James Smart2e0fef82007-06-17 19:56:36 -05001037 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001038 return 0;
1039 }
1040 if (psli->iocbq_lookup)
1041 memcpy(new_arr, old_arr,
1042 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001043 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001044 psli->iocbq_lookup = new_arr;
1045 psli->iocbq_lookup_len = new_len;
1046 psli->last_iotag = iotag;
1047 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001048 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001049 iocbq->iotag = iotag;
1050 kfree(old_arr);
1051 return iotag;
1052 }
James Smart8f6d98d2006-08-01 07:34:00 -04001053 } else
James Smart2e0fef82007-06-17 19:56:36 -05001054 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001055
James Bottomley604a3e32005-10-29 10:28:33 -05001056 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001057 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1058 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001059
James Bottomley604a3e32005-10-29 10:28:33 -05001060 return 0;
dea31012005-04-17 16:05:31 -05001061}
1062
James Smarte59058c2008-08-24 21:49:00 -04001063/**
James Smart3621a712009-04-06 18:47:14 -04001064 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001065 * @phba: Pointer to HBA context object.
1066 * @pring: Pointer to driver SLI ring object.
1067 * @iocb: Pointer to iocb slot in the ring.
1068 * @nextiocb: Pointer to driver iocb object which need to be
1069 * posted to firmware.
1070 *
1071 * This function is called with hbalock held to post a new iocb to
1072 * the firmware. This function copies the new iocb to ring iocb slot and
1073 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1074 * a completion call back for this iocb else the function will free the
1075 * iocb object.
1076 **/
dea31012005-04-17 16:05:31 -05001077static void
1078lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1079 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1080{
1081 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001082 * Set up an iotag
dea31012005-04-17 16:05:31 -05001083 */
James Bottomley604a3e32005-10-29 10:28:33 -05001084 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001085
James Smarte2a0a9d2008-12-04 22:40:02 -05001086
James Smarta58cbd52007-08-02 11:09:43 -04001087 if (pring->ringno == LPFC_ELS_RING) {
1088 lpfc_debugfs_slow_ring_trc(phba,
1089 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1090 *(((uint32_t *) &nextiocb->iocb) + 4),
1091 *(((uint32_t *) &nextiocb->iocb) + 6),
1092 *(((uint32_t *) &nextiocb->iocb) + 7));
1093 }
1094
dea31012005-04-17 16:05:31 -05001095 /*
1096 * Issue iocb command to adapter
1097 */
James Smart92d7f7b2007-06-17 19:56:38 -05001098 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001099 wmb();
1100 pring->stats.iocb_cmd++;
1101
1102 /*
1103 * If there is no completion routine to call, we can release the
1104 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1105 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1106 */
1107 if (nextiocb->iocb_cmpl)
1108 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001109 else
James Smart2e0fef82007-06-17 19:56:36 -05001110 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001111
1112 /*
1113 * Let the HBA know what IOCB slot will be the next one the
1114 * driver will put a command into.
1115 */
1116 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001117 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001118}
1119
James Smarte59058c2008-08-24 21:49:00 -04001120/**
James Smart3621a712009-04-06 18:47:14 -04001121 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001122 * @phba: Pointer to HBA context object.
1123 * @pring: Pointer to driver SLI ring object.
1124 *
1125 * The caller is not required to hold any lock for calling this function.
1126 * This function updates the chip attention bits for the ring to inform firmware
1127 * that there are pending work to be done for this ring and requests an
1128 * interrupt when there is space available in the ring. This function is
1129 * called when the driver is unable to post more iocbs to the ring due
1130 * to unavailability of space in the ring.
1131 **/
dea31012005-04-17 16:05:31 -05001132static void
James Smart2e0fef82007-06-17 19:56:36 -05001133lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001134{
1135 int ringno = pring->ringno;
1136
1137 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1138
1139 wmb();
1140
1141 /*
1142 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1143 * The HBA will tell us when an IOCB entry is available.
1144 */
1145 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1146 readl(phba->CAregaddr); /* flush */
1147
1148 pring->stats.iocb_cmd_full++;
1149}
1150
James Smarte59058c2008-08-24 21:49:00 -04001151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001153 * @phba: Pointer to HBA context object.
1154 * @pring: Pointer to driver SLI ring object.
1155 *
1156 * This function updates the chip attention register bit for the
1157 * given ring to inform HBA that there is more work to be done
1158 * in this ring. The caller is not required to hold any lock.
1159 **/
dea31012005-04-17 16:05:31 -05001160static void
James Smart2e0fef82007-06-17 19:56:36 -05001161lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001162{
1163 int ringno = pring->ringno;
1164
1165 /*
1166 * Tell the HBA that there is work to do in this ring.
1167 */
James Smart34b02dc2008-08-24 21:49:55 -04001168 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1169 wmb();
1170 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1171 readl(phba->CAregaddr); /* flush */
1172 }
dea31012005-04-17 16:05:31 -05001173}
1174
James Smarte59058c2008-08-24 21:49:00 -04001175/**
James Smart3621a712009-04-06 18:47:14 -04001176 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001177 * @phba: Pointer to HBA context object.
1178 * @pring: Pointer to driver SLI ring object.
1179 *
1180 * This function is called with hbalock held to post pending iocbs
1181 * in the txq to the firmware. This function is called when driver
1182 * detects space available in the ring.
1183 **/
dea31012005-04-17 16:05:31 -05001184static void
James Smart2e0fef82007-06-17 19:56:36 -05001185lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001186{
1187 IOCB_t *iocb;
1188 struct lpfc_iocbq *nextiocb;
1189
1190 /*
1191 * Check to see if:
1192 * (a) there is anything on the txq to send
1193 * (b) link is up
1194 * (c) link attention events can be processed (fcp ring only)
1195 * (d) IOCB processing is not blocked by the outstanding mbox command.
1196 */
1197 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001198 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001199 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001200 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001201
1202 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1203 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1204 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1205
1206 if (iocb)
1207 lpfc_sli_update_ring(phba, pring);
1208 else
1209 lpfc_sli_update_full_ring(phba, pring);
1210 }
1211
1212 return;
1213}
1214
James Smarte59058c2008-08-24 21:49:00 -04001215/**
James Smart3621a712009-04-06 18:47:14 -04001216 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001217 * @phba: Pointer to HBA context object.
1218 * @hbqno: HBQ number.
1219 *
1220 * This function is called with hbalock held to get the next
1221 * available slot for the given HBQ. If there is free slot
1222 * available for the HBQ it will return pointer to the next available
1223 * HBQ entry else it will return NULL.
1224 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001225static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001226lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1227{
1228 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1229
1230 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1231 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1232 hbqp->next_hbqPutIdx = 0;
1233
1234 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001235 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001236 uint32_t getidx = le32_to_cpu(raw_index);
1237
1238 hbqp->local_hbqGetIdx = getidx;
1239
1240 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1241 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001242 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001243 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001244 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001245 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001246 hbqp->entry_count);
1247
1248 phba->link_state = LPFC_HBA_ERROR;
1249 return NULL;
1250 }
1251
1252 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1253 return NULL;
1254 }
1255
James Smart51ef4c22007-08-02 11:10:31 -04001256 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1257 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001258}
1259
James Smarte59058c2008-08-24 21:49:00 -04001260/**
James Smart3621a712009-04-06 18:47:14 -04001261 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001262 * @phba: Pointer to HBA context object.
1263 *
1264 * This function is called with no lock held to free all the
1265 * hbq buffers while uninitializing the SLI interface. It also
1266 * frees the HBQ buffers returned by the firmware but not yet
1267 * processed by the upper layers.
1268 **/
James Smarted957682007-06-17 19:56:37 -05001269void
1270lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1271{
James Smart92d7f7b2007-06-17 19:56:38 -05001272 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1273 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001274 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001275 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001276 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001277
James Smart51ef4c22007-08-02 11:10:31 -04001278 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001279 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001280 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001281 for (i = 0; i < hbq_count; ++i) {
1282 list_for_each_entry_safe(dmabuf, next_dmabuf,
1283 &phba->hbqs[i].hbq_buffer_list, list) {
1284 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1285 list_del(&hbq_buf->dbuf.list);
1286 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1287 }
James Smarta8adb832007-10-27 13:37:53 -04001288 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001289 }
James Smart3163f722008-02-08 18:50:25 -05001290 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001291 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1292 list) {
James Smart3163f722008-02-08 18:50:25 -05001293 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1294 list_del(&hbq_buf->dbuf.list);
1295 if (hbq_buf->tag == -1) {
1296 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1297 (phba, hbq_buf);
1298 } else {
1299 hbqno = hbq_buf->tag >> 16;
1300 if (hbqno >= LPFC_MAX_HBQS)
1301 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1302 (phba, hbq_buf);
1303 else
1304 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1305 hbq_buf);
1306 }
1307 }
1308
1309 /* Mark the HBQs not in use */
1310 phba->hbq_in_use = 0;
1311 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001312}
1313
James Smarte59058c2008-08-24 21:49:00 -04001314/**
James Smart3621a712009-04-06 18:47:14 -04001315 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001316 * @phba: Pointer to HBA context object.
1317 * @hbqno: HBQ number.
1318 * @hbq_buf: Pointer to HBQ buffer.
1319 *
1320 * This function is called with the hbalock held to post a
1321 * hbq buffer to the firmware. If the function finds an empty
1322 * slot in the HBQ, it will post the buffer. The function will return
1323 * pointer to the hbq entry if it successfully post the buffer
1324 * else it will return NULL.
1325 **/
James Smart3772a992009-05-22 14:50:54 -04001326static int
James Smarted957682007-06-17 19:56:37 -05001327lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001328 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001329{
James Smart3772a992009-05-22 14:50:54 -04001330 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1331}
1332
1333/**
1334 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1335 * @phba: Pointer to HBA context object.
1336 * @hbqno: HBQ number.
1337 * @hbq_buf: Pointer to HBQ buffer.
1338 *
1339 * This function is called with the hbalock held to post a hbq buffer to the
1340 * firmware. If the function finds an empty slot in the HBQ, it will post the
1341 * buffer and place it on the hbq_buffer_list. The function will return zero if
1342 * it successfully post the buffer else it will return an error.
1343 **/
1344static int
1345lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1346 struct hbq_dmabuf *hbq_buf)
1347{
James Smarted957682007-06-17 19:56:37 -05001348 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001349 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001350
1351 /* Get next HBQ entry slot to use */
1352 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1353 if (hbqe) {
1354 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1355
James Smart92d7f7b2007-06-17 19:56:38 -05001356 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1357 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001358 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001359 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001360 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1361 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1362 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001363 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1364 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001365 /* flush */
James Smarted957682007-06-17 19:56:37 -05001366 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001367 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001368 return 0;
1369 } else
1370 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001371}
1372
James Smart4f774512009-05-22 14:52:35 -04001373/**
1374 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1375 * @phba: Pointer to HBA context object.
1376 * @hbqno: HBQ number.
1377 * @hbq_buf: Pointer to HBQ buffer.
1378 *
1379 * This function is called with the hbalock held to post an RQE to the SLI4
1380 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1381 * the hbq_buffer_list and return zero, otherwise it will return an error.
1382 **/
1383static int
1384lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1385 struct hbq_dmabuf *hbq_buf)
1386{
1387 int rc;
1388 struct lpfc_rqe hrqe;
1389 struct lpfc_rqe drqe;
1390
1391 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1392 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1393 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1394 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1395 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1396 &hrqe, &drqe);
1397 if (rc < 0)
1398 return rc;
1399 hbq_buf->tag = rc;
1400 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1401 return 0;
1402}
1403
James Smarte59058c2008-08-24 21:49:00 -04001404/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001405static struct lpfc_hbq_init lpfc_els_hbq = {
1406 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001407 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001408 .mask_count = 0,
1409 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001410 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001411 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001412 .init_count = 40,
1413 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001414};
James Smarted957682007-06-17 19:56:37 -05001415
James Smarte59058c2008-08-24 21:49:00 -04001416/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001417static struct lpfc_hbq_init lpfc_extra_hbq = {
1418 .rn = 1,
1419 .entry_count = 200,
1420 .mask_count = 0,
1421 .profile = 0,
1422 .ring_mask = (1 << LPFC_EXTRA_RING),
1423 .buffer_count = 0,
1424 .init_count = 0,
1425 .add_count = 5,
1426};
1427
James Smarte59058c2008-08-24 21:49:00 -04001428/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001429struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001430 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001431 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001432};
1433
James Smarte59058c2008-08-24 21:49:00 -04001434/**
James Smart3621a712009-04-06 18:47:14 -04001435 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001436 * @phba: Pointer to HBA context object.
1437 * @hbqno: HBQ number.
1438 * @count: Number of HBQ buffers to be posted.
1439 *
James Smartd7c255b2008-08-24 21:50:00 -04001440 * This function is called with no lock held to post more hbq buffers to the
1441 * given HBQ. The function returns the number of HBQ buffers successfully
1442 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001443 **/
James Smart311464e2007-08-02 11:10:37 -04001444static int
James Smart92d7f7b2007-06-17 19:56:38 -05001445lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1446{
James Smartd7c255b2008-08-24 21:50:00 -04001447 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001448 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001449 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001450 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001451 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001452 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001453
James Smartd7c255b2008-08-24 21:50:00 -04001454 if ((phba->hbqs[hbqno].buffer_count + count) >
1455 lpfc_hbq_defs[hbqno]->entry_count)
1456 count = lpfc_hbq_defs[hbqno]->entry_count -
1457 phba->hbqs[hbqno].buffer_count;
1458 if (!count)
1459 return 0;
1460 /* Allocate HBQ entries */
1461 for (i = 0; i < count; i++) {
1462 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1463 if (!hbq_buffer)
1464 break;
1465 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1466 }
James Smart3163f722008-02-08 18:50:25 -05001467 /* Check whether HBQ is still in use */
1468 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001469 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001470 goto err;
1471 while (!list_empty(&hbq_buf_list)) {
1472 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1473 dbuf.list);
1474 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1475 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001476 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001477 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001478 posted++;
1479 } else
James Smart51ef4c22007-08-02 11:10:31 -04001480 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001481 }
James Smart3163f722008-02-08 18:50:25 -05001482 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001483 return posted;
1484err:
1485 spin_unlock_irqrestore(&phba->hbalock, flags);
1486 while (!list_empty(&hbq_buf_list)) {
1487 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1488 dbuf.list);
1489 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1490 }
James Smart92d7f7b2007-06-17 19:56:38 -05001491 return 0;
James Smarted957682007-06-17 19:56:37 -05001492}
1493
James Smarte59058c2008-08-24 21:49:00 -04001494/**
James Smart3621a712009-04-06 18:47:14 -04001495 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001496 * @phba: Pointer to HBA context object.
1497 * @qno: HBQ number.
1498 *
1499 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001500 * is called with no lock held. The function returns the number of HBQ entries
1501 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001502 **/
James Smarted957682007-06-17 19:56:37 -05001503int
James Smart92d7f7b2007-06-17 19:56:38 -05001504lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001505{
James Smartdef9c7a2009-12-21 17:02:28 -05001506 if (phba->sli_rev == LPFC_SLI_REV4)
1507 return 0;
1508 else
1509 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1510 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001511}
1512
James Smarte59058c2008-08-24 21:49:00 -04001513/**
James Smart3621a712009-04-06 18:47:14 -04001514 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001515 * @phba: Pointer to HBA context object.
1516 * @qno: HBQ queue number.
1517 *
1518 * This function is called from SLI initialization code path with
1519 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001520 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001521 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001522static int
James Smart92d7f7b2007-06-17 19:56:38 -05001523lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001524{
James Smartdef9c7a2009-12-21 17:02:28 -05001525 if (phba->sli_rev == LPFC_SLI_REV4)
1526 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1527 lpfc_hbq_defs[qno]->entry_count);
1528 else
1529 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1530 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001531}
1532
James Smarte59058c2008-08-24 21:49:00 -04001533/**
James Smart3772a992009-05-22 14:50:54 -04001534 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1535 * @phba: Pointer to HBA context object.
1536 * @hbqno: HBQ number.
1537 *
1538 * This function removes the first hbq buffer on an hbq list and returns a
1539 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1540 **/
1541static struct hbq_dmabuf *
1542lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1543{
1544 struct lpfc_dmabuf *d_buf;
1545
1546 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1547 if (!d_buf)
1548 return NULL;
1549 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1550}
1551
1552/**
James Smart3621a712009-04-06 18:47:14 -04001553 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001554 * @phba: Pointer to HBA context object.
1555 * @tag: Tag of the hbq buffer.
1556 *
1557 * This function is called with hbalock held. This function searches
1558 * for the hbq buffer associated with the given tag in the hbq buffer
1559 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1560 * it returns NULL.
1561 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001562static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001563lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1564{
James Smart92d7f7b2007-06-17 19:56:38 -05001565 struct lpfc_dmabuf *d_buf;
1566 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001567 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001568
James Smart51ef4c22007-08-02 11:10:31 -04001569 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001570 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001571 return NULL;
1572
James Smart3772a992009-05-22 14:50:54 -04001573 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001574 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001575 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001576 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001577 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001578 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001579 }
1580 }
James Smart3772a992009-05-22 14:50:54 -04001581 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001583 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001584 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001585 return NULL;
James Smarted957682007-06-17 19:56:37 -05001586}
1587
James Smarte59058c2008-08-24 21:49:00 -04001588/**
James Smart3621a712009-04-06 18:47:14 -04001589 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001590 * @phba: Pointer to HBA context object.
1591 * @hbq_buffer: Pointer to HBQ buffer.
1592 *
1593 * This function is called with hbalock. This function gives back
1594 * the hbq buffer to firmware. If the HBQ does not have space to
1595 * post the buffer, it will free the buffer.
1596 **/
James Smarted957682007-06-17 19:56:37 -05001597void
James Smart51ef4c22007-08-02 11:10:31 -04001598lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001599{
1600 uint32_t hbqno;
1601
James Smart51ef4c22007-08-02 11:10:31 -04001602 if (hbq_buffer) {
1603 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001604 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001605 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001606 }
1607}
1608
James Smarte59058c2008-08-24 21:49:00 -04001609/**
James Smart3621a712009-04-06 18:47:14 -04001610 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001611 * @mbxCommand: mailbox command code.
1612 *
1613 * This function is called by the mailbox event handler function to verify
1614 * that the completed mailbox command is a legitimate mailbox command. If the
1615 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1616 * and the mailbox event handler will take the HBA offline.
1617 **/
dea31012005-04-17 16:05:31 -05001618static int
1619lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1620{
1621 uint8_t ret;
1622
1623 switch (mbxCommand) {
1624 case MBX_LOAD_SM:
1625 case MBX_READ_NV:
1626 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001627 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001628 case MBX_RUN_BIU_DIAG:
1629 case MBX_INIT_LINK:
1630 case MBX_DOWN_LINK:
1631 case MBX_CONFIG_LINK:
1632 case MBX_CONFIG_RING:
1633 case MBX_RESET_RING:
1634 case MBX_READ_CONFIG:
1635 case MBX_READ_RCONFIG:
1636 case MBX_READ_SPARM:
1637 case MBX_READ_STATUS:
1638 case MBX_READ_RPI:
1639 case MBX_READ_XRI:
1640 case MBX_READ_REV:
1641 case MBX_READ_LNK_STAT:
1642 case MBX_REG_LOGIN:
1643 case MBX_UNREG_LOGIN:
1644 case MBX_READ_LA:
1645 case MBX_CLEAR_LA:
1646 case MBX_DUMP_MEMORY:
1647 case MBX_DUMP_CONTEXT:
1648 case MBX_RUN_DIAGS:
1649 case MBX_RESTART:
1650 case MBX_UPDATE_CFG:
1651 case MBX_DOWN_LOAD:
1652 case MBX_DEL_LD_ENTRY:
1653 case MBX_RUN_PROGRAM:
1654 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001655 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001656 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001657 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001658 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001659 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001660 case MBX_LOAD_AREA:
1661 case MBX_RUN_BIU_DIAG64:
1662 case MBX_CONFIG_PORT:
1663 case MBX_READ_SPARM64:
1664 case MBX_READ_RPI64:
1665 case MBX_REG_LOGIN64:
1666 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001667 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001668 case MBX_SET_DEBUG:
1669 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001670 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001671 case MBX_REG_VPI:
1672 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001673 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001674 case MBX_PORT_CAPABILITIES:
1675 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001676 case MBX_SLI4_CONFIG:
1677 case MBX_SLI4_REQ_FTRS:
1678 case MBX_REG_FCFI:
1679 case MBX_UNREG_FCFI:
1680 case MBX_REG_VFI:
1681 case MBX_UNREG_VFI:
1682 case MBX_INIT_VPI:
1683 case MBX_INIT_VFI:
1684 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04001685 case MBX_READ_EVENT_LOG_STATUS:
1686 case MBX_READ_EVENT_LOG:
dea31012005-04-17 16:05:31 -05001687 ret = mbxCommand;
1688 break;
1689 default:
1690 ret = MBX_SHUTDOWN;
1691 break;
1692 }
James Smart2e0fef82007-06-17 19:56:36 -05001693 return ret;
dea31012005-04-17 16:05:31 -05001694}
James Smarte59058c2008-08-24 21:49:00 -04001695
1696/**
James Smart3621a712009-04-06 18:47:14 -04001697 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001698 * @phba: Pointer to HBA context object.
1699 * @pmboxq: Pointer to mailbox command.
1700 *
1701 * This is completion handler function for mailbox commands issued from
1702 * lpfc_sli_issue_mbox_wait function. This function is called by the
1703 * mailbox event handler function with no lock held. This function
1704 * will wake up thread waiting on the wait queue pointed by context1
1705 * of the mailbox.
1706 **/
James Smart04c68492009-05-22 14:52:52 -04001707void
James Smart2e0fef82007-06-17 19:56:36 -05001708lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001709{
1710 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001711 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001712
1713 /*
1714 * If pdone_q is empty, the driver thread gave up waiting and
1715 * continued running.
1716 */
James Smart7054a602007-04-25 09:52:34 -04001717 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001718 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001719 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1720 if (pdone_q)
1721 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001722 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001723 return;
1724}
1725
James Smarte59058c2008-08-24 21:49:00 -04001726
1727/**
James Smart3621a712009-04-06 18:47:14 -04001728 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001729 * @phba: Pointer to HBA context object.
1730 * @pmb: Pointer to mailbox object.
1731 *
1732 * This function is the default mailbox completion handler. It
1733 * frees the memory resources associated with the completed mailbox
1734 * command. If the completed command is a REG_LOGIN mailbox command,
1735 * this function will issue a UREG_LOGIN to re-claim the RPI.
1736 **/
dea31012005-04-17 16:05:31 -05001737void
James Smart2e0fef82007-06-17 19:56:36 -05001738lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001739{
1740 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001741 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001742 int rc;
James Smart695a8142010-01-26 23:08:03 -05001743 struct lpfc_vport *vport = pmb->vport;
James Smart7054a602007-04-25 09:52:34 -04001744
dea31012005-04-17 16:05:31 -05001745 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001746
dea31012005-04-17 16:05:31 -05001747 if (mp) {
1748 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1749 kfree(mp);
1750 }
James Smart7054a602007-04-25 09:52:34 -04001751
James Smart04c68492009-05-22 14:52:52 -04001752 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1753 (phba->sli_rev == LPFC_SLI_REV4))
1754 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1755
James Smart7054a602007-04-25 09:52:34 -04001756 /*
1757 * If a REG_LOGIN succeeded after node is destroyed or node
1758 * is in re-discovery driver need to cleanup the RPI.
1759 */
James Smart2e0fef82007-06-17 19:56:36 -05001760 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001761 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1762 !pmb->u.mb.mbxStatus) {
1763 rpi = pmb->u.mb.un.varWords[0];
1764 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1765 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001766 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001767 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1768 if (rc != MBX_NOT_FINISHED)
1769 return;
1770 }
1771
James Smart695a8142010-01-26 23:08:03 -05001772 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1773 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1774 !(phba->pport->load_flag & FC_UNLOADING) &&
1775 !pmb->u.mb.mbxStatus) {
1776 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1777 pmb->vport = vport;
1778 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1779 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1780 if (rc != MBX_NOT_FINISHED)
1781 return;
1782 }
1783
James Smart04c68492009-05-22 14:52:52 -04001784 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1785 lpfc_sli4_mbox_cmd_free(phba, pmb);
1786 else
1787 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001788}
1789
James Smarte59058c2008-08-24 21:49:00 -04001790/**
James Smart3621a712009-04-06 18:47:14 -04001791 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001792 * @phba: Pointer to HBA context object.
1793 *
1794 * This function is called with no lock held. This function processes all
1795 * the completed mailbox commands and gives it to upper layers. The interrupt
1796 * service routine processes mailbox completion interrupt and adds completed
1797 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1798 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1799 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1800 * function returns the mailbox commands to the upper layer by calling the
1801 * completion handler function of each mailbox.
1802 **/
dea31012005-04-17 16:05:31 -05001803int
James Smart2e0fef82007-06-17 19:56:36 -05001804lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001805{
James Smart92d7f7b2007-06-17 19:56:38 -05001806 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001807 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001808 int rc;
1809 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001810
1811 phba->sli.slistat.mbox_event++;
1812
James Smart92d7f7b2007-06-17 19:56:38 -05001813 /* Get all completed mailboxe buffers into the cmplq */
1814 spin_lock_irq(&phba->hbalock);
1815 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1816 spin_unlock_irq(&phba->hbalock);
1817
dea31012005-04-17 16:05:31 -05001818 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001819 do {
1820 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1821 if (pmb == NULL)
1822 break;
1823
James Smart04c68492009-05-22 14:52:52 -04001824 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001825
James Smart858c9f62007-06-17 19:56:39 -05001826 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1827 if (pmb->vport) {
1828 lpfc_debugfs_disc_trc(pmb->vport,
1829 LPFC_DISC_TRC_MBOX_VPORT,
1830 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1831 (uint32_t)pmbox->mbxCommand,
1832 pmbox->un.varWords[0],
1833 pmbox->un.varWords[1]);
1834 }
1835 else {
1836 lpfc_debugfs_disc_trc(phba->pport,
1837 LPFC_DISC_TRC_MBOX,
1838 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1839 (uint32_t)pmbox->mbxCommand,
1840 pmbox->un.varWords[0],
1841 pmbox->un.varWords[1]);
1842 }
1843 }
1844
dea31012005-04-17 16:05:31 -05001845 /*
1846 * It is a fatal error if unknown mbox command completion.
1847 */
1848 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1849 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001850 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001852 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001853 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001854 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001855 pmbox->mbxCommand,
1856 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001857 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001858 phba->work_hs = HS_FFER3;
1859 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001860 continue;
dea31012005-04-17 16:05:31 -05001861 }
1862
dea31012005-04-17 16:05:31 -05001863 if (pmbox->mbxStatus) {
1864 phba->sli.slistat.mbox_stat_err++;
1865 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1866 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001867 lpfc_printf_log(phba, KERN_INFO,
1868 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001869 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001870 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001871 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001872 pmb->vport ? pmb->vport->vpi :0,
1873 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001874 lpfc_sli4_mbox_opcode_get(phba,
1875 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001876 pmbox->mbxStatus,
1877 pmbox->un.varWords[0],
1878 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001879 pmbox->mbxStatus = 0;
1880 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001882 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001883 continue;
dea31012005-04-17 16:05:31 -05001884 }
1885 }
1886
1887 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001888 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001889 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001890 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001891 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001892 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001893 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001894 pmb->mbox_cmpl,
1895 *((uint32_t *) pmbox),
1896 pmbox->un.varWords[0],
1897 pmbox->un.varWords[1],
1898 pmbox->un.varWords[2],
1899 pmbox->un.varWords[3],
1900 pmbox->un.varWords[4],
1901 pmbox->un.varWords[5],
1902 pmbox->un.varWords[6],
1903 pmbox->un.varWords[7]);
1904
James Smart92d7f7b2007-06-17 19:56:38 -05001905 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001906 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001907 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001908 return 0;
dea31012005-04-17 16:05:31 -05001909}
James Smart92d7f7b2007-06-17 19:56:38 -05001910
James Smarte59058c2008-08-24 21:49:00 -04001911/**
James Smart3621a712009-04-06 18:47:14 -04001912 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001913 * @phba: Pointer to HBA context object.
1914 * @pring: Pointer to driver SLI ring object.
1915 * @tag: buffer tag.
1916 *
1917 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1918 * is set in the tag the buffer is posted for a particular exchange,
1919 * the function will return the buffer without replacing the buffer.
1920 * If the buffer is for unsolicited ELS or CT traffic, this function
1921 * returns the buffer and also posts another buffer to the firmware.
1922 **/
James Smart76bb24e2007-10-27 13:38:00 -04001923static struct lpfc_dmabuf *
1924lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001925 struct lpfc_sli_ring *pring,
1926 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001927{
James Smart9f1e1b52008-12-04 22:39:40 -05001928 struct hbq_dmabuf *hbq_entry;
1929
James Smart76bb24e2007-10-27 13:38:00 -04001930 if (tag & QUE_BUFTAG_BIT)
1931 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001932 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1933 if (!hbq_entry)
1934 return NULL;
1935 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001936}
James Smart57127f12007-10-27 13:37:05 -04001937
James Smart3772a992009-05-22 14:50:54 -04001938/**
1939 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1940 * @phba: Pointer to HBA context object.
1941 * @pring: Pointer to driver SLI ring object.
1942 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1943 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1944 * @fch_type: the type for the first frame of the sequence.
1945 *
1946 * This function is called with no lock held. This function uses the r_ctl and
1947 * type of the received sequence to find the correct callback function to call
1948 * to process the sequence.
1949 **/
1950static int
1951lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1952 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1953 uint32_t fch_type)
1954{
1955 int i;
1956
1957 /* unSolicited Responses */
1958 if (pring->prt[0].profile) {
1959 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1960 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1961 saveq);
1962 return 1;
1963 }
1964 /* We must search, based on rctl / type
1965 for the right routine */
1966 for (i = 0; i < pring->num_mask; i++) {
1967 if ((pring->prt[i].rctl == fch_r_ctl) &&
1968 (pring->prt[i].type == fch_type)) {
1969 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1970 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1971 (phba, pring, saveq);
1972 return 1;
1973 }
1974 }
1975 return 0;
1976}
James Smarte59058c2008-08-24 21:49:00 -04001977
1978/**
James Smart3621a712009-04-06 18:47:14 -04001979 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001980 * @phba: Pointer to HBA context object.
1981 * @pring: Pointer to driver SLI ring object.
1982 * @saveq: Pointer to the unsolicited iocb.
1983 *
1984 * This function is called with no lock held by the ring event handler
1985 * when there is an unsolicited iocb posted to the response ring by the
1986 * firmware. This function gets the buffer associated with the iocbs
1987 * and calls the event handler for the ring. This function handles both
1988 * qring buffers and hbq buffers.
1989 * When the function returns 1 the caller can free the iocb object otherwise
1990 * upper layer functions will free the iocb objects.
1991 **/
dea31012005-04-17 16:05:31 -05001992static int
1993lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1994 struct lpfc_iocbq *saveq)
1995{
1996 IOCB_t * irsp;
1997 WORD5 * w5p;
1998 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001999 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002000 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002001 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002002
2003 match = 0;
2004 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002005
2006 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2007 if (pring->lpfc_sli_rcv_async_status)
2008 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2009 else
2010 lpfc_printf_log(phba,
2011 KERN_WARNING,
2012 LOG_SLI,
2013 "0316 Ring %d handler: unexpected "
2014 "ASYNC_STATUS iocb received evt_code "
2015 "0x%x\n",
2016 pring->ringno,
2017 irsp->un.asyncstat.evt_code);
2018 return 1;
2019 }
2020
James Smart3163f722008-02-08 18:50:25 -05002021 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2022 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2023 if (irsp->ulpBdeCount > 0) {
2024 dmzbuf = lpfc_sli_get_buff(phba, pring,
2025 irsp->un.ulpWord[3]);
2026 lpfc_in_buf_free(phba, dmzbuf);
2027 }
2028
2029 if (irsp->ulpBdeCount > 1) {
2030 dmzbuf = lpfc_sli_get_buff(phba, pring,
2031 irsp->unsli3.sli3Words[3]);
2032 lpfc_in_buf_free(phba, dmzbuf);
2033 }
2034
2035 if (irsp->ulpBdeCount > 2) {
2036 dmzbuf = lpfc_sli_get_buff(phba, pring,
2037 irsp->unsli3.sli3Words[7]);
2038 lpfc_in_buf_free(phba, dmzbuf);
2039 }
2040
2041 return 1;
2042 }
2043
James Smart92d7f7b2007-06-17 19:56:38 -05002044 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002045 if (irsp->ulpBdeCount != 0) {
2046 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002047 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002048 if (!saveq->context2)
2049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_SLI,
2052 "0341 Ring %d Cannot find buffer for "
2053 "an unsolicited iocb. tag 0x%x\n",
2054 pring->ringno,
2055 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002056 }
2057 if (irsp->ulpBdeCount == 2) {
2058 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002059 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002060 if (!saveq->context3)
2061 lpfc_printf_log(phba,
2062 KERN_ERR,
2063 LOG_SLI,
2064 "0342 Ring %d Cannot find buffer for an"
2065 " unsolicited iocb. tag 0x%x\n",
2066 pring->ringno,
2067 irsp->unsli3.sli3Words[7]);
2068 }
2069 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002070 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002071 if (irsp->ulpBdeCount != 0) {
2072 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2073 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002074 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002075 lpfc_printf_log(phba,
2076 KERN_ERR,
2077 LOG_SLI,
2078 "0343 Ring %d Cannot find "
2079 "buffer for an unsolicited iocb"
2080 ". tag 0x%x\n", pring->ringno,
2081 irsp->un.ulpWord[3]);
2082 }
2083 if (irsp->ulpBdeCount == 2) {
2084 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2085 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002086 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002087 lpfc_printf_log(phba,
2088 KERN_ERR,
2089 LOG_SLI,
2090 "0344 Ring %d Cannot find "
2091 "buffer for an unsolicited "
2092 "iocb. tag 0x%x\n",
2093 pring->ringno,
2094 irsp->unsli3.sli3Words[7]);
2095 }
2096 }
James Smart92d7f7b2007-06-17 19:56:38 -05002097 }
James Smart9c2face2008-01-11 01:53:18 -05002098 if (irsp->ulpBdeCount != 0 &&
2099 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2100 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2101 int found = 0;
2102
2103 /* search continue save q for same XRI */
2104 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2105 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2106 list_add_tail(&saveq->list, &iocbq->list);
2107 found = 1;
2108 break;
2109 }
2110 }
2111 if (!found)
2112 list_add_tail(&saveq->clist,
2113 &pring->iocb_continue_saveq);
2114 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2115 list_del_init(&iocbq->clist);
2116 saveq = iocbq;
2117 irsp = &(saveq->iocb);
2118 } else
2119 return 0;
2120 }
2121 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2122 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2123 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002124 Rctl = FC_RCTL_ELS_REQ;
2125 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002126 } else {
2127 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2128 Rctl = w5p->hcsw.Rctl;
2129 Type = w5p->hcsw.Type;
2130
2131 /* Firmware Workaround */
2132 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2133 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2134 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002135 Rctl = FC_RCTL_ELS_REQ;
2136 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002137 w5p->hcsw.Rctl = Rctl;
2138 w5p->hcsw.Type = Type;
2139 }
2140 }
James Smart92d7f7b2007-06-17 19:56:38 -05002141
James Smart3772a992009-05-22 14:50:54 -04002142 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002143 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002144 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002145 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002146 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002147
James Smart92d7f7b2007-06-17 19:56:38 -05002148 return 1;
dea31012005-04-17 16:05:31 -05002149}
2150
James Smarte59058c2008-08-24 21:49:00 -04002151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: Pointer to HBA context object.
2154 * @pring: Pointer to driver SLI ring object.
2155 * @prspiocb: Pointer to response iocb object.
2156 *
2157 * This function looks up the iocb_lookup table to get the command iocb
2158 * corresponding to the given response iocb using the iotag of the
2159 * response iocb. This function is called with the hbalock held.
2160 * This function returns the command iocb object if it finds the command
2161 * iocb else returns NULL.
2162 **/
dea31012005-04-17 16:05:31 -05002163static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002164lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2165 struct lpfc_sli_ring *pring,
2166 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002167{
dea31012005-04-17 16:05:31 -05002168 struct lpfc_iocbq *cmd_iocb = NULL;
2169 uint16_t iotag;
2170
James Bottomley604a3e32005-10-29 10:28:33 -05002171 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002172
James Bottomley604a3e32005-10-29 10:28:33 -05002173 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2174 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002175 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002176 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2177 pring->txcmplq_cnt--;
2178 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2179 }
James Bottomley604a3e32005-10-29 10:28:33 -05002180 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002181 }
2182
dea31012005-04-17 16:05:31 -05002183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002184 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002185 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002186 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002187 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002188 return NULL;
2189}
2190
James Smarte59058c2008-08-24 21:49:00 -04002191/**
James Smart3772a992009-05-22 14:50:54 -04002192 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2193 * @phba: Pointer to HBA context object.
2194 * @pring: Pointer to driver SLI ring object.
2195 * @iotag: IOCB tag.
2196 *
2197 * This function looks up the iocb_lookup table to get the command iocb
2198 * corresponding to the given iotag. This function is called with the
2199 * hbalock held.
2200 * This function returns the command iocb object if it finds the command
2201 * iocb else returns NULL.
2202 **/
2203static struct lpfc_iocbq *
2204lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2205 struct lpfc_sli_ring *pring, uint16_t iotag)
2206{
2207 struct lpfc_iocbq *cmd_iocb;
2208
2209 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2210 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2211 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002212 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2213 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2214 pring->txcmplq_cnt--;
2215 }
James Smart3772a992009-05-22 14:50:54 -04002216 return cmd_iocb;
2217 }
2218
2219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2220 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2221 iotag, phba->sli.last_iotag);
2222 return NULL;
2223}
2224
2225/**
James Smart3621a712009-04-06 18:47:14 -04002226 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002227 * @phba: Pointer to HBA context object.
2228 * @pring: Pointer to driver SLI ring object.
2229 * @saveq: Pointer to the response iocb to be processed.
2230 *
2231 * This function is called by the ring event handler for non-fcp
2232 * rings when there is a new response iocb in the response ring.
2233 * The caller is not required to hold any locks. This function
2234 * gets the command iocb associated with the response iocb and
2235 * calls the completion handler for the command iocb. If there
2236 * is no completion handler, the function will free the resources
2237 * associated with command iocb. If the response iocb is for
2238 * an already aborted command iocb, the status of the completion
2239 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2240 * This function always returns 1.
2241 **/
dea31012005-04-17 16:05:31 -05002242static int
James Smart2e0fef82007-06-17 19:56:36 -05002243lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002244 struct lpfc_iocbq *saveq)
2245{
James Smart2e0fef82007-06-17 19:56:36 -05002246 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002247 int rc = 1;
2248 unsigned long iflag;
2249
2250 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002251 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002252 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002253 spin_unlock_irqrestore(&phba->hbalock, iflag);
2254
dea31012005-04-17 16:05:31 -05002255 if (cmdiocbp) {
2256 if (cmdiocbp->iocb_cmpl) {
2257 /*
James Smartea2151b2008-09-07 11:52:10 -04002258 * If an ELS command failed send an event to mgmt
2259 * application.
2260 */
2261 if (saveq->iocb.ulpStatus &&
2262 (pring->ringno == LPFC_ELS_RING) &&
2263 (cmdiocbp->iocb.ulpCommand ==
2264 CMD_ELS_REQUEST64_CR))
2265 lpfc_send_els_failure_event(phba,
2266 cmdiocbp, saveq);
2267
2268 /*
dea31012005-04-17 16:05:31 -05002269 * Post all ELS completions to the worker thread.
2270 * All other are passed to the completion callback.
2271 */
2272 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002273 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2274 (cmdiocbp->iocb_flag &
2275 LPFC_DRIVER_ABORTED)) {
2276 spin_lock_irqsave(&phba->hbalock,
2277 iflag);
James Smart07951072007-04-25 09:51:38 -04002278 cmdiocbp->iocb_flag &=
2279 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002280 spin_unlock_irqrestore(&phba->hbalock,
2281 iflag);
James Smart07951072007-04-25 09:51:38 -04002282 saveq->iocb.ulpStatus =
2283 IOSTAT_LOCAL_REJECT;
2284 saveq->iocb.un.ulpWord[4] =
2285 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002286
2287 /* Firmware could still be in progress
2288 * of DMAing payload, so don't free data
2289 * buffer till after a hbeat.
2290 */
James Smart341af102010-01-26 23:07:37 -05002291 spin_lock_irqsave(&phba->hbalock,
2292 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002293 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002294 spin_unlock_irqrestore(&phba->hbalock,
2295 iflag);
2296 }
James Smart0f65ff62010-02-26 14:14:23 -05002297 if (phba->sli_rev == LPFC_SLI_REV4) {
2298 if (saveq->iocb_flag &
2299 LPFC_EXCHANGE_BUSY) {
2300 /* Set cmdiocb flag for the
2301 * exchange busy so sgl (xri)
2302 * will not be released until
2303 * the abort xri is received
2304 * from hba.
2305 */
2306 spin_lock_irqsave(
2307 &phba->hbalock, iflag);
2308 cmdiocbp->iocb_flag |=
2309 LPFC_EXCHANGE_BUSY;
2310 spin_unlock_irqrestore(
2311 &phba->hbalock, iflag);
2312 }
2313 if (cmdiocbp->iocb_flag &
2314 LPFC_DRIVER_ABORTED) {
2315 /*
2316 * Clear LPFC_DRIVER_ABORTED
2317 * bit in case it was driver
2318 * initiated abort.
2319 */
2320 spin_lock_irqsave(
2321 &phba->hbalock, iflag);
2322 cmdiocbp->iocb_flag &=
2323 ~LPFC_DRIVER_ABORTED;
2324 spin_unlock_irqrestore(
2325 &phba->hbalock, iflag);
2326 cmdiocbp->iocb.ulpStatus =
2327 IOSTAT_LOCAL_REJECT;
2328 cmdiocbp->iocb.un.ulpWord[4] =
2329 IOERR_ABORT_REQUESTED;
2330 /*
2331 * For SLI4, irsiocb contains
2332 * NO_XRI in sli_xritag, it
2333 * shall not affect releasing
2334 * sgl (xri) process.
2335 */
2336 saveq->iocb.ulpStatus =
2337 IOSTAT_LOCAL_REJECT;
2338 saveq->iocb.un.ulpWord[4] =
2339 IOERR_SLI_ABORTED;
2340 spin_lock_irqsave(
2341 &phba->hbalock, iflag);
2342 saveq->iocb_flag |=
2343 LPFC_DELAY_MEM_FREE;
2344 spin_unlock_irqrestore(
2345 &phba->hbalock, iflag);
2346 }
James Smart07951072007-04-25 09:51:38 -04002347 }
dea31012005-04-17 16:05:31 -05002348 }
James Smart2e0fef82007-06-17 19:56:36 -05002349 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002350 } else
2351 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002352 } else {
2353 /*
2354 * Unknown initiating command based on the response iotag.
2355 * This could be the case on the ELS ring because of
2356 * lpfc_els_abort().
2357 */
2358 if (pring->ringno != LPFC_ELS_RING) {
2359 /*
2360 * Ring <ringno> handler: unexpected completion IoTag
2361 * <IoTag>
2362 */
James Smarta257bf92009-04-06 18:48:10 -04002363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002364 "0322 Ring %d handler: "
2365 "unexpected completion IoTag x%x "
2366 "Data: x%x x%x x%x x%x\n",
2367 pring->ringno,
2368 saveq->iocb.ulpIoTag,
2369 saveq->iocb.ulpStatus,
2370 saveq->iocb.un.ulpWord[4],
2371 saveq->iocb.ulpCommand,
2372 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002373 }
2374 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002375
dea31012005-04-17 16:05:31 -05002376 return rc;
2377}
2378
James Smarte59058c2008-08-24 21:49:00 -04002379/**
James Smart3621a712009-04-06 18:47:14 -04002380 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002381 * @phba: Pointer to HBA context object.
2382 * @pring: Pointer to driver SLI ring object.
2383 *
2384 * This function is called from the iocb ring event handlers when
2385 * put pointer is ahead of the get pointer for a ring. This function signal
2386 * an error attention condition to the worker thread and the worker
2387 * thread will transition the HBA to offline state.
2388 **/
James Smart2e0fef82007-06-17 19:56:36 -05002389static void
2390lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002391{
James Smart34b02dc2008-08-24 21:49:55 -04002392 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002393 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002394 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002395 * rsp ring <portRspMax>
2396 */
2397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002398 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002399 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002400 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002401 pring->numRiocb);
2402
James Smart2e0fef82007-06-17 19:56:36 -05002403 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002404
2405 /*
2406 * All error attention handlers are posted to
2407 * worker thread
2408 */
2409 phba->work_ha |= HA_ERATT;
2410 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002411
James Smart5e9d9b82008-06-14 22:52:53 -04002412 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002413
2414 return;
2415}
2416
James Smarte59058c2008-08-24 21:49:00 -04002417/**
James Smart3621a712009-04-06 18:47:14 -04002418 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002419 * @ptr: Pointer to address of HBA context object.
2420 *
2421 * This function is invoked by the Error Attention polling timer when the
2422 * timer times out. It will check the SLI Error Attention register for
2423 * possible attention events. If so, it will post an Error Attention event
2424 * and wake up worker thread to process it. Otherwise, it will set up the
2425 * Error Attention polling timer for the next poll.
2426 **/
2427void lpfc_poll_eratt(unsigned long ptr)
2428{
2429 struct lpfc_hba *phba;
2430 uint32_t eratt = 0;
2431
2432 phba = (struct lpfc_hba *)ptr;
2433
2434 /* Check chip HA register for error event */
2435 eratt = lpfc_sli_check_eratt(phba);
2436
2437 if (eratt)
2438 /* Tell the worker thread there is work to do */
2439 lpfc_worker_wake_up(phba);
2440 else
2441 /* Restart the timer for next eratt poll */
2442 mod_timer(&phba->eratt_poll, jiffies +
2443 HZ * LPFC_ERATT_POLL_INTERVAL);
2444 return;
2445}
2446
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002447
James Smarte59058c2008-08-24 21:49:00 -04002448/**
James Smart3621a712009-04-06 18:47:14 -04002449 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002450 * @phba: Pointer to HBA context object.
2451 * @pring: Pointer to driver SLI ring object.
2452 * @mask: Host attention register mask for this ring.
2453 *
2454 * This function is called from the interrupt context when there is a ring
2455 * event for the fcp ring. The caller does not hold any lock.
2456 * The function processes each response iocb in the response ring until it
2457 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2458 * LE bit set. The function will call the completion handler of the command iocb
2459 * if the response iocb indicates a completion for a command iocb or it is
2460 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2461 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002462 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002463 * to check it explicitly.
2464 */
2465int
James Smart2e0fef82007-06-17 19:56:36 -05002466lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2467 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002468{
James Smart34b02dc2008-08-24 21:49:55 -04002469 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002470 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002471 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002472 struct lpfc_iocbq *cmdiocbq = NULL;
2473 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002474 uint32_t status;
2475 uint32_t portRspPut, portRspMax;
2476 int rc = 1;
2477 lpfc_iocb_type type;
2478 unsigned long iflag;
2479 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002480
James Smart2e0fef82007-06-17 19:56:36 -05002481 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002482 pring->stats.iocb_event++;
2483
dea31012005-04-17 16:05:31 -05002484 /*
2485 * The next available response entry should never exceed the maximum
2486 * entries. If it does, treat it as an adapter hardware error.
2487 */
2488 portRspMax = pring->numRiocb;
2489 portRspPut = le32_to_cpu(pgp->rspPutInx);
2490 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002491 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002492 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002493 return 1;
2494 }
James Smart45ed1192009-10-02 15:17:02 -04002495 if (phba->fcp_ring_in_use) {
2496 spin_unlock_irqrestore(&phba->hbalock, iflag);
2497 return 1;
2498 } else
2499 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002500
2501 rmb();
2502 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002503 /*
2504 * Fetch an entry off the ring and copy it into a local data
2505 * structure. The copy involves a byte-swap since the
2506 * network byte order and pci byte orders are different.
2507 */
James Smarted957682007-06-17 19:56:37 -05002508 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002509 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002510
2511 if (++pring->rspidx >= portRspMax)
2512 pring->rspidx = 0;
2513
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002514 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2515 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002516 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002517 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002518 irsp = &rspiocbq.iocb;
2519
dea31012005-04-17 16:05:31 -05002520 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2521 pring->stats.iocb_rsp++;
2522 rsp_cmpl++;
2523
2524 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002525 /*
2526 * If resource errors reported from HBA, reduce
2527 * queuedepths of the SCSI device.
2528 */
2529 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2530 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2531 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002532 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002533 spin_lock_irqsave(&phba->hbalock, iflag);
2534 }
2535
dea31012005-04-17 16:05:31 -05002536 /* Rsp ring <ringno> error: IOCB */
2537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002538 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002539 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002540 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002541 irsp->un.ulpWord[0],
2542 irsp->un.ulpWord[1],
2543 irsp->un.ulpWord[2],
2544 irsp->un.ulpWord[3],
2545 irsp->un.ulpWord[4],
2546 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002547 *(uint32_t *)&irsp->un1,
2548 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002549 }
2550
2551 switch (type) {
2552 case LPFC_ABORT_IOCB:
2553 case LPFC_SOL_IOCB:
2554 /*
2555 * Idle exchange closed via ABTS from port. No iocb
2556 * resources need to be recovered.
2557 */
2558 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002559 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002560 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002561 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002562 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002563 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002564 break;
2565 }
2566
James Bottomley604a3e32005-10-29 10:28:33 -05002567 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2568 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002569 if (unlikely(!cmdiocbq))
2570 break;
2571 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2572 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2573 if (cmdiocbq->iocb_cmpl) {
2574 spin_unlock_irqrestore(&phba->hbalock, iflag);
2575 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2576 &rspiocbq);
2577 spin_lock_irqsave(&phba->hbalock, iflag);
2578 }
dea31012005-04-17 16:05:31 -05002579 break;
James Smarta4bc3372006-12-02 13:34:16 -05002580 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002581 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002582 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002583 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002584 break;
dea31012005-04-17 16:05:31 -05002585 default:
2586 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2587 char adaptermsg[LPFC_MAX_ADPTMSG];
2588 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2589 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2590 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002591 dev_warn(&((phba->pcidev)->dev),
2592 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002593 phba->brd_no, adaptermsg);
2594 } else {
2595 /* Unknown IOCB command */
2596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002597 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002598 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002599 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002600 irsp->ulpStatus,
2601 irsp->ulpIoTag,
2602 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002603 }
2604 break;
2605 }
2606
2607 /*
2608 * The response IOCB has been processed. Update the ring
2609 * pointer in SLIM. If the port response put pointer has not
2610 * been updated, sync the pgp->rspPutInx and fetch the new port
2611 * response put pointer.
2612 */
James Smarted957682007-06-17 19:56:37 -05002613 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002614
2615 if (pring->rspidx == portRspPut)
2616 portRspPut = le32_to_cpu(pgp->rspPutInx);
2617 }
2618
2619 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2620 pring->stats.iocb_rsp_full++;
2621 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2622 writel(status, phba->CAregaddr);
2623 readl(phba->CAregaddr);
2624 }
2625 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2626 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2627 pring->stats.iocb_cmd_empty++;
2628
2629 /* Force update of the local copy of cmdGetInx */
2630 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2631 lpfc_sli_resume_iocb(phba, pring);
2632
2633 if ((pring->lpfc_sli_cmd_available))
2634 (pring->lpfc_sli_cmd_available) (phba, pring);
2635
2636 }
2637
James Smart45ed1192009-10-02 15:17:02 -04002638 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002639 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002640 return rc;
2641}
2642
James Smarte59058c2008-08-24 21:49:00 -04002643/**
James Smart3772a992009-05-22 14:50:54 -04002644 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2645 * @phba: Pointer to HBA context object.
2646 * @pring: Pointer to driver SLI ring object.
2647 * @rspiocbp: Pointer to driver response IOCB object.
2648 *
2649 * This function is called from the worker thread when there is a slow-path
2650 * response IOCB to process. This function chains all the response iocbs until
2651 * seeing the iocb with the LE bit set. The function will call
2652 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2653 * completion of a command iocb. The function will call the
2654 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2655 * The function frees the resources or calls the completion handler if this
2656 * iocb is an abort completion. The function returns NULL when the response
2657 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2658 * this function shall chain the iocb on to the iocb_continueq and return the
2659 * response iocb passed in.
2660 **/
2661static struct lpfc_iocbq *
2662lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2663 struct lpfc_iocbq *rspiocbp)
2664{
2665 struct lpfc_iocbq *saveq;
2666 struct lpfc_iocbq *cmdiocbp;
2667 struct lpfc_iocbq *next_iocb;
2668 IOCB_t *irsp = NULL;
2669 uint32_t free_saveq;
2670 uint8_t iocb_cmd_type;
2671 lpfc_iocb_type type;
2672 unsigned long iflag;
2673 int rc;
2674
2675 spin_lock_irqsave(&phba->hbalock, iflag);
2676 /* First add the response iocb to the countinueq list */
2677 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2678 pring->iocb_continueq_cnt++;
2679
2680 /* Now, determine whetehr the list is completed for processing */
2681 irsp = &rspiocbp->iocb;
2682 if (irsp->ulpLe) {
2683 /*
2684 * By default, the driver expects to free all resources
2685 * associated with this iocb completion.
2686 */
2687 free_saveq = 1;
2688 saveq = list_get_first(&pring->iocb_continueq,
2689 struct lpfc_iocbq, list);
2690 irsp = &(saveq->iocb);
2691 list_del_init(&pring->iocb_continueq);
2692 pring->iocb_continueq_cnt = 0;
2693
2694 pring->stats.iocb_rsp++;
2695
2696 /*
2697 * If resource errors reported from HBA, reduce
2698 * queuedepths of the SCSI device.
2699 */
2700 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2701 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2702 spin_unlock_irqrestore(&phba->hbalock, iflag);
2703 phba->lpfc_rampdown_queue_depth(phba);
2704 spin_lock_irqsave(&phba->hbalock, iflag);
2705 }
2706
2707 if (irsp->ulpStatus) {
2708 /* Rsp ring <ringno> error: IOCB */
2709 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2710 "0328 Rsp Ring %d error: "
2711 "IOCB Data: "
2712 "x%x x%x x%x x%x "
2713 "x%x x%x x%x x%x "
2714 "x%x x%x x%x x%x "
2715 "x%x x%x x%x x%x\n",
2716 pring->ringno,
2717 irsp->un.ulpWord[0],
2718 irsp->un.ulpWord[1],
2719 irsp->un.ulpWord[2],
2720 irsp->un.ulpWord[3],
2721 irsp->un.ulpWord[4],
2722 irsp->un.ulpWord[5],
2723 *(((uint32_t *) irsp) + 6),
2724 *(((uint32_t *) irsp) + 7),
2725 *(((uint32_t *) irsp) + 8),
2726 *(((uint32_t *) irsp) + 9),
2727 *(((uint32_t *) irsp) + 10),
2728 *(((uint32_t *) irsp) + 11),
2729 *(((uint32_t *) irsp) + 12),
2730 *(((uint32_t *) irsp) + 13),
2731 *(((uint32_t *) irsp) + 14),
2732 *(((uint32_t *) irsp) + 15));
2733 }
2734
2735 /*
2736 * Fetch the IOCB command type and call the correct completion
2737 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2738 * get freed back to the lpfc_iocb_list by the discovery
2739 * kernel thread.
2740 */
2741 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2742 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2743 switch (type) {
2744 case LPFC_SOL_IOCB:
2745 spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2747 spin_lock_irqsave(&phba->hbalock, iflag);
2748 break;
2749
2750 case LPFC_UNSOL_IOCB:
2751 spin_unlock_irqrestore(&phba->hbalock, iflag);
2752 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2753 spin_lock_irqsave(&phba->hbalock, iflag);
2754 if (!rc)
2755 free_saveq = 0;
2756 break;
2757
2758 case LPFC_ABORT_IOCB:
2759 cmdiocbp = NULL;
2760 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2761 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2762 saveq);
2763 if (cmdiocbp) {
2764 /* Call the specified completion routine */
2765 if (cmdiocbp->iocb_cmpl) {
2766 spin_unlock_irqrestore(&phba->hbalock,
2767 iflag);
2768 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2769 saveq);
2770 spin_lock_irqsave(&phba->hbalock,
2771 iflag);
2772 } else
2773 __lpfc_sli_release_iocbq(phba,
2774 cmdiocbp);
2775 }
2776 break;
2777
2778 case LPFC_UNKNOWN_IOCB:
2779 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2780 char adaptermsg[LPFC_MAX_ADPTMSG];
2781 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2782 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2783 MAX_MSG_DATA);
2784 dev_warn(&((phba->pcidev)->dev),
2785 "lpfc%d: %s\n",
2786 phba->brd_no, adaptermsg);
2787 } else {
2788 /* Unknown IOCB command */
2789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2790 "0335 Unknown IOCB "
2791 "command Data: x%x "
2792 "x%x x%x x%x\n",
2793 irsp->ulpCommand,
2794 irsp->ulpStatus,
2795 irsp->ulpIoTag,
2796 irsp->ulpContext);
2797 }
2798 break;
2799 }
2800
2801 if (free_saveq) {
2802 list_for_each_entry_safe(rspiocbp, next_iocb,
2803 &saveq->list, list) {
2804 list_del(&rspiocbp->list);
2805 __lpfc_sli_release_iocbq(phba, rspiocbp);
2806 }
2807 __lpfc_sli_release_iocbq(phba, saveq);
2808 }
2809 rspiocbp = NULL;
2810 }
2811 spin_unlock_irqrestore(&phba->hbalock, iflag);
2812 return rspiocbp;
2813}
2814
2815/**
2816 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002817 * @phba: Pointer to HBA context object.
2818 * @pring: Pointer to driver SLI ring object.
2819 * @mask: Host attention register mask for this ring.
2820 *
James Smart3772a992009-05-22 14:50:54 -04002821 * This routine wraps the actual slow_ring event process routine from the
2822 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002823 **/
James Smart3772a992009-05-22 14:50:54 -04002824void
James Smart2e0fef82007-06-17 19:56:36 -05002825lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2826 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002827{
James Smart3772a992009-05-22 14:50:54 -04002828 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2829}
2830
2831/**
2832 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2833 * @phba: Pointer to HBA context object.
2834 * @pring: Pointer to driver SLI ring object.
2835 * @mask: Host attention register mask for this ring.
2836 *
2837 * This function is called from the worker thread when there is a ring event
2838 * for non-fcp rings. The caller does not hold any lock. The function will
2839 * remove each response iocb in the response ring and calls the handle
2840 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2841 **/
2842static void
2843lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2844 struct lpfc_sli_ring *pring, uint32_t mask)
2845{
James Smart34b02dc2008-08-24 21:49:55 -04002846 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002847 IOCB_t *entry;
2848 IOCB_t *irsp = NULL;
2849 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002850 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002851 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002852 uint32_t status;
dea31012005-04-17 16:05:31 -05002853
James Smart34b02dc2008-08-24 21:49:55 -04002854 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002856 pring->stats.iocb_event++;
2857
dea31012005-04-17 16:05:31 -05002858 /*
2859 * The next available response entry should never exceed the maximum
2860 * entries. If it does, treat it as an adapter hardware error.
2861 */
2862 portRspMax = pring->numRiocb;
2863 portRspPut = le32_to_cpu(pgp->rspPutInx);
2864 if (portRspPut >= portRspMax) {
2865 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002866 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002867 * rsp ring <portRspMax>
2868 */
James Smarted957682007-06-17 19:56:37 -05002869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002870 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002871 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002872 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002873
James Smart2e0fef82007-06-17 19:56:36 -05002874 phba->link_state = LPFC_HBA_ERROR;
2875 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002876
2877 phba->work_hs = HS_FFER3;
2878 lpfc_handle_eratt(phba);
2879
James Smart3772a992009-05-22 14:50:54 -04002880 return;
dea31012005-04-17 16:05:31 -05002881 }
2882
2883 rmb();
dea31012005-04-17 16:05:31 -05002884 while (pring->rspidx != portRspPut) {
2885 /*
2886 * Build a completion list and call the appropriate handler.
2887 * The process is to get the next available response iocb, get
2888 * a free iocb from the list, copy the response data into the
2889 * free iocb, insert to the continuation list, and update the
2890 * next response index to slim. This process makes response
2891 * iocb's in the ring available to DMA as fast as possible but
2892 * pays a penalty for a copy operation. Since the iocb is
2893 * only 32 bytes, this penalty is considered small relative to
2894 * the PCI reads for register values and a slim write. When
2895 * the ulpLe field is set, the entire Command has been
2896 * received.
2897 */
James Smarted957682007-06-17 19:56:37 -05002898 entry = lpfc_resp_iocb(phba, pring);
2899
James Smart858c9f62007-06-17 19:56:39 -05002900 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002901 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002902 if (rspiocbp == NULL) {
2903 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002904 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002905 break;
2906 }
2907
James Smarted957682007-06-17 19:56:37 -05002908 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2909 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002910 irsp = &rspiocbp->iocb;
2911
2912 if (++pring->rspidx >= portRspMax)
2913 pring->rspidx = 0;
2914
James Smarta58cbd52007-08-02 11:09:43 -04002915 if (pring->ringno == LPFC_ELS_RING) {
2916 lpfc_debugfs_slow_ring_trc(phba,
2917 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2918 *(((uint32_t *) irsp) + 4),
2919 *(((uint32_t *) irsp) + 6),
2920 *(((uint32_t *) irsp) + 7));
2921 }
2922
James Smarted957682007-06-17 19:56:37 -05002923 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002924
James Smart3772a992009-05-22 14:50:54 -04002925 spin_unlock_irqrestore(&phba->hbalock, iflag);
2926 /* Handle the response IOCB */
2927 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2928 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002929
2930 /*
2931 * If the port response put pointer has not been updated, sync
2932 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2933 * response put pointer.
2934 */
2935 if (pring->rspidx == portRspPut) {
2936 portRspPut = le32_to_cpu(pgp->rspPutInx);
2937 }
2938 } /* while (pring->rspidx != portRspPut) */
2939
James Smart92d7f7b2007-06-17 19:56:38 -05002940 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002941 /* At least one response entry has been freed */
2942 pring->stats.iocb_rsp_full++;
2943 /* SET RxRE_RSP in Chip Att register */
2944 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2945 writel(status, phba->CAregaddr);
2946 readl(phba->CAregaddr); /* flush */
2947 }
2948 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2949 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2950 pring->stats.iocb_cmd_empty++;
2951
2952 /* Force update of the local copy of cmdGetInx */
2953 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2954 lpfc_sli_resume_iocb(phba, pring);
2955
2956 if ((pring->lpfc_sli_cmd_available))
2957 (pring->lpfc_sli_cmd_available) (phba, pring);
2958
2959 }
2960
James Smart2e0fef82007-06-17 19:56:36 -05002961 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002962 return;
dea31012005-04-17 16:05:31 -05002963}
2964
James Smarte59058c2008-08-24 21:49:00 -04002965/**
James Smart4f774512009-05-22 14:52:35 -04002966 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2967 * @phba: Pointer to HBA context object.
2968 * @pring: Pointer to driver SLI ring object.
2969 * @mask: Host attention register mask for this ring.
2970 *
2971 * This function is called from the worker thread when there is a pending
2972 * ELS response iocb on the driver internal slow-path response iocb worker
2973 * queue. The caller does not hold any lock. The function will remove each
2974 * response iocb from the response worker queue and calls the handle
2975 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2976 **/
2977static void
2978lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2979 struct lpfc_sli_ring *pring, uint32_t mask)
2980{
2981 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002982 struct hbq_dmabuf *dmabuf;
2983 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002984 unsigned long iflag;
2985
James Smart45ed1192009-10-02 15:17:02 -04002986 spin_lock_irqsave(&phba->hbalock, iflag);
2987 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2988 spin_unlock_irqrestore(&phba->hbalock, iflag);
2989 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002990 /* Get the response iocb from the head of work queue */
2991 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002992 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002993 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002994 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002995
2996 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2997 case CQE_CODE_COMPL_WQE:
2998 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2999 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003000 /* Translate ELS WCQE to response IOCBQ */
3001 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3002 irspiocbq);
3003 if (irspiocbq)
3004 lpfc_sli_sp_handle_rspiocb(phba, pring,
3005 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003006 break;
3007 case CQE_CODE_RECEIVE:
3008 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3009 cq_event);
3010 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3011 break;
3012 default:
3013 break;
3014 }
James Smart4f774512009-05-22 14:52:35 -04003015 }
3016}
3017
3018/**
James Smart3621a712009-04-06 18:47:14 -04003019 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003020 * @phba: Pointer to HBA context object.
3021 * @pring: Pointer to driver SLI ring object.
3022 *
3023 * This function aborts all iocbs in the given ring and frees all the iocb
3024 * objects in txq. This function issues an abort iocb for all the iocb commands
3025 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3026 * the return of this function. The caller is not required to hold any locks.
3027 **/
James Smart2e0fef82007-06-17 19:56:36 -05003028void
dea31012005-04-17 16:05:31 -05003029lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3030{
James Smart2534ba72007-04-25 09:52:20 -04003031 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003032 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003033
James Smart92d7f7b2007-06-17 19:56:38 -05003034 if (pring->ringno == LPFC_ELS_RING) {
3035 lpfc_fabric_abort_hba(phba);
3036 }
3037
dea31012005-04-17 16:05:31 -05003038 /* Error everything on txq and txcmplq
3039 * First do the txq.
3040 */
James Smart2e0fef82007-06-17 19:56:36 -05003041 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003042 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003043 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003044
3045 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003046 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3047 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3048
James Smart2e0fef82007-06-17 19:56:36 -05003049 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003050
James Smarta257bf92009-04-06 18:48:10 -04003051 /* Cancel all the IOCBs from the completions list */
3052 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3053 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003054}
3055
James Smarte59058c2008-08-24 21:49:00 -04003056/**
James Smart3621a712009-04-06 18:47:14 -04003057 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003058 * @phba: Pointer to HBA context object.
3059 *
3060 * This function flushes all iocbs in the fcp ring and frees all the iocb
3061 * objects in txq and txcmplq. This function will not issue abort iocbs
3062 * for all the iocb commands in txcmplq, they will just be returned with
3063 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3064 * slot has been permanently disabled.
3065 **/
3066void
3067lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3068{
3069 LIST_HEAD(txq);
3070 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003071 struct lpfc_sli *psli = &phba->sli;
3072 struct lpfc_sli_ring *pring;
3073
3074 /* Currently, only one fcp ring */
3075 pring = &psli->ring[psli->fcp_ring];
3076
3077 spin_lock_irq(&phba->hbalock);
3078 /* Retrieve everything on txq */
3079 list_splice_init(&pring->txq, &txq);
3080 pring->txq_cnt = 0;
3081
3082 /* Retrieve everything on the txcmplq */
3083 list_splice_init(&pring->txcmplq, &txcmplq);
3084 pring->txcmplq_cnt = 0;
3085 spin_unlock_irq(&phba->hbalock);
3086
3087 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003088 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3089 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003090
3091 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003092 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3093 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003094}
3095
3096/**
James Smart3772a992009-05-22 14:50:54 -04003097 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003098 * @phba: Pointer to HBA context object.
3099 * @mask: Bit mask to be checked.
3100 *
3101 * This function reads the host status register and compares
3102 * with the provided bit mask to check if HBA completed
3103 * the restart. This function will wait in a loop for the
3104 * HBA to complete restart. If the HBA does not restart within
3105 * 15 iterations, the function will reset the HBA again. The
3106 * function returns 1 when HBA fail to restart otherwise returns
3107 * zero.
3108 **/
James Smart3772a992009-05-22 14:50:54 -04003109static int
3110lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003111{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003112 uint32_t status;
3113 int i = 0;
3114 int retval = 0;
dea31012005-04-17 16:05:31 -05003115
Jamie Wellnitz41415862006-02-28 19:25:27 -05003116 /* Read the HBA Host Status Register */
3117 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003118
Jamie Wellnitz41415862006-02-28 19:25:27 -05003119 /*
3120 * Check status register every 100ms for 5 retries, then every
3121 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3122 * every 2.5 sec for 4.
3123 * Break our of the loop if errors occurred during init.
3124 */
3125 while (((status & mask) != mask) &&
3126 !(status & HS_FFERM) &&
3127 i++ < 20) {
dea31012005-04-17 16:05:31 -05003128
Jamie Wellnitz41415862006-02-28 19:25:27 -05003129 if (i <= 5)
3130 msleep(10);
3131 else if (i <= 10)
3132 msleep(500);
3133 else
3134 msleep(2500);
dea31012005-04-17 16:05:31 -05003135
Jamie Wellnitz41415862006-02-28 19:25:27 -05003136 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003137 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003138 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003139 lpfc_sli_brdrestart(phba);
3140 }
3141 /* Read the HBA Host Status Register */
3142 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003143 }
dea31012005-04-17 16:05:31 -05003144
Jamie Wellnitz41415862006-02-28 19:25:27 -05003145 /* Check to see if any errors occurred during init */
3146 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3148 "2751 Adapter failed to restart, "
3149 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3150 status,
3151 readl(phba->MBslimaddr + 0xa8),
3152 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003153 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003154 retval = 1;
3155 }
dea31012005-04-17 16:05:31 -05003156
Jamie Wellnitz41415862006-02-28 19:25:27 -05003157 return retval;
dea31012005-04-17 16:05:31 -05003158}
3159
James Smartda0436e2009-05-22 14:51:39 -04003160/**
3161 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3162 * @phba: Pointer to HBA context object.
3163 * @mask: Bit mask to be checked.
3164 *
3165 * This function checks the host status register to check if HBA is
3166 * ready. This function will wait in a loop for the HBA to be ready
3167 * If the HBA is not ready , the function will will reset the HBA PCI
3168 * function again. The function returns 1 when HBA fail to be ready
3169 * otherwise returns zero.
3170 **/
3171static int
3172lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3173{
3174 uint32_t status;
3175 int retval = 0;
3176
3177 /* Read the HBA Host Status Register */
3178 status = lpfc_sli4_post_status_check(phba);
3179
3180 if (status) {
3181 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3182 lpfc_sli_brdrestart(phba);
3183 status = lpfc_sli4_post_status_check(phba);
3184 }
3185
3186 /* Check to see if any errors occurred during init */
3187 if (status) {
3188 phba->link_state = LPFC_HBA_ERROR;
3189 retval = 1;
3190 } else
3191 phba->sli4_hba.intr_enable = 0;
3192
3193 return retval;
3194}
3195
3196/**
3197 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3198 * @phba: Pointer to HBA context object.
3199 * @mask: Bit mask to be checked.
3200 *
3201 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3202 * from the API jump table function pointer from the lpfc_hba struct.
3203 **/
3204int
3205lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3206{
3207 return phba->lpfc_sli_brdready(phba, mask);
3208}
3209
James Smart92908312006-03-07 15:04:13 -05003210#define BARRIER_TEST_PATTERN (0xdeadbeef)
3211
James Smarte59058c2008-08-24 21:49:00 -04003212/**
James Smart3621a712009-04-06 18:47:14 -04003213 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003214 * @phba: Pointer to HBA context object.
3215 *
3216 * This function is called before resetting an HBA. This
3217 * function requests HBA to quiesce DMAs before a reset.
3218 **/
James Smart2e0fef82007-06-17 19:56:36 -05003219void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003220{
James Smart65a29c12006-07-06 15:50:50 -04003221 uint32_t __iomem *resp_buf;
3222 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003223 volatile uint32_t mbox;
3224 uint32_t hc_copy;
3225 int i;
3226 uint8_t hdrtype;
3227
3228 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3229 if (hdrtype != 0x80 ||
3230 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3231 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3232 return;
3233
3234 /*
3235 * Tell the other part of the chip to suspend temporarily all
3236 * its DMA activity.
3237 */
James Smart65a29c12006-07-06 15:50:50 -04003238 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003239
3240 /* Disable the error attention */
3241 hc_copy = readl(phba->HCregaddr);
3242 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3243 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003244 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003245
3246 if (readl(phba->HAregaddr) & HA_ERATT) {
3247 /* Clear Chip error bit */
3248 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003249 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003250 }
3251
3252 mbox = 0;
3253 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3254 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3255
3256 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003257 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003258 writel(mbox, mbox_buf);
3259
3260 for (i = 0;
3261 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3262 mdelay(1);
3263
3264 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003265 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003266 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003267 goto restore_hc;
3268 else
3269 goto clear_errat;
3270 }
3271
3272 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3273 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3274 mdelay(1);
3275
3276clear_errat:
3277
3278 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3279 mdelay(1);
3280
3281 if (readl(phba->HAregaddr) & HA_ERATT) {
3282 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003283 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003284 }
3285
3286restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003287 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003288 writel(hc_copy, phba->HCregaddr);
3289 readl(phba->HCregaddr); /* flush */
3290}
3291
James Smarte59058c2008-08-24 21:49:00 -04003292/**
James Smart3621a712009-04-06 18:47:14 -04003293 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003294 * @phba: Pointer to HBA context object.
3295 *
3296 * This function issues a kill_board mailbox command and waits for
3297 * the error attention interrupt. This function is called for stopping
3298 * the firmware processing. The caller is not required to hold any
3299 * locks. This function calls lpfc_hba_down_post function to free
3300 * any pending commands after the kill. The function will return 1 when it
3301 * fails to kill the board else will return 0.
3302 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003303int
James Smart2e0fef82007-06-17 19:56:36 -05003304lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003305{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003306 struct lpfc_sli *psli;
3307 LPFC_MBOXQ_t *pmb;
3308 uint32_t status;
3309 uint32_t ha_copy;
3310 int retval;
3311 int i = 0;
3312
3313 psli = &phba->sli;
3314
3315 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003316 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003317 "0329 Kill HBA Data: x%x x%x\n",
3318 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003319
James Smart98c9ea52007-10-27 13:37:33 -04003320 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3321 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003322 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003323
3324 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003326 status = readl(phba->HCregaddr);
3327 status &= ~HC_ERINT_ENA;
3328 writel(status, phba->HCregaddr);
3329 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003330 phba->link_flag |= LS_IGNORE_ERATT;
3331 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003332
3333 lpfc_kill_board(phba, pmb);
3334 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3335 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3336
3337 if (retval != MBX_SUCCESS) {
3338 if (retval != MBX_BUSY)
3339 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 "2752 KILL_BOARD command failed retval %d\n",
3342 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003343 spin_lock_irq(&phba->hbalock);
3344 phba->link_flag &= ~LS_IGNORE_ERATT;
3345 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003346 return 1;
3347 }
3348
James Smartf4b4c682009-05-22 14:53:12 -04003349 spin_lock_irq(&phba->hbalock);
3350 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3351 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003352
Jamie Wellnitz41415862006-02-28 19:25:27 -05003353 mempool_free(pmb, phba->mbox_mem_pool);
3354
3355 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3356 * attention every 100ms for 3 seconds. If we don't get ERATT after
3357 * 3 seconds we still set HBA_ERROR state because the status of the
3358 * board is now undefined.
3359 */
3360 ha_copy = readl(phba->HAregaddr);
3361
3362 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3363 mdelay(100);
3364 ha_copy = readl(phba->HAregaddr);
3365 }
3366
3367 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003368 if (ha_copy & HA_ERATT) {
3369 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003370 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003371 }
James Smart2e0fef82007-06-17 19:56:36 -05003372 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003373 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003374 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003375 phba->link_flag &= ~LS_IGNORE_ERATT;
3376 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003377
Jamie Wellnitz41415862006-02-28 19:25:27 -05003378 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003379 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003380
James Smart2e0fef82007-06-17 19:56:36 -05003381 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003382}
3383
James Smarte59058c2008-08-24 21:49:00 -04003384/**
James Smart3772a992009-05-22 14:50:54 -04003385 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003386 * @phba: Pointer to HBA context object.
3387 *
3388 * This function resets the HBA by writing HC_INITFF to the control
3389 * register. After the HBA resets, this function resets all the iocb ring
3390 * indices. This function disables PCI layer parity checking during
3391 * the reset.
3392 * This function returns 0 always.
3393 * The caller is not required to hold any locks.
3394 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003395int
James Smart2e0fef82007-06-17 19:56:36 -05003396lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003397{
3398 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003399 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003400 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003401 int i;
dea31012005-04-17 16:05:31 -05003402
Jamie Wellnitz41415862006-02-28 19:25:27 -05003403 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003404
Jamie Wellnitz41415862006-02-28 19:25:27 -05003405 /* Reset HBA */
3406 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003407 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003408 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003409
3410 /* perform board reset */
3411 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003412 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003413 phba->pport->fc_myDID = 0;
3414 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003415
Jamie Wellnitz41415862006-02-28 19:25:27 -05003416 /* Turn off parity checking and serr during the physical reset */
3417 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3418 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3419 (cfg_value &
3420 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3421
James Smart3772a992009-05-22 14:50:54 -04003422 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3423
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424 /* Now toggle INITFF bit in the Host Control Register */
3425 writel(HC_INITFF, phba->HCregaddr);
3426 mdelay(1);
3427 readl(phba->HCregaddr); /* flush */
3428 writel(0, phba->HCregaddr);
3429 readl(phba->HCregaddr); /* flush */
3430
3431 /* Restore PCI cmd register */
3432 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003433
3434 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003435 for (i = 0; i < psli->num_rings; i++) {
3436 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003437 pring->flag = 0;
3438 pring->rspidx = 0;
3439 pring->next_cmdidx = 0;
3440 pring->local_getidx = 0;
3441 pring->cmdidx = 0;
3442 pring->missbufcnt = 0;
3443 }
dea31012005-04-17 16:05:31 -05003444
James Smart2e0fef82007-06-17 19:56:36 -05003445 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003446 return 0;
3447}
3448
James Smarte59058c2008-08-24 21:49:00 -04003449/**
James Smartda0436e2009-05-22 14:51:39 -04003450 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3451 * @phba: Pointer to HBA context object.
3452 *
3453 * This function resets a SLI4 HBA. This function disables PCI layer parity
3454 * checking during resets the device. The caller is not required to hold
3455 * any locks.
3456 *
3457 * This function returns 0 always.
3458 **/
3459int
3460lpfc_sli4_brdreset(struct lpfc_hba *phba)
3461{
3462 struct lpfc_sli *psli = &phba->sli;
3463 uint16_t cfg_value;
3464 uint8_t qindx;
3465
3466 /* Reset HBA */
3467 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3468 "0295 Reset HBA Data: x%x x%x\n",
3469 phba->pport->port_state, psli->sli_flag);
3470
3471 /* perform board reset */
3472 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003473 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003474 phba->pport->fc_myDID = 0;
3475 phba->pport->fc_prevDID = 0;
3476
3477 /* Turn off parity checking and serr during the physical reset */
3478 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3479 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3480 (cfg_value &
3481 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3482
3483 spin_lock_irq(&phba->hbalock);
3484 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3485 phba->fcf.fcf_flag = 0;
3486 /* Clean up the child queue list for the CQs */
3487 list_del_init(&phba->sli4_hba.mbx_wq->list);
3488 list_del_init(&phba->sli4_hba.els_wq->list);
3489 list_del_init(&phba->sli4_hba.hdr_rq->list);
3490 list_del_init(&phba->sli4_hba.dat_rq->list);
3491 list_del_init(&phba->sli4_hba.mbx_cq->list);
3492 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003493 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3494 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3495 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3496 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3497 spin_unlock_irq(&phba->hbalock);
3498
3499 /* Now physically reset the device */
3500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3501 "0389 Performing PCI function reset!\n");
3502 /* Perform FCoE PCI function reset */
3503 lpfc_pci_function_reset(phba);
3504
3505 return 0;
3506}
3507
3508/**
3509 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003510 * @phba: Pointer to HBA context object.
3511 *
3512 * This function is called in the SLI initialization code path to
3513 * restart the HBA. The caller is not required to hold any lock.
3514 * This function writes MBX_RESTART mailbox command to the SLIM and
3515 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3516 * function to free any pending commands. The function enables
3517 * POST only during the first initialization. The function returns zero.
3518 * The function does not guarantee completion of MBX_RESTART mailbox
3519 * command before the return of this function.
3520 **/
James Smartda0436e2009-05-22 14:51:39 -04003521static int
3522lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003523{
3524 MAILBOX_t *mb;
3525 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003526 volatile uint32_t word0;
3527 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003528 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003529
James Smart2e0fef82007-06-17 19:56:36 -05003530 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003531
James Smart0d878412009-10-02 15:16:56 -04003532 /* Take PCIe device Advanced Error Reporting (AER) state */
3533 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3534
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535 psli = &phba->sli;
3536
3537 /* Restart HBA */
3538 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003539 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003540 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003541
3542 word0 = 0;
3543 mb = (MAILBOX_t *) &word0;
3544 mb->mbxCommand = MBX_RESTART;
3545 mb->mbxHc = 1;
3546
James Smart92908312006-03-07 15:04:13 -05003547 lpfc_reset_barrier(phba);
3548
Jamie Wellnitz41415862006-02-28 19:25:27 -05003549 to_slim = phba->MBslimaddr;
3550 writel(*(uint32_t *) mb, to_slim);
3551 readl(to_slim); /* flush */
3552
3553 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003554 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003555 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003556 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003558 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003559 writel(*(uint32_t *) mb, to_slim);
3560 readl(to_slim); /* flush */
3561
3562 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003563 phba->pport->stopped = 0;
3564 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003565 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003566 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003567
James Smart64ba8812006-08-02 15:24:34 -04003568 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3569 psli->stats_start = get_seconds();
3570
James Smarteaf15d52008-12-04 22:39:29 -05003571 /* Give the INITFF and Post time to settle. */
3572 mdelay(100);
dea31012005-04-17 16:05:31 -05003573
James Smart0d878412009-10-02 15:16:56 -04003574 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3575 if (hba_aer_enabled)
3576 pci_disable_pcie_error_reporting(phba->pcidev);
3577
Jamie Wellnitz41415862006-02-28 19:25:27 -05003578 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003579
3580 return 0;
3581}
3582
James Smarte59058c2008-08-24 21:49:00 -04003583/**
James Smartda0436e2009-05-22 14:51:39 -04003584 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3585 * @phba: Pointer to HBA context object.
3586 *
3587 * This function is called in the SLI initialization code path to restart
3588 * a SLI4 HBA. The caller is not required to hold any lock.
3589 * At the end of the function, it calls lpfc_hba_down_post function to
3590 * free any pending commands.
3591 **/
3592static int
3593lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3594{
3595 struct lpfc_sli *psli = &phba->sli;
3596
3597
3598 /* Restart HBA */
3599 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3600 "0296 Restart HBA Data: x%x x%x\n",
3601 phba->pport->port_state, psli->sli_flag);
3602
3603 lpfc_sli4_brdreset(phba);
3604
3605 spin_lock_irq(&phba->hbalock);
3606 phba->pport->stopped = 0;
3607 phba->link_state = LPFC_INIT_START;
3608 phba->hba_flag = 0;
3609 spin_unlock_irq(&phba->hbalock);
3610
3611 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3612 psli->stats_start = get_seconds();
3613
3614 lpfc_hba_down_post(phba);
3615
3616 return 0;
3617}
3618
3619/**
3620 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3621 * @phba: Pointer to HBA context object.
3622 *
3623 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3624 * API jump table function pointer from the lpfc_hba struct.
3625**/
3626int
3627lpfc_sli_brdrestart(struct lpfc_hba *phba)
3628{
3629 return phba->lpfc_sli_brdrestart(phba);
3630}
3631
3632/**
James Smart3621a712009-04-06 18:47:14 -04003633 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003634 * @phba: Pointer to HBA context object.
3635 *
3636 * This function is called after a HBA restart to wait for successful
3637 * restart of the HBA. Successful restart of the HBA is indicated by
3638 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3639 * iteration, the function will restart the HBA again. The function returns
3640 * zero if HBA successfully restarted else returns negative error code.
3641 **/
dea31012005-04-17 16:05:31 -05003642static int
3643lpfc_sli_chipset_init(struct lpfc_hba *phba)
3644{
3645 uint32_t status, i = 0;
3646
3647 /* Read the HBA Host Status Register */
3648 status = readl(phba->HSregaddr);
3649
3650 /* Check status register to see what current state is */
3651 i = 0;
3652 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3653
3654 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3655 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3656 * 4.
3657 */
3658 if (i++ >= 20) {
3659 /* Adapter failed to init, timeout, status reg
3660 <status> */
James Smarted957682007-06-17 19:56:37 -05003661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003662 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003663 "timeout, status reg x%x, "
3664 "FW Data: A8 x%x AC x%x\n", status,
3665 readl(phba->MBslimaddr + 0xa8),
3666 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003667 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003668 return -ETIMEDOUT;
3669 }
3670
3671 /* Check to see if any errors occurred during init */
3672 if (status & HS_FFERM) {
3673 /* ERROR: During chipset initialization */
3674 /* Adapter failed to init, chipset, status reg
3675 <status> */
James Smarted957682007-06-17 19:56:37 -05003676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003677 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003678 "chipset, status reg x%x, "
3679 "FW Data: A8 x%x AC x%x\n", status,
3680 readl(phba->MBslimaddr + 0xa8),
3681 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003682 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003683 return -EIO;
3684 }
3685
3686 if (i <= 5) {
3687 msleep(10);
3688 } else if (i <= 10) {
3689 msleep(500);
3690 } else {
3691 msleep(2500);
3692 }
3693
3694 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003695 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003696 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003697 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003698 }
3699 /* Read the HBA Host Status Register */
3700 status = readl(phba->HSregaddr);
3701 }
3702
3703 /* Check to see if any errors occurred during init */
3704 if (status & HS_FFERM) {
3705 /* ERROR: During chipset initialization */
3706 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003708 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003709 "status reg x%x, "
3710 "FW Data: A8 x%x AC x%x\n", status,
3711 readl(phba->MBslimaddr + 0xa8),
3712 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003713 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003714 return -EIO;
3715 }
3716
3717 /* Clear all interrupt enable conditions */
3718 writel(0, phba->HCregaddr);
3719 readl(phba->HCregaddr); /* flush */
3720
3721 /* setup host attn register */
3722 writel(0xffffffff, phba->HAregaddr);
3723 readl(phba->HAregaddr); /* flush */
3724 return 0;
3725}
3726
James Smarte59058c2008-08-24 21:49:00 -04003727/**
James Smart3621a712009-04-06 18:47:14 -04003728 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003729 *
3730 * This function calculates and returns the number of HBQs required to be
3731 * configured.
3732 **/
James Smart78b2d852007-08-02 11:10:21 -04003733int
James Smarted957682007-06-17 19:56:37 -05003734lpfc_sli_hbq_count(void)
3735{
James Smart92d7f7b2007-06-17 19:56:38 -05003736 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003737}
3738
James Smarte59058c2008-08-24 21:49:00 -04003739/**
James Smart3621a712009-04-06 18:47:14 -04003740 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003741 *
3742 * This function adds the number of hbq entries in every HBQ to get
3743 * the total number of hbq entries required for the HBA and returns
3744 * the total count.
3745 **/
James Smarted957682007-06-17 19:56:37 -05003746static int
3747lpfc_sli_hbq_entry_count(void)
3748{
3749 int hbq_count = lpfc_sli_hbq_count();
3750 int count = 0;
3751 int i;
3752
3753 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003754 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003755 return count;
3756}
3757
James Smarte59058c2008-08-24 21:49:00 -04003758/**
James Smart3621a712009-04-06 18:47:14 -04003759 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003760 *
3761 * This function calculates amount of memory required for all hbq entries
3762 * to be configured and returns the total memory required.
3763 **/
dea31012005-04-17 16:05:31 -05003764int
James Smarted957682007-06-17 19:56:37 -05003765lpfc_sli_hbq_size(void)
3766{
3767 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3768}
3769
James Smarte59058c2008-08-24 21:49:00 -04003770/**
James Smart3621a712009-04-06 18:47:14 -04003771 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003772 * @phba: Pointer to HBA context object.
3773 *
3774 * This function is called during the SLI initialization to configure
3775 * all the HBQs and post buffers to the HBQ. The caller is not
3776 * required to hold any locks. This function will return zero if successful
3777 * else it will return negative error code.
3778 **/
James Smarted957682007-06-17 19:56:37 -05003779static int
3780lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3781{
3782 int hbq_count = lpfc_sli_hbq_count();
3783 LPFC_MBOXQ_t *pmb;
3784 MAILBOX_t *pmbox;
3785 uint32_t hbqno;
3786 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003787
James Smart92d7f7b2007-06-17 19:56:38 -05003788 /* Get a Mailbox buffer to setup mailbox
3789 * commands for HBA initialization
3790 */
James Smarted957682007-06-17 19:56:37 -05003791 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3792
3793 if (!pmb)
3794 return -ENOMEM;
3795
James Smart04c68492009-05-22 14:52:52 -04003796 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003797
3798 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3799 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003800 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003801
3802 hbq_entry_index = 0;
3803 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3804 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3805 phba->hbqs[hbqno].hbqPutIdx = 0;
3806 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3807 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003808 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003809 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3810 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003811 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3812
3813 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3814 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3815 mbxStatus <status>, ring <num> */
3816
3817 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003818 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003819 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003820 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003821 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003822 pmbox->mbxStatus, hbqno);
3823
3824 phba->link_state = LPFC_HBA_ERROR;
3825 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04003826 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05003827 }
3828 }
3829 phba->hbq_count = hbq_count;
3830
James Smarted957682007-06-17 19:56:37 -05003831 mempool_free(pmb, phba->mbox_mem_pool);
3832
James Smart92d7f7b2007-06-17 19:56:38 -05003833 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003834 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3835 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003836 return 0;
3837}
3838
James Smarte59058c2008-08-24 21:49:00 -04003839/**
James Smart4f774512009-05-22 14:52:35 -04003840 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3841 * @phba: Pointer to HBA context object.
3842 *
3843 * This function is called during the SLI initialization to configure
3844 * all the HBQs and post buffers to the HBQ. The caller is not
3845 * required to hold any locks. This function will return zero if successful
3846 * else it will return negative error code.
3847 **/
3848static int
3849lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3850{
3851 phba->hbq_in_use = 1;
3852 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3853 phba->hbq_count = 1;
3854 /* Initially populate or replenish the HBQs */
3855 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3856 return 0;
3857}
3858
3859/**
James Smart3621a712009-04-06 18:47:14 -04003860 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003861 * @phba: Pointer to HBA context object.
3862 * @sli_mode: sli mode - 2/3
3863 *
3864 * This function is called by the sli intialization code path
3865 * to issue config_port mailbox command. This function restarts the
3866 * HBA firmware and issues a config_port mailbox command to configure
3867 * the SLI interface in the sli mode specified by sli_mode
3868 * variable. The caller is not required to hold any locks.
3869 * The function returns 0 if successful, else returns negative error
3870 * code.
3871 **/
James Smart93996272008-08-24 21:50:30 -04003872int
3873lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003874{
3875 LPFC_MBOXQ_t *pmb;
3876 uint32_t resetcount = 0, rc = 0, done = 0;
3877
3878 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3879 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003881 return -ENOMEM;
3882 }
3883
James Smarted957682007-06-17 19:56:37 -05003884 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003885 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003886 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003887 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003888 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003889 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003890 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003891 rc = lpfc_sli_chipset_init(phba);
3892 if (rc)
3893 break;
3894
James Smart2e0fef82007-06-17 19:56:36 -05003895 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003896 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003897 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003898 resetcount++;
3899
James Smarted957682007-06-17 19:56:37 -05003900 /* Call pre CONFIG_PORT mailbox command initialization. A
3901 * value of 0 means the call was successful. Any other
3902 * nonzero value is a failure, but if ERESTART is returned,
3903 * the driver may reset the HBA and try again.
3904 */
dea31012005-04-17 16:05:31 -05003905 rc = lpfc_config_port_prep(phba);
3906 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003907 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003908 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003909 } else if (rc)
dea31012005-04-17 16:05:31 -05003910 break;
James Smart2e0fef82007-06-17 19:56:36 -05003911 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003912 lpfc_config_port(phba, pmb);
3913 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003914 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3915 LPFC_SLI3_HBQ_ENABLED |
3916 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003917 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003918 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003920 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003921 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003922 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003923 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003924 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003925 spin_unlock_irq(&phba->hbalock);
3926 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003927 } else {
3928 /* Allow asynchronous mailbox command to go through */
3929 spin_lock_irq(&phba->hbalock);
3930 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3931 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003932 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003933 }
dea31012005-04-17 16:05:31 -05003934 }
James Smarted957682007-06-17 19:56:37 -05003935 if (!done) {
3936 rc = -EINVAL;
3937 goto do_prep_failed;
3938 }
James Smart04c68492009-05-22 14:52:52 -04003939 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3940 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003941 rc = -ENXIO;
3942 goto do_prep_failed;
3943 }
James Smart04c68492009-05-22 14:52:52 -04003944 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003945 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003946 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3947 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3948 phba->max_vpi : phba->max_vports;
3949
James Smart34b02dc2008-08-24 21:49:55 -04003950 } else
3951 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003952 if (pmb->u.mb.un.varCfgPort.gdss)
3953 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3954 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003955 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003956 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003957 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04003958
3959 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3960 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05003961
3962 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003963 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003964 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3965 else
3966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3967 "0443 Adapter did not grant "
3968 "BlockGuard\n");
3969 }
James Smart34b02dc2008-08-24 21:49:55 -04003970 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003971 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003972 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04003973 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003974 }
James Smart92d7f7b2007-06-17 19:56:38 -05003975do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003976 mempool_free(pmb, phba->mbox_mem_pool);
3977 return rc;
3978}
3979
James Smarte59058c2008-08-24 21:49:00 -04003980
3981/**
James Smart3621a712009-04-06 18:47:14 -04003982 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003983 * @phba: Pointer to HBA context object.
3984 *
3985 * This function is the main SLI intialization function. This function
3986 * is called by the HBA intialization code, HBA reset code and HBA
3987 * error attention handler code. Caller is not required to hold any
3988 * locks. This function issues config_port mailbox command to configure
3989 * the SLI, setup iocb rings and HBQ rings. In the end the function
3990 * calls the config_port_post function to issue init_link mailbox
3991 * command and to start the discovery. The function will return zero
3992 * if successful, else it will return negative error code.
3993 **/
James Smarted957682007-06-17 19:56:37 -05003994int
3995lpfc_sli_hba_setup(struct lpfc_hba *phba)
3996{
3997 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003998 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003999
4000 switch (lpfc_sli_mode) {
4001 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004002 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004004 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004005 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004006 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004007 break;
4008 }
James Smarted957682007-06-17 19:56:37 -05004009 mode = 2;
4010 break;
4011 case 0:
4012 case 3:
4013 break;
4014 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004016 "1819 Unrecognized lpfc_sli_mode "
4017 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004018
4019 break;
4020 }
4021
James Smart93996272008-08-24 21:50:30 -04004022 rc = lpfc_sli_config_port(phba, mode);
4023
James Smarted957682007-06-17 19:56:37 -05004024 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004026 "1820 Unable to select SLI-3. "
4027 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004028 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004029 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004030 if (rc)
dea31012005-04-17 16:05:31 -05004031 goto lpfc_sli_hba_setup_error;
4032
James Smart0d878412009-10-02 15:16:56 -04004033 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4034 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4035 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4036 if (!rc) {
4037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4038 "2709 This device supports "
4039 "Advanced Error Reporting (AER)\n");
4040 spin_lock_irq(&phba->hbalock);
4041 phba->hba_flag |= HBA_AER_ENABLED;
4042 spin_unlock_irq(&phba->hbalock);
4043 } else {
4044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4045 "2708 This device does not support "
4046 "Advanced Error Reporting (AER)\n");
4047 phba->cfg_aer_support = 0;
4048 }
4049 }
4050
James Smarted957682007-06-17 19:56:37 -05004051 if (phba->sli_rev == 3) {
4052 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4053 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004054 } else {
4055 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4056 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004057 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004058 }
4059
4060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004061 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4062 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004063 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004064
4065 if (rc)
4066 goto lpfc_sli_hba_setup_error;
4067
James Smart93996272008-08-24 21:50:30 -04004068 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004069 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4070 rc = lpfc_sli_hbq_setup(phba);
4071 if (rc)
4072 goto lpfc_sli_hba_setup_error;
4073 }
James Smart04c68492009-05-22 14:52:52 -04004074 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004075 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004076 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004077
4078 rc = lpfc_config_port_post(phba);
4079 if (rc)
4080 goto lpfc_sli_hba_setup_error;
4081
James Smarted957682007-06-17 19:56:37 -05004082 return rc;
4083
James Smart92d7f7b2007-06-17 19:56:38 -05004084lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004085 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004087 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004088 return rc;
4089}
4090
James Smartda0436e2009-05-22 14:51:39 -04004091/**
4092 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4093 * @phba: Pointer to HBA context object.
4094 * @mboxq: mailbox pointer.
4095 * This function issue a dump mailbox command to read config region
4096 * 23 and parse the records in the region and populate driver
4097 * data structure.
4098 **/
4099static int
4100lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4101 LPFC_MBOXQ_t *mboxq)
4102{
4103 struct lpfc_dmabuf *mp;
4104 struct lpfc_mqe *mqe;
4105 uint32_t data_length;
4106 int rc;
4107
4108 /* Program the default value of vlan_id and fc_map */
4109 phba->valid_vlan = 0;
4110 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4111 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4112 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4113
4114 mqe = &mboxq->u.mqe;
4115 if (lpfc_dump_fcoe_param(phba, mboxq))
4116 return -ENOMEM;
4117
4118 mp = (struct lpfc_dmabuf *) mboxq->context1;
4119 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4120
4121 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4122 "(%d):2571 Mailbox cmd x%x Status x%x "
4123 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4124 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4125 "CQ: x%x x%x x%x x%x\n",
4126 mboxq->vport ? mboxq->vport->vpi : 0,
4127 bf_get(lpfc_mqe_command, mqe),
4128 bf_get(lpfc_mqe_status, mqe),
4129 mqe->un.mb_words[0], mqe->un.mb_words[1],
4130 mqe->un.mb_words[2], mqe->un.mb_words[3],
4131 mqe->un.mb_words[4], mqe->un.mb_words[5],
4132 mqe->un.mb_words[6], mqe->un.mb_words[7],
4133 mqe->un.mb_words[8], mqe->un.mb_words[9],
4134 mqe->un.mb_words[10], mqe->un.mb_words[11],
4135 mqe->un.mb_words[12], mqe->un.mb_words[13],
4136 mqe->un.mb_words[14], mqe->un.mb_words[15],
4137 mqe->un.mb_words[16], mqe->un.mb_words[50],
4138 mboxq->mcqe.word0,
4139 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4140 mboxq->mcqe.trailer);
4141
4142 if (rc) {
4143 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4144 kfree(mp);
4145 return -EIO;
4146 }
4147 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004148 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004149 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4150 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004151 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004152 }
James Smartda0436e2009-05-22 14:51:39 -04004153
4154 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4155 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4156 kfree(mp);
4157 return 0;
4158}
4159
4160/**
4161 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4162 * @phba: pointer to lpfc hba data structure.
4163 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4164 * @vpd: pointer to the memory to hold resulting port vpd data.
4165 * @vpd_size: On input, the number of bytes allocated to @vpd.
4166 * On output, the number of data bytes in @vpd.
4167 *
4168 * This routine executes a READ_REV SLI4 mailbox command. In
4169 * addition, this routine gets the port vpd data.
4170 *
4171 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004172 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004173 * ENOMEM - could not allocated memory.
4174 **/
4175static int
4176lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4177 uint8_t *vpd, uint32_t *vpd_size)
4178{
4179 int rc = 0;
4180 uint32_t dma_size;
4181 struct lpfc_dmabuf *dmabuf;
4182 struct lpfc_mqe *mqe;
4183
4184 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4185 if (!dmabuf)
4186 return -ENOMEM;
4187
4188 /*
4189 * Get a DMA buffer for the vpd data resulting from the READ_REV
4190 * mailbox command.
4191 */
4192 dma_size = *vpd_size;
4193 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4194 dma_size,
4195 &dmabuf->phys,
4196 GFP_KERNEL);
4197 if (!dmabuf->virt) {
4198 kfree(dmabuf);
4199 return -ENOMEM;
4200 }
4201 memset(dmabuf->virt, 0, dma_size);
4202
4203 /*
4204 * The SLI4 implementation of READ_REV conflicts at word1,
4205 * bits 31:16 and SLI4 adds vpd functionality not present
4206 * in SLI3. This code corrects the conflicts.
4207 */
4208 lpfc_read_rev(phba, mboxq);
4209 mqe = &mboxq->u.mqe;
4210 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4211 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4212 mqe->un.read_rev.word1 &= 0x0000FFFF;
4213 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4214 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4215
4216 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4217 if (rc) {
4218 dma_free_coherent(&phba->pcidev->dev, dma_size,
4219 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004220 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004221 return -EIO;
4222 }
4223
James Smartda0436e2009-05-22 14:51:39 -04004224 /*
4225 * The available vpd length cannot be bigger than the
4226 * DMA buffer passed to the port. Catch the less than
4227 * case and update the caller's size.
4228 */
4229 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4230 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4231
4232 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4233 dma_free_coherent(&phba->pcidev->dev, dma_size,
4234 dmabuf->virt, dmabuf->phys);
4235 kfree(dmabuf);
4236 return 0;
4237}
4238
4239/**
4240 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4241 * @phba: pointer to lpfc hba data structure.
4242 *
4243 * This routine is called to explicitly arm the SLI4 device's completion and
4244 * event queues
4245 **/
4246static void
4247lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4248{
4249 uint8_t fcp_eqidx;
4250
4251 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4252 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004253 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4254 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4255 LPFC_QUEUE_REARM);
4256 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4257 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4258 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4259 LPFC_QUEUE_REARM);
4260}
4261
4262/**
4263 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4264 * @phba: Pointer to HBA context object.
4265 *
4266 * This function is the main SLI4 device intialization PCI function. This
4267 * function is called by the HBA intialization code, HBA reset code and
4268 * HBA error attention handler code. Caller is not required to hold any
4269 * locks.
4270 **/
4271int
4272lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4273{
4274 int rc;
4275 LPFC_MBOXQ_t *mboxq;
4276 struct lpfc_mqe *mqe;
4277 uint8_t *vpd;
4278 uint32_t vpd_size;
4279 uint32_t ftr_rsp = 0;
4280 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4281 struct lpfc_vport *vport = phba->pport;
4282 struct lpfc_dmabuf *mp;
4283
4284 /* Perform a PCI function reset to start from clean */
4285 rc = lpfc_pci_function_reset(phba);
4286 if (unlikely(rc))
4287 return -ENODEV;
4288
4289 /* Check the HBA Host Status Register for readyness */
4290 rc = lpfc_sli4_post_status_check(phba);
4291 if (unlikely(rc))
4292 return -ENODEV;
4293 else {
4294 spin_lock_irq(&phba->hbalock);
4295 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4296 spin_unlock_irq(&phba->hbalock);
4297 }
4298
4299 /*
4300 * Allocate a single mailbox container for initializing the
4301 * port.
4302 */
4303 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4304 if (!mboxq)
4305 return -ENOMEM;
4306
4307 /*
4308 * Continue initialization with default values even if driver failed
4309 * to read FCoE param config regions
4310 */
4311 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004313 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004314
4315 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004316 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004317 vpd = kzalloc(vpd_size, GFP_KERNEL);
4318 if (!vpd) {
4319 rc = -ENOMEM;
4320 goto out_free_mbox;
4321 }
4322
4323 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4324 if (unlikely(rc))
4325 goto out_free_vpd;
4326
4327 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004328 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4329 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4330 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004331
4332 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4333 LPFC_DCBX_CEE_MODE)
4334 phba->hba_flag |= HBA_FIP_SUPPORT;
4335 else
4336 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4337
James Smartf1126682009-06-10 17:22:44 -04004338 if (phba->sli_rev != LPFC_SLI_REV4 ||
4339 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004340 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4341 "0376 READ_REV Error. SLI Level %d "
4342 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004343 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004344 rc = -EIO;
4345 goto out_free_vpd;
4346 }
James Smartda0436e2009-05-22 14:51:39 -04004347 /*
4348 * Evaluate the read rev and vpd data. Populate the driver
4349 * state with the results. If this routine fails, the failure
4350 * is not fatal as the driver will use generic values.
4351 */
4352 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4353 if (unlikely(!rc)) {
4354 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4355 "0377 Error %d parsing vpd. "
4356 "Using defaults.\n", rc);
4357 rc = 0;
4358 }
4359
James Smartf1126682009-06-10 17:22:44 -04004360 /* Save information as VPD data */
4361 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4362 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4363 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4364 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4365 &mqe->un.read_rev);
4366 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4367 &mqe->un.read_rev);
4368 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4369 &mqe->un.read_rev);
4370 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4371 &mqe->un.read_rev);
4372 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4373 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4374 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4375 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4376 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4377 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4378 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4379 "(%d):0380 READ_REV Status x%x "
4380 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4381 mboxq->vport ? mboxq->vport->vpi : 0,
4382 bf_get(lpfc_mqe_status, mqe),
4383 phba->vpd.rev.opFwName,
4384 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4385 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004386
4387 /*
4388 * Discover the port's supported feature set and match it against the
4389 * hosts requests.
4390 */
4391 lpfc_request_features(phba, mboxq);
4392 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4393 if (unlikely(rc)) {
4394 rc = -EIO;
4395 goto out_free_vpd;
4396 }
4397
4398 /*
4399 * The port must support FCP initiator mode as this is the
4400 * only mode running in the host.
4401 */
4402 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4403 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4404 "0378 No support for fcpi mode.\n");
4405 ftr_rsp++;
4406 }
4407
4408 /*
4409 * If the port cannot support the host's requested features
4410 * then turn off the global config parameters to disable the
4411 * feature in the driver. This is not a fatal error.
4412 */
4413 if ((phba->cfg_enable_bg) &&
4414 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4415 ftr_rsp++;
4416
4417 if (phba->max_vpi && phba->cfg_enable_npiv &&
4418 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4419 ftr_rsp++;
4420
4421 if (ftr_rsp) {
4422 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4423 "0379 Feature Mismatch Data: x%08x %08x "
4424 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4425 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4426 phba->cfg_enable_npiv, phba->max_vpi);
4427 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4428 phba->cfg_enable_bg = 0;
4429 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4430 phba->cfg_enable_npiv = 0;
4431 }
4432
4433 /* These SLI3 features are assumed in SLI4 */
4434 spin_lock_irq(&phba->hbalock);
4435 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4436 spin_unlock_irq(&phba->hbalock);
4437
4438 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004439 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4440 if (rc) {
4441 phba->link_state = LPFC_HBA_ERROR;
4442 rc = -ENOMEM;
4443 goto out_free_vpd;
4444 }
4445
James Smartda0436e2009-05-22 14:51:39 -04004446 mboxq->vport = vport;
4447 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4448 mp = (struct lpfc_dmabuf *) mboxq->context1;
4449 if (rc == MBX_SUCCESS) {
4450 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4451 rc = 0;
4452 }
4453
4454 /*
4455 * This memory was allocated by the lpfc_read_sparam routine. Release
4456 * it to the mbuf pool.
4457 */
4458 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4459 kfree(mp);
4460 mboxq->context1 = NULL;
4461 if (unlikely(rc)) {
4462 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4463 "0382 READ_SPARAM command failed "
4464 "status %d, mbxStatus x%x\n",
4465 rc, bf_get(lpfc_mqe_status, mqe));
4466 phba->link_state = LPFC_HBA_ERROR;
4467 rc = -EIO;
4468 goto out_free_vpd;
4469 }
4470
4471 if (phba->cfg_soft_wwnn)
4472 u64_to_wwn(phba->cfg_soft_wwnn,
4473 vport->fc_sparam.nodeName.u.wwn);
4474 if (phba->cfg_soft_wwpn)
4475 u64_to_wwn(phba->cfg_soft_wwpn,
4476 vport->fc_sparam.portName.u.wwn);
4477 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4478 sizeof(struct lpfc_name));
4479 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4480 sizeof(struct lpfc_name));
4481
4482 /* Update the fc_host data structures with new wwn. */
4483 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4484 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4485
4486 /* Register SGL pool to the device using non-embedded mailbox command */
4487 rc = lpfc_sli4_post_sgl_list(phba);
4488 if (unlikely(rc)) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004490 "0582 Error %d during sgl post operation\n",
4491 rc);
James Smartda0436e2009-05-22 14:51:39 -04004492 rc = -ENODEV;
4493 goto out_free_vpd;
4494 }
4495
4496 /* Register SCSI SGL pool to the device */
4497 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4498 if (unlikely(rc)) {
4499 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004500 "0383 Error %d during scsi sgl post "
4501 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004502 /* Some Scsi buffers were moved to the abort scsi list */
4503 /* A pci function reset will repost them */
4504 rc = -ENODEV;
4505 goto out_free_vpd;
4506 }
4507
4508 /* Post the rpi header region to the device. */
4509 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4510 if (unlikely(rc)) {
4511 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4512 "0393 Error %d during rpi post operation\n",
4513 rc);
4514 rc = -ENODEV;
4515 goto out_free_vpd;
4516 }
James Smartda0436e2009-05-22 14:51:39 -04004517
4518 /* Set up all the queues to the device */
4519 rc = lpfc_sli4_queue_setup(phba);
4520 if (unlikely(rc)) {
4521 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4522 "0381 Error %d during queue setup.\n ", rc);
4523 goto out_stop_timers;
4524 }
4525
4526 /* Arm the CQs and then EQs on device */
4527 lpfc_sli4_arm_cqeq_intr(phba);
4528
4529 /* Indicate device interrupt mode */
4530 phba->sli4_hba.intr_enable = 1;
4531
4532 /* Allow asynchronous mailbox command to go through */
4533 spin_lock_irq(&phba->hbalock);
4534 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4535 spin_unlock_irq(&phba->hbalock);
4536
4537 /* Post receive buffers to the device */
4538 lpfc_sli4_rb_setup(phba);
4539
James Smartfc2b9892010-02-26 14:15:29 -05004540 /* Reset HBA FCF states after HBA reset */
4541 phba->fcf.fcf_flag = 0;
4542 phba->fcf.current_rec.flag = 0;
4543
James Smartda0436e2009-05-22 14:51:39 -04004544 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004545 mod_timer(&vport->els_tmofunc,
4546 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004547
4548 /* Start heart beat timer */
4549 mod_timer(&phba->hb_tmofunc,
4550 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4551 phba->hb_outstanding = 0;
4552 phba->last_completion_time = jiffies;
4553
4554 /* Start error attention (ERATT) polling timer */
4555 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4556
4557 /*
4558 * The port is ready, set the host's link state to LINK_DOWN
4559 * in preparation for link interrupts.
4560 */
4561 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4562 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4563 lpfc_set_loopback_flag(phba);
4564 /* Change driver state to LPFC_LINK_DOWN right before init link */
4565 spin_lock_irq(&phba->hbalock);
4566 phba->link_state = LPFC_LINK_DOWN;
4567 spin_unlock_irq(&phba->hbalock);
4568 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4569 if (unlikely(rc != MBX_NOT_FINISHED)) {
4570 kfree(vpd);
4571 return 0;
4572 } else
4573 rc = -EIO;
4574
4575 /* Unset all the queues set up in this routine when error out */
4576 if (rc)
4577 lpfc_sli4_queue_unset(phba);
4578
4579out_stop_timers:
4580 if (rc)
4581 lpfc_stop_hba_timers(phba);
4582out_free_vpd:
4583 kfree(vpd);
4584out_free_mbox:
4585 mempool_free(mboxq, phba->mbox_mem_pool);
4586 return rc;
4587}
James Smarte59058c2008-08-24 21:49:00 -04004588
4589/**
James Smart3621a712009-04-06 18:47:14 -04004590 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004591 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004592 *
James Smarte59058c2008-08-24 21:49:00 -04004593 * This is the callback function for mailbox timer. The mailbox
4594 * timer is armed when a new mailbox command is issued and the timer
4595 * is deleted when the mailbox complete. The function is called by
4596 * the kernel timer code when a mailbox does not complete within
4597 * expected time. This function wakes up the worker thread to
4598 * process the mailbox timeout and returns. All the processing is
4599 * done by the worker thread function lpfc_mbox_timeout_handler.
4600 **/
dea31012005-04-17 16:05:31 -05004601void
4602lpfc_mbox_timeout(unsigned long ptr)
4603{
James Smart92d7f7b2007-06-17 19:56:38 -05004604 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004605 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004606 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004607
James Smart2e0fef82007-06-17 19:56:36 -05004608 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004609 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004610 if (!tmo_posted)
4611 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4612 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4613
James Smart5e9d9b82008-06-14 22:52:53 -04004614 if (!tmo_posted)
4615 lpfc_worker_wake_up(phba);
4616 return;
dea31012005-04-17 16:05:31 -05004617}
4618
James Smarte59058c2008-08-24 21:49:00 -04004619
4620/**
James Smart3621a712009-04-06 18:47:14 -04004621 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004622 * @phba: Pointer to HBA context object.
4623 *
4624 * This function is called from worker thread when a mailbox command times out.
4625 * The caller is not required to hold any locks. This function will reset the
4626 * HBA and recover all the pending commands.
4627 **/
dea31012005-04-17 16:05:31 -05004628void
4629lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4630{
James Smart2e0fef82007-06-17 19:56:36 -05004631 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004632 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004633 struct lpfc_sli *psli = &phba->sli;
4634 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004635
James Smarta257bf92009-04-06 18:48:10 -04004636 /* Check the pmbox pointer first. There is a race condition
4637 * between the mbox timeout handler getting executed in the
4638 * worklist and the mailbox actually completing. When this
4639 * race condition occurs, the mbox_active will be NULL.
4640 */
4641 spin_lock_irq(&phba->hbalock);
4642 if (pmbox == NULL) {
4643 lpfc_printf_log(phba, KERN_WARNING,
4644 LOG_MBOX | LOG_SLI,
4645 "0353 Active Mailbox cleared - mailbox timeout "
4646 "exiting\n");
4647 spin_unlock_irq(&phba->hbalock);
4648 return;
4649 }
4650
dea31012005-04-17 16:05:31 -05004651 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004653 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004654 mb->mbxCommand,
4655 phba->pport->port_state,
4656 phba->sli.sli_flag,
4657 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004658 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004659
James Smart1dcb58e2007-04-25 09:51:30 -04004660 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4661 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4662 * it to fail all oustanding SCSI IO.
4663 */
James Smart2e0fef82007-06-17 19:56:36 -05004664 spin_lock_irq(&phba->pport->work_port_lock);
4665 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4666 spin_unlock_irq(&phba->pport->work_port_lock);
4667 spin_lock_irq(&phba->hbalock);
4668 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004669 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004670 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004671
4672 pring = &psli->ring[psli->fcp_ring];
4673 lpfc_sli_abort_iocb_ring(phba, pring);
4674
4675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004676 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004677
4678 /* Reset the HBA device */
4679 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004680}
4681
James Smarte59058c2008-08-24 21:49:00 -04004682/**
James Smart3772a992009-05-22 14:50:54 -04004683 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004684 * @phba: Pointer to HBA context object.
4685 * @pmbox: Pointer to mailbox object.
4686 * @flag: Flag indicating how the mailbox need to be processed.
4687 *
4688 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004689 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4690 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004691 * The mailbox command can be submitted in polling mode, in which case
4692 * this function will wait in a polling loop for the completion of the
4693 * mailbox.
4694 * If the mailbox is submitted in no_wait mode (not polling) the
4695 * function will submit the command and returns immediately without waiting
4696 * for the mailbox completion. The no_wait is supported only when HBA
4697 * is in SLI2/SLI3 mode - interrupts are enabled.
4698 * The SLI interface allows only one mailbox pending at a time. If the
4699 * mailbox is issued in polling mode and there is already a mailbox
4700 * pending, then the function will return an error. If the mailbox is issued
4701 * in NO_WAIT mode and there is a mailbox pending already, the function
4702 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4703 * The sli layer owns the mailbox object until the completion of mailbox
4704 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4705 * return codes the caller owns the mailbox command after the return of
4706 * the function.
4707 **/
James Smart3772a992009-05-22 14:50:54 -04004708static int
4709lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4710 uint32_t flag)
dea31012005-04-17 16:05:31 -05004711{
dea31012005-04-17 16:05:31 -05004712 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004713 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004714 uint32_t status, evtctr;
4715 uint32_t ha_copy;
4716 int i;
James Smart09372822008-01-11 01:52:54 -05004717 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004718 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004719 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004720 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004721 int processing_queue = 0;
4722
4723 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4724 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004725 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004726 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004727 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4728 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4729 return MBX_SUCCESS;
4730 }
James Smart58da1ff2008-04-07 10:15:56 -04004731 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004732 pmbox = lpfc_mbox_get(phba);
4733 if (!pmbox) {
4734 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4735 return MBX_SUCCESS;
4736 }
4737 }
dea31012005-04-17 16:05:31 -05004738
James Smarted957682007-06-17 19:56:37 -05004739 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004740 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004741 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004742 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004743 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004744 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004745 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004746 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004747 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004748 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004749 }
4750 }
4751
Linas Vepstas8d63f372007-02-14 14:28:36 -06004752 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004753 if (unlikely(pci_channel_offline(phba->pcidev))) {
4754 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4755 goto out_not_finished;
4756 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004757
James Smarta257bf92009-04-06 18:48:10 -04004758 /* If HBA has a deferred error attention, fail the iocb. */
4759 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4760 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4761 goto out_not_finished;
4762 }
4763
dea31012005-04-17 16:05:31 -05004764 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004765
James Smart3772a992009-05-22 14:50:54 -04004766 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004767 status = MBX_SUCCESS;
4768
James Smart2e0fef82007-06-17 19:56:36 -05004769 if (phba->link_state == LPFC_HBA_ERROR) {
4770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004771
4772 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004773 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4774 "(%d):0311 Mailbox command x%x cannot "
4775 "issue Data: x%x x%x\n",
4776 pmbox->vport ? pmbox->vport->vpi : 0,
4777 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004778 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004779 }
4780
James Smart92908312006-03-07 15:04:13 -05004781 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4782 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004783 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004784 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4785 "(%d):2528 Mailbox command x%x cannot "
4786 "issue Data: x%x x%x\n",
4787 pmbox->vport ? pmbox->vport->vpi : 0,
4788 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004789 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004790 }
4791
dea31012005-04-17 16:05:31 -05004792 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4793 /* Polling for a mbox command when another one is already active
4794 * is not allowed in SLI. Also, the driver must have established
4795 * SLI2 mode to queue and process multiple mbox commands.
4796 */
4797
4798 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004799 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004800
4801 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004802 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4803 "(%d):2529 Mailbox command x%x "
4804 "cannot issue Data: x%x x%x\n",
4805 pmbox->vport ? pmbox->vport->vpi : 0,
4806 pmbox->u.mb.mbxCommand,
4807 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004808 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004809 }
4810
James Smart3772a992009-05-22 14:50:54 -04004811 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004813 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4815 "(%d):2530 Mailbox command x%x "
4816 "cannot issue Data: x%x x%x\n",
4817 pmbox->vport ? pmbox->vport->vpi : 0,
4818 pmbox->u.mb.mbxCommand,
4819 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004820 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004821 }
4822
dea31012005-04-17 16:05:31 -05004823 /* Another mailbox command is still being processed, queue this
4824 * command to be processed later.
4825 */
4826 lpfc_mbox_put(phba, pmbox);
4827
4828 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004829 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004830 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004831 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004832 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4833 mb->mbxCommand, phba->pport->port_state,
4834 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004835
4836 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004838
James Smart858c9f62007-06-17 19:56:39 -05004839 if (pmbox->vport) {
4840 lpfc_debugfs_disc_trc(pmbox->vport,
4841 LPFC_DISC_TRC_MBOX_VPORT,
4842 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4843 (uint32_t)mb->mbxCommand,
4844 mb->un.varWords[0], mb->un.varWords[1]);
4845 }
4846 else {
4847 lpfc_debugfs_disc_trc(phba->pport,
4848 LPFC_DISC_TRC_MBOX,
4849 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4850 (uint32_t)mb->mbxCommand,
4851 mb->un.varWords[0], mb->un.varWords[1]);
4852 }
4853
James Smart2e0fef82007-06-17 19:56:36 -05004854 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004855 }
4856
dea31012005-04-17 16:05:31 -05004857 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4858
4859 /* If we are not polling, we MUST be in SLI2 mode */
4860 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004861 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004862 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004863 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004864 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004865 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4867 "(%d):2531 Mailbox command x%x "
4868 "cannot issue Data: x%x x%x\n",
4869 pmbox->vport ? pmbox->vport->vpi : 0,
4870 pmbox->u.mb.mbxCommand,
4871 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004872 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004873 }
4874 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004875 mod_timer(&psli->mbox_tmo, (jiffies +
4876 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004877 }
4878
4879 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004880 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004881 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004882 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004883 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004884 mb->mbxCommand, phba->pport->port_state,
4885 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004886
James Smart858c9f62007-06-17 19:56:39 -05004887 if (mb->mbxCommand != MBX_HEARTBEAT) {
4888 if (pmbox->vport) {
4889 lpfc_debugfs_disc_trc(pmbox->vport,
4890 LPFC_DISC_TRC_MBOX_VPORT,
4891 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4892 (uint32_t)mb->mbxCommand,
4893 mb->un.varWords[0], mb->un.varWords[1]);
4894 }
4895 else {
4896 lpfc_debugfs_disc_trc(phba->pport,
4897 LPFC_DISC_TRC_MBOX,
4898 "MBOX Send: cmd:x%x mb:x%x x%x",
4899 (uint32_t)mb->mbxCommand,
4900 mb->un.varWords[0], mb->un.varWords[1]);
4901 }
4902 }
4903
dea31012005-04-17 16:05:31 -05004904 psli->slistat.mbox_cmd++;
4905 evtctr = psli->slistat.mbox_event;
4906
4907 /* next set own bit for the adapter and copy over command word */
4908 mb->mbxOwner = OWN_CHIP;
4909
James Smart3772a992009-05-22 14:50:54 -04004910 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04004911 /* Populate mbox extension offset word. */
4912 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4913 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4914 = (uint8_t *)phba->mbox_ext
4915 - (uint8_t *)phba->mbox;
4916 }
4917
4918 /* Copy the mailbox extension data */
4919 if (pmbox->in_ext_byte_len && pmbox->context2) {
4920 lpfc_sli_pcimem_bcopy(pmbox->context2,
4921 (uint8_t *)phba->mbox_ext,
4922 pmbox->in_ext_byte_len);
4923 }
4924 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004925 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004926 } else {
James Smart7a470272010-03-15 11:25:20 -04004927 /* Populate mbox extension offset word. */
4928 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4929 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4930 = MAILBOX_HBA_EXT_OFFSET;
4931
4932 /* Copy the mailbox extension data */
4933 if (pmbox->in_ext_byte_len && pmbox->context2) {
4934 lpfc_memcpy_to_slim(phba->MBslimaddr +
4935 MAILBOX_HBA_EXT_OFFSET,
4936 pmbox->context2, pmbox->in_ext_byte_len);
4937
4938 }
James Smart92908312006-03-07 15:04:13 -05004939 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004940 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004941 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004942 }
4943
4944 /* First copy mbox command data to HBA SLIM, skip past first
4945 word */
4946 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4947 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4948 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4949
4950 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004951 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004952 to_slim = phba->MBslimaddr;
4953 writel(ldata, to_slim);
4954 readl(to_slim); /* flush */
4955
4956 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4957 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004958 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004959 }
4960 }
4961
4962 wmb();
dea31012005-04-17 16:05:31 -05004963
4964 switch (flag) {
4965 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004966 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004967 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004968 /* Interrupt board to do it */
4969 writel(CA_MBATT, phba->CAregaddr);
4970 readl(phba->CAregaddr); /* flush */
4971 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004972 break;
4973
4974 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004975 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004976 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004977 /* Interrupt board to do it */
4978 writel(CA_MBATT, phba->CAregaddr);
4979 readl(phba->CAregaddr); /* flush */
4980
James Smart3772a992009-05-22 14:50:54 -04004981 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004982 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004983 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004984 word0 = le32_to_cpu(word0);
4985 } else {
4986 /* First read mbox status word */
4987 word0 = readl(phba->MBslimaddr);
4988 }
4989
4990 /* Read the HBA Host Attention Register */
4991 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004992 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4993 mb->mbxCommand) *
4994 1000) + jiffies;
4995 i = 0;
dea31012005-04-17 16:05:31 -05004996 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004997 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4998 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004999 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005000 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005001 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005002 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005003 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005004 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005005 }
5006
5007 /* Check if we took a mbox interrupt while we were
5008 polling */
5009 if (((word0 & OWN_CHIP) != OWN_CHIP)
5010 && (evtctr != psli->slistat.mbox_event))
5011 break;
5012
James Smart09372822008-01-11 01:52:54 -05005013 if (i++ > 10) {
5014 spin_unlock_irqrestore(&phba->hbalock,
5015 drvr_flag);
5016 msleep(1);
5017 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5018 }
dea31012005-04-17 16:05:31 -05005019
James Smart3772a992009-05-22 14:50:54 -04005020 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005021 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005022 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005023 word0 = le32_to_cpu(word0);
5024 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5025 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005026 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005027 /* Check real SLIM for any errors */
5028 slimword0 = readl(phba->MBslimaddr);
5029 slimmb = (MAILBOX_t *) & slimword0;
5030 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5031 && slimmb->mbxStatus) {
5032 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005033 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005034 word0 = slimword0;
5035 }
5036 }
5037 } else {
5038 /* First copy command data */
5039 word0 = readl(phba->MBslimaddr);
5040 }
5041 /* Read the HBA Host Attention Register */
5042 ha_copy = readl(phba->HAregaddr);
5043 }
5044
James Smart3772a992009-05-22 14:50:54 -04005045 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005046 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005047 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005048 /* Copy the mailbox extension data */
5049 if (pmbox->out_ext_byte_len && pmbox->context2) {
5050 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5051 pmbox->context2,
5052 pmbox->out_ext_byte_len);
5053 }
dea31012005-04-17 16:05:31 -05005054 } else {
5055 /* First copy command data */
5056 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5057 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005058 /* Copy the mailbox extension data */
5059 if (pmbox->out_ext_byte_len && pmbox->context2) {
5060 lpfc_memcpy_from_slim(pmbox->context2,
5061 phba->MBslimaddr +
5062 MAILBOX_HBA_EXT_OFFSET,
5063 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005064 }
5065 }
5066
5067 writel(HA_MBATT, phba->HAregaddr);
5068 readl(phba->HAregaddr); /* flush */
5069
5070 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5071 status = mb->mbxStatus;
5072 }
5073
James Smart2e0fef82007-06-17 19:56:36 -05005074 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5075 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005076
5077out_not_finished:
5078 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005079 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005080 lpfc_mbox_cmpl_put(phba, pmbox);
5081 }
5082 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005083}
5084
James Smarte59058c2008-08-24 21:49:00 -04005085/**
James Smartf1126682009-06-10 17:22:44 -04005086 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5087 * @phba: Pointer to HBA context object.
5088 *
5089 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5090 * the driver internal pending mailbox queue. It will then try to wait out the
5091 * possible outstanding mailbox command before return.
5092 *
5093 * Returns:
5094 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5095 * the outstanding mailbox command timed out.
5096 **/
5097static int
5098lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5099{
5100 struct lpfc_sli *psli = &phba->sli;
5101 uint8_t actcmd = MBX_HEARTBEAT;
5102 int rc = 0;
5103 unsigned long timeout;
5104
5105 /* Mark the asynchronous mailbox command posting as blocked */
5106 spin_lock_irq(&phba->hbalock);
5107 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5108 if (phba->sli.mbox_active)
5109 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5110 spin_unlock_irq(&phba->hbalock);
5111 /* Determine how long we might wait for the active mailbox
5112 * command to be gracefully completed by firmware.
5113 */
5114 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5115 jiffies;
5116 /* Wait for the outstnading mailbox command to complete */
5117 while (phba->sli.mbox_active) {
5118 /* Check active mailbox complete status every 2ms */
5119 msleep(2);
5120 if (time_after(jiffies, timeout)) {
5121 /* Timeout, marked the outstanding cmd not complete */
5122 rc = 1;
5123 break;
5124 }
5125 }
5126
5127 /* Can not cleanly block async mailbox command, fails it */
5128 if (rc) {
5129 spin_lock_irq(&phba->hbalock);
5130 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5131 spin_unlock_irq(&phba->hbalock);
5132 }
5133 return rc;
5134}
5135
5136/**
5137 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5138 * @phba: Pointer to HBA context object.
5139 *
5140 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5141 * commands from the driver internal pending mailbox queue. It makes sure
5142 * that there is no outstanding mailbox command before resuming posting
5143 * asynchronous mailbox commands. If, for any reason, there is outstanding
5144 * mailbox command, it will try to wait it out before resuming asynchronous
5145 * mailbox command posting.
5146 **/
5147static void
5148lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5149{
5150 struct lpfc_sli *psli = &phba->sli;
5151
5152 spin_lock_irq(&phba->hbalock);
5153 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5154 /* Asynchronous mailbox posting is not blocked, do nothing */
5155 spin_unlock_irq(&phba->hbalock);
5156 return;
5157 }
5158
5159 /* Outstanding synchronous mailbox command is guaranteed to be done,
5160 * successful or timeout, after timing-out the outstanding mailbox
5161 * command shall always be removed, so just unblock posting async
5162 * mailbox command and resume
5163 */
5164 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5165 spin_unlock_irq(&phba->hbalock);
5166
5167 /* wake up worker thread to post asynchronlous mailbox command */
5168 lpfc_worker_wake_up(phba);
5169}
5170
5171/**
James Smartda0436e2009-05-22 14:51:39 -04005172 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5173 * @phba: Pointer to HBA context object.
5174 * @mboxq: Pointer to mailbox object.
5175 *
5176 * The function posts a mailbox to the port. The mailbox is expected
5177 * to be comletely filled in and ready for the port to operate on it.
5178 * This routine executes a synchronous completion operation on the
5179 * mailbox by polling for its completion.
5180 *
5181 * The caller must not be holding any locks when calling this routine.
5182 *
5183 * Returns:
5184 * MBX_SUCCESS - mailbox posted successfully
5185 * Any of the MBX error values.
5186 **/
5187static int
5188lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5189{
5190 int rc = MBX_SUCCESS;
5191 unsigned long iflag;
5192 uint32_t db_ready;
5193 uint32_t mcqe_status;
5194 uint32_t mbx_cmnd;
5195 unsigned long timeout;
5196 struct lpfc_sli *psli = &phba->sli;
5197 struct lpfc_mqe *mb = &mboxq->u.mqe;
5198 struct lpfc_bmbx_create *mbox_rgn;
5199 struct dma_address *dma_address;
5200 struct lpfc_register bmbx_reg;
5201
5202 /*
5203 * Only one mailbox can be active to the bootstrap mailbox region
5204 * at a time and there is no queueing provided.
5205 */
5206 spin_lock_irqsave(&phba->hbalock, iflag);
5207 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5208 spin_unlock_irqrestore(&phba->hbalock, iflag);
5209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5210 "(%d):2532 Mailbox command x%x (x%x) "
5211 "cannot issue Data: x%x x%x\n",
5212 mboxq->vport ? mboxq->vport->vpi : 0,
5213 mboxq->u.mb.mbxCommand,
5214 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5215 psli->sli_flag, MBX_POLL);
5216 return MBXERR_ERROR;
5217 }
5218 /* The server grabs the token and owns it until release */
5219 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5220 phba->sli.mbox_active = mboxq;
5221 spin_unlock_irqrestore(&phba->hbalock, iflag);
5222
5223 /*
5224 * Initialize the bootstrap memory region to avoid stale data areas
5225 * in the mailbox post. Then copy the caller's mailbox contents to
5226 * the bmbx mailbox region.
5227 */
5228 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5229 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5230 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5231 sizeof(struct lpfc_mqe));
5232
5233 /* Post the high mailbox dma address to the port and wait for ready. */
5234 dma_address = &phba->sli4_hba.bmbx.dma_address;
5235 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5236
5237 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5238 * 1000) + jiffies;
5239 do {
5240 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5241 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5242 if (!db_ready)
5243 msleep(2);
5244
5245 if (time_after(jiffies, timeout)) {
5246 rc = MBXERR_ERROR;
5247 goto exit;
5248 }
5249 } while (!db_ready);
5250
5251 /* Post the low mailbox dma address to the port. */
5252 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5253 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5254 * 1000) + jiffies;
5255 do {
5256 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5257 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5258 if (!db_ready)
5259 msleep(2);
5260
5261 if (time_after(jiffies, timeout)) {
5262 rc = MBXERR_ERROR;
5263 goto exit;
5264 }
5265 } while (!db_ready);
5266
5267 /*
5268 * Read the CQ to ensure the mailbox has completed.
5269 * If so, update the mailbox status so that the upper layers
5270 * can complete the request normally.
5271 */
5272 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5273 sizeof(struct lpfc_mqe));
5274 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5275 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5276 sizeof(struct lpfc_mcqe));
5277 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5278
5279 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5280 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5281 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5282 rc = MBXERR_ERROR;
5283 }
5284
5285 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5286 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5287 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5288 " x%x x%x CQ: x%x x%x x%x x%x\n",
5289 mboxq->vport ? mboxq->vport->vpi : 0,
5290 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5291 bf_get(lpfc_mqe_status, mb),
5292 mb->un.mb_words[0], mb->un.mb_words[1],
5293 mb->un.mb_words[2], mb->un.mb_words[3],
5294 mb->un.mb_words[4], mb->un.mb_words[5],
5295 mb->un.mb_words[6], mb->un.mb_words[7],
5296 mb->un.mb_words[8], mb->un.mb_words[9],
5297 mb->un.mb_words[10], mb->un.mb_words[11],
5298 mb->un.mb_words[12], mboxq->mcqe.word0,
5299 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5300 mboxq->mcqe.trailer);
5301exit:
5302 /* We are holding the token, no needed for lock when release */
5303 spin_lock_irqsave(&phba->hbalock, iflag);
5304 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5305 phba->sli.mbox_active = NULL;
5306 spin_unlock_irqrestore(&phba->hbalock, iflag);
5307 return rc;
5308}
5309
5310/**
5311 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5312 * @phba: Pointer to HBA context object.
5313 * @pmbox: Pointer to mailbox object.
5314 * @flag: Flag indicating how the mailbox need to be processed.
5315 *
5316 * This function is called by discovery code and HBA management code to submit
5317 * a mailbox command to firmware with SLI-4 interface spec.
5318 *
5319 * Return codes the caller owns the mailbox command after the return of the
5320 * function.
5321 **/
5322static int
5323lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5324 uint32_t flag)
5325{
5326 struct lpfc_sli *psli = &phba->sli;
5327 unsigned long iflags;
5328 int rc;
5329
James Smart8fa38512009-07-19 10:01:03 -04005330 rc = lpfc_mbox_dev_check(phba);
5331 if (unlikely(rc)) {
5332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5333 "(%d):2544 Mailbox command x%x (x%x) "
5334 "cannot issue Data: x%x x%x\n",
5335 mboxq->vport ? mboxq->vport->vpi : 0,
5336 mboxq->u.mb.mbxCommand,
5337 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5338 psli->sli_flag, flag);
5339 goto out_not_finished;
5340 }
5341
James Smartda0436e2009-05-22 14:51:39 -04005342 /* Detect polling mode and jump to a handler */
5343 if (!phba->sli4_hba.intr_enable) {
5344 if (flag == MBX_POLL)
5345 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5346 else
5347 rc = -EIO;
5348 if (rc != MBX_SUCCESS)
5349 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5350 "(%d):2541 Mailbox command x%x "
5351 "(x%x) cannot issue Data: x%x x%x\n",
5352 mboxq->vport ? mboxq->vport->vpi : 0,
5353 mboxq->u.mb.mbxCommand,
5354 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5355 psli->sli_flag, flag);
5356 return rc;
5357 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005358 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5359 "(%d):2542 Try to issue mailbox command "
5360 "x%x (x%x) synchronously ahead of async"
5361 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005362 mboxq->vport ? mboxq->vport->vpi : 0,
5363 mboxq->u.mb.mbxCommand,
5364 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5365 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005366 /* Try to block the asynchronous mailbox posting */
5367 rc = lpfc_sli4_async_mbox_block(phba);
5368 if (!rc) {
5369 /* Successfully blocked, now issue sync mbox cmd */
5370 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5371 if (rc != MBX_SUCCESS)
5372 lpfc_printf_log(phba, KERN_ERR,
5373 LOG_MBOX | LOG_SLI,
5374 "(%d):2597 Mailbox command "
5375 "x%x (x%x) cannot issue "
5376 "Data: x%x x%x\n",
5377 mboxq->vport ?
5378 mboxq->vport->vpi : 0,
5379 mboxq->u.mb.mbxCommand,
5380 lpfc_sli4_mbox_opcode_get(phba,
5381 mboxq),
5382 psli->sli_flag, flag);
5383 /* Unblock the async mailbox posting afterward */
5384 lpfc_sli4_async_mbox_unblock(phba);
5385 }
5386 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005387 }
5388
5389 /* Now, interrupt mode asynchrous mailbox command */
5390 rc = lpfc_mbox_cmd_check(phba, mboxq);
5391 if (rc) {
5392 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5393 "(%d):2543 Mailbox command x%x (x%x) "
5394 "cannot issue Data: x%x x%x\n",
5395 mboxq->vport ? mboxq->vport->vpi : 0,
5396 mboxq->u.mb.mbxCommand,
5397 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5398 psli->sli_flag, flag);
5399 goto out_not_finished;
5400 }
James Smartda0436e2009-05-22 14:51:39 -04005401
5402 /* Put the mailbox command to the driver internal FIFO */
5403 psli->slistat.mbox_busy++;
5404 spin_lock_irqsave(&phba->hbalock, iflags);
5405 lpfc_mbox_put(phba, mboxq);
5406 spin_unlock_irqrestore(&phba->hbalock, iflags);
5407 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5408 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5409 "x%x (x%x) x%x x%x x%x\n",
5410 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5411 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5412 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5413 phba->pport->port_state,
5414 psli->sli_flag, MBX_NOWAIT);
5415 /* Wake up worker thread to transport mailbox command from head */
5416 lpfc_worker_wake_up(phba);
5417
5418 return MBX_BUSY;
5419
5420out_not_finished:
5421 return MBX_NOT_FINISHED;
5422}
5423
5424/**
5425 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5426 * @phba: Pointer to HBA context object.
5427 *
5428 * This function is called by worker thread to send a mailbox command to
5429 * SLI4 HBA firmware.
5430 *
5431 **/
5432int
5433lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5434{
5435 struct lpfc_sli *psli = &phba->sli;
5436 LPFC_MBOXQ_t *mboxq;
5437 int rc = MBX_SUCCESS;
5438 unsigned long iflags;
5439 struct lpfc_mqe *mqe;
5440 uint32_t mbx_cmnd;
5441
5442 /* Check interrupt mode before post async mailbox command */
5443 if (unlikely(!phba->sli4_hba.intr_enable))
5444 return MBX_NOT_FINISHED;
5445
5446 /* Check for mailbox command service token */
5447 spin_lock_irqsave(&phba->hbalock, iflags);
5448 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5449 spin_unlock_irqrestore(&phba->hbalock, iflags);
5450 return MBX_NOT_FINISHED;
5451 }
5452 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5453 spin_unlock_irqrestore(&phba->hbalock, iflags);
5454 return MBX_NOT_FINISHED;
5455 }
5456 if (unlikely(phba->sli.mbox_active)) {
5457 spin_unlock_irqrestore(&phba->hbalock, iflags);
5458 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5459 "0384 There is pending active mailbox cmd\n");
5460 return MBX_NOT_FINISHED;
5461 }
5462 /* Take the mailbox command service token */
5463 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5464
5465 /* Get the next mailbox command from head of queue */
5466 mboxq = lpfc_mbox_get(phba);
5467
5468 /* If no more mailbox command waiting for post, we're done */
5469 if (!mboxq) {
5470 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5471 spin_unlock_irqrestore(&phba->hbalock, iflags);
5472 return MBX_SUCCESS;
5473 }
5474 phba->sli.mbox_active = mboxq;
5475 spin_unlock_irqrestore(&phba->hbalock, iflags);
5476
5477 /* Check device readiness for posting mailbox command */
5478 rc = lpfc_mbox_dev_check(phba);
5479 if (unlikely(rc))
5480 /* Driver clean routine will clean up pending mailbox */
5481 goto out_not_finished;
5482
5483 /* Prepare the mbox command to be posted */
5484 mqe = &mboxq->u.mqe;
5485 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5486
5487 /* Start timer for the mbox_tmo and log some mailbox post messages */
5488 mod_timer(&psli->mbox_tmo, (jiffies +
5489 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5490
5491 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5492 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5493 "x%x x%x\n",
5494 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5495 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5496 phba->pport->port_state, psli->sli_flag);
5497
5498 if (mbx_cmnd != MBX_HEARTBEAT) {
5499 if (mboxq->vport) {
5500 lpfc_debugfs_disc_trc(mboxq->vport,
5501 LPFC_DISC_TRC_MBOX_VPORT,
5502 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5503 mbx_cmnd, mqe->un.mb_words[0],
5504 mqe->un.mb_words[1]);
5505 } else {
5506 lpfc_debugfs_disc_trc(phba->pport,
5507 LPFC_DISC_TRC_MBOX,
5508 "MBOX Send: cmd:x%x mb:x%x x%x",
5509 mbx_cmnd, mqe->un.mb_words[0],
5510 mqe->un.mb_words[1]);
5511 }
5512 }
5513 psli->slistat.mbox_cmd++;
5514
5515 /* Post the mailbox command to the port */
5516 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5517 if (rc != MBX_SUCCESS) {
5518 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5519 "(%d):2533 Mailbox command x%x (x%x) "
5520 "cannot issue Data: x%x x%x\n",
5521 mboxq->vport ? mboxq->vport->vpi : 0,
5522 mboxq->u.mb.mbxCommand,
5523 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5524 psli->sli_flag, MBX_NOWAIT);
5525 goto out_not_finished;
5526 }
5527
5528 return rc;
5529
5530out_not_finished:
5531 spin_lock_irqsave(&phba->hbalock, iflags);
5532 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5533 __lpfc_mbox_cmpl_put(phba, mboxq);
5534 /* Release the token */
5535 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5536 phba->sli.mbox_active = NULL;
5537 spin_unlock_irqrestore(&phba->hbalock, iflags);
5538
5539 return MBX_NOT_FINISHED;
5540}
5541
5542/**
5543 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5544 * @phba: Pointer to HBA context object.
5545 * @pmbox: Pointer to mailbox object.
5546 * @flag: Flag indicating how the mailbox need to be processed.
5547 *
5548 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5549 * the API jump table function pointer from the lpfc_hba struct.
5550 *
5551 * Return codes the caller owns the mailbox command after the return of the
5552 * function.
5553 **/
5554int
5555lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5556{
5557 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5558}
5559
5560/**
5561 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5562 * @phba: The hba struct for which this call is being executed.
5563 * @dev_grp: The HBA PCI-Device group number.
5564 *
5565 * This routine sets up the mbox interface API function jump table in @phba
5566 * struct.
5567 * Returns: 0 - success, -ENODEV - failure.
5568 **/
5569int
5570lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5571{
5572
5573 switch (dev_grp) {
5574 case LPFC_PCI_DEV_LP:
5575 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5576 phba->lpfc_sli_handle_slow_ring_event =
5577 lpfc_sli_handle_slow_ring_event_s3;
5578 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5579 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5580 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5581 break;
5582 case LPFC_PCI_DEV_OC:
5583 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5584 phba->lpfc_sli_handle_slow_ring_event =
5585 lpfc_sli_handle_slow_ring_event_s4;
5586 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5587 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5588 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5589 break;
5590 default:
5591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5592 "1420 Invalid HBA PCI-device group: 0x%x\n",
5593 dev_grp);
5594 return -ENODEV;
5595 break;
5596 }
5597 return 0;
5598}
5599
5600/**
James Smart3621a712009-04-06 18:47:14 -04005601 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005602 * @phba: Pointer to HBA context object.
5603 * @pring: Pointer to driver SLI ring object.
5604 * @piocb: Pointer to address of newly added command iocb.
5605 *
5606 * This function is called with hbalock held to add a command
5607 * iocb to the txq when SLI layer cannot submit the command iocb
5608 * to the ring.
5609 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04005610void
James Smart92d7f7b2007-06-17 19:56:38 -05005611__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005612 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005613{
5614 /* Insert the caller's iocb in the txq tail for later processing. */
5615 list_add_tail(&piocb->list, &pring->txq);
5616 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005617}
5618
James Smarte59058c2008-08-24 21:49:00 -04005619/**
James Smart3621a712009-04-06 18:47:14 -04005620 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005621 * @phba: Pointer to HBA context object.
5622 * @pring: Pointer to driver SLI ring object.
5623 * @piocb: Pointer to address of newly added command iocb.
5624 *
5625 * This function is called with hbalock held before a new
5626 * iocb is submitted to the firmware. This function checks
5627 * txq to flush the iocbs in txq to Firmware before
5628 * submitting new iocbs to the Firmware.
5629 * If there are iocbs in the txq which need to be submitted
5630 * to firmware, lpfc_sli_next_iocb returns the first element
5631 * of the txq after dequeuing it from txq.
5632 * If there is no iocb in the txq then the function will return
5633 * *piocb and *piocb is set to NULL. Caller needs to check
5634 * *piocb to find if there are more commands in the txq.
5635 **/
dea31012005-04-17 16:05:31 -05005636static struct lpfc_iocbq *
5637lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005638 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005639{
5640 struct lpfc_iocbq * nextiocb;
5641
5642 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5643 if (!nextiocb) {
5644 nextiocb = *piocb;
5645 *piocb = NULL;
5646 }
5647
5648 return nextiocb;
5649}
5650
James Smarte59058c2008-08-24 21:49:00 -04005651/**
James Smart3772a992009-05-22 14:50:54 -04005652 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005653 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005654 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005655 * @piocb: Pointer to command iocb.
5656 * @flag: Flag indicating if this command can be put into txq.
5657 *
James Smart3772a992009-05-22 14:50:54 -04005658 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5659 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5660 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5661 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5662 * this function allows only iocbs for posting buffers. This function finds
5663 * next available slot in the command ring and posts the command to the
5664 * available slot and writes the port attention register to request HBA start
5665 * processing new iocb. If there is no slot available in the ring and
5666 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5667 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005668 *
James Smart3772a992009-05-22 14:50:54 -04005669 * This function is called with hbalock held. The function will return success
5670 * after it successfully submit the iocb to firmware or after adding to the
5671 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005672 **/
James Smart98c9ea52007-10-27 13:37:33 -04005673static int
James Smart3772a992009-05-22 14:50:54 -04005674__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005675 struct lpfc_iocbq *piocb, uint32_t flag)
5676{
5677 struct lpfc_iocbq *nextiocb;
5678 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005679 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005680
James Smart92d7f7b2007-06-17 19:56:38 -05005681 if (piocb->iocb_cmpl && (!piocb->vport) &&
5682 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5683 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5684 lpfc_printf_log(phba, KERN_ERR,
5685 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005686 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005687 piocb->iocb.ulpCommand);
5688 dump_stack();
5689 return IOCB_ERROR;
5690 }
5691
5692
Linas Vepstas8d63f372007-02-14 14:28:36 -06005693 /* If the PCI channel is in offline state, do not post iocbs. */
5694 if (unlikely(pci_channel_offline(phba->pcidev)))
5695 return IOCB_ERROR;
5696
James Smarta257bf92009-04-06 18:48:10 -04005697 /* If HBA has a deferred error attention, fail the iocb. */
5698 if (unlikely(phba->hba_flag & DEFER_ERATT))
5699 return IOCB_ERROR;
5700
dea31012005-04-17 16:05:31 -05005701 /*
5702 * We should never get an IOCB if we are in a < LINK_DOWN state
5703 */
James Smart2e0fef82007-06-17 19:56:36 -05005704 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005705 return IOCB_ERROR;
5706
5707 /*
5708 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005709 * outstanding event.
dea31012005-04-17 16:05:31 -05005710 */
James Smart0b727fe2007-10-27 13:37:25 -04005711 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005712 goto iocb_busy;
5713
James Smart2e0fef82007-06-17 19:56:36 -05005714 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005715 /*
James Smart2680eea2007-04-25 09:52:55 -04005716 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005717 * can be issued if the link is not up.
5718 */
5719 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005720 case CMD_GEN_REQUEST64_CR:
5721 case CMD_GEN_REQUEST64_CX:
5722 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5723 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005724 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005725 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5726 MENLO_TRANSPORT_TYPE))
5727
5728 goto iocb_busy;
5729 break;
dea31012005-04-17 16:05:31 -05005730 case CMD_QUE_RING_BUF_CN:
5731 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005732 /*
5733 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5734 * completion, iocb_cmpl MUST be 0.
5735 */
5736 if (piocb->iocb_cmpl)
5737 piocb->iocb_cmpl = NULL;
5738 /*FALLTHROUGH*/
5739 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005740 case CMD_CLOSE_XRI_CN:
5741 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005742 break;
5743 default:
5744 goto iocb_busy;
5745 }
5746
5747 /*
5748 * For FCP commands, we must be in a state where we can process link
5749 * attention events.
5750 */
5751 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005752 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005753 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005754 }
dea31012005-04-17 16:05:31 -05005755
dea31012005-04-17 16:05:31 -05005756 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5757 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5758 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5759
5760 if (iocb)
5761 lpfc_sli_update_ring(phba, pring);
5762 else
5763 lpfc_sli_update_full_ring(phba, pring);
5764
5765 if (!piocb)
5766 return IOCB_SUCCESS;
5767
5768 goto out_busy;
5769
5770 iocb_busy:
5771 pring->stats.iocb_cmd_delay++;
5772
5773 out_busy:
5774
5775 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005776 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005777 return IOCB_SUCCESS;
5778 }
5779
5780 return IOCB_BUSY;
5781}
5782
James Smart3772a992009-05-22 14:50:54 -04005783/**
James Smart4f774512009-05-22 14:52:35 -04005784 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5785 * @phba: Pointer to HBA context object.
5786 * @piocb: Pointer to command iocb.
5787 * @sglq: Pointer to the scatter gather queue object.
5788 *
5789 * This routine converts the bpl or bde that is in the IOCB
5790 * to a sgl list for the sli4 hardware. The physical address
5791 * of the bpl/bde is converted back to a virtual address.
5792 * If the IOCB contains a BPL then the list of BDE's is
5793 * converted to sli4_sge's. If the IOCB contains a single
5794 * BDE then it is converted to a single sli_sge.
5795 * The IOCB is still in cpu endianess so the contents of
5796 * the bpl can be used without byte swapping.
5797 *
5798 * Returns valid XRI = Success, NO_XRI = Failure.
5799**/
5800static uint16_t
5801lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5802 struct lpfc_sglq *sglq)
5803{
5804 uint16_t xritag = NO_XRI;
5805 struct ulp_bde64 *bpl = NULL;
5806 struct ulp_bde64 bde;
5807 struct sli4_sge *sgl = NULL;
5808 IOCB_t *icmd;
5809 int numBdes = 0;
5810 int i = 0;
5811
5812 if (!piocbq || !sglq)
5813 return xritag;
5814
5815 sgl = (struct sli4_sge *)sglq->sgl;
5816 icmd = &piocbq->iocb;
5817 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5818 numBdes = icmd->un.genreq64.bdl.bdeSize /
5819 sizeof(struct ulp_bde64);
5820 /* The addrHigh and addrLow fields within the IOCB
5821 * have not been byteswapped yet so there is no
5822 * need to swap them back.
5823 */
5824 bpl = (struct ulp_bde64 *)
5825 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5826
5827 if (!bpl)
5828 return xritag;
5829
5830 for (i = 0; i < numBdes; i++) {
5831 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005832 sgl->addr_hi = bpl->addrHigh;
5833 sgl->addr_lo = bpl->addrLow;
5834
James Smart4f774512009-05-22 14:52:35 -04005835 if ((i+1) == numBdes)
5836 bf_set(lpfc_sli4_sge_last, sgl, 1);
5837 else
5838 bf_set(lpfc_sli4_sge_last, sgl, 0);
5839 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005840 /* swap the size field back to the cpu so we
5841 * can assign it to the sgl.
5842 */
5843 bde.tus.w = le32_to_cpu(bpl->tus.w);
5844 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005845 bpl++;
5846 sgl++;
5847 }
5848 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5849 /* The addrHigh and addrLow fields of the BDE have not
5850 * been byteswapped yet so they need to be swapped
5851 * before putting them in the sgl.
5852 */
5853 sgl->addr_hi =
5854 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5855 sgl->addr_lo =
5856 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005857 bf_set(lpfc_sli4_sge_last, sgl, 1);
5858 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005859 sgl->sge_len =
5860 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005861 }
5862 return sglq->sli4_xritag;
5863}
5864
5865/**
5866 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5867 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005868 *
5869 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005870 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5871 * held.
James Smart4f774512009-05-22 14:52:35 -04005872 *
5873 * Return: index into SLI4 fast-path FCP queue index.
5874 **/
5875static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005876lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005877{
James Smart8fa38512009-07-19 10:01:03 -04005878 ++phba->fcp_qidx;
5879 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5880 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005881
James Smart8fa38512009-07-19 10:01:03 -04005882 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005883}
5884
5885/**
5886 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5887 * @phba: Pointer to HBA context object.
5888 * @piocb: Pointer to command iocb.
5889 * @wqe: Pointer to the work queue entry.
5890 *
5891 * This routine converts the iocb command to its Work Queue Entry
5892 * equivalent. The wqe pointer should not have any fields set when
5893 * this routine is called because it will memcpy over them.
5894 * This routine does not set the CQ_ID or the WQEC bits in the
5895 * wqe.
5896 *
5897 * Returns: 0 = Success, IOCB_ERROR = Failure.
5898 **/
5899static int
5900lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5901 union lpfc_wqe *wqe)
5902{
James Smart5ffc2662009-11-18 15:39:44 -05005903 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005904 uint8_t ct = 0;
5905 uint32_t fip;
5906 uint32_t abort_tag;
5907 uint8_t command_type = ELS_COMMAND_NON_FIP;
5908 uint8_t cmnd;
5909 uint16_t xritag;
5910 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005911 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005912 int numBdes, i;
5913 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005914
James Smart45ed1192009-10-02 15:17:02 -04005915 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005916 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005917 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005918 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005919 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005920 command_type = ELS_COMMAND_FIP;
5921 else
5922 command_type = ELS_COMMAND_NON_FIP;
5923
James Smart4f774512009-05-22 14:52:35 -04005924 /* Some of the fields are in the right position already */
5925 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5926 abort_tag = (uint32_t) iocbq->iotag;
5927 xritag = iocbq->sli4_xritag;
5928 wqe->words[7] = 0; /* The ct field has moved so reset */
5929 /* words0-2 bpl convert bde */
5930 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005931 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5932 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005933 bpl = (struct ulp_bde64 *)
5934 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5935 if (!bpl)
5936 return IOCB_ERROR;
5937
5938 /* Should already be byte swapped. */
5939 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5940 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5941 /* swap the size field back to the cpu so we
5942 * can assign it to the sgl.
5943 */
5944 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005945 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5946 total_len = 0;
5947 for (i = 0; i < numBdes; i++) {
5948 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5949 total_len += bde.tus.f.bdeSize;
5950 }
James Smart4f774512009-05-22 14:52:35 -04005951 } else
James Smart5ffc2662009-11-18 15:39:44 -05005952 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005953
5954 iocbq->iocb.ulpIoTag = iocbq->iotag;
5955 cmnd = iocbq->iocb.ulpCommand;
5956
5957 switch (iocbq->iocb.ulpCommand) {
5958 case CMD_ELS_REQUEST64_CR:
5959 if (!iocbq->iocb.ulpLe) {
5960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5961 "2007 Only Limited Edition cmd Format"
5962 " supported 0x%x\n",
5963 iocbq->iocb.ulpCommand);
5964 return IOCB_ERROR;
5965 }
James Smart5ffc2662009-11-18 15:39:44 -05005966 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005967 /* Els_reguest64 has a TMO */
5968 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5969 iocbq->iocb.ulpTimeout);
5970 /* Need a VF for word 4 set the vf bit*/
5971 bf_set(els_req64_vf, &wqe->els_req, 0);
5972 /* And a VFID for word 12 */
5973 bf_set(els_req64_vfid, &wqe->els_req, 0);
5974 /*
5975 * Set ct field to 3, indicates that the context_tag field
5976 * contains the FCFI and remote N_Port_ID is
5977 * in word 5.
5978 */
5979
5980 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5981 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5982 iocbq->iocb.ulpContext);
5983
James Smart4f774512009-05-22 14:52:35 -04005984 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5985 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5986 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005987
5988 if (command_type == ELS_COMMAND_FIP) {
5989 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5990 >> LPFC_FIP_ELS_ID_SHIFT);
5991 }
5992 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5993
James Smart4f774512009-05-22 14:52:35 -04005994 break;
James Smart5ffc2662009-11-18 15:39:44 -05005995 case CMD_XMIT_SEQUENCE64_CX:
5996 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5997 iocbq->iocb.un.ulpWord[3]);
5998 wqe->generic.word3 = 0;
5999 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006000 /* The entire sequence is transmitted for this IOCB */
6001 xmit_len = total_len;
6002 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006003 case CMD_XMIT_SEQUENCE64_CR:
6004 /* word3 iocb=io_tag32 wqe=payload_offset */
6005 /* payload offset used for multilpe outstanding
6006 * sequences on the same exchange
6007 */
6008 wqe->words[3] = 0;
6009 /* word4 relative_offset memcpy */
6010 /* word5 r_ctl/df_ctl memcpy */
6011 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006012 wqe->xmit_sequence.xmit_len = xmit_len;
6013 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006014 break;
6015 case CMD_XMIT_BCAST64_CN:
6016 /* word3 iocb=iotag32 wqe=payload_len */
6017 wqe->words[3] = 0; /* no definition for this in wqe */
6018 /* word4 iocb=rsvd wqe=rsvd */
6019 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6020 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6021 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6022 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6023 break;
6024 case CMD_FCP_IWRITE64_CR:
6025 command_type = FCP_COMMAND_DATA_OUT;
6026 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6027 * confusing.
6028 * word3 is payload_len: byte offset to the sgl entry for the
6029 * fcp_command.
6030 * word4 is total xfer len, same as the IOCB->ulpParameter.
6031 * word5 is initial xfer len 0 = wait for xfer-ready
6032 */
6033
6034 /* Always wait for xfer-ready before sending data */
6035 wqe->fcp_iwrite.initial_xfer_len = 0;
6036 /* word 4 (xfer length) should have been set on the memcpy */
6037
6038 /* allow write to fall through to read */
6039 case CMD_FCP_IREAD64_CR:
6040 /* FCP_CMD is always the 1st sgl entry */
6041 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05006042 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04006043
6044 /* word 4 (xfer length) should have been set on the memcpy */
6045
6046 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6047 iocbq->iocb.ulpFCP2Rcvy);
6048 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6049 /* The XC bit and the XS bit are similar. The driver never
6050 * tracked whether or not the exchange was previouslly open.
6051 * XC = Exchange create, 0 is create. 1 is already open.
6052 * XS = link cmd: 1 do not close the exchange after command.
6053 * XS = 0 close exchange when command completes.
6054 * The only time we would not set the XC bit is when the XS bit
6055 * is set and we are sending our 2nd or greater command on
6056 * this exchange.
6057 */
James Smart4f774512009-05-22 14:52:35 -04006058 /* Always open the exchange */
6059 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6060
6061 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6062 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006063 break;
6064 case CMD_FCP_ICMND64_CR:
6065 /* Always open the exchange */
6066 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6067
6068 wqe->words[4] = 0;
6069 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6070 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006071 break;
6072 case CMD_GEN_REQUEST64_CR:
6073 /* word3 command length is described as byte offset to the
6074 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6075 * sgl[0] = cmnd
6076 * sgl[1] = rsp.
6077 *
6078 */
James Smart5ffc2662009-11-18 15:39:44 -05006079 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006080 /* Word4 parameter copied in the memcpy */
6081 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6082 /* word6 context tag copied in memcpy */
6083 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6084 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6086 "2015 Invalid CT %x command 0x%x\n",
6087 ct, iocbq->iocb.ulpCommand);
6088 return IOCB_ERROR;
6089 }
6090 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6091 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6092 iocbq->iocb.ulpTimeout);
6093
6094 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6095 command_type = OTHER_COMMAND;
6096 break;
6097 case CMD_XMIT_ELS_RSP64_CX:
6098 /* words0-2 BDE memcpy */
6099 /* word3 iocb=iotag32 wqe=rsvd */
6100 wqe->words[3] = 0;
6101 /* word4 iocb=did wge=rsvd. */
6102 wqe->words[4] = 0;
6103 /* word5 iocb=rsvd wge=did */
6104 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6105 iocbq->iocb.un.elsreq64.remoteID);
6106
6107 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6108 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6109
6110 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6111 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6112 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6113 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6114 iocbq->vport->vpi + phba->vpi_base);
6115 command_type = OTHER_COMMAND;
6116 break;
6117 case CMD_CLOSE_XRI_CN:
6118 case CMD_ABORT_XRI_CN:
6119 case CMD_ABORT_XRI_CX:
6120 /* words 0-2 memcpy should be 0 rserved */
6121 /* port will send abts */
6122 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6123 /*
6124 * The link is down so the fw does not need to send abts
6125 * on the wire.
6126 */
6127 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6128 else
6129 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6130 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006131 wqe->words[5] = 0;
6132 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6133 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6134 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006135 /*
6136 * The abort handler will send us CMD_ABORT_XRI_CN or
6137 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6138 */
6139 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6140 cmnd = CMD_ABORT_XRI_CX;
6141 command_type = OTHER_COMMAND;
6142 xritag = 0;
6143 break;
James Smart6669f9b2009-10-02 15:16:45 -04006144 case CMD_XMIT_BLS_RSP64_CX:
6145 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6146 * we re-construct this WQE here based on information in
6147 * iocbq from scratch.
6148 */
6149 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006150 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006151 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006152 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6153 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6154 LPFC_ABTS_UNSOL_INT) {
6155 /* ABTS sent by initiator to CT exchange, the
6156 * RX_ID field will be filled with the newly
6157 * allocated responder XRI.
6158 */
6159 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6160 iocbq->sli4_xritag);
6161 } else {
6162 /* ABTS sent by responder to CT exchange, the
6163 * RX_ID field will be filled with the responder
6164 * RX_ID from ABTS.
6165 */
6166 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6167 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6168 }
James Smart6669f9b2009-10-02 15:16:45 -04006169 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6170 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6171 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6172 iocbq->iocb.ulpContext);
6173 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6174 command_type = OTHER_COMMAND;
6175 break;
James Smart4f774512009-05-22 14:52:35 -04006176 case CMD_XRI_ABORTED_CX:
6177 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6178 /* words0-2 are all 0's no bde */
6179 /* word3 and word4 are rsvrd */
6180 wqe->words[3] = 0;
6181 wqe->words[4] = 0;
6182 /* word5 iocb=rsvd wge=did */
6183 /* There is no remote port id in the IOCB? */
6184 /* Let this fall through and fail */
6185 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6186 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6187 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6188 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6189 default:
6190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6191 "2014 Invalid command 0x%x\n",
6192 iocbq->iocb.ulpCommand);
6193 return IOCB_ERROR;
6194 break;
6195
6196 }
6197 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6198 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6199 wqe->generic.abort_tag = abort_tag;
6200 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6201 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6202 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6203 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6204
6205 return 0;
6206}
6207
6208/**
6209 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6210 * @phba: Pointer to HBA context object.
6211 * @ring_number: SLI ring number to issue iocb on.
6212 * @piocb: Pointer to command iocb.
6213 * @flag: Flag indicating if this command can be put into txq.
6214 *
6215 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6216 * an iocb command to an HBA with SLI-4 interface spec.
6217 *
6218 * This function is called with hbalock held. The function will return success
6219 * after it successfully submit the iocb to firmware or after adding to the
6220 * txq.
6221 **/
6222static int
6223__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6224 struct lpfc_iocbq *piocb, uint32_t flag)
6225{
6226 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006227 union lpfc_wqe wqe;
6228 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006229
6230 if (piocb->sli4_xritag == NO_XRI) {
6231 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006232 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006233 sglq = NULL;
6234 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006235 if (pring->txq_cnt) {
6236 if (!(flag & SLI_IOCB_RET_IOCB)) {
6237 __lpfc_sli_ringtx_put(phba,
6238 pring, piocb);
6239 return IOCB_SUCCESS;
6240 } else {
6241 return IOCB_BUSY;
6242 }
6243 } else {
James Smart4f774512009-05-22 14:52:35 -04006244 sglq = __lpfc_sli_get_sglq(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04006245 if (!sglq) {
6246 if (!(flag & SLI_IOCB_RET_IOCB)) {
6247 __lpfc_sli_ringtx_put(phba,
6248 pring,
6249 piocb);
6250 return IOCB_SUCCESS;
6251 } else
6252 return IOCB_BUSY;
6253 }
6254 }
James Smart4f774512009-05-22 14:52:35 -04006255 }
6256 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6257 sglq = NULL; /* These IO's already have an XRI and
6258 * a mapped sgl.
6259 */
6260 } else {
6261 /* This is a continuation of a commandi,(CX) so this
6262 * sglq is on the active list
6263 */
6264 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6265 if (!sglq)
6266 return IOCB_ERROR;
6267 }
6268
6269 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006270 piocb->sli4_xritag = sglq->sli4_xritag;
6271
6272 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006273 return IOCB_ERROR;
6274 }
6275
6276 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6277 return IOCB_ERROR;
6278
James Smart341af102010-01-26 23:07:37 -05006279 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6280 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006281 /*
6282 * For FCP command IOCB, get a new WQ index to distribute
6283 * WQE across the WQsr. On the other hand, for abort IOCB,
6284 * it carries the same WQ index to the original command
6285 * IOCB.
6286 */
James Smart341af102010-01-26 23:07:37 -05006287 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006288 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6289 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6290 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006291 return IOCB_ERROR;
6292 } else {
6293 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6294 return IOCB_ERROR;
6295 }
6296 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6297
6298 return 0;
6299}
6300
6301/**
James Smart3772a992009-05-22 14:50:54 -04006302 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6303 *
6304 * This routine wraps the actual lockless version for issusing IOCB function
6305 * pointer from the lpfc_hba struct.
6306 *
6307 * Return codes:
6308 * IOCB_ERROR - Error
6309 * IOCB_SUCCESS - Success
6310 * IOCB_BUSY - Busy
6311 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006312int
James Smart3772a992009-05-22 14:50:54 -04006313__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6314 struct lpfc_iocbq *piocb, uint32_t flag)
6315{
6316 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6317}
6318
6319/**
6320 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6321 * @phba: The hba struct for which this call is being executed.
6322 * @dev_grp: The HBA PCI-Device group number.
6323 *
6324 * This routine sets up the SLI interface API function jump table in @phba
6325 * struct.
6326 * Returns: 0 - success, -ENODEV - failure.
6327 **/
6328int
6329lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6330{
6331
6332 switch (dev_grp) {
6333 case LPFC_PCI_DEV_LP:
6334 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6335 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6336 break;
James Smart4f774512009-05-22 14:52:35 -04006337 case LPFC_PCI_DEV_OC:
6338 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6339 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6340 break;
James Smart3772a992009-05-22 14:50:54 -04006341 default:
6342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6343 "1419 Invalid HBA PCI-device group: 0x%x\n",
6344 dev_grp);
6345 return -ENODEV;
6346 break;
6347 }
6348 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6349 return 0;
6350}
James Smart92d7f7b2007-06-17 19:56:38 -05006351
James Smarte59058c2008-08-24 21:49:00 -04006352/**
James Smart3621a712009-04-06 18:47:14 -04006353 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006354 * @phba: Pointer to HBA context object.
6355 * @pring: Pointer to driver SLI ring object.
6356 * @piocb: Pointer to command iocb.
6357 * @flag: Flag indicating if this command can be put into txq.
6358 *
6359 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6360 * function. This function gets the hbalock and calls
6361 * __lpfc_sli_issue_iocb function and will return the error returned
6362 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6363 * functions which do not hold hbalock.
6364 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006365int
James Smart3772a992009-05-22 14:50:54 -04006366lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006367 struct lpfc_iocbq *piocb, uint32_t flag)
6368{
6369 unsigned long iflags;
6370 int rc;
6371
6372 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006373 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006374 spin_unlock_irqrestore(&phba->hbalock, iflags);
6375
6376 return rc;
6377}
6378
James Smarte59058c2008-08-24 21:49:00 -04006379/**
James Smart3621a712009-04-06 18:47:14 -04006380 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006381 * @phba: Pointer to HBA context object.
6382 *
6383 * This function is called while driver attaches with the
6384 * HBA to setup the extra ring. The extra ring is used
6385 * only when driver needs to support target mode functionality
6386 * or IP over FC functionalities.
6387 *
6388 * This function is called with no lock held.
6389 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006390static int
6391lpfc_extra_ring_setup( struct lpfc_hba *phba)
6392{
6393 struct lpfc_sli *psli;
6394 struct lpfc_sli_ring *pring;
6395
6396 psli = &phba->sli;
6397
6398 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006399
6400 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006401 pring = &psli->ring[psli->fcp_ring];
6402 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6403 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6404 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6405 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6406
James Smarta4bc3372006-12-02 13:34:16 -05006407 /* and give them to the extra ring */
6408 pring = &psli->ring[psli->extra_ring];
6409
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006410 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6411 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6412 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6413 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6414
6415 /* Setup default profile for this ring */
6416 pring->iotag_max = 4096;
6417 pring->num_mask = 1;
6418 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006419 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6420 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006421 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6422 return 0;
6423}
6424
James Smarte59058c2008-08-24 21:49:00 -04006425/**
James Smart3621a712009-04-06 18:47:14 -04006426 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006427 * @phba: Pointer to HBA context object.
6428 * @pring: Pointer to driver SLI ring object.
6429 * @iocbq: Pointer to iocb object.
6430 *
6431 * This function is called by the slow ring event handler
6432 * function when there is an ASYNC event iocb in the ring.
6433 * This function is called with no lock held.
6434 * Currently this function handles only temperature related
6435 * ASYNC events. The function decodes the temperature sensor
6436 * event message and posts events for the management applications.
6437 **/
James Smart98c9ea52007-10-27 13:37:33 -04006438static void
James Smart57127f12007-10-27 13:37:05 -04006439lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6440 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6441{
6442 IOCB_t *icmd;
6443 uint16_t evt_code;
6444 uint16_t temp;
6445 struct temp_event temp_event_data;
6446 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006447 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006448
6449 icmd = &iocbq->iocb;
6450 evt_code = icmd->un.asyncstat.evt_code;
6451 temp = icmd->ulpContext;
6452
6453 if ((evt_code != ASYNC_TEMP_WARN) &&
6454 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006455 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006456 lpfc_printf_log(phba,
6457 KERN_ERR,
6458 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006459 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006460 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006461 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6462 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6463 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6464 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006465 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006466 icmd->un.asyncstat.evt_code,
6467 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6468 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6469 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6470 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6471
James Smart57127f12007-10-27 13:37:05 -04006472 return;
6473 }
6474 temp_event_data.data = (uint32_t)temp;
6475 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6476 if (evt_code == ASYNC_TEMP_WARN) {
6477 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6478 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006479 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006480 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006481 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006482 "corrective action. temperature : %d Celsius\n",
6483 temp);
6484 }
6485 if (evt_code == ASYNC_TEMP_SAFE) {
6486 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6487 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006488 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006489 LOG_TEMP,
6490 "0340 Adapter temperature is OK now. "
6491 "temperature : %d Celsius\n",
6492 temp);
6493 }
6494
6495 /* Send temperature change event to applications */
6496 shost = lpfc_shost_from_vport(phba->pport);
6497 fc_host_post_vendor_event(shost, fc_get_event_number(),
6498 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006499 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006500
6501}
6502
6503
James Smarte59058c2008-08-24 21:49:00 -04006504/**
James Smart3621a712009-04-06 18:47:14 -04006505 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006506 * @phba: Pointer to HBA context object.
6507 *
6508 * lpfc_sli_setup sets up rings of the SLI interface with
6509 * number of iocbs per ring and iotags. This function is
6510 * called while driver attach to the HBA and before the
6511 * interrupts are enabled. So there is no need for locking.
6512 *
6513 * This function always returns 0.
6514 **/
dea31012005-04-17 16:05:31 -05006515int
6516lpfc_sli_setup(struct lpfc_hba *phba)
6517{
James Smarted957682007-06-17 19:56:37 -05006518 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006519 struct lpfc_sli *psli = &phba->sli;
6520 struct lpfc_sli_ring *pring;
6521
6522 psli->num_rings = MAX_CONFIGURED_RINGS;
6523 psli->sli_flag = 0;
6524 psli->fcp_ring = LPFC_FCP_RING;
6525 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006526 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006527
James Bottomley604a3e32005-10-29 10:28:33 -05006528 psli->iocbq_lookup = NULL;
6529 psli->iocbq_lookup_len = 0;
6530 psli->last_iotag = 0;
6531
dea31012005-04-17 16:05:31 -05006532 for (i = 0; i < psli->num_rings; i++) {
6533 pring = &psli->ring[i];
6534 switch (i) {
6535 case LPFC_FCP_RING: /* ring 0 - FCP */
6536 /* numCiocb and numRiocb are used in config_port */
6537 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6538 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6539 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6540 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6541 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6542 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006543 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006544 SLI3_IOCB_CMD_SIZE :
6545 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006546 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006547 SLI3_IOCB_RSP_SIZE :
6548 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006549 pring->iotag_ctr = 0;
6550 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006551 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006552 pring->fast_iotag = pring->iotag_max;
6553 pring->num_mask = 0;
6554 break;
James Smarta4bc3372006-12-02 13:34:16 -05006555 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006556 /* numCiocb and numRiocb are used in config_port */
6557 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6558 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006559 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006560 SLI3_IOCB_CMD_SIZE :
6561 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006562 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006563 SLI3_IOCB_RSP_SIZE :
6564 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006565 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006566 pring->num_mask = 0;
6567 break;
6568 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6569 /* numCiocb and numRiocb are used in config_port */
6570 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6571 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006572 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006573 SLI3_IOCB_CMD_SIZE :
6574 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006575 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006576 SLI3_IOCB_RSP_SIZE :
6577 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006578 pring->fast_iotag = 0;
6579 pring->iotag_ctr = 0;
6580 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006581 pring->lpfc_sli_rcv_async_status =
6582 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006583 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006584 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006585 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6586 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006587 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006588 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006589 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006590 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6591 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006592 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006593 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006594 pring->prt[2].profile = 0; /* Mask 2 */
6595 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006596 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006597 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006598 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006599 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006600 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006601 pring->prt[3].profile = 0; /* Mask 3 */
6602 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006603 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006604 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006605 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006606 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006607 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006608 /* abort unsolicited sequence */
6609 pring->prt[4].profile = 0; /* Mask 4 */
6610 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6611 pring->prt[4].type = FC_TYPE_BLS;
6612 pring->prt[4].lpfc_sli_rcv_unsol_event =
6613 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006614 break;
6615 }
James Smarted957682007-06-17 19:56:37 -05006616 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006617 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006618 }
James Smarted957682007-06-17 19:56:37 -05006619 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006620 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006621 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6622 "SLI2 SLIM Data: x%x x%lx\n",
6623 phba->brd_no, totiocbsize,
6624 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006625 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006626 if (phba->cfg_multi_ring_support == 2)
6627 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006628
6629 return 0;
6630}
6631
James Smarte59058c2008-08-24 21:49:00 -04006632/**
James Smart3621a712009-04-06 18:47:14 -04006633 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006634 * @phba: Pointer to HBA context object.
6635 *
6636 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6637 * ring. This function also initializes ring indices of each ring.
6638 * This function is called during the initialization of the SLI
6639 * interface of an HBA.
6640 * This function is called with no lock held and always returns
6641 * 1.
6642 **/
dea31012005-04-17 16:05:31 -05006643int
James Smart2e0fef82007-06-17 19:56:36 -05006644lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006645{
6646 struct lpfc_sli *psli;
6647 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006648 int i;
dea31012005-04-17 16:05:31 -05006649
6650 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006651 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006652 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006653 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006654 /* Initialize list headers for txq and txcmplq as double linked lists */
6655 for (i = 0; i < psli->num_rings; i++) {
6656 pring = &psli->ring[i];
6657 pring->ringno = i;
6658 pring->next_cmdidx = 0;
6659 pring->local_getidx = 0;
6660 pring->cmdidx = 0;
6661 INIT_LIST_HEAD(&pring->txq);
6662 INIT_LIST_HEAD(&pring->txcmplq);
6663 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006664 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006665 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006666 }
James Smart2e0fef82007-06-17 19:56:36 -05006667 spin_unlock_irq(&phba->hbalock);
6668 return 1;
dea31012005-04-17 16:05:31 -05006669}
6670
James Smarte59058c2008-08-24 21:49:00 -04006671/**
James Smart04c68492009-05-22 14:52:52 -04006672 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6673 * @phba: Pointer to HBA context object.
6674 *
6675 * This routine flushes the mailbox command subsystem. It will unconditionally
6676 * flush all the mailbox commands in the three possible stages in the mailbox
6677 * command sub-system: pending mailbox command queue; the outstanding mailbox
6678 * command; and completed mailbox command queue. It is caller's responsibility
6679 * to make sure that the driver is in the proper state to flush the mailbox
6680 * command sub-system. Namely, the posting of mailbox commands into the
6681 * pending mailbox command queue from the various clients must be stopped;
6682 * either the HBA is in a state that it will never works on the outstanding
6683 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6684 * mailbox command has been completed.
6685 **/
6686static void
6687lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6688{
6689 LIST_HEAD(completions);
6690 struct lpfc_sli *psli = &phba->sli;
6691 LPFC_MBOXQ_t *pmb;
6692 unsigned long iflag;
6693
6694 /* Flush all the mailbox commands in the mbox system */
6695 spin_lock_irqsave(&phba->hbalock, iflag);
6696 /* The pending mailbox command queue */
6697 list_splice_init(&phba->sli.mboxq, &completions);
6698 /* The outstanding active mailbox command */
6699 if (psli->mbox_active) {
6700 list_add_tail(&psli->mbox_active->list, &completions);
6701 psli->mbox_active = NULL;
6702 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6703 }
6704 /* The completed mailbox command queue */
6705 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6706 spin_unlock_irqrestore(&phba->hbalock, iflag);
6707
6708 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6709 while (!list_empty(&completions)) {
6710 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6711 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6712 if (pmb->mbox_cmpl)
6713 pmb->mbox_cmpl(phba, pmb);
6714 }
6715}
6716
6717/**
James Smart3621a712009-04-06 18:47:14 -04006718 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006719 * @vport: Pointer to virtual port object.
6720 *
6721 * lpfc_sli_host_down is called to clean up the resources
6722 * associated with a vport before destroying virtual
6723 * port data structures.
6724 * This function does following operations:
6725 * - Free discovery resources associated with this virtual
6726 * port.
6727 * - Free iocbs associated with this virtual port in
6728 * the txq.
6729 * - Send abort for all iocb commands associated with this
6730 * vport in txcmplq.
6731 *
6732 * This function is called with no lock held and always returns 1.
6733 **/
dea31012005-04-17 16:05:31 -05006734int
James Smart92d7f7b2007-06-17 19:56:38 -05006735lpfc_sli_host_down(struct lpfc_vport *vport)
6736{
James Smart858c9f62007-06-17 19:56:39 -05006737 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006738 struct lpfc_hba *phba = vport->phba;
6739 struct lpfc_sli *psli = &phba->sli;
6740 struct lpfc_sli_ring *pring;
6741 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006742 int i;
6743 unsigned long flags = 0;
6744 uint16_t prev_pring_flag;
6745
6746 lpfc_cleanup_discovery_resources(vport);
6747
6748 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006749 for (i = 0; i < psli->num_rings; i++) {
6750 pring = &psli->ring[i];
6751 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006752 /* Only slow rings */
6753 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006754 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006755 /* Set the lpfc data pending flag */
6756 set_bit(LPFC_DATA_READY, &phba->data_flags);
6757 }
James Smart92d7f7b2007-06-17 19:56:38 -05006758 /*
6759 * Error everything on the txq since these iocbs have not been
6760 * given to the FW yet.
6761 */
James Smart92d7f7b2007-06-17 19:56:38 -05006762 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6763 if (iocb->vport != vport)
6764 continue;
James Smart858c9f62007-06-17 19:56:39 -05006765 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006766 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006767 }
6768
6769 /* Next issue ABTS for everything on the txcmplq */
6770 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6771 list) {
6772 if (iocb->vport != vport)
6773 continue;
6774 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6775 }
6776
6777 pring->flag = prev_pring_flag;
6778 }
6779
6780 spin_unlock_irqrestore(&phba->hbalock, flags);
6781
James Smarta257bf92009-04-06 18:48:10 -04006782 /* Cancel all the IOCBs from the completions list */
6783 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6784 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006785 return 1;
6786}
6787
James Smarte59058c2008-08-24 21:49:00 -04006788/**
James Smart3621a712009-04-06 18:47:14 -04006789 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006790 * @phba: Pointer to HBA context object.
6791 *
6792 * This function cleans up all iocb, buffers, mailbox commands
6793 * while shutting down the HBA. This function is called with no
6794 * lock held and always returns 1.
6795 * This function does the following to cleanup driver resources:
6796 * - Free discovery resources for each virtual port
6797 * - Cleanup any pending fabric iocbs
6798 * - Iterate through the iocb txq and free each entry
6799 * in the list.
6800 * - Free up any buffer posted to the HBA
6801 * - Free mailbox commands in the mailbox queue.
6802 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006803int
James Smart2e0fef82007-06-17 19:56:36 -05006804lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006805{
James Smart2534ba72007-04-25 09:52:20 -04006806 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006807 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006808 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006809 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006810 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006811 int i;
6812
6813 /* Shutdown the mailbox command sub-system */
6814 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006815
dea31012005-04-17 16:05:31 -05006816 lpfc_hba_down_prep(phba);
6817
James Smart92d7f7b2007-06-17 19:56:38 -05006818 lpfc_fabric_abort_hba(phba);
6819
James Smart2e0fef82007-06-17 19:56:36 -05006820 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006821 for (i = 0; i < psli->num_rings; i++) {
6822 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006823 /* Only slow rings */
6824 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006825 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006826 /* Set the lpfc data pending flag */
6827 set_bit(LPFC_DATA_READY, &phba->data_flags);
6828 }
dea31012005-04-17 16:05:31 -05006829
6830 /*
6831 * Error everything on the txq since these iocbs have not been
6832 * given to the FW yet.
6833 */
James Smart2534ba72007-04-25 09:52:20 -04006834 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006835 pring->txq_cnt = 0;
6836
dea31012005-04-17 16:05:31 -05006837 }
James Smart2e0fef82007-06-17 19:56:36 -05006838 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006839
James Smarta257bf92009-04-06 18:48:10 -04006840 /* Cancel all the IOCBs from the completions list */
6841 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6842 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006843
James Smart0ff10d42008-01-11 01:52:36 -05006844 spin_lock_irqsave(&phba->hbalock, flags);
6845 list_splice_init(&phba->elsbuf, &completions);
6846 phba->elsbuf_cnt = 0;
6847 phba->elsbuf_prev_cnt = 0;
6848 spin_unlock_irqrestore(&phba->hbalock, flags);
6849
6850 while (!list_empty(&completions)) {
6851 list_remove_head(&completions, buf_ptr,
6852 struct lpfc_dmabuf, list);
6853 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6854 kfree(buf_ptr);
6855 }
6856
dea31012005-04-17 16:05:31 -05006857 /* Return any active mbox cmds */
6858 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006859
James Smartda0436e2009-05-22 14:51:39 -04006860 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006861 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006862 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006863
James Smartda0436e2009-05-22 14:51:39 -04006864 return 1;
6865}
James Smart92d7f7b2007-06-17 19:56:38 -05006866
James Smartda0436e2009-05-22 14:51:39 -04006867/**
6868 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6869 * @phba: Pointer to HBA context object.
6870 *
6871 * This function cleans up all queues, iocb, buffers, mailbox commands while
6872 * shutting down the SLI4 HBA FCoE function. This function is called with no
6873 * lock held and always returns 1.
6874 *
6875 * This function does the following to cleanup driver FCoE function resources:
6876 * - Free discovery resources for each virtual port
6877 * - Cleanup any pending fabric iocbs
6878 * - Iterate through the iocb txq and free each entry in the list.
6879 * - Free up any buffer posted to the HBA.
6880 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6881 * - Free mailbox commands in the mailbox queue.
6882 **/
6883int
6884lpfc_sli4_hba_down(struct lpfc_hba *phba)
6885{
6886 /* Stop the SLI4 device port */
6887 lpfc_stop_port(phba);
6888
6889 /* Tear down the queues in the HBA */
6890 lpfc_sli4_queue_unset(phba);
6891
6892 /* unregister default FCFI from the HBA */
6893 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6894
dea31012005-04-17 16:05:31 -05006895 return 1;
6896}
6897
James Smarte59058c2008-08-24 21:49:00 -04006898/**
James Smart3621a712009-04-06 18:47:14 -04006899 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006900 * @srcp: Source memory pointer.
6901 * @destp: Destination memory pointer.
6902 * @cnt: Number of words required to be copied.
6903 *
6904 * This function is used for copying data between driver memory
6905 * and the SLI memory. This function also changes the endianness
6906 * of each word if native endianness is different from SLI
6907 * endianness. This function can be called with or without
6908 * lock.
6909 **/
dea31012005-04-17 16:05:31 -05006910void
6911lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6912{
6913 uint32_t *src = srcp;
6914 uint32_t *dest = destp;
6915 uint32_t ldata;
6916 int i;
6917
6918 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6919 ldata = *src;
6920 ldata = le32_to_cpu(ldata);
6921 *dest = ldata;
6922 src++;
6923 dest++;
6924 }
6925}
6926
James Smarte59058c2008-08-24 21:49:00 -04006927
6928/**
James Smarta0c87cb2009-07-19 10:01:10 -04006929 * lpfc_sli_bemem_bcopy - SLI memory copy function
6930 * @srcp: Source memory pointer.
6931 * @destp: Destination memory pointer.
6932 * @cnt: Number of words required to be copied.
6933 *
6934 * This function is used for copying data between a data structure
6935 * with big endian representation to local endianness.
6936 * This function can be called with or without lock.
6937 **/
6938void
6939lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6940{
6941 uint32_t *src = srcp;
6942 uint32_t *dest = destp;
6943 uint32_t ldata;
6944 int i;
6945
6946 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6947 ldata = *src;
6948 ldata = be32_to_cpu(ldata);
6949 *dest = ldata;
6950 src++;
6951 dest++;
6952 }
6953}
6954
6955/**
James Smart3621a712009-04-06 18:47:14 -04006956 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006957 * @phba: Pointer to HBA context object.
6958 * @pring: Pointer to driver SLI ring object.
6959 * @mp: Pointer to driver buffer object.
6960 *
6961 * This function is called with no lock held.
6962 * It always return zero after adding the buffer to the postbufq
6963 * buffer list.
6964 **/
dea31012005-04-17 16:05:31 -05006965int
James Smart2e0fef82007-06-17 19:56:36 -05006966lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6967 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006968{
6969 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6970 later */
James Smart2e0fef82007-06-17 19:56:36 -05006971 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006972 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006973 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006974 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006975 return 0;
6976}
6977
James Smarte59058c2008-08-24 21:49:00 -04006978/**
James Smart3621a712009-04-06 18:47:14 -04006979 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006980 * @phba: Pointer to HBA context object.
6981 *
6982 * When HBQ is enabled, buffers are searched based on tags. This function
6983 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6984 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6985 * does not conflict with tags of buffer posted for unsolicited events.
6986 * The function returns the allocated tag. The function is called with
6987 * no locks held.
6988 **/
James Smart76bb24e2007-10-27 13:38:00 -04006989uint32_t
6990lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6991{
6992 spin_lock_irq(&phba->hbalock);
6993 phba->buffer_tag_count++;
6994 /*
6995 * Always set the QUE_BUFTAG_BIT to distiguish between
6996 * a tag assigned by HBQ.
6997 */
6998 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6999 spin_unlock_irq(&phba->hbalock);
7000 return phba->buffer_tag_count;
7001}
7002
James Smarte59058c2008-08-24 21:49:00 -04007003/**
James Smart3621a712009-04-06 18:47:14 -04007004 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007005 * @phba: Pointer to HBA context object.
7006 * @pring: Pointer to driver SLI ring object.
7007 * @tag: Buffer tag.
7008 *
7009 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7010 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7011 * iocb is posted to the response ring with the tag of the buffer.
7012 * This function searches the pring->postbufq list using the tag
7013 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7014 * iocb. If the buffer is found then lpfc_dmabuf object of the
7015 * buffer is returned to the caller else NULL is returned.
7016 * This function is called with no lock held.
7017 **/
James Smart76bb24e2007-10-27 13:38:00 -04007018struct lpfc_dmabuf *
7019lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7020 uint32_t tag)
7021{
7022 struct lpfc_dmabuf *mp, *next_mp;
7023 struct list_head *slp = &pring->postbufq;
7024
7025 /* Search postbufq, from the begining, looking for a match on tag */
7026 spin_lock_irq(&phba->hbalock);
7027 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7028 if (mp->buffer_tag == tag) {
7029 list_del_init(&mp->list);
7030 pring->postbufq_cnt--;
7031 spin_unlock_irq(&phba->hbalock);
7032 return mp;
7033 }
7034 }
7035
7036 spin_unlock_irq(&phba->hbalock);
7037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007038 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007039 "ring %d Data x%lx x%p x%p x%x\n",
7040 pring->ringno, (unsigned long) tag,
7041 slp->next, slp->prev, pring->postbufq_cnt);
7042
7043 return NULL;
7044}
dea31012005-04-17 16:05:31 -05007045
James Smarte59058c2008-08-24 21:49:00 -04007046/**
James Smart3621a712009-04-06 18:47:14 -04007047 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007048 * @phba: Pointer to HBA context object.
7049 * @pring: Pointer to driver SLI ring object.
7050 * @phys: DMA address of the buffer.
7051 *
7052 * This function searches the buffer list using the dma_address
7053 * of unsolicited event to find the driver's lpfc_dmabuf object
7054 * corresponding to the dma_address. The function returns the
7055 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7056 * This function is called by the ct and els unsolicited event
7057 * handlers to get the buffer associated with the unsolicited
7058 * event.
7059 *
7060 * This function is called with no lock held.
7061 **/
dea31012005-04-17 16:05:31 -05007062struct lpfc_dmabuf *
7063lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7064 dma_addr_t phys)
7065{
7066 struct lpfc_dmabuf *mp, *next_mp;
7067 struct list_head *slp = &pring->postbufq;
7068
7069 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007070 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007071 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7072 if (mp->phys == phys) {
7073 list_del_init(&mp->list);
7074 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007075 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007076 return mp;
7077 }
7078 }
7079
James Smart2e0fef82007-06-17 19:56:36 -05007080 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007082 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007083 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007084 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007085 slp->next, slp->prev, pring->postbufq_cnt);
7086 return NULL;
7087}
7088
James Smarte59058c2008-08-24 21:49:00 -04007089/**
James Smart3621a712009-04-06 18:47:14 -04007090 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007091 * @phba: Pointer to HBA context object.
7092 * @cmdiocb: Pointer to driver command iocb object.
7093 * @rspiocb: Pointer to driver response iocb object.
7094 *
7095 * This function is the completion handler for the abort iocbs for
7096 * ELS commands. This function is called from the ELS ring event
7097 * handler with no lock held. This function frees memory resources
7098 * associated with the abort iocb.
7099 **/
dea31012005-04-17 16:05:31 -05007100static void
James Smart2e0fef82007-06-17 19:56:36 -05007101lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7102 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007103{
James Smart2e0fef82007-06-17 19:56:36 -05007104 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007105 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007106 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007107 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7108
7109 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007110
7111 if (irsp->ulpStatus) {
7112 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7113 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7114
James Smart2e0fef82007-06-17 19:56:36 -05007115 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007116 if (phba->sli_rev < LPFC_SLI_REV4) {
7117 if (abort_iotag != 0 &&
7118 abort_iotag <= phba->sli.last_iotag)
7119 abort_iocb =
7120 phba->sli.iocbq_lookup[abort_iotag];
7121 } else
7122 /* For sli4 the abort_tag is the XRI,
7123 * so the abort routine puts the iotag of the iocb
7124 * being aborted in the context field of the abort
7125 * IOCB.
7126 */
7127 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007128
James Smart2680eea2007-04-25 09:52:55 -04007129 /*
James Smart58da1ff2008-04-07 10:15:56 -04007130 * If the iocb is not found in Firmware queue the iocb
7131 * might have completed already. Do not free it again.
7132 */
James Smart9b379602008-04-07 10:16:00 -04007133 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007134 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7135 spin_unlock_irq(&phba->hbalock);
7136 lpfc_sli_release_iocbq(phba, cmdiocb);
7137 return;
7138 }
7139 /* For SLI4 the ulpContext field for abort IOCB
7140 * holds the iotag of the IOCB being aborted so
7141 * the local abort_context needs to be reset to
7142 * match the aborted IOCBs ulpContext.
7143 */
7144 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7145 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007146 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007147
7148 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7149 "0327 Cannot abort els iocb %p "
7150 "with tag %x context %x, abort status %x, "
7151 "abort code %x\n",
7152 abort_iocb, abort_iotag, abort_context,
7153 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007154 /*
James Smart2680eea2007-04-25 09:52:55 -04007155 * make sure we have the right iocbq before taking it
7156 * off the txcmplq and try to call completion routine.
7157 */
James Smart2e0fef82007-06-17 19:56:36 -05007158 if (!abort_iocb ||
7159 abort_iocb->iocb.ulpContext != abort_context ||
7160 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7161 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007162 else if (phba->sli_rev < LPFC_SLI_REV4) {
7163 /*
7164 * leave the SLI4 aborted command on the txcmplq
7165 * list and the command complete WCQE's XB bit
7166 * will tell whether the SGL (XRI) can be released
7167 * immediately or to the aborted SGL list for the
7168 * following abort XRI from the HBA.
7169 */
James Smart92d7f7b2007-06-17 19:56:38 -05007170 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007171 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7172 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7173 pring->txcmplq_cnt--;
7174 }
James Smart2680eea2007-04-25 09:52:55 -04007175
James Smart0ff10d42008-01-11 01:52:36 -05007176 /* Firmware could still be in progress of DMAing
7177 * payload, so don't free data buffer till after
7178 * a hbeat.
7179 */
7180 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007181 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007182 spin_unlock_irq(&phba->hbalock);
7183
James Smart92d7f7b2007-06-17 19:56:38 -05007184 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007185 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007186 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007187 } else
7188 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007189 }
7190
James Bottomley604a3e32005-10-29 10:28:33 -05007191 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007192 return;
7193}
7194
James Smarte59058c2008-08-24 21:49:00 -04007195/**
James Smart3621a712009-04-06 18:47:14 -04007196 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007197 * @phba: Pointer to HBA context object.
7198 * @cmdiocb: Pointer to driver command iocb object.
7199 * @rspiocb: Pointer to driver response iocb object.
7200 *
7201 * The function is called from SLI ring event handler with no
7202 * lock held. This function is the completion handler for ELS commands
7203 * which are aborted. The function frees memory resources used for
7204 * the aborted ELS commands.
7205 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007206static void
7207lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7208 struct lpfc_iocbq *rspiocb)
7209{
7210 IOCB_t *irsp = &rspiocb->iocb;
7211
7212 /* ELS cmd tag <ulpIoTag> completes */
7213 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007214 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007215 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007216 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007217 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007218 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7219 lpfc_ct_free_iocb(phba, cmdiocb);
7220 else
7221 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007222 return;
7223}
7224
James Smarte59058c2008-08-24 21:49:00 -04007225/**
James Smart3621a712009-04-06 18:47:14 -04007226 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007227 * @phba: Pointer to HBA context object.
7228 * @pring: Pointer to driver SLI ring object.
7229 * @cmdiocb: Pointer to driver command iocb object.
7230 *
7231 * This function issues an abort iocb for the provided command
7232 * iocb. This function is called with hbalock held.
7233 * The function returns 0 when it fails due to memory allocation
7234 * failure or when the command iocb is an abort request.
7235 **/
dea31012005-04-17 16:05:31 -05007236int
James Smart2e0fef82007-06-17 19:56:36 -05007237lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7238 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007239{
James Smart2e0fef82007-06-17 19:56:36 -05007240 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007241 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007242 IOCB_t *icmd = NULL;
7243 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007244 int retval = IOCB_ERROR;
7245
James Smart92d7f7b2007-06-17 19:56:38 -05007246 /*
7247 * There are certain command types we don't want to abort. And we
7248 * don't want to abort commands that are already in the process of
7249 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007250 */
7251 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007252 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007253 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7254 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007255 return 0;
7256
James Smart858c9f62007-06-17 19:56:39 -05007257 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7258 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007259 */
James Smart858c9f62007-06-17 19:56:39 -05007260 if ((vport->load_flag & FC_UNLOADING) &&
7261 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007262 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7263 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7264 else
7265 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007266 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007267 }
dea31012005-04-17 16:05:31 -05007268
7269 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007270 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007271 if (abtsiocbp == NULL)
7272 return 0;
dea31012005-04-17 16:05:31 -05007273
James Smart07951072007-04-25 09:51:38 -04007274 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007275 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007276 */
7277 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7278
dea31012005-04-17 16:05:31 -05007279 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007280 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7281 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007282 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007283 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007284 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7285 }
James Smartda0436e2009-05-22 14:51:39 -04007286 else
7287 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007288 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007289 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007290
James Smart5ffc2662009-11-18 15:39:44 -05007291 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7292 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007293 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7294 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007295
James Smart2e0fef82007-06-17 19:56:36 -05007296 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007297 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7298 else
7299 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7300
7301 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007302
James Smarte8b62012007-08-02 11:10:09 -04007303 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7304 "0339 Abort xri x%x, original iotag x%x, "
7305 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007306 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007307 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007308 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007309 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007310
James Smartd7c255b2008-08-24 21:50:00 -04007311 if (retval)
7312 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007313abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007314 /*
7315 * Caller to this routine should check for IOCB_ERROR
7316 * and handle it properly. This routine no longer removes
7317 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007318 */
James Smart2e0fef82007-06-17 19:56:36 -05007319 return retval;
dea31012005-04-17 16:05:31 -05007320}
7321
James Smarte59058c2008-08-24 21:49:00 -04007322/**
James Smart3621a712009-04-06 18:47:14 -04007323 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007324 * @iocbq: Pointer to driver iocb object.
7325 * @vport: Pointer to driver virtual port object.
7326 * @tgt_id: SCSI ID of the target.
7327 * @lun_id: LUN ID of the scsi device.
7328 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7329 *
James Smart3621a712009-04-06 18:47:14 -04007330 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007331 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7332 * 0 if the filtering criteria is met for the given iocb and will return
7333 * 1 if the filtering criteria is not met.
7334 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7335 * given iocb is for the SCSI device specified by vport, tgt_id and
7336 * lun_id parameter.
7337 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7338 * given iocb is for the SCSI target specified by vport and tgt_id
7339 * parameters.
7340 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7341 * given iocb is for the SCSI host associated with the given vport.
7342 * This function is called with no locks held.
7343 **/
dea31012005-04-17 16:05:31 -05007344static int
James Smart51ef4c22007-08-02 11:10:31 -04007345lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7346 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007347 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007348{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007349 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007350 int rc = 1;
7351
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007352 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7353 return rc;
7354
James Smart51ef4c22007-08-02 11:10:31 -04007355 if (iocbq->vport != vport)
7356 return rc;
7357
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007358 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007359
James Smart495a7142008-06-14 22:52:59 -04007360 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007361 return rc;
7362
7363 switch (ctx_cmd) {
7364 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007365 if ((lpfc_cmd->rdata->pnode) &&
7366 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7367 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007368 rc = 0;
7369 break;
7370 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007371 if ((lpfc_cmd->rdata->pnode) &&
7372 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007373 rc = 0;
7374 break;
dea31012005-04-17 16:05:31 -05007375 case LPFC_CTX_HOST:
7376 rc = 0;
7377 break;
7378 default:
7379 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007380 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007381 break;
7382 }
7383
7384 return rc;
7385}
7386
James Smarte59058c2008-08-24 21:49:00 -04007387/**
James Smart3621a712009-04-06 18:47:14 -04007388 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007389 * @vport: Pointer to virtual port.
7390 * @tgt_id: SCSI ID of the target.
7391 * @lun_id: LUN ID of the scsi device.
7392 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7393 *
7394 * This function returns number of FCP commands pending for the vport.
7395 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7396 * commands pending on the vport associated with SCSI device specified
7397 * by tgt_id and lun_id parameters.
7398 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7399 * commands pending on the vport associated with SCSI target specified
7400 * by tgt_id parameter.
7401 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7402 * commands pending on the vport.
7403 * This function returns the number of iocbs which satisfy the filter.
7404 * This function is called without any lock held.
7405 **/
dea31012005-04-17 16:05:31 -05007406int
James Smart51ef4c22007-08-02 11:10:31 -04007407lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7408 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007409{
James Smart51ef4c22007-08-02 11:10:31 -04007410 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007411 struct lpfc_iocbq *iocbq;
7412 int sum, i;
dea31012005-04-17 16:05:31 -05007413
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007414 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7415 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007416
James Smart51ef4c22007-08-02 11:10:31 -04007417 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7418 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007419 sum++;
dea31012005-04-17 16:05:31 -05007420 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007421
dea31012005-04-17 16:05:31 -05007422 return sum;
7423}
7424
James Smarte59058c2008-08-24 21:49:00 -04007425/**
James Smart3621a712009-04-06 18:47:14 -04007426 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007427 * @phba: Pointer to HBA context object
7428 * @cmdiocb: Pointer to command iocb object.
7429 * @rspiocb: Pointer to response iocb object.
7430 *
7431 * This function is called when an aborted FCP iocb completes. This
7432 * function is called by the ring event handler with no lock held.
7433 * This function frees the iocb.
7434 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007435void
James Smart2e0fef82007-06-17 19:56:36 -05007436lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7437 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007438{
James Bottomley604a3e32005-10-29 10:28:33 -05007439 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007440 return;
7441}
7442
James Smarte59058c2008-08-24 21:49:00 -04007443/**
James Smart3621a712009-04-06 18:47:14 -04007444 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007445 * @vport: Pointer to virtual port.
7446 * @pring: Pointer to driver SLI ring object.
7447 * @tgt_id: SCSI ID of the target.
7448 * @lun_id: LUN ID of the scsi device.
7449 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7450 *
7451 * This function sends an abort command for every SCSI command
7452 * associated with the given virtual port pending on the ring
7453 * filtered by lpfc_sli_validate_fcp_iocb function.
7454 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7455 * FCP iocbs associated with lun specified by tgt_id and lun_id
7456 * parameters
7457 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7458 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7459 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7460 * FCP iocbs associated with virtual port.
7461 * This function returns number of iocbs it failed to abort.
7462 * This function is called with no locks held.
7463 **/
dea31012005-04-17 16:05:31 -05007464int
James Smart51ef4c22007-08-02 11:10:31 -04007465lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7466 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007467{
James Smart51ef4c22007-08-02 11:10:31 -04007468 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007469 struct lpfc_iocbq *iocbq;
7470 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007471 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007472 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007473 int i;
dea31012005-04-17 16:05:31 -05007474
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007475 for (i = 1; i <= phba->sli.last_iotag; i++) {
7476 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007477
James Smart51ef4c22007-08-02 11:10:31 -04007478 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007479 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007480 continue;
7481
7482 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007483 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007484 if (abtsiocb == NULL) {
7485 errcnt++;
7486 continue;
7487 }
dea31012005-04-17 16:05:31 -05007488
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007489 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007490 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7491 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007492 if (phba->sli_rev == LPFC_SLI_REV4)
7493 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7494 else
7495 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007496 abtsiocb->iocb.ulpLe = 1;
7497 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007498 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007499
James Smart5ffc2662009-11-18 15:39:44 -05007500 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7501 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007502 if (iocbq->iocb_flag & LPFC_IO_FCP)
7503 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007504
James Smart2e0fef82007-06-17 19:56:36 -05007505 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007506 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7507 else
7508 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7509
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007510 /* Setup callback routine and issue the command. */
7511 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007512 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7513 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007514 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007515 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007516 errcnt++;
7517 continue;
7518 }
7519 }
7520
7521 return errcnt;
7522}
7523
James Smarte59058c2008-08-24 21:49:00 -04007524/**
James Smart3621a712009-04-06 18:47:14 -04007525 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007526 * @phba: Pointer to HBA context object.
7527 * @cmdiocbq: Pointer to command iocb.
7528 * @rspiocbq: Pointer to response iocb.
7529 *
7530 * This function is the completion handler for iocbs issued using
7531 * lpfc_sli_issue_iocb_wait function. This function is called by the
7532 * ring event handler function without any lock held. This function
7533 * can be called from both worker thread context and interrupt
7534 * context. This function also can be called from other thread which
7535 * cleans up the SLI layer objects.
7536 * This function copy the contents of the response iocb to the
7537 * response iocb memory object provided by the caller of
7538 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7539 * sleeps for the iocb completion.
7540 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007541static void
7542lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7543 struct lpfc_iocbq *cmdiocbq,
7544 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007545{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007546 wait_queue_head_t *pdone_q;
7547 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007548 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007549
James Smart2e0fef82007-06-17 19:56:36 -05007550 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007551 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7552 if (cmdiocbq->context2 && rspiocbq)
7553 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7554 &rspiocbq->iocb, sizeof(IOCB_t));
7555
James Smart0f65ff62010-02-26 14:14:23 -05007556 /* Set the exchange busy flag for task management commands */
7557 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7558 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7559 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7560 cur_iocbq);
7561 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7562 }
7563
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007564 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007565 if (pdone_q)
7566 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007567 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007568 return;
7569}
7570
James Smarte59058c2008-08-24 21:49:00 -04007571/**
James Smartd11e31d2009-06-10 17:23:06 -04007572 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7573 * @phba: Pointer to HBA context object..
7574 * @piocbq: Pointer to command iocb.
7575 * @flag: Flag to test.
7576 *
7577 * This routine grabs the hbalock and then test the iocb_flag to
7578 * see if the passed in flag is set.
7579 * Returns:
7580 * 1 if flag is set.
7581 * 0 if flag is not set.
7582 **/
7583static int
7584lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7585 struct lpfc_iocbq *piocbq, uint32_t flag)
7586{
7587 unsigned long iflags;
7588 int ret;
7589
7590 spin_lock_irqsave(&phba->hbalock, iflags);
7591 ret = piocbq->iocb_flag & flag;
7592 spin_unlock_irqrestore(&phba->hbalock, iflags);
7593 return ret;
7594
7595}
7596
7597/**
James Smart3621a712009-04-06 18:47:14 -04007598 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007599 * @phba: Pointer to HBA context object..
7600 * @pring: Pointer to sli ring.
7601 * @piocb: Pointer to command iocb.
7602 * @prspiocbq: Pointer to response iocb.
7603 * @timeout: Timeout in number of seconds.
7604 *
7605 * This function issues the iocb to firmware and waits for the
7606 * iocb to complete. If the iocb command is not
7607 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7608 * Caller should not free the iocb resources if this function
7609 * returns IOCB_TIMEDOUT.
7610 * The function waits for the iocb completion using an
7611 * non-interruptible wait.
7612 * This function will sleep while waiting for iocb completion.
7613 * So, this function should not be called from any context which
7614 * does not allow sleeping. Due to the same reason, this function
7615 * cannot be called with interrupt disabled.
7616 * This function assumes that the iocb completions occur while
7617 * this function sleep. So, this function cannot be called from
7618 * the thread which process iocb completion for this ring.
7619 * This function clears the iocb_flag of the iocb object before
7620 * issuing the iocb and the iocb completion handler sets this
7621 * flag and wakes this thread when the iocb completes.
7622 * The contents of the response iocb will be copied to prspiocbq
7623 * by the completion handler when the command completes.
7624 * This function returns IOCB_SUCCESS when success.
7625 * This function is called with no lock held.
7626 **/
dea31012005-04-17 16:05:31 -05007627int
James Smart2e0fef82007-06-17 19:56:36 -05007628lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007629 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007630 struct lpfc_iocbq *piocb,
7631 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007632 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007633{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007634 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007635 long timeleft, timeout_req = 0;
7636 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007637 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04007638 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05007639 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007640 * If the caller has provided a response iocbq buffer, then context2
7641 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007642 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007643 if (prspiocbq) {
7644 if (piocb->context2)
7645 return IOCB_ERROR;
7646 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007647 }
7648
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007649 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7650 piocb->context_un.wait_queue = &done_q;
7651 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007652
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007653 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7654 creg_val = readl(phba->HCregaddr);
7655 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7656 writel(creg_val, phba->HCregaddr);
7657 readl(phba->HCregaddr); /* flush */
7658 }
7659
James Smart2a9bf3d2010-06-07 15:24:45 -04007660 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7661 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007662 if (retval == IOCB_SUCCESS) {
7663 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007664 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007665 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007666 timeout_req);
dea31012005-04-17 16:05:31 -05007667
James Smart7054a602007-04-25 09:52:34 -04007668 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7669 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007670 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007671 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007673 "0338 IOCB wait timeout error - no "
7674 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007675 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007676 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007678 "0330 IOCB wake NOT set, "
7679 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007680 timeout, (timeleft / jiffies));
7681 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007682 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007683 } else if (retval == IOCB_BUSY) {
7684 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7685 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7686 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7687 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007688 } else {
7689 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007690 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007691 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007692 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007693 }
7694
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007695 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7696 creg_val = readl(phba->HCregaddr);
7697 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7698 writel(creg_val, phba->HCregaddr);
7699 readl(phba->HCregaddr); /* flush */
7700 }
7701
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007702 if (prspiocbq)
7703 piocb->context2 = NULL;
7704
7705 piocb->context_un.wait_queue = NULL;
7706 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007707 return retval;
7708}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007709
James Smarte59058c2008-08-24 21:49:00 -04007710/**
James Smart3621a712009-04-06 18:47:14 -04007711 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007712 * @phba: Pointer to HBA context object.
7713 * @pmboxq: Pointer to driver mailbox object.
7714 * @timeout: Timeout in number of seconds.
7715 *
7716 * This function issues the mailbox to firmware and waits for the
7717 * mailbox command to complete. If the mailbox command is not
7718 * completed within timeout seconds, it returns MBX_TIMEOUT.
7719 * The function waits for the mailbox completion using an
7720 * interruptible wait. If the thread is woken up due to a
7721 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7722 * should not free the mailbox resources, if this function returns
7723 * MBX_TIMEOUT.
7724 * This function will sleep while waiting for mailbox completion.
7725 * So, this function should not be called from any context which
7726 * does not allow sleeping. Due to the same reason, this function
7727 * cannot be called with interrupt disabled.
7728 * This function assumes that the mailbox completion occurs while
7729 * this function sleep. So, this function cannot be called from
7730 * the worker thread which processes mailbox completion.
7731 * This function is called in the context of HBA management
7732 * applications.
7733 * This function returns MBX_SUCCESS when successful.
7734 * This function is called with no lock held.
7735 **/
dea31012005-04-17 16:05:31 -05007736int
James Smart2e0fef82007-06-17 19:56:36 -05007737lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007738 uint32_t timeout)
7739{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007740 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007741 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007742 unsigned long flag;
dea31012005-04-17 16:05:31 -05007743
7744 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007745 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007746 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007747
James Smart495a7142008-06-14 22:52:59 -04007748 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007749 /* setup wake call as IOCB callback */
7750 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7751 /* setup context field to pass wait_queue pointer to wake function */
7752 pmboxq->context1 = &done_q;
7753
dea31012005-04-17 16:05:31 -05007754 /* now issue the command */
7755 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7756
7757 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007758 wait_event_interruptible_timeout(done_q,
7759 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7760 timeout * HZ);
7761
James Smart858c9f62007-06-17 19:56:39 -05007762 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007763 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007764 /*
7765 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7766 * else do not free the resources.
7767 */
7768 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007769 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007770 else {
James Smart7054a602007-04-25 09:52:34 -04007771 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007772 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7773 }
7774 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007775 }
7776
dea31012005-04-17 16:05:31 -05007777 return retval;
7778}
7779
James Smarte59058c2008-08-24 21:49:00 -04007780/**
James Smart3772a992009-05-22 14:50:54 -04007781 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007782 * @phba: Pointer to HBA context.
7783 *
James Smart3772a992009-05-22 14:50:54 -04007784 * This function is called to shutdown the driver's mailbox sub-system.
7785 * It first marks the mailbox sub-system is in a block state to prevent
7786 * the asynchronous mailbox command from issued off the pending mailbox
7787 * command queue. If the mailbox command sub-system shutdown is due to
7788 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7789 * the mailbox sub-system flush routine to forcefully bring down the
7790 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7791 * as with offline or HBA function reset), this routine will wait for the
7792 * outstanding mailbox command to complete before invoking the mailbox
7793 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007794 **/
James Smart3772a992009-05-22 14:50:54 -04007795void
7796lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007797{
James Smart3772a992009-05-22 14:50:54 -04007798 struct lpfc_sli *psli = &phba->sli;
7799 uint8_t actcmd = MBX_HEARTBEAT;
7800 unsigned long timeout;
7801
7802 spin_lock_irq(&phba->hbalock);
7803 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7804 spin_unlock_irq(&phba->hbalock);
7805
7806 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7807 spin_lock_irq(&phba->hbalock);
7808 if (phba->sli.mbox_active)
7809 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7810 spin_unlock_irq(&phba->hbalock);
7811 /* Determine how long we might wait for the active mailbox
7812 * command to be gracefully completed by firmware.
7813 */
7814 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7815 1000) + jiffies;
7816 while (phba->sli.mbox_active) {
7817 /* Check active mailbox complete status every 2ms */
7818 msleep(2);
7819 if (time_after(jiffies, timeout))
7820 /* Timeout, let the mailbox flush routine to
7821 * forcefully release active mailbox command
7822 */
7823 break;
7824 }
7825 }
7826 lpfc_sli_mbox_sys_flush(phba);
7827}
7828
7829/**
7830 * lpfc_sli_eratt_read - read sli-3 error attention events
7831 * @phba: Pointer to HBA context.
7832 *
7833 * This function is called to read the SLI3 device error attention registers
7834 * for possible error attention events. The caller must hold the hostlock
7835 * with spin_lock_irq().
7836 *
7837 * This fucntion returns 1 when there is Error Attention in the Host Attention
7838 * Register and returns 0 otherwise.
7839 **/
7840static int
7841lpfc_sli_eratt_read(struct lpfc_hba *phba)
7842{
James Smarted957682007-06-17 19:56:37 -05007843 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007844
James Smart3772a992009-05-22 14:50:54 -04007845 /* Read chip Host Attention (HA) register */
7846 ha_copy = readl(phba->HAregaddr);
7847 if (ha_copy & HA_ERATT) {
7848 /* Read host status register to retrieve error event */
7849 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007850
James Smart3772a992009-05-22 14:50:54 -04007851 /* Check if there is a deferred error condition is active */
7852 if ((HS_FFER1 & phba->work_hs) &&
7853 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7854 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007855 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007856 /* Clear all interrupt enable conditions */
7857 writel(0, phba->HCregaddr);
7858 readl(phba->HCregaddr);
7859 }
7860
7861 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007862 phba->work_ha |= HA_ERATT;
7863 /* Indicate polling handles this ERATT */
7864 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007865 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007866 }
James Smart3772a992009-05-22 14:50:54 -04007867 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007868}
7869
James Smarte59058c2008-08-24 21:49:00 -04007870/**
James Smartda0436e2009-05-22 14:51:39 -04007871 * lpfc_sli4_eratt_read - read sli-4 error attention events
7872 * @phba: Pointer to HBA context.
7873 *
7874 * This function is called to read the SLI4 device error attention registers
7875 * for possible error attention events. The caller must hold the hostlock
7876 * with spin_lock_irq().
7877 *
7878 * This fucntion returns 1 when there is Error Attention in the Host Attention
7879 * Register and returns 0 otherwise.
7880 **/
7881static int
7882lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7883{
7884 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007885
7886 /* For now, use the SLI4 device internal unrecoverable error
7887 * registers for error attention. This can be changed later.
7888 */
James Smarta747c9c2009-11-18 15:41:10 -05007889 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7890 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7891 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7892 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7894 "1423 HBA Unrecoverable error: "
7895 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7896 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7897 uerr_sta_lo, uerr_sta_hi,
7898 phba->sli4_hba.ue_mask_lo,
7899 phba->sli4_hba.ue_mask_hi);
7900 phba->work_status[0] = uerr_sta_lo;
7901 phba->work_status[1] = uerr_sta_hi;
7902 /* Set the driver HA work bitmap */
7903 phba->work_ha |= HA_ERATT;
7904 /* Indicate polling handles this ERATT */
7905 phba->hba_flag |= HBA_ERATT_HANDLED;
7906 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007907 }
7908 return 0;
7909}
7910
7911/**
James Smart3621a712009-04-06 18:47:14 -04007912 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007913 * @phba: Pointer to HBA context.
7914 *
James Smart3772a992009-05-22 14:50:54 -04007915 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007916 * error attention register bit for error attention events.
7917 *
7918 * This fucntion returns 1 when there is Error Attention in the Host Attention
7919 * Register and returns 0 otherwise.
7920 **/
7921int
7922lpfc_sli_check_eratt(struct lpfc_hba *phba)
7923{
7924 uint32_t ha_copy;
7925
7926 /* If somebody is waiting to handle an eratt, don't process it
7927 * here. The brdkill function will do this.
7928 */
7929 if (phba->link_flag & LS_IGNORE_ERATT)
7930 return 0;
7931
7932 /* Check if interrupt handler handles this ERATT */
7933 spin_lock_irq(&phba->hbalock);
7934 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7935 /* Interrupt handler has handled ERATT */
7936 spin_unlock_irq(&phba->hbalock);
7937 return 0;
7938 }
7939
James Smarta257bf92009-04-06 18:48:10 -04007940 /*
7941 * If there is deferred error attention, do not check for error
7942 * attention
7943 */
7944 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7945 spin_unlock_irq(&phba->hbalock);
7946 return 0;
7947 }
7948
James Smart3772a992009-05-22 14:50:54 -04007949 /* If PCI channel is offline, don't process it */
7950 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007951 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007952 return 0;
7953 }
7954
7955 switch (phba->sli_rev) {
7956 case LPFC_SLI_REV2:
7957 case LPFC_SLI_REV3:
7958 /* Read chip Host Attention (HA) register */
7959 ha_copy = lpfc_sli_eratt_read(phba);
7960 break;
James Smartda0436e2009-05-22 14:51:39 -04007961 case LPFC_SLI_REV4:
7962 /* Read devcie Uncoverable Error (UERR) registers */
7963 ha_copy = lpfc_sli4_eratt_read(phba);
7964 break;
James Smart3772a992009-05-22 14:50:54 -04007965 default:
7966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7967 "0299 Invalid SLI revision (%d)\n",
7968 phba->sli_rev);
7969 ha_copy = 0;
7970 break;
James Smart93996272008-08-24 21:50:30 -04007971 }
7972 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007973
7974 return ha_copy;
7975}
7976
7977/**
7978 * lpfc_intr_state_check - Check device state for interrupt handling
7979 * @phba: Pointer to HBA context.
7980 *
7981 * This inline routine checks whether a device or its PCI slot is in a state
7982 * that the interrupt should be handled.
7983 *
7984 * This function returns 0 if the device or the PCI slot is in a state that
7985 * interrupt should be handled, otherwise -EIO.
7986 */
7987static inline int
7988lpfc_intr_state_check(struct lpfc_hba *phba)
7989{
7990 /* If the pci channel is offline, ignore all the interrupts */
7991 if (unlikely(pci_channel_offline(phba->pcidev)))
7992 return -EIO;
7993
7994 /* Update device level interrupt statistics */
7995 phba->sli.slistat.sli_intr++;
7996
7997 /* Ignore all interrupts during initialization. */
7998 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7999 return -EIO;
8000
James Smart93996272008-08-24 21:50:30 -04008001 return 0;
8002}
8003
8004/**
James Smart3772a992009-05-22 14:50:54 -04008005 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008006 * @irq: Interrupt number.
8007 * @dev_id: The device context pointer.
8008 *
James Smart93996272008-08-24 21:50:30 -04008009 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008010 * service routine when device with SLI-3 interface spec is enabled with
8011 * MSI-X multi-message interrupt mode and there are slow-path events in
8012 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8013 * interrupt mode, this function is called as part of the device-level
8014 * interrupt handler. When the PCI slot is in error recovery or the HBA
8015 * is undergoing initialization, the interrupt handler will not process
8016 * the interrupt. The link attention and ELS ring attention events are
8017 * handled by the worker thread. The interrupt handler signals the worker
8018 * thread and returns for these events. This function is called without
8019 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008020 * structures.
8021 *
8022 * This function returns IRQ_HANDLED when interrupt is handled else it
8023 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008024 **/
dea31012005-04-17 16:05:31 -05008025irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008026lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008027{
James Smart2e0fef82007-06-17 19:56:36 -05008028 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008029 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008030 uint32_t work_ha_copy;
8031 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008032 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008033 uint32_t control;
8034
James Smart92d7f7b2007-06-17 19:56:38 -05008035 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008036 struct lpfc_vport *vport;
8037 struct lpfc_nodelist *ndlp;
8038 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008039 LPFC_MBOXQ_t *pmb;
8040 int rc;
8041
dea31012005-04-17 16:05:31 -05008042 /*
8043 * Get the driver's phba structure from the dev_id and
8044 * assume the HBA is not interrupting.
8045 */
James Smart93996272008-08-24 21:50:30 -04008046 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008047
8048 if (unlikely(!phba))
8049 return IRQ_NONE;
8050
dea31012005-04-17 16:05:31 -05008051 /*
James Smart93996272008-08-24 21:50:30 -04008052 * Stuff needs to be attented to when this function is invoked as an
8053 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008054 */
James Smart93996272008-08-24 21:50:30 -04008055 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008056 /* Check device state for handling interrupt */
8057 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008058 return IRQ_NONE;
8059 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008060 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008061 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008062 /* If somebody is waiting to handle an eratt don't process it
8063 * here. The brdkill function will do this.
8064 */
8065 if (phba->link_flag & LS_IGNORE_ERATT)
8066 ha_copy &= ~HA_ERATT;
8067 /* Check the need for handling ERATT in interrupt handler */
8068 if (ha_copy & HA_ERATT) {
8069 if (phba->hba_flag & HBA_ERATT_HANDLED)
8070 /* ERATT polling has handled ERATT */
8071 ha_copy &= ~HA_ERATT;
8072 else
8073 /* Indicate interrupt handler handles ERATT */
8074 phba->hba_flag |= HBA_ERATT_HANDLED;
8075 }
James Smarta257bf92009-04-06 18:48:10 -04008076
8077 /*
8078 * If there is deferred error attention, do not check for any
8079 * interrupt.
8080 */
8081 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008082 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008083 return IRQ_NONE;
8084 }
8085
James Smart93996272008-08-24 21:50:30 -04008086 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008087 hc_copy = readl(phba->HCregaddr);
8088 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8089 HC_LAINT_ENA | HC_ERINT_ENA),
8090 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008091 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8092 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008093 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008094 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008095 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008096 } else
8097 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008098
dea31012005-04-17 16:05:31 -05008099 work_ha_copy = ha_copy & phba->work_ha_mask;
8100
James Smart93996272008-08-24 21:50:30 -04008101 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008102 if (work_ha_copy & HA_LATT) {
8103 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8104 /*
8105 * Turn off Link Attention interrupts
8106 * until CLEAR_LA done
8107 */
James Smart5b75da22008-12-04 22:39:35 -05008108 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008109 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8110 control = readl(phba->HCregaddr);
8111 control &= ~HC_LAINT_ENA;
8112 writel(control, phba->HCregaddr);
8113 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008114 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008115 }
8116 else
8117 work_ha_copy &= ~HA_LATT;
8118 }
8119
James Smart93996272008-08-24 21:50:30 -04008120 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008121 /*
8122 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8123 * the only slow ring.
8124 */
8125 status = (work_ha_copy &
8126 (HA_RXMASK << (4*LPFC_ELS_RING)));
8127 status >>= (4*LPFC_ELS_RING);
8128 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008129 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008130 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008131
8132 lpfc_debugfs_slow_ring_trc(phba,
8133 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8134 control, status,
8135 (uint32_t)phba->sli.slistat.sli_intr);
8136
James Smart858c9f62007-06-17 19:56:39 -05008137 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008138 lpfc_debugfs_slow_ring_trc(phba,
8139 "ISR Disable ring:"
8140 "pwork:x%x hawork:x%x wait:x%x",
8141 phba->work_ha, work_ha_copy,
8142 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008143 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008144
James Smart858c9f62007-06-17 19:56:39 -05008145 control &=
8146 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008147 writel(control, phba->HCregaddr);
8148 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008149 }
James Smarta58cbd52007-08-02 11:09:43 -04008150 else {
8151 lpfc_debugfs_slow_ring_trc(phba,
8152 "ISR slow ring: pwork:"
8153 "x%x hawork:x%x wait:x%x",
8154 phba->work_ha, work_ha_copy,
8155 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008156 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008157 }
James Smart5b75da22008-12-04 22:39:35 -05008158 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008159 }
8160 }
James Smart5b75da22008-12-04 22:39:35 -05008161 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008162 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008163 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008164 /*
8165 * Check if there is a deferred error condition
8166 * is active
8167 */
8168 if ((HS_FFER1 & phba->work_hs) &&
8169 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8170 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8171 phba->hba_flag |= DEFER_ERATT;
8172 /* Clear all interrupt enable conditions */
8173 writel(0, phba->HCregaddr);
8174 readl(phba->HCregaddr);
8175 }
8176 }
8177
James Smart93996272008-08-24 21:50:30 -04008178 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008179 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008180 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008181 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008182 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008183
8184 /* First check out the status word */
8185 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8186 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008187 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008188 /*
8189 * Stray Mailbox Interrupt, mbxCommand <cmd>
8190 * mbxStatus <status>
8191 */
James Smart09372822008-01-11 01:52:54 -05008192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008193 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008194 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008195 "Interrupt mbxCommand x%x "
8196 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008197 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008198 pmbox->mbxCommand,
8199 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008200 /* clear mailbox attention bit */
8201 work_ha_copy &= ~HA_MBATT;
8202 } else {
James Smart97eab632008-04-07 10:16:05 -04008203 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008204 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008205 phba->last_completion_time = jiffies;
8206 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008207 if (pmb->mbox_cmpl) {
8208 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8209 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008210 if (pmb->out_ext_byte_len &&
8211 pmb->context2)
8212 lpfc_sli_pcimem_bcopy(
8213 phba->mbox_ext,
8214 pmb->context2,
8215 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008216 }
James Smart09372822008-01-11 01:52:54 -05008217 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8218 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8219
8220 lpfc_debugfs_disc_trc(vport,
8221 LPFC_DISC_TRC_MBOX_VPORT,
8222 "MBOX dflt rpi: : "
8223 "status:x%x rpi:x%x",
8224 (uint32_t)pmbox->mbxStatus,
8225 pmbox->un.varWords[0], 0);
8226
8227 if (!pmbox->mbxStatus) {
8228 mp = (struct lpfc_dmabuf *)
8229 (pmb->context1);
8230 ndlp = (struct lpfc_nodelist *)
8231 pmb->context2;
8232
8233 /* Reg_LOGIN of dflt RPI was
8234 * successful. new lets get
8235 * rid of the RPI using the
8236 * same mbox buffer.
8237 */
8238 lpfc_unreg_login(phba,
8239 vport->vpi,
8240 pmbox->un.varWords[0],
8241 pmb);
8242 pmb->mbox_cmpl =
8243 lpfc_mbx_cmpl_dflt_rpi;
8244 pmb->context1 = mp;
8245 pmb->context2 = ndlp;
8246 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008247 rc = lpfc_sli_issue_mbox(phba,
8248 pmb,
8249 MBX_NOWAIT);
8250 if (rc != MBX_BUSY)
8251 lpfc_printf_log(phba,
8252 KERN_ERR,
8253 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008254 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008255 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008256 if (rc != MBX_NOT_FINISHED)
8257 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008258 }
8259 }
James Smart5b75da22008-12-04 22:39:35 -05008260 spin_lock_irqsave(
8261 &phba->pport->work_port_lock,
8262 iflag);
James Smart09372822008-01-11 01:52:54 -05008263 phba->pport->work_port_events &=
8264 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008265 spin_unlock_irqrestore(
8266 &phba->pport->work_port_lock,
8267 iflag);
James Smart09372822008-01-11 01:52:54 -05008268 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008269 }
James Smart97eab632008-04-07 10:16:05 -04008270 } else
James Smart5b75da22008-12-04 22:39:35 -05008271 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008272
James Smart92d7f7b2007-06-17 19:56:38 -05008273 if ((work_ha_copy & HA_MBATT) &&
8274 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008275send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008276 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008277 do {
8278 rc = lpfc_sli_issue_mbox(phba, NULL,
8279 MBX_NOWAIT);
8280 } while (rc == MBX_NOT_FINISHED);
8281 if (rc != MBX_SUCCESS)
8282 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8283 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008284 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008285 }
8286
James Smart5b75da22008-12-04 22:39:35 -05008287 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008288 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008289 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008290 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008291 }
James Smart93996272008-08-24 21:50:30 -04008292 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008293
James Smart3772a992009-05-22 14:50:54 -04008294} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008295
8296/**
James Smart3772a992009-05-22 14:50:54 -04008297 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008298 * @irq: Interrupt number.
8299 * @dev_id: The device context pointer.
8300 *
8301 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008302 * service routine when device with SLI-3 interface spec is enabled with
8303 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8304 * ring event in the HBA. However, when the device is enabled with either
8305 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8306 * device-level interrupt handler. When the PCI slot is in error recovery
8307 * or the HBA is undergoing initialization, the interrupt handler will not
8308 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8309 * the intrrupt context. This function is called without any lock held.
8310 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008311 *
8312 * This function returns IRQ_HANDLED when interrupt is handled else it
8313 * returns IRQ_NONE.
8314 **/
8315irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008316lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008317{
8318 struct lpfc_hba *phba;
8319 uint32_t ha_copy;
8320 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008321 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008322
8323 /* Get the driver's phba structure from the dev_id and
8324 * assume the HBA is not interrupting.
8325 */
8326 phba = (struct lpfc_hba *) dev_id;
8327
8328 if (unlikely(!phba))
8329 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008330
8331 /*
James Smart93996272008-08-24 21:50:30 -04008332 * Stuff needs to be attented to when this function is invoked as an
8333 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008334 */
James Smart93996272008-08-24 21:50:30 -04008335 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008336 /* Check device state for handling interrupt */
8337 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008338 return IRQ_NONE;
8339 /* Need to read HA REG for FCP ring and other ring events */
8340 ha_copy = readl(phba->HAregaddr);
8341 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008342 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008343 /*
8344 * If there is deferred error attention, do not check for
8345 * any interrupt.
8346 */
8347 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008348 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008349 return IRQ_NONE;
8350 }
James Smart93996272008-08-24 21:50:30 -04008351 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8352 phba->HAregaddr);
8353 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008354 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008355 } else
8356 ha_copy = phba->ha_copy;
8357
8358 /*
8359 * Process all events on FCP ring. Take the optimized path for FCP IO.
8360 */
8361 ha_copy &= ~(phba->work_ha_mask);
8362
8363 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008364 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008365 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008366 lpfc_sli_handle_fast_ring_event(phba,
8367 &phba->sli.ring[LPFC_FCP_RING],
8368 status);
James Smarta4bc3372006-12-02 13:34:16 -05008369
8370 if (phba->cfg_multi_ring_support == 2) {
8371 /*
James Smart93996272008-08-24 21:50:30 -04008372 * Process all events on extra ring. Take the optimized path
8373 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008374 */
James Smart93996272008-08-24 21:50:30 -04008375 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008376 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008377 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008378 lpfc_sli_handle_fast_ring_event(phba,
8379 &phba->sli.ring[LPFC_EXTRA_RING],
8380 status);
8381 }
8382 }
dea31012005-04-17 16:05:31 -05008383 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008384} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008385
James Smart93996272008-08-24 21:50:30 -04008386/**
James Smart3772a992009-05-22 14:50:54 -04008387 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008388 * @irq: Interrupt number.
8389 * @dev_id: The device context pointer.
8390 *
James Smart3772a992009-05-22 14:50:54 -04008391 * This function is the HBA device-level interrupt handler to device with
8392 * SLI-3 interface spec, called from the PCI layer when either MSI or
8393 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8394 * requires driver attention. This function invokes the slow-path interrupt
8395 * attention handling function and fast-path interrupt attention handling
8396 * function in turn to process the relevant HBA attention events. This
8397 * function is called without any lock held. It gets the hbalock to access
8398 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008399 *
8400 * This function returns IRQ_HANDLED when interrupt is handled, else it
8401 * returns IRQ_NONE.
8402 **/
8403irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008404lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008405{
8406 struct lpfc_hba *phba;
8407 irqreturn_t sp_irq_rc, fp_irq_rc;
8408 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008409 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008410
8411 /*
8412 * Get the driver's phba structure from the dev_id and
8413 * assume the HBA is not interrupting.
8414 */
8415 phba = (struct lpfc_hba *) dev_id;
8416
8417 if (unlikely(!phba))
8418 return IRQ_NONE;
8419
James Smart3772a992009-05-22 14:50:54 -04008420 /* Check device state for handling interrupt */
8421 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008422 return IRQ_NONE;
8423
8424 spin_lock(&phba->hbalock);
8425 phba->ha_copy = readl(phba->HAregaddr);
8426 if (unlikely(!phba->ha_copy)) {
8427 spin_unlock(&phba->hbalock);
8428 return IRQ_NONE;
8429 } else if (phba->ha_copy & HA_ERATT) {
8430 if (phba->hba_flag & HBA_ERATT_HANDLED)
8431 /* ERATT polling has handled ERATT */
8432 phba->ha_copy &= ~HA_ERATT;
8433 else
8434 /* Indicate interrupt handler handles ERATT */
8435 phba->hba_flag |= HBA_ERATT_HANDLED;
8436 }
8437
James Smarta257bf92009-04-06 18:48:10 -04008438 /*
8439 * If there is deferred error attention, do not check for any interrupt.
8440 */
8441 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8442 spin_unlock_irq(&phba->hbalock);
8443 return IRQ_NONE;
8444 }
8445
James Smart93996272008-08-24 21:50:30 -04008446 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008447 hc_copy = readl(phba->HCregaddr);
8448 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8449 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8450 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008451 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008452 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008453 readl(phba->HAregaddr); /* flush */
8454 spin_unlock(&phba->hbalock);
8455
8456 /*
8457 * Invokes slow-path host attention interrupt handling as appropriate.
8458 */
8459
8460 /* status of events with mailbox and link attention */
8461 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8462
8463 /* status of events with ELS ring */
8464 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8465 status2 >>= (4*LPFC_ELS_RING);
8466
8467 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008468 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008469 else
8470 sp_irq_rc = IRQ_NONE;
8471
8472 /*
8473 * Invoke fast-path host attention interrupt handling as appropriate.
8474 */
8475
8476 /* status of events with FCP ring */
8477 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8478 status1 >>= (4*LPFC_FCP_RING);
8479
8480 /* status of events with extra ring */
8481 if (phba->cfg_multi_ring_support == 2) {
8482 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8483 status2 >>= (4*LPFC_EXTRA_RING);
8484 } else
8485 status2 = 0;
8486
8487 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008488 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008489 else
8490 fp_irq_rc = IRQ_NONE;
8491
8492 /* Return device-level interrupt handling status */
8493 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008494} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008495
8496/**
8497 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8498 * @phba: pointer to lpfc hba data structure.
8499 *
8500 * This routine is invoked by the worker thread to process all the pending
8501 * SLI4 FCP abort XRI events.
8502 **/
8503void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8504{
8505 struct lpfc_cq_event *cq_event;
8506
8507 /* First, declare the fcp xri abort event has been handled */
8508 spin_lock_irq(&phba->hbalock);
8509 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8510 spin_unlock_irq(&phba->hbalock);
8511 /* Now, handle all the fcp xri abort events */
8512 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8513 /* Get the first event from the head of the event queue */
8514 spin_lock_irq(&phba->hbalock);
8515 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8516 cq_event, struct lpfc_cq_event, list);
8517 spin_unlock_irq(&phba->hbalock);
8518 /* Notify aborted XRI for FCP work queue */
8519 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8520 /* Free the event processed back to the free pool */
8521 lpfc_sli4_cq_event_release(phba, cq_event);
8522 }
8523}
8524
8525/**
8526 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8527 * @phba: pointer to lpfc hba data structure.
8528 *
8529 * This routine is invoked by the worker thread to process all the pending
8530 * SLI4 els abort xri events.
8531 **/
8532void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8533{
8534 struct lpfc_cq_event *cq_event;
8535
8536 /* First, declare the els xri abort event has been handled */
8537 spin_lock_irq(&phba->hbalock);
8538 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8539 spin_unlock_irq(&phba->hbalock);
8540 /* Now, handle all the els xri abort events */
8541 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8542 /* Get the first event from the head of the event queue */
8543 spin_lock_irq(&phba->hbalock);
8544 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8545 cq_event, struct lpfc_cq_event, list);
8546 spin_unlock_irq(&phba->hbalock);
8547 /* Notify aborted XRI for ELS work queue */
8548 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8549 /* Free the event processed back to the free pool */
8550 lpfc_sli4_cq_event_release(phba, cq_event);
8551 }
8552}
8553
James Smart341af102010-01-26 23:07:37 -05008554/**
8555 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8556 * @phba: pointer to lpfc hba data structure
8557 * @pIocbIn: pointer to the rspiocbq
8558 * @pIocbOut: pointer to the cmdiocbq
8559 * @wcqe: pointer to the complete wcqe
8560 *
8561 * This routine transfers the fields of a command iocbq to a response iocbq
8562 * by copying all the IOCB fields from command iocbq and transferring the
8563 * completion status information from the complete wcqe.
8564 **/
James Smart4f774512009-05-22 14:52:35 -04008565static void
James Smart341af102010-01-26 23:07:37 -05008566lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8567 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008568 struct lpfc_iocbq *pIocbOut,
8569 struct lpfc_wcqe_complete *wcqe)
8570{
James Smart341af102010-01-26 23:07:37 -05008571 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008572 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8573
8574 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8575 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008576 /* Map WCQE parameters into irspiocb parameters */
8577 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8578 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8579 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8580 pIocbIn->iocb.un.fcpi.fcpi_parm =
8581 pIocbOut->iocb.un.fcpi.fcpi_parm -
8582 wcqe->total_data_placed;
8583 else
8584 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008585 else {
James Smart4f774512009-05-22 14:52:35 -04008586 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008587 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8588 }
James Smart341af102010-01-26 23:07:37 -05008589
8590 /* Pick up HBA exchange busy condition */
8591 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8592 spin_lock_irqsave(&phba->hbalock, iflags);
8593 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8594 spin_unlock_irqrestore(&phba->hbalock, iflags);
8595 }
James Smart4f774512009-05-22 14:52:35 -04008596}
8597
8598/**
James Smart45ed1192009-10-02 15:17:02 -04008599 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8600 * @phba: Pointer to HBA context object.
8601 * @wcqe: Pointer to work-queue completion queue entry.
8602 *
8603 * This routine handles an ELS work-queue completion event and construct
8604 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8605 * discovery engine to handle.
8606 *
8607 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8608 **/
8609static struct lpfc_iocbq *
8610lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8611 struct lpfc_iocbq *irspiocbq)
8612{
8613 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8614 struct lpfc_iocbq *cmdiocbq;
8615 struct lpfc_wcqe_complete *wcqe;
8616 unsigned long iflags;
8617
8618 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8619 spin_lock_irqsave(&phba->hbalock, iflags);
8620 pring->stats.iocb_event++;
8621 /* Look up the ELS command IOCB and create pseudo response IOCB */
8622 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8623 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8624 spin_unlock_irqrestore(&phba->hbalock, iflags);
8625
8626 if (unlikely(!cmdiocbq)) {
8627 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8628 "0386 ELS complete with no corresponding "
8629 "cmdiocb: iotag (%d)\n",
8630 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8631 lpfc_sli_release_iocbq(phba, irspiocbq);
8632 return NULL;
8633 }
8634
8635 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008636 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008637
8638 return irspiocbq;
8639}
8640
8641/**
James Smart04c68492009-05-22 14:52:52 -04008642 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8643 * @phba: Pointer to HBA context object.
8644 * @cqe: Pointer to mailbox completion queue entry.
8645 *
8646 * This routine process a mailbox completion queue entry with asynchrous
8647 * event.
8648 *
8649 * Return: true if work posted to worker thread, otherwise false.
8650 **/
8651static bool
8652lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8653{
8654 struct lpfc_cq_event *cq_event;
8655 unsigned long iflags;
8656
8657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8658 "0392 Async Event: word0:x%x, word1:x%x, "
8659 "word2:x%x, word3:x%x\n", mcqe->word0,
8660 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8661
8662 /* Allocate a new internal CQ_EVENT entry */
8663 cq_event = lpfc_sli4_cq_event_alloc(phba);
8664 if (!cq_event) {
8665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8666 "0394 Failed to allocate CQ_EVENT entry\n");
8667 return false;
8668 }
8669
8670 /* Move the CQE into an asynchronous event entry */
8671 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8672 spin_lock_irqsave(&phba->hbalock, iflags);
8673 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8674 /* Set the async event flag */
8675 phba->hba_flag |= ASYNC_EVENT;
8676 spin_unlock_irqrestore(&phba->hbalock, iflags);
8677
8678 return true;
8679}
8680
8681/**
8682 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8683 * @phba: Pointer to HBA context object.
8684 * @cqe: Pointer to mailbox completion queue entry.
8685 *
8686 * This routine process a mailbox completion queue entry with mailbox
8687 * completion event.
8688 *
8689 * Return: true if work posted to worker thread, otherwise false.
8690 **/
8691static bool
8692lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8693{
8694 uint32_t mcqe_status;
8695 MAILBOX_t *mbox, *pmbox;
8696 struct lpfc_mqe *mqe;
8697 struct lpfc_vport *vport;
8698 struct lpfc_nodelist *ndlp;
8699 struct lpfc_dmabuf *mp;
8700 unsigned long iflags;
8701 LPFC_MBOXQ_t *pmb;
8702 bool workposted = false;
8703 int rc;
8704
8705 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8706 if (!bf_get(lpfc_trailer_completed, mcqe))
8707 goto out_no_mqe_complete;
8708
8709 /* Get the reference to the active mbox command */
8710 spin_lock_irqsave(&phba->hbalock, iflags);
8711 pmb = phba->sli.mbox_active;
8712 if (unlikely(!pmb)) {
8713 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8714 "1832 No pending MBOX command to handle\n");
8715 spin_unlock_irqrestore(&phba->hbalock, iflags);
8716 goto out_no_mqe_complete;
8717 }
8718 spin_unlock_irqrestore(&phba->hbalock, iflags);
8719 mqe = &pmb->u.mqe;
8720 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8721 mbox = phba->mbox;
8722 vport = pmb->vport;
8723
8724 /* Reset heartbeat timer */
8725 phba->last_completion_time = jiffies;
8726 del_timer(&phba->sli.mbox_tmo);
8727
8728 /* Move mbox data to caller's mailbox region, do endian swapping */
8729 if (pmb->mbox_cmpl && mbox)
8730 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8731 /* Set the mailbox status with SLI4 range 0x4000 */
8732 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8733 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8734 bf_set(lpfc_mqe_status, mqe,
8735 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8736
8737 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8738 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8739 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8740 "MBOX dflt rpi: status:x%x rpi:x%x",
8741 mcqe_status,
8742 pmbox->un.varWords[0], 0);
8743 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8744 mp = (struct lpfc_dmabuf *)(pmb->context1);
8745 ndlp = (struct lpfc_nodelist *)pmb->context2;
8746 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8747 * RID of the PPI using the same mbox buffer.
8748 */
8749 lpfc_unreg_login(phba, vport->vpi,
8750 pmbox->un.varWords[0], pmb);
8751 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8752 pmb->context1 = mp;
8753 pmb->context2 = ndlp;
8754 pmb->vport = vport;
8755 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8756 if (rc != MBX_BUSY)
8757 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8758 LOG_SLI, "0385 rc should "
8759 "have been MBX_BUSY\n");
8760 if (rc != MBX_NOT_FINISHED)
8761 goto send_current_mbox;
8762 }
8763 }
8764 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8765 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8766 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8767
8768 /* There is mailbox completion work to do */
8769 spin_lock_irqsave(&phba->hbalock, iflags);
8770 __lpfc_mbox_cmpl_put(phba, pmb);
8771 phba->work_ha |= HA_MBATT;
8772 spin_unlock_irqrestore(&phba->hbalock, iflags);
8773 workposted = true;
8774
8775send_current_mbox:
8776 spin_lock_irqsave(&phba->hbalock, iflags);
8777 /* Release the mailbox command posting token */
8778 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8779 /* Setting active mailbox pointer need to be in sync to flag clear */
8780 phba->sli.mbox_active = NULL;
8781 spin_unlock_irqrestore(&phba->hbalock, iflags);
8782 /* Wake up worker thread to post the next pending mailbox command */
8783 lpfc_worker_wake_up(phba);
8784out_no_mqe_complete:
8785 if (bf_get(lpfc_trailer_consumed, mcqe))
8786 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8787 return workposted;
8788}
8789
8790/**
8791 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8792 * @phba: Pointer to HBA context object.
8793 * @cqe: Pointer to mailbox completion queue entry.
8794 *
8795 * This routine process a mailbox completion queue entry, it invokes the
8796 * proper mailbox complete handling or asynchrous event handling routine
8797 * according to the MCQE's async bit.
8798 *
8799 * Return: true if work posted to worker thread, otherwise false.
8800 **/
8801static bool
8802lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8803{
8804 struct lpfc_mcqe mcqe;
8805 bool workposted;
8806
8807 /* Copy the mailbox MCQE and convert endian order as needed */
8808 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8809
8810 /* Invoke the proper event handling routine */
8811 if (!bf_get(lpfc_trailer_async, &mcqe))
8812 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8813 else
8814 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8815 return workposted;
8816}
8817
8818/**
James Smart4f774512009-05-22 14:52:35 -04008819 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8820 * @phba: Pointer to HBA context object.
8821 * @wcqe: Pointer to work-queue completion queue entry.
8822 *
8823 * This routine handles an ELS work-queue completion event.
8824 *
8825 * Return: true if work posted to worker thread, otherwise false.
8826 **/
8827static bool
8828lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8829 struct lpfc_wcqe_complete *wcqe)
8830{
James Smart4f774512009-05-22 14:52:35 -04008831 struct lpfc_iocbq *irspiocbq;
8832 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04008833 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04008834
James Smart45ed1192009-10-02 15:17:02 -04008835 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008836 irspiocbq = lpfc_sli_get_iocbq(phba);
8837 if (!irspiocbq) {
8838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04008839 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8840 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8841 pring->txq_cnt, phba->iocb_cnt,
8842 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8843 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04008844 return false;
James Smart4f774512009-05-22 14:52:35 -04008845 }
James Smart4f774512009-05-22 14:52:35 -04008846
James Smart45ed1192009-10-02 15:17:02 -04008847 /* Save off the slow-path queue event for work thread to process */
8848 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008849 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008850 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008851 &phba->sli4_hba.sp_queue_event);
8852 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008853 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008854
James Smart45ed1192009-10-02 15:17:02 -04008855 return true;
James Smart4f774512009-05-22 14:52:35 -04008856}
8857
8858/**
8859 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8860 * @phba: Pointer to HBA context object.
8861 * @wcqe: Pointer to work-queue completion queue entry.
8862 *
8863 * This routine handles slow-path WQ entry comsumed event by invoking the
8864 * proper WQ release routine to the slow-path WQ.
8865 **/
8866static void
8867lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8868 struct lpfc_wcqe_release *wcqe)
8869{
8870 /* Check for the slow-path ELS work queue */
8871 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8872 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8873 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8874 else
8875 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8876 "2579 Slow-path wqe consume event carries "
8877 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8878 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8879 phba->sli4_hba.els_wq->queue_id);
8880}
8881
8882/**
8883 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8884 * @phba: Pointer to HBA context object.
8885 * @cq: Pointer to a WQ completion queue.
8886 * @wcqe: Pointer to work-queue completion queue entry.
8887 *
8888 * This routine handles an XRI abort event.
8889 *
8890 * Return: true if work posted to worker thread, otherwise false.
8891 **/
8892static bool
8893lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8894 struct lpfc_queue *cq,
8895 struct sli4_wcqe_xri_aborted *wcqe)
8896{
8897 bool workposted = false;
8898 struct lpfc_cq_event *cq_event;
8899 unsigned long iflags;
8900
8901 /* Allocate a new internal CQ_EVENT entry */
8902 cq_event = lpfc_sli4_cq_event_alloc(phba);
8903 if (!cq_event) {
8904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8905 "0602 Failed to allocate CQ_EVENT entry\n");
8906 return false;
8907 }
8908
8909 /* Move the CQE into the proper xri abort event list */
8910 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8911 switch (cq->subtype) {
8912 case LPFC_FCP:
8913 spin_lock_irqsave(&phba->hbalock, iflags);
8914 list_add_tail(&cq_event->list,
8915 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8916 /* Set the fcp xri abort event flag */
8917 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8918 spin_unlock_irqrestore(&phba->hbalock, iflags);
8919 workposted = true;
8920 break;
8921 case LPFC_ELS:
8922 spin_lock_irqsave(&phba->hbalock, iflags);
8923 list_add_tail(&cq_event->list,
8924 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8925 /* Set the els xri abort event flag */
8926 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8927 spin_unlock_irqrestore(&phba->hbalock, iflags);
8928 workposted = true;
8929 break;
8930 default:
8931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8932 "0603 Invalid work queue CQE subtype (x%x)\n",
8933 cq->subtype);
8934 workposted = false;
8935 break;
8936 }
8937 return workposted;
8938}
8939
8940/**
James Smart4d9ab992009-10-02 15:16:39 -04008941 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008942 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008943 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008944 *
James Smart4d9ab992009-10-02 15:16:39 -04008945 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008946 *
8947 * Return: true if work posted to worker thread, otherwise false.
8948 **/
8949static bool
James Smart4d9ab992009-10-02 15:16:39 -04008950lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8951{
8952 bool workposted = false;
8953 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8954 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8955 struct hbq_dmabuf *dma_buf;
8956 uint32_t status;
8957 unsigned long iflags;
8958
James Smart4d9ab992009-10-02 15:16:39 -04008959 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8960 goto out;
8961
8962 status = bf_get(lpfc_rcqe_status, rcqe);
8963 switch (status) {
8964 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8965 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8966 "2537 Receive Frame Truncated!!\n");
8967 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008968 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008969 spin_lock_irqsave(&phba->hbalock, iflags);
8970 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8971 if (!dma_buf) {
8972 spin_unlock_irqrestore(&phba->hbalock, iflags);
8973 goto out;
8974 }
8975 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8976 /* save off the frame for the word thread to process */
8977 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008978 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008979 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008980 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008981 spin_unlock_irqrestore(&phba->hbalock, iflags);
8982 workposted = true;
8983 break;
8984 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8985 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8986 /* Post more buffers if possible */
8987 spin_lock_irqsave(&phba->hbalock, iflags);
8988 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8989 spin_unlock_irqrestore(&phba->hbalock, iflags);
8990 workposted = true;
8991 break;
8992 }
8993out:
8994 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008995}
8996
8997/**
8998 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8999 * @phba: Pointer to HBA context object.
9000 * @cq: Pointer to the completion queue.
9001 * @wcqe: Pointer to a completion queue entry.
9002 *
9003 * This routine process a slow-path work-queue or recieve queue completion queue
9004 * entry.
9005 *
9006 * Return: true if work posted to worker thread, otherwise false.
9007 **/
9008static bool
9009lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009010 struct lpfc_cqe *cqe)
9011{
James Smart45ed1192009-10-02 15:17:02 -04009012 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009013 bool workposted = false;
9014
9015 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009016 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009017
9018 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009019 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009020 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009021 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04009022 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009023 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009024 break;
9025 case CQE_CODE_RELEASE_WQE:
9026 /* Process the WQ release event */
9027 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009028 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009029 break;
9030 case CQE_CODE_XRI_ABORTED:
9031 /* Process the WQ XRI abort event */
9032 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009033 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009034 break;
James Smart4d9ab992009-10-02 15:16:39 -04009035 case CQE_CODE_RECEIVE:
9036 /* Process the RQ event */
9037 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009038 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009039 break;
James Smart4f774512009-05-22 14:52:35 -04009040 default:
9041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9042 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009043 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009044 break;
9045 }
9046 return workposted;
9047}
9048
9049/**
James Smart4f774512009-05-22 14:52:35 -04009050 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9051 * @phba: Pointer to HBA context object.
9052 * @eqe: Pointer to fast-path event queue entry.
9053 *
9054 * This routine process a event queue entry from the slow-path event queue.
9055 * It will check the MajorCode and MinorCode to determine this is for a
9056 * completion event on a completion queue, if not, an error shall be logged
9057 * and just return. Otherwise, it will get to the corresponding completion
9058 * queue and process all the entries on that completion queue, rearm the
9059 * completion queue, and then return.
9060 *
9061 **/
9062static void
9063lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9064{
9065 struct lpfc_queue *cq = NULL, *childq, *speq;
9066 struct lpfc_cqe *cqe;
9067 bool workposted = false;
9068 int ecount = 0;
9069 uint16_t cqid;
9070
James Smartcb5172e2010-03-15 11:25:07 -04009071 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9073 "0359 Not a valid slow-path completion "
9074 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009075 bf_get_le32(lpfc_eqe_major_code, eqe),
9076 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009077 return;
9078 }
9079
9080 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009081 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009082
9083 /* Search for completion queue pointer matching this cqid */
9084 speq = phba->sli4_hba.sp_eq;
9085 list_for_each_entry(childq, &speq->child_list, list) {
9086 if (childq->queue_id == cqid) {
9087 cq = childq;
9088 break;
9089 }
9090 }
9091 if (unlikely(!cq)) {
9092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9093 "0365 Slow-path CQ identifier (%d) does "
9094 "not exist\n", cqid);
9095 return;
9096 }
9097
9098 /* Process all the entries to the CQ */
9099 switch (cq->type) {
9100 case LPFC_MCQ:
9101 while ((cqe = lpfc_sli4_cq_get(cq))) {
9102 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9103 if (!(++ecount % LPFC_GET_QE_REL_INT))
9104 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9105 }
9106 break;
9107 case LPFC_WCQ:
9108 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009109 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009110 if (!(++ecount % LPFC_GET_QE_REL_INT))
9111 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9112 }
9113 break;
9114 default:
9115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9116 "0370 Invalid completion queue type (%d)\n",
9117 cq->type);
9118 return;
9119 }
9120
9121 /* Catch the no cq entry condition, log an error */
9122 if (unlikely(ecount == 0))
9123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9124 "0371 No entry from the CQ: identifier "
9125 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9126
9127 /* In any case, flash and re-arm the RCQ */
9128 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9129
9130 /* wake up worker thread if there are works to be done */
9131 if (workposted)
9132 lpfc_worker_wake_up(phba);
9133}
9134
9135/**
9136 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9137 * @eqe: Pointer to fast-path completion queue entry.
9138 *
9139 * This routine process a fast-path work queue completion entry from fast-path
9140 * event queue for FCP command response completion.
9141 **/
9142static void
9143lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9144 struct lpfc_wcqe_complete *wcqe)
9145{
9146 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9147 struct lpfc_iocbq *cmdiocbq;
9148 struct lpfc_iocbq irspiocbq;
9149 unsigned long iflags;
9150
9151 spin_lock_irqsave(&phba->hbalock, iflags);
9152 pring->stats.iocb_event++;
9153 spin_unlock_irqrestore(&phba->hbalock, iflags);
9154
9155 /* Check for response status */
9156 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9157 /* If resource errors reported from HBA, reduce queue
9158 * depth of the SCSI device.
9159 */
9160 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9161 IOSTAT_LOCAL_REJECT) &&
9162 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9163 phba->lpfc_rampdown_queue_depth(phba);
9164 }
9165 /* Log the error status */
9166 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9167 "0373 FCP complete error: status=x%x, "
9168 "hw_status=x%x, total_data_specified=%d, "
9169 "parameter=x%x, word3=x%x\n",
9170 bf_get(lpfc_wcqe_c_status, wcqe),
9171 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9172 wcqe->total_data_placed, wcqe->parameter,
9173 wcqe->word3);
9174 }
9175
9176 /* Look up the FCP command IOCB and create pseudo response IOCB */
9177 spin_lock_irqsave(&phba->hbalock, iflags);
9178 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9179 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9180 spin_unlock_irqrestore(&phba->hbalock, iflags);
9181 if (unlikely(!cmdiocbq)) {
9182 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9183 "0374 FCP complete with no corresponding "
9184 "cmdiocb: iotag (%d)\n",
9185 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9186 return;
9187 }
9188 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9189 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9190 "0375 FCP cmdiocb not callback function "
9191 "iotag: (%d)\n",
9192 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9193 return;
9194 }
9195
9196 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009197 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009198
James Smart0f65ff62010-02-26 14:14:23 -05009199 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9200 spin_lock_irqsave(&phba->hbalock, iflags);
9201 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9202 spin_unlock_irqrestore(&phba->hbalock, iflags);
9203 }
9204
James Smart4f774512009-05-22 14:52:35 -04009205 /* Pass the cmd_iocb and the rsp state to the upper layer */
9206 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9207}
9208
9209/**
9210 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9211 * @phba: Pointer to HBA context object.
9212 * @cq: Pointer to completion queue.
9213 * @wcqe: Pointer to work-queue completion queue entry.
9214 *
9215 * This routine handles an fast-path WQ entry comsumed event by invoking the
9216 * proper WQ release routine to the slow-path WQ.
9217 **/
9218static void
9219lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9220 struct lpfc_wcqe_release *wcqe)
9221{
9222 struct lpfc_queue *childwq;
9223 bool wqid_matched = false;
9224 uint16_t fcp_wqid;
9225
9226 /* Check for fast-path FCP work queue release */
9227 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9228 list_for_each_entry(childwq, &cq->child_list, list) {
9229 if (childwq->queue_id == fcp_wqid) {
9230 lpfc_sli4_wq_release(childwq,
9231 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9232 wqid_matched = true;
9233 break;
9234 }
9235 }
9236 /* Report warning log message if no match found */
9237 if (wqid_matched != true)
9238 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9239 "2580 Fast-path wqe consume event carries "
9240 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9241}
9242
9243/**
9244 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9245 * @cq: Pointer to the completion queue.
9246 * @eqe: Pointer to fast-path completion queue entry.
9247 *
9248 * This routine process a fast-path work queue completion entry from fast-path
9249 * event queue for FCP command response completion.
9250 **/
9251static int
9252lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9253 struct lpfc_cqe *cqe)
9254{
9255 struct lpfc_wcqe_release wcqe;
9256 bool workposted = false;
James Smart98fc5dd2010-06-07 15:24:29 -04009257 unsigned long iflag;
James Smart4f774512009-05-22 14:52:35 -04009258
9259 /* Copy the work queue CQE and convert endian order if needed */
9260 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9261
9262 /* Check and process for different type of WCQE and dispatch */
9263 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9264 case CQE_CODE_COMPL_WQE:
9265 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009266 spin_lock_irqsave(&phba->hbalock, iflag);
9267 phba->last_completion_time = jiffies;
9268 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4f774512009-05-22 14:52:35 -04009269 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9270 (struct lpfc_wcqe_complete *)&wcqe);
9271 break;
9272 case CQE_CODE_RELEASE_WQE:
9273 /* Process the WQ release event */
9274 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9275 (struct lpfc_wcqe_release *)&wcqe);
9276 break;
9277 case CQE_CODE_XRI_ABORTED:
9278 /* Process the WQ XRI abort event */
9279 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9280 (struct sli4_wcqe_xri_aborted *)&wcqe);
9281 break;
9282 default:
9283 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9284 "0144 Not a valid WCQE code: x%x\n",
9285 bf_get(lpfc_wcqe_c_code, &wcqe));
9286 break;
9287 }
9288 return workposted;
9289}
9290
9291/**
9292 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9293 * @phba: Pointer to HBA context object.
9294 * @eqe: Pointer to fast-path event queue entry.
9295 *
9296 * This routine process a event queue entry from the fast-path event queue.
9297 * It will check the MajorCode and MinorCode to determine this is for a
9298 * completion event on a completion queue, if not, an error shall be logged
9299 * and just return. Otherwise, it will get to the corresponding completion
9300 * queue and process all the entries on the completion queue, rearm the
9301 * completion queue, and then return.
9302 **/
9303static void
9304lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9305 uint32_t fcp_cqidx)
9306{
9307 struct lpfc_queue *cq;
9308 struct lpfc_cqe *cqe;
9309 bool workposted = false;
9310 uint16_t cqid;
9311 int ecount = 0;
9312
James Smartcb5172e2010-03-15 11:25:07 -04009313 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9315 "0366 Not a valid fast-path completion "
9316 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009317 bf_get_le32(lpfc_eqe_major_code, eqe),
9318 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009319 return;
9320 }
9321
9322 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9323 if (unlikely(!cq)) {
9324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9325 "0367 Fast-path completion queue does not "
9326 "exist\n");
9327 return;
9328 }
9329
9330 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009331 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009332 if (unlikely(cqid != cq->queue_id)) {
9333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9334 "0368 Miss-matched fast-path completion "
9335 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9336 cqid, cq->queue_id);
9337 return;
9338 }
9339
9340 /* Process all the entries to the CQ */
9341 while ((cqe = lpfc_sli4_cq_get(cq))) {
9342 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9343 if (!(++ecount % LPFC_GET_QE_REL_INT))
9344 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9345 }
9346
9347 /* Catch the no cq entry condition */
9348 if (unlikely(ecount == 0))
9349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9350 "0369 No entry from fast-path completion "
9351 "queue fcpcqid=%d\n", cq->queue_id);
9352
9353 /* In any case, flash and re-arm the CQ */
9354 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9355
9356 /* wake up worker thread if there are works to be done */
9357 if (workposted)
9358 lpfc_worker_wake_up(phba);
9359}
9360
9361static void
9362lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9363{
9364 struct lpfc_eqe *eqe;
9365
9366 /* walk all the EQ entries and drop on the floor */
9367 while ((eqe = lpfc_sli4_eq_get(eq)))
9368 ;
9369
9370 /* Clear and re-arm the EQ */
9371 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9372}
9373
9374/**
9375 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9376 * @irq: Interrupt number.
9377 * @dev_id: The device context pointer.
9378 *
9379 * This function is directly called from the PCI layer as an interrupt
9380 * service routine when device with SLI-4 interface spec is enabled with
9381 * MSI-X multi-message interrupt mode and there are slow-path events in
9382 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9383 * interrupt mode, this function is called as part of the device-level
9384 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9385 * undergoing initialization, the interrupt handler will not process the
9386 * interrupt. The link attention and ELS ring attention events are handled
9387 * by the worker thread. The interrupt handler signals the worker thread
9388 * and returns for these events. This function is called without any lock
9389 * held. It gets the hbalock to access and update SLI data structures.
9390 *
9391 * This function returns IRQ_HANDLED when interrupt is handled else it
9392 * returns IRQ_NONE.
9393 **/
9394irqreturn_t
9395lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9396{
9397 struct lpfc_hba *phba;
9398 struct lpfc_queue *speq;
9399 struct lpfc_eqe *eqe;
9400 unsigned long iflag;
9401 int ecount = 0;
9402
9403 /*
9404 * Get the driver's phba structure from the dev_id
9405 */
9406 phba = (struct lpfc_hba *)dev_id;
9407
9408 if (unlikely(!phba))
9409 return IRQ_NONE;
9410
9411 /* Get to the EQ struct associated with this vector */
9412 speq = phba->sli4_hba.sp_eq;
9413
9414 /* Check device state for handling interrupt */
9415 if (unlikely(lpfc_intr_state_check(phba))) {
9416 /* Check again for link_state with lock held */
9417 spin_lock_irqsave(&phba->hbalock, iflag);
9418 if (phba->link_state < LPFC_LINK_DOWN)
9419 /* Flush, clear interrupt, and rearm the EQ */
9420 lpfc_sli4_eq_flush(phba, speq);
9421 spin_unlock_irqrestore(&phba->hbalock, iflag);
9422 return IRQ_NONE;
9423 }
9424
9425 /*
9426 * Process all the event on FCP slow-path EQ
9427 */
9428 while ((eqe = lpfc_sli4_eq_get(speq))) {
9429 lpfc_sli4_sp_handle_eqe(phba, eqe);
9430 if (!(++ecount % LPFC_GET_QE_REL_INT))
9431 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9432 }
9433
9434 /* Always clear and re-arm the slow-path EQ */
9435 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9436
9437 /* Catch the no cq entry condition */
9438 if (unlikely(ecount == 0)) {
9439 if (phba->intr_type == MSIX)
9440 /* MSI-X treated interrupt served as no EQ share INT */
9441 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9442 "0357 MSI-X interrupt with no EQE\n");
9443 else
9444 /* Non MSI-X treated on interrupt as EQ share INT */
9445 return IRQ_NONE;
9446 }
9447
9448 return IRQ_HANDLED;
9449} /* lpfc_sli4_sp_intr_handler */
9450
9451/**
9452 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9453 * @irq: Interrupt number.
9454 * @dev_id: The device context pointer.
9455 *
9456 * This function is directly called from the PCI layer as an interrupt
9457 * service routine when device with SLI-4 interface spec is enabled with
9458 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9459 * ring event in the HBA. However, when the device is enabled with either
9460 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9461 * device-level interrupt handler. When the PCI slot is in error recovery
9462 * or the HBA is undergoing initialization, the interrupt handler will not
9463 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9464 * the intrrupt context. This function is called without any lock held.
9465 * It gets the hbalock to access and update SLI data structures. Note that,
9466 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9467 * equal to that of FCP CQ index.
9468 *
9469 * This function returns IRQ_HANDLED when interrupt is handled else it
9470 * returns IRQ_NONE.
9471 **/
9472irqreturn_t
9473lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9474{
9475 struct lpfc_hba *phba;
9476 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9477 struct lpfc_queue *fpeq;
9478 struct lpfc_eqe *eqe;
9479 unsigned long iflag;
9480 int ecount = 0;
9481 uint32_t fcp_eqidx;
9482
9483 /* Get the driver's phba structure from the dev_id */
9484 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9485 phba = fcp_eq_hdl->phba;
9486 fcp_eqidx = fcp_eq_hdl->idx;
9487
9488 if (unlikely(!phba))
9489 return IRQ_NONE;
9490
9491 /* Get to the EQ struct associated with this vector */
9492 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9493
9494 /* Check device state for handling interrupt */
9495 if (unlikely(lpfc_intr_state_check(phba))) {
9496 /* Check again for link_state with lock held */
9497 spin_lock_irqsave(&phba->hbalock, iflag);
9498 if (phba->link_state < LPFC_LINK_DOWN)
9499 /* Flush, clear interrupt, and rearm the EQ */
9500 lpfc_sli4_eq_flush(phba, fpeq);
9501 spin_unlock_irqrestore(&phba->hbalock, iflag);
9502 return IRQ_NONE;
9503 }
9504
9505 /*
9506 * Process all the event on FCP fast-path EQ
9507 */
9508 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9509 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9510 if (!(++ecount % LPFC_GET_QE_REL_INT))
9511 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9512 }
9513
9514 /* Always clear and re-arm the fast-path EQ */
9515 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9516
9517 if (unlikely(ecount == 0)) {
9518 if (phba->intr_type == MSIX)
9519 /* MSI-X treated interrupt served as no EQ share INT */
9520 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9521 "0358 MSI-X interrupt with no EQE\n");
9522 else
9523 /* Non MSI-X treated on interrupt as EQ share INT */
9524 return IRQ_NONE;
9525 }
9526
9527 return IRQ_HANDLED;
9528} /* lpfc_sli4_fp_intr_handler */
9529
9530/**
9531 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9532 * @irq: Interrupt number.
9533 * @dev_id: The device context pointer.
9534 *
9535 * This function is the device-level interrupt handler to device with SLI-4
9536 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9537 * interrupt mode is enabled and there is an event in the HBA which requires
9538 * driver attention. This function invokes the slow-path interrupt attention
9539 * handling function and fast-path interrupt attention handling function in
9540 * turn to process the relevant HBA attention events. This function is called
9541 * without any lock held. It gets the hbalock to access and update SLI data
9542 * structures.
9543 *
9544 * This function returns IRQ_HANDLED when interrupt is handled, else it
9545 * returns IRQ_NONE.
9546 **/
9547irqreturn_t
9548lpfc_sli4_intr_handler(int irq, void *dev_id)
9549{
9550 struct lpfc_hba *phba;
9551 irqreturn_t sp_irq_rc, fp_irq_rc;
9552 bool fp_handled = false;
9553 uint32_t fcp_eqidx;
9554
9555 /* Get the driver's phba structure from the dev_id */
9556 phba = (struct lpfc_hba *)dev_id;
9557
9558 if (unlikely(!phba))
9559 return IRQ_NONE;
9560
9561 /*
9562 * Invokes slow-path host attention interrupt handling as appropriate.
9563 */
9564 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9565
9566 /*
9567 * Invoke fast-path host attention interrupt handling as appropriate.
9568 */
9569 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9570 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9571 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9572 if (fp_irq_rc == IRQ_HANDLED)
9573 fp_handled |= true;
9574 }
9575
9576 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9577} /* lpfc_sli4_intr_handler */
9578
9579/**
9580 * lpfc_sli4_queue_free - free a queue structure and associated memory
9581 * @queue: The queue structure to free.
9582 *
9583 * This function frees a queue structure and the DMAable memeory used for
9584 * the host resident queue. This function must be called after destroying the
9585 * queue on the HBA.
9586 **/
9587void
9588lpfc_sli4_queue_free(struct lpfc_queue *queue)
9589{
9590 struct lpfc_dmabuf *dmabuf;
9591
9592 if (!queue)
9593 return;
9594
9595 while (!list_empty(&queue->page_list)) {
9596 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9597 list);
James Smart49198b32010-04-06 15:04:33 -04009598 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -04009599 dmabuf->virt, dmabuf->phys);
9600 kfree(dmabuf);
9601 }
9602 kfree(queue);
9603 return;
9604}
9605
9606/**
9607 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9608 * @phba: The HBA that this queue is being created on.
9609 * @entry_size: The size of each queue entry for this queue.
9610 * @entry count: The number of entries that this queue will handle.
9611 *
9612 * This function allocates a queue structure and the DMAable memory used for
9613 * the host resident queue. This function must be called before creating the
9614 * queue on the HBA.
9615 **/
9616struct lpfc_queue *
9617lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9618 uint32_t entry_count)
9619{
9620 struct lpfc_queue *queue;
9621 struct lpfc_dmabuf *dmabuf;
9622 int x, total_qe_count;
9623 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -04009624 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -04009625
James Smartcb5172e2010-03-15 11:25:07 -04009626 if (!phba->sli4_hba.pc_sli4_params.supported)
9627 hw_page_size = SLI4_PAGE_SIZE;
9628
James Smart4f774512009-05-22 14:52:35 -04009629 queue = kzalloc(sizeof(struct lpfc_queue) +
9630 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9631 if (!queue)
9632 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -04009633 queue->page_count = (ALIGN(entry_size * entry_count,
9634 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -04009635 INIT_LIST_HEAD(&queue->list);
9636 INIT_LIST_HEAD(&queue->page_list);
9637 INIT_LIST_HEAD(&queue->child_list);
9638 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9639 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9640 if (!dmabuf)
9641 goto out_fail;
9642 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -04009643 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -04009644 GFP_KERNEL);
9645 if (!dmabuf->virt) {
9646 kfree(dmabuf);
9647 goto out_fail;
9648 }
James Smartcb5172e2010-03-15 11:25:07 -04009649 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009650 dmabuf->buffer_tag = x;
9651 list_add_tail(&dmabuf->list, &queue->page_list);
9652 /* initialize queue's entry array */
9653 dma_pointer = dmabuf->virt;
9654 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -04009655 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -04009656 total_qe_count++, dma_pointer += entry_size) {
9657 queue->qe[total_qe_count].address = dma_pointer;
9658 }
9659 }
9660 queue->entry_size = entry_size;
9661 queue->entry_count = entry_count;
9662 queue->phba = phba;
9663
9664 return queue;
9665out_fail:
9666 lpfc_sli4_queue_free(queue);
9667 return NULL;
9668}
9669
9670/**
9671 * lpfc_eq_create - Create an Event Queue on the HBA
9672 * @phba: HBA structure that indicates port to create a queue on.
9673 * @eq: The queue structure to use to create the event queue.
9674 * @imax: The maximum interrupt per second limit.
9675 *
9676 * This function creates an event queue, as detailed in @eq, on a port,
9677 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9678 *
9679 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9680 * is used to get the entry count and entry size that are necessary to
9681 * determine the number of pages to allocate and use for this queue. This
9682 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9683 * event queue. This function is asynchronous and will wait for the mailbox
9684 * command to finish before continuing.
9685 *
9686 * On success this function will return a zero. If unable to allocate enough
9687 * memory this function will return ENOMEM. If the queue create mailbox command
9688 * fails this function will return ENXIO.
9689 **/
9690uint32_t
9691lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9692{
9693 struct lpfc_mbx_eq_create *eq_create;
9694 LPFC_MBOXQ_t *mbox;
9695 int rc, length, status = 0;
9696 struct lpfc_dmabuf *dmabuf;
9697 uint32_t shdr_status, shdr_add_status;
9698 union lpfc_sli4_cfg_shdr *shdr;
9699 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -04009700 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9701
9702 if (!phba->sli4_hba.pc_sli4_params.supported)
9703 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -04009704
9705 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9706 if (!mbox)
9707 return -ENOMEM;
9708 length = (sizeof(struct lpfc_mbx_eq_create) -
9709 sizeof(struct lpfc_sli4_cfg_mhdr));
9710 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9711 LPFC_MBOX_OPCODE_EQ_CREATE,
9712 length, LPFC_SLI4_MBX_EMBED);
9713 eq_create = &mbox->u.mqe.un.eq_create;
9714 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9715 eq->page_count);
9716 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9717 LPFC_EQE_SIZE);
9718 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9719 /* Calculate delay multiper from maximum interrupt per second */
9720 dmult = LPFC_DMULT_CONST/imax - 1;
9721 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9722 dmult);
9723 switch (eq->entry_count) {
9724 default:
9725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9726 "0360 Unsupported EQ count. (%d)\n",
9727 eq->entry_count);
9728 if (eq->entry_count < 256)
9729 return -EINVAL;
9730 /* otherwise default to smallest count (drop through) */
9731 case 256:
9732 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9733 LPFC_EQ_CNT_256);
9734 break;
9735 case 512:
9736 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9737 LPFC_EQ_CNT_512);
9738 break;
9739 case 1024:
9740 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9741 LPFC_EQ_CNT_1024);
9742 break;
9743 case 2048:
9744 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9745 LPFC_EQ_CNT_2048);
9746 break;
9747 case 4096:
9748 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9749 LPFC_EQ_CNT_4096);
9750 break;
9751 }
9752 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009753 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009754 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9755 putPaddrLow(dmabuf->phys);
9756 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9757 putPaddrHigh(dmabuf->phys);
9758 }
9759 mbox->vport = phba->pport;
9760 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9761 mbox->context1 = NULL;
9762 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9763 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9764 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9765 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9766 if (shdr_status || shdr_add_status || rc) {
9767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9768 "2500 EQ_CREATE mailbox failed with "
9769 "status x%x add_status x%x, mbx status x%x\n",
9770 shdr_status, shdr_add_status, rc);
9771 status = -ENXIO;
9772 }
9773 eq->type = LPFC_EQ;
9774 eq->subtype = LPFC_NONE;
9775 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9776 if (eq->queue_id == 0xFFFF)
9777 status = -ENXIO;
9778 eq->host_index = 0;
9779 eq->hba_index = 0;
9780
James Smart8fa38512009-07-19 10:01:03 -04009781 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009782 return status;
9783}
9784
9785/**
9786 * lpfc_cq_create - Create a Completion Queue on the HBA
9787 * @phba: HBA structure that indicates port to create a queue on.
9788 * @cq: The queue structure to use to create the completion queue.
9789 * @eq: The event queue to bind this completion queue to.
9790 *
9791 * This function creates a completion queue, as detailed in @wq, on a port,
9792 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9793 *
9794 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9795 * is used to get the entry count and entry size that are necessary to
9796 * determine the number of pages to allocate and use for this queue. The @eq
9797 * is used to indicate which event queue to bind this completion queue to. This
9798 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9799 * completion queue. This function is asynchronous and will wait for the mailbox
9800 * command to finish before continuing.
9801 *
9802 * On success this function will return a zero. If unable to allocate enough
9803 * memory this function will return ENOMEM. If the queue create mailbox command
9804 * fails this function will return ENXIO.
9805 **/
9806uint32_t
9807lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9808 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9809{
9810 struct lpfc_mbx_cq_create *cq_create;
9811 struct lpfc_dmabuf *dmabuf;
9812 LPFC_MBOXQ_t *mbox;
9813 int rc, length, status = 0;
9814 uint32_t shdr_status, shdr_add_status;
9815 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009816 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9817
9818 if (!phba->sli4_hba.pc_sli4_params.supported)
9819 hw_page_size = SLI4_PAGE_SIZE;
9820
James Smart4f774512009-05-22 14:52:35 -04009821
9822 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9823 if (!mbox)
9824 return -ENOMEM;
9825 length = (sizeof(struct lpfc_mbx_cq_create) -
9826 sizeof(struct lpfc_sli4_cfg_mhdr));
9827 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9828 LPFC_MBOX_OPCODE_CQ_CREATE,
9829 length, LPFC_SLI4_MBX_EMBED);
9830 cq_create = &mbox->u.mqe.un.cq_create;
9831 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9832 cq->page_count);
9833 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9834 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9835 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9836 switch (cq->entry_count) {
9837 default:
9838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9839 "0361 Unsupported CQ count. (%d)\n",
9840 cq->entry_count);
9841 if (cq->entry_count < 256)
9842 return -EINVAL;
9843 /* otherwise default to smallest count (drop through) */
9844 case 256:
9845 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9846 LPFC_CQ_CNT_256);
9847 break;
9848 case 512:
9849 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9850 LPFC_CQ_CNT_512);
9851 break;
9852 case 1024:
9853 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9854 LPFC_CQ_CNT_1024);
9855 break;
9856 }
9857 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -04009858 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -04009859 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9860 putPaddrLow(dmabuf->phys);
9861 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9862 putPaddrHigh(dmabuf->phys);
9863 }
9864 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9865
9866 /* The IOCTL status is embedded in the mailbox subheader. */
9867 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9868 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9869 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9870 if (shdr_status || shdr_add_status || rc) {
9871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9872 "2501 CQ_CREATE mailbox failed with "
9873 "status x%x add_status x%x, mbx status x%x\n",
9874 shdr_status, shdr_add_status, rc);
9875 status = -ENXIO;
9876 goto out;
9877 }
9878 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9879 if (cq->queue_id == 0xFFFF) {
9880 status = -ENXIO;
9881 goto out;
9882 }
9883 /* link the cq onto the parent eq child list */
9884 list_add_tail(&cq->list, &eq->child_list);
9885 /* Set up completion queue's type and subtype */
9886 cq->type = type;
9887 cq->subtype = subtype;
9888 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9889 cq->host_index = 0;
9890 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009891
James Smart8fa38512009-07-19 10:01:03 -04009892out:
9893 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009894 return status;
9895}
9896
9897/**
James Smartb19a0612010-04-06 14:48:51 -04009898 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -04009899 * @phba: HBA structure that indicates port to create a queue on.
9900 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -04009901 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9902 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -04009903 *
James Smartb19a0612010-04-06 14:48:51 -04009904 * This function provides failback (fb) functionality when the
9905 * mq_create_ext fails on older FW generations. It's purpose is identical
9906 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -04009907 *
James Smartb19a0612010-04-06 14:48:51 -04009908 * This routine cannot fail as all attributes were previously accessed and
9909 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -04009910 **/
James Smartb19a0612010-04-06 14:48:51 -04009911static void
9912lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9913 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -04009914{
9915 struct lpfc_mbx_mq_create *mq_create;
9916 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -04009917 int length;
James Smart04c68492009-05-22 14:52:52 -04009918
James Smart04c68492009-05-22 14:52:52 -04009919 length = (sizeof(struct lpfc_mbx_mq_create) -
9920 sizeof(struct lpfc_sli4_cfg_mhdr));
9921 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9922 LPFC_MBOX_OPCODE_MQ_CREATE,
9923 length, LPFC_SLI4_MBX_EMBED);
9924 mq_create = &mbox->u.mqe.un.mq_create;
9925 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -04009926 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -04009927 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -04009928 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -04009929 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9930 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -04009931 case 16:
9932 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9933 LPFC_MQ_CNT_16);
9934 break;
9935 case 32:
9936 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9937 LPFC_MQ_CNT_32);
9938 break;
9939 case 64:
9940 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9941 LPFC_MQ_CNT_64);
9942 break;
9943 case 128:
9944 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9945 LPFC_MQ_CNT_128);
9946 break;
9947 }
9948 list_for_each_entry(dmabuf, &mq->page_list, list) {
9949 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -04009950 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -04009951 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -04009952 putPaddrHigh(dmabuf->phys);
9953 }
9954}
9955
9956/**
9957 * lpfc_mq_create - Create a mailbox Queue on the HBA
9958 * @phba: HBA structure that indicates port to create a queue on.
9959 * @mq: The queue structure to use to create the mailbox queue.
9960 * @cq: The completion queue to associate with this cq.
9961 * @subtype: The queue's subtype.
9962 *
9963 * This function creates a mailbox queue, as detailed in @mq, on a port,
9964 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9965 *
9966 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9967 * is used to get the entry count and entry size that are necessary to
9968 * determine the number of pages to allocate and use for this queue. This
9969 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9970 * mailbox queue. This function is asynchronous and will wait for the mailbox
9971 * command to finish before continuing.
9972 *
9973 * On success this function will return a zero. If unable to allocate enough
9974 * memory this function will return ENOMEM. If the queue create mailbox command
9975 * fails this function will return ENXIO.
9976 **/
9977int32_t
9978lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9979 struct lpfc_queue *cq, uint32_t subtype)
9980{
9981 struct lpfc_mbx_mq_create *mq_create;
9982 struct lpfc_mbx_mq_create_ext *mq_create_ext;
9983 struct lpfc_dmabuf *dmabuf;
9984 LPFC_MBOXQ_t *mbox;
9985 int rc, length, status = 0;
9986 uint32_t shdr_status, shdr_add_status;
9987 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -04009988 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -04009989
James Smart49198b32010-04-06 15:04:33 -04009990 if (!phba->sli4_hba.pc_sli4_params.supported)
9991 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -04009992
9993 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9994 if (!mbox)
9995 return -ENOMEM;
9996 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
9997 sizeof(struct lpfc_sli4_cfg_mhdr));
9998 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9999 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10000 length, LPFC_SLI4_MBX_EMBED);
10001
10002 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10003 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
10004 mq->page_count);
10005 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10006 1);
10007 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10008 &mq_create_ext->u.request, 1);
10009 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10010 &mq_create_ext->u.request, 1);
10011 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10012 cq->queue_id);
10013 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10014 switch (mq->entry_count) {
10015 default:
10016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10017 "0362 Unsupported MQ count. (%d)\n",
10018 mq->entry_count);
10019 if (mq->entry_count < 16)
10020 return -EINVAL;
10021 /* otherwise default to smallest count (drop through) */
10022 case 16:
10023 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10024 LPFC_MQ_CNT_16);
10025 break;
10026 case 32:
10027 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10028 LPFC_MQ_CNT_32);
10029 break;
10030 case 64:
10031 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10032 LPFC_MQ_CNT_64);
10033 break;
10034 case 128:
10035 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10036 LPFC_MQ_CNT_128);
10037 break;
10038 }
10039 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010040 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010041 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10042 putPaddrLow(dmabuf->phys);
10043 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010044 putPaddrHigh(dmabuf->phys);
10045 }
10046 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010047 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10048 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10049 &mq_create_ext->u.response);
10050 if (rc != MBX_SUCCESS) {
10051 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10052 "2795 MQ_CREATE_EXT failed with "
10053 "status x%x. Failback to MQ_CREATE.\n",
10054 rc);
10055 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10056 mq_create = &mbox->u.mqe.un.mq_create;
10057 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10058 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10059 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10060 &mq_create->u.response);
10061 }
10062
James Smart04c68492009-05-22 14:52:52 -040010063 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010064 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10065 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10066 if (shdr_status || shdr_add_status || rc) {
10067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10068 "2502 MQ_CREATE mailbox failed with "
10069 "status x%x add_status x%x, mbx status x%x\n",
10070 shdr_status, shdr_add_status, rc);
10071 status = -ENXIO;
10072 goto out;
10073 }
James Smart04c68492009-05-22 14:52:52 -040010074 if (mq->queue_id == 0xFFFF) {
10075 status = -ENXIO;
10076 goto out;
10077 }
10078 mq->type = LPFC_MQ;
10079 mq->subtype = subtype;
10080 mq->host_index = 0;
10081 mq->hba_index = 0;
10082
10083 /* link the mq onto the parent cq child list */
10084 list_add_tail(&mq->list, &cq->child_list);
10085out:
James Smart8fa38512009-07-19 10:01:03 -040010086 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010087 return status;
10088}
10089
10090/**
James Smart4f774512009-05-22 14:52:35 -040010091 * lpfc_wq_create - Create a Work Queue on the HBA
10092 * @phba: HBA structure that indicates port to create a queue on.
10093 * @wq: The queue structure to use to create the work queue.
10094 * @cq: The completion queue to bind this work queue to.
10095 * @subtype: The subtype of the work queue indicating its functionality.
10096 *
10097 * This function creates a work queue, as detailed in @wq, on a port, described
10098 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10099 *
10100 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10101 * is used to get the entry count and entry size that are necessary to
10102 * determine the number of pages to allocate and use for this queue. The @cq
10103 * is used to indicate which completion queue to bind this work queue to. This
10104 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10105 * work queue. This function is asynchronous and will wait for the mailbox
10106 * command to finish before continuing.
10107 *
10108 * On success this function will return a zero. If unable to allocate enough
10109 * memory this function will return ENOMEM. If the queue create mailbox command
10110 * fails this function will return ENXIO.
10111 **/
10112uint32_t
10113lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10114 struct lpfc_queue *cq, uint32_t subtype)
10115{
10116 struct lpfc_mbx_wq_create *wq_create;
10117 struct lpfc_dmabuf *dmabuf;
10118 LPFC_MBOXQ_t *mbox;
10119 int rc, length, status = 0;
10120 uint32_t shdr_status, shdr_add_status;
10121 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010122 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10123
10124 if (!phba->sli4_hba.pc_sli4_params.supported)
10125 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010126
10127 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10128 if (!mbox)
10129 return -ENOMEM;
10130 length = (sizeof(struct lpfc_mbx_wq_create) -
10131 sizeof(struct lpfc_sli4_cfg_mhdr));
10132 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10133 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10134 length, LPFC_SLI4_MBX_EMBED);
10135 wq_create = &mbox->u.mqe.un.wq_create;
10136 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10137 wq->page_count);
10138 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10139 cq->queue_id);
10140 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010141 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010142 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10143 putPaddrLow(dmabuf->phys);
10144 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10145 putPaddrHigh(dmabuf->phys);
10146 }
10147 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10148 /* The IOCTL status is embedded in the mailbox subheader. */
10149 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10150 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10151 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10152 if (shdr_status || shdr_add_status || rc) {
10153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10154 "2503 WQ_CREATE mailbox failed with "
10155 "status x%x add_status x%x, mbx status x%x\n",
10156 shdr_status, shdr_add_status, rc);
10157 status = -ENXIO;
10158 goto out;
10159 }
10160 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10161 if (wq->queue_id == 0xFFFF) {
10162 status = -ENXIO;
10163 goto out;
10164 }
10165 wq->type = LPFC_WQ;
10166 wq->subtype = subtype;
10167 wq->host_index = 0;
10168 wq->hba_index = 0;
10169
10170 /* link the wq onto the parent cq child list */
10171 list_add_tail(&wq->list, &cq->child_list);
10172out:
James Smart8fa38512009-07-19 10:01:03 -040010173 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010174 return status;
10175}
10176
10177/**
10178 * lpfc_rq_create - Create a Receive Queue on the HBA
10179 * @phba: HBA structure that indicates port to create a queue on.
10180 * @hrq: The queue structure to use to create the header receive queue.
10181 * @drq: The queue structure to use to create the data receive queue.
10182 * @cq: The completion queue to bind this work queue to.
10183 *
10184 * This function creates a receive buffer queue pair , as detailed in @hrq and
10185 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10186 * to the HBA.
10187 *
10188 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10189 * struct is used to get the entry count that is necessary to determine the
10190 * number of pages to use for this queue. The @cq is used to indicate which
10191 * completion queue to bind received buffers that are posted to these queues to.
10192 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10193 * receive queue pair. This function is asynchronous and will wait for the
10194 * mailbox command to finish before continuing.
10195 *
10196 * On success this function will return a zero. If unable to allocate enough
10197 * memory this function will return ENOMEM. If the queue create mailbox command
10198 * fails this function will return ENXIO.
10199 **/
10200uint32_t
10201lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10202 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10203{
10204 struct lpfc_mbx_rq_create *rq_create;
10205 struct lpfc_dmabuf *dmabuf;
10206 LPFC_MBOXQ_t *mbox;
10207 int rc, length, status = 0;
10208 uint32_t shdr_status, shdr_add_status;
10209 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010210 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10211
10212 if (!phba->sli4_hba.pc_sli4_params.supported)
10213 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010214
10215 if (hrq->entry_count != drq->entry_count)
10216 return -EINVAL;
10217 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10218 if (!mbox)
10219 return -ENOMEM;
10220 length = (sizeof(struct lpfc_mbx_rq_create) -
10221 sizeof(struct lpfc_sli4_cfg_mhdr));
10222 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10223 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10224 length, LPFC_SLI4_MBX_EMBED);
10225 rq_create = &mbox->u.mqe.un.rq_create;
10226 switch (hrq->entry_count) {
10227 default:
10228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10229 "2535 Unsupported RQ count. (%d)\n",
10230 hrq->entry_count);
10231 if (hrq->entry_count < 512)
10232 return -EINVAL;
10233 /* otherwise default to smallest count (drop through) */
10234 case 512:
10235 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10236 LPFC_RQ_RING_SIZE_512);
10237 break;
10238 case 1024:
10239 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10240 LPFC_RQ_RING_SIZE_1024);
10241 break;
10242 case 2048:
10243 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10244 LPFC_RQ_RING_SIZE_2048);
10245 break;
10246 case 4096:
10247 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10248 LPFC_RQ_RING_SIZE_4096);
10249 break;
10250 }
10251 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10252 cq->queue_id);
10253 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10254 hrq->page_count);
10255 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10256 LPFC_HDR_BUF_SIZE);
10257 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010258 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010259 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10260 putPaddrLow(dmabuf->phys);
10261 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10262 putPaddrHigh(dmabuf->phys);
10263 }
10264 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10265 /* The IOCTL status is embedded in the mailbox subheader. */
10266 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10269 if (shdr_status || shdr_add_status || rc) {
10270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10271 "2504 RQ_CREATE mailbox failed with "
10272 "status x%x add_status x%x, mbx status x%x\n",
10273 shdr_status, shdr_add_status, rc);
10274 status = -ENXIO;
10275 goto out;
10276 }
10277 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10278 if (hrq->queue_id == 0xFFFF) {
10279 status = -ENXIO;
10280 goto out;
10281 }
10282 hrq->type = LPFC_HRQ;
10283 hrq->subtype = subtype;
10284 hrq->host_index = 0;
10285 hrq->hba_index = 0;
10286
10287 /* now create the data queue */
10288 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10289 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10290 length, LPFC_SLI4_MBX_EMBED);
10291 switch (drq->entry_count) {
10292 default:
10293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10294 "2536 Unsupported RQ count. (%d)\n",
10295 drq->entry_count);
10296 if (drq->entry_count < 512)
10297 return -EINVAL;
10298 /* otherwise default to smallest count (drop through) */
10299 case 512:
10300 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10301 LPFC_RQ_RING_SIZE_512);
10302 break;
10303 case 1024:
10304 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10305 LPFC_RQ_RING_SIZE_1024);
10306 break;
10307 case 2048:
10308 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10309 LPFC_RQ_RING_SIZE_2048);
10310 break;
10311 case 4096:
10312 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10313 LPFC_RQ_RING_SIZE_4096);
10314 break;
10315 }
10316 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10317 cq->queue_id);
10318 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10319 drq->page_count);
10320 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10321 LPFC_DATA_BUF_SIZE);
10322 list_for_each_entry(dmabuf, &drq->page_list, list) {
10323 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10324 putPaddrLow(dmabuf->phys);
10325 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10326 putPaddrHigh(dmabuf->phys);
10327 }
10328 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10329 /* The IOCTL status is embedded in the mailbox subheader. */
10330 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10331 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10332 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10333 if (shdr_status || shdr_add_status || rc) {
10334 status = -ENXIO;
10335 goto out;
10336 }
10337 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10338 if (drq->queue_id == 0xFFFF) {
10339 status = -ENXIO;
10340 goto out;
10341 }
10342 drq->type = LPFC_DRQ;
10343 drq->subtype = subtype;
10344 drq->host_index = 0;
10345 drq->hba_index = 0;
10346
10347 /* link the header and data RQs onto the parent cq child list */
10348 list_add_tail(&hrq->list, &cq->child_list);
10349 list_add_tail(&drq->list, &cq->child_list);
10350
10351out:
James Smart8fa38512009-07-19 10:01:03 -040010352 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010353 return status;
10354}
10355
10356/**
10357 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10358 * @eq: The queue structure associated with the queue to destroy.
10359 *
10360 * This function destroys a queue, as detailed in @eq by sending an mailbox
10361 * command, specific to the type of queue, to the HBA.
10362 *
10363 * The @eq struct is used to get the queue ID of the queue to destroy.
10364 *
10365 * On success this function will return a zero. If the queue destroy mailbox
10366 * command fails this function will return ENXIO.
10367 **/
10368uint32_t
10369lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10370{
10371 LPFC_MBOXQ_t *mbox;
10372 int rc, length, status = 0;
10373 uint32_t shdr_status, shdr_add_status;
10374 union lpfc_sli4_cfg_shdr *shdr;
10375
10376 if (!eq)
10377 return -ENODEV;
10378 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10379 if (!mbox)
10380 return -ENOMEM;
10381 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10382 sizeof(struct lpfc_sli4_cfg_mhdr));
10383 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10384 LPFC_MBOX_OPCODE_EQ_DESTROY,
10385 length, LPFC_SLI4_MBX_EMBED);
10386 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10387 eq->queue_id);
10388 mbox->vport = eq->phba->pport;
10389 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10390
10391 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10392 /* The IOCTL status is embedded in the mailbox subheader. */
10393 shdr = (union lpfc_sli4_cfg_shdr *)
10394 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10395 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10397 if (shdr_status || shdr_add_status || rc) {
10398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10399 "2505 EQ_DESTROY mailbox failed with "
10400 "status x%x add_status x%x, mbx status x%x\n",
10401 shdr_status, shdr_add_status, rc);
10402 status = -ENXIO;
10403 }
10404
10405 /* Remove eq from any list */
10406 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010407 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010408 return status;
10409}
10410
10411/**
10412 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10413 * @cq: The queue structure associated with the queue to destroy.
10414 *
10415 * This function destroys a queue, as detailed in @cq by sending an mailbox
10416 * command, specific to the type of queue, to the HBA.
10417 *
10418 * The @cq struct is used to get the queue ID of the queue to destroy.
10419 *
10420 * On success this function will return a zero. If the queue destroy mailbox
10421 * command fails this function will return ENXIO.
10422 **/
10423uint32_t
10424lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10425{
10426 LPFC_MBOXQ_t *mbox;
10427 int rc, length, status = 0;
10428 uint32_t shdr_status, shdr_add_status;
10429 union lpfc_sli4_cfg_shdr *shdr;
10430
10431 if (!cq)
10432 return -ENODEV;
10433 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10434 if (!mbox)
10435 return -ENOMEM;
10436 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10437 sizeof(struct lpfc_sli4_cfg_mhdr));
10438 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10439 LPFC_MBOX_OPCODE_CQ_DESTROY,
10440 length, LPFC_SLI4_MBX_EMBED);
10441 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10442 cq->queue_id);
10443 mbox->vport = cq->phba->pport;
10444 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10445 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10446 /* The IOCTL status is embedded in the mailbox subheader. */
10447 shdr = (union lpfc_sli4_cfg_shdr *)
10448 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10449 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10450 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10451 if (shdr_status || shdr_add_status || rc) {
10452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10453 "2506 CQ_DESTROY mailbox failed with "
10454 "status x%x add_status x%x, mbx status x%x\n",
10455 shdr_status, shdr_add_status, rc);
10456 status = -ENXIO;
10457 }
10458 /* Remove cq from any list */
10459 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010460 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010461 return status;
10462}
10463
10464/**
James Smart04c68492009-05-22 14:52:52 -040010465 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10466 * @qm: The queue structure associated with the queue to destroy.
10467 *
10468 * This function destroys a queue, as detailed in @mq by sending an mailbox
10469 * command, specific to the type of queue, to the HBA.
10470 *
10471 * The @mq struct is used to get the queue ID of the queue to destroy.
10472 *
10473 * On success this function will return a zero. If the queue destroy mailbox
10474 * command fails this function will return ENXIO.
10475 **/
10476uint32_t
10477lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10478{
10479 LPFC_MBOXQ_t *mbox;
10480 int rc, length, status = 0;
10481 uint32_t shdr_status, shdr_add_status;
10482 union lpfc_sli4_cfg_shdr *shdr;
10483
10484 if (!mq)
10485 return -ENODEV;
10486 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10487 if (!mbox)
10488 return -ENOMEM;
10489 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10490 sizeof(struct lpfc_sli4_cfg_mhdr));
10491 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10492 LPFC_MBOX_OPCODE_MQ_DESTROY,
10493 length, LPFC_SLI4_MBX_EMBED);
10494 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10495 mq->queue_id);
10496 mbox->vport = mq->phba->pport;
10497 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10498 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10499 /* The IOCTL status is embedded in the mailbox subheader. */
10500 shdr = (union lpfc_sli4_cfg_shdr *)
10501 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10504 if (shdr_status || shdr_add_status || rc) {
10505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10506 "2507 MQ_DESTROY mailbox failed with "
10507 "status x%x add_status x%x, mbx status x%x\n",
10508 shdr_status, shdr_add_status, rc);
10509 status = -ENXIO;
10510 }
10511 /* Remove mq from any list */
10512 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010513 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010514 return status;
10515}
10516
10517/**
James Smart4f774512009-05-22 14:52:35 -040010518 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10519 * @wq: The queue structure associated with the queue to destroy.
10520 *
10521 * This function destroys a queue, as detailed in @wq by sending an mailbox
10522 * command, specific to the type of queue, to the HBA.
10523 *
10524 * The @wq struct is used to get the queue ID of the queue to destroy.
10525 *
10526 * On success this function will return a zero. If the queue destroy mailbox
10527 * command fails this function will return ENXIO.
10528 **/
10529uint32_t
10530lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10531{
10532 LPFC_MBOXQ_t *mbox;
10533 int rc, length, status = 0;
10534 uint32_t shdr_status, shdr_add_status;
10535 union lpfc_sli4_cfg_shdr *shdr;
10536
10537 if (!wq)
10538 return -ENODEV;
10539 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10540 if (!mbox)
10541 return -ENOMEM;
10542 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10543 sizeof(struct lpfc_sli4_cfg_mhdr));
10544 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10545 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10546 length, LPFC_SLI4_MBX_EMBED);
10547 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10548 wq->queue_id);
10549 mbox->vport = wq->phba->pport;
10550 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10551 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10552 shdr = (union lpfc_sli4_cfg_shdr *)
10553 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10554 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10555 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10556 if (shdr_status || shdr_add_status || rc) {
10557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10558 "2508 WQ_DESTROY mailbox failed with "
10559 "status x%x add_status x%x, mbx status x%x\n",
10560 shdr_status, shdr_add_status, rc);
10561 status = -ENXIO;
10562 }
10563 /* Remove wq from any list */
10564 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010565 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010566 return status;
10567}
10568
10569/**
10570 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10571 * @rq: The queue structure associated with the queue to destroy.
10572 *
10573 * This function destroys a queue, as detailed in @rq by sending an mailbox
10574 * command, specific to the type of queue, to the HBA.
10575 *
10576 * The @rq struct is used to get the queue ID of the queue to destroy.
10577 *
10578 * On success this function will return a zero. If the queue destroy mailbox
10579 * command fails this function will return ENXIO.
10580 **/
10581uint32_t
10582lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10583 struct lpfc_queue *drq)
10584{
10585 LPFC_MBOXQ_t *mbox;
10586 int rc, length, status = 0;
10587 uint32_t shdr_status, shdr_add_status;
10588 union lpfc_sli4_cfg_shdr *shdr;
10589
10590 if (!hrq || !drq)
10591 return -ENODEV;
10592 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10593 if (!mbox)
10594 return -ENOMEM;
10595 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10596 sizeof(struct mbox_header));
10597 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10598 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10599 length, LPFC_SLI4_MBX_EMBED);
10600 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10601 hrq->queue_id);
10602 mbox->vport = hrq->phba->pport;
10603 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10604 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10605 /* The IOCTL status is embedded in the mailbox subheader. */
10606 shdr = (union lpfc_sli4_cfg_shdr *)
10607 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10608 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10609 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10610 if (shdr_status || shdr_add_status || rc) {
10611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10612 "2509 RQ_DESTROY mailbox failed with "
10613 "status x%x add_status x%x, mbx status x%x\n",
10614 shdr_status, shdr_add_status, rc);
10615 if (rc != MBX_TIMEOUT)
10616 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10617 return -ENXIO;
10618 }
10619 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10620 drq->queue_id);
10621 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10622 shdr = (union lpfc_sli4_cfg_shdr *)
10623 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10626 if (shdr_status || shdr_add_status || rc) {
10627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10628 "2510 RQ_DESTROY mailbox failed with "
10629 "status x%x add_status x%x, mbx status x%x\n",
10630 shdr_status, shdr_add_status, rc);
10631 status = -ENXIO;
10632 }
10633 list_del_init(&hrq->list);
10634 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010635 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010636 return status;
10637}
10638
10639/**
10640 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10641 * @phba: The virtual port for which this call being executed.
10642 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10643 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10644 * @xritag: the xritag that ties this io to the SGL pages.
10645 *
10646 * This routine will post the sgl pages for the IO that has the xritag
10647 * that is in the iocbq structure. The xritag is assigned during iocbq
10648 * creation and persists for as long as the driver is loaded.
10649 * if the caller has fewer than 256 scatter gather segments to map then
10650 * pdma_phys_addr1 should be 0.
10651 * If the caller needs to map more than 256 scatter gather segment then
10652 * pdma_phys_addr1 should be a valid physical address.
10653 * physical address for SGLs must be 64 byte aligned.
10654 * If you are going to map 2 SGL's then the first one must have 256 entries
10655 * the second sgl can have between 1 and 256 entries.
10656 *
10657 * Return codes:
10658 * 0 - Success
10659 * -ENXIO, -ENOMEM - Failure
10660 **/
10661int
10662lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10663 dma_addr_t pdma_phys_addr0,
10664 dma_addr_t pdma_phys_addr1,
10665 uint16_t xritag)
10666{
10667 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10668 LPFC_MBOXQ_t *mbox;
10669 int rc;
10670 uint32_t shdr_status, shdr_add_status;
10671 union lpfc_sli4_cfg_shdr *shdr;
10672
10673 if (xritag == NO_XRI) {
10674 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10675 "0364 Invalid param:\n");
10676 return -EINVAL;
10677 }
10678
10679 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10680 if (!mbox)
10681 return -ENOMEM;
10682
10683 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10684 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10685 sizeof(struct lpfc_mbx_post_sgl_pages) -
10686 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10687
10688 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10689 &mbox->u.mqe.un.post_sgl_pages;
10690 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10691 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10692
10693 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10694 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10695 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10696 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10697
10698 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10699 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10700 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10701 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10702 if (!phba->sli4_hba.intr_enable)
10703 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10704 else
10705 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10706 /* The IOCTL status is embedded in the mailbox subheader. */
10707 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10708 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10709 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10710 if (rc != MBX_TIMEOUT)
10711 mempool_free(mbox, phba->mbox_mem_pool);
10712 if (shdr_status || shdr_add_status || rc) {
10713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10714 "2511 POST_SGL mailbox failed with "
10715 "status x%x add_status x%x, mbx status x%x\n",
10716 shdr_status, shdr_add_status, rc);
10717 rc = -ENXIO;
10718 }
10719 return 0;
10720}
10721/**
10722 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10723 * @phba: The virtual port for which this call being executed.
10724 *
10725 * This routine will remove all of the sgl pages registered with the hba.
10726 *
10727 * Return codes:
10728 * 0 - Success
10729 * -ENXIO, -ENOMEM - Failure
10730 **/
10731int
10732lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10733{
10734 LPFC_MBOXQ_t *mbox;
10735 int rc;
10736 uint32_t shdr_status, shdr_add_status;
10737 union lpfc_sli4_cfg_shdr *shdr;
10738
10739 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10740 if (!mbox)
10741 return -ENOMEM;
10742
10743 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10744 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10745 LPFC_SLI4_MBX_EMBED);
10746 if (!phba->sli4_hba.intr_enable)
10747 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10748 else
10749 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10750 /* The IOCTL status is embedded in the mailbox subheader. */
10751 shdr = (union lpfc_sli4_cfg_shdr *)
10752 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10753 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10754 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10755 if (rc != MBX_TIMEOUT)
10756 mempool_free(mbox, phba->mbox_mem_pool);
10757 if (shdr_status || shdr_add_status || rc) {
10758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10759 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10760 "status x%x add_status x%x, mbx status x%x\n",
10761 shdr_status, shdr_add_status, rc);
10762 rc = -ENXIO;
10763 }
10764 return rc;
10765}
10766
10767/**
10768 * lpfc_sli4_next_xritag - Get an xritag for the io
10769 * @phba: Pointer to HBA context object.
10770 *
10771 * This function gets an xritag for the iocb. If there is no unused xritag
10772 * it will return 0xffff.
10773 * The function returns the allocated xritag if successful, else returns zero.
10774 * Zero is not a valid xritag.
10775 * The caller is not required to hold any lock.
10776 **/
10777uint16_t
10778lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10779{
10780 uint16_t xritag;
10781
10782 spin_lock_irq(&phba->hbalock);
10783 xritag = phba->sli4_hba.next_xri;
10784 if ((xritag != (uint16_t) -1) && xritag <
10785 (phba->sli4_hba.max_cfg_param.max_xri
10786 + phba->sli4_hba.max_cfg_param.xri_base)) {
10787 phba->sli4_hba.next_xri++;
10788 phba->sli4_hba.max_cfg_param.xri_used++;
10789 spin_unlock_irq(&phba->hbalock);
10790 return xritag;
10791 }
10792 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010793 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010794 "2004 Failed to allocate XRI.last XRITAG is %d"
10795 " Max XRI is %d, Used XRI is %d\n",
10796 phba->sli4_hba.next_xri,
10797 phba->sli4_hba.max_cfg_param.max_xri,
10798 phba->sli4_hba.max_cfg_param.xri_used);
10799 return -1;
10800}
10801
10802/**
10803 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10804 * @phba: pointer to lpfc hba data structure.
10805 *
10806 * This routine is invoked to post a block of driver's sgl pages to the
10807 * HBA using non-embedded mailbox command. No Lock is held. This routine
10808 * is only called when the driver is loading and after all IO has been
10809 * stopped.
10810 **/
10811int
10812lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10813{
10814 struct lpfc_sglq *sglq_entry;
10815 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10816 struct sgl_page_pairs *sgl_pg_pairs;
10817 void *viraddr;
10818 LPFC_MBOXQ_t *mbox;
10819 uint32_t reqlen, alloclen, pg_pairs;
10820 uint32_t mbox_tmo;
10821 uint16_t xritag_start = 0;
10822 int els_xri_cnt, rc = 0;
10823 uint32_t shdr_status, shdr_add_status;
10824 union lpfc_sli4_cfg_shdr *shdr;
10825
10826 /* The number of sgls to be posted */
10827 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10828
10829 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10830 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010831 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010832 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10833 "2559 Block sgl registration required DMA "
10834 "size (%d) great than a page\n", reqlen);
10835 return -ENOMEM;
10836 }
10837 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10838 if (!mbox) {
10839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10840 "2560 Failed to allocate mbox cmd memory\n");
10841 return -ENOMEM;
10842 }
10843
10844 /* Allocate DMA memory and set up the non-embedded mailbox command */
10845 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10846 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10847 LPFC_SLI4_MBX_NEMBED);
10848
10849 if (alloclen < reqlen) {
10850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10851 "0285 Allocated DMA memory size (%d) is "
10852 "less than the requested DMA memory "
10853 "size (%d)\n", alloclen, reqlen);
10854 lpfc_sli4_mbox_cmd_free(phba, mbox);
10855 return -ENOMEM;
10856 }
James Smart4f774512009-05-22 14:52:35 -040010857 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010858 viraddr = mbox->sge_array->addr[0];
10859
10860 /* Set up the SGL pages in the non-embedded DMA pages */
10861 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10862 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10863
10864 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10865 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10866 /* Set up the sge entry */
10867 sgl_pg_pairs->sgl_pg0_addr_lo =
10868 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10869 sgl_pg_pairs->sgl_pg0_addr_hi =
10870 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10871 sgl_pg_pairs->sgl_pg1_addr_lo =
10872 cpu_to_le32(putPaddrLow(0));
10873 sgl_pg_pairs->sgl_pg1_addr_hi =
10874 cpu_to_le32(putPaddrHigh(0));
10875 /* Keep the first xritag on the list */
10876 if (pg_pairs == 0)
10877 xritag_start = sglq_entry->sli4_xritag;
10878 sgl_pg_pairs++;
10879 }
10880 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010881 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010882 /* Perform endian conversion if necessary */
10883 sgl->word0 = cpu_to_le32(sgl->word0);
10884
10885 if (!phba->sli4_hba.intr_enable)
10886 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10887 else {
10888 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10889 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10890 }
10891 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10892 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10893 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10894 if (rc != MBX_TIMEOUT)
10895 lpfc_sli4_mbox_cmd_free(phba, mbox);
10896 if (shdr_status || shdr_add_status || rc) {
10897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10898 "2513 POST_SGL_BLOCK mailbox command failed "
10899 "status x%x add_status x%x mbx status x%x\n",
10900 shdr_status, shdr_add_status, rc);
10901 rc = -ENXIO;
10902 }
10903 return rc;
10904}
10905
10906/**
10907 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10908 * @phba: pointer to lpfc hba data structure.
10909 * @sblist: pointer to scsi buffer list.
10910 * @count: number of scsi buffers on the list.
10911 *
10912 * This routine is invoked to post a block of @count scsi sgl pages from a
10913 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10914 * No Lock is held.
10915 *
10916 **/
10917int
10918lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10919 int cnt)
10920{
10921 struct lpfc_scsi_buf *psb;
10922 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10923 struct sgl_page_pairs *sgl_pg_pairs;
10924 void *viraddr;
10925 LPFC_MBOXQ_t *mbox;
10926 uint32_t reqlen, alloclen, pg_pairs;
10927 uint32_t mbox_tmo;
10928 uint16_t xritag_start = 0;
10929 int rc = 0;
10930 uint32_t shdr_status, shdr_add_status;
10931 dma_addr_t pdma_phys_bpl1;
10932 union lpfc_sli4_cfg_shdr *shdr;
10933
10934 /* Calculate the requested length of the dma memory */
10935 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10936 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040010937 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040010938 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10939 "0217 Block sgl registration required DMA "
10940 "size (%d) great than a page\n", reqlen);
10941 return -ENOMEM;
10942 }
10943 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10944 if (!mbox) {
10945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10946 "0283 Failed to allocate mbox cmd memory\n");
10947 return -ENOMEM;
10948 }
10949
10950 /* Allocate DMA memory and set up the non-embedded mailbox command */
10951 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10952 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10953 LPFC_SLI4_MBX_NEMBED);
10954
10955 if (alloclen < reqlen) {
10956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10957 "2561 Allocated DMA memory size (%d) is "
10958 "less than the requested DMA memory "
10959 "size (%d)\n", alloclen, reqlen);
10960 lpfc_sli4_mbox_cmd_free(phba, mbox);
10961 return -ENOMEM;
10962 }
James Smart4f774512009-05-22 14:52:35 -040010963 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010964 viraddr = mbox->sge_array->addr[0];
10965
10966 /* Set up the SGL pages in the non-embedded DMA pages */
10967 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10968 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10969
10970 pg_pairs = 0;
10971 list_for_each_entry(psb, sblist, list) {
10972 /* Set up the sge entry */
10973 sgl_pg_pairs->sgl_pg0_addr_lo =
10974 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10975 sgl_pg_pairs->sgl_pg0_addr_hi =
10976 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10977 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10978 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10979 else
10980 pdma_phys_bpl1 = 0;
10981 sgl_pg_pairs->sgl_pg1_addr_lo =
10982 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10983 sgl_pg_pairs->sgl_pg1_addr_hi =
10984 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10985 /* Keep the first xritag on the list */
10986 if (pg_pairs == 0)
10987 xritag_start = psb->cur_iocbq.sli4_xritag;
10988 sgl_pg_pairs++;
10989 pg_pairs++;
10990 }
10991 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10992 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10993 /* Perform endian conversion if necessary */
10994 sgl->word0 = cpu_to_le32(sgl->word0);
10995
10996 if (!phba->sli4_hba.intr_enable)
10997 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10998 else {
10999 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11000 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11001 }
11002 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11003 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11005 if (rc != MBX_TIMEOUT)
11006 lpfc_sli4_mbox_cmd_free(phba, mbox);
11007 if (shdr_status || shdr_add_status || rc) {
11008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11009 "2564 POST_SGL_BLOCK mailbox command failed "
11010 "status x%x add_status x%x mbx status x%x\n",
11011 shdr_status, shdr_add_status, rc);
11012 rc = -ENXIO;
11013 }
11014 return rc;
11015}
11016
11017/**
11018 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11019 * @phba: pointer to lpfc_hba struct that the frame was received on
11020 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11021 *
11022 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11023 * valid type of frame that the LPFC driver will handle. This function will
11024 * return a zero if the frame is a valid frame or a non zero value when the
11025 * frame does not pass the check.
11026 **/
11027static int
11028lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11029{
11030 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11031 char *type_names[] = FC_TYPE_NAMES_INIT;
11032 struct fc_vft_header *fc_vft_hdr;
11033
11034 switch (fc_hdr->fh_r_ctl) {
11035 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11036 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11037 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11038 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11039 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11040 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11041 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11042 case FC_RCTL_DD_CMD_STATUS: /* command status */
11043 case FC_RCTL_ELS_REQ: /* extended link services request */
11044 case FC_RCTL_ELS_REP: /* extended link services reply */
11045 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11046 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11047 case FC_RCTL_BA_NOP: /* basic link service NOP */
11048 case FC_RCTL_BA_ABTS: /* basic link service abort */
11049 case FC_RCTL_BA_RMC: /* remove connection */
11050 case FC_RCTL_BA_ACC: /* basic accept */
11051 case FC_RCTL_BA_RJT: /* basic reject */
11052 case FC_RCTL_BA_PRMT:
11053 case FC_RCTL_ACK_1: /* acknowledge_1 */
11054 case FC_RCTL_ACK_0: /* acknowledge_0 */
11055 case FC_RCTL_P_RJT: /* port reject */
11056 case FC_RCTL_F_RJT: /* fabric reject */
11057 case FC_RCTL_P_BSY: /* port busy */
11058 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11059 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11060 case FC_RCTL_LCR: /* link credit reset */
11061 case FC_RCTL_END: /* end */
11062 break;
11063 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11064 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11065 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11066 return lpfc_fc_frame_check(phba, fc_hdr);
11067 default:
11068 goto drop;
11069 }
11070 switch (fc_hdr->fh_type) {
11071 case FC_TYPE_BLS:
11072 case FC_TYPE_ELS:
11073 case FC_TYPE_FCP:
11074 case FC_TYPE_CT:
11075 break;
11076 case FC_TYPE_IP:
11077 case FC_TYPE_ILS:
11078 default:
11079 goto drop;
11080 }
11081 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11082 "2538 Received frame rctl:%s type:%s\n",
11083 rctl_names[fc_hdr->fh_r_ctl],
11084 type_names[fc_hdr->fh_type]);
11085 return 0;
11086drop:
11087 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11088 "2539 Dropped frame rctl:%s type:%s\n",
11089 rctl_names[fc_hdr->fh_r_ctl],
11090 type_names[fc_hdr->fh_type]);
11091 return 1;
11092}
11093
11094/**
11095 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11096 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11097 *
11098 * This function processes the FC header to retrieve the VFI from the VF
11099 * header, if one exists. This function will return the VFI if one exists
11100 * or 0 if no VSAN Header exists.
11101 **/
11102static uint32_t
11103lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11104{
11105 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11106
11107 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11108 return 0;
11109 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11110}
11111
11112/**
11113 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11114 * @phba: Pointer to the HBA structure to search for the vport on
11115 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11116 * @fcfi: The FC Fabric ID that the frame came from
11117 *
11118 * This function searches the @phba for a vport that matches the content of the
11119 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11120 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11121 * returns the matching vport pointer or NULL if unable to match frame to a
11122 * vport.
11123 **/
11124static struct lpfc_vport *
11125lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11126 uint16_t fcfi)
11127{
11128 struct lpfc_vport **vports;
11129 struct lpfc_vport *vport = NULL;
11130 int i;
11131 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11132 fc_hdr->fh_d_id[1] << 8 |
11133 fc_hdr->fh_d_id[2]);
11134
11135 vports = lpfc_create_vport_work_array(phba);
11136 if (vports != NULL)
11137 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11138 if (phba->fcf.fcfi == fcfi &&
11139 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11140 vports[i]->fc_myDID == did) {
11141 vport = vports[i];
11142 break;
11143 }
11144 }
11145 lpfc_destroy_vport_work_array(phba, vports);
11146 return vport;
11147}
11148
11149/**
James Smart45ed1192009-10-02 15:17:02 -040011150 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11151 * @vport: The vport to work on.
11152 *
11153 * This function updates the receive sequence time stamp for this vport. The
11154 * receive sequence time stamp indicates the time that the last frame of the
11155 * the sequence that has been idle for the longest amount of time was received.
11156 * the driver uses this time stamp to indicate if any received sequences have
11157 * timed out.
11158 **/
11159void
11160lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11161{
11162 struct lpfc_dmabuf *h_buf;
11163 struct hbq_dmabuf *dmabuf = NULL;
11164
11165 /* get the oldest sequence on the rcv list */
11166 h_buf = list_get_first(&vport->rcv_buffer_list,
11167 struct lpfc_dmabuf, list);
11168 if (!h_buf)
11169 return;
11170 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11171 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11172}
11173
11174/**
11175 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11176 * @vport: The vport that the received sequences were sent to.
11177 *
11178 * This function cleans up all outstanding received sequences. This is called
11179 * by the driver when a link event or user action invalidates all the received
11180 * sequences.
11181 **/
11182void
11183lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11184{
11185 struct lpfc_dmabuf *h_buf, *hnext;
11186 struct lpfc_dmabuf *d_buf, *dnext;
11187 struct hbq_dmabuf *dmabuf = NULL;
11188
11189 /* start with the oldest sequence on the rcv list */
11190 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11191 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11192 list_del_init(&dmabuf->hbuf.list);
11193 list_for_each_entry_safe(d_buf, dnext,
11194 &dmabuf->dbuf.list, list) {
11195 list_del_init(&d_buf->list);
11196 lpfc_in_buf_free(vport->phba, d_buf);
11197 }
11198 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11199 }
11200}
11201
11202/**
11203 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11204 * @vport: The vport that the received sequences were sent to.
11205 *
11206 * This function determines whether any received sequences have timed out by
11207 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11208 * indicates that there is at least one timed out sequence this routine will
11209 * go through the received sequences one at a time from most inactive to most
11210 * active to determine which ones need to be cleaned up. Once it has determined
11211 * that a sequence needs to be cleaned up it will simply free up the resources
11212 * without sending an abort.
11213 **/
11214void
11215lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11216{
11217 struct lpfc_dmabuf *h_buf, *hnext;
11218 struct lpfc_dmabuf *d_buf, *dnext;
11219 struct hbq_dmabuf *dmabuf = NULL;
11220 unsigned long timeout;
11221 int abort_count = 0;
11222
11223 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11224 vport->rcv_buffer_time_stamp);
11225 if (list_empty(&vport->rcv_buffer_list) ||
11226 time_before(jiffies, timeout))
11227 return;
11228 /* start with the oldest sequence on the rcv list */
11229 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11230 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11231 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11232 dmabuf->time_stamp);
11233 if (time_before(jiffies, timeout))
11234 break;
11235 abort_count++;
11236 list_del_init(&dmabuf->hbuf.list);
11237 list_for_each_entry_safe(d_buf, dnext,
11238 &dmabuf->dbuf.list, list) {
11239 list_del_init(&d_buf->list);
11240 lpfc_in_buf_free(vport->phba, d_buf);
11241 }
11242 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11243 }
11244 if (abort_count)
11245 lpfc_update_rcv_time_stamp(vport);
11246}
11247
11248/**
James Smart4f774512009-05-22 14:52:35 -040011249 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11250 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11251 *
11252 * This function searches through the existing incomplete sequences that have
11253 * been sent to this @vport. If the frame matches one of the incomplete
11254 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11255 * make up that sequence. If no sequence is found that matches this frame then
11256 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11257 * This function returns a pointer to the first dmabuf in the sequence list that
11258 * the frame was linked to.
11259 **/
11260static struct hbq_dmabuf *
11261lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11262{
11263 struct fc_frame_header *new_hdr;
11264 struct fc_frame_header *temp_hdr;
11265 struct lpfc_dmabuf *d_buf;
11266 struct lpfc_dmabuf *h_buf;
11267 struct hbq_dmabuf *seq_dmabuf = NULL;
11268 struct hbq_dmabuf *temp_dmabuf = NULL;
11269
James Smart4d9ab992009-10-02 15:16:39 -040011270 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011271 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011272 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11273 /* Use the hdr_buf to find the sequence that this frame belongs to */
11274 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11275 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11276 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11277 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11278 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11279 continue;
11280 /* found a pending sequence that matches this frame */
11281 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11282 break;
11283 }
11284 if (!seq_dmabuf) {
11285 /*
11286 * This indicates first frame received for this sequence.
11287 * Queue the buffer on the vport's rcv_buffer_list.
11288 */
11289 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011290 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011291 return dmabuf;
11292 }
11293 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011294 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11295 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011296 list_del_init(&seq_dmabuf->hbuf.list);
11297 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11298 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011299 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011300 return dmabuf;
11301 }
James Smart45ed1192009-10-02 15:17:02 -040011302 /* move this sequence to the tail to indicate a young sequence */
11303 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11304 seq_dmabuf->time_stamp = jiffies;
11305 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011306 if (list_empty(&seq_dmabuf->dbuf.list)) {
11307 temp_hdr = dmabuf->hbuf.virt;
11308 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11309 return seq_dmabuf;
11310 }
James Smart4f774512009-05-22 14:52:35 -040011311 /* find the correct place in the sequence to insert this frame */
11312 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11313 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11314 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11315 /*
11316 * If the frame's sequence count is greater than the frame on
11317 * the list then insert the frame right after this frame
11318 */
James Smarteeead812009-12-21 17:01:23 -050011319 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11320 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011321 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11322 return seq_dmabuf;
11323 }
11324 }
11325 return NULL;
11326}
11327
11328/**
James Smart6669f9b2009-10-02 15:16:45 -040011329 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11330 * @vport: pointer to a vitural port
11331 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11332 *
11333 * This function tries to abort from the partially assembed sequence, described
11334 * by the information from basic abbort @dmabuf. It checks to see whether such
11335 * partially assembled sequence held by the driver. If so, it shall free up all
11336 * the frames from the partially assembled sequence.
11337 *
11338 * Return
11339 * true -- if there is matching partially assembled sequence present and all
11340 * the frames freed with the sequence;
11341 * false -- if there is no matching partially assembled sequence present so
11342 * nothing got aborted in the lower layer driver
11343 **/
11344static bool
11345lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11346 struct hbq_dmabuf *dmabuf)
11347{
11348 struct fc_frame_header *new_hdr;
11349 struct fc_frame_header *temp_hdr;
11350 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11351 struct hbq_dmabuf *seq_dmabuf = NULL;
11352
11353 /* Use the hdr_buf to find the sequence that matches this frame */
11354 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11355 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11356 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11357 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11358 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11359 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11360 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11361 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11362 continue;
11363 /* found a pending sequence that matches this frame */
11364 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11365 break;
11366 }
11367
11368 /* Free up all the frames from the partially assembled sequence */
11369 if (seq_dmabuf) {
11370 list_for_each_entry_safe(d_buf, n_buf,
11371 &seq_dmabuf->dbuf.list, list) {
11372 list_del_init(&d_buf->list);
11373 lpfc_in_buf_free(vport->phba, d_buf);
11374 }
11375 return true;
11376 }
11377 return false;
11378}
11379
11380/**
11381 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11382 * @phba: Pointer to HBA context object.
11383 * @cmd_iocbq: pointer to the command iocbq structure.
11384 * @rsp_iocbq: pointer to the response iocbq structure.
11385 *
11386 * This function handles the sequence abort accept iocb command complete
11387 * event. It properly releases the memory allocated to the sequence abort
11388 * accept iocb.
11389 **/
11390static void
11391lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11392 struct lpfc_iocbq *cmd_iocbq,
11393 struct lpfc_iocbq *rsp_iocbq)
11394{
11395 if (cmd_iocbq)
11396 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11397}
11398
11399/**
11400 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11401 * @phba: Pointer to HBA context object.
11402 * @fc_hdr: pointer to a FC frame header.
11403 *
11404 * This function sends a basic accept to a previous unsol sequence abort
11405 * event after aborting the sequence handling.
11406 **/
11407static void
11408lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11409 struct fc_frame_header *fc_hdr)
11410{
11411 struct lpfc_iocbq *ctiocb = NULL;
11412 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011413 uint16_t oxid, rxid;
11414 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011415 IOCB_t *icmd;
11416
11417 if (!lpfc_is_link_up(phba))
11418 return;
11419
11420 sid = sli4_sid_from_fc_hdr(fc_hdr);
11421 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011422 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011423
11424 ndlp = lpfc_findnode_did(phba->pport, sid);
11425 if (!ndlp) {
11426 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11427 "1268 Find ndlp returned NULL for oxid:x%x "
11428 "SID:x%x\n", oxid, sid);
11429 return;
11430 }
11431
11432 /* Allocate buffer for acc iocb */
11433 ctiocb = lpfc_sli_get_iocbq(phba);
11434 if (!ctiocb)
11435 return;
11436
James Smart5ffc2662009-11-18 15:39:44 -050011437 /* Extract the F_CTL field from FC_HDR */
11438 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11439
James Smart6669f9b2009-10-02 15:16:45 -040011440 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011441 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011442 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011443 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11444 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11445 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11446
11447 /* Fill in the rest of iocb fields */
11448 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11449 icmd->ulpBdeCount = 0;
11450 icmd->ulpLe = 1;
11451 icmd->ulpClass = CLASS3;
11452 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011453
James Smart6669f9b2009-10-02 15:16:45 -040011454 ctiocb->iocb_cmpl = NULL;
11455 ctiocb->vport = phba->pport;
11456 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11457
James Smart5ffc2662009-11-18 15:39:44 -050011458 if (fctl & FC_FC_EX_CTX) {
11459 /* ABTS sent by responder to CT exchange, construction
11460 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11461 * field and RX_ID from ABTS for RX_ID field.
11462 */
11463 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11464 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11465 ctiocb->sli4_xritag = oxid;
11466 } else {
11467 /* ABTS sent by initiator to CT exchange, construction
11468 * of BA_ACC will need to allocate a new XRI as for the
11469 * XRI_TAG and RX_ID fields.
11470 */
11471 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11472 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11473 ctiocb->sli4_xritag = NO_XRI;
11474 }
11475 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11476
James Smart6669f9b2009-10-02 15:16:45 -040011477 /* Xmit CT abts accept on exchange <xid> */
11478 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11479 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11480 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11481 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11482}
11483
11484/**
11485 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11486 * @vport: Pointer to the vport on which this sequence was received
11487 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11488 *
11489 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11490 * receive sequence is only partially assembed by the driver, it shall abort
11491 * the partially assembled frames for the sequence. Otherwise, if the
11492 * unsolicited receive sequence has been completely assembled and passed to
11493 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11494 * unsolicited sequence has been aborted. After that, it will issue a basic
11495 * accept to accept the abort.
11496 **/
11497void
11498lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11499 struct hbq_dmabuf *dmabuf)
11500{
11501 struct lpfc_hba *phba = vport->phba;
11502 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011503 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011504 bool abts_par;
11505
James Smart6669f9b2009-10-02 15:16:45 -040011506 /* Make a copy of fc_hdr before the dmabuf being released */
11507 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011508 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011509
James Smart5ffc2662009-11-18 15:39:44 -050011510 if (fctl & FC_FC_EX_CTX) {
11511 /*
11512 * ABTS sent by responder to exchange, just free the buffer
11513 */
James Smart6669f9b2009-10-02 15:16:45 -040011514 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011515 } else {
11516 /*
11517 * ABTS sent by initiator to exchange, need to do cleanup
11518 */
11519 /* Try to abort partially assembled seq */
11520 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11521
11522 /* Send abort to ULP if partially seq abort failed */
11523 if (abts_par == false)
11524 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11525 else
11526 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11527 }
James Smart6669f9b2009-10-02 15:16:45 -040011528 /* Send basic accept (BA_ACC) to the abort requester */
11529 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11530}
11531
11532/**
James Smart4f774512009-05-22 14:52:35 -040011533 * lpfc_seq_complete - Indicates if a sequence is complete
11534 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11535 *
11536 * This function checks the sequence, starting with the frame described by
11537 * @dmabuf, to see if all the frames associated with this sequence are present.
11538 * the frames associated with this sequence are linked to the @dmabuf using the
11539 * dbuf list. This function looks for two major things. 1) That the first frame
11540 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11541 * set. 3) That there are no holes in the sequence count. The function will
11542 * return 1 when the sequence is complete, otherwise it will return 0.
11543 **/
11544static int
11545lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11546{
11547 struct fc_frame_header *hdr;
11548 struct lpfc_dmabuf *d_buf;
11549 struct hbq_dmabuf *seq_dmabuf;
11550 uint32_t fctl;
11551 int seq_count = 0;
11552
11553 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11554 /* make sure first fame of sequence has a sequence count of zero */
11555 if (hdr->fh_seq_cnt != seq_count)
11556 return 0;
11557 fctl = (hdr->fh_f_ctl[0] << 16 |
11558 hdr->fh_f_ctl[1] << 8 |
11559 hdr->fh_f_ctl[2]);
11560 /* If last frame of sequence we can return success. */
11561 if (fctl & FC_FC_END_SEQ)
11562 return 1;
11563 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11564 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11565 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11566 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011567 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011568 return 0;
11569 fctl = (hdr->fh_f_ctl[0] << 16 |
11570 hdr->fh_f_ctl[1] << 8 |
11571 hdr->fh_f_ctl[2]);
11572 /* If last frame of sequence we can return success. */
11573 if (fctl & FC_FC_END_SEQ)
11574 return 1;
11575 }
11576 return 0;
11577}
11578
11579/**
11580 * lpfc_prep_seq - Prep sequence for ULP processing
11581 * @vport: Pointer to the vport on which this sequence was received
11582 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11583 *
11584 * This function takes a sequence, described by a list of frames, and creates
11585 * a list of iocbq structures to describe the sequence. This iocbq list will be
11586 * used to issue to the generic unsolicited sequence handler. This routine
11587 * returns a pointer to the first iocbq in the list. If the function is unable
11588 * to allocate an iocbq then it throw out the received frames that were not
11589 * able to be described and return a pointer to the first iocbq. If unable to
11590 * allocate any iocbqs (including the first) this function will return NULL.
11591 **/
11592static struct lpfc_iocbq *
11593lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11594{
11595 struct lpfc_dmabuf *d_buf, *n_buf;
11596 struct lpfc_iocbq *first_iocbq, *iocbq;
11597 struct fc_frame_header *fc_hdr;
11598 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011599 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011600
11601 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11602 /* remove from receive buffer list */
11603 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011604 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011605 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011606 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011607 /* Get an iocbq struct to fill in. */
11608 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11609 if (first_iocbq) {
11610 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011611 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011612 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11613 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11614 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11615 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11616 vport->vpi + vport->phba->vpi_base;
11617 /* put the first buffer into the first IOCBq */
11618 first_iocbq->context2 = &seq_dmabuf->dbuf;
11619 first_iocbq->context3 = NULL;
11620 first_iocbq->iocb.ulpBdeCount = 1;
11621 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11622 LPFC_DATA_BUF_SIZE;
11623 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011624 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011625 bf_get(lpfc_rcqe_length,
11626 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011627 }
11628 iocbq = first_iocbq;
11629 /*
11630 * Each IOCBq can have two Buffers assigned, so go through the list
11631 * of buffers for this sequence and save two buffers in each IOCBq
11632 */
11633 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11634 if (!iocbq) {
11635 lpfc_in_buf_free(vport->phba, d_buf);
11636 continue;
11637 }
11638 if (!iocbq->context3) {
11639 iocbq->context3 = d_buf;
11640 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011641 pbde = (struct ulp_bde64 *)
11642 &iocbq->iocb.unsli3.sli3Words[4];
11643 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011644 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011645 bf_get(lpfc_rcqe_length,
11646 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011647 } else {
11648 iocbq = lpfc_sli_get_iocbq(vport->phba);
11649 if (!iocbq) {
11650 if (first_iocbq) {
11651 first_iocbq->iocb.ulpStatus =
11652 IOSTAT_FCP_RSP_ERROR;
11653 first_iocbq->iocb.un.ulpWord[4] =
11654 IOERR_NO_RESOURCES;
11655 }
11656 lpfc_in_buf_free(vport->phba, d_buf);
11657 continue;
11658 }
11659 iocbq->context2 = d_buf;
11660 iocbq->context3 = NULL;
11661 iocbq->iocb.ulpBdeCount = 1;
11662 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11663 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011664 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011665 bf_get(lpfc_rcqe_length,
11666 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011667 iocbq->iocb.un.rcvels.remoteID = sid;
11668 list_add_tail(&iocbq->list, &first_iocbq->list);
11669 }
11670 }
11671 return first_iocbq;
11672}
11673
James Smart6669f9b2009-10-02 15:16:45 -040011674static void
11675lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11676 struct hbq_dmabuf *seq_dmabuf)
11677{
11678 struct fc_frame_header *fc_hdr;
11679 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11680 struct lpfc_hba *phba = vport->phba;
11681
11682 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11683 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11684 if (!iocbq) {
11685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11686 "2707 Ring %d handler: Failed to allocate "
11687 "iocb Rctl x%x Type x%x received\n",
11688 LPFC_ELS_RING,
11689 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11690 return;
11691 }
11692 if (!lpfc_complete_unsol_iocb(phba,
11693 &phba->sli.ring[LPFC_ELS_RING],
11694 iocbq, fc_hdr->fh_r_ctl,
11695 fc_hdr->fh_type))
11696 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11697 "2540 Ring %d handler: unexpected Rctl "
11698 "x%x Type x%x received\n",
11699 LPFC_ELS_RING,
11700 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11701
11702 /* Free iocb created in lpfc_prep_seq */
11703 list_for_each_entry_safe(curr_iocb, next_iocb,
11704 &iocbq->list, list) {
11705 list_del_init(&curr_iocb->list);
11706 lpfc_sli_release_iocbq(phba, curr_iocb);
11707 }
11708 lpfc_sli_release_iocbq(phba, iocbq);
11709}
11710
James Smart4f774512009-05-22 14:52:35 -040011711/**
11712 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11713 * @phba: Pointer to HBA context object.
11714 *
11715 * This function is called with no lock held. This function processes all
11716 * the received buffers and gives it to upper layers when a received buffer
11717 * indicates that it is the final frame in the sequence. The interrupt
11718 * service routine processes received buffers at interrupt contexts and adds
11719 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11720 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11721 * appropriate receive function when the final frame in a sequence is received.
11722 **/
James Smart4d9ab992009-10-02 15:16:39 -040011723void
11724lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11725 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011726{
James Smart4d9ab992009-10-02 15:16:39 -040011727 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011728 struct fc_frame_header *fc_hdr;
11729 struct lpfc_vport *vport;
11730 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011731
James Smart4f774512009-05-22 14:52:35 -040011732 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011733 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11734 /* check to see if this a valid type of frame */
11735 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11736 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11737 return;
11738 }
11739 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11740 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011741 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011742 /* throw out the frame */
11743 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11744 return;
11745 }
James Smart6669f9b2009-10-02 15:16:45 -040011746 /* Handle the basic abort sequence (BA_ABTS) event */
11747 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11748 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11749 return;
11750 }
11751
James Smart4d9ab992009-10-02 15:16:39 -040011752 /* Link this frame */
11753 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11754 if (!seq_dmabuf) {
11755 /* unable to add frame to vport - throw it out */
11756 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11757 return;
11758 }
11759 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011760 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011761 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011762
James Smart6669f9b2009-10-02 15:16:45 -040011763 /* Send the complete sequence to the upper layer protocol */
11764 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011765}
James Smart6fb120a2009-05-22 14:52:59 -040011766
11767/**
11768 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11769 * @phba: pointer to lpfc hba data structure.
11770 *
11771 * This routine is invoked to post rpi header templates to the
11772 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011773 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11774 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011775 *
11776 * This routine does not require any locks. It's usage is expected
11777 * to be driver load or reset recovery when the driver is
11778 * sequential.
11779 *
11780 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011781 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011782 * EIO - The mailbox failed to complete successfully.
11783 * When this error occurs, the driver is not guaranteed
11784 * to have any rpi regions posted to the device and
11785 * must either attempt to repost the regions or take a
11786 * fatal error.
11787 **/
11788int
11789lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11790{
11791 struct lpfc_rpi_hdr *rpi_page;
11792 uint32_t rc = 0;
11793
11794 /* Post all rpi memory regions to the port. */
11795 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11796 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11797 if (rc != MBX_SUCCESS) {
11798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11799 "2008 Error %d posting all rpi "
11800 "headers\n", rc);
11801 rc = -EIO;
11802 break;
11803 }
11804 }
11805
11806 return rc;
11807}
11808
11809/**
11810 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11811 * @phba: pointer to lpfc hba data structure.
11812 * @rpi_page: pointer to the rpi memory region.
11813 *
11814 * This routine is invoked to post a single rpi header to the
11815 * HBA consistent with the SLI-4 interface spec. This memory region
11816 * maps up to 64 rpi context regions.
11817 *
11818 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011819 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011820 * ENOMEM - No available memory
11821 * EIO - The mailbox failed to complete successfully.
11822 **/
11823int
11824lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11825{
11826 LPFC_MBOXQ_t *mboxq;
11827 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11828 uint32_t rc = 0;
11829 uint32_t mbox_tmo;
11830 uint32_t shdr_status, shdr_add_status;
11831 union lpfc_sli4_cfg_shdr *shdr;
11832
11833 /* The port is notified of the header region via a mailbox command. */
11834 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11835 if (!mboxq) {
11836 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11837 "2001 Unable to allocate memory for issuing "
11838 "SLI_CONFIG_SPECIAL mailbox command\n");
11839 return -ENOMEM;
11840 }
11841
11842 /* Post all rpi memory regions to the port. */
11843 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11844 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11845 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11846 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11847 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11848 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11849 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11850 hdr_tmpl, rpi_page->page_count);
11851 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11852 rpi_page->start_rpi);
11853 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11854 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011855 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011856 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11857 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11858 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11859 if (rc != MBX_TIMEOUT)
11860 mempool_free(mboxq, phba->mbox_mem_pool);
11861 if (shdr_status || shdr_add_status || rc) {
11862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11863 "2514 POST_RPI_HDR mailbox failed with "
11864 "status x%x add_status x%x, mbx status x%x\n",
11865 shdr_status, shdr_add_status, rc);
11866 rc = -ENXIO;
11867 }
11868 return rc;
11869}
11870
11871/**
11872 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11873 * @phba: pointer to lpfc hba data structure.
11874 *
11875 * This routine is invoked to post rpi header templates to the
11876 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040011877 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11878 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040011879 *
11880 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011881 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011882 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11883 **/
11884int
11885lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11886{
11887 int rpi;
11888 uint16_t max_rpi, rpi_base, rpi_limit;
11889 uint16_t rpi_remaining;
11890 struct lpfc_rpi_hdr *rpi_hdr;
11891
11892 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11893 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11894 rpi_limit = phba->sli4_hba.next_rpi;
11895
11896 /*
11897 * The valid rpi range is not guaranteed to be zero-based. Start
11898 * the search at the rpi_base as reported by the port.
11899 */
11900 spin_lock_irq(&phba->hbalock);
11901 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11902 if (rpi >= rpi_limit || rpi < rpi_base)
11903 rpi = LPFC_RPI_ALLOC_ERROR;
11904 else {
11905 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11906 phba->sli4_hba.max_cfg_param.rpi_used++;
11907 phba->sli4_hba.rpi_count++;
11908 }
11909
11910 /*
11911 * Don't try to allocate more rpi header regions if the device limit
11912 * on available rpis max has been exhausted.
11913 */
11914 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11915 (phba->sli4_hba.rpi_count >= max_rpi)) {
11916 spin_unlock_irq(&phba->hbalock);
11917 return rpi;
11918 }
11919
11920 /*
11921 * If the driver is running low on rpi resources, allocate another
11922 * page now. Note that the next_rpi value is used because
11923 * it represents how many are actually in use whereas max_rpi notes
11924 * how many are supported max by the device.
11925 */
11926 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11927 phba->sli4_hba.rpi_count;
11928 spin_unlock_irq(&phba->hbalock);
11929 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11930 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11931 if (!rpi_hdr) {
11932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11933 "2002 Error Could not grow rpi "
11934 "count\n");
11935 } else {
11936 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11937 }
11938 }
11939
11940 return rpi;
11941}
11942
11943/**
11944 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11945 * @phba: pointer to lpfc hba data structure.
11946 *
11947 * This routine is invoked to release an rpi to the pool of
11948 * available rpis maintained by the driver.
11949 **/
11950void
11951lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11952{
11953 spin_lock_irq(&phba->hbalock);
11954 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11955 phba->sli4_hba.rpi_count--;
11956 phba->sli4_hba.max_cfg_param.rpi_used--;
11957 spin_unlock_irq(&phba->hbalock);
11958}
11959
11960/**
11961 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11962 * @phba: pointer to lpfc hba data structure.
11963 *
11964 * This routine is invoked to remove the memory region that
11965 * provided rpi via a bitmask.
11966 **/
11967void
11968lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11969{
11970 kfree(phba->sli4_hba.rpi_bmask);
11971}
11972
11973/**
11974 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11975 * @phba: pointer to lpfc hba data structure.
11976 *
11977 * This routine is invoked to remove the memory region that
11978 * provided rpi via a bitmask.
11979 **/
11980int
11981lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11982{
11983 LPFC_MBOXQ_t *mboxq;
11984 struct lpfc_hba *phba = ndlp->phba;
11985 int rc;
11986
11987 /* The port is notified of the header region via a mailbox command. */
11988 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11989 if (!mboxq)
11990 return -ENOMEM;
11991
11992 /* Post all rpi memory regions to the port. */
11993 lpfc_resume_rpi(mboxq, ndlp);
11994 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11995 if (rc == MBX_NOT_FINISHED) {
11996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11997 "2010 Resume RPI Mailbox failed "
11998 "status %d, mbxStatus x%x\n", rc,
11999 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12000 mempool_free(mboxq, phba->mbox_mem_pool);
12001 return -EIO;
12002 }
12003 return 0;
12004}
12005
12006/**
12007 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12008 * @phba: pointer to lpfc hba data structure.
12009 * @vpi: vpi value to activate with the port.
12010 *
12011 * This routine is invoked to activate a vpi with the
12012 * port when the host intends to use vports with a
12013 * nonzero vpi.
12014 *
12015 * Returns:
12016 * 0 success
12017 * -Evalue otherwise
12018 **/
12019int
12020lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12021{
12022 LPFC_MBOXQ_t *mboxq;
12023 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012024 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012025 uint32_t mbox_tmo;
12026
12027 if (vpi == 0)
12028 return -EINVAL;
12029 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12030 if (!mboxq)
12031 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040012032 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012033 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12034 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012035 if (rc != MBX_SUCCESS) {
12036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12037 "2022 INIT VPI Mailbox failed "
12038 "status %d, mbxStatus x%x\n", rc,
12039 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012040 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012041 }
James Smart6a9c52c2009-10-02 15:16:51 -040012042 if (rc != MBX_TIMEOUT)
12043 mempool_free(mboxq, phba->mbox_mem_pool);
12044
12045 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012046}
12047
12048/**
12049 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12050 * @phba: pointer to lpfc hba data structure.
12051 * @mboxq: Pointer to mailbox object.
12052 *
12053 * This routine is invoked to manually add a single FCF record. The caller
12054 * must pass a completely initialized FCF_Record. This routine takes
12055 * care of the nonembedded mailbox operations.
12056 **/
12057static void
12058lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12059{
12060 void *virt_addr;
12061 union lpfc_sli4_cfg_shdr *shdr;
12062 uint32_t shdr_status, shdr_add_status;
12063
12064 virt_addr = mboxq->sge_array->addr[0];
12065 /* The IOCTL status is embedded in the mailbox subheader. */
12066 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12067 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12068 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12069
12070 if ((shdr_status || shdr_add_status) &&
12071 (shdr_status != STATUS_FCF_IN_USE))
12072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12073 "2558 ADD_FCF_RECORD mailbox failed with "
12074 "status x%x add_status x%x\n",
12075 shdr_status, shdr_add_status);
12076
12077 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12078}
12079
12080/**
12081 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12082 * @phba: pointer to lpfc hba data structure.
12083 * @fcf_record: pointer to the initialized fcf record to add.
12084 *
12085 * This routine is invoked to manually add a single FCF record. The caller
12086 * must pass a completely initialized FCF_Record. This routine takes
12087 * care of the nonembedded mailbox operations.
12088 **/
12089int
12090lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12091{
12092 int rc = 0;
12093 LPFC_MBOXQ_t *mboxq;
12094 uint8_t *bytep;
12095 void *virt_addr;
12096 dma_addr_t phys_addr;
12097 struct lpfc_mbx_sge sge;
12098 uint32_t alloc_len, req_len;
12099 uint32_t fcfindex;
12100
12101 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12102 if (!mboxq) {
12103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12104 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12105 return -ENOMEM;
12106 }
12107
12108 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12109 sizeof(uint32_t);
12110
12111 /* Allocate DMA memory and set up the non-embedded mailbox command */
12112 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12113 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12114 req_len, LPFC_SLI4_MBX_NEMBED);
12115 if (alloc_len < req_len) {
12116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12117 "2523 Allocated DMA memory size (x%x) is "
12118 "less than the requested DMA memory "
12119 "size (x%x)\n", alloc_len, req_len);
12120 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12121 return -ENOMEM;
12122 }
12123
12124 /*
12125 * Get the first SGE entry from the non-embedded DMA memory. This
12126 * routine only uses a single SGE.
12127 */
12128 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12129 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012130 virt_addr = mboxq->sge_array->addr[0];
12131 /*
12132 * Configure the FCF record for FCFI 0. This is the driver's
12133 * hardcoded default and gets used in nonFIP mode.
12134 */
12135 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12136 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12137 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12138
12139 /*
12140 * Copy the fcf_index and the FCF Record Data. The data starts after
12141 * the FCoE header plus word10. The data copy needs to be endian
12142 * correct.
12143 */
12144 bytep += sizeof(uint32_t);
12145 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12146 mboxq->vport = phba->pport;
12147 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12148 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12149 if (rc == MBX_NOT_FINISHED) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "2515 ADD_FCF_RECORD mailbox failed with "
12152 "status 0x%x\n", rc);
12153 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12154 rc = -EIO;
12155 } else
12156 rc = 0;
12157
12158 return rc;
12159}
12160
12161/**
12162 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12163 * @phba: pointer to lpfc hba data structure.
12164 * @fcf_record: pointer to the fcf record to write the default data.
12165 * @fcf_index: FCF table entry index.
12166 *
12167 * This routine is invoked to build the driver's default FCF record. The
12168 * values used are hardcoded. This routine handles memory initialization.
12169 *
12170 **/
12171void
12172lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12173 struct fcf_record *fcf_record,
12174 uint16_t fcf_index)
12175{
12176 memset(fcf_record, 0, sizeof(struct fcf_record));
12177 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12178 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12179 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12180 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12181 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12182 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12183 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12184 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12185 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12186 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12187 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12188 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12189 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012190 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012191 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12192 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12193 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12194 /* Set the VLAN bit map */
12195 if (phba->valid_vlan) {
12196 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12197 = 1 << (phba->vlan_id % 8);
12198 }
12199}
12200
12201/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012202 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012203 * @phba: pointer to lpfc hba data structure.
12204 * @fcf_index: FCF table entry offset.
12205 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012206 * This routine is invoked to scan the entire FCF table by reading FCF
12207 * record and processing it one at a time starting from the @fcf_index
12208 * for initial FCF discovery or fast FCF failover rediscovery.
12209 *
12210 * Return 0 if the mailbox command is submitted sucessfully, none 0
12211 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012212 **/
12213int
James Smart0c9ab6f2010-02-26 14:15:57 -050012214lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012215{
12216 int rc = 0, error;
12217 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012218
James Smart32b97932009-07-19 10:01:21 -040012219 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012220 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12221 if (!mboxq) {
12222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12223 "2000 Failed to allocate mbox for "
12224 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012225 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012226 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012227 }
James Smartecfd03c2010-02-12 14:41:27 -050012228 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012229 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012230 if (rc) {
12231 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012232 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012233 }
James Smartecfd03c2010-02-12 14:41:27 -050012234 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012235 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012236 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smart6fb120a2009-05-22 14:52:59 -040012237 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012238 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012239 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012240 else {
James Smart32b97932009-07-19 10:01:21 -040012241 spin_lock_irq(&phba->hbalock);
12242 phba->hba_flag |= FCF_DISC_INPROGRESS;
12243 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012244 /* Reset FCF round robin index bmask for new scan */
James Smart999d8132010-03-15 11:24:56 -040012245 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012246 memset(phba->fcf.fcf_rr_bmask, 0,
12247 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart999d8132010-03-15 11:24:56 -040012248 phba->fcf.eligible_fcf_cnt = 0;
12249 }
James Smart6fb120a2009-05-22 14:52:59 -040012250 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012251 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012252fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012253 if (error) {
12254 if (mboxq)
12255 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12256 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12257 spin_lock_irq(&phba->hbalock);
12258 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12259 spin_unlock_irq(&phba->hbalock);
12260 }
James Smart6fb120a2009-05-22 14:52:59 -040012261 return error;
12262}
James Smarta0c87cb2009-07-19 10:01:10 -040012263
12264/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012265 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12266 * @phba: pointer to lpfc hba data structure.
12267 * @fcf_index: FCF table entry offset.
12268 *
12269 * This routine is invoked to read an FCF record indicated by @fcf_index
12270 * and to use it for FLOGI round robin FCF failover.
12271 *
12272 * Return 0 if the mailbox command is submitted sucessfully, none 0
12273 * otherwise.
12274 **/
12275int
12276lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12277{
12278 int rc = 0, error;
12279 LPFC_MBOXQ_t *mboxq;
12280
12281 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12282 if (!mboxq) {
12283 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12284 "2763 Failed to allocate mbox for "
12285 "READ_FCF cmd\n");
12286 error = -ENOMEM;
12287 goto fail_fcf_read;
12288 }
12289 /* Construct the read FCF record mailbox command */
12290 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12291 if (rc) {
12292 error = -EINVAL;
12293 goto fail_fcf_read;
12294 }
12295 /* Issue the mailbox command asynchronously */
12296 mboxq->vport = phba->pport;
12297 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12298 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12299 if (rc == MBX_NOT_FINISHED)
12300 error = -EIO;
12301 else
12302 error = 0;
12303
12304fail_fcf_read:
12305 if (error && mboxq)
12306 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12307 return error;
12308}
12309
12310/**
12311 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12312 * @phba: pointer to lpfc hba data structure.
12313 * @fcf_index: FCF table entry offset.
12314 *
12315 * This routine is invoked to read an FCF record indicated by @fcf_index to
12316 * determine whether it's eligible for FLOGI round robin failover list.
12317 *
12318 * Return 0 if the mailbox command is submitted sucessfully, none 0
12319 * otherwise.
12320 **/
12321int
12322lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12323{
12324 int rc = 0, error;
12325 LPFC_MBOXQ_t *mboxq;
12326
12327 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12328 if (!mboxq) {
12329 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12330 "2758 Failed to allocate mbox for "
12331 "READ_FCF cmd\n");
12332 error = -ENOMEM;
12333 goto fail_fcf_read;
12334 }
12335 /* Construct the read FCF record mailbox command */
12336 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12337 if (rc) {
12338 error = -EINVAL;
12339 goto fail_fcf_read;
12340 }
12341 /* Issue the mailbox command asynchronously */
12342 mboxq->vport = phba->pport;
12343 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12344 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12345 if (rc == MBX_NOT_FINISHED)
12346 error = -EIO;
12347 else
12348 error = 0;
12349
12350fail_fcf_read:
12351 if (error && mboxq)
12352 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12353 return error;
12354}
12355
12356/**
12357 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12358 * @phba: pointer to lpfc hba data structure.
12359 *
12360 * This routine is to get the next eligible FCF record index in a round
12361 * robin fashion. If the next eligible FCF record index equals to the
12362 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12363 * shall be returned, otherwise, the next eligible FCF record's index
12364 * shall be returned.
12365 **/
12366uint16_t
12367lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12368{
12369 uint16_t next_fcf_index;
12370
12371 /* Search from the currently registered FCF index */
12372 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12373 LPFC_SLI4_FCF_TBL_INDX_MAX,
12374 phba->fcf.current_rec.fcf_indx);
12375 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12376 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12377 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12378 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12379 /* Round robin failover stop condition */
12380 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12381 return LPFC_FCOE_FCF_NEXT_NONE;
12382
12383 return next_fcf_index;
12384}
12385
12386/**
12387 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12388 * @phba: pointer to lpfc hba data structure.
12389 *
12390 * This routine sets the FCF record index in to the eligible bmask for
12391 * round robin failover search. It checks to make sure that the index
12392 * does not go beyond the range of the driver allocated bmask dimension
12393 * before setting the bit.
12394 *
12395 * Returns 0 if the index bit successfully set, otherwise, it returns
12396 * -EINVAL.
12397 **/
12398int
12399lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12400{
12401 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12402 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12403 "2610 HBA FCF index reached driver's "
12404 "book keeping dimension: fcf_index:%d, "
12405 "driver_bmask_max:%d\n",
12406 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12407 return -EINVAL;
12408 }
12409 /* Set the eligible FCF record index bmask */
12410 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12411
12412 return 0;
12413}
12414
12415/**
12416 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12417 * @phba: pointer to lpfc hba data structure.
12418 *
12419 * This routine clears the FCF record index from the eligible bmask for
12420 * round robin failover search. It checks to make sure that the index
12421 * does not go beyond the range of the driver allocated bmask dimension
12422 * before clearing the bit.
12423 **/
12424void
12425lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12426{
12427 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12428 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12429 "2762 HBA FCF index goes beyond driver's "
12430 "book keeping dimension: fcf_index:%d, "
12431 "driver_bmask_max:%d\n",
12432 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12433 return;
12434 }
12435 /* Clear the eligible FCF record index bmask */
12436 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12437}
12438
12439/**
James Smartecfd03c2010-02-12 14:41:27 -050012440 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12441 * @phba: pointer to lpfc hba data structure.
12442 *
12443 * This routine is the completion routine for the rediscover FCF table mailbox
12444 * command. If the mailbox command returned failure, it will try to stop the
12445 * FCF rediscover wait timer.
12446 **/
12447void
12448lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12449{
12450 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12451 uint32_t shdr_status, shdr_add_status;
12452
12453 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12454
12455 shdr_status = bf_get(lpfc_mbox_hdr_status,
12456 &redisc_fcf->header.cfg_shdr.response);
12457 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12458 &redisc_fcf->header.cfg_shdr.response);
12459 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050012460 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050012461 "2746 Requesting for FCF rediscovery failed "
12462 "status x%x add_status x%x\n",
12463 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050012464 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050012465 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012466 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012467 spin_unlock_irq(&phba->hbalock);
12468 /*
12469 * CVL event triggered FCF rediscover request failed,
12470 * last resort to re-try current registered FCF entry.
12471 */
12472 lpfc_retry_pport_discovery(phba);
12473 } else {
12474 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050012475 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050012476 spin_unlock_irq(&phba->hbalock);
12477 /*
12478 * DEAD FCF event triggered FCF rediscover request
12479 * failed, last resort to fail over as a link down
12480 * to FCF registration.
12481 */
12482 lpfc_sli4_fcf_dead_failthrough(phba);
12483 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012484 } else {
12485 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12486 "2775 Start FCF rediscovery quiescent period "
12487 "wait timer before scaning FCF table\n");
James Smartecfd03c2010-02-12 14:41:27 -050012488 /*
12489 * Start FCF rediscovery wait timer for pending FCF
12490 * before rescan FCF record table.
12491 */
12492 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050012493 }
James Smartecfd03c2010-02-12 14:41:27 -050012494
12495 mempool_free(mbox, phba->mbox_mem_pool);
12496}
12497
12498/**
12499 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12500 * @phba: pointer to lpfc hba data structure.
12501 *
12502 * This routine is invoked to request for rediscovery of the entire FCF table
12503 * by the port.
12504 **/
12505int
12506lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12507{
12508 LPFC_MBOXQ_t *mbox;
12509 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12510 int rc, length;
12511
James Smart0c9ab6f2010-02-26 14:15:57 -050012512 /* Cancel retry delay timers to all vports before FCF rediscover */
12513 lpfc_cancel_all_vport_retry_delay_timer(phba);
12514
James Smartecfd03c2010-02-12 14:41:27 -050012515 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12516 if (!mbox) {
12517 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12518 "2745 Failed to allocate mbox for "
12519 "requesting FCF rediscover.\n");
12520 return -ENOMEM;
12521 }
12522
12523 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12524 sizeof(struct lpfc_sli4_cfg_mhdr));
12525 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12526 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12527 length, LPFC_SLI4_MBX_EMBED);
12528
12529 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12530 /* Set count to 0 for invalidating the entire FCF database */
12531 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12532
12533 /* Issue the mailbox command asynchronously */
12534 mbox->vport = phba->pport;
12535 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12536 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12537
12538 if (rc == MBX_NOT_FINISHED) {
12539 mempool_free(mbox, phba->mbox_mem_pool);
12540 return -EIO;
12541 }
12542 return 0;
12543}
12544
12545/**
James Smartfc2b9892010-02-26 14:15:29 -050012546 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12547 * @phba: pointer to lpfc hba data structure.
12548 *
12549 * This function is the failover routine as a last resort to the FCF DEAD
12550 * event when driver failed to perform fast FCF failover.
12551 **/
12552void
12553lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12554{
12555 uint32_t link_state;
12556
12557 /*
12558 * Last resort as FCF DEAD event failover will treat this as
12559 * a link down, but save the link state because we don't want
12560 * it to be changed to Link Down unless it is already down.
12561 */
12562 link_state = phba->link_state;
12563 lpfc_linkdown(phba);
12564 phba->link_state = link_state;
12565
12566 /* Unregister FCF if no devices connected to it */
12567 lpfc_unregister_unused_fcf(phba);
12568}
12569
12570/**
James Smarta0c87cb2009-07-19 10:01:10 -040012571 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12572 * @phba: pointer to lpfc hba data structure.
12573 *
12574 * This function read region 23 and parse TLV for port status to
12575 * decide if the user disaled the port. If the TLV indicates the
12576 * port is disabled, the hba_flag is set accordingly.
12577 **/
12578void
12579lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12580{
12581 LPFC_MBOXQ_t *pmb = NULL;
12582 MAILBOX_t *mb;
12583 uint8_t *rgn23_data = NULL;
12584 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12585 int rc;
12586
12587 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12588 if (!pmb) {
12589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12590 "2600 lpfc_sli_read_serdes_param failed to"
12591 " allocate mailbox memory\n");
12592 goto out;
12593 }
12594 mb = &pmb->u.mb;
12595
12596 /* Get adapter Region 23 data */
12597 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12598 if (!rgn23_data)
12599 goto out;
12600
12601 do {
12602 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12603 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12604
12605 if (rc != MBX_SUCCESS) {
12606 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12607 "2601 lpfc_sli_read_link_ste failed to"
12608 " read config region 23 rc 0x%x Status 0x%x\n",
12609 rc, mb->mbxStatus);
12610 mb->un.varDmp.word_cnt = 0;
12611 }
12612 /*
12613 * dump mem may return a zero when finished or we got a
12614 * mailbox error, either way we are done.
12615 */
12616 if (mb->un.varDmp.word_cnt == 0)
12617 break;
12618 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12619 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12620
12621 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12622 rgn23_data + offset,
12623 mb->un.varDmp.word_cnt);
12624 offset += mb->un.varDmp.word_cnt;
12625 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12626
12627 data_size = offset;
12628 offset = 0;
12629
12630 if (!data_size)
12631 goto out;
12632
12633 /* Check the region signature first */
12634 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12636 "2619 Config region 23 has bad signature\n");
12637 goto out;
12638 }
12639 offset += 4;
12640
12641 /* Check the data structure version */
12642 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12644 "2620 Config region 23 has bad version\n");
12645 goto out;
12646 }
12647 offset += 4;
12648
12649 /* Parse TLV entries in the region */
12650 while (offset < data_size) {
12651 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12652 break;
12653 /*
12654 * If the TLV is not driver specific TLV or driver id is
12655 * not linux driver id, skip the record.
12656 */
12657 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12658 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12659 (rgn23_data[offset + 3] != 0)) {
12660 offset += rgn23_data[offset + 1] * 4 + 4;
12661 continue;
12662 }
12663
12664 /* Driver found a driver specific TLV in the config region */
12665 sub_tlv_len = rgn23_data[offset + 1] * 4;
12666 offset += 4;
12667 tlv_offset = 0;
12668
12669 /*
12670 * Search for configured port state sub-TLV.
12671 */
12672 while ((offset < data_size) &&
12673 (tlv_offset < sub_tlv_len)) {
12674 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12675 offset += 4;
12676 tlv_offset += 4;
12677 break;
12678 }
12679 if (rgn23_data[offset] != PORT_STE_TYPE) {
12680 offset += rgn23_data[offset + 1] * 4 + 4;
12681 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12682 continue;
12683 }
12684
12685 /* This HBA contains PORT_STE configured */
12686 if (!rgn23_data[offset + 2])
12687 phba->hba_flag |= LINK_DISABLED;
12688
12689 goto out;
12690 }
12691 }
12692out:
12693 if (pmb)
12694 mempool_free(pmb, phba->mbox_mem_pool);
12695 kfree(rgn23_data);
12696 return;
12697}
James Smart695a8142010-01-26 23:08:03 -050012698
12699/**
12700 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12701 * @vport: pointer to vport data structure.
12702 *
12703 * This function iterate through the mailboxq and clean up all REG_LOGIN
12704 * and REG_VPI mailbox commands associated with the vport. This function
12705 * is called when driver want to restart discovery of the vport due to
12706 * a Clear Virtual Link event.
12707 **/
12708void
12709lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12710{
12711 struct lpfc_hba *phba = vport->phba;
12712 LPFC_MBOXQ_t *mb, *nextmb;
12713 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040012714 struct lpfc_nodelist *ndlp;
James Smart695a8142010-01-26 23:08:03 -050012715
12716 spin_lock_irq(&phba->hbalock);
12717 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12718 if (mb->vport != vport)
12719 continue;
12720
12721 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12722 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12723 continue;
12724
12725 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12726 mp = (struct lpfc_dmabuf *) (mb->context1);
12727 if (mp) {
12728 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12729 kfree(mp);
12730 }
James Smart78730cf2010-04-06 15:06:30 -040012731 ndlp = (struct lpfc_nodelist *) mb->context2;
12732 if (ndlp) {
12733 lpfc_nlp_put(ndlp);
12734 mb->context2 = NULL;
12735 }
James Smart695a8142010-01-26 23:08:03 -050012736 }
12737 list_del(&mb->list);
12738 mempool_free(mb, phba->mbox_mem_pool);
12739 }
12740 mb = phba->sli.mbox_active;
12741 if (mb && (mb->vport == vport)) {
12742 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12743 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12744 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart78730cf2010-04-06 15:06:30 -040012745 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12746 ndlp = (struct lpfc_nodelist *) mb->context2;
12747 if (ndlp) {
12748 lpfc_nlp_put(ndlp);
12749 mb->context2 = NULL;
12750 }
12751 /* Unregister the RPI when mailbox complete */
12752 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12753 }
James Smart695a8142010-01-26 23:08:03 -050012754 }
12755 spin_unlock_irq(&phba->hbalock);
12756}
12757
James Smart2a9bf3d2010-06-07 15:24:45 -040012758/**
12759 * lpfc_drain_txq - Drain the txq
12760 * @phba: Pointer to HBA context object.
12761 *
12762 * This function attempt to submit IOCBs on the txq
12763 * to the adapter. For SLI4 adapters, the txq contains
12764 * ELS IOCBs that have been deferred because the there
12765 * are no SGLs. This congestion can occur with large
12766 * vport counts during node discovery.
12767 **/
12768
12769uint32_t
12770lpfc_drain_txq(struct lpfc_hba *phba)
12771{
12772 LIST_HEAD(completions);
12773 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12774 struct lpfc_iocbq *piocbq = 0;
12775 unsigned long iflags = 0;
12776 char *fail_msg = NULL;
12777 struct lpfc_sglq *sglq;
12778 union lpfc_wqe wqe;
12779
12780 spin_lock_irqsave(&phba->hbalock, iflags);
12781 if (pring->txq_cnt > pring->txq_max)
12782 pring->txq_max = pring->txq_cnt;
12783
12784 spin_unlock_irqrestore(&phba->hbalock, iflags);
12785
12786 while (pring->txq_cnt) {
12787 spin_lock_irqsave(&phba->hbalock, iflags);
12788
12789 sglq = __lpfc_sli_get_sglq(phba);
12790 if (!sglq) {
12791 spin_unlock_irqrestore(&phba->hbalock, iflags);
12792 break;
12793 } else {
12794 piocbq = lpfc_sli_ringtx_get(phba, pring);
12795 if (!piocbq) {
12796 /* The txq_cnt out of sync. This should
12797 * never happen
12798 */
12799 sglq = __lpfc_clear_active_sglq(phba,
12800 sglq->sli4_xritag);
12801 spin_unlock_irqrestore(&phba->hbalock, iflags);
12802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12803 "2823 txq empty and txq_cnt is %d\n ",
12804 pring->txq_cnt);
12805 break;
12806 }
12807 }
12808
12809 /* The xri and iocb resources secured,
12810 * attempt to issue request
12811 */
12812 piocbq->sli4_xritag = sglq->sli4_xritag;
12813 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12814 fail_msg = "to convert bpl to sgl";
12815 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12816 fail_msg = "to convert iocb to wqe";
12817 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12818 fail_msg = " - Wq is full";
12819 else
12820 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12821
12822 if (fail_msg) {
12823 /* Failed means we can't issue and need to cancel */
12824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12825 "2822 IOCB failed %s iotag 0x%x "
12826 "xri 0x%x\n",
12827 fail_msg,
12828 piocbq->iotag, piocbq->sli4_xritag);
12829 list_add_tail(&piocbq->list, &completions);
12830 }
12831 spin_unlock_irqrestore(&phba->hbalock, iflags);
12832 }
12833
12834 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12835 phba->pport->work_port_events &= ~WORKER_SERVICE_TXQ;
12836 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12837
12838 /* Cancel all the IOCBs that cannot be issued */
12839 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12840 IOERR_SLI_ABORTED);
12841
12842 return pring->txq_cnt;
12843}