blob: 97bbafb21bc21a97d714a6e9ffb51d4f45ff8bdf [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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 Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70
James Smart4f774512009-05-22 14:52:35 -040071static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
92 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93 struct lpfc_register doorbell;
94 uint32_t host_index;
95
96 /* If the host has not yet processed the next entry then we are done */
97 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98 return -ENOMEM;
99 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500100 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400101 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500102 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400104 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
105
106 /* Update the host index before invoking device */
107 host_index = q->host_index;
108 q->host_index = ((q->host_index + 1) % q->entry_count);
109
110 /* Ring Doorbell */
111 doorbell.word0 = 0;
112 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
117
118 return 0;
119}
120
121/**
122 * lpfc_sli4_wq_release - Updates internal hba index for WQ
123 * @q: The Work Queue to operate on.
124 * @index: The index to advance the hba index to.
125 *
126 * This routine will update the HBA index of a queue to reflect consumption of
127 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128 * an entry the host calls this function to update the queue's internal
129 * pointers. This routine returns the number of entries that were consumed by
130 * the HBA.
131 **/
132static uint32_t
133lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
134{
135 uint32_t released = 0;
136
137 if (q->hba_index == index)
138 return 0;
139 do {
140 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141 released++;
142 } while (q->hba_index != index);
143 return released;
144}
145
146/**
147 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148 * @q: The Mailbox Queue to operate on.
149 * @wqe: The Mailbox Queue Entry to put on the Work queue.
150 *
151 * This routine will copy the contents of @mqe to the next available entry on
152 * the @q. This function will then ring the Work Queue Doorbell to signal the
153 * HBA to start processing the Work Queue Entry. This function returns 0 if
154 * successful. If no entries are available on @q then this function will return
155 * -ENOMEM.
156 * The caller is expected to hold the hbalock when calling this routine.
157 **/
158static uint32_t
159lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
160{
161 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162 struct lpfc_register doorbell;
163 uint32_t host_index;
164
165 /* If the host has not yet processed the next entry then we are done */
166 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167 return -ENOMEM;
168 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169 /* Save off the mailbox pointer for completion */
170 q->phba->mbox = (MAILBOX_t *)temp_mqe;
171
172 /* Update the host index before invoking device */
173 host_index = q->host_index;
174 q->host_index = ((q->host_index + 1) % q->entry_count);
175
176 /* Ring Doorbell */
177 doorbell.word0 = 0;
178 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182 return 0;
183}
184
185/**
186 * lpfc_sli4_mq_release - Updates internal hba index for MQ
187 * @q: The Mailbox Queue to operate on.
188 *
189 * This routine will update the HBA index of a queue to reflect consumption of
190 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191 * an entry the host calls this function to update the queue's internal
192 * pointers. This routine returns the number of entries that were consumed by
193 * the HBA.
194 **/
195static uint32_t
196lpfc_sli4_mq_release(struct lpfc_queue *q)
197{
198 /* Clear the mailbox pointer for completion */
199 q->phba->mbox = NULL;
200 q->hba_index = ((q->hba_index + 1) % q->entry_count);
201 return 1;
202}
203
204/**
205 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206 * @q: The Event Queue to get the first valid EQE from
207 *
208 * This routine will get the first valid Event Queue Entry from @q, update
209 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210 * the Queue (no more work to do), or the Queue is full of EQEs that have been
211 * processed, but not popped back to the HBA then this routine will return NULL.
212 **/
213static struct lpfc_eqe *
214lpfc_sli4_eq_get(struct lpfc_queue *q)
215{
216 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
217
218 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400219 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400220 return NULL;
221 /* If the host has not yet processed the next entry then we are done */
222 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223 return NULL;
224
225 q->hba_index = ((q->hba_index + 1) % q->entry_count);
226 return eqe;
227}
228
229/**
230 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231 * @q: The Event Queue that the host has completed processing for.
232 * @arm: Indicates whether the host wants to arms this CQ.
233 *
234 * This routine will mark all Event Queue Entries on @q, from the last
235 * known completed entry to the last entry that was processed, as completed
236 * by clearing the valid bit for each completion queue entry. Then it will
237 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238 * The internal host index in the @q will be updated by this routine to indicate
239 * that the host has finished processing the entries. The @arm parameter
240 * indicates that the queue should be rearmed when ringing the doorbell.
241 *
242 * This function will return the number of EQEs that were popped.
243 **/
244uint32_t
245lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
246{
247 uint32_t released = 0;
248 struct lpfc_eqe *temp_eqe;
249 struct lpfc_register doorbell;
250
251 /* while there are valid entries */
252 while (q->hba_index != q->host_index) {
253 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400254 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400255 released++;
256 q->host_index = ((q->host_index + 1) % q->entry_count);
257 }
258 if (unlikely(released == 0 && !arm))
259 return 0;
260
261 /* ring doorbell for number popped */
262 doorbell.word0 = 0;
263 if (arm) {
264 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
266 }
267 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500271 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400274 return released;
275}
276
277/**
278 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279 * @q: The Completion Queue to get the first valid CQE from
280 *
281 * This routine will get the first valid Completion Queue Entry from @q, update
282 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283 * the Queue (no more work to do), or the Queue is full of CQEs that have been
284 * processed, but not popped back to the HBA then this routine will return NULL.
285 **/
286static struct lpfc_cqe *
287lpfc_sli4_cq_get(struct lpfc_queue *q)
288{
289 struct lpfc_cqe *cqe;
290
291 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400292 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400293 return NULL;
294 /* If the host has not yet processed the next entry then we are done */
295 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296 return NULL;
297
298 cqe = q->qe[q->hba_index].cqe;
299 q->hba_index = ((q->hba_index + 1) % q->entry_count);
300 return cqe;
301}
302
303/**
304 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305 * @q: The Completion Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
307 *
308 * This routine will mark all Completion queue entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
315 *
316 * This function will return the number of CQEs that were released.
317 **/
318uint32_t
319lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
320{
321 uint32_t released = 0;
322 struct lpfc_cqe *temp_qe;
323 struct lpfc_register doorbell;
324
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400328 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm)
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343 return released;
344}
345
346/**
347 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348 * @q: The Header Receive Queue to operate on.
349 * @wqe: The Receive Queue Entry to put on the Receive queue.
350 *
351 * This routine will copy the contents of @wqe to the next available entry on
352 * the @q. This function will then ring the Receive Queue Doorbell to signal the
353 * HBA to start processing the Receive Queue Entry. This function returns the
354 * index that the rqe was copied to if successful. If no entries are available
355 * on @q then this function will return -ENOMEM.
356 * The caller is expected to hold the hbalock when calling this routine.
357 **/
358static int
359lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
361{
362 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364 struct lpfc_register doorbell;
365 int put_index = hq->host_index;
366
367 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368 return -EINVAL;
369 if (hq->host_index != dq->host_index)
370 return -EINVAL;
371 /* If the host has not yet processed the next entry then we are done */
372 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373 return -EBUSY;
374 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
376
377 /* Update the host index to point to the next slot */
378 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
380
381 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400382 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400385 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400386 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
388 }
389 return put_index;
390}
391
392/**
393 * lpfc_sli4_rq_release - Updates internal hba index for RQ
394 * @q: The Header Receive Queue to operate on.
395 *
396 * This routine will update the HBA index of a queue to reflect consumption of
397 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398 * consumed an entry the host calls this function to update the queue's
399 * internal pointers. This routine returns the number of entries that were
400 * consumed by the HBA.
401 **/
402static uint32_t
403lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
404{
405 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406 return 0;
407 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409 return 1;
410}
411
James Smarte59058c2008-08-24 21:49:00 -0400412/**
James Smart3621a712009-04-06 18:47:14 -0400413 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400414 * @phba: Pointer to HBA context object.
415 * @pring: Pointer to driver SLI ring object.
416 *
417 * This function returns pointer to next command iocb entry
418 * in the command ring. The caller must hold hbalock to prevent
419 * other threads consume the next command iocb.
420 * SLI-2/SLI-3 provide different sized iocbs.
421 **/
James Smarted957682007-06-17 19:56:37 -0500422static inline IOCB_t *
423lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
424{
425 return (IOCB_t *) (((char *) pring->cmdringaddr) +
426 pring->cmdidx * phba->iocb_cmd_size);
427}
428
James Smarte59058c2008-08-24 21:49:00 -0400429/**
James Smart3621a712009-04-06 18:47:14 -0400430 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400431 * @phba: Pointer to HBA context object.
432 * @pring: Pointer to driver SLI ring object.
433 *
434 * This function returns pointer to next response iocb entry
435 * in the response ring. The caller must hold hbalock to make sure
436 * that no other thread consume the next response iocb.
437 * SLI-2/SLI-3 provide different sized iocbs.
438 **/
James Smarted957682007-06-17 19:56:37 -0500439static inline IOCB_t *
440lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
441{
442 return (IOCB_t *) (((char *) pring->rspringaddr) +
443 pring->rspidx * phba->iocb_rsp_size);
444}
445
James Smarte59058c2008-08-24 21:49:00 -0400446/**
James Smart3621a712009-04-06 18:47:14 -0400447 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400448 * @phba: Pointer to HBA context object.
449 *
450 * This function is called with hbalock held. This function
451 * allocates a new driver iocb object from the iocb pool. If the
452 * allocation is successful, it returns pointer to the newly
453 * allocated iocb object else it returns NULL.
454 **/
James Smart2e0fef82007-06-17 19:56:36 -0500455static struct lpfc_iocbq *
456__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400457{
458 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459 struct lpfc_iocbq * iocbq = NULL;
460
461 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400462 if (iocbq)
463 phba->iocb_cnt++;
464 if (phba->iocb_cnt > phba->iocb_max)
465 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400466 return iocbq;
467}
468
James Smarte59058c2008-08-24 21:49:00 -0400469/**
James Smartda0436e2009-05-22 14:51:39 -0400470 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471 * @phba: Pointer to HBA context object.
472 * @xritag: XRI value.
473 *
474 * This function clears the sglq pointer from the array of acive
475 * sglq's. The xritag that is passed in is used to index into the
476 * array. Before the xritag can be used it needs to be adjusted
477 * by subtracting the xribase.
478 *
479 * Returns sglq ponter = success, NULL = Failure.
480 **/
481static struct lpfc_sglq *
482__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
483{
James Smartda0436e2009-05-22 14:51:39 -0400484 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400485
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400488 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{
James Smartda0436e2009-05-22 14:51:39 -0400506 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400507
508 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400509 return sglq;
510}
511
512/**
James Smart19ca7602010-11-20 23:11:55 -0500513 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514 * @phba: Pointer to HBA context object.
515 * @ndlp: nodelist pointer for this target.
516 * @xritag: xri used in this exchange.
517 * @rxid: Remote Exchange ID.
518 * @send_rrq: Flag used to determine if we should send rrq els cmd.
519 *
520 * This function is called with hbalock held.
521 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522 * rrq struct and adds it to the active_rrq_list.
523 *
524 * returns 0 for rrq slot for this xri
525 * < 0 Were not able to get rrq mem or invalid parameter.
526 **/
527static int
528__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
530{
James Smart19ca7602010-11-20 23:11:55 -0500531 struct lpfc_node_rrq *rrq;
532 int empty;
James Smart1151e3e2011-02-16 12:39:35 -0500533 uint32_t did = 0;
534
535
536 if (!ndlp)
537 return -EINVAL;
538
539 if (!phba->cfg_enable_rrq)
540 return -EINVAL;
541
542 if (phba->pport->load_flag & FC_UNLOADING) {
543 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544 goto out;
545 }
546 did = ndlp->nlp_DID;
James Smart19ca7602010-11-20 23:11:55 -0500547
548 /*
549 * set the active bit even if there is no mem available.
550 */
James Smart1151e3e2011-02-16 12:39:35 -0500551 if (NLP_CHK_FREE_REQ(ndlp))
552 goto out;
553
554 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555 goto out;
556
James Smart6d368e52011-05-24 11:44:12 -0400557 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart1151e3e2011-02-16 12:39:35 -0500558 goto out;
559
James Smart19ca7602010-11-20 23:11:55 -0500560 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561 if (rrq) {
562 rrq->send_rrq = send_rrq;
James Smart7851fe22011-07-22 18:36:52 -0400563 rrq->xritag = xritag;
James Smart19ca7602010-11-20 23:11:55 -0500564 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565 rrq->ndlp = ndlp;
566 rrq->nlp_DID = ndlp->nlp_DID;
567 rrq->vport = ndlp->vport;
568 rrq->rxid = rxid;
569 empty = list_empty(&phba->active_rrq_list);
James Smart1151e3e2011-02-16 12:39:35 -0500570 rrq->send_rrq = send_rrq;
James Smart19ca7602010-11-20 23:11:55 -0500571 list_add_tail(&rrq->list, &phba->active_rrq_list);
572 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573 phba->hba_flag |= HBA_RRQ_ACTIVE;
574 if (empty)
575 lpfc_worker_wake_up(phba);
576 }
577 return 0;
578 }
James Smart1151e3e2011-02-16 12:39:35 -0500579out:
580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582 " DID:0x%x Send:%d\n",
583 xritag, rxid, did, send_rrq);
584 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500585}
586
587/**
James Smart1151e3e2011-02-16 12:39:35 -0500588 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500589 * @phba: Pointer to HBA context object.
590 * @xritag: xri used in this exchange.
591 * @rrq: The RRQ to be cleared.
592 *
James Smart19ca7602010-11-20 23:11:55 -0500593 **/
James Smart1151e3e2011-02-16 12:39:35 -0500594void
595lpfc_clr_rrq_active(struct lpfc_hba *phba,
596 uint16_t xritag,
597 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500598{
James Smart1151e3e2011-02-16 12:39:35 -0500599 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500600
James Smart1151e3e2011-02-16 12:39:35 -0500601 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500603
604 /* The target DID could have been swapped (cable swap)
605 * we should use the ndlp from the findnode if it is
606 * available.
607 */
James Smart1151e3e2011-02-16 12:39:35 -0500608 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500609 ndlp = rrq->ndlp;
610
James Smart1151e3e2011-02-16 12:39:35 -0500611 if (!ndlp)
612 goto out;
613
James Smart6d368e52011-05-24 11:44:12 -0400614 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500615 rrq->send_rrq = 0;
616 rrq->xritag = 0;
617 rrq->rrq_stop_time = 0;
618 }
James Smart1151e3e2011-02-16 12:39:35 -0500619out:
James Smart19ca7602010-11-20 23:11:55 -0500620 mempool_free(rrq, phba->rrq_pool);
621}
622
623/**
624 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625 * @phba: Pointer to HBA context object.
626 *
627 * This function is called with hbalock held. This function
628 * Checks if stop_time (ratov from setting rrq active) has
629 * been reached, if it has and the send_rrq flag is set then
630 * it will call lpfc_send_rrq. If the send_rrq flag is not set
631 * then it will just call the routine to clear the rrq and
632 * free the rrq resource.
633 * The timer is set to the next rrq that is going to expire before
634 * leaving the routine.
635 *
636 **/
637void
638lpfc_handle_rrq_active(struct lpfc_hba *phba)
639{
640 struct lpfc_node_rrq *rrq;
641 struct lpfc_node_rrq *nextrrq;
642 unsigned long next_time;
643 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500644 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500645
646 spin_lock_irqsave(&phba->hbalock, iflags);
647 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648 next_time = jiffies + HZ * (phba->fc_ratov + 1);
649 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500650 &phba->active_rrq_list, list) {
651 if (time_after(jiffies, rrq->rrq_stop_time))
652 list_move(&rrq->list, &send_rrq);
653 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500654 next_time = rrq->rrq_stop_time;
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500659 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660 list_del(&rrq->list);
661 if (!rrq->send_rrq)
662 /* this call will free the rrq */
663 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664 else if (lpfc_send_rrq(phba, rrq)) {
665 /* if we send the rrq then the completion handler
666 * will clear the bit in the xribitmap.
667 */
668 lpfc_clr_rrq_active(phba, rrq->xritag,
669 rrq);
670 }
671 }
James Smart19ca7602010-11-20 23:11:55 -0500672}
673
674/**
675 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676 * @vport: Pointer to vport context object.
677 * @xri: The xri used in the exchange.
678 * @did: The targets DID for this exchange.
679 *
680 * returns NULL = rrq not found in the phba->active_rrq_list.
681 * rrq = rrq for this xri and target.
682 **/
683struct lpfc_node_rrq *
684lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
685{
686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long iflags;
690
691 if (phba->sli_rev != LPFC_SLI_REV4)
692 return NULL;
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695 if (rrq->vport == vport && rrq->xritag == xri &&
696 rrq->nlp_DID == did){
697 list_del(&rrq->list);
698 spin_unlock_irqrestore(&phba->hbalock, iflags);
699 return rrq;
700 }
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return NULL;
704}
705
706/**
707 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500709 * @ndlp: Pointer to the lpfc_node_list structure.
710 * If ndlp is NULL Remove all active RRQs for this vport from the
711 * phba->active_rrq_list and clear the rrq.
712 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500713 **/
714void
James Smart1151e3e2011-02-16 12:39:35 -0500715lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500716
717{
718 struct lpfc_hba *phba = vport->phba;
719 struct lpfc_node_rrq *rrq;
720 struct lpfc_node_rrq *nextrrq;
721 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500722 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500723
724 if (phba->sli_rev != LPFC_SLI_REV4)
725 return;
James Smart1151e3e2011-02-16 12:39:35 -0500726 if (!ndlp) {
727 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500729 }
James Smart1151e3e2011-02-16 12:39:35 -0500730 spin_lock_irqsave(&phba->hbalock, iflags);
731 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
733 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500734 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500735
736 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737 list_del(&rrq->list);
738 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
739 }
James Smart19ca7602010-11-20 23:11:55 -0500740}
741
742/**
743 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744 * @phba: Pointer to HBA context object.
745 *
746 * Remove all rrqs from the phba->active_rrq_list and free them by
747 * calling __lpfc_clr_active_rrq
748 *
749 **/
750void
751lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
752{
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long next_time;
756 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500757 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500758
759 if (phba->sli_rev != LPFC_SLI_REV4)
760 return;
761 spin_lock_irqsave(&phba->hbalock, iflags);
762 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500764 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500765 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500766
767 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
768 list_del(&rrq->list);
769 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
770 }
James Smart19ca7602010-11-20 23:11:55 -0500771 if (!list_empty(&phba->active_rrq_list))
772 mod_timer(&phba->rrq_tmr, next_time);
773}
774
775
776/**
James Smart1151e3e2011-02-16 12:39:35 -0500777 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500778 * @phba: Pointer to HBA context object.
779 * @ndlp: Targets nodelist pointer for this exchange.
780 * @xritag the xri in the bitmap to test.
781 *
782 * This function is called with hbalock held. This function
783 * returns 0 = rrq not active for this xri
784 * 1 = rrq is valid for this xri.
785 **/
James Smart1151e3e2011-02-16 12:39:35 -0500786int
787lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500788 uint16_t xritag)
789{
James Smart19ca7602010-11-20 23:11:55 -0500790 if (!ndlp)
791 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400792 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500793 return 1;
794 else
795 return 0;
796}
797
798/**
799 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: nodelist pointer for this target.
802 * @xritag: xri used in this exchange.
803 * @rxid: Remote Exchange ID.
804 * @send_rrq: Flag used to determine if we should send rrq els cmd.
805 *
806 * This function takes the hbalock.
807 * The active bit is always set in the active rrq xri_bitmap even
808 * if there is no slot avaiable for the other rrq information.
809 *
810 * returns 0 rrq actived for this xri
811 * < 0 No memory or invalid ndlp.
812 **/
813int
814lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
816{
817 int ret;
818 unsigned long iflags;
819
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return ret;
824}
825
826/**
James Smartda0436e2009-05-22 14:51:39 -0400827 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500829 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400830 *
831 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400832 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400833 * list is not empty then it is successful, it returns pointer to the newly
834 * allocated sglq object else it returns NULL.
835 **/
836static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500837__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400838{
839 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500841 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500842 struct lpfc_scsi_buf *lpfc_cmd;
843 struct lpfc_nodelist *ndlp;
844 int found = 0;
845
846 if (piocbq->iocb_flag & LPFC_IO_FCP) {
847 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500849 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500851 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500852 else
853 ndlp = piocbq->context1;
854
James Smartda0436e2009-05-22 14:51:39 -0400855 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500856 start_sglq = sglq;
857 while (!found) {
858 if (!sglq)
859 return NULL;
James Smart1151e3e2011-02-16 12:39:35 -0500860 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500861 /* This xri has an rrq outstanding for this DID.
862 * put it back in the list and get another xri.
863 */
864 list_add_tail(&sglq->list, lpfc_sgl_list);
865 sglq = NULL;
866 list_remove_head(lpfc_sgl_list, sglq,
867 struct lpfc_sglq, list);
868 if (sglq == start_sglq) {
869 sglq = NULL;
870 break;
871 } else
872 continue;
873 }
874 sglq->ndlp = ndlp;
875 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400876 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500877 sglq->state = SGL_ALLOCATED;
878 }
James Smartda0436e2009-05-22 14:51:39 -0400879 return sglq;
880}
881
882/**
James Smart3621a712009-04-06 18:47:14 -0400883 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400884 * @phba: Pointer to HBA context object.
885 *
886 * This function is called with no lock held. This function
887 * allocates a new driver iocb object from the iocb pool. If the
888 * allocation is successful, it returns pointer to the newly
889 * allocated iocb object else it returns NULL.
890 **/
James Smart2e0fef82007-06-17 19:56:36 -0500891struct lpfc_iocbq *
892lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500893{
James Smart2e0fef82007-06-17 19:56:36 -0500894 struct lpfc_iocbq * iocbq = NULL;
895 unsigned long iflags;
896
897 spin_lock_irqsave(&phba->hbalock, iflags);
898 iocbq = __lpfc_sli_get_iocbq(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return iocbq;
901}
902
James Smarte59058c2008-08-24 21:49:00 -0400903/**
James Smart4f774512009-05-22 14:52:35 -0400904 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905 * @phba: Pointer to HBA context object.
906 * @iocbq: Pointer to driver iocb object.
907 *
908 * This function is called with hbalock held to release driver
909 * iocb object to the iocb pool. The iotag in the iocb object
910 * does not change for each use of the iocb object. This function
911 * clears all other fields of the iocb object when it is freed.
912 * The sqlq structure that holds the xritag and phys and virtual
913 * mappings for the scatter gather list is retrieved from the
914 * active array of sglq. The get of the sglq pointer also clears
915 * the entry in the array. If the status of the IO indiactes that
916 * this IO was aborted then the sglq entry it put on the
917 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918 * IO has good status or fails for any other reason then the sglq
919 * entry is added to the free list (lpfc_sgl_list).
920 **/
921static void
922__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
923{
924 struct lpfc_sglq *sglq;
925 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400926 unsigned long iflag = 0;
927 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400928
929 if (iocbq->sli4_xritag == NO_XRI)
930 sglq = NULL;
931 else
James Smart6d368e52011-05-24 11:44:12 -0400932 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
933
James Smart4f774512009-05-22 14:52:35 -0400934 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500935 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400937 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938 iflag);
939 list_add(&sglq->list,
940 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941 spin_unlock_irqrestore(
942 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500943 } else {
944 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500945 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500946 list_add_tail(&sglq->list,
947 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400948
949 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400950 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400951 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500952 }
James Smart4f774512009-05-22 14:52:35 -0400953 }
954
955
956 /*
957 * Clean all volatile data fields, preserve iotag and node struct.
958 */
959 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -0400960 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -0400961 iocbq->sli4_xritag = NO_XRI;
962 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
963}
964
James Smart2a9bf3d2010-06-07 15:24:45 -0400965
James Smart4f774512009-05-22 14:52:35 -0400966/**
James Smart3772a992009-05-22 14:50:54 -0400967 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
968 * @phba: Pointer to HBA context object.
969 * @iocbq: Pointer to driver iocb object.
970 *
971 * This function is called with hbalock held to release driver
972 * iocb object to the iocb pool. The iotag in the iocb object
973 * does not change for each use of the iocb object. This function
974 * clears all other fields of the iocb object when it is freed.
975 **/
976static void
977__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
978{
979 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
980
981 /*
982 * Clean all volatile data fields, preserve iotag and node struct.
983 */
984 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985 iocbq->sli4_xritag = NO_XRI;
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987}
988
989/**
James Smart3621a712009-04-06 18:47:14 -0400990 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100999static void
James Smart2e0fef82007-06-17 19:56:36 -05001000__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1001{
James Smart3772a992009-05-22 14:50:54 -04001002 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001003 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001004}
1005
James Smarte59058c2008-08-24 21:49:00 -04001006/**
James Smart3621a712009-04-06 18:47:14 -04001007 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001008 * @phba: Pointer to HBA context object.
1009 * @iocbq: Pointer to driver iocb object.
1010 *
1011 * This function is called with no lock held to release the iocb to
1012 * iocb pool.
1013 **/
James Smart2e0fef82007-06-17 19:56:36 -05001014void
1015lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1016{
1017 unsigned long iflags;
1018
1019 /*
1020 * Clean all volatile data fields, preserve iotag and node struct.
1021 */
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 __lpfc_sli_release_iocbq(phba, iocbq);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025}
1026
James Smarte59058c2008-08-24 21:49:00 -04001027/**
James Smarta257bf92009-04-06 18:48:10 -04001028 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029 * @phba: Pointer to HBA context object.
1030 * @iocblist: List of IOCBs.
1031 * @ulpstatus: ULP status in IOCB command field.
1032 * @ulpWord4: ULP word-4 in IOCB command field.
1033 *
1034 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035 * on the list by invoking the complete callback function associated with the
1036 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037 * fields.
1038 **/
1039void
1040lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041 uint32_t ulpstatus, uint32_t ulpWord4)
1042{
1043 struct lpfc_iocbq *piocb;
1044
1045 while (!list_empty(iocblist)) {
1046 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1047
1048 if (!piocb->iocb_cmpl)
1049 lpfc_sli_release_iocbq(phba, piocb);
1050 else {
1051 piocb->iocb.ulpStatus = ulpstatus;
1052 piocb->iocb.un.ulpWord[4] = ulpWord4;
1053 (piocb->iocb_cmpl) (phba, piocb, piocb);
1054 }
1055 }
1056 return;
1057}
1058
1059/**
James Smart3621a712009-04-06 18:47:14 -04001060 * lpfc_sli_iocb_cmd_type - Get the iocb type
1061 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001062 *
1063 * This function is called by ring event handler function to get the iocb type.
1064 * This function translates the iocb command to an iocb command type used to
1065 * decide the final disposition of each completed IOCB.
1066 * The function returns
1067 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068 * LPFC_SOL_IOCB if it is a solicited iocb completion
1069 * LPFC_ABORT_IOCB if it is an abort iocb
1070 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1071 *
1072 * The caller is not required to hold any lock.
1073 **/
dea31012005-04-17 16:05:31 -05001074static lpfc_iocb_type
1075lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1076{
1077 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1078
1079 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080 return 0;
1081
1082 switch (iocb_cmnd) {
1083 case CMD_XMIT_SEQUENCE_CR:
1084 case CMD_XMIT_SEQUENCE_CX:
1085 case CMD_XMIT_BCAST_CN:
1086 case CMD_XMIT_BCAST_CX:
1087 case CMD_ELS_REQUEST_CR:
1088 case CMD_ELS_REQUEST_CX:
1089 case CMD_CREATE_XRI_CR:
1090 case CMD_CREATE_XRI_CX:
1091 case CMD_GET_RPI_CN:
1092 case CMD_XMIT_ELS_RSP_CX:
1093 case CMD_GET_RPI_CR:
1094 case CMD_FCP_IWRITE_CR:
1095 case CMD_FCP_IWRITE_CX:
1096 case CMD_FCP_IREAD_CR:
1097 case CMD_FCP_IREAD_CX:
1098 case CMD_FCP_ICMND_CR:
1099 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001100 case CMD_FCP_TSEND_CX:
1101 case CMD_FCP_TRSP_CX:
1102 case CMD_FCP_TRECEIVE_CX:
1103 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001104 case CMD_ADAPTER_MSG:
1105 case CMD_ADAPTER_DUMP:
1106 case CMD_XMIT_SEQUENCE64_CR:
1107 case CMD_XMIT_SEQUENCE64_CX:
1108 case CMD_XMIT_BCAST64_CN:
1109 case CMD_XMIT_BCAST64_CX:
1110 case CMD_ELS_REQUEST64_CR:
1111 case CMD_ELS_REQUEST64_CX:
1112 case CMD_FCP_IWRITE64_CR:
1113 case CMD_FCP_IWRITE64_CX:
1114 case CMD_FCP_IREAD64_CR:
1115 case CMD_FCP_IREAD64_CX:
1116 case CMD_FCP_ICMND64_CR:
1117 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001118 case CMD_FCP_TSEND64_CX:
1119 case CMD_FCP_TRSP64_CX:
1120 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001121 case CMD_GEN_REQUEST64_CR:
1122 case CMD_GEN_REQUEST64_CX:
1123 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001124 case DSSCMD_IWRITE64_CR:
1125 case DSSCMD_IWRITE64_CX:
1126 case DSSCMD_IREAD64_CR:
1127 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001128 type = LPFC_SOL_IOCB;
1129 break;
1130 case CMD_ABORT_XRI_CN:
1131 case CMD_ABORT_XRI_CX:
1132 case CMD_CLOSE_XRI_CN:
1133 case CMD_CLOSE_XRI_CX:
1134 case CMD_XRI_ABORTED_CX:
1135 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001136 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001137 type = LPFC_ABORT_IOCB;
1138 break;
1139 case CMD_RCV_SEQUENCE_CX:
1140 case CMD_RCV_ELS_REQ_CX:
1141 case CMD_RCV_SEQUENCE64_CX:
1142 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001143 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001144 case CMD_IOCB_RCV_SEQ64_CX:
1145 case CMD_IOCB_RCV_ELS64_CX:
1146 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001147 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001148 type = LPFC_UNSOL_IOCB;
1149 break;
James Smart3163f722008-02-08 18:50:25 -05001150 case CMD_IOCB_XMIT_MSEQ64_CR:
1151 case CMD_IOCB_XMIT_MSEQ64_CX:
1152 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153 case CMD_IOCB_RCV_ELS_LIST64_CX:
1154 case CMD_IOCB_CLOSE_EXTENDED_CN:
1155 case CMD_IOCB_ABORT_EXTENDED_CN:
1156 case CMD_IOCB_RET_HBQE64_CN:
1157 case CMD_IOCB_FCP_IBIDIR64_CR:
1158 case CMD_IOCB_FCP_IBIDIR64_CX:
1159 case CMD_IOCB_FCP_ITASKMGT64_CX:
1160 case CMD_IOCB_LOGENTRY_CN:
1161 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001163 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001164 type = LPFC_UNKNOWN_IOCB;
1165 break;
dea31012005-04-17 16:05:31 -05001166 default:
1167 type = LPFC_UNKNOWN_IOCB;
1168 break;
1169 }
1170
1171 return type;
1172}
1173
James Smarte59058c2008-08-24 21:49:00 -04001174/**
James Smart3621a712009-04-06 18:47:14 -04001175 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001176 * @phba: Pointer to HBA context object.
1177 *
1178 * This function is called from SLI initialization code
1179 * to configure every ring of the HBA's SLI interface. The
1180 * caller is not required to hold any lock. This function issues
1181 * a config_ring mailbox command for each ring.
1182 * This function returns zero if successful else returns a negative
1183 * error code.
1184 **/
dea31012005-04-17 16:05:31 -05001185static int
James Smarted957682007-06-17 19:56:37 -05001186lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001187{
1188 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001189 LPFC_MBOXQ_t *pmb;
1190 MAILBOX_t *pmbox;
1191 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001192
James Smarted957682007-06-17 19:56:37 -05001193 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194 if (!pmb)
1195 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001196 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001197 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001198 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001199 lpfc_config_ring(phba, i, pmb);
1200 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001203 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001204 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001206 rc, pmbox->mbxCommand,
1207 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001208 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001209 ret = -ENXIO;
1210 break;
dea31012005-04-17 16:05:31 -05001211 }
1212 }
James Smarted957682007-06-17 19:56:37 -05001213 mempool_free(pmb, phba->mbox_mem_pool);
1214 return ret;
dea31012005-04-17 16:05:31 -05001215}
1216
James Smarte59058c2008-08-24 21:49:00 -04001217/**
James Smart3621a712009-04-06 18:47:14 -04001218 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001219 * @phba: Pointer to HBA context object.
1220 * @pring: Pointer to driver SLI ring object.
1221 * @piocb: Pointer to the driver iocb object.
1222 *
1223 * This function is called with hbalock held. The function adds the
1224 * new iocb to txcmplq of the given ring. This function always returns
1225 * 0. If this function is called for ELS ring, this function checks if
1226 * there is a vport associated with the ELS command. This function also
1227 * starts els_tmofunc timer if this is an ELS command.
1228 **/
dea31012005-04-17 16:05:31 -05001229static int
James Smart2e0fef82007-06-17 19:56:36 -05001230lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001232{
dea31012005-04-17 16:05:31 -05001233 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001234 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001235 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001236 if (pring->txcmplq_cnt > pring->txcmplq_max)
1237 pring->txcmplq_max = pring->txcmplq_cnt;
1238
James Smart92d7f7b2007-06-17 19:56:38 -05001239 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242 if (!piocb->vport)
1243 BUG();
1244 else
1245 mod_timer(&piocb->vport->els_tmofunc,
1246 jiffies + HZ * (phba->fc_ratov << 1));
1247 }
1248
dea31012005-04-17 16:05:31 -05001249
James Smart2e0fef82007-06-17 19:56:36 -05001250 return 0;
dea31012005-04-17 16:05:31 -05001251}
1252
James Smarte59058c2008-08-24 21:49:00 -04001253/**
James Smart3621a712009-04-06 18:47:14 -04001254 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001255 * @phba: Pointer to HBA context object.
1256 * @pring: Pointer to driver SLI ring object.
1257 *
1258 * This function is called with hbalock held to get next
1259 * iocb in txq of the given ring. If there is any iocb in
1260 * the txq, the function returns first iocb in the list after
1261 * removing the iocb from the list, else it returns NULL.
1262 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001263struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001264lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001265{
dea31012005-04-17 16:05:31 -05001266 struct lpfc_iocbq *cmd_iocb;
1267
James Smart858c9f62007-06-17 19:56:39 -05001268 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001270 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001271 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001272}
1273
James Smarte59058c2008-08-24 21:49:00 -04001274/**
James Smart3621a712009-04-06 18:47:14 -04001275 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001276 * @phba: Pointer to HBA context object.
1277 * @pring: Pointer to driver SLI ring object.
1278 *
1279 * This function is called with hbalock held and the caller must post the
1280 * iocb without releasing the lock. If the caller releases the lock,
1281 * iocb slot returned by the function is not guaranteed to be available.
1282 * The function returns pointer to the next available iocb slot if there
1283 * is available slot in the ring, else it returns NULL.
1284 * If the get index of the ring is ahead of the put index, the function
1285 * will post an error attention event to the worker thread to take the
1286 * HBA to offline state.
1287 **/
dea31012005-04-17 16:05:31 -05001288static IOCB_t *
1289lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1290{
James Smart34b02dc2008-08-24 21:49:55 -04001291 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001292 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001293 if ((pring->next_cmdidx == pring->cmdidx) &&
1294 (++pring->next_cmdidx >= max_cmd_idx))
1295 pring->next_cmdidx = 0;
1296
1297 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1298
1299 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1300
1301 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001303 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001304 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001305 pring->ringno,
dea31012005-04-17 16:05:31 -05001306 pring->local_getidx, max_cmd_idx);
1307
James Smart2e0fef82007-06-17 19:56:36 -05001308 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001309 /*
1310 * All error attention handlers are posted to
1311 * worker thread
1312 */
1313 phba->work_ha |= HA_ERATT;
1314 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001315
James Smart5e9d9b82008-06-14 22:52:53 -04001316 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001317
1318 return NULL;
1319 }
1320
1321 if (pring->local_getidx == pring->next_cmdidx)
1322 return NULL;
1323 }
1324
James Smarted957682007-06-17 19:56:37 -05001325 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001326}
1327
James Smarte59058c2008-08-24 21:49:00 -04001328/**
James Smart3621a712009-04-06 18:47:14 -04001329 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
1333 * This function gets an iotag for the iocb. If there is no unused iotag and
1334 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335 * array and assigns a new iotag.
1336 * The function returns the allocated iotag if successful, else returns zero.
1337 * Zero is not a valid iotag.
1338 * The caller is not required to hold any lock.
1339 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001340uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001341lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001342{
James Smart2e0fef82007-06-17 19:56:36 -05001343 struct lpfc_iocbq **new_arr;
1344 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001345 size_t new_len;
1346 struct lpfc_sli *psli = &phba->sli;
1347 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001348
James Smart2e0fef82007-06-17 19:56:36 -05001349 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001350 iotag = psli->last_iotag;
1351 if(++iotag < psli->iocbq_lookup_len) {
1352 psli->last_iotag = iotag;
1353 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001354 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001355 iocbq->iotag = iotag;
1356 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001357 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001358 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001360 spin_unlock_irq(&phba->hbalock);
1361 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001362 GFP_KERNEL);
1363 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001364 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001365 old_arr = psli->iocbq_lookup;
1366 if (new_len <= psli->iocbq_lookup_len) {
1367 /* highly unprobable case */
1368 kfree(new_arr);
1369 iotag = psli->last_iotag;
1370 if(++iotag < psli->iocbq_lookup_len) {
1371 psli->last_iotag = iotag;
1372 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001373 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001374 iocbq->iotag = iotag;
1375 return iotag;
1376 }
James Smart2e0fef82007-06-17 19:56:36 -05001377 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001378 return 0;
1379 }
1380 if (psli->iocbq_lookup)
1381 memcpy(new_arr, old_arr,
1382 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001383 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001384 psli->iocbq_lookup = new_arr;
1385 psli->iocbq_lookup_len = new_len;
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001388 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001389 iocbq->iotag = iotag;
1390 kfree(old_arr);
1391 return iotag;
1392 }
James Smart8f6d98d2006-08-01 07:34:00 -04001393 } else
James Smart2e0fef82007-06-17 19:56:36 -05001394 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001395
James Smartbc739052010-08-04 16:11:18 -04001396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001397 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001399
James Bottomley604a3e32005-10-29 10:28:33 -05001400 return 0;
dea31012005-04-17 16:05:31 -05001401}
1402
James Smarte59058c2008-08-24 21:49:00 -04001403/**
James Smart3621a712009-04-06 18:47:14 -04001404 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 * @iocb: Pointer to iocb slot in the ring.
1408 * @nextiocb: Pointer to driver iocb object which need to be
1409 * posted to firmware.
1410 *
1411 * This function is called with hbalock held to post a new iocb to
1412 * the firmware. This function copies the new iocb to ring iocb slot and
1413 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414 * a completion call back for this iocb else the function will free the
1415 * iocb object.
1416 **/
dea31012005-04-17 16:05:31 -05001417static void
1418lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1420{
1421 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001422 * Set up an iotag
dea31012005-04-17 16:05:31 -05001423 */
James Bottomley604a3e32005-10-29 10:28:33 -05001424 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001425
James Smarte2a0a9d2008-12-04 22:40:02 -05001426
James Smarta58cbd52007-08-02 11:09:43 -04001427 if (pring->ringno == LPFC_ELS_RING) {
1428 lpfc_debugfs_slow_ring_trc(phba,
1429 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1430 *(((uint32_t *) &nextiocb->iocb) + 4),
1431 *(((uint32_t *) &nextiocb->iocb) + 6),
1432 *(((uint32_t *) &nextiocb->iocb) + 7));
1433 }
1434
dea31012005-04-17 16:05:31 -05001435 /*
1436 * Issue iocb command to adapter
1437 */
James Smart92d7f7b2007-06-17 19:56:38 -05001438 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001439 wmb();
1440 pring->stats.iocb_cmd++;
1441
1442 /*
1443 * If there is no completion routine to call, we can release the
1444 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1446 */
1447 if (nextiocb->iocb_cmpl)
1448 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001449 else
James Smart2e0fef82007-06-17 19:56:36 -05001450 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001451
1452 /*
1453 * Let the HBA know what IOCB slot will be the next one the
1454 * driver will put a command into.
1455 */
1456 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001457 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001458}
1459
James Smarte59058c2008-08-24 21:49:00 -04001460/**
James Smart3621a712009-04-06 18:47:14 -04001461 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001462 * @phba: Pointer to HBA context object.
1463 * @pring: Pointer to driver SLI ring object.
1464 *
1465 * The caller is not required to hold any lock for calling this function.
1466 * This function updates the chip attention bits for the ring to inform firmware
1467 * that there are pending work to be done for this ring and requests an
1468 * interrupt when there is space available in the ring. This function is
1469 * called when the driver is unable to post more iocbs to the ring due
1470 * to unavailability of space in the ring.
1471 **/
dea31012005-04-17 16:05:31 -05001472static void
James Smart2e0fef82007-06-17 19:56:36 -05001473lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001474{
1475 int ringno = pring->ringno;
1476
1477 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1478
1479 wmb();
1480
1481 /*
1482 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483 * The HBA will tell us when an IOCB entry is available.
1484 */
1485 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486 readl(phba->CAregaddr); /* flush */
1487
1488 pring->stats.iocb_cmd_full++;
1489}
1490
James Smarte59058c2008-08-24 21:49:00 -04001491/**
James Smart3621a712009-04-06 18:47:14 -04001492 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001493 * @phba: Pointer to HBA context object.
1494 * @pring: Pointer to driver SLI ring object.
1495 *
1496 * This function updates the chip attention register bit for the
1497 * given ring to inform HBA that there is more work to be done
1498 * in this ring. The caller is not required to hold any lock.
1499 **/
dea31012005-04-17 16:05:31 -05001500static void
James Smart2e0fef82007-06-17 19:56:36 -05001501lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001502{
1503 int ringno = pring->ringno;
1504
1505 /*
1506 * Tell the HBA that there is work to do in this ring.
1507 */
James Smart34b02dc2008-08-24 21:49:55 -04001508 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509 wmb();
1510 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511 readl(phba->CAregaddr); /* flush */
1512 }
dea31012005-04-17 16:05:31 -05001513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart3621a712009-04-06 18:47:14 -04001516 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 *
1520 * This function is called with hbalock held to post pending iocbs
1521 * in the txq to the firmware. This function is called when driver
1522 * detects space available in the ring.
1523 **/
dea31012005-04-17 16:05:31 -05001524static void
James Smart2e0fef82007-06-17 19:56:36 -05001525lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001526{
1527 IOCB_t *iocb;
1528 struct lpfc_iocbq *nextiocb;
1529
1530 /*
1531 * Check to see if:
1532 * (a) there is anything on the txq to send
1533 * (b) link is up
1534 * (c) link attention events can be processed (fcp ring only)
1535 * (d) IOCB processing is not blocked by the outstanding mbox command.
1536 */
1537 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001538 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001539 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001540 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001541
1542 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1545
1546 if (iocb)
1547 lpfc_sli_update_ring(phba, pring);
1548 else
1549 lpfc_sli_update_full_ring(phba, pring);
1550 }
1551
1552 return;
1553}
1554
James Smarte59058c2008-08-24 21:49:00 -04001555/**
James Smart3621a712009-04-06 18:47:14 -04001556 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001557 * @phba: Pointer to HBA context object.
1558 * @hbqno: HBQ number.
1559 *
1560 * This function is called with hbalock held to get the next
1561 * available slot for the given HBQ. If there is free slot
1562 * available for the HBQ it will return pointer to the next available
1563 * HBQ entry else it will return NULL.
1564 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001565static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001566lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1567{
1568 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1569
1570 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572 hbqp->next_hbqPutIdx = 0;
1573
1574 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001575 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001576 uint32_t getidx = le32_to_cpu(raw_index);
1577
1578 hbqp->local_hbqGetIdx = getidx;
1579
1580 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001582 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001583 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001584 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001585 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001586 hbqp->entry_count);
1587
1588 phba->link_state = LPFC_HBA_ERROR;
1589 return NULL;
1590 }
1591
1592 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593 return NULL;
1594 }
1595
James Smart51ef4c22007-08-02 11:10:31 -04001596 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001598}
1599
James Smarte59058c2008-08-24 21:49:00 -04001600/**
James Smart3621a712009-04-06 18:47:14 -04001601 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001602 * @phba: Pointer to HBA context object.
1603 *
1604 * This function is called with no lock held to free all the
1605 * hbq buffers while uninitializing the SLI interface. It also
1606 * frees the HBQ buffers returned by the firmware but not yet
1607 * processed by the upper layers.
1608 **/
James Smarted957682007-06-17 19:56:37 -05001609void
1610lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1611{
James Smart92d7f7b2007-06-17 19:56:38 -05001612 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001614 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001615 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001616 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001617
James Smart51ef4c22007-08-02 11:10:31 -04001618 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001619 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001620 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001621 for (i = 0; i < hbq_count; ++i) {
1622 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623 &phba->hbqs[i].hbq_buffer_list, list) {
1624 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625 list_del(&hbq_buf->dbuf.list);
1626 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1627 }
James Smarta8adb832007-10-27 13:37:53 -04001628 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001629 }
James Smart3163f722008-02-08 18:50:25 -05001630 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001631 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632 list) {
James Smart3163f722008-02-08 18:50:25 -05001633 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634 list_del(&hbq_buf->dbuf.list);
1635 if (hbq_buf->tag == -1) {
1636 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637 (phba, hbq_buf);
1638 } else {
1639 hbqno = hbq_buf->tag >> 16;
1640 if (hbqno >= LPFC_MAX_HBQS)
1641 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642 (phba, hbq_buf);
1643 else
1644 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645 hbq_buf);
1646 }
1647 }
1648
1649 /* Mark the HBQs not in use */
1650 phba->hbq_in_use = 0;
1651 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001652}
1653
James Smarte59058c2008-08-24 21:49:00 -04001654/**
James Smart3621a712009-04-06 18:47:14 -04001655 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 * @hbq_buf: Pointer to HBQ buffer.
1659 *
1660 * This function is called with the hbalock held to post a
1661 * hbq buffer to the firmware. If the function finds an empty
1662 * slot in the HBQ, it will post the buffer. The function will return
1663 * pointer to the hbq entry if it successfully post the buffer
1664 * else it will return NULL.
1665 **/
James Smart3772a992009-05-22 14:50:54 -04001666static int
James Smarted957682007-06-17 19:56:37 -05001667lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001668 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001669{
James Smart3772a992009-05-22 14:50:54 -04001670 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1671}
1672
1673/**
1674 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675 * @phba: Pointer to HBA context object.
1676 * @hbqno: HBQ number.
1677 * @hbq_buf: Pointer to HBQ buffer.
1678 *
1679 * This function is called with the hbalock held to post a hbq buffer to the
1680 * firmware. If the function finds an empty slot in the HBQ, it will post the
1681 * buffer and place it on the hbq_buffer_list. The function will return zero if
1682 * it successfully post the buffer else it will return an error.
1683 **/
1684static int
1685lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686 struct hbq_dmabuf *hbq_buf)
1687{
James Smarted957682007-06-17 19:56:37 -05001688 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001689 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001690
1691 /* Get next HBQ entry slot to use */
1692 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693 if (hbqe) {
1694 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1695
James Smart92d7f7b2007-06-17 19:56:38 -05001696 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001698 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001699 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001700 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001703 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001705 /* flush */
James Smarted957682007-06-17 19:56:37 -05001706 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001707 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001708 return 0;
1709 } else
1710 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001711}
1712
James Smart4f774512009-05-22 14:52:35 -04001713/**
1714 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715 * @phba: Pointer to HBA context object.
1716 * @hbqno: HBQ number.
1717 * @hbq_buf: Pointer to HBQ buffer.
1718 *
1719 * This function is called with the hbalock held to post an RQE to the SLI4
1720 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721 * the hbq_buffer_list and return zero, otherwise it will return an error.
1722 **/
1723static int
1724lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725 struct hbq_dmabuf *hbq_buf)
1726{
1727 int rc;
1728 struct lpfc_rqe hrqe;
1729 struct lpfc_rqe drqe;
1730
1731 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736 &hrqe, &drqe);
1737 if (rc < 0)
1738 return rc;
1739 hbq_buf->tag = rc;
1740 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741 return 0;
1742}
1743
James Smarte59058c2008-08-24 21:49:00 -04001744/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001745static struct lpfc_hbq_init lpfc_els_hbq = {
1746 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001747 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001748 .mask_count = 0,
1749 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001750 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001751 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001752 .init_count = 40,
1753 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001754};
James Smarted957682007-06-17 19:56:37 -05001755
James Smarte59058c2008-08-24 21:49:00 -04001756/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001757static struct lpfc_hbq_init lpfc_extra_hbq = {
1758 .rn = 1,
1759 .entry_count = 200,
1760 .mask_count = 0,
1761 .profile = 0,
1762 .ring_mask = (1 << LPFC_EXTRA_RING),
1763 .buffer_count = 0,
1764 .init_count = 0,
1765 .add_count = 5,
1766};
1767
James Smarte59058c2008-08-24 21:49:00 -04001768/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001769struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001770 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001771 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001772};
1773
James Smarte59058c2008-08-24 21:49:00 -04001774/**
James Smart3621a712009-04-06 18:47:14 -04001775 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @count: Number of HBQ buffers to be posted.
1779 *
James Smartd7c255b2008-08-24 21:50:00 -04001780 * This function is called with no lock held to post more hbq buffers to the
1781 * given HBQ. The function returns the number of HBQ buffers successfully
1782 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001783 **/
James Smart311464e2007-08-02 11:10:37 -04001784static int
James Smart92d7f7b2007-06-17 19:56:38 -05001785lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1786{
James Smartd7c255b2008-08-24 21:50:00 -04001787 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001788 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001789 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001790 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001791 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001792 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001793
James Smartd7c255b2008-08-24 21:50:00 -04001794 if ((phba->hbqs[hbqno].buffer_count + count) >
1795 lpfc_hbq_defs[hbqno]->entry_count)
1796 count = lpfc_hbq_defs[hbqno]->entry_count -
1797 phba->hbqs[hbqno].buffer_count;
1798 if (!count)
1799 return 0;
1800 /* Allocate HBQ entries */
1801 for (i = 0; i < count; i++) {
1802 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803 if (!hbq_buffer)
1804 break;
1805 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1806 }
James Smart3163f722008-02-08 18:50:25 -05001807 /* Check whether HBQ is still in use */
1808 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001809 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001810 goto err;
1811 while (!list_empty(&hbq_buf_list)) {
1812 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813 dbuf.list);
1814 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001816 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001817 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001818 posted++;
1819 } else
James Smart51ef4c22007-08-02 11:10:31 -04001820 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001821 }
James Smart3163f722008-02-08 18:50:25 -05001822 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001823 return posted;
1824err:
1825 spin_unlock_irqrestore(&phba->hbalock, flags);
1826 while (!list_empty(&hbq_buf_list)) {
1827 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828 dbuf.list);
1829 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1830 }
James Smart92d7f7b2007-06-17 19:56:38 -05001831 return 0;
James Smarted957682007-06-17 19:56:37 -05001832}
1833
James Smarte59058c2008-08-24 21:49:00 -04001834/**
James Smart3621a712009-04-06 18:47:14 -04001835 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001836 * @phba: Pointer to HBA context object.
1837 * @qno: HBQ number.
1838 *
1839 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001840 * is called with no lock held. The function returns the number of HBQ entries
1841 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001842 **/
James Smarted957682007-06-17 19:56:37 -05001843int
James Smart92d7f7b2007-06-17 19:56:38 -05001844lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001845{
James Smartdef9c7a2009-12-21 17:02:28 -05001846 if (phba->sli_rev == LPFC_SLI_REV4)
1847 return 0;
1848 else
1849 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001851}
1852
James Smarte59058c2008-08-24 21:49:00 -04001853/**
James Smart3621a712009-04-06 18:47:14 -04001854 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001855 * @phba: Pointer to HBA context object.
1856 * @qno: HBQ queue number.
1857 *
1858 * This function is called from SLI initialization code path with
1859 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001860 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001861 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001862static int
James Smart92d7f7b2007-06-17 19:56:38 -05001863lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001864{
James Smartdef9c7a2009-12-21 17:02:28 -05001865 if (phba->sli_rev == LPFC_SLI_REV4)
1866 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001867 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001868 else
1869 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001871}
1872
James Smarte59058c2008-08-24 21:49:00 -04001873/**
James Smart3772a992009-05-22 14:50:54 -04001874 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875 * @phba: Pointer to HBA context object.
1876 * @hbqno: HBQ number.
1877 *
1878 * This function removes the first hbq buffer on an hbq list and returns a
1879 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1880 **/
1881static struct hbq_dmabuf *
1882lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1883{
1884 struct lpfc_dmabuf *d_buf;
1885
1886 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887 if (!d_buf)
1888 return NULL;
1889 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1890}
1891
1892/**
James Smart3621a712009-04-06 18:47:14 -04001893 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001894 * @phba: Pointer to HBA context object.
1895 * @tag: Tag of the hbq buffer.
1896 *
1897 * This function is called with hbalock held. This function searches
1898 * for the hbq buffer associated with the given tag in the hbq buffer
1899 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900 * it returns NULL.
1901 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001902static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001903lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1904{
James Smart92d7f7b2007-06-17 19:56:38 -05001905 struct lpfc_dmabuf *d_buf;
1906 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001907 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001908
James Smart51ef4c22007-08-02 11:10:31 -04001909 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001910 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001911 return NULL;
1912
James Smart3772a992009-05-22 14:50:54 -04001913 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001914 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001915 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001916 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001917 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001918 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001919 }
1920 }
James Smart3772a992009-05-22 14:50:54 -04001921 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001923 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001924 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001925 return NULL;
James Smarted957682007-06-17 19:56:37 -05001926}
1927
James Smarte59058c2008-08-24 21:49:00 -04001928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001930 * @phba: Pointer to HBA context object.
1931 * @hbq_buffer: Pointer to HBQ buffer.
1932 *
1933 * This function is called with hbalock. This function gives back
1934 * the hbq buffer to firmware. If the HBQ does not have space to
1935 * post the buffer, it will free the buffer.
1936 **/
James Smarted957682007-06-17 19:56:37 -05001937void
James Smart51ef4c22007-08-02 11:10:31 -04001938lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001939{
1940 uint32_t hbqno;
1941
James Smart51ef4c22007-08-02 11:10:31 -04001942 if (hbq_buffer) {
1943 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001944 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001945 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001946 }
1947}
1948
James Smarte59058c2008-08-24 21:49:00 -04001949/**
James Smart3621a712009-04-06 18:47:14 -04001950 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001951 * @mbxCommand: mailbox command code.
1952 *
1953 * This function is called by the mailbox event handler function to verify
1954 * that the completed mailbox command is a legitimate mailbox command. If the
1955 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956 * and the mailbox event handler will take the HBA offline.
1957 **/
dea31012005-04-17 16:05:31 -05001958static int
1959lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1960{
1961 uint8_t ret;
1962
1963 switch (mbxCommand) {
1964 case MBX_LOAD_SM:
1965 case MBX_READ_NV:
1966 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001967 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001968 case MBX_RUN_BIU_DIAG:
1969 case MBX_INIT_LINK:
1970 case MBX_DOWN_LINK:
1971 case MBX_CONFIG_LINK:
1972 case MBX_CONFIG_RING:
1973 case MBX_RESET_RING:
1974 case MBX_READ_CONFIG:
1975 case MBX_READ_RCONFIG:
1976 case MBX_READ_SPARM:
1977 case MBX_READ_STATUS:
1978 case MBX_READ_RPI:
1979 case MBX_READ_XRI:
1980 case MBX_READ_REV:
1981 case MBX_READ_LNK_STAT:
1982 case MBX_REG_LOGIN:
1983 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05001984 case MBX_CLEAR_LA:
1985 case MBX_DUMP_MEMORY:
1986 case MBX_DUMP_CONTEXT:
1987 case MBX_RUN_DIAGS:
1988 case MBX_RESTART:
1989 case MBX_UPDATE_CFG:
1990 case MBX_DOWN_LOAD:
1991 case MBX_DEL_LD_ENTRY:
1992 case MBX_RUN_PROGRAM:
1993 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001994 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001995 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001996 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001997 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001998 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001999 case MBX_LOAD_AREA:
2000 case MBX_RUN_BIU_DIAG64:
2001 case MBX_CONFIG_PORT:
2002 case MBX_READ_SPARM64:
2003 case MBX_READ_RPI64:
2004 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002005 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002006 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002007 case MBX_SET_DEBUG:
2008 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002009 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002010 case MBX_REG_VPI:
2011 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002012 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002013 case MBX_PORT_CAPABILITIES:
2014 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002015 case MBX_SLI4_CONFIG:
2016 case MBX_SLI4_REQ_FTRS:
2017 case MBX_REG_FCFI:
2018 case MBX_UNREG_FCFI:
2019 case MBX_REG_VFI:
2020 case MBX_UNREG_VFI:
2021 case MBX_INIT_VPI:
2022 case MBX_INIT_VFI:
2023 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002024 case MBX_READ_EVENT_LOG_STATUS:
2025 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002026 case MBX_SECURITY_MGMT:
2027 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002028 ret = mbxCommand;
2029 break;
2030 default:
2031 ret = MBX_SHUTDOWN;
2032 break;
2033 }
James Smart2e0fef82007-06-17 19:56:36 -05002034 return ret;
dea31012005-04-17 16:05:31 -05002035}
James Smarte59058c2008-08-24 21:49:00 -04002036
2037/**
James Smart3621a712009-04-06 18:47:14 -04002038 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002039 * @phba: Pointer to HBA context object.
2040 * @pmboxq: Pointer to mailbox command.
2041 *
2042 * This is completion handler function for mailbox commands issued from
2043 * lpfc_sli_issue_mbox_wait function. This function is called by the
2044 * mailbox event handler function with no lock held. This function
2045 * will wake up thread waiting on the wait queue pointed by context1
2046 * of the mailbox.
2047 **/
James Smart04c68492009-05-22 14:52:52 -04002048void
James Smart2e0fef82007-06-17 19:56:36 -05002049lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002050{
2051 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002052 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002053
2054 /*
2055 * If pdone_q is empty, the driver thread gave up waiting and
2056 * continued running.
2057 */
James Smart7054a602007-04-25 09:52:34 -04002058 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002060 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061 if (pdone_q)
2062 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002064 return;
2065}
2066
James Smarte59058c2008-08-24 21:49:00 -04002067
2068/**
James Smart3621a712009-04-06 18:47:14 -04002069 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002070 * @phba: Pointer to HBA context object.
2071 * @pmb: Pointer to mailbox object.
2072 *
2073 * This function is the default mailbox completion handler. It
2074 * frees the memory resources associated with the completed mailbox
2075 * command. If the completed command is a REG_LOGIN mailbox command,
2076 * this function will issue a UREG_LOGIN to re-claim the RPI.
2077 **/
dea31012005-04-17 16:05:31 -05002078void
James Smart2e0fef82007-06-17 19:56:36 -05002079lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002080{
James Smartd439d282010-09-29 11:18:45 -04002081 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002082 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002083 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002084 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002085 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002086 int rc;
2087
dea31012005-04-17 16:05:31 -05002088 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002089
dea31012005-04-17 16:05:31 -05002090 if (mp) {
2091 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092 kfree(mp);
2093 }
James Smart7054a602007-04-25 09:52:34 -04002094
2095 /*
2096 * If a REG_LOGIN succeeded after node is destroyed or node
2097 * is in re-discovery driver need to cleanup the RPI.
2098 */
James Smart2e0fef82007-06-17 19:56:36 -05002099 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002100 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101 !pmb->u.mb.mbxStatus) {
2102 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002103 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002104 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002105 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107 if (rc != MBX_NOT_FINISHED)
2108 return;
2109 }
2110
James Smart695a8142010-01-26 23:08:03 -05002111 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112 !(phba->pport->load_flag & FC_UNLOADING) &&
2113 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002114 shost = lpfc_shost_from_vport(vport);
2115 spin_lock_irq(shost->host_lock);
2116 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002119 }
2120
James Smartd439d282010-09-29 11:18:45 -04002121 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123 lpfc_nlp_put(ndlp);
2124 pmb->context2 = NULL;
2125 }
2126
James Smartdcf2a4e2010-09-29 11:18:53 -04002127 /* Check security permission status on INIT_LINK mailbox command */
2128 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131 "2860 SLI authentication is required "
2132 "for INIT_LINK but has not done yet\n");
2133
James Smart04c68492009-05-22 14:52:52 -04002134 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136 else
2137 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002138}
2139
James Smarte59058c2008-08-24 21:49:00 -04002140/**
James Smart3621a712009-04-06 18:47:14 -04002141 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002142 * @phba: Pointer to HBA context object.
2143 *
2144 * This function is called with no lock held. This function processes all
2145 * the completed mailbox commands and gives it to upper layers. The interrupt
2146 * service routine processes mailbox completion interrupt and adds completed
2147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150 * function returns the mailbox commands to the upper layer by calling the
2151 * completion handler function of each mailbox.
2152 **/
dea31012005-04-17 16:05:31 -05002153int
James Smart2e0fef82007-06-17 19:56:36 -05002154lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002155{
James Smart92d7f7b2007-06-17 19:56:38 -05002156 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002157 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002158 int rc;
2159 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002160
2161 phba->sli.slistat.mbox_event++;
2162
James Smart92d7f7b2007-06-17 19:56:38 -05002163 /* Get all completed mailboxe buffers into the cmplq */
2164 spin_lock_irq(&phba->hbalock);
2165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166 spin_unlock_irq(&phba->hbalock);
2167
dea31012005-04-17 16:05:31 -05002168 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002169 do {
2170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171 if (pmb == NULL)
2172 break;
2173
James Smart04c68492009-05-22 14:52:52 -04002174 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002175
James Smart858c9f62007-06-17 19:56:39 -05002176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177 if (pmb->vport) {
2178 lpfc_debugfs_disc_trc(pmb->vport,
2179 LPFC_DISC_TRC_MBOX_VPORT,
2180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181 (uint32_t)pmbox->mbxCommand,
2182 pmbox->un.varWords[0],
2183 pmbox->un.varWords[1]);
2184 }
2185 else {
2186 lpfc_debugfs_disc_trc(phba->pport,
2187 LPFC_DISC_TRC_MBOX,
2188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2189 (uint32_t)pmbox->mbxCommand,
2190 pmbox->un.varWords[0],
2191 pmbox->un.varWords[1]);
2192 }
2193 }
2194
dea31012005-04-17 16:05:31 -05002195 /*
2196 * It is a fatal error if unknown mbox command completion.
2197 */
2198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002200 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002202 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002203 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002204 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002205 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002206 lpfc_sli_config_mbox_subsys_get(phba,
2207 pmb),
2208 lpfc_sli_config_mbox_opcode_get(phba,
2209 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002210 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002211 phba->work_hs = HS_FFER3;
2212 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002213 continue;
dea31012005-04-17 16:05:31 -05002214 }
2215
dea31012005-04-17 16:05:31 -05002216 if (pmbox->mbxStatus) {
2217 phba->sli.slistat.mbox_stat_err++;
2218 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2219 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002220 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002221 LOG_MBOX | LOG_SLI,
2222 "(%d):0305 Mbox cmd cmpl "
2223 "error - RETRYing Data: x%x "
2224 "(x%x/x%x) x%x x%x x%x\n",
2225 pmb->vport ? pmb->vport->vpi : 0,
2226 pmbox->mbxCommand,
2227 lpfc_sli_config_mbox_subsys_get(phba,
2228 pmb),
2229 lpfc_sli_config_mbox_opcode_get(phba,
2230 pmb),
2231 pmbox->mbxStatus,
2232 pmbox->un.varWords[0],
2233 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002234 pmbox->mbxStatus = 0;
2235 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002236 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002237 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002238 continue;
dea31012005-04-17 16:05:31 -05002239 }
2240 }
2241
2242 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002243 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002244 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002245 "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 -05002246 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002247 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002248 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002250 pmb->mbox_cmpl,
2251 *((uint32_t *) pmbox),
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1],
2254 pmbox->un.varWords[2],
2255 pmbox->un.varWords[3],
2256 pmbox->un.varWords[4],
2257 pmbox->un.varWords[5],
2258 pmbox->un.varWords[6],
2259 pmbox->un.varWords[7]);
2260
James Smart92d7f7b2007-06-17 19:56:38 -05002261 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002262 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002263 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002264 return 0;
dea31012005-04-17 16:05:31 -05002265}
James Smart92d7f7b2007-06-17 19:56:38 -05002266
James Smarte59058c2008-08-24 21:49:00 -04002267/**
James Smart3621a712009-04-06 18:47:14 -04002268 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 * @tag: buffer tag.
2272 *
2273 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2274 * is set in the tag the buffer is posted for a particular exchange,
2275 * the function will return the buffer without replacing the buffer.
2276 * If the buffer is for unsolicited ELS or CT traffic, this function
2277 * returns the buffer and also posts another buffer to the firmware.
2278 **/
James Smart76bb24e2007-10-27 13:38:00 -04002279static struct lpfc_dmabuf *
2280lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002281 struct lpfc_sli_ring *pring,
2282 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002283{
James Smart9f1e1b52008-12-04 22:39:40 -05002284 struct hbq_dmabuf *hbq_entry;
2285
James Smart76bb24e2007-10-27 13:38:00 -04002286 if (tag & QUE_BUFTAG_BIT)
2287 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002288 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2289 if (!hbq_entry)
2290 return NULL;
2291 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002292}
James Smart57127f12007-10-27 13:37:05 -04002293
James Smart3772a992009-05-22 14:50:54 -04002294/**
2295 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2296 * @phba: Pointer to HBA context object.
2297 * @pring: Pointer to driver SLI ring object.
2298 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2299 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2300 * @fch_type: the type for the first frame of the sequence.
2301 *
2302 * This function is called with no lock held. This function uses the r_ctl and
2303 * type of the received sequence to find the correct callback function to call
2304 * to process the sequence.
2305 **/
2306static int
2307lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2308 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2309 uint32_t fch_type)
2310{
2311 int i;
2312
2313 /* unSolicited Responses */
2314 if (pring->prt[0].profile) {
2315 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2316 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2317 saveq);
2318 return 1;
2319 }
2320 /* We must search, based on rctl / type
2321 for the right routine */
2322 for (i = 0; i < pring->num_mask; i++) {
2323 if ((pring->prt[i].rctl == fch_r_ctl) &&
2324 (pring->prt[i].type == fch_type)) {
2325 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2326 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2327 (phba, pring, saveq);
2328 return 1;
2329 }
2330 }
2331 return 0;
2332}
James Smarte59058c2008-08-24 21:49:00 -04002333
2334/**
James Smart3621a712009-04-06 18:47:14 -04002335 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002336 * @phba: Pointer to HBA context object.
2337 * @pring: Pointer to driver SLI ring object.
2338 * @saveq: Pointer to the unsolicited iocb.
2339 *
2340 * This function is called with no lock held by the ring event handler
2341 * when there is an unsolicited iocb posted to the response ring by the
2342 * firmware. This function gets the buffer associated with the iocbs
2343 * and calls the event handler for the ring. This function handles both
2344 * qring buffers and hbq buffers.
2345 * When the function returns 1 the caller can free the iocb object otherwise
2346 * upper layer functions will free the iocb objects.
2347 **/
dea31012005-04-17 16:05:31 -05002348static int
2349lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2350 struct lpfc_iocbq *saveq)
2351{
2352 IOCB_t * irsp;
2353 WORD5 * w5p;
2354 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002355 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002356 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002357 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002358
2359 match = 0;
2360 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002361
2362 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2363 if (pring->lpfc_sli_rcv_async_status)
2364 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2365 else
2366 lpfc_printf_log(phba,
2367 KERN_WARNING,
2368 LOG_SLI,
2369 "0316 Ring %d handler: unexpected "
2370 "ASYNC_STATUS iocb received evt_code "
2371 "0x%x\n",
2372 pring->ringno,
2373 irsp->un.asyncstat.evt_code);
2374 return 1;
2375 }
2376
James Smart3163f722008-02-08 18:50:25 -05002377 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2378 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2379 if (irsp->ulpBdeCount > 0) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->un.ulpWord[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 1) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[3]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 if (irsp->ulpBdeCount > 2) {
2392 dmzbuf = lpfc_sli_get_buff(phba, pring,
2393 irsp->unsli3.sli3Words[7]);
2394 lpfc_in_buf_free(phba, dmzbuf);
2395 }
2396
2397 return 1;
2398 }
2399
James Smart92d7f7b2007-06-17 19:56:38 -05002400 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002401 if (irsp->ulpBdeCount != 0) {
2402 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002403 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002404 if (!saveq->context2)
2405 lpfc_printf_log(phba,
2406 KERN_ERR,
2407 LOG_SLI,
2408 "0341 Ring %d Cannot find buffer for "
2409 "an unsolicited iocb. tag 0x%x\n",
2410 pring->ringno,
2411 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002412 }
2413 if (irsp->ulpBdeCount == 2) {
2414 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002415 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002416 if (!saveq->context3)
2417 lpfc_printf_log(phba,
2418 KERN_ERR,
2419 LOG_SLI,
2420 "0342 Ring %d Cannot find buffer for an"
2421 " unsolicited iocb. tag 0x%x\n",
2422 pring->ringno,
2423 irsp->unsli3.sli3Words[7]);
2424 }
2425 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002426 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002427 if (irsp->ulpBdeCount != 0) {
2428 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002430 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002431 lpfc_printf_log(phba,
2432 KERN_ERR,
2433 LOG_SLI,
2434 "0343 Ring %d Cannot find "
2435 "buffer for an unsolicited iocb"
2436 ". tag 0x%x\n", pring->ringno,
2437 irsp->un.ulpWord[3]);
2438 }
2439 if (irsp->ulpBdeCount == 2) {
2440 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2441 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002442 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002443 lpfc_printf_log(phba,
2444 KERN_ERR,
2445 LOG_SLI,
2446 "0344 Ring %d Cannot find "
2447 "buffer for an unsolicited "
2448 "iocb. tag 0x%x\n",
2449 pring->ringno,
2450 irsp->unsli3.sli3Words[7]);
2451 }
2452 }
James Smart92d7f7b2007-06-17 19:56:38 -05002453 }
James Smart9c2face2008-01-11 01:53:18 -05002454 if (irsp->ulpBdeCount != 0 &&
2455 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2456 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2457 int found = 0;
2458
2459 /* search continue save q for same XRI */
2460 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002461 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2462 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002463 list_add_tail(&saveq->list, &iocbq->list);
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (!found)
2469 list_add_tail(&saveq->clist,
2470 &pring->iocb_continue_saveq);
2471 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2472 list_del_init(&iocbq->clist);
2473 saveq = iocbq;
2474 irsp = &(saveq->iocb);
2475 } else
2476 return 0;
2477 }
2478 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2479 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2480 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002481 Rctl = FC_RCTL_ELS_REQ;
2482 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002483 } else {
2484 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2485 Rctl = w5p->hcsw.Rctl;
2486 Type = w5p->hcsw.Type;
2487
2488 /* Firmware Workaround */
2489 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2490 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2491 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002492 Rctl = FC_RCTL_ELS_REQ;
2493 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002494 w5p->hcsw.Rctl = Rctl;
2495 w5p->hcsw.Type = Type;
2496 }
2497 }
James Smart92d7f7b2007-06-17 19:56:38 -05002498
James Smart3772a992009-05-22 14:50:54 -04002499 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002500 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002501 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002502 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002503 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002504
James Smart92d7f7b2007-06-17 19:56:38 -05002505 return 1;
dea31012005-04-17 16:05:31 -05002506}
2507
James Smarte59058c2008-08-24 21:49:00 -04002508/**
James Smart3621a712009-04-06 18:47:14 -04002509 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002510 * @phba: Pointer to HBA context object.
2511 * @pring: Pointer to driver SLI ring object.
2512 * @prspiocb: Pointer to response iocb object.
2513 *
2514 * This function looks up the iocb_lookup table to get the command iocb
2515 * corresponding to the given response iocb using the iotag of the
2516 * response iocb. This function is called with the hbalock held.
2517 * This function returns the command iocb object if it finds the command
2518 * iocb else returns NULL.
2519 **/
dea31012005-04-17 16:05:31 -05002520static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002521lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2522 struct lpfc_sli_ring *pring,
2523 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002524{
dea31012005-04-17 16:05:31 -05002525 struct lpfc_iocbq *cmd_iocb = NULL;
2526 uint16_t iotag;
2527
James Bottomley604a3e32005-10-29 10:28:33 -05002528 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002529
James Bottomley604a3e32005-10-29 10:28:33 -05002530 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2531 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002532 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002533 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2534 pring->txcmplq_cnt--;
2535 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2536 }
James Bottomley604a3e32005-10-29 10:28:33 -05002537 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002538 }
2539
dea31012005-04-17 16:05:31 -05002540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002541 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002542 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002543 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002544 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002545 return NULL;
2546}
2547
James Smarte59058c2008-08-24 21:49:00 -04002548/**
James Smart3772a992009-05-22 14:50:54 -04002549 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @iotag: IOCB tag.
2553 *
2554 * This function looks up the iocb_lookup table to get the command iocb
2555 * corresponding to the given iotag. This function is called with the
2556 * hbalock held.
2557 * This function returns the command iocb object if it finds the command
2558 * iocb else returns NULL.
2559 **/
2560static struct lpfc_iocbq *
2561lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2562 struct lpfc_sli_ring *pring, uint16_t iotag)
2563{
2564 struct lpfc_iocbq *cmd_iocb;
2565
2566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2568 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2571 pring->txcmplq_cnt--;
2572 }
James Smart3772a992009-05-22 14:50:54 -04002573 return cmd_iocb;
2574 }
2575
2576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2577 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2578 iotag, phba->sli.last_iotag);
2579 return NULL;
2580}
2581
2582/**
James Smart3621a712009-04-06 18:47:14 -04002583 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @saveq: Pointer to the response iocb to be processed.
2587 *
2588 * This function is called by the ring event handler for non-fcp
2589 * rings when there is a new response iocb in the response ring.
2590 * The caller is not required to hold any locks. This function
2591 * gets the command iocb associated with the response iocb and
2592 * calls the completion handler for the command iocb. If there
2593 * is no completion handler, the function will free the resources
2594 * associated with command iocb. If the response iocb is for
2595 * an already aborted command iocb, the status of the completion
2596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2597 * This function always returns 1.
2598 **/
dea31012005-04-17 16:05:31 -05002599static int
James Smart2e0fef82007-06-17 19:56:36 -05002600lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002601 struct lpfc_iocbq *saveq)
2602{
James Smart2e0fef82007-06-17 19:56:36 -05002603 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002604 int rc = 1;
2605 unsigned long iflag;
2606
2607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002608 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2611
dea31012005-04-17 16:05:31 -05002612 if (cmdiocbp) {
2613 if (cmdiocbp->iocb_cmpl) {
2614 /*
James Smartea2151b2008-09-07 11:52:10 -04002615 * If an ELS command failed send an event to mgmt
2616 * application.
2617 */
2618 if (saveq->iocb.ulpStatus &&
2619 (pring->ringno == LPFC_ELS_RING) &&
2620 (cmdiocbp->iocb.ulpCommand ==
2621 CMD_ELS_REQUEST64_CR))
2622 lpfc_send_els_failure_event(phba,
2623 cmdiocbp, saveq);
2624
2625 /*
dea31012005-04-17 16:05:31 -05002626 * Post all ELS completions to the worker thread.
2627 * All other are passed to the completion callback.
2628 */
2629 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002630 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2631 (cmdiocbp->iocb_flag &
2632 LPFC_DRIVER_ABORTED)) {
2633 spin_lock_irqsave(&phba->hbalock,
2634 iflag);
James Smart07951072007-04-25 09:51:38 -04002635 cmdiocbp->iocb_flag &=
2636 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002637 spin_unlock_irqrestore(&phba->hbalock,
2638 iflag);
James Smart07951072007-04-25 09:51:38 -04002639 saveq->iocb.ulpStatus =
2640 IOSTAT_LOCAL_REJECT;
2641 saveq->iocb.un.ulpWord[4] =
2642 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002643
2644 /* Firmware could still be in progress
2645 * of DMAing payload, so don't free data
2646 * buffer till after a hbeat.
2647 */
James Smart341af102010-01-26 23:07:37 -05002648 spin_lock_irqsave(&phba->hbalock,
2649 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002650 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2653 }
James Smart0f65ff62010-02-26 14:14:23 -05002654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (saveq->iocb_flag &
2656 LPFC_EXCHANGE_BUSY) {
2657 /* Set cmdiocb flag for the
2658 * exchange busy so sgl (xri)
2659 * will not be released until
2660 * the abort xri is received
2661 * from hba.
2662 */
2663 spin_lock_irqsave(
2664 &phba->hbalock, iflag);
2665 cmdiocbp->iocb_flag |=
2666 LPFC_EXCHANGE_BUSY;
2667 spin_unlock_irqrestore(
2668 &phba->hbalock, iflag);
2669 }
2670 if (cmdiocbp->iocb_flag &
2671 LPFC_DRIVER_ABORTED) {
2672 /*
2673 * Clear LPFC_DRIVER_ABORTED
2674 * bit in case it was driver
2675 * initiated abort.
2676 */
2677 spin_lock_irqsave(
2678 &phba->hbalock, iflag);
2679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
2681 spin_unlock_irqrestore(
2682 &phba->hbalock, iflag);
2683 cmdiocbp->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 cmdiocbp->iocb.un.ulpWord[4] =
2686 IOERR_ABORT_REQUESTED;
2687 /*
2688 * For SLI4, irsiocb contains
2689 * NO_XRI in sli_xritag, it
2690 * shall not affect releasing
2691 * sgl (xri) process.
2692 */
2693 saveq->iocb.ulpStatus =
2694 IOSTAT_LOCAL_REJECT;
2695 saveq->iocb.un.ulpWord[4] =
2696 IOERR_SLI_ABORTED;
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 saveq->iocb_flag |=
2700 LPFC_DELAY_MEM_FREE;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2703 }
James Smart07951072007-04-25 09:51:38 -04002704 }
dea31012005-04-17 16:05:31 -05002705 }
James Smart2e0fef82007-06-17 19:56:36 -05002706 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002707 } else
2708 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002709 } else {
2710 /*
2711 * Unknown initiating command based on the response iotag.
2712 * This could be the case on the ELS ring because of
2713 * lpfc_els_abort().
2714 */
2715 if (pring->ringno != LPFC_ELS_RING) {
2716 /*
2717 * Ring <ringno> handler: unexpected completion IoTag
2718 * <IoTag>
2719 */
James Smarta257bf92009-04-06 18:48:10 -04002720 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002721 "0322 Ring %d handler: "
2722 "unexpected completion IoTag x%x "
2723 "Data: x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 saveq->iocb.ulpIoTag,
2726 saveq->iocb.ulpStatus,
2727 saveq->iocb.un.ulpWord[4],
2728 saveq->iocb.ulpCommand,
2729 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002730 }
2731 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002732
dea31012005-04-17 16:05:31 -05002733 return rc;
2734}
2735
James Smarte59058c2008-08-24 21:49:00 -04002736/**
James Smart3621a712009-04-06 18:47:14 -04002737 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002738 * @phba: Pointer to HBA context object.
2739 * @pring: Pointer to driver SLI ring object.
2740 *
2741 * This function is called from the iocb ring event handlers when
2742 * put pointer is ahead of the get pointer for a ring. This function signal
2743 * an error attention condition to the worker thread and the worker
2744 * thread will transition the HBA to offline state.
2745 **/
James Smart2e0fef82007-06-17 19:56:36 -05002746static void
2747lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002748{
James Smart34b02dc2008-08-24 21:49:55 -04002749 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002750 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002752 * rsp ring <portRspMax>
2753 */
2754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002755 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002756 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002757 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002758 pring->numRiocb);
2759
James Smart2e0fef82007-06-17 19:56:36 -05002760 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002761
2762 /*
2763 * All error attention handlers are posted to
2764 * worker thread
2765 */
2766 phba->work_ha |= HA_ERATT;
2767 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002768
James Smart5e9d9b82008-06-14 22:52:53 -04002769 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002770
2771 return;
2772}
2773
James Smarte59058c2008-08-24 21:49:00 -04002774/**
James Smart3621a712009-04-06 18:47:14 -04002775 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002776 * @ptr: Pointer to address of HBA context object.
2777 *
2778 * This function is invoked by the Error Attention polling timer when the
2779 * timer times out. It will check the SLI Error Attention register for
2780 * possible attention events. If so, it will post an Error Attention event
2781 * and wake up worker thread to process it. Otherwise, it will set up the
2782 * Error Attention polling timer for the next poll.
2783 **/
2784void lpfc_poll_eratt(unsigned long ptr)
2785{
2786 struct lpfc_hba *phba;
2787 uint32_t eratt = 0;
2788
2789 phba = (struct lpfc_hba *)ptr;
2790
2791 /* Check chip HA register for error event */
2792 eratt = lpfc_sli_check_eratt(phba);
2793
2794 if (eratt)
2795 /* Tell the worker thread there is work to do */
2796 lpfc_worker_wake_up(phba);
2797 else
2798 /* Restart the timer for next eratt poll */
2799 mod_timer(&phba->eratt_poll, jiffies +
2800 HZ * LPFC_ERATT_POLL_INTERVAL);
2801 return;
2802}
2803
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002804
James Smarte59058c2008-08-24 21:49:00 -04002805/**
James Smart3621a712009-04-06 18:47:14 -04002806 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @mask: Host attention register mask for this ring.
2810 *
2811 * This function is called from the interrupt context when there is a ring
2812 * event for the fcp ring. The caller does not hold any lock.
2813 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002814 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002815 * LE bit set. The function will call the completion handler of the command iocb
2816 * if the response iocb indicates a completion for a command iocb or it is
2817 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2818 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002819 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002820 * to check it explicitly.
2821 */
2822int
James Smart2e0fef82007-06-17 19:56:36 -05002823lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2824 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002825{
James Smart34b02dc2008-08-24 21:49:55 -04002826 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002827 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002828 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002829 struct lpfc_iocbq *cmdiocbq = NULL;
2830 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002831 uint32_t status;
2832 uint32_t portRspPut, portRspMax;
2833 int rc = 1;
2834 lpfc_iocb_type type;
2835 unsigned long iflag;
2836 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002837
James Smart2e0fef82007-06-17 19:56:36 -05002838 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002839 pring->stats.iocb_event++;
2840
dea31012005-04-17 16:05:31 -05002841 /*
2842 * The next available response entry should never exceed the maximum
2843 * entries. If it does, treat it as an adapter hardware error.
2844 */
2845 portRspMax = pring->numRiocb;
2846 portRspPut = le32_to_cpu(pgp->rspPutInx);
2847 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002848 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002849 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002850 return 1;
2851 }
James Smart45ed1192009-10-02 15:17:02 -04002852 if (phba->fcp_ring_in_use) {
2853 spin_unlock_irqrestore(&phba->hbalock, iflag);
2854 return 1;
2855 } else
2856 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002857
2858 rmb();
2859 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002860 /*
2861 * Fetch an entry off the ring and copy it into a local data
2862 * structure. The copy involves a byte-swap since the
2863 * network byte order and pci byte orders are different.
2864 */
James Smarted957682007-06-17 19:56:37 -05002865 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002866 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002867
2868 if (++pring->rspidx >= portRspMax)
2869 pring->rspidx = 0;
2870
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002871 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2872 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002873 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002874 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002875 irsp = &rspiocbq.iocb;
2876
dea31012005-04-17 16:05:31 -05002877 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2878 pring->stats.iocb_rsp++;
2879 rsp_cmpl++;
2880
2881 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002882 /*
2883 * If resource errors reported from HBA, reduce
2884 * queuedepths of the SCSI device.
2885 */
2886 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2887 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2888 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002889 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002890 spin_lock_irqsave(&phba->hbalock, iflag);
2891 }
2892
dea31012005-04-17 16:05:31 -05002893 /* Rsp ring <ringno> error: IOCB */
2894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002895 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002896 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002897 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002898 irsp->un.ulpWord[0],
2899 irsp->un.ulpWord[1],
2900 irsp->un.ulpWord[2],
2901 irsp->un.ulpWord[3],
2902 irsp->un.ulpWord[4],
2903 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002904 *(uint32_t *)&irsp->un1,
2905 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002906 }
2907
2908 switch (type) {
2909 case LPFC_ABORT_IOCB:
2910 case LPFC_SOL_IOCB:
2911 /*
2912 * Idle exchange closed via ABTS from port. No iocb
2913 * resources need to be recovered.
2914 */
2915 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002917 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002918 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002919 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002920 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002921 break;
2922 }
2923
James Bottomley604a3e32005-10-29 10:28:33 -05002924 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2925 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002926 if (unlikely(!cmdiocbq))
2927 break;
2928 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2929 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2930 if (cmdiocbq->iocb_cmpl) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2933 &rspiocbq);
2934 spin_lock_irqsave(&phba->hbalock, iflag);
2935 }
dea31012005-04-17 16:05:31 -05002936 break;
James Smarta4bc3372006-12-02 13:34:16 -05002937 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002938 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002939 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002940 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002941 break;
dea31012005-04-17 16:05:31 -05002942 default:
2943 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2944 char adaptermsg[LPFC_MAX_ADPTMSG];
2945 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2946 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2947 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002948 dev_warn(&((phba->pcidev)->dev),
2949 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002950 phba->brd_no, adaptermsg);
2951 } else {
2952 /* Unknown IOCB command */
2953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002954 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002955 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002956 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002957 irsp->ulpStatus,
2958 irsp->ulpIoTag,
2959 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002960 }
2961 break;
2962 }
2963
2964 /*
2965 * The response IOCB has been processed. Update the ring
2966 * pointer in SLIM. If the port response put pointer has not
2967 * been updated, sync the pgp->rspPutInx and fetch the new port
2968 * response put pointer.
2969 */
James Smarted957682007-06-17 19:56:37 -05002970 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002971
2972 if (pring->rspidx == portRspPut)
2973 portRspPut = le32_to_cpu(pgp->rspPutInx);
2974 }
2975
2976 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2977 pring->stats.iocb_rsp_full++;
2978 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2979 writel(status, phba->CAregaddr);
2980 readl(phba->CAregaddr);
2981 }
2982 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2983 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2984 pring->stats.iocb_cmd_empty++;
2985
2986 /* Force update of the local copy of cmdGetInx */
2987 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2988 lpfc_sli_resume_iocb(phba, pring);
2989
2990 if ((pring->lpfc_sli_cmd_available))
2991 (pring->lpfc_sli_cmd_available) (phba, pring);
2992
2993 }
2994
James Smart45ed1192009-10-02 15:17:02 -04002995 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002996 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002997 return rc;
2998}
2999
James Smarte59058c2008-08-24 21:49:00 -04003000/**
James Smart3772a992009-05-22 14:50:54 -04003001 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3002 * @phba: Pointer to HBA context object.
3003 * @pring: Pointer to driver SLI ring object.
3004 * @rspiocbp: Pointer to driver response IOCB object.
3005 *
3006 * This function is called from the worker thread when there is a slow-path
3007 * response IOCB to process. This function chains all the response iocbs until
3008 * seeing the iocb with the LE bit set. The function will call
3009 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3010 * completion of a command iocb. The function will call the
3011 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3012 * The function frees the resources or calls the completion handler if this
3013 * iocb is an abort completion. The function returns NULL when the response
3014 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3015 * this function shall chain the iocb on to the iocb_continueq and return the
3016 * response iocb passed in.
3017 **/
3018static struct lpfc_iocbq *
3019lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3020 struct lpfc_iocbq *rspiocbp)
3021{
3022 struct lpfc_iocbq *saveq;
3023 struct lpfc_iocbq *cmdiocbp;
3024 struct lpfc_iocbq *next_iocb;
3025 IOCB_t *irsp = NULL;
3026 uint32_t free_saveq;
3027 uint8_t iocb_cmd_type;
3028 lpfc_iocb_type type;
3029 unsigned long iflag;
3030 int rc;
3031
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 /* First add the response iocb to the countinueq list */
3034 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3035 pring->iocb_continueq_cnt++;
3036
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003037 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003038 irsp = &rspiocbp->iocb;
3039 if (irsp->ulpLe) {
3040 /*
3041 * By default, the driver expects to free all resources
3042 * associated with this iocb completion.
3043 */
3044 free_saveq = 1;
3045 saveq = list_get_first(&pring->iocb_continueq,
3046 struct lpfc_iocbq, list);
3047 irsp = &(saveq->iocb);
3048 list_del_init(&pring->iocb_continueq);
3049 pring->iocb_continueq_cnt = 0;
3050
3051 pring->stats.iocb_rsp++;
3052
3053 /*
3054 * If resource errors reported from HBA, reduce
3055 * queuedepths of the SCSI device.
3056 */
3057 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3058 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
3064 if (irsp->ulpStatus) {
3065 /* Rsp ring <ringno> error: IOCB */
3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3067 "0328 Rsp Ring %d error: "
3068 "IOCB Data: "
3069 "x%x x%x x%x x%x "
3070 "x%x x%x x%x x%x "
3071 "x%x x%x x%x x%x "
3072 "x%x x%x x%x x%x\n",
3073 pring->ringno,
3074 irsp->un.ulpWord[0],
3075 irsp->un.ulpWord[1],
3076 irsp->un.ulpWord[2],
3077 irsp->un.ulpWord[3],
3078 irsp->un.ulpWord[4],
3079 irsp->un.ulpWord[5],
3080 *(((uint32_t *) irsp) + 6),
3081 *(((uint32_t *) irsp) + 7),
3082 *(((uint32_t *) irsp) + 8),
3083 *(((uint32_t *) irsp) + 9),
3084 *(((uint32_t *) irsp) + 10),
3085 *(((uint32_t *) irsp) + 11),
3086 *(((uint32_t *) irsp) + 12),
3087 *(((uint32_t *) irsp) + 13),
3088 *(((uint32_t *) irsp) + 14),
3089 *(((uint32_t *) irsp) + 15));
3090 }
3091
3092 /*
3093 * Fetch the IOCB command type and call the correct completion
3094 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3095 * get freed back to the lpfc_iocb_list by the discovery
3096 * kernel thread.
3097 */
3098 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3099 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3100 switch (type) {
3101 case LPFC_SOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 break;
3106
3107 case LPFC_UNSOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 if (!rc)
3112 free_saveq = 0;
3113 break;
3114
3115 case LPFC_ABORT_IOCB:
3116 cmdiocbp = NULL;
3117 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3118 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3119 saveq);
3120 if (cmdiocbp) {
3121 /* Call the specified completion routine */
3122 if (cmdiocbp->iocb_cmpl) {
3123 spin_unlock_irqrestore(&phba->hbalock,
3124 iflag);
3125 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3126 saveq);
3127 spin_lock_irqsave(&phba->hbalock,
3128 iflag);
3129 } else
3130 __lpfc_sli_release_iocbq(phba,
3131 cmdiocbp);
3132 }
3133 break;
3134
3135 case LPFC_UNKNOWN_IOCB:
3136 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3137 char adaptermsg[LPFC_MAX_ADPTMSG];
3138 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3139 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3140 MAX_MSG_DATA);
3141 dev_warn(&((phba->pcidev)->dev),
3142 "lpfc%d: %s\n",
3143 phba->brd_no, adaptermsg);
3144 } else {
3145 /* Unknown IOCB command */
3146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147 "0335 Unknown IOCB "
3148 "command Data: x%x "
3149 "x%x x%x x%x\n",
3150 irsp->ulpCommand,
3151 irsp->ulpStatus,
3152 irsp->ulpIoTag,
3153 irsp->ulpContext);
3154 }
3155 break;
3156 }
3157
3158 if (free_saveq) {
3159 list_for_each_entry_safe(rspiocbp, next_iocb,
3160 &saveq->list, list) {
3161 list_del(&rspiocbp->list);
3162 __lpfc_sli_release_iocbq(phba, rspiocbp);
3163 }
3164 __lpfc_sli_release_iocbq(phba, saveq);
3165 }
3166 rspiocbp = NULL;
3167 }
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rspiocbp;
3170}
3171
3172/**
3173 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @mask: Host attention register mask for this ring.
3177 *
James Smart3772a992009-05-22 14:50:54 -04003178 * This routine wraps the actual slow_ring event process routine from the
3179 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003180 **/
James Smart3772a992009-05-22 14:50:54 -04003181void
James Smart2e0fef82007-06-17 19:56:36 -05003182lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3183 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003184{
James Smart3772a992009-05-22 14:50:54 -04003185 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3186}
3187
3188/**
3189 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3190 * @phba: Pointer to HBA context object.
3191 * @pring: Pointer to driver SLI ring object.
3192 * @mask: Host attention register mask for this ring.
3193 *
3194 * This function is called from the worker thread when there is a ring event
3195 * for non-fcp rings. The caller does not hold any lock. The function will
3196 * remove each response iocb in the response ring and calls the handle
3197 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3198 **/
3199static void
3200lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3201 struct lpfc_sli_ring *pring, uint32_t mask)
3202{
James Smart34b02dc2008-08-24 21:49:55 -04003203 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003204 IOCB_t *entry;
3205 IOCB_t *irsp = NULL;
3206 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003207 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003208 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003209 uint32_t status;
dea31012005-04-17 16:05:31 -05003210
James Smart34b02dc2008-08-24 21:49:55 -04003211 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003212 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003213 pring->stats.iocb_event++;
3214
dea31012005-04-17 16:05:31 -05003215 /*
3216 * The next available response entry should never exceed the maximum
3217 * entries. If it does, treat it as an adapter hardware error.
3218 */
3219 portRspMax = pring->numRiocb;
3220 portRspPut = le32_to_cpu(pgp->rspPutInx);
3221 if (portRspPut >= portRspMax) {
3222 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003223 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003224 * rsp ring <portRspMax>
3225 */
James Smarted957682007-06-17 19:56:37 -05003226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003227 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003228 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003229 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003230
James Smart2e0fef82007-06-17 19:56:36 -05003231 phba->link_state = LPFC_HBA_ERROR;
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003233
3234 phba->work_hs = HS_FFER3;
3235 lpfc_handle_eratt(phba);
3236
James Smart3772a992009-05-22 14:50:54 -04003237 return;
dea31012005-04-17 16:05:31 -05003238 }
3239
3240 rmb();
dea31012005-04-17 16:05:31 -05003241 while (pring->rspidx != portRspPut) {
3242 /*
3243 * Build a completion list and call the appropriate handler.
3244 * The process is to get the next available response iocb, get
3245 * a free iocb from the list, copy the response data into the
3246 * free iocb, insert to the continuation list, and update the
3247 * next response index to slim. This process makes response
3248 * iocb's in the ring available to DMA as fast as possible but
3249 * pays a penalty for a copy operation. Since the iocb is
3250 * only 32 bytes, this penalty is considered small relative to
3251 * the PCI reads for register values and a slim write. When
3252 * the ulpLe field is set, the entire Command has been
3253 * received.
3254 */
James Smarted957682007-06-17 19:56:37 -05003255 entry = lpfc_resp_iocb(phba, pring);
3256
James Smart858c9f62007-06-17 19:56:39 -05003257 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003258 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003259 if (rspiocbp == NULL) {
3260 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003261 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003262 break;
3263 }
3264
James Smarted957682007-06-17 19:56:37 -05003265 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3266 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003267 irsp = &rspiocbp->iocb;
3268
3269 if (++pring->rspidx >= portRspMax)
3270 pring->rspidx = 0;
3271
James Smarta58cbd52007-08-02 11:09:43 -04003272 if (pring->ringno == LPFC_ELS_RING) {
3273 lpfc_debugfs_slow_ring_trc(phba,
3274 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3275 *(((uint32_t *) irsp) + 4),
3276 *(((uint32_t *) irsp) + 6),
3277 *(((uint32_t *) irsp) + 7));
3278 }
3279
James Smarted957682007-06-17 19:56:37 -05003280 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003281
James Smart3772a992009-05-22 14:50:54 -04003282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 /* Handle the response IOCB */
3284 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3285 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003286
3287 /*
3288 * If the port response put pointer has not been updated, sync
3289 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3290 * response put pointer.
3291 */
3292 if (pring->rspidx == portRspPut) {
3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 }
3295 } /* while (pring->rspidx != portRspPut) */
3296
James Smart92d7f7b2007-06-17 19:56:38 -05003297 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003298 /* At least one response entry has been freed */
3299 pring->stats.iocb_rsp_full++;
3300 /* SET RxRE_RSP in Chip Att register */
3301 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3302 writel(status, phba->CAregaddr);
3303 readl(phba->CAregaddr); /* flush */
3304 }
3305 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3306 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3307 pring->stats.iocb_cmd_empty++;
3308
3309 /* Force update of the local copy of cmdGetInx */
3310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3311 lpfc_sli_resume_iocb(phba, pring);
3312
3313 if ((pring->lpfc_sli_cmd_available))
3314 (pring->lpfc_sli_cmd_available) (phba, pring);
3315
3316 }
3317
James Smart2e0fef82007-06-17 19:56:36 -05003318 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003319 return;
dea31012005-04-17 16:05:31 -05003320}
3321
James Smarte59058c2008-08-24 21:49:00 -04003322/**
James Smart4f774512009-05-22 14:52:35 -04003323 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3324 * @phba: Pointer to HBA context object.
3325 * @pring: Pointer to driver SLI ring object.
3326 * @mask: Host attention register mask for this ring.
3327 *
3328 * This function is called from the worker thread when there is a pending
3329 * ELS response iocb on the driver internal slow-path response iocb worker
3330 * queue. The caller does not hold any lock. The function will remove each
3331 * response iocb from the response worker queue and calls the handle
3332 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3333 **/
3334static void
3335lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3336 struct lpfc_sli_ring *pring, uint32_t mask)
3337{
3338 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003339 struct hbq_dmabuf *dmabuf;
3340 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003341 unsigned long iflag;
3342
James Smart45ed1192009-10-02 15:17:02 -04003343 spin_lock_irqsave(&phba->hbalock, iflag);
3344 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003347 /* Get the response iocb from the head of work queue */
3348 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003349 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003350 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003351 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003352
3353 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3354 case CQE_CODE_COMPL_WQE:
3355 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3356 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003357 /* Translate ELS WCQE to response IOCBQ */
3358 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3359 irspiocbq);
3360 if (irspiocbq)
3361 lpfc_sli_sp_handle_rspiocb(phba, pring,
3362 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003363 break;
3364 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003365 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003366 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3367 cq_event);
3368 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3369 break;
3370 default:
3371 break;
3372 }
James Smart4f774512009-05-22 14:52:35 -04003373 }
3374}
3375
3376/**
James Smart3621a712009-04-06 18:47:14 -04003377 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003378 * @phba: Pointer to HBA context object.
3379 * @pring: Pointer to driver SLI ring object.
3380 *
3381 * This function aborts all iocbs in the given ring and frees all the iocb
3382 * objects in txq. This function issues an abort iocb for all the iocb commands
3383 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3384 * the return of this function. The caller is not required to hold any locks.
3385 **/
James Smart2e0fef82007-06-17 19:56:36 -05003386void
dea31012005-04-17 16:05:31 -05003387lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3388{
James Smart2534ba72007-04-25 09:52:20 -04003389 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003390 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003391
James Smart92d7f7b2007-06-17 19:56:38 -05003392 if (pring->ringno == LPFC_ELS_RING) {
3393 lpfc_fabric_abort_hba(phba);
3394 }
3395
dea31012005-04-17 16:05:31 -05003396 /* Error everything on txq and txcmplq
3397 * First do the txq.
3398 */
James Smart2e0fef82007-06-17 19:56:36 -05003399 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003400 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003401 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003402
3403 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003404 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3405 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3406
James Smart2e0fef82007-06-17 19:56:36 -05003407 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003408
James Smarta257bf92009-04-06 18:48:10 -04003409 /* Cancel all the IOCBs from the completions list */
3410 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3411 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003412}
3413
James Smarte59058c2008-08-24 21:49:00 -04003414/**
James Smart3621a712009-04-06 18:47:14 -04003415 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003416 * @phba: Pointer to HBA context object.
3417 *
3418 * This function flushes all iocbs in the fcp ring and frees all the iocb
3419 * objects in txq and txcmplq. This function will not issue abort iocbs
3420 * for all the iocb commands in txcmplq, they will just be returned with
3421 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3422 * slot has been permanently disabled.
3423 **/
3424void
3425lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3426{
3427 LIST_HEAD(txq);
3428 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003429 struct lpfc_sli *psli = &phba->sli;
3430 struct lpfc_sli_ring *pring;
3431
3432 /* Currently, only one fcp ring */
3433 pring = &psli->ring[psli->fcp_ring];
3434
3435 spin_lock_irq(&phba->hbalock);
3436 /* Retrieve everything on txq */
3437 list_splice_init(&pring->txq, &txq);
3438 pring->txq_cnt = 0;
3439
3440 /* Retrieve everything on the txcmplq */
3441 list_splice_init(&pring->txcmplq, &txcmplq);
3442 pring->txcmplq_cnt = 0;
3443 spin_unlock_irq(&phba->hbalock);
3444
3445 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003446 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3447 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003448
3449 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003450 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3451 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003452}
3453
3454/**
James Smart3772a992009-05-22 14:50:54 -04003455 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003456 * @phba: Pointer to HBA context object.
3457 * @mask: Bit mask to be checked.
3458 *
3459 * This function reads the host status register and compares
3460 * with the provided bit mask to check if HBA completed
3461 * the restart. This function will wait in a loop for the
3462 * HBA to complete restart. If the HBA does not restart within
3463 * 15 iterations, the function will reset the HBA again. The
3464 * function returns 1 when HBA fail to restart otherwise returns
3465 * zero.
3466 **/
James Smart3772a992009-05-22 14:50:54 -04003467static int
3468lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003469{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003470 uint32_t status;
3471 int i = 0;
3472 int retval = 0;
dea31012005-04-17 16:05:31 -05003473
Jamie Wellnitz41415862006-02-28 19:25:27 -05003474 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003475 if (lpfc_readl(phba->HSregaddr, &status))
3476 return 1;
dea31012005-04-17 16:05:31 -05003477
Jamie Wellnitz41415862006-02-28 19:25:27 -05003478 /*
3479 * Check status register every 100ms for 5 retries, then every
3480 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3481 * every 2.5 sec for 4.
3482 * Break our of the loop if errors occurred during init.
3483 */
3484 while (((status & mask) != mask) &&
3485 !(status & HS_FFERM) &&
3486 i++ < 20) {
dea31012005-04-17 16:05:31 -05003487
Jamie Wellnitz41415862006-02-28 19:25:27 -05003488 if (i <= 5)
3489 msleep(10);
3490 else if (i <= 10)
3491 msleep(500);
3492 else
3493 msleep(2500);
dea31012005-04-17 16:05:31 -05003494
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003496 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003497 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003498 lpfc_sli_brdrestart(phba);
3499 }
3500 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003501 if (lpfc_readl(phba->HSregaddr, &status)) {
3502 retval = 1;
3503 break;
3504 }
dea31012005-04-17 16:05:31 -05003505 }
dea31012005-04-17 16:05:31 -05003506
Jamie Wellnitz41415862006-02-28 19:25:27 -05003507 /* Check to see if any errors occurred during init */
3508 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3510 "2751 Adapter failed to restart, "
3511 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3512 status,
3513 readl(phba->MBslimaddr + 0xa8),
3514 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003515 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003516 retval = 1;
3517 }
dea31012005-04-17 16:05:31 -05003518
Jamie Wellnitz41415862006-02-28 19:25:27 -05003519 return retval;
dea31012005-04-17 16:05:31 -05003520}
3521
James Smartda0436e2009-05-22 14:51:39 -04003522/**
3523 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3524 * @phba: Pointer to HBA context object.
3525 * @mask: Bit mask to be checked.
3526 *
3527 * This function checks the host status register to check if HBA is
3528 * ready. This function will wait in a loop for the HBA to be ready
3529 * If the HBA is not ready , the function will will reset the HBA PCI
3530 * function again. The function returns 1 when HBA fail to be ready
3531 * otherwise returns zero.
3532 **/
3533static int
3534lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3535{
3536 uint32_t status;
3537 int retval = 0;
3538
3539 /* Read the HBA Host Status Register */
3540 status = lpfc_sli4_post_status_check(phba);
3541
3542 if (status) {
3543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3544 lpfc_sli_brdrestart(phba);
3545 status = lpfc_sli4_post_status_check(phba);
3546 }
3547
3548 /* Check to see if any errors occurred during init */
3549 if (status) {
3550 phba->link_state = LPFC_HBA_ERROR;
3551 retval = 1;
3552 } else
3553 phba->sli4_hba.intr_enable = 0;
3554
3555 return retval;
3556}
3557
3558/**
3559 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3560 * @phba: Pointer to HBA context object.
3561 * @mask: Bit mask to be checked.
3562 *
3563 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3564 * from the API jump table function pointer from the lpfc_hba struct.
3565 **/
3566int
3567lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3568{
3569 return phba->lpfc_sli_brdready(phba, mask);
3570}
3571
James Smart92908312006-03-07 15:04:13 -05003572#define BARRIER_TEST_PATTERN (0xdeadbeef)
3573
James Smarte59058c2008-08-24 21:49:00 -04003574/**
James Smart3621a712009-04-06 18:47:14 -04003575 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003576 * @phba: Pointer to HBA context object.
3577 *
3578 * This function is called before resetting an HBA. This
3579 * function requests HBA to quiesce DMAs before a reset.
3580 **/
James Smart2e0fef82007-06-17 19:56:36 -05003581void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003582{
James Smart65a29c12006-07-06 15:50:50 -04003583 uint32_t __iomem *resp_buf;
3584 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003585 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003586 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003587 int i;
3588 uint8_t hdrtype;
3589
3590 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3591 if (hdrtype != 0x80 ||
3592 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3593 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3594 return;
3595
3596 /*
3597 * Tell the other part of the chip to suspend temporarily all
3598 * its DMA activity.
3599 */
James Smart65a29c12006-07-06 15:50:50 -04003600 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003601
3602 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003603 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3604 return;
James Smart92908312006-03-07 15:04:13 -05003605 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003607 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003608
James Smart9940b972011-03-11 16:06:12 -05003609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3610 return;
3611 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003612 /* Clear Chip error bit */
3613 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003614 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003615 }
3616
3617 mbox = 0;
3618 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3619 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3620
3621 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003622 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003623 writel(mbox, mbox_buf);
3624
James Smart9940b972011-03-11 16:06:12 -05003625 for (i = 0; i < 50; i++) {
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN))
3629 mdelay(1);
3630 else
3631 break;
3632 }
3633 resp_data = 0;
3634 if (lpfc_readl((resp_buf + 1), &resp_data))
3635 return;
3636 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003637 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003638 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003639 goto restore_hc;
3640 else
3641 goto clear_errat;
3642 }
3643
3644 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003645 resp_data = 0;
3646 for (i = 0; i < 500; i++) {
3647 if (lpfc_readl(resp_buf, &resp_data))
3648 return;
3649 if (resp_data != mbox)
3650 mdelay(1);
3651 else
3652 break;
3653 }
James Smart92908312006-03-07 15:04:13 -05003654
3655clear_errat:
3656
James Smart9940b972011-03-11 16:06:12 -05003657 while (++i < 500) {
3658 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3659 return;
3660 if (!(ha_copy & HA_ERATT))
3661 mdelay(1);
3662 else
3663 break;
3664 }
James Smart92908312006-03-07 15:04:13 -05003665
3666 if (readl(phba->HAregaddr) & HA_ERATT) {
3667 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003668 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003669 }
3670
3671restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003672 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003673 writel(hc_copy, phba->HCregaddr);
3674 readl(phba->HCregaddr); /* flush */
3675}
3676
James Smarte59058c2008-08-24 21:49:00 -04003677/**
James Smart3621a712009-04-06 18:47:14 -04003678 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003679 * @phba: Pointer to HBA context object.
3680 *
3681 * This function issues a kill_board mailbox command and waits for
3682 * the error attention interrupt. This function is called for stopping
3683 * the firmware processing. The caller is not required to hold any
3684 * locks. This function calls lpfc_hba_down_post function to free
3685 * any pending commands after the kill. The function will return 1 when it
3686 * fails to kill the board else will return 0.
3687 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003688int
James Smart2e0fef82007-06-17 19:56:36 -05003689lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003690{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003691 struct lpfc_sli *psli;
3692 LPFC_MBOXQ_t *pmb;
3693 uint32_t status;
3694 uint32_t ha_copy;
3695 int retval;
3696 int i = 0;
3697
3698 psli = &phba->sli;
3699
3700 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003702 "0329 Kill HBA Data: x%x x%x\n",
3703 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003704
James Smart98c9ea52007-10-27 13:37:33 -04003705 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3706 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003707 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003708
3709 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003710 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003711 if (lpfc_readl(phba->HCregaddr, &status)) {
3712 spin_unlock_irq(&phba->hbalock);
3713 mempool_free(pmb, phba->mbox_mem_pool);
3714 return 1;
3715 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003716 status &= ~HC_ERINT_ENA;
3717 writel(status, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003719 phba->link_flag |= LS_IGNORE_ERATT;
3720 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003721
3722 lpfc_kill_board(phba, pmb);
3723 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3724 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3725
3726 if (retval != MBX_SUCCESS) {
3727 if (retval != MBX_BUSY)
3728 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3730 "2752 KILL_BOARD command failed retval %d\n",
3731 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003732 spin_lock_irq(&phba->hbalock);
3733 phba->link_flag &= ~LS_IGNORE_ERATT;
3734 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003735 return 1;
3736 }
3737
James Smartf4b4c682009-05-22 14:53:12 -04003738 spin_lock_irq(&phba->hbalock);
3739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3740 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003741
Jamie Wellnitz41415862006-02-28 19:25:27 -05003742 mempool_free(pmb, phba->mbox_mem_pool);
3743
3744 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3745 * attention every 100ms for 3 seconds. If we don't get ERATT after
3746 * 3 seconds we still set HBA_ERROR state because the status of the
3747 * board is now undefined.
3748 */
James Smart9940b972011-03-11 16:06:12 -05003749 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3750 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003751 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3752 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003753 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3754 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003755 }
3756
3757 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003758 if (ha_copy & HA_ERATT) {
3759 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003760 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003761 }
James Smart2e0fef82007-06-17 19:56:36 -05003762 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003764 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003765 phba->link_flag &= ~LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003767
Jamie Wellnitz41415862006-02-28 19:25:27 -05003768 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003769 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003770
James Smart2e0fef82007-06-17 19:56:36 -05003771 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003772}
3773
James Smarte59058c2008-08-24 21:49:00 -04003774/**
James Smart3772a992009-05-22 14:50:54 -04003775 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003776 * @phba: Pointer to HBA context object.
3777 *
3778 * This function resets the HBA by writing HC_INITFF to the control
3779 * register. After the HBA resets, this function resets all the iocb ring
3780 * indices. This function disables PCI layer parity checking during
3781 * the reset.
3782 * This function returns 0 always.
3783 * The caller is not required to hold any locks.
3784 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003785int
James Smart2e0fef82007-06-17 19:56:36 -05003786lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003787{
3788 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003789 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003790 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003791 int i;
dea31012005-04-17 16:05:31 -05003792
Jamie Wellnitz41415862006-02-28 19:25:27 -05003793 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003794
Jamie Wellnitz41415862006-02-28 19:25:27 -05003795 /* Reset HBA */
3796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003797 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003798 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003799
3800 /* perform board reset */
3801 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003802 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003803 phba->pport->fc_myDID = 0;
3804 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003805
Jamie Wellnitz41415862006-02-28 19:25:27 -05003806 /* Turn off parity checking and serr during the physical reset */
3807 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3809 (cfg_value &
3810 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3811
James Smart3772a992009-05-22 14:50:54 -04003812 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3813
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814 /* Now toggle INITFF bit in the Host Control Register */
3815 writel(HC_INITFF, phba->HCregaddr);
3816 mdelay(1);
3817 readl(phba->HCregaddr); /* flush */
3818 writel(0, phba->HCregaddr);
3819 readl(phba->HCregaddr); /* flush */
3820
3821 /* Restore PCI cmd register */
3822 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003823
3824 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 for (i = 0; i < psli->num_rings; i++) {
3826 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003827 pring->flag = 0;
3828 pring->rspidx = 0;
3829 pring->next_cmdidx = 0;
3830 pring->local_getidx = 0;
3831 pring->cmdidx = 0;
3832 pring->missbufcnt = 0;
3833 }
dea31012005-04-17 16:05:31 -05003834
James Smart2e0fef82007-06-17 19:56:36 -05003835 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003836 return 0;
3837}
3838
James Smarte59058c2008-08-24 21:49:00 -04003839/**
James Smartda0436e2009-05-22 14:51:39 -04003840 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3841 * @phba: Pointer to HBA context object.
3842 *
3843 * This function resets a SLI4 HBA. This function disables PCI layer parity
3844 * checking during resets the device. The caller is not required to hold
3845 * any locks.
3846 *
3847 * This function returns 0 always.
3848 **/
3849int
3850lpfc_sli4_brdreset(struct lpfc_hba *phba)
3851{
3852 struct lpfc_sli *psli = &phba->sli;
3853 uint16_t cfg_value;
3854 uint8_t qindx;
3855
3856 /* Reset HBA */
3857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3858 "0295 Reset HBA Data: x%x x%x\n",
3859 phba->pport->port_state, psli->sli_flag);
3860
3861 /* perform board reset */
3862 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003863 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003864 phba->pport->fc_myDID = 0;
3865 phba->pport->fc_prevDID = 0;
3866
James Smartda0436e2009-05-22 14:51:39 -04003867 spin_lock_irq(&phba->hbalock);
3868 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3869 phba->fcf.fcf_flag = 0;
3870 /* Clean up the child queue list for the CQs */
3871 list_del_init(&phba->sli4_hba.mbx_wq->list);
3872 list_del_init(&phba->sli4_hba.els_wq->list);
3873 list_del_init(&phba->sli4_hba.hdr_rq->list);
3874 list_del_init(&phba->sli4_hba.dat_rq->list);
3875 list_del_init(&phba->sli4_hba.mbx_cq->list);
3876 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003877 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3878 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003879 qindx = 0;
3880 do
James Smartda0436e2009-05-22 14:51:39 -04003881 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003882 while (++qindx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04003883 spin_unlock_irq(&phba->hbalock);
3884
3885 /* Now physically reset the device */
3886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3887 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003888
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3892 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3893
James Smartda0436e2009-05-22 14:51:39 -04003894 /* Perform FCoE PCI function reset */
3895 lpfc_pci_function_reset(phba);
3896
James Smartbe858b62010-12-15 17:57:20 -05003897 /* Restore PCI cmd register */
3898 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3899
James Smartda0436e2009-05-22 14:51:39 -04003900 return 0;
3901}
3902
3903/**
3904 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003905 * @phba: Pointer to HBA context object.
3906 *
3907 * This function is called in the SLI initialization code path to
3908 * restart the HBA. The caller is not required to hold any lock.
3909 * This function writes MBX_RESTART mailbox command to the SLIM and
3910 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3911 * function to free any pending commands. The function enables
3912 * POST only during the first initialization. The function returns zero.
3913 * The function does not guarantee completion of MBX_RESTART mailbox
3914 * command before the return of this function.
3915 **/
James Smartda0436e2009-05-22 14:51:39 -04003916static int
3917lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003918{
3919 MAILBOX_t *mb;
3920 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003921 volatile uint32_t word0;
3922 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003923 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003924
James Smart2e0fef82007-06-17 19:56:36 -05003925 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003926
James Smart0d878412009-10-02 15:16:56 -04003927 /* Take PCIe device Advanced Error Reporting (AER) state */
3928 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3929
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930 psli = &phba->sli;
3931
3932 /* Restart HBA */
3933 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003934 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003935 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936
3937 word0 = 0;
3938 mb = (MAILBOX_t *) &word0;
3939 mb->mbxCommand = MBX_RESTART;
3940 mb->mbxHc = 1;
3941
James Smart92908312006-03-07 15:04:13 -05003942 lpfc_reset_barrier(phba);
3943
Jamie Wellnitz41415862006-02-28 19:25:27 -05003944 to_slim = phba->MBslimaddr;
3945 writel(*(uint32_t *) mb, to_slim);
3946 readl(to_slim); /* flush */
3947
3948 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003949 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003950 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003951 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003953 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003954 writel(*(uint32_t *) mb, to_slim);
3955 readl(to_slim); /* flush */
3956
3957 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003958 phba->pport->stopped = 0;
3959 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003960 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003961 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003962
James Smart64ba8812006-08-02 15:24:34 -04003963 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3964 psli->stats_start = get_seconds();
3965
James Smarteaf15d52008-12-04 22:39:29 -05003966 /* Give the INITFF and Post time to settle. */
3967 mdelay(100);
dea31012005-04-17 16:05:31 -05003968
James Smart0d878412009-10-02 15:16:56 -04003969 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3970 if (hba_aer_enabled)
3971 pci_disable_pcie_error_reporting(phba->pcidev);
3972
Jamie Wellnitz41415862006-02-28 19:25:27 -05003973 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003974
3975 return 0;
3976}
3977
James Smarte59058c2008-08-24 21:49:00 -04003978/**
James Smartda0436e2009-05-22 14:51:39 -04003979 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3980 * @phba: Pointer to HBA context object.
3981 *
3982 * This function is called in the SLI initialization code path to restart
3983 * a SLI4 HBA. The caller is not required to hold any lock.
3984 * At the end of the function, it calls lpfc_hba_down_post function to
3985 * free any pending commands.
3986 **/
3987static int
3988lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3989{
3990 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003991 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003992
3993 /* Restart HBA */
3994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3995 "0296 Restart HBA Data: x%x x%x\n",
3996 phba->pport->port_state, psli->sli_flag);
3997
James Smart75baf692010-06-08 18:31:21 -04003998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
James Smartda0436e2009-05-22 14:51:39 -04004001 lpfc_sli4_brdreset(phba);
4002
4003 spin_lock_irq(&phba->hbalock);
4004 phba->pport->stopped = 0;
4005 phba->link_state = LPFC_INIT_START;
4006 phba->hba_flag = 0;
4007 spin_unlock_irq(&phba->hbalock);
4008
4009 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4010 psli->stats_start = get_seconds();
4011
James Smart75baf692010-06-08 18:31:21 -04004012 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4013 if (hba_aer_enabled)
4014 pci_disable_pcie_error_reporting(phba->pcidev);
4015
James Smartda0436e2009-05-22 14:51:39 -04004016 lpfc_hba_down_post(phba);
4017
4018 return 0;
4019}
4020
4021/**
4022 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4023 * @phba: Pointer to HBA context object.
4024 *
4025 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4026 * API jump table function pointer from the lpfc_hba struct.
4027**/
4028int
4029lpfc_sli_brdrestart(struct lpfc_hba *phba)
4030{
4031 return phba->lpfc_sli_brdrestart(phba);
4032}
4033
4034/**
James Smart3621a712009-04-06 18:47:14 -04004035 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function is called after a HBA restart to wait for successful
4039 * restart of the HBA. Successful restart of the HBA is indicated by
4040 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4041 * iteration, the function will restart the HBA again. The function returns
4042 * zero if HBA successfully restarted else returns negative error code.
4043 **/
dea31012005-04-17 16:05:31 -05004044static int
4045lpfc_sli_chipset_init(struct lpfc_hba *phba)
4046{
4047 uint32_t status, i = 0;
4048
4049 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004050 if (lpfc_readl(phba->HSregaddr, &status))
4051 return -EIO;
dea31012005-04-17 16:05:31 -05004052
4053 /* Check status register to see what current state is */
4054 i = 0;
4055 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4056
James Smartdcf2a4e2010-09-29 11:18:53 -04004057 /* Check every 10ms for 10 retries, then every 100ms for 90
4058 * retries, then every 1 sec for 50 retires for a total of
4059 * ~60 seconds before reset the board again and check every
4060 * 1 sec for 50 retries. The up to 60 seconds before the
4061 * board ready is required by the Falcon FIPS zeroization
4062 * complete, and any reset the board in between shall cause
4063 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004064 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004065 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004066 /* Adapter failed to init, timeout, status reg
4067 <status> */
James Smarted957682007-06-17 19:56:37 -05004068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004069 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004070 "timeout, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004074 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004075 return -ETIMEDOUT;
4076 }
4077
4078 /* Check to see if any errors occurred during init */
4079 if (status & HS_FFERM) {
4080 /* ERROR: During chipset initialization */
4081 /* Adapter failed to init, chipset, status reg
4082 <status> */
James Smarted957682007-06-17 19:56:37 -05004083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004084 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004085 "chipset, status reg x%x, "
4086 "FW Data: A8 x%x AC x%x\n", status,
4087 readl(phba->MBslimaddr + 0xa8),
4088 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004089 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004090 return -EIO;
4091 }
4092
James Smartdcf2a4e2010-09-29 11:18:53 -04004093 if (i <= 10)
dea31012005-04-17 16:05:31 -05004094 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004095 else if (i <= 100)
4096 msleep(100);
4097 else
4098 msleep(1000);
dea31012005-04-17 16:05:31 -05004099
James Smartdcf2a4e2010-09-29 11:18:53 -04004100 if (i == 150) {
4101 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004102 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004103 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004104 }
4105 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004106 if (lpfc_readl(phba->HSregaddr, &status))
4107 return -EIO;
dea31012005-04-17 16:05:31 -05004108 }
4109
4110 /* Check to see if any errors occurred during init */
4111 if (status & HS_FFERM) {
4112 /* ERROR: During chipset initialization */
4113 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004115 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004116 "status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004120 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004121 return -EIO;
4122 }
4123
4124 /* Clear all interrupt enable conditions */
4125 writel(0, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
4127
4128 /* setup host attn register */
4129 writel(0xffffffff, phba->HAregaddr);
4130 readl(phba->HAregaddr); /* flush */
4131 return 0;
4132}
4133
James Smarte59058c2008-08-24 21:49:00 -04004134/**
James Smart3621a712009-04-06 18:47:14 -04004135 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004136 *
4137 * This function calculates and returns the number of HBQs required to be
4138 * configured.
4139 **/
James Smart78b2d852007-08-02 11:10:21 -04004140int
James Smarted957682007-06-17 19:56:37 -05004141lpfc_sli_hbq_count(void)
4142{
James Smart92d7f7b2007-06-17 19:56:38 -05004143 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004144}
4145
James Smarte59058c2008-08-24 21:49:00 -04004146/**
James Smart3621a712009-04-06 18:47:14 -04004147 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004148 *
4149 * This function adds the number of hbq entries in every HBQ to get
4150 * the total number of hbq entries required for the HBA and returns
4151 * the total count.
4152 **/
James Smarted957682007-06-17 19:56:37 -05004153static int
4154lpfc_sli_hbq_entry_count(void)
4155{
4156 int hbq_count = lpfc_sli_hbq_count();
4157 int count = 0;
4158 int i;
4159
4160 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004161 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004162 return count;
4163}
4164
James Smarte59058c2008-08-24 21:49:00 -04004165/**
James Smart3621a712009-04-06 18:47:14 -04004166 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004167 *
4168 * This function calculates amount of memory required for all hbq entries
4169 * to be configured and returns the total memory required.
4170 **/
dea31012005-04-17 16:05:31 -05004171int
James Smarted957682007-06-17 19:56:37 -05004172lpfc_sli_hbq_size(void)
4173{
4174 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4175}
4176
James Smarte59058c2008-08-24 21:49:00 -04004177/**
James Smart3621a712009-04-06 18:47:14 -04004178 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004179 * @phba: Pointer to HBA context object.
4180 *
4181 * This function is called during the SLI initialization to configure
4182 * all the HBQs and post buffers to the HBQ. The caller is not
4183 * required to hold any locks. This function will return zero if successful
4184 * else it will return negative error code.
4185 **/
James Smarted957682007-06-17 19:56:37 -05004186static int
4187lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4188{
4189 int hbq_count = lpfc_sli_hbq_count();
4190 LPFC_MBOXQ_t *pmb;
4191 MAILBOX_t *pmbox;
4192 uint32_t hbqno;
4193 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004194
James Smart92d7f7b2007-06-17 19:56:38 -05004195 /* Get a Mailbox buffer to setup mailbox
4196 * commands for HBA initialization
4197 */
James Smarted957682007-06-17 19:56:37 -05004198 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4199
4200 if (!pmb)
4201 return -ENOMEM;
4202
James Smart04c68492009-05-22 14:52:52 -04004203 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004204
4205 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4206 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004207 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004208
4209 hbq_entry_index = 0;
4210 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4211 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4212 phba->hbqs[hbqno].hbqPutIdx = 0;
4213 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4214 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004215 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004216 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4217 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004218 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4219
4220 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4221 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4222 mbxStatus <status>, ring <num> */
4223
4224 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004225 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004226 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004227 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004228 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004229 pmbox->mbxStatus, hbqno);
4230
4231 phba->link_state = LPFC_HBA_ERROR;
4232 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004233 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004234 }
4235 }
4236 phba->hbq_count = hbq_count;
4237
James Smarted957682007-06-17 19:56:37 -05004238 mempool_free(pmb, phba->mbox_mem_pool);
4239
James Smart92d7f7b2007-06-17 19:56:38 -05004240 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004241 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4242 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004243 return 0;
4244}
4245
James Smarte59058c2008-08-24 21:49:00 -04004246/**
James Smart4f774512009-05-22 14:52:35 -04004247 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4248 * @phba: Pointer to HBA context object.
4249 *
4250 * This function is called during the SLI initialization to configure
4251 * all the HBQs and post buffers to the HBQ. The caller is not
4252 * required to hold any locks. This function will return zero if successful
4253 * else it will return negative error code.
4254 **/
4255static int
4256lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4257{
4258 phba->hbq_in_use = 1;
4259 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4260 phba->hbq_count = 1;
4261 /* Initially populate or replenish the HBQs */
4262 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4263 return 0;
4264}
4265
4266/**
James Smart3621a712009-04-06 18:47:14 -04004267 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004268 * @phba: Pointer to HBA context object.
4269 * @sli_mode: sli mode - 2/3
4270 *
4271 * This function is called by the sli intialization code path
4272 * to issue config_port mailbox command. This function restarts the
4273 * HBA firmware and issues a config_port mailbox command to configure
4274 * the SLI interface in the sli mode specified by sli_mode
4275 * variable. The caller is not required to hold any locks.
4276 * The function returns 0 if successful, else returns negative error
4277 * code.
4278 **/
James Smart93996272008-08-24 21:50:30 -04004279int
4280lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004281{
4282 LPFC_MBOXQ_t *pmb;
4283 uint32_t resetcount = 0, rc = 0, done = 0;
4284
4285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004287 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004288 return -ENOMEM;
4289 }
4290
James Smarted957682007-06-17 19:56:37 -05004291 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004292 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004293 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004294 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004295 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004296 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004297 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004298 rc = lpfc_sli_chipset_init(phba);
4299 if (rc)
4300 break;
4301
James Smart2e0fef82007-06-17 19:56:36 -05004302 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004303 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004304 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004305 resetcount++;
4306
James Smarted957682007-06-17 19:56:37 -05004307 /* Call pre CONFIG_PORT mailbox command initialization. A
4308 * value of 0 means the call was successful. Any other
4309 * nonzero value is a failure, but if ERESTART is returned,
4310 * the driver may reset the HBA and try again.
4311 */
dea31012005-04-17 16:05:31 -05004312 rc = lpfc_config_port_prep(phba);
4313 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004314 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004315 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004316 } else if (rc)
dea31012005-04-17 16:05:31 -05004317 break;
James Smart6d368e52011-05-24 11:44:12 -04004318
James Smart2e0fef82007-06-17 19:56:36 -05004319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004320 lpfc_config_port(phba, pmb);
4321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004322 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4323 LPFC_SLI3_HBQ_ENABLED |
4324 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004325 LPFC_SLI3_BG_ENABLED |
4326 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004327 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004329 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004330 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004331 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004332 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004333 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_unlock_irq(&phba->hbalock);
4335 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004336 } else {
4337 /* Allow asynchronous mailbox command to go through */
4338 spin_lock_irq(&phba->hbalock);
4339 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4340 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004341 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004342
4343 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4344 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4345 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4346 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004347 }
dea31012005-04-17 16:05:31 -05004348 }
James Smarted957682007-06-17 19:56:37 -05004349 if (!done) {
4350 rc = -EINVAL;
4351 goto do_prep_failed;
4352 }
James Smart04c68492009-05-22 14:52:52 -04004353 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4354 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004355 rc = -ENXIO;
4356 goto do_prep_failed;
4357 }
James Smart04c68492009-05-22 14:52:52 -04004358 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004359 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004360 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4361 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4362 phba->max_vpi : phba->max_vports;
4363
James Smart34b02dc2008-08-24 21:49:55 -04004364 } else
4365 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004366 phba->fips_level = 0;
4367 phba->fips_spec_rev = 0;
4368 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004369 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004370 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4371 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4372 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4373 "2850 Security Crypto Active. FIPS x%d "
4374 "(Spec Rev: x%d)",
4375 phba->fips_level, phba->fips_spec_rev);
4376 }
4377 if (pmb->u.mb.un.varCfgPort.sec_err) {
4378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4379 "2856 Config Port Security Crypto "
4380 "Error: x%x ",
4381 pmb->u.mb.un.varCfgPort.sec_err);
4382 }
James Smart04c68492009-05-22 14:52:52 -04004383 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004384 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004385 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004386 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004387
4388 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4389 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004390
4391 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004392 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004393 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4394 else
4395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4396 "0443 Adapter did not grant "
4397 "BlockGuard\n");
4398 }
James Smart34b02dc2008-08-24 21:49:55 -04004399 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004400 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004401 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004402 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004403 }
James Smart92d7f7b2007-06-17 19:56:38 -05004404do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004405 mempool_free(pmb, phba->mbox_mem_pool);
4406 return rc;
4407}
4408
James Smarte59058c2008-08-24 21:49:00 -04004409
4410/**
James Smart3621a712009-04-06 18:47:14 -04004411 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004412 * @phba: Pointer to HBA context object.
4413 *
4414 * This function is the main SLI intialization function. This function
4415 * is called by the HBA intialization code, HBA reset code and HBA
4416 * error attention handler code. Caller is not required to hold any
4417 * locks. This function issues config_port mailbox command to configure
4418 * the SLI, setup iocb rings and HBQ rings. In the end the function
4419 * calls the config_port_post function to issue init_link mailbox
4420 * command and to start the discovery. The function will return zero
4421 * if successful, else it will return negative error code.
4422 **/
James Smarted957682007-06-17 19:56:37 -05004423int
4424lpfc_sli_hba_setup(struct lpfc_hba *phba)
4425{
4426 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004427 int mode = 3, i;
4428 int longs;
James Smarted957682007-06-17 19:56:37 -05004429
4430 switch (lpfc_sli_mode) {
4431 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004432 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004434 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004435 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004436 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004437 break;
4438 }
James Smarted957682007-06-17 19:56:37 -05004439 mode = 2;
4440 break;
4441 case 0:
4442 case 3:
4443 break;
4444 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004446 "1819 Unrecognized lpfc_sli_mode "
4447 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004448
4449 break;
4450 }
4451
James Smart93996272008-08-24 21:50:30 -04004452 rc = lpfc_sli_config_port(phba, mode);
4453
James Smarted957682007-06-17 19:56:37 -05004454 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004456 "1820 Unable to select SLI-3. "
4457 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004458 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004459 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004460 if (rc)
dea31012005-04-17 16:05:31 -05004461 goto lpfc_sli_hba_setup_error;
4462
James Smart0d878412009-10-02 15:16:56 -04004463 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4464 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4465 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4466 if (!rc) {
4467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4468 "2709 This device supports "
4469 "Advanced Error Reporting (AER)\n");
4470 spin_lock_irq(&phba->hbalock);
4471 phba->hba_flag |= HBA_AER_ENABLED;
4472 spin_unlock_irq(&phba->hbalock);
4473 } else {
4474 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4475 "2708 This device does not support "
4476 "Advanced Error Reporting (AER)\n");
4477 phba->cfg_aer_support = 0;
4478 }
4479 }
4480
James Smarted957682007-06-17 19:56:37 -05004481 if (phba->sli_rev == 3) {
4482 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4483 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004484 } else {
4485 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4486 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004487 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004488 }
4489
4490 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004491 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4492 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004493 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004494
4495 if (rc)
4496 goto lpfc_sli_hba_setup_error;
4497
James Smart6d368e52011-05-24 11:44:12 -04004498 /* Initialize VPIs. */
4499 if (phba->sli_rev == LPFC_SLI_REV3) {
4500 /*
4501 * The VPI bitmask and physical ID array are allocated
4502 * and initialized once only - at driver load. A port
4503 * reset doesn't need to reinitialize this memory.
4504 */
4505 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4506 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4507 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4508 GFP_KERNEL);
4509 if (!phba->vpi_bmask) {
4510 rc = -ENOMEM;
4511 goto lpfc_sli_hba_setup_error;
4512 }
4513
4514 phba->vpi_ids = kzalloc(
4515 (phba->max_vpi+1) * sizeof(uint16_t),
4516 GFP_KERNEL);
4517 if (!phba->vpi_ids) {
4518 kfree(phba->vpi_bmask);
4519 rc = -ENOMEM;
4520 goto lpfc_sli_hba_setup_error;
4521 }
4522 for (i = 0; i < phba->max_vpi; i++)
4523 phba->vpi_ids[i] = i;
4524 }
4525 }
4526
James Smart93996272008-08-24 21:50:30 -04004527 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004528 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4529 rc = lpfc_sli_hbq_setup(phba);
4530 if (rc)
4531 goto lpfc_sli_hba_setup_error;
4532 }
James Smart04c68492009-05-22 14:52:52 -04004533 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004534 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004535 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004536
4537 rc = lpfc_config_port_post(phba);
4538 if (rc)
4539 goto lpfc_sli_hba_setup_error;
4540
James Smarted957682007-06-17 19:56:37 -05004541 return rc;
4542
James Smart92d7f7b2007-06-17 19:56:38 -05004543lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004544 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004546 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004547 return rc;
4548}
4549
James Smartda0436e2009-05-22 14:51:39 -04004550/**
4551 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4552 * @phba: Pointer to HBA context object.
4553 * @mboxq: mailbox pointer.
4554 * This function issue a dump mailbox command to read config region
4555 * 23 and parse the records in the region and populate driver
4556 * data structure.
4557 **/
4558static int
James Smartff78d8f2011-12-13 13:21:35 -05004559lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004560{
James Smartff78d8f2011-12-13 13:21:35 -05004561 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004562 struct lpfc_dmabuf *mp;
4563 struct lpfc_mqe *mqe;
4564 uint32_t data_length;
4565 int rc;
4566
4567 /* Program the default value of vlan_id and fc_map */
4568 phba->valid_vlan = 0;
4569 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4570 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4571 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4572
James Smartff78d8f2011-12-13 13:21:35 -05004573 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4574 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004575 return -ENOMEM;
4576
James Smartff78d8f2011-12-13 13:21:35 -05004577 mqe = &mboxq->u.mqe;
4578 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4579 rc = -ENOMEM;
4580 goto out_free_mboxq;
4581 }
4582
James Smartda0436e2009-05-22 14:51:39 -04004583 mp = (struct lpfc_dmabuf *) mboxq->context1;
4584 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4585
4586 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4587 "(%d):2571 Mailbox cmd x%x Status x%x "
4588 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4589 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4590 "CQ: x%x x%x x%x x%x\n",
4591 mboxq->vport ? mboxq->vport->vpi : 0,
4592 bf_get(lpfc_mqe_command, mqe),
4593 bf_get(lpfc_mqe_status, mqe),
4594 mqe->un.mb_words[0], mqe->un.mb_words[1],
4595 mqe->un.mb_words[2], mqe->un.mb_words[3],
4596 mqe->un.mb_words[4], mqe->un.mb_words[5],
4597 mqe->un.mb_words[6], mqe->un.mb_words[7],
4598 mqe->un.mb_words[8], mqe->un.mb_words[9],
4599 mqe->un.mb_words[10], mqe->un.mb_words[11],
4600 mqe->un.mb_words[12], mqe->un.mb_words[13],
4601 mqe->un.mb_words[14], mqe->un.mb_words[15],
4602 mqe->un.mb_words[16], mqe->un.mb_words[50],
4603 mboxq->mcqe.word0,
4604 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4605 mboxq->mcqe.trailer);
4606
4607 if (rc) {
4608 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4609 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004610 rc = -EIO;
4611 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004612 }
4613 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004614 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004615 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4616 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004617 rc = -EIO;
4618 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004619 }
James Smartda0436e2009-05-22 14:51:39 -04004620
4621 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4622 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4623 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004624 rc = 0;
4625
4626out_free_mboxq:
4627 mempool_free(mboxq, phba->mbox_mem_pool);
4628 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004629}
4630
4631/**
4632 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4633 * @phba: pointer to lpfc hba data structure.
4634 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4635 * @vpd: pointer to the memory to hold resulting port vpd data.
4636 * @vpd_size: On input, the number of bytes allocated to @vpd.
4637 * On output, the number of data bytes in @vpd.
4638 *
4639 * This routine executes a READ_REV SLI4 mailbox command. In
4640 * addition, this routine gets the port vpd data.
4641 *
4642 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004643 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004644 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004645 **/
4646static int
4647lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4648 uint8_t *vpd, uint32_t *vpd_size)
4649{
4650 int rc = 0;
4651 uint32_t dma_size;
4652 struct lpfc_dmabuf *dmabuf;
4653 struct lpfc_mqe *mqe;
4654
4655 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4656 if (!dmabuf)
4657 return -ENOMEM;
4658
4659 /*
4660 * Get a DMA buffer for the vpd data resulting from the READ_REV
4661 * mailbox command.
4662 */
4663 dma_size = *vpd_size;
4664 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4665 dma_size,
4666 &dmabuf->phys,
4667 GFP_KERNEL);
4668 if (!dmabuf->virt) {
4669 kfree(dmabuf);
4670 return -ENOMEM;
4671 }
4672 memset(dmabuf->virt, 0, dma_size);
4673
4674 /*
4675 * The SLI4 implementation of READ_REV conflicts at word1,
4676 * bits 31:16 and SLI4 adds vpd functionality not present
4677 * in SLI3. This code corrects the conflicts.
4678 */
4679 lpfc_read_rev(phba, mboxq);
4680 mqe = &mboxq->u.mqe;
4681 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4682 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4683 mqe->un.read_rev.word1 &= 0x0000FFFF;
4684 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4685 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4686
4687 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4688 if (rc) {
4689 dma_free_coherent(&phba->pcidev->dev, dma_size,
4690 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004691 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004692 return -EIO;
4693 }
4694
James Smartda0436e2009-05-22 14:51:39 -04004695 /*
4696 * The available vpd length cannot be bigger than the
4697 * DMA buffer passed to the port. Catch the less than
4698 * case and update the caller's size.
4699 */
4700 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4701 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4702
James Smartd7c47992010-06-08 18:31:54 -04004703 memcpy(vpd, dmabuf->virt, *vpd_size);
4704
James Smartda0436e2009-05-22 14:51:39 -04004705 dma_free_coherent(&phba->pcidev->dev, dma_size,
4706 dmabuf->virt, dmabuf->phys);
4707 kfree(dmabuf);
4708 return 0;
4709}
4710
4711/**
James Smartcd1c8302011-10-10 21:33:25 -04004712 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4713 * @phba: pointer to lpfc hba data structure.
4714 *
4715 * This routine retrieves SLI4 device physical port name this PCI function
4716 * is attached to.
4717 *
4718 * Return codes
4719 * 0 - sucessful
4720 * otherwise - failed to retrieve physical port name
4721 **/
4722static int
4723lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4724{
4725 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004726 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4727 struct lpfc_controller_attribute *cntl_attr;
4728 struct lpfc_mbx_get_port_name *get_port_name;
4729 void *virtaddr = NULL;
4730 uint32_t alloclen, reqlen;
4731 uint32_t shdr_status, shdr_add_status;
4732 union lpfc_sli4_cfg_shdr *shdr;
4733 char cport_name = 0;
4734 int rc;
4735
4736 /* We assume nothing at this point */
4737 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4738 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4739
4740 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4741 if (!mboxq)
4742 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004743 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004744 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4745 lpfc_sli4_read_config(phba);
4746 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4747 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004748
4749 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4750 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4751 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4752 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4753 LPFC_SLI4_MBX_NEMBED);
4754 if (alloclen < reqlen) {
4755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4756 "3084 Allocated DMA memory size (%d) is "
4757 "less than the requested DMA memory size "
4758 "(%d)\n", alloclen, reqlen);
4759 rc = -ENOMEM;
4760 goto out_free_mboxq;
4761 }
4762 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4763 virtaddr = mboxq->sge_array->addr[0];
4764 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4765 shdr = &mbx_cntl_attr->cfg_shdr;
4766 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4767 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4768 if (shdr_status || shdr_add_status || rc) {
4769 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4770 "3085 Mailbox x%x (x%x/x%x) failed, "
4771 "rc:x%x, status:x%x, add_status:x%x\n",
4772 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4773 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4774 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4775 rc, shdr_status, shdr_add_status);
4776 rc = -ENXIO;
4777 goto out_free_mboxq;
4778 }
4779 cntl_attr = &mbx_cntl_attr->cntl_attr;
4780 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4781 phba->sli4_hba.lnk_info.lnk_tp =
4782 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4783 phba->sli4_hba.lnk_info.lnk_no =
4784 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4785 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4786 "3086 lnk_type:%d, lnk_numb:%d\n",
4787 phba->sli4_hba.lnk_info.lnk_tp,
4788 phba->sli4_hba.lnk_info.lnk_no);
4789
4790retrieve_ppname:
4791 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4792 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4793 sizeof(struct lpfc_mbx_get_port_name) -
4794 sizeof(struct lpfc_sli4_cfg_mhdr),
4795 LPFC_SLI4_MBX_EMBED);
4796 get_port_name = &mboxq->u.mqe.un.get_port_name;
4797 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4798 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4799 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4800 phba->sli4_hba.lnk_info.lnk_tp);
4801 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4804 if (shdr_status || shdr_add_status || rc) {
4805 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4806 "3087 Mailbox x%x (x%x/x%x) failed: "
4807 "rc:x%x, status:x%x, add_status:x%x\n",
4808 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4809 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4810 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4811 rc, shdr_status, shdr_add_status);
4812 rc = -ENXIO;
4813 goto out_free_mboxq;
4814 }
4815 switch (phba->sli4_hba.lnk_info.lnk_no) {
4816 case LPFC_LINK_NUMBER_0:
4817 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4818 &get_port_name->u.response);
4819 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4820 break;
4821 case LPFC_LINK_NUMBER_1:
4822 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4823 &get_port_name->u.response);
4824 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4825 break;
4826 case LPFC_LINK_NUMBER_2:
4827 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4828 &get_port_name->u.response);
4829 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4830 break;
4831 case LPFC_LINK_NUMBER_3:
4832 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4833 &get_port_name->u.response);
4834 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4835 break;
4836 default:
4837 break;
4838 }
4839
4840 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4841 phba->Port[0] = cport_name;
4842 phba->Port[1] = '\0';
4843 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4844 "3091 SLI get port name: %s\n", phba->Port);
4845 }
4846
4847out_free_mboxq:
4848 if (rc != MBX_TIMEOUT) {
4849 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4850 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4851 else
4852 mempool_free(mboxq, phba->mbox_mem_pool);
4853 }
4854 return rc;
4855}
4856
4857/**
James Smartda0436e2009-05-22 14:51:39 -04004858 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4859 * @phba: pointer to lpfc hba data structure.
4860 *
4861 * This routine is called to explicitly arm the SLI4 device's completion and
4862 * event queues
4863 **/
4864static void
4865lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4866{
4867 uint8_t fcp_eqidx;
4868
4869 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4870 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004871 fcp_eqidx = 0;
4872 do
James Smartda0436e2009-05-22 14:51:39 -04004873 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4874 LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004875 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04004876 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4877 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4878 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4879 LPFC_QUEUE_REARM);
4880}
4881
4882/**
James Smart6d368e52011-05-24 11:44:12 -04004883 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4884 * @phba: Pointer to HBA context object.
4885 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004886 * @extnt_count: buffer to hold port available extent count.
4887 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004888 *
James Smartb76f2dc2011-07-22 18:37:42 -04004889 * This function calls the port and retrievs the number of available
4890 * extents and their size for a particular extent type.
4891 *
4892 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004893 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004894int
James Smart6d368e52011-05-24 11:44:12 -04004895lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4896 uint16_t *extnt_count, uint16_t *extnt_size)
4897{
4898 int rc = 0;
4899 uint32_t length;
4900 uint32_t mbox_tmo;
4901 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4902 LPFC_MBOXQ_t *mbox;
4903
4904 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4905 if (!mbox)
4906 return -ENOMEM;
4907
4908 /* Find out how many extents are available for this resource type */
4909 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4910 sizeof(struct lpfc_sli4_cfg_mhdr));
4911 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4912 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4913 length, LPFC_SLI4_MBX_EMBED);
4914
4915 /* Send an extents count of 0 - the GET doesn't use it. */
4916 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4917 LPFC_SLI4_MBX_EMBED);
4918 if (unlikely(rc)) {
4919 rc = -EIO;
4920 goto err_exit;
4921 }
4922
4923 if (!phba->sli4_hba.intr_enable)
4924 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4925 else {
James Smarta183a152011-10-10 21:32:43 -04004926 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004927 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4928 }
4929 if (unlikely(rc)) {
4930 rc = -EIO;
4931 goto err_exit;
4932 }
4933
4934 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4935 if (bf_get(lpfc_mbox_hdr_status,
4936 &rsrc_info->header.cfg_shdr.response)) {
4937 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4938 "2930 Failed to get resource extents "
4939 "Status 0x%x Add'l Status 0x%x\n",
4940 bf_get(lpfc_mbox_hdr_status,
4941 &rsrc_info->header.cfg_shdr.response),
4942 bf_get(lpfc_mbox_hdr_add_status,
4943 &rsrc_info->header.cfg_shdr.response));
4944 rc = -EIO;
4945 goto err_exit;
4946 }
4947
4948 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4949 &rsrc_info->u.rsp);
4950 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4951 &rsrc_info->u.rsp);
4952 err_exit:
4953 mempool_free(mbox, phba->mbox_mem_pool);
4954 return rc;
4955}
4956
4957/**
4958 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4959 * @phba: Pointer to HBA context object.
4960 * @type: The extent type to check.
4961 *
4962 * This function reads the current available extents from the port and checks
4963 * if the extent count or extent size has changed since the last access.
4964 * Callers use this routine post port reset to understand if there is a
4965 * extent reprovisioning requirement.
4966 *
4967 * Returns:
4968 * -Error: error indicates problem.
4969 * 1: Extent count or size has changed.
4970 * 0: No changes.
4971 **/
4972static int
4973lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4974{
4975 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4976 uint16_t size_diff, rsrc_ext_size;
4977 int rc = 0;
4978 struct lpfc_rsrc_blks *rsrc_entry;
4979 struct list_head *rsrc_blk_list = NULL;
4980
4981 size_diff = 0;
4982 curr_ext_cnt = 0;
4983 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4984 &rsrc_ext_cnt,
4985 &rsrc_ext_size);
4986 if (unlikely(rc))
4987 return -EIO;
4988
4989 switch (type) {
4990 case LPFC_RSC_TYPE_FCOE_RPI:
4991 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4992 break;
4993 case LPFC_RSC_TYPE_FCOE_VPI:
4994 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4995 break;
4996 case LPFC_RSC_TYPE_FCOE_XRI:
4997 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4998 break;
4999 case LPFC_RSC_TYPE_FCOE_VFI:
5000 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5001 break;
5002 default:
5003 break;
5004 }
5005
5006 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5007 curr_ext_cnt++;
5008 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5009 size_diff++;
5010 }
5011
5012 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5013 rc = 1;
5014
5015 return rc;
5016}
5017
5018/**
5019 * lpfc_sli4_cfg_post_extnts -
5020 * @phba: Pointer to HBA context object.
5021 * @extnt_cnt - number of available extents.
5022 * @type - the extent type (rpi, xri, vfi, vpi).
5023 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5024 * @mbox - pointer to the caller's allocated mailbox structure.
5025 *
5026 * This function executes the extents allocation request. It also
5027 * takes care of the amount of memory needed to allocate or get the
5028 * allocated extents. It is the caller's responsibility to evaluate
5029 * the response.
5030 *
5031 * Returns:
5032 * -Error: Error value describes the condition found.
5033 * 0: if successful
5034 **/
5035static int
5036lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5037 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5038{
5039 int rc = 0;
5040 uint32_t req_len;
5041 uint32_t emb_len;
5042 uint32_t alloc_len, mbox_tmo;
5043
5044 /* Calculate the total requested length of the dma memory */
5045 req_len = *extnt_cnt * sizeof(uint16_t);
5046
5047 /*
5048 * Calculate the size of an embedded mailbox. The uint32_t
5049 * accounts for extents-specific word.
5050 */
5051 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5052 sizeof(uint32_t);
5053
5054 /*
5055 * Presume the allocation and response will fit into an embedded
5056 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5057 */
5058 *emb = LPFC_SLI4_MBX_EMBED;
5059 if (req_len > emb_len) {
5060 req_len = *extnt_cnt * sizeof(uint16_t) +
5061 sizeof(union lpfc_sli4_cfg_shdr) +
5062 sizeof(uint32_t);
5063 *emb = LPFC_SLI4_MBX_NEMBED;
5064 }
5065
5066 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5067 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5068 req_len, *emb);
5069 if (alloc_len < req_len) {
5070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005071 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005072 "less than the requested DMA memory "
5073 "size (x%x)\n", alloc_len, req_len);
5074 return -ENOMEM;
5075 }
5076 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5077 if (unlikely(rc))
5078 return -EIO;
5079
5080 if (!phba->sli4_hba.intr_enable)
5081 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5082 else {
James Smarta183a152011-10-10 21:32:43 -04005083 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005084 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5085 }
5086
5087 if (unlikely(rc))
5088 rc = -EIO;
5089 return rc;
5090}
5091
5092/**
5093 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5094 * @phba: Pointer to HBA context object.
5095 * @type: The resource extent type to allocate.
5096 *
5097 * This function allocates the number of elements for the specified
5098 * resource type.
5099 **/
5100static int
5101lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5102{
5103 bool emb = false;
5104 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5105 uint16_t rsrc_id, rsrc_start, j, k;
5106 uint16_t *ids;
5107 int i, rc;
5108 unsigned long longs;
5109 unsigned long *bmask;
5110 struct lpfc_rsrc_blks *rsrc_blks;
5111 LPFC_MBOXQ_t *mbox;
5112 uint32_t length;
5113 struct lpfc_id_range *id_array = NULL;
5114 void *virtaddr = NULL;
5115 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5116 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5117 struct list_head *ext_blk_list;
5118
5119 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5120 &rsrc_cnt,
5121 &rsrc_size);
5122 if (unlikely(rc))
5123 return -EIO;
5124
5125 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5126 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5127 "3009 No available Resource Extents "
5128 "for resource type 0x%x: Count: 0x%x, "
5129 "Size 0x%x\n", type, rsrc_cnt,
5130 rsrc_size);
5131 return -ENOMEM;
5132 }
5133
5134 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5135 "2903 Available Resource Extents "
5136 "for resource type 0x%x: Count: 0x%x, "
5137 "Size 0x%x\n", type, rsrc_cnt,
5138 rsrc_size);
5139
5140 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5141 if (!mbox)
5142 return -ENOMEM;
5143
5144 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5145 if (unlikely(rc)) {
5146 rc = -EIO;
5147 goto err_exit;
5148 }
5149
5150 /*
5151 * Figure out where the response is located. Then get local pointers
5152 * to the response data. The port does not guarantee to respond to
5153 * all extents counts request so update the local variable with the
5154 * allocated count from the port.
5155 */
5156 if (emb == LPFC_SLI4_MBX_EMBED) {
5157 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5158 id_array = &rsrc_ext->u.rsp.id[0];
5159 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5160 } else {
5161 virtaddr = mbox->sge_array->addr[0];
5162 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5163 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5164 id_array = &n_rsrc->id;
5165 }
5166
5167 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5168 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5169
5170 /*
5171 * Based on the resource size and count, correct the base and max
5172 * resource values.
5173 */
5174 length = sizeof(struct lpfc_rsrc_blks);
5175 switch (type) {
5176 case LPFC_RSC_TYPE_FCOE_RPI:
5177 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5178 sizeof(unsigned long),
5179 GFP_KERNEL);
5180 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5181 rc = -ENOMEM;
5182 goto err_exit;
5183 }
5184 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5185 sizeof(uint16_t),
5186 GFP_KERNEL);
5187 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5188 kfree(phba->sli4_hba.rpi_bmask);
5189 rc = -ENOMEM;
5190 goto err_exit;
5191 }
5192
5193 /*
5194 * The next_rpi was initialized with the maximum available
5195 * count but the port may allocate a smaller number. Catch
5196 * that case and update the next_rpi.
5197 */
5198 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5199
5200 /* Initialize local ptrs for common extent processing later. */
5201 bmask = phba->sli4_hba.rpi_bmask;
5202 ids = phba->sli4_hba.rpi_ids;
5203 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5204 break;
5205 case LPFC_RSC_TYPE_FCOE_VPI:
5206 phba->vpi_bmask = kzalloc(longs *
5207 sizeof(unsigned long),
5208 GFP_KERNEL);
5209 if (unlikely(!phba->vpi_bmask)) {
5210 rc = -ENOMEM;
5211 goto err_exit;
5212 }
5213 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5214 sizeof(uint16_t),
5215 GFP_KERNEL);
5216 if (unlikely(!phba->vpi_ids)) {
5217 kfree(phba->vpi_bmask);
5218 rc = -ENOMEM;
5219 goto err_exit;
5220 }
5221
5222 /* Initialize local ptrs for common extent processing later. */
5223 bmask = phba->vpi_bmask;
5224 ids = phba->vpi_ids;
5225 ext_blk_list = &phba->lpfc_vpi_blk_list;
5226 break;
5227 case LPFC_RSC_TYPE_FCOE_XRI:
5228 phba->sli4_hba.xri_bmask = kzalloc(longs *
5229 sizeof(unsigned long),
5230 GFP_KERNEL);
5231 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5232 rc = -ENOMEM;
5233 goto err_exit;
5234 }
5235 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5236 sizeof(uint16_t),
5237 GFP_KERNEL);
5238 if (unlikely(!phba->sli4_hba.xri_ids)) {
5239 kfree(phba->sli4_hba.xri_bmask);
5240 rc = -ENOMEM;
5241 goto err_exit;
5242 }
5243
5244 /* Initialize local ptrs for common extent processing later. */
5245 bmask = phba->sli4_hba.xri_bmask;
5246 ids = phba->sli4_hba.xri_ids;
5247 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_VFI:
5250 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5251 sizeof(unsigned long),
5252 GFP_KERNEL);
5253 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5254 rc = -ENOMEM;
5255 goto err_exit;
5256 }
5257 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5258 sizeof(uint16_t),
5259 GFP_KERNEL);
5260 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5261 kfree(phba->sli4_hba.vfi_bmask);
5262 rc = -ENOMEM;
5263 goto err_exit;
5264 }
5265
5266 /* Initialize local ptrs for common extent processing later. */
5267 bmask = phba->sli4_hba.vfi_bmask;
5268 ids = phba->sli4_hba.vfi_ids;
5269 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5270 break;
5271 default:
5272 /* Unsupported Opcode. Fail call. */
5273 id_array = NULL;
5274 bmask = NULL;
5275 ids = NULL;
5276 ext_blk_list = NULL;
5277 goto err_exit;
5278 }
5279
5280 /*
5281 * Complete initializing the extent configuration with the
5282 * allocated ids assigned to this function. The bitmask serves
5283 * as an index into the array and manages the available ids. The
5284 * array just stores the ids communicated to the port via the wqes.
5285 */
5286 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5287 if ((i % 2) == 0)
5288 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5289 &id_array[k]);
5290 else
5291 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5292 &id_array[k]);
5293
5294 rsrc_blks = kzalloc(length, GFP_KERNEL);
5295 if (unlikely(!rsrc_blks)) {
5296 rc = -ENOMEM;
5297 kfree(bmask);
5298 kfree(ids);
5299 goto err_exit;
5300 }
5301 rsrc_blks->rsrc_start = rsrc_id;
5302 rsrc_blks->rsrc_size = rsrc_size;
5303 list_add_tail(&rsrc_blks->list, ext_blk_list);
5304 rsrc_start = rsrc_id;
5305 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5306 phba->sli4_hba.scsi_xri_start = rsrc_start +
5307 lpfc_sli4_get_els_iocb_cnt(phba);
5308
5309 while (rsrc_id < (rsrc_start + rsrc_size)) {
5310 ids[j] = rsrc_id;
5311 rsrc_id++;
5312 j++;
5313 }
5314 /* Entire word processed. Get next word.*/
5315 if ((i % 2) == 1)
5316 k++;
5317 }
5318 err_exit:
5319 lpfc_sli4_mbox_cmd_free(phba, mbox);
5320 return rc;
5321}
5322
5323/**
5324 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5325 * @phba: Pointer to HBA context object.
5326 * @type: the extent's type.
5327 *
5328 * This function deallocates all extents of a particular resource type.
5329 * SLI4 does not allow for deallocating a particular extent range. It
5330 * is the caller's responsibility to release all kernel memory resources.
5331 **/
5332static int
5333lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5334{
5335 int rc;
5336 uint32_t length, mbox_tmo = 0;
5337 LPFC_MBOXQ_t *mbox;
5338 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5339 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5340
5341 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5342 if (!mbox)
5343 return -ENOMEM;
5344
5345 /*
5346 * This function sends an embedded mailbox because it only sends the
5347 * the resource type. All extents of this type are released by the
5348 * port.
5349 */
5350 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5351 sizeof(struct lpfc_sli4_cfg_mhdr));
5352 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5353 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5354 length, LPFC_SLI4_MBX_EMBED);
5355
5356 /* Send an extents count of 0 - the dealloc doesn't use it. */
5357 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5358 LPFC_SLI4_MBX_EMBED);
5359 if (unlikely(rc)) {
5360 rc = -EIO;
5361 goto out_free_mbox;
5362 }
5363 if (!phba->sli4_hba.intr_enable)
5364 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5365 else {
James Smarta183a152011-10-10 21:32:43 -04005366 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005367 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5368 }
5369 if (unlikely(rc)) {
5370 rc = -EIO;
5371 goto out_free_mbox;
5372 }
5373
5374 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5375 if (bf_get(lpfc_mbox_hdr_status,
5376 &dealloc_rsrc->header.cfg_shdr.response)) {
5377 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5378 "2919 Failed to release resource extents "
5379 "for type %d - Status 0x%x Add'l Status 0x%x. "
5380 "Resource memory not released.\n",
5381 type,
5382 bf_get(lpfc_mbox_hdr_status,
5383 &dealloc_rsrc->header.cfg_shdr.response),
5384 bf_get(lpfc_mbox_hdr_add_status,
5385 &dealloc_rsrc->header.cfg_shdr.response));
5386 rc = -EIO;
5387 goto out_free_mbox;
5388 }
5389
5390 /* Release kernel memory resources for the specific type. */
5391 switch (type) {
5392 case LPFC_RSC_TYPE_FCOE_VPI:
5393 kfree(phba->vpi_bmask);
5394 kfree(phba->vpi_ids);
5395 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5396 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5397 &phba->lpfc_vpi_blk_list, list) {
5398 list_del_init(&rsrc_blk->list);
5399 kfree(rsrc_blk);
5400 }
5401 break;
5402 case LPFC_RSC_TYPE_FCOE_XRI:
5403 kfree(phba->sli4_hba.xri_bmask);
5404 kfree(phba->sli4_hba.xri_ids);
5405 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5406 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5407 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5408 list_del_init(&rsrc_blk->list);
5409 kfree(rsrc_blk);
5410 }
5411 break;
5412 case LPFC_RSC_TYPE_FCOE_VFI:
5413 kfree(phba->sli4_hba.vfi_bmask);
5414 kfree(phba->sli4_hba.vfi_ids);
5415 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5416 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5417 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5418 list_del_init(&rsrc_blk->list);
5419 kfree(rsrc_blk);
5420 }
5421 break;
5422 case LPFC_RSC_TYPE_FCOE_RPI:
5423 /* RPI bitmask and physical id array are cleaned up earlier. */
5424 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5425 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5426 list_del_init(&rsrc_blk->list);
5427 kfree(rsrc_blk);
5428 }
5429 break;
5430 default:
5431 break;
5432 }
5433
5434 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5435
5436 out_free_mbox:
5437 mempool_free(mbox, phba->mbox_mem_pool);
5438 return rc;
5439}
5440
5441/**
5442 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5443 * @phba: Pointer to HBA context object.
5444 *
5445 * This function allocates all SLI4 resource identifiers.
5446 **/
5447int
5448lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5449{
5450 int i, rc, error = 0;
5451 uint16_t count, base;
5452 unsigned long longs;
5453
James Smartff78d8f2011-12-13 13:21:35 -05005454 if (!phba->sli4_hba.rpi_hdrs_in_use)
5455 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005456 if (phba->sli4_hba.extents_in_use) {
5457 /*
5458 * The port supports resource extents. The XRI, VPI, VFI, RPI
5459 * resource extent count must be read and allocated before
5460 * provisioning the resource id arrays.
5461 */
5462 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5463 LPFC_IDX_RSRC_RDY) {
5464 /*
5465 * Extent-based resources are set - the driver could
5466 * be in a port reset. Figure out if any corrective
5467 * actions need to be taken.
5468 */
5469 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5470 LPFC_RSC_TYPE_FCOE_VFI);
5471 if (rc != 0)
5472 error++;
5473 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5474 LPFC_RSC_TYPE_FCOE_VPI);
5475 if (rc != 0)
5476 error++;
5477 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5478 LPFC_RSC_TYPE_FCOE_XRI);
5479 if (rc != 0)
5480 error++;
5481 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5482 LPFC_RSC_TYPE_FCOE_RPI);
5483 if (rc != 0)
5484 error++;
5485
5486 /*
5487 * It's possible that the number of resources
5488 * provided to this port instance changed between
5489 * resets. Detect this condition and reallocate
5490 * resources. Otherwise, there is no action.
5491 */
5492 if (error) {
5493 lpfc_printf_log(phba, KERN_INFO,
5494 LOG_MBOX | LOG_INIT,
5495 "2931 Detected extent resource "
5496 "change. Reallocating all "
5497 "extents.\n");
5498 rc = lpfc_sli4_dealloc_extent(phba,
5499 LPFC_RSC_TYPE_FCOE_VFI);
5500 rc = lpfc_sli4_dealloc_extent(phba,
5501 LPFC_RSC_TYPE_FCOE_VPI);
5502 rc = lpfc_sli4_dealloc_extent(phba,
5503 LPFC_RSC_TYPE_FCOE_XRI);
5504 rc = lpfc_sli4_dealloc_extent(phba,
5505 LPFC_RSC_TYPE_FCOE_RPI);
5506 } else
5507 return 0;
5508 }
5509
5510 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5511 if (unlikely(rc))
5512 goto err_exit;
5513
5514 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5515 if (unlikely(rc))
5516 goto err_exit;
5517
5518 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5519 if (unlikely(rc))
5520 goto err_exit;
5521
5522 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5523 if (unlikely(rc))
5524 goto err_exit;
5525 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5526 LPFC_IDX_RSRC_RDY);
5527 return rc;
5528 } else {
5529 /*
5530 * The port does not support resource extents. The XRI, VPI,
5531 * VFI, RPI resource ids were determined from READ_CONFIG.
5532 * Just allocate the bitmasks and provision the resource id
5533 * arrays. If a port reset is active, the resources don't
5534 * need any action - just exit.
5535 */
5536 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005537 LPFC_IDX_RSRC_RDY) {
5538 lpfc_sli4_dealloc_resource_identifiers(phba);
5539 lpfc_sli4_remove_rpis(phba);
5540 }
James Smart6d368e52011-05-24 11:44:12 -04005541 /* RPIs. */
5542 count = phba->sli4_hba.max_cfg_param.max_rpi;
5543 base = phba->sli4_hba.max_cfg_param.rpi_base;
5544 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5545 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5546 sizeof(unsigned long),
5547 GFP_KERNEL);
5548 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5549 rc = -ENOMEM;
5550 goto err_exit;
5551 }
5552 phba->sli4_hba.rpi_ids = kzalloc(count *
5553 sizeof(uint16_t),
5554 GFP_KERNEL);
5555 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5556 rc = -ENOMEM;
5557 goto free_rpi_bmask;
5558 }
5559
5560 for (i = 0; i < count; i++)
5561 phba->sli4_hba.rpi_ids[i] = base + i;
5562
5563 /* VPIs. */
5564 count = phba->sli4_hba.max_cfg_param.max_vpi;
5565 base = phba->sli4_hba.max_cfg_param.vpi_base;
5566 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5567 phba->vpi_bmask = kzalloc(longs *
5568 sizeof(unsigned long),
5569 GFP_KERNEL);
5570 if (unlikely(!phba->vpi_bmask)) {
5571 rc = -ENOMEM;
5572 goto free_rpi_ids;
5573 }
5574 phba->vpi_ids = kzalloc(count *
5575 sizeof(uint16_t),
5576 GFP_KERNEL);
5577 if (unlikely(!phba->vpi_ids)) {
5578 rc = -ENOMEM;
5579 goto free_vpi_bmask;
5580 }
5581
5582 for (i = 0; i < count; i++)
5583 phba->vpi_ids[i] = base + i;
5584
5585 /* XRIs. */
5586 count = phba->sli4_hba.max_cfg_param.max_xri;
5587 base = phba->sli4_hba.max_cfg_param.xri_base;
5588 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5589 phba->sli4_hba.xri_bmask = kzalloc(longs *
5590 sizeof(unsigned long),
5591 GFP_KERNEL);
5592 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5593 rc = -ENOMEM;
5594 goto free_vpi_ids;
5595 }
5596 phba->sli4_hba.xri_ids = kzalloc(count *
5597 sizeof(uint16_t),
5598 GFP_KERNEL);
5599 if (unlikely(!phba->sli4_hba.xri_ids)) {
5600 rc = -ENOMEM;
5601 goto free_xri_bmask;
5602 }
5603
5604 for (i = 0; i < count; i++)
5605 phba->sli4_hba.xri_ids[i] = base + i;
5606
5607 /* VFIs. */
5608 count = phba->sli4_hba.max_cfg_param.max_vfi;
5609 base = phba->sli4_hba.max_cfg_param.vfi_base;
5610 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5611 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5612 sizeof(unsigned long),
5613 GFP_KERNEL);
5614 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5615 rc = -ENOMEM;
5616 goto free_xri_ids;
5617 }
5618 phba->sli4_hba.vfi_ids = kzalloc(count *
5619 sizeof(uint16_t),
5620 GFP_KERNEL);
5621 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5622 rc = -ENOMEM;
5623 goto free_vfi_bmask;
5624 }
5625
5626 for (i = 0; i < count; i++)
5627 phba->sli4_hba.vfi_ids[i] = base + i;
5628
5629 /*
5630 * Mark all resources ready. An HBA reset doesn't need
5631 * to reset the initialization.
5632 */
5633 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5634 LPFC_IDX_RSRC_RDY);
5635 return 0;
5636 }
5637
5638 free_vfi_bmask:
5639 kfree(phba->sli4_hba.vfi_bmask);
5640 free_xri_ids:
5641 kfree(phba->sli4_hba.xri_ids);
5642 free_xri_bmask:
5643 kfree(phba->sli4_hba.xri_bmask);
5644 free_vpi_ids:
5645 kfree(phba->vpi_ids);
5646 free_vpi_bmask:
5647 kfree(phba->vpi_bmask);
5648 free_rpi_ids:
5649 kfree(phba->sli4_hba.rpi_ids);
5650 free_rpi_bmask:
5651 kfree(phba->sli4_hba.rpi_bmask);
5652 err_exit:
5653 return rc;
5654}
5655
5656/**
5657 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5658 * @phba: Pointer to HBA context object.
5659 *
5660 * This function allocates the number of elements for the specified
5661 * resource type.
5662 **/
5663int
5664lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5665{
5666 if (phba->sli4_hba.extents_in_use) {
5667 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5668 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5669 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5670 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5671 } else {
5672 kfree(phba->vpi_bmask);
5673 kfree(phba->vpi_ids);
5674 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5675 kfree(phba->sli4_hba.xri_bmask);
5676 kfree(phba->sli4_hba.xri_ids);
5677 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5678 kfree(phba->sli4_hba.vfi_bmask);
5679 kfree(phba->sli4_hba.vfi_ids);
5680 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5681 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5682 }
5683
5684 return 0;
5685}
5686
5687/**
James Smartb76f2dc2011-07-22 18:37:42 -04005688 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5689 * @phba: Pointer to HBA context object.
5690 * @type: The resource extent type.
5691 * @extnt_count: buffer to hold port extent count response
5692 * @extnt_size: buffer to hold port extent size response.
5693 *
5694 * This function calls the port to read the host allocated extents
5695 * for a particular type.
5696 **/
5697int
5698lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5699 uint16_t *extnt_cnt, uint16_t *extnt_size)
5700{
5701 bool emb;
5702 int rc = 0;
5703 uint16_t curr_blks = 0;
5704 uint32_t req_len, emb_len;
5705 uint32_t alloc_len, mbox_tmo;
5706 struct list_head *blk_list_head;
5707 struct lpfc_rsrc_blks *rsrc_blk;
5708 LPFC_MBOXQ_t *mbox;
5709 void *virtaddr = NULL;
5710 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5711 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5712 union lpfc_sli4_cfg_shdr *shdr;
5713
5714 switch (type) {
5715 case LPFC_RSC_TYPE_FCOE_VPI:
5716 blk_list_head = &phba->lpfc_vpi_blk_list;
5717 break;
5718 case LPFC_RSC_TYPE_FCOE_XRI:
5719 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5720 break;
5721 case LPFC_RSC_TYPE_FCOE_VFI:
5722 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5723 break;
5724 case LPFC_RSC_TYPE_FCOE_RPI:
5725 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5726 break;
5727 default:
5728 return -EIO;
5729 }
5730
5731 /* Count the number of extents currently allocatd for this type. */
5732 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5733 if (curr_blks == 0) {
5734 /*
5735 * The GET_ALLOCATED mailbox does not return the size,
5736 * just the count. The size should be just the size
5737 * stored in the current allocated block and all sizes
5738 * for an extent type are the same so set the return
5739 * value now.
5740 */
5741 *extnt_size = rsrc_blk->rsrc_size;
5742 }
5743 curr_blks++;
5744 }
5745
5746 /* Calculate the total requested length of the dma memory. */
5747 req_len = curr_blks * sizeof(uint16_t);
5748
5749 /*
5750 * Calculate the size of an embedded mailbox. The uint32_t
5751 * accounts for extents-specific word.
5752 */
5753 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5754 sizeof(uint32_t);
5755
5756 /*
5757 * Presume the allocation and response will fit into an embedded
5758 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5759 */
5760 emb = LPFC_SLI4_MBX_EMBED;
5761 req_len = emb_len;
5762 if (req_len > emb_len) {
5763 req_len = curr_blks * sizeof(uint16_t) +
5764 sizeof(union lpfc_sli4_cfg_shdr) +
5765 sizeof(uint32_t);
5766 emb = LPFC_SLI4_MBX_NEMBED;
5767 }
5768
5769 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5770 if (!mbox)
5771 return -ENOMEM;
5772 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5773
5774 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5775 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5776 req_len, emb);
5777 if (alloc_len < req_len) {
5778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5779 "2983 Allocated DMA memory size (x%x) is "
5780 "less than the requested DMA memory "
5781 "size (x%x)\n", alloc_len, req_len);
5782 rc = -ENOMEM;
5783 goto err_exit;
5784 }
5785 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5786 if (unlikely(rc)) {
5787 rc = -EIO;
5788 goto err_exit;
5789 }
5790
5791 if (!phba->sli4_hba.intr_enable)
5792 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5793 else {
James Smarta183a152011-10-10 21:32:43 -04005794 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005795 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5796 }
5797
5798 if (unlikely(rc)) {
5799 rc = -EIO;
5800 goto err_exit;
5801 }
5802
5803 /*
5804 * Figure out where the response is located. Then get local pointers
5805 * to the response data. The port does not guarantee to respond to
5806 * all extents counts request so update the local variable with the
5807 * allocated count from the port.
5808 */
5809 if (emb == LPFC_SLI4_MBX_EMBED) {
5810 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5811 shdr = &rsrc_ext->header.cfg_shdr;
5812 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5813 } else {
5814 virtaddr = mbox->sge_array->addr[0];
5815 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5816 shdr = &n_rsrc->cfg_shdr;
5817 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5818 }
5819
5820 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5821 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5822 "2984 Failed to read allocated resources "
5823 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5824 type,
5825 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5826 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5827 rc = -EIO;
5828 goto err_exit;
5829 }
5830 err_exit:
5831 lpfc_sli4_mbox_cmd_free(phba, mbox);
5832 return rc;
5833}
5834
5835/**
James Smartda0436e2009-05-22 14:51:39 -04005836 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5837 * @phba: Pointer to HBA context object.
5838 *
5839 * This function is the main SLI4 device intialization PCI function. This
5840 * function is called by the HBA intialization code, HBA reset code and
5841 * HBA error attention handler code. Caller is not required to hold any
5842 * locks.
5843 **/
5844int
5845lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5846{
5847 int rc;
5848 LPFC_MBOXQ_t *mboxq;
5849 struct lpfc_mqe *mqe;
5850 uint8_t *vpd;
5851 uint32_t vpd_size;
5852 uint32_t ftr_rsp = 0;
5853 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5854 struct lpfc_vport *vport = phba->pport;
5855 struct lpfc_dmabuf *mp;
5856
5857 /* Perform a PCI function reset to start from clean */
5858 rc = lpfc_pci_function_reset(phba);
5859 if (unlikely(rc))
5860 return -ENODEV;
5861
5862 /* Check the HBA Host Status Register for readyness */
5863 rc = lpfc_sli4_post_status_check(phba);
5864 if (unlikely(rc))
5865 return -ENODEV;
5866 else {
5867 spin_lock_irq(&phba->hbalock);
5868 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5869 spin_unlock_irq(&phba->hbalock);
5870 }
5871
5872 /*
5873 * Allocate a single mailbox container for initializing the
5874 * port.
5875 */
5876 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5877 if (!mboxq)
5878 return -ENOMEM;
5879
James Smartda0436e2009-05-22 14:51:39 -04005880 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005881 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005882 vpd = kzalloc(vpd_size, GFP_KERNEL);
5883 if (!vpd) {
5884 rc = -ENOMEM;
5885 goto out_free_mbox;
5886 }
5887
5888 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005889 if (unlikely(rc)) {
5890 kfree(vpd);
5891 goto out_free_mbox;
5892 }
James Smartda0436e2009-05-22 14:51:39 -04005893 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005894 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5895 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005896 phba->hba_flag |= HBA_FCOE_MODE;
5897 else
5898 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005899
5900 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5901 LPFC_DCBX_CEE_MODE)
5902 phba->hba_flag |= HBA_FIP_SUPPORT;
5903 else
5904 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5905
James Smartc31098c2011-04-16 11:03:33 -04005906 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5908 "0376 READ_REV Error. SLI Level %d "
5909 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005910 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005911 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005912 kfree(vpd);
5913 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005914 }
James Smartcd1c8302011-10-10 21:33:25 -04005915
5916 /*
James Smartff78d8f2011-12-13 13:21:35 -05005917 * Continue initialization with default values even if driver failed
5918 * to read FCoE param config regions, only read parameters if the
5919 * board is FCoE
5920 */
5921 if (phba->hba_flag & HBA_FCOE_MODE &&
5922 lpfc_sli4_read_fcoe_params(phba))
5923 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5924 "2570 Failed to read FCoE parameters\n");
5925
5926 /*
James Smartcd1c8302011-10-10 21:33:25 -04005927 * Retrieve sli4 device physical port name, failure of doing it
5928 * is considered as non-fatal.
5929 */
5930 rc = lpfc_sli4_retrieve_pport_name(phba);
5931 if (!rc)
5932 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5933 "3080 Successful retrieving SLI4 device "
5934 "physical port name: %s.\n", phba->Port);
5935
James Smartda0436e2009-05-22 14:51:39 -04005936 /*
5937 * Evaluate the read rev and vpd data. Populate the driver
5938 * state with the results. If this routine fails, the failure
5939 * is not fatal as the driver will use generic values.
5940 */
5941 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5942 if (unlikely(!rc)) {
5943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5944 "0377 Error %d parsing vpd. "
5945 "Using defaults.\n", rc);
5946 rc = 0;
5947 }
James Smart76a95d72010-11-20 23:11:48 -05005948 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005949
James Smartf1126682009-06-10 17:22:44 -04005950 /* Save information as VPD data */
5951 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5952 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5953 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5954 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5955 &mqe->un.read_rev);
5956 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5957 &mqe->un.read_rev);
5958 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5959 &mqe->un.read_rev);
5960 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5961 &mqe->un.read_rev);
5962 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5963 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5964 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5965 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5966 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5967 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5968 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5969 "(%d):0380 READ_REV Status x%x "
5970 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5971 mboxq->vport ? mboxq->vport->vpi : 0,
5972 bf_get(lpfc_mqe_status, mqe),
5973 phba->vpd.rev.opFwName,
5974 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5975 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04005976
5977 /*
5978 * Discover the port's supported feature set and match it against the
5979 * hosts requests.
5980 */
5981 lpfc_request_features(phba, mboxq);
5982 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5983 if (unlikely(rc)) {
5984 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005985 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005986 }
5987
5988 /*
5989 * The port must support FCP initiator mode as this is the
5990 * only mode running in the host.
5991 */
5992 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5993 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5994 "0378 No support for fcpi mode.\n");
5995 ftr_rsp++;
5996 }
James Smartfedd3b72011-02-16 12:39:24 -05005997 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5998 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5999 else
6000 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006001 /*
6002 * If the port cannot support the host's requested features
6003 * then turn off the global config parameters to disable the
6004 * feature in the driver. This is not a fatal error.
6005 */
James Smartbf086112011-08-21 21:48:13 -04006006 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6007 if (phba->cfg_enable_bg) {
6008 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6009 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6010 else
6011 ftr_rsp++;
6012 }
James Smartda0436e2009-05-22 14:51:39 -04006013
6014 if (phba->max_vpi && phba->cfg_enable_npiv &&
6015 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6016 ftr_rsp++;
6017
6018 if (ftr_rsp) {
6019 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6020 "0379 Feature Mismatch Data: x%08x %08x "
6021 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6022 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6023 phba->cfg_enable_npiv, phba->max_vpi);
6024 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6025 phba->cfg_enable_bg = 0;
6026 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6027 phba->cfg_enable_npiv = 0;
6028 }
6029
6030 /* These SLI3 features are assumed in SLI4 */
6031 spin_lock_irq(&phba->hbalock);
6032 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6033 spin_unlock_irq(&phba->hbalock);
6034
James Smart6d368e52011-05-24 11:44:12 -04006035 /*
6036 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6037 * calls depends on these resources to complete port setup.
6038 */
6039 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6040 if (rc) {
6041 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6042 "2920 Failed to alloc Resource IDs "
6043 "rc = x%x\n", rc);
6044 goto out_free_mbox;
6045 }
James Smartff78d8f2011-12-13 13:21:35 -05006046 /* update physical xri mappings in the scsi buffers */
6047 lpfc_scsi_buf_update(phba);
James Smart6d368e52011-05-24 11:44:12 -04006048
James Smartda0436e2009-05-22 14:51:39 -04006049 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006050 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6051 if (rc) {
6052 phba->link_state = LPFC_HBA_ERROR;
6053 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006054 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006055 }
6056
James Smartda0436e2009-05-22 14:51:39 -04006057 mboxq->vport = vport;
6058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6059 mp = (struct lpfc_dmabuf *) mboxq->context1;
6060 if (rc == MBX_SUCCESS) {
6061 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6062 rc = 0;
6063 }
6064
6065 /*
6066 * This memory was allocated by the lpfc_read_sparam routine. Release
6067 * it to the mbuf pool.
6068 */
6069 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6070 kfree(mp);
6071 mboxq->context1 = NULL;
6072 if (unlikely(rc)) {
6073 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6074 "0382 READ_SPARAM command failed "
6075 "status %d, mbxStatus x%x\n",
6076 rc, bf_get(lpfc_mqe_status, mqe));
6077 phba->link_state = LPFC_HBA_ERROR;
6078 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006079 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006080 }
6081
James Smart05580562011-05-24 11:40:48 -04006082 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006083
6084 /* Update the fc_host data structures with new wwn. */
6085 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6086 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6087
6088 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04006089 if (!phba->sli4_hba.extents_in_use) {
6090 rc = lpfc_sli4_post_els_sgl_list(phba);
6091 if (unlikely(rc)) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6093 "0582 Error %d during els sgl post "
6094 "operation\n", rc);
6095 rc = -ENODEV;
6096 goto out_free_mbox;
6097 }
6098 } else {
6099 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6100 if (unlikely(rc)) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6102 "2560 Error %d during els sgl post "
6103 "operation\n", rc);
6104 rc = -ENODEV;
6105 goto out_free_mbox;
6106 }
James Smartda0436e2009-05-22 14:51:39 -04006107 }
6108
6109 /* Register SCSI SGL pool to the device */
6110 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6111 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006112 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006113 "0383 Error %d during scsi sgl post "
6114 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006115 /* Some Scsi buffers were moved to the abort scsi list */
6116 /* A pci function reset will repost them */
6117 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006118 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006119 }
6120
6121 /* Post the rpi header region to the device. */
6122 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6123 if (unlikely(rc)) {
6124 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6125 "0393 Error %d during rpi post operation\n",
6126 rc);
6127 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006128 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006129 }
James Smartda0436e2009-05-22 14:51:39 -04006130
James Smart5350d872011-10-10 21:33:49 -04006131 /* Create all the SLI4 queues */
6132 rc = lpfc_sli4_queue_create(phba);
6133 if (rc) {
6134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6135 "3089 Failed to allocate queues\n");
6136 rc = -ENODEV;
6137 goto out_stop_timers;
6138 }
James Smartda0436e2009-05-22 14:51:39 -04006139 /* Set up all the queues to the device */
6140 rc = lpfc_sli4_queue_setup(phba);
6141 if (unlikely(rc)) {
6142 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6143 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006144 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006145 }
6146
6147 /* Arm the CQs and then EQs on device */
6148 lpfc_sli4_arm_cqeq_intr(phba);
6149
6150 /* Indicate device interrupt mode */
6151 phba->sli4_hba.intr_enable = 1;
6152
6153 /* Allow asynchronous mailbox command to go through */
6154 spin_lock_irq(&phba->hbalock);
6155 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6156 spin_unlock_irq(&phba->hbalock);
6157
6158 /* Post receive buffers to the device */
6159 lpfc_sli4_rb_setup(phba);
6160
James Smartfc2b9892010-02-26 14:15:29 -05006161 /* Reset HBA FCF states after HBA reset */
6162 phba->fcf.fcf_flag = 0;
6163 phba->fcf.current_rec.flag = 0;
6164
James Smartda0436e2009-05-22 14:51:39 -04006165 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006166 mod_timer(&vport->els_tmofunc,
6167 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006168
6169 /* Start heart beat timer */
6170 mod_timer(&phba->hb_tmofunc,
6171 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6172 phba->hb_outstanding = 0;
6173 phba->last_completion_time = jiffies;
6174
6175 /* Start error attention (ERATT) polling timer */
6176 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6177
James Smart75baf692010-06-08 18:31:21 -04006178 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6179 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6180 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6181 if (!rc) {
6182 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6183 "2829 This device supports "
6184 "Advanced Error Reporting (AER)\n");
6185 spin_lock_irq(&phba->hbalock);
6186 phba->hba_flag |= HBA_AER_ENABLED;
6187 spin_unlock_irq(&phba->hbalock);
6188 } else {
6189 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6190 "2830 This device does not support "
6191 "Advanced Error Reporting (AER)\n");
6192 phba->cfg_aer_support = 0;
6193 }
James Smart0a96e972011-07-22 18:37:28 -04006194 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006195 }
6196
James Smart76a95d72010-11-20 23:11:48 -05006197 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6198 /*
6199 * The FC Port needs to register FCFI (index 0)
6200 */
6201 lpfc_reg_fcfi(phba, mboxq);
6202 mboxq->vport = phba->pport;
6203 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006204 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006205 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006206 rc = 0;
6207 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6208 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006209
6210 /* Check if the port is configured to be disabled */
6211 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006212 }
James Smart026abb82011-12-13 13:20:45 -05006213
James Smartda0436e2009-05-22 14:51:39 -04006214 /*
6215 * The port is ready, set the host's link state to LINK_DOWN
6216 * in preparation for link interrupts.
6217 */
James Smartda0436e2009-05-22 14:51:39 -04006218 spin_lock_irq(&phba->hbalock);
6219 phba->link_state = LPFC_LINK_DOWN;
6220 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006221 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6222 (phba->hba_flag & LINK_DISABLED)) {
6223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6224 "3103 Adapter Link is disabled.\n");
6225 lpfc_down_link(phba, mboxq);
6226 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6227 if (rc != MBX_SUCCESS) {
6228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6229 "3104 Adapter failed to issue "
6230 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6231 goto out_unset_queue;
6232 }
6233 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smartfedd3b72011-02-16 12:39:24 -05006234 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart5350d872011-10-10 21:33:49 -04006235 if (rc)
6236 goto out_unset_queue;
6237 }
6238 mempool_free(mboxq, phba->mbox_mem_pool);
6239 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006240out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006241 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006242 lpfc_sli4_queue_unset(phba);
6243out_destroy_queue:
6244 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006245out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006246 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006247out_free_mbox:
6248 mempool_free(mboxq, phba->mbox_mem_pool);
6249 return rc;
6250}
James Smarte59058c2008-08-24 21:49:00 -04006251
6252/**
James Smart3621a712009-04-06 18:47:14 -04006253 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006254 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006255 *
James Smarte59058c2008-08-24 21:49:00 -04006256 * This is the callback function for mailbox timer. The mailbox
6257 * timer is armed when a new mailbox command is issued and the timer
6258 * is deleted when the mailbox complete. The function is called by
6259 * the kernel timer code when a mailbox does not complete within
6260 * expected time. This function wakes up the worker thread to
6261 * process the mailbox timeout and returns. All the processing is
6262 * done by the worker thread function lpfc_mbox_timeout_handler.
6263 **/
dea31012005-04-17 16:05:31 -05006264void
6265lpfc_mbox_timeout(unsigned long ptr)
6266{
James Smart92d7f7b2007-06-17 19:56:38 -05006267 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006268 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006269 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006270
James Smart2e0fef82007-06-17 19:56:36 -05006271 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006272 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006273 if (!tmo_posted)
6274 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6275 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6276
James Smart5e9d9b82008-06-14 22:52:53 -04006277 if (!tmo_posted)
6278 lpfc_worker_wake_up(phba);
6279 return;
dea31012005-04-17 16:05:31 -05006280}
6281
James Smarte59058c2008-08-24 21:49:00 -04006282
6283/**
James Smart3621a712009-04-06 18:47:14 -04006284 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006285 * @phba: Pointer to HBA context object.
6286 *
6287 * This function is called from worker thread when a mailbox command times out.
6288 * The caller is not required to hold any locks. This function will reset the
6289 * HBA and recover all the pending commands.
6290 **/
dea31012005-04-17 16:05:31 -05006291void
6292lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6293{
James Smart2e0fef82007-06-17 19:56:36 -05006294 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006295 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006296 struct lpfc_sli *psli = &phba->sli;
6297 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006298
James Smarta257bf92009-04-06 18:48:10 -04006299 /* Check the pmbox pointer first. There is a race condition
6300 * between the mbox timeout handler getting executed in the
6301 * worklist and the mailbox actually completing. When this
6302 * race condition occurs, the mbox_active will be NULL.
6303 */
6304 spin_lock_irq(&phba->hbalock);
6305 if (pmbox == NULL) {
6306 lpfc_printf_log(phba, KERN_WARNING,
6307 LOG_MBOX | LOG_SLI,
6308 "0353 Active Mailbox cleared - mailbox timeout "
6309 "exiting\n");
6310 spin_unlock_irq(&phba->hbalock);
6311 return;
6312 }
6313
dea31012005-04-17 16:05:31 -05006314 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006316 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006317 mb->mbxCommand,
6318 phba->pport->port_state,
6319 phba->sli.sli_flag,
6320 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006321 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006322
James Smart1dcb58e2007-04-25 09:51:30 -04006323 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6324 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006325 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006326 */
James Smart2e0fef82007-06-17 19:56:36 -05006327 spin_lock_irq(&phba->pport->work_port_lock);
6328 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6329 spin_unlock_irq(&phba->pport->work_port_lock);
6330 spin_lock_irq(&phba->hbalock);
6331 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006332 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006333 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006334
6335 pring = &psli->ring[psli->fcp_ring];
6336 lpfc_sli_abort_iocb_ring(phba, pring);
6337
6338 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006339 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006340
6341 /* Reset the HBA device */
6342 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006343}
6344
James Smarte59058c2008-08-24 21:49:00 -04006345/**
James Smart3772a992009-05-22 14:50:54 -04006346 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006347 * @phba: Pointer to HBA context object.
6348 * @pmbox: Pointer to mailbox object.
6349 * @flag: Flag indicating how the mailbox need to be processed.
6350 *
6351 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006352 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6353 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006354 * The mailbox command can be submitted in polling mode, in which case
6355 * this function will wait in a polling loop for the completion of the
6356 * mailbox.
6357 * If the mailbox is submitted in no_wait mode (not polling) the
6358 * function will submit the command and returns immediately without waiting
6359 * for the mailbox completion. The no_wait is supported only when HBA
6360 * is in SLI2/SLI3 mode - interrupts are enabled.
6361 * The SLI interface allows only one mailbox pending at a time. If the
6362 * mailbox is issued in polling mode and there is already a mailbox
6363 * pending, then the function will return an error. If the mailbox is issued
6364 * in NO_WAIT mode and there is a mailbox pending already, the function
6365 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6366 * The sli layer owns the mailbox object until the completion of mailbox
6367 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6368 * return codes the caller owns the mailbox command after the return of
6369 * the function.
6370 **/
James Smart3772a992009-05-22 14:50:54 -04006371static int
6372lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6373 uint32_t flag)
dea31012005-04-17 16:05:31 -05006374{
dea31012005-04-17 16:05:31 -05006375 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006376 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006377 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006378 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006379 int i;
James Smart09372822008-01-11 01:52:54 -05006380 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006381 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006382 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006383 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006384 int processing_queue = 0;
6385
6386 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6387 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006388 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006389 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006390 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6391 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6392 return MBX_SUCCESS;
6393 }
James Smart58da1ff2008-04-07 10:15:56 -04006394 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006395 pmbox = lpfc_mbox_get(phba);
6396 if (!pmbox) {
6397 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6398 return MBX_SUCCESS;
6399 }
6400 }
dea31012005-04-17 16:05:31 -05006401
James Smarted957682007-06-17 19:56:37 -05006402 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006403 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006404 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006405 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006406 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006407 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006408 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006409 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006410 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006411 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006412 }
6413 }
6414
Linas Vepstas8d63f372007-02-14 14:28:36 -06006415 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006416 if (unlikely(pci_channel_offline(phba->pcidev))) {
6417 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6418 goto out_not_finished;
6419 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006420
James Smarta257bf92009-04-06 18:48:10 -04006421 /* If HBA has a deferred error attention, fail the iocb. */
6422 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6423 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6424 goto out_not_finished;
6425 }
6426
dea31012005-04-17 16:05:31 -05006427 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006428
James Smart3772a992009-05-22 14:50:54 -04006429 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006430 status = MBX_SUCCESS;
6431
James Smart2e0fef82007-06-17 19:56:36 -05006432 if (phba->link_state == LPFC_HBA_ERROR) {
6433 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006434
6435 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006436 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6437 "(%d):0311 Mailbox command x%x cannot "
6438 "issue Data: x%x x%x\n",
6439 pmbox->vport ? pmbox->vport->vpi : 0,
6440 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006441 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006442 }
6443
James Smart9940b972011-03-11 16:06:12 -05006444 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6445 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6446 !(hc_copy & HC_MBINT_ENA)) {
6447 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6448 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006449 "(%d):2528 Mailbox command x%x cannot "
6450 "issue Data: x%x x%x\n",
6451 pmbox->vport ? pmbox->vport->vpi : 0,
6452 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006453 goto out_not_finished;
6454 }
James Smart92908312006-03-07 15:04:13 -05006455 }
6456
dea31012005-04-17 16:05:31 -05006457 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6458 /* Polling for a mbox command when another one is already active
6459 * is not allowed in SLI. Also, the driver must have established
6460 * SLI2 mode to queue and process multiple mbox commands.
6461 */
6462
6463 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006464 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006465
6466 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006467 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6468 "(%d):2529 Mailbox command x%x "
6469 "cannot issue Data: x%x x%x\n",
6470 pmbox->vport ? pmbox->vport->vpi : 0,
6471 pmbox->u.mb.mbxCommand,
6472 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006473 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006474 }
6475
James Smart3772a992009-05-22 14:50:54 -04006476 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006477 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006478 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006479 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6480 "(%d):2530 Mailbox command x%x "
6481 "cannot issue Data: x%x x%x\n",
6482 pmbox->vport ? pmbox->vport->vpi : 0,
6483 pmbox->u.mb.mbxCommand,
6484 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006485 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006486 }
6487
dea31012005-04-17 16:05:31 -05006488 /* Another mailbox command is still being processed, queue this
6489 * command to be processed later.
6490 */
6491 lpfc_mbox_put(phba, pmbox);
6492
6493 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006494 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006495 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006496 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006497 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6498 mb->mbxCommand, phba->pport->port_state,
6499 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006500
6501 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006502 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006503
James Smart858c9f62007-06-17 19:56:39 -05006504 if (pmbox->vport) {
6505 lpfc_debugfs_disc_trc(pmbox->vport,
6506 LPFC_DISC_TRC_MBOX_VPORT,
6507 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6508 (uint32_t)mb->mbxCommand,
6509 mb->un.varWords[0], mb->un.varWords[1]);
6510 }
6511 else {
6512 lpfc_debugfs_disc_trc(phba->pport,
6513 LPFC_DISC_TRC_MBOX,
6514 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6515 (uint32_t)mb->mbxCommand,
6516 mb->un.varWords[0], mb->un.varWords[1]);
6517 }
6518
James Smart2e0fef82007-06-17 19:56:36 -05006519 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006520 }
6521
dea31012005-04-17 16:05:31 -05006522 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6523
6524 /* If we are not polling, we MUST be in SLI2 mode */
6525 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006526 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006527 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006528 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006529 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006530 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006531 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6532 "(%d):2531 Mailbox command x%x "
6533 "cannot issue Data: x%x x%x\n",
6534 pmbox->vport ? pmbox->vport->vpi : 0,
6535 pmbox->u.mb.mbxCommand,
6536 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006537 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006538 }
6539 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006540 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006541 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006542 }
6543
6544 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006545 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006546 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006547 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006548 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006549 mb->mbxCommand, phba->pport->port_state,
6550 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006551
James Smart858c9f62007-06-17 19:56:39 -05006552 if (mb->mbxCommand != MBX_HEARTBEAT) {
6553 if (pmbox->vport) {
6554 lpfc_debugfs_disc_trc(pmbox->vport,
6555 LPFC_DISC_TRC_MBOX_VPORT,
6556 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6557 (uint32_t)mb->mbxCommand,
6558 mb->un.varWords[0], mb->un.varWords[1]);
6559 }
6560 else {
6561 lpfc_debugfs_disc_trc(phba->pport,
6562 LPFC_DISC_TRC_MBOX,
6563 "MBOX Send: cmd:x%x mb:x%x x%x",
6564 (uint32_t)mb->mbxCommand,
6565 mb->un.varWords[0], mb->un.varWords[1]);
6566 }
6567 }
6568
dea31012005-04-17 16:05:31 -05006569 psli->slistat.mbox_cmd++;
6570 evtctr = psli->slistat.mbox_event;
6571
6572 /* next set own bit for the adapter and copy over command word */
6573 mb->mbxOwner = OWN_CHIP;
6574
James Smart3772a992009-05-22 14:50:54 -04006575 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006576 /* Populate mbox extension offset word. */
6577 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6578 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6579 = (uint8_t *)phba->mbox_ext
6580 - (uint8_t *)phba->mbox;
6581 }
6582
6583 /* Copy the mailbox extension data */
6584 if (pmbox->in_ext_byte_len && pmbox->context2) {
6585 lpfc_sli_pcimem_bcopy(pmbox->context2,
6586 (uint8_t *)phba->mbox_ext,
6587 pmbox->in_ext_byte_len);
6588 }
6589 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006590 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006591 } else {
James Smart7a470272010-03-15 11:25:20 -04006592 /* Populate mbox extension offset word. */
6593 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6594 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6595 = MAILBOX_HBA_EXT_OFFSET;
6596
6597 /* Copy the mailbox extension data */
6598 if (pmbox->in_ext_byte_len && pmbox->context2) {
6599 lpfc_memcpy_to_slim(phba->MBslimaddr +
6600 MAILBOX_HBA_EXT_OFFSET,
6601 pmbox->context2, pmbox->in_ext_byte_len);
6602
6603 }
James Smart92908312006-03-07 15:04:13 -05006604 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006605 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006606 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006607 }
6608
6609 /* First copy mbox command data to HBA SLIM, skip past first
6610 word */
6611 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6612 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6613 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6614
6615 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006616 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006617 to_slim = phba->MBslimaddr;
6618 writel(ldata, to_slim);
6619 readl(to_slim); /* flush */
6620
6621 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6622 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006623 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006624 }
6625 }
6626
6627 wmb();
dea31012005-04-17 16:05:31 -05006628
6629 switch (flag) {
6630 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006631 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006632 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006633 /* Interrupt board to do it */
6634 writel(CA_MBATT, phba->CAregaddr);
6635 readl(phba->CAregaddr); /* flush */
6636 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006637 break;
6638
6639 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006640 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006641 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006642 /* Interrupt board to do it */
6643 writel(CA_MBATT, phba->CAregaddr);
6644 readl(phba->CAregaddr); /* flush */
6645
James Smart3772a992009-05-22 14:50:54 -04006646 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006647 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006648 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006649 word0 = le32_to_cpu(word0);
6650 } else {
6651 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006652 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6653 spin_unlock_irqrestore(&phba->hbalock,
6654 drvr_flag);
6655 goto out_not_finished;
6656 }
dea31012005-04-17 16:05:31 -05006657 }
6658
6659 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006660 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6661 spin_unlock_irqrestore(&phba->hbalock,
6662 drvr_flag);
6663 goto out_not_finished;
6664 }
James Smarta183a152011-10-10 21:32:43 -04006665 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6666 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006667 i = 0;
dea31012005-04-17 16:05:31 -05006668 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006669 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6670 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006671 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006672 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006673 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006674 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006675 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006676 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006677 }
6678
6679 /* Check if we took a mbox interrupt while we were
6680 polling */
6681 if (((word0 & OWN_CHIP) != OWN_CHIP)
6682 && (evtctr != psli->slistat.mbox_event))
6683 break;
6684
James Smart09372822008-01-11 01:52:54 -05006685 if (i++ > 10) {
6686 spin_unlock_irqrestore(&phba->hbalock,
6687 drvr_flag);
6688 msleep(1);
6689 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6690 }
dea31012005-04-17 16:05:31 -05006691
James Smart3772a992009-05-22 14:50:54 -04006692 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006693 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006694 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006695 word0 = le32_to_cpu(word0);
6696 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6697 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006698 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006699 /* Check real SLIM for any errors */
6700 slimword0 = readl(phba->MBslimaddr);
6701 slimmb = (MAILBOX_t *) & slimword0;
6702 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6703 && slimmb->mbxStatus) {
6704 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006705 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006706 word0 = slimword0;
6707 }
6708 }
6709 } else {
6710 /* First copy command data */
6711 word0 = readl(phba->MBslimaddr);
6712 }
6713 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006714 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6715 spin_unlock_irqrestore(&phba->hbalock,
6716 drvr_flag);
6717 goto out_not_finished;
6718 }
dea31012005-04-17 16:05:31 -05006719 }
6720
James Smart3772a992009-05-22 14:50:54 -04006721 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006722 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006723 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006724 /* Copy the mailbox extension data */
6725 if (pmbox->out_ext_byte_len && pmbox->context2) {
6726 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6727 pmbox->context2,
6728 pmbox->out_ext_byte_len);
6729 }
dea31012005-04-17 16:05:31 -05006730 } else {
6731 /* First copy command data */
6732 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6733 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006734 /* Copy the mailbox extension data */
6735 if (pmbox->out_ext_byte_len && pmbox->context2) {
6736 lpfc_memcpy_from_slim(pmbox->context2,
6737 phba->MBslimaddr +
6738 MAILBOX_HBA_EXT_OFFSET,
6739 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006740 }
6741 }
6742
6743 writel(HA_MBATT, phba->HAregaddr);
6744 readl(phba->HAregaddr); /* flush */
6745
6746 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6747 status = mb->mbxStatus;
6748 }
6749
James Smart2e0fef82007-06-17 19:56:36 -05006750 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6751 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006752
6753out_not_finished:
6754 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006755 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006756 lpfc_mbox_cmpl_put(phba, pmbox);
6757 }
6758 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006759}
6760
James Smarte59058c2008-08-24 21:49:00 -04006761/**
James Smartf1126682009-06-10 17:22:44 -04006762 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6763 * @phba: Pointer to HBA context object.
6764 *
6765 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6766 * the driver internal pending mailbox queue. It will then try to wait out the
6767 * possible outstanding mailbox command before return.
6768 *
6769 * Returns:
6770 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6771 * the outstanding mailbox command timed out.
6772 **/
6773static int
6774lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6775{
6776 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006777 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006778 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006779
6780 /* Mark the asynchronous mailbox command posting as blocked */
6781 spin_lock_irq(&phba->hbalock);
6782 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006783 /* Determine how long we might wait for the active mailbox
6784 * command to be gracefully completed by firmware.
6785 */
James Smarta183a152011-10-10 21:32:43 -04006786 if (phba->sli.mbox_active)
6787 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6788 phba->sli.mbox_active) *
6789 1000) + jiffies;
6790 spin_unlock_irq(&phba->hbalock);
6791
James Smartf1126682009-06-10 17:22:44 -04006792 /* Wait for the outstnading mailbox command to complete */
6793 while (phba->sli.mbox_active) {
6794 /* Check active mailbox complete status every 2ms */
6795 msleep(2);
6796 if (time_after(jiffies, timeout)) {
6797 /* Timeout, marked the outstanding cmd not complete */
6798 rc = 1;
6799 break;
6800 }
6801 }
6802
6803 /* Can not cleanly block async mailbox command, fails it */
6804 if (rc) {
6805 spin_lock_irq(&phba->hbalock);
6806 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6807 spin_unlock_irq(&phba->hbalock);
6808 }
6809 return rc;
6810}
6811
6812/**
6813 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6814 * @phba: Pointer to HBA context object.
6815 *
6816 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6817 * commands from the driver internal pending mailbox queue. It makes sure
6818 * that there is no outstanding mailbox command before resuming posting
6819 * asynchronous mailbox commands. If, for any reason, there is outstanding
6820 * mailbox command, it will try to wait it out before resuming asynchronous
6821 * mailbox command posting.
6822 **/
6823static void
6824lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6825{
6826 struct lpfc_sli *psli = &phba->sli;
6827
6828 spin_lock_irq(&phba->hbalock);
6829 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6830 /* Asynchronous mailbox posting is not blocked, do nothing */
6831 spin_unlock_irq(&phba->hbalock);
6832 return;
6833 }
6834
6835 /* Outstanding synchronous mailbox command is guaranteed to be done,
6836 * successful or timeout, after timing-out the outstanding mailbox
6837 * command shall always be removed, so just unblock posting async
6838 * mailbox command and resume
6839 */
6840 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6841 spin_unlock_irq(&phba->hbalock);
6842
6843 /* wake up worker thread to post asynchronlous mailbox command */
6844 lpfc_worker_wake_up(phba);
6845}
6846
6847/**
James Smartda0436e2009-05-22 14:51:39 -04006848 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6849 * @phba: Pointer to HBA context object.
6850 * @mboxq: Pointer to mailbox object.
6851 *
6852 * The function posts a mailbox to the port. The mailbox is expected
6853 * to be comletely filled in and ready for the port to operate on it.
6854 * This routine executes a synchronous completion operation on the
6855 * mailbox by polling for its completion.
6856 *
6857 * The caller must not be holding any locks when calling this routine.
6858 *
6859 * Returns:
6860 * MBX_SUCCESS - mailbox posted successfully
6861 * Any of the MBX error values.
6862 **/
6863static int
6864lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6865{
6866 int rc = MBX_SUCCESS;
6867 unsigned long iflag;
6868 uint32_t db_ready;
6869 uint32_t mcqe_status;
6870 uint32_t mbx_cmnd;
6871 unsigned long timeout;
6872 struct lpfc_sli *psli = &phba->sli;
6873 struct lpfc_mqe *mb = &mboxq->u.mqe;
6874 struct lpfc_bmbx_create *mbox_rgn;
6875 struct dma_address *dma_address;
6876 struct lpfc_register bmbx_reg;
6877
6878 /*
6879 * Only one mailbox can be active to the bootstrap mailbox region
6880 * at a time and there is no queueing provided.
6881 */
6882 spin_lock_irqsave(&phba->hbalock, iflag);
6883 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6884 spin_unlock_irqrestore(&phba->hbalock, iflag);
6885 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006886 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006887 "cannot issue Data: x%x x%x\n",
6888 mboxq->vport ? mboxq->vport->vpi : 0,
6889 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006890 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6891 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006892 psli->sli_flag, MBX_POLL);
6893 return MBXERR_ERROR;
6894 }
6895 /* The server grabs the token and owns it until release */
6896 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6897 phba->sli.mbox_active = mboxq;
6898 spin_unlock_irqrestore(&phba->hbalock, iflag);
6899
6900 /*
6901 * Initialize the bootstrap memory region to avoid stale data areas
6902 * in the mailbox post. Then copy the caller's mailbox contents to
6903 * the bmbx mailbox region.
6904 */
6905 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6906 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6907 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6908 sizeof(struct lpfc_mqe));
6909
6910 /* Post the high mailbox dma address to the port and wait for ready. */
6911 dma_address = &phba->sli4_hba.bmbx.dma_address;
6912 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6913
James Smarta183a152011-10-10 21:32:43 -04006914 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006915 * 1000) + jiffies;
6916 do {
6917 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6918 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6919 if (!db_ready)
6920 msleep(2);
6921
6922 if (time_after(jiffies, timeout)) {
6923 rc = MBXERR_ERROR;
6924 goto exit;
6925 }
6926 } while (!db_ready);
6927
6928 /* Post the low mailbox dma address to the port. */
6929 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04006930 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006931 * 1000) + jiffies;
6932 do {
6933 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6934 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6935 if (!db_ready)
6936 msleep(2);
6937
6938 if (time_after(jiffies, timeout)) {
6939 rc = MBXERR_ERROR;
6940 goto exit;
6941 }
6942 } while (!db_ready);
6943
6944 /*
6945 * Read the CQ to ensure the mailbox has completed.
6946 * If so, update the mailbox status so that the upper layers
6947 * can complete the request normally.
6948 */
6949 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6950 sizeof(struct lpfc_mqe));
6951 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6952 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6953 sizeof(struct lpfc_mcqe));
6954 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006955 /*
6956 * When the CQE status indicates a failure and the mailbox status
6957 * indicates success then copy the CQE status into the mailbox status
6958 * (and prefix it with x4000).
6959 */
James Smartda0436e2009-05-22 14:51:39 -04006960 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006961 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6962 bf_set(lpfc_mqe_status, mb,
6963 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04006964 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04006965 } else
6966 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04006967
6968 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006969 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04006970 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6971 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04006972 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6973 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6974 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006975 bf_get(lpfc_mqe_status, mb),
6976 mb->un.mb_words[0], mb->un.mb_words[1],
6977 mb->un.mb_words[2], mb->un.mb_words[3],
6978 mb->un.mb_words[4], mb->un.mb_words[5],
6979 mb->un.mb_words[6], mb->un.mb_words[7],
6980 mb->un.mb_words[8], mb->un.mb_words[9],
6981 mb->un.mb_words[10], mb->un.mb_words[11],
6982 mb->un.mb_words[12], mboxq->mcqe.word0,
6983 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6984 mboxq->mcqe.trailer);
6985exit:
6986 /* We are holding the token, no needed for lock when release */
6987 spin_lock_irqsave(&phba->hbalock, iflag);
6988 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6989 phba->sli.mbox_active = NULL;
6990 spin_unlock_irqrestore(&phba->hbalock, iflag);
6991 return rc;
6992}
6993
6994/**
6995 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6996 * @phba: Pointer to HBA context object.
6997 * @pmbox: Pointer to mailbox object.
6998 * @flag: Flag indicating how the mailbox need to be processed.
6999 *
7000 * This function is called by discovery code and HBA management code to submit
7001 * a mailbox command to firmware with SLI-4 interface spec.
7002 *
7003 * Return codes the caller owns the mailbox command after the return of the
7004 * function.
7005 **/
7006static int
7007lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7008 uint32_t flag)
7009{
7010 struct lpfc_sli *psli = &phba->sli;
7011 unsigned long iflags;
7012 int rc;
7013
James Smartb76f2dc2011-07-22 18:37:42 -04007014 /* dump from issue mailbox command if setup */
7015 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7016
James Smart8fa38512009-07-19 10:01:03 -04007017 rc = lpfc_mbox_dev_check(phba);
7018 if (unlikely(rc)) {
7019 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007020 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007021 "cannot issue Data: x%x x%x\n",
7022 mboxq->vport ? mboxq->vport->vpi : 0,
7023 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007024 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7025 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007026 psli->sli_flag, flag);
7027 goto out_not_finished;
7028 }
7029
James Smartda0436e2009-05-22 14:51:39 -04007030 /* Detect polling mode and jump to a handler */
7031 if (!phba->sli4_hba.intr_enable) {
7032 if (flag == MBX_POLL)
7033 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7034 else
7035 rc = -EIO;
7036 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007037 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007038 "(%d):2541 Mailbox command x%x "
James Smarta183a152011-10-10 21:32:43 -04007039 "(x%x/x%x) cannot issue Data: "
7040 "x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007041 mboxq->vport ? mboxq->vport->vpi : 0,
7042 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007043 lpfc_sli_config_mbox_subsys_get(phba,
7044 mboxq),
7045 lpfc_sli_config_mbox_opcode_get(phba,
7046 mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007047 psli->sli_flag, flag);
7048 return rc;
7049 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007050 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7051 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007052 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007053 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007054 mboxq->vport ? mboxq->vport->vpi : 0,
7055 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007056 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7057 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007058 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007059 /* Try to block the asynchronous mailbox posting */
7060 rc = lpfc_sli4_async_mbox_block(phba);
7061 if (!rc) {
7062 /* Successfully blocked, now issue sync mbox cmd */
7063 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7064 if (rc != MBX_SUCCESS)
7065 lpfc_printf_log(phba, KERN_ERR,
James Smarta183a152011-10-10 21:32:43 -04007066 LOG_MBOX | LOG_SLI,
7067 "(%d):2597 Mailbox command "
7068 "x%x (x%x/x%x) cannot issue "
7069 "Data: x%x x%x\n",
7070 mboxq->vport ?
7071 mboxq->vport->vpi : 0,
7072 mboxq->u.mb.mbxCommand,
7073 lpfc_sli_config_mbox_subsys_get(phba,
7074 mboxq),
7075 lpfc_sli_config_mbox_opcode_get(phba,
7076 mboxq),
7077 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007078 /* Unblock the async mailbox posting afterward */
7079 lpfc_sli4_async_mbox_unblock(phba);
7080 }
7081 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007082 }
7083
7084 /* Now, interrupt mode asynchrous mailbox command */
7085 rc = lpfc_mbox_cmd_check(phba, mboxq);
7086 if (rc) {
7087 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007088 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007089 "cannot issue Data: x%x x%x\n",
7090 mboxq->vport ? mboxq->vport->vpi : 0,
7091 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007092 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7093 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007094 psli->sli_flag, flag);
7095 goto out_not_finished;
7096 }
James Smartda0436e2009-05-22 14:51:39 -04007097
7098 /* Put the mailbox command to the driver internal FIFO */
7099 psli->slistat.mbox_busy++;
7100 spin_lock_irqsave(&phba->hbalock, iflags);
7101 lpfc_mbox_put(phba, mboxq);
7102 spin_unlock_irqrestore(&phba->hbalock, iflags);
7103 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7104 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007105 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007106 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7107 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007108 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7109 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007110 phba->pport->port_state,
7111 psli->sli_flag, MBX_NOWAIT);
7112 /* Wake up worker thread to transport mailbox command from head */
7113 lpfc_worker_wake_up(phba);
7114
7115 return MBX_BUSY;
7116
7117out_not_finished:
7118 return MBX_NOT_FINISHED;
7119}
7120
7121/**
7122 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7123 * @phba: Pointer to HBA context object.
7124 *
7125 * This function is called by worker thread to send a mailbox command to
7126 * SLI4 HBA firmware.
7127 *
7128 **/
7129int
7130lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7131{
7132 struct lpfc_sli *psli = &phba->sli;
7133 LPFC_MBOXQ_t *mboxq;
7134 int rc = MBX_SUCCESS;
7135 unsigned long iflags;
7136 struct lpfc_mqe *mqe;
7137 uint32_t mbx_cmnd;
7138
7139 /* Check interrupt mode before post async mailbox command */
7140 if (unlikely(!phba->sli4_hba.intr_enable))
7141 return MBX_NOT_FINISHED;
7142
7143 /* Check for mailbox command service token */
7144 spin_lock_irqsave(&phba->hbalock, iflags);
7145 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7146 spin_unlock_irqrestore(&phba->hbalock, iflags);
7147 return MBX_NOT_FINISHED;
7148 }
7149 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7150 spin_unlock_irqrestore(&phba->hbalock, iflags);
7151 return MBX_NOT_FINISHED;
7152 }
7153 if (unlikely(phba->sli.mbox_active)) {
7154 spin_unlock_irqrestore(&phba->hbalock, iflags);
7155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7156 "0384 There is pending active mailbox cmd\n");
7157 return MBX_NOT_FINISHED;
7158 }
7159 /* Take the mailbox command service token */
7160 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7161
7162 /* Get the next mailbox command from head of queue */
7163 mboxq = lpfc_mbox_get(phba);
7164
7165 /* If no more mailbox command waiting for post, we're done */
7166 if (!mboxq) {
7167 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7168 spin_unlock_irqrestore(&phba->hbalock, iflags);
7169 return MBX_SUCCESS;
7170 }
7171 phba->sli.mbox_active = mboxq;
7172 spin_unlock_irqrestore(&phba->hbalock, iflags);
7173
7174 /* Check device readiness for posting mailbox command */
7175 rc = lpfc_mbox_dev_check(phba);
7176 if (unlikely(rc))
7177 /* Driver clean routine will clean up pending mailbox */
7178 goto out_not_finished;
7179
7180 /* Prepare the mbox command to be posted */
7181 mqe = &mboxq->u.mqe;
7182 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7183
7184 /* Start timer for the mbox_tmo and log some mailbox post messages */
7185 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007186 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007187
7188 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007189 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007190 "x%x x%x\n",
7191 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007192 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7193 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007194 phba->pport->port_state, psli->sli_flag);
7195
7196 if (mbx_cmnd != MBX_HEARTBEAT) {
7197 if (mboxq->vport) {
7198 lpfc_debugfs_disc_trc(mboxq->vport,
7199 LPFC_DISC_TRC_MBOX_VPORT,
7200 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7201 mbx_cmnd, mqe->un.mb_words[0],
7202 mqe->un.mb_words[1]);
7203 } else {
7204 lpfc_debugfs_disc_trc(phba->pport,
7205 LPFC_DISC_TRC_MBOX,
7206 "MBOX Send: cmd:x%x mb:x%x x%x",
7207 mbx_cmnd, mqe->un.mb_words[0],
7208 mqe->un.mb_words[1]);
7209 }
7210 }
7211 psli->slistat.mbox_cmd++;
7212
7213 /* Post the mailbox command to the port */
7214 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7215 if (rc != MBX_SUCCESS) {
7216 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007217 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007218 "cannot issue Data: x%x x%x\n",
7219 mboxq->vport ? mboxq->vport->vpi : 0,
7220 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007221 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7222 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007223 psli->sli_flag, MBX_NOWAIT);
7224 goto out_not_finished;
7225 }
7226
7227 return rc;
7228
7229out_not_finished:
7230 spin_lock_irqsave(&phba->hbalock, iflags);
7231 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7232 __lpfc_mbox_cmpl_put(phba, mboxq);
7233 /* Release the token */
7234 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7235 phba->sli.mbox_active = NULL;
7236 spin_unlock_irqrestore(&phba->hbalock, iflags);
7237
7238 return MBX_NOT_FINISHED;
7239}
7240
7241/**
7242 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7243 * @phba: Pointer to HBA context object.
7244 * @pmbox: Pointer to mailbox object.
7245 * @flag: Flag indicating how the mailbox need to be processed.
7246 *
7247 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7248 * the API jump table function pointer from the lpfc_hba struct.
7249 *
7250 * Return codes the caller owns the mailbox command after the return of the
7251 * function.
7252 **/
7253int
7254lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7255{
7256 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7257}
7258
7259/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007260 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007261 * @phba: The hba struct for which this call is being executed.
7262 * @dev_grp: The HBA PCI-Device group number.
7263 *
7264 * This routine sets up the mbox interface API function jump table in @phba
7265 * struct.
7266 * Returns: 0 - success, -ENODEV - failure.
7267 **/
7268int
7269lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7270{
7271
7272 switch (dev_grp) {
7273 case LPFC_PCI_DEV_LP:
7274 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7275 phba->lpfc_sli_handle_slow_ring_event =
7276 lpfc_sli_handle_slow_ring_event_s3;
7277 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7278 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7279 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7280 break;
7281 case LPFC_PCI_DEV_OC:
7282 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7283 phba->lpfc_sli_handle_slow_ring_event =
7284 lpfc_sli_handle_slow_ring_event_s4;
7285 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7286 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7287 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7288 break;
7289 default:
7290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7291 "1420 Invalid HBA PCI-device group: 0x%x\n",
7292 dev_grp);
7293 return -ENODEV;
7294 break;
7295 }
7296 return 0;
7297}
7298
7299/**
James Smart3621a712009-04-06 18:47:14 -04007300 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007301 * @phba: Pointer to HBA context object.
7302 * @pring: Pointer to driver SLI ring object.
7303 * @piocb: Pointer to address of newly added command iocb.
7304 *
7305 * This function is called with hbalock held to add a command
7306 * iocb to the txq when SLI layer cannot submit the command iocb
7307 * to the ring.
7308 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007309void
James Smart92d7f7b2007-06-17 19:56:38 -05007310__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007311 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007312{
7313 /* Insert the caller's iocb in the txq tail for later processing. */
7314 list_add_tail(&piocb->list, &pring->txq);
7315 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007316}
7317
James Smarte59058c2008-08-24 21:49:00 -04007318/**
James Smart3621a712009-04-06 18:47:14 -04007319 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007320 * @phba: Pointer to HBA context object.
7321 * @pring: Pointer to driver SLI ring object.
7322 * @piocb: Pointer to address of newly added command iocb.
7323 *
7324 * This function is called with hbalock held before a new
7325 * iocb is submitted to the firmware. This function checks
7326 * txq to flush the iocbs in txq to Firmware before
7327 * submitting new iocbs to the Firmware.
7328 * If there are iocbs in the txq which need to be submitted
7329 * to firmware, lpfc_sli_next_iocb returns the first element
7330 * of the txq after dequeuing it from txq.
7331 * If there is no iocb in the txq then the function will return
7332 * *piocb and *piocb is set to NULL. Caller needs to check
7333 * *piocb to find if there are more commands in the txq.
7334 **/
dea31012005-04-17 16:05:31 -05007335static struct lpfc_iocbq *
7336lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007337 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007338{
7339 struct lpfc_iocbq * nextiocb;
7340
7341 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7342 if (!nextiocb) {
7343 nextiocb = *piocb;
7344 *piocb = NULL;
7345 }
7346
7347 return nextiocb;
7348}
7349
James Smarte59058c2008-08-24 21:49:00 -04007350/**
James Smart3772a992009-05-22 14:50:54 -04007351 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007352 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007353 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007354 * @piocb: Pointer to command iocb.
7355 * @flag: Flag indicating if this command can be put into txq.
7356 *
James Smart3772a992009-05-22 14:50:54 -04007357 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7358 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7359 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7360 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7361 * this function allows only iocbs for posting buffers. This function finds
7362 * next available slot in the command ring and posts the command to the
7363 * available slot and writes the port attention register to request HBA start
7364 * processing new iocb. If there is no slot available in the ring and
7365 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7366 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007367 *
James Smart3772a992009-05-22 14:50:54 -04007368 * This function is called with hbalock held. The function will return success
7369 * after it successfully submit the iocb to firmware or after adding to the
7370 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007371 **/
James Smart98c9ea52007-10-27 13:37:33 -04007372static int
James Smart3772a992009-05-22 14:50:54 -04007373__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007374 struct lpfc_iocbq *piocb, uint32_t flag)
7375{
7376 struct lpfc_iocbq *nextiocb;
7377 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007378 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007379
James Smart92d7f7b2007-06-17 19:56:38 -05007380 if (piocb->iocb_cmpl && (!piocb->vport) &&
7381 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7382 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7383 lpfc_printf_log(phba, KERN_ERR,
7384 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007385 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007386 piocb->iocb.ulpCommand);
7387 dump_stack();
7388 return IOCB_ERROR;
7389 }
7390
7391
Linas Vepstas8d63f372007-02-14 14:28:36 -06007392 /* If the PCI channel is in offline state, do not post iocbs. */
7393 if (unlikely(pci_channel_offline(phba->pcidev)))
7394 return IOCB_ERROR;
7395
James Smarta257bf92009-04-06 18:48:10 -04007396 /* If HBA has a deferred error attention, fail the iocb. */
7397 if (unlikely(phba->hba_flag & DEFER_ERATT))
7398 return IOCB_ERROR;
7399
dea31012005-04-17 16:05:31 -05007400 /*
7401 * We should never get an IOCB if we are in a < LINK_DOWN state
7402 */
James Smart2e0fef82007-06-17 19:56:36 -05007403 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007404 return IOCB_ERROR;
7405
7406 /*
7407 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007408 * outstanding event.
dea31012005-04-17 16:05:31 -05007409 */
James Smart0b727fe2007-10-27 13:37:25 -04007410 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007411 goto iocb_busy;
7412
James Smart2e0fef82007-06-17 19:56:36 -05007413 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007414 /*
James Smart2680eea2007-04-25 09:52:55 -04007415 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007416 * can be issued if the link is not up.
7417 */
7418 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007419 case CMD_GEN_REQUEST64_CR:
7420 case CMD_GEN_REQUEST64_CX:
7421 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7422 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007423 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007424 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7425 MENLO_TRANSPORT_TYPE))
7426
7427 goto iocb_busy;
7428 break;
dea31012005-04-17 16:05:31 -05007429 case CMD_QUE_RING_BUF_CN:
7430 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007431 /*
7432 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7433 * completion, iocb_cmpl MUST be 0.
7434 */
7435 if (piocb->iocb_cmpl)
7436 piocb->iocb_cmpl = NULL;
7437 /*FALLTHROUGH*/
7438 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007439 case CMD_CLOSE_XRI_CN:
7440 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007441 break;
7442 default:
7443 goto iocb_busy;
7444 }
7445
7446 /*
7447 * For FCP commands, we must be in a state where we can process link
7448 * attention events.
7449 */
7450 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007451 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007452 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007453 }
dea31012005-04-17 16:05:31 -05007454
dea31012005-04-17 16:05:31 -05007455 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7456 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7457 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7458
7459 if (iocb)
7460 lpfc_sli_update_ring(phba, pring);
7461 else
7462 lpfc_sli_update_full_ring(phba, pring);
7463
7464 if (!piocb)
7465 return IOCB_SUCCESS;
7466
7467 goto out_busy;
7468
7469 iocb_busy:
7470 pring->stats.iocb_cmd_delay++;
7471
7472 out_busy:
7473
7474 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007475 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007476 return IOCB_SUCCESS;
7477 }
7478
7479 return IOCB_BUSY;
7480}
7481
James Smart3772a992009-05-22 14:50:54 -04007482/**
James Smart4f774512009-05-22 14:52:35 -04007483 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7484 * @phba: Pointer to HBA context object.
7485 * @piocb: Pointer to command iocb.
7486 * @sglq: Pointer to the scatter gather queue object.
7487 *
7488 * This routine converts the bpl or bde that is in the IOCB
7489 * to a sgl list for the sli4 hardware. The physical address
7490 * of the bpl/bde is converted back to a virtual address.
7491 * If the IOCB contains a BPL then the list of BDE's is
7492 * converted to sli4_sge's. If the IOCB contains a single
7493 * BDE then it is converted to a single sli_sge.
7494 * The IOCB is still in cpu endianess so the contents of
7495 * the bpl can be used without byte swapping.
7496 *
7497 * Returns valid XRI = Success, NO_XRI = Failure.
7498**/
7499static uint16_t
7500lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7501 struct lpfc_sglq *sglq)
7502{
7503 uint16_t xritag = NO_XRI;
7504 struct ulp_bde64 *bpl = NULL;
7505 struct ulp_bde64 bde;
7506 struct sli4_sge *sgl = NULL;
7507 IOCB_t *icmd;
7508 int numBdes = 0;
7509 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007510 uint32_t offset = 0; /* accumulated offset in the sg request list */
7511 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007512
7513 if (!piocbq || !sglq)
7514 return xritag;
7515
7516 sgl = (struct sli4_sge *)sglq->sgl;
7517 icmd = &piocbq->iocb;
7518 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7519 numBdes = icmd->un.genreq64.bdl.bdeSize /
7520 sizeof(struct ulp_bde64);
7521 /* The addrHigh and addrLow fields within the IOCB
7522 * have not been byteswapped yet so there is no
7523 * need to swap them back.
7524 */
7525 bpl = (struct ulp_bde64 *)
7526 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7527
7528 if (!bpl)
7529 return xritag;
7530
7531 for (i = 0; i < numBdes; i++) {
7532 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007533 sgl->addr_hi = bpl->addrHigh;
7534 sgl->addr_lo = bpl->addrLow;
7535
James Smart05580562011-05-24 11:40:48 -04007536 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007537 if ((i+1) == numBdes)
7538 bf_set(lpfc_sli4_sge_last, sgl, 1);
7539 else
7540 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007541 /* swap the size field back to the cpu so we
7542 * can assign it to the sgl.
7543 */
7544 bde.tus.w = le32_to_cpu(bpl->tus.w);
7545 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007546 /* The offsets in the sgl need to be accumulated
7547 * separately for the request and reply lists.
7548 * The request is always first, the reply follows.
7549 */
7550 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7551 /* add up the reply sg entries */
7552 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7553 inbound++;
7554 /* first inbound? reset the offset */
7555 if (inbound == 1)
7556 offset = 0;
7557 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007558 bf_set(lpfc_sli4_sge_type, sgl,
7559 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007560 offset += bde.tus.f.bdeSize;
7561 }
James Smart546fc852011-03-11 16:06:29 -05007562 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007563 bpl++;
7564 sgl++;
7565 }
7566 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7567 /* The addrHigh and addrLow fields of the BDE have not
7568 * been byteswapped yet so they need to be swapped
7569 * before putting them in the sgl.
7570 */
7571 sgl->addr_hi =
7572 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7573 sgl->addr_lo =
7574 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007575 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007576 bf_set(lpfc_sli4_sge_last, sgl, 1);
7577 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007578 sgl->sge_len =
7579 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007580 }
7581 return sglq->sli4_xritag;
7582}
7583
7584/**
7585 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7586 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007587 *
James Smarta93ff372010-10-22 11:06:08 -04007588 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007589 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7590 * held.
James Smart4f774512009-05-22 14:52:35 -04007591 *
7592 * Return: index into SLI4 fast-path FCP queue index.
7593 **/
7594static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007595lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007596{
James Smart8fa38512009-07-19 10:01:03 -04007597 ++phba->fcp_qidx;
7598 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7599 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007600
James Smart8fa38512009-07-19 10:01:03 -04007601 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007602}
7603
7604/**
7605 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7606 * @phba: Pointer to HBA context object.
7607 * @piocb: Pointer to command iocb.
7608 * @wqe: Pointer to the work queue entry.
7609 *
7610 * This routine converts the iocb command to its Work Queue Entry
7611 * equivalent. The wqe pointer should not have any fields set when
7612 * this routine is called because it will memcpy over them.
7613 * This routine does not set the CQ_ID or the WQEC bits in the
7614 * wqe.
7615 *
7616 * Returns: 0 = Success, IOCB_ERROR = Failure.
7617 **/
7618static int
7619lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7620 union lpfc_wqe *wqe)
7621{
James Smart5ffc2662009-11-18 15:39:44 -05007622 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007623 uint8_t ct = 0;
7624 uint32_t fip;
7625 uint32_t abort_tag;
7626 uint8_t command_type = ELS_COMMAND_NON_FIP;
7627 uint8_t cmnd;
7628 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007629 uint16_t abrt_iotag;
7630 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007631 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007632 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007633 int numBdes, i;
7634 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007635 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007636 uint32_t *pcmd;
James Smart4f774512009-05-22 14:52:35 -04007637
James Smart45ed1192009-10-02 15:17:02 -04007638 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007639 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007640 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007641 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007642 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007643 command_type = ELS_COMMAND_FIP;
7644 else
7645 command_type = ELS_COMMAND_NON_FIP;
7646
James Smart4f774512009-05-22 14:52:35 -04007647 /* Some of the fields are in the right position already */
7648 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7649 abort_tag = (uint32_t) iocbq->iotag;
7650 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007651 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007652 /* words0-2 bpl convert bde */
7653 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007654 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7655 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007656 bpl = (struct ulp_bde64 *)
7657 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7658 if (!bpl)
7659 return IOCB_ERROR;
7660
7661 /* Should already be byte swapped. */
7662 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7663 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7664 /* swap the size field back to the cpu so we
7665 * can assign it to the sgl.
7666 */
7667 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007668 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7669 total_len = 0;
7670 for (i = 0; i < numBdes; i++) {
7671 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7672 total_len += bde.tus.f.bdeSize;
7673 }
James Smart4f774512009-05-22 14:52:35 -04007674 } else
James Smart5ffc2662009-11-18 15:39:44 -05007675 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007676
7677 iocbq->iocb.ulpIoTag = iocbq->iotag;
7678 cmnd = iocbq->iocb.ulpCommand;
7679
7680 switch (iocbq->iocb.ulpCommand) {
7681 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007682 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007683 if (!iocbq->iocb.ulpLe) {
7684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7685 "2007 Only Limited Edition cmd Format"
7686 " supported 0x%x\n",
7687 iocbq->iocb.ulpCommand);
7688 return IOCB_ERROR;
7689 }
James Smartff78d8f2011-12-13 13:21:35 -05007690
James Smart5ffc2662009-11-18 15:39:44 -05007691 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007692 /* Els_reguest64 has a TMO */
7693 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7694 iocbq->iocb.ulpTimeout);
7695 /* Need a VF for word 4 set the vf bit*/
7696 bf_set(els_req64_vf, &wqe->els_req, 0);
7697 /* And a VFID for word 12 */
7698 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007699 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007700 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7701 iocbq->iocb.ulpContext);
7702 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7703 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007704 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007705 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007706 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7707 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007708 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7709 iocbq->context2)->virt);
7710 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7711 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007712 *pcmd == ELS_CMD_SCR ||
James Smartff78d8f2011-12-13 13:21:35 -05007713 *pcmd == ELS_CMD_PLOGI)) {
7714 bf_set(els_req64_sp, &wqe->els_req, 1);
7715 bf_set(els_req64_sid, &wqe->els_req,
7716 iocbq->vport->fc_myDID);
7717 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7718 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7719 phba->vpi_ids[phba->pport->vpi]);
7720 } else if (iocbq->context1) {
7721 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7722 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7723 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7724 }
James Smartc8685952009-11-18 15:39:16 -05007725 }
James Smart6d368e52011-05-24 11:44:12 -04007726 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7727 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007728 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7729 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7730 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7731 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7732 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7733 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007734 break;
James Smart5ffc2662009-11-18 15:39:44 -05007735 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007736 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7737 iocbq->iocb.un.ulpWord[3]);
7738 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007739 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007740 /* The entire sequence is transmitted for this IOCB */
7741 xmit_len = total_len;
7742 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007743 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007744 /* word3 iocb=io_tag32 wqe=reserved */
7745 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007746 /* word4 relative_offset memcpy */
7747 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007748 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7749 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7750 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7751 LPFC_WQE_IOD_WRITE);
7752 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7753 LPFC_WQE_LENLOC_WORD12);
7754 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007755 wqe->xmit_sequence.xmit_len = xmit_len;
7756 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007757 break;
James Smart4f774512009-05-22 14:52:35 -04007758 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007759 /* word3 iocb=iotag32 wqe=seq_payload_len */
7760 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007761 /* word4 iocb=rsvd wqe=rsvd */
7762 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7763 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007764 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007765 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007766 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7767 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7768 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7769 LPFC_WQE_LENLOC_WORD3);
7770 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007771 break;
James Smart4f774512009-05-22 14:52:35 -04007772 case CMD_FCP_IWRITE64_CR:
7773 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007774 /* word3 iocb=iotag wqe=payload_offset_len */
7775 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7776 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007777 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007778 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7779 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7780 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7781 iocbq->iocb.ulpFCP2Rcvy);
7782 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7783 /* Always open the exchange */
7784 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7785 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7786 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7787 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7788 LPFC_WQE_LENLOC_WORD4);
7789 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7790 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007791 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007792 case CMD_FCP_IREAD64_CR:
7793 /* word3 iocb=iotag wqe=payload_offset_len */
7794 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7795 wqe->fcp_iread.payload_offset_len =
7796 xmit_len + sizeof(struct fcp_rsp);
7797 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7798 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7799 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7800 iocbq->iocb.ulpFCP2Rcvy);
7801 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007802 /* Always open the exchange */
7803 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007804 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7805 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7806 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7807 LPFC_WQE_LENLOC_WORD4);
7808 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7809 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007810 break;
James Smartf1126682009-06-10 17:22:44 -04007811 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007812 /* word3 iocb=IO_TAG wqe=reserved */
7813 wqe->fcp_icmd.rsrvd3 = 0;
7814 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007815 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007816 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7817 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7818 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7819 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7820 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7821 LPFC_WQE_LENLOC_NONE);
7822 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007823 break;
James Smart4f774512009-05-22 14:52:35 -04007824 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007825 /* For this command calculate the xmit length of the
7826 * request bde.
7827 */
7828 xmit_len = 0;
7829 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7830 sizeof(struct ulp_bde64);
7831 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007832 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007833 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7834 break;
James Smart63e801c2010-11-20 23:14:19 -05007835 xmit_len += bde.tus.f.bdeSize;
7836 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007837 /* word3 iocb=IO_TAG wqe=request_payload_len */
7838 wqe->gen_req.request_payload_len = xmit_len;
7839 /* word4 iocb=parameter wqe=relative_offset memcpy */
7840 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007841 /* word6 context tag copied in memcpy */
7842 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7843 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7845 "2015 Invalid CT %x command 0x%x\n",
7846 ct, iocbq->iocb.ulpCommand);
7847 return IOCB_ERROR;
7848 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007849 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7850 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7851 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7852 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7853 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7854 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7855 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7856 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007857 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007858 break;
James Smart4f774512009-05-22 14:52:35 -04007859 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007860 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007861 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007862 /* word3 iocb=iotag32 wqe=response_payload_len */
7863 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007864 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007865 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007866 /* word5 iocb=rsvd wge=did */
7867 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7868 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007869 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7870 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7871 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7872 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007873 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007874 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007875 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007876 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007877 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7878 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7879 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7880 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7881 LPFC_WQE_LENLOC_WORD3);
7882 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007883 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7884 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05007885 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7886 iocbq->context2)->virt);
7887 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7888 bf_set(els_req64_sp, &wqe->els_req, 1);
7889 bf_set(els_req64_sid, &wqe->els_req,
7890 iocbq->vport->fc_myDID);
7891 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7892 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7893 phba->vpi_ids[phba->pport->vpi]);
7894 }
James Smart4f774512009-05-22 14:52:35 -04007895 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007896 break;
James Smart4f774512009-05-22 14:52:35 -04007897 case CMD_CLOSE_XRI_CN:
7898 case CMD_ABORT_XRI_CN:
7899 case CMD_ABORT_XRI_CX:
7900 /* words 0-2 memcpy should be 0 rserved */
7901 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007902 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7903 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7904 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7905 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7906 } else
7907 fip = 0;
7908
7909 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007910 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007911 * The link is down, or the command was ELS_FIP
7912 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007913 * on the wire.
7914 */
7915 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7916 else
7917 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7918 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007919 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7920 wqe->abort_cmd.rsrvd5 = 0;
7921 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007922 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7923 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007924 /*
7925 * The abort handler will send us CMD_ABORT_XRI_CN or
7926 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7927 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007928 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7929 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7930 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7931 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007932 cmnd = CMD_ABORT_XRI_CX;
7933 command_type = OTHER_COMMAND;
7934 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007935 break;
James Smart6669f9b2009-10-02 15:16:45 -04007936 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007937 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007938 * we re-construct this WQE here based on information in
7939 * iocbq from scratch.
7940 */
7941 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007942 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007943 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007944 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7945 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007946 LPFC_ABTS_UNSOL_INT) {
7947 /* ABTS sent by initiator to CT exchange, the
7948 * RX_ID field will be filled with the newly
7949 * allocated responder XRI.
7950 */
7951 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7952 iocbq->sli4_xritag);
7953 } else {
7954 /* ABTS sent by responder to CT exchange, the
7955 * RX_ID field will be filled with the responder
7956 * RX_ID from ABTS.
7957 */
7958 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007959 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007960 }
James Smart6669f9b2009-10-02 15:16:45 -04007961 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7962 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7963 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7964 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04007965 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7966 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7967 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04007968 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7969 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05007970 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7971 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7972 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7973 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7974 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7975 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7976 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7977 }
7978
James Smart7851fe22011-07-22 18:36:52 -04007979 break;
James Smart4f774512009-05-22 14:52:35 -04007980 case CMD_XRI_ABORTED_CX:
7981 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04007982 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7983 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7984 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7985 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7986 default:
7987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7988 "2014 Invalid command 0x%x\n",
7989 iocbq->iocb.ulpCommand);
7990 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04007991 break;
James Smart4f774512009-05-22 14:52:35 -04007992 }
James Smart6d368e52011-05-24 11:44:12 -04007993
James Smartf0d9bcc2010-10-22 11:07:09 -04007994 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7995 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7996 wqe->generic.wqe_com.abort_tag = abort_tag;
7997 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7998 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7999 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8000 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008001 return 0;
8002}
8003
8004/**
8005 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8006 * @phba: Pointer to HBA context object.
8007 * @ring_number: SLI ring number to issue iocb on.
8008 * @piocb: Pointer to command iocb.
8009 * @flag: Flag indicating if this command can be put into txq.
8010 *
8011 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8012 * an iocb command to an HBA with SLI-4 interface spec.
8013 *
8014 * This function is called with hbalock held. The function will return success
8015 * after it successfully submit the iocb to firmware or after adding to the
8016 * txq.
8017 **/
8018static int
8019__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8020 struct lpfc_iocbq *piocb, uint32_t flag)
8021{
8022 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008023 union lpfc_wqe wqe;
8024 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008025
8026 if (piocb->sli4_xritag == NO_XRI) {
8027 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05008028 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
8029 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04008030 sglq = NULL;
8031 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008032 if (pring->txq_cnt) {
8033 if (!(flag & SLI_IOCB_RET_IOCB)) {
8034 __lpfc_sli_ringtx_put(phba,
8035 pring, piocb);
8036 return IOCB_SUCCESS;
8037 } else {
8038 return IOCB_BUSY;
8039 }
8040 } else {
James Smart6d368e52011-05-24 11:44:12 -04008041 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008042 if (!sglq) {
8043 if (!(flag & SLI_IOCB_RET_IOCB)) {
8044 __lpfc_sli_ringtx_put(phba,
8045 pring,
8046 piocb);
8047 return IOCB_SUCCESS;
8048 } else
8049 return IOCB_BUSY;
8050 }
8051 }
James Smart4f774512009-05-22 14:52:35 -04008052 }
8053 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008054 /* These IO's already have an XRI and a mapped sgl. */
8055 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008056 } else {
James Smart6d368e52011-05-24 11:44:12 -04008057 /*
8058 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008059 * sglq is on the active list
8060 */
8061 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8062 if (!sglq)
8063 return IOCB_ERROR;
8064 }
8065
8066 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008067 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008068 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008069 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008070 return IOCB_ERROR;
8071 }
8072
8073 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8074 return IOCB_ERROR;
8075
James Smart341af102010-01-26 23:07:37 -05008076 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8077 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008078 /*
8079 * For FCP command IOCB, get a new WQ index to distribute
8080 * WQE across the WQsr. On the other hand, for abort IOCB,
8081 * it carries the same WQ index to the original command
8082 * IOCB.
8083 */
James Smart341af102010-01-26 23:07:37 -05008084 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008085 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8086 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8087 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008088 return IOCB_ERROR;
8089 } else {
8090 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8091 return IOCB_ERROR;
8092 }
8093 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8094
8095 return 0;
8096}
8097
8098/**
James Smart3772a992009-05-22 14:50:54 -04008099 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8100 *
8101 * This routine wraps the actual lockless version for issusing IOCB function
8102 * pointer from the lpfc_hba struct.
8103 *
8104 * Return codes:
8105 * IOCB_ERROR - Error
8106 * IOCB_SUCCESS - Success
8107 * IOCB_BUSY - Busy
8108 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008109int
James Smart3772a992009-05-22 14:50:54 -04008110__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8111 struct lpfc_iocbq *piocb, uint32_t flag)
8112{
8113 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8114}
8115
8116/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008117 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008118 * @phba: The hba struct for which this call is being executed.
8119 * @dev_grp: The HBA PCI-Device group number.
8120 *
8121 * This routine sets up the SLI interface API function jump table in @phba
8122 * struct.
8123 * Returns: 0 - success, -ENODEV - failure.
8124 **/
8125int
8126lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8127{
8128
8129 switch (dev_grp) {
8130 case LPFC_PCI_DEV_LP:
8131 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8132 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8133 break;
James Smart4f774512009-05-22 14:52:35 -04008134 case LPFC_PCI_DEV_OC:
8135 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8136 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8137 break;
James Smart3772a992009-05-22 14:50:54 -04008138 default:
8139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8140 "1419 Invalid HBA PCI-device group: 0x%x\n",
8141 dev_grp);
8142 return -ENODEV;
8143 break;
8144 }
8145 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8146 return 0;
8147}
James Smart92d7f7b2007-06-17 19:56:38 -05008148
James Smarte59058c2008-08-24 21:49:00 -04008149/**
James Smart3621a712009-04-06 18:47:14 -04008150 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008151 * @phba: Pointer to HBA context object.
8152 * @pring: Pointer to driver SLI ring object.
8153 * @piocb: Pointer to command iocb.
8154 * @flag: Flag indicating if this command can be put into txq.
8155 *
8156 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8157 * function. This function gets the hbalock and calls
8158 * __lpfc_sli_issue_iocb function and will return the error returned
8159 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8160 * functions which do not hold hbalock.
8161 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008162int
James Smart3772a992009-05-22 14:50:54 -04008163lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008164 struct lpfc_iocbq *piocb, uint32_t flag)
8165{
8166 unsigned long iflags;
8167 int rc;
8168
8169 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008170 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008171 spin_unlock_irqrestore(&phba->hbalock, iflags);
8172
8173 return rc;
8174}
8175
James Smarte59058c2008-08-24 21:49:00 -04008176/**
James Smart3621a712009-04-06 18:47:14 -04008177 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008178 * @phba: Pointer to HBA context object.
8179 *
8180 * This function is called while driver attaches with the
8181 * HBA to setup the extra ring. The extra ring is used
8182 * only when driver needs to support target mode functionality
8183 * or IP over FC functionalities.
8184 *
8185 * This function is called with no lock held.
8186 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008187static int
8188lpfc_extra_ring_setup( struct lpfc_hba *phba)
8189{
8190 struct lpfc_sli *psli;
8191 struct lpfc_sli_ring *pring;
8192
8193 psli = &phba->sli;
8194
8195 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008196
8197 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008198 pring = &psli->ring[psli->fcp_ring];
8199 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8200 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8201 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8202 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8203
James Smarta4bc3372006-12-02 13:34:16 -05008204 /* and give them to the extra ring */
8205 pring = &psli->ring[psli->extra_ring];
8206
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008207 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8208 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8209 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8210 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8211
8212 /* Setup default profile for this ring */
8213 pring->iotag_max = 4096;
8214 pring->num_mask = 1;
8215 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008216 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8217 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008218 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8219 return 0;
8220}
8221
James Smartcb69f7d2011-12-13 13:21:57 -05008222/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8223 * @vport: pointer to virtual port object.
8224 * @ndlp: nodelist pointer for the impacted rport.
8225 *
8226 * The driver calls this routine in response to a XRI ABORT CQE
8227 * event from the port. In this event, the driver is required to
8228 * recover its login to the rport even though its login may be valid
8229 * from the driver's perspective. The failed ABTS notice from the
8230 * port indicates the rport is not responding.
8231 */
8232static void
8233lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8234 struct lpfc_nodelist *ndlp)
8235{
8236 struct Scsi_Host *shost;
8237 struct lpfc_hba *phba;
8238 unsigned long flags = 0;
8239
8240 shost = lpfc_shost_from_vport(vport);
8241 phba = vport->phba;
8242 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8243 lpfc_printf_log(phba, KERN_INFO,
8244 LOG_SLI, "3093 No rport recovery needed. "
8245 "rport in state 0x%x\n",
8246 ndlp->nlp_state);
8247 return;
8248 }
8249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8250 "3094 Start rport recovery on shost id 0x%x "
8251 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8252 "flags 0x%x\n",
8253 shost->host_no, ndlp->nlp_DID,
8254 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8255 ndlp->nlp_flag);
8256 /*
8257 * The rport is not responding. Don't attempt ADISC recovery.
8258 * Remove the FCP-2 flag to force a PLOGI.
8259 */
8260 spin_lock_irqsave(shost->host_lock, flags);
8261 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8262 spin_unlock_irqrestore(shost->host_lock, flags);
8263 lpfc_disc_state_machine(vport, ndlp, NULL,
8264 NLP_EVT_DEVICE_RECOVERY);
8265 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8266 spin_lock_irqsave(shost->host_lock, flags);
8267 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8268 spin_unlock_irqrestore(shost->host_lock, flags);
8269 lpfc_disc_start(vport);
8270}
8271
8272/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8273 * @phba: Pointer to HBA context object.
8274 * @iocbq: Pointer to iocb object.
8275 *
8276 * The async_event handler calls this routine when it receives
8277 * an ASYNC_STATUS_CN event from the port. The port generates
8278 * this event when an Abort Sequence request to an rport fails
8279 * twice in succession. The abort could be originated by the
8280 * driver or by the port. The ABTS could have been for an ELS
8281 * or FCP IO. The port only generates this event when an ABTS
8282 * fails to complete after one retry.
8283 */
8284static void
8285lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8286 struct lpfc_iocbq *iocbq)
8287{
8288 struct lpfc_nodelist *ndlp = NULL;
8289 uint16_t rpi = 0, vpi = 0;
8290 struct lpfc_vport *vport = NULL;
8291
8292 /* The rpi in the ulpContext is vport-sensitive. */
8293 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8294 rpi = iocbq->iocb.ulpContext;
8295
8296 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8297 "3092 Port generated ABTS async event "
8298 "on vpi %d rpi %d status 0x%x\n",
8299 vpi, rpi, iocbq->iocb.ulpStatus);
8300
8301 vport = lpfc_find_vport_by_vpid(phba, vpi);
8302 if (!vport)
8303 goto err_exit;
8304 ndlp = lpfc_findnode_rpi(vport, rpi);
8305 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8306 goto err_exit;
8307
8308 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8309 lpfc_sli_abts_recover_port(vport, ndlp);
8310 return;
8311
8312 err_exit:
8313 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8314 "3095 Event Context not found, no "
8315 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8316 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8317 vpi, rpi);
8318}
8319
8320/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8321 * @phba: pointer to HBA context object.
8322 * @ndlp: nodelist pointer for the impacted rport.
8323 * @axri: pointer to the wcqe containing the failed exchange.
8324 *
8325 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8326 * port. The port generates this event when an abort exchange request to an
8327 * rport fails twice in succession with no reply. The abort could be originated
8328 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8329 */
8330void
8331lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8332 struct lpfc_nodelist *ndlp,
8333 struct sli4_wcqe_xri_aborted *axri)
8334{
8335 struct lpfc_vport *vport;
8336
8337 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8338 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8339 "3115 Node Context not found, driver "
8340 "ignoring abts err event\n");
8341 vport = ndlp->vport;
8342 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8343 "3116 Port generated FCP XRI ABORT event on "
8344 "vpi %d rpi %d xri x%x status 0x%x\n",
8345 ndlp->vport->vpi, ndlp->nlp_rpi,
8346 bf_get(lpfc_wcqe_xa_xri, axri),
8347 bf_get(lpfc_wcqe_xa_status, axri));
8348
8349 if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
8350 lpfc_sli_abts_recover_port(vport, ndlp);
8351}
8352
James Smarte59058c2008-08-24 21:49:00 -04008353/**
James Smart3621a712009-04-06 18:47:14 -04008354 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008355 * @phba: Pointer to HBA context object.
8356 * @pring: Pointer to driver SLI ring object.
8357 * @iocbq: Pointer to iocb object.
8358 *
8359 * This function is called by the slow ring event handler
8360 * function when there is an ASYNC event iocb in the ring.
8361 * This function is called with no lock held.
8362 * Currently this function handles only temperature related
8363 * ASYNC events. The function decodes the temperature sensor
8364 * event message and posts events for the management applications.
8365 **/
James Smart98c9ea52007-10-27 13:37:33 -04008366static void
James Smart57127f12007-10-27 13:37:05 -04008367lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8368 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8369{
8370 IOCB_t *icmd;
8371 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008372 struct temp_event temp_event_data;
8373 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008374 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008375
8376 icmd = &iocbq->iocb;
8377 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008378
James Smartcb69f7d2011-12-13 13:21:57 -05008379 switch (evt_code) {
8380 case ASYNC_TEMP_WARN:
8381 case ASYNC_TEMP_SAFE:
8382 temp_event_data.data = (uint32_t) icmd->ulpContext;
8383 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8384 if (evt_code == ASYNC_TEMP_WARN) {
8385 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8386 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8387 "0347 Adapter is very hot, please take "
8388 "corrective action. temperature : %d Celsius\n",
8389 (uint32_t) icmd->ulpContext);
8390 } else {
8391 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8392 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8393 "0340 Adapter temperature is OK now. "
8394 "temperature : %d Celsius\n",
8395 (uint32_t) icmd->ulpContext);
8396 }
8397
8398 /* Send temperature change event to applications */
8399 shost = lpfc_shost_from_vport(phba->pport);
8400 fc_host_post_vendor_event(shost, fc_get_event_number(),
8401 sizeof(temp_event_data), (char *) &temp_event_data,
8402 LPFC_NL_VENDOR_ID);
8403 break;
8404 case ASYNC_STATUS_CN:
8405 lpfc_sli_abts_err_handler(phba, iocbq);
8406 break;
8407 default:
James Smarta257bf92009-04-06 18:48:10 -04008408 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008410 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008411 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008412 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8413 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8414 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8415 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008416 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008417 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8418 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8419 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8420 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8421
James Smartcb69f7d2011-12-13 13:21:57 -05008422 break;
James Smart57127f12007-10-27 13:37:05 -04008423 }
James Smart57127f12007-10-27 13:37:05 -04008424}
8425
8426
James Smarte59058c2008-08-24 21:49:00 -04008427/**
James Smart3621a712009-04-06 18:47:14 -04008428 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008429 * @phba: Pointer to HBA context object.
8430 *
8431 * lpfc_sli_setup sets up rings of the SLI interface with
8432 * number of iocbs per ring and iotags. This function is
8433 * called while driver attach to the HBA and before the
8434 * interrupts are enabled. So there is no need for locking.
8435 *
8436 * This function always returns 0.
8437 **/
dea31012005-04-17 16:05:31 -05008438int
8439lpfc_sli_setup(struct lpfc_hba *phba)
8440{
James Smarted957682007-06-17 19:56:37 -05008441 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008442 struct lpfc_sli *psli = &phba->sli;
8443 struct lpfc_sli_ring *pring;
8444
8445 psli->num_rings = MAX_CONFIGURED_RINGS;
8446 psli->sli_flag = 0;
8447 psli->fcp_ring = LPFC_FCP_RING;
8448 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008449 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008450
James Bottomley604a3e32005-10-29 10:28:33 -05008451 psli->iocbq_lookup = NULL;
8452 psli->iocbq_lookup_len = 0;
8453 psli->last_iotag = 0;
8454
dea31012005-04-17 16:05:31 -05008455 for (i = 0; i < psli->num_rings; i++) {
8456 pring = &psli->ring[i];
8457 switch (i) {
8458 case LPFC_FCP_RING: /* ring 0 - FCP */
8459 /* numCiocb and numRiocb are used in config_port */
8460 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8461 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8462 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8463 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8464 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8465 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008466 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008467 SLI3_IOCB_CMD_SIZE :
8468 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008469 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008470 SLI3_IOCB_RSP_SIZE :
8471 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008472 pring->iotag_ctr = 0;
8473 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008474 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008475 pring->fast_iotag = pring->iotag_max;
8476 pring->num_mask = 0;
8477 break;
James Smarta4bc3372006-12-02 13:34:16 -05008478 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008479 /* numCiocb and numRiocb are used in config_port */
8480 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8481 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008482 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008483 SLI3_IOCB_CMD_SIZE :
8484 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008485 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008486 SLI3_IOCB_RSP_SIZE :
8487 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008488 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008489 pring->num_mask = 0;
8490 break;
8491 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8492 /* numCiocb and numRiocb are used in config_port */
8493 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8494 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008495 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008496 SLI3_IOCB_CMD_SIZE :
8497 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008498 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008499 SLI3_IOCB_RSP_SIZE :
8500 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008501 pring->fast_iotag = 0;
8502 pring->iotag_ctr = 0;
8503 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008504 pring->lpfc_sli_rcv_async_status =
8505 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008506 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008507 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008508 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8509 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008510 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008511 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008512 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008513 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8514 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008515 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008516 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008517 pring->prt[2].profile = 0; /* Mask 2 */
8518 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008519 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008520 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008521 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008522 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008523 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008524 pring->prt[3].profile = 0; /* Mask 3 */
8525 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008526 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008527 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008528 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008529 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008530 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008531 /* abort unsolicited sequence */
8532 pring->prt[4].profile = 0; /* Mask 4 */
8533 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8534 pring->prt[4].type = FC_TYPE_BLS;
8535 pring->prt[4].lpfc_sli_rcv_unsol_event =
8536 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008537 break;
8538 }
James Smarted957682007-06-17 19:56:37 -05008539 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008540 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008541 }
James Smarted957682007-06-17 19:56:37 -05008542 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008543 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008544 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8545 "SLI2 SLIM Data: x%x x%lx\n",
8546 phba->brd_no, totiocbsize,
8547 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008548 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008549 if (phba->cfg_multi_ring_support == 2)
8550 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008551
8552 return 0;
8553}
8554
James Smarte59058c2008-08-24 21:49:00 -04008555/**
James Smart3621a712009-04-06 18:47:14 -04008556 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008557 * @phba: Pointer to HBA context object.
8558 *
8559 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8560 * ring. This function also initializes ring indices of each ring.
8561 * This function is called during the initialization of the SLI
8562 * interface of an HBA.
8563 * This function is called with no lock held and always returns
8564 * 1.
8565 **/
dea31012005-04-17 16:05:31 -05008566int
James Smart2e0fef82007-06-17 19:56:36 -05008567lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008568{
8569 struct lpfc_sli *psli;
8570 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008571 int i;
dea31012005-04-17 16:05:31 -05008572
8573 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008574 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008575 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008576 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008577 /* Initialize list headers for txq and txcmplq as double linked lists */
8578 for (i = 0; i < psli->num_rings; i++) {
8579 pring = &psli->ring[i];
8580 pring->ringno = i;
8581 pring->next_cmdidx = 0;
8582 pring->local_getidx = 0;
8583 pring->cmdidx = 0;
8584 INIT_LIST_HEAD(&pring->txq);
8585 INIT_LIST_HEAD(&pring->txcmplq);
8586 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008587 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008588 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008589 }
James Smart2e0fef82007-06-17 19:56:36 -05008590 spin_unlock_irq(&phba->hbalock);
8591 return 1;
dea31012005-04-17 16:05:31 -05008592}
8593
James Smarte59058c2008-08-24 21:49:00 -04008594/**
James Smart04c68492009-05-22 14:52:52 -04008595 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8596 * @phba: Pointer to HBA context object.
8597 *
8598 * This routine flushes the mailbox command subsystem. It will unconditionally
8599 * flush all the mailbox commands in the three possible stages in the mailbox
8600 * command sub-system: pending mailbox command queue; the outstanding mailbox
8601 * command; and completed mailbox command queue. It is caller's responsibility
8602 * to make sure that the driver is in the proper state to flush the mailbox
8603 * command sub-system. Namely, the posting of mailbox commands into the
8604 * pending mailbox command queue from the various clients must be stopped;
8605 * either the HBA is in a state that it will never works on the outstanding
8606 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8607 * mailbox command has been completed.
8608 **/
8609static void
8610lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8611{
8612 LIST_HEAD(completions);
8613 struct lpfc_sli *psli = &phba->sli;
8614 LPFC_MBOXQ_t *pmb;
8615 unsigned long iflag;
8616
8617 /* Flush all the mailbox commands in the mbox system */
8618 spin_lock_irqsave(&phba->hbalock, iflag);
8619 /* The pending mailbox command queue */
8620 list_splice_init(&phba->sli.mboxq, &completions);
8621 /* The outstanding active mailbox command */
8622 if (psli->mbox_active) {
8623 list_add_tail(&psli->mbox_active->list, &completions);
8624 psli->mbox_active = NULL;
8625 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8626 }
8627 /* The completed mailbox command queue */
8628 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8629 spin_unlock_irqrestore(&phba->hbalock, iflag);
8630
8631 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8632 while (!list_empty(&completions)) {
8633 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8634 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8635 if (pmb->mbox_cmpl)
8636 pmb->mbox_cmpl(phba, pmb);
8637 }
8638}
8639
8640/**
James Smart3621a712009-04-06 18:47:14 -04008641 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008642 * @vport: Pointer to virtual port object.
8643 *
8644 * lpfc_sli_host_down is called to clean up the resources
8645 * associated with a vport before destroying virtual
8646 * port data structures.
8647 * This function does following operations:
8648 * - Free discovery resources associated with this virtual
8649 * port.
8650 * - Free iocbs associated with this virtual port in
8651 * the txq.
8652 * - Send abort for all iocb commands associated with this
8653 * vport in txcmplq.
8654 *
8655 * This function is called with no lock held and always returns 1.
8656 **/
dea31012005-04-17 16:05:31 -05008657int
James Smart92d7f7b2007-06-17 19:56:38 -05008658lpfc_sli_host_down(struct lpfc_vport *vport)
8659{
James Smart858c9f62007-06-17 19:56:39 -05008660 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008661 struct lpfc_hba *phba = vport->phba;
8662 struct lpfc_sli *psli = &phba->sli;
8663 struct lpfc_sli_ring *pring;
8664 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008665 int i;
8666 unsigned long flags = 0;
8667 uint16_t prev_pring_flag;
8668
8669 lpfc_cleanup_discovery_resources(vport);
8670
8671 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008672 for (i = 0; i < psli->num_rings; i++) {
8673 pring = &psli->ring[i];
8674 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008675 /* Only slow rings */
8676 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008677 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008678 /* Set the lpfc data pending flag */
8679 set_bit(LPFC_DATA_READY, &phba->data_flags);
8680 }
James Smart92d7f7b2007-06-17 19:56:38 -05008681 /*
8682 * Error everything on the txq since these iocbs have not been
8683 * given to the FW yet.
8684 */
James Smart92d7f7b2007-06-17 19:56:38 -05008685 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8686 if (iocb->vport != vport)
8687 continue;
James Smart858c9f62007-06-17 19:56:39 -05008688 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008689 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008690 }
8691
8692 /* Next issue ABTS for everything on the txcmplq */
8693 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8694 list) {
8695 if (iocb->vport != vport)
8696 continue;
8697 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8698 }
8699
8700 pring->flag = prev_pring_flag;
8701 }
8702
8703 spin_unlock_irqrestore(&phba->hbalock, flags);
8704
James Smarta257bf92009-04-06 18:48:10 -04008705 /* Cancel all the IOCBs from the completions list */
8706 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8707 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008708 return 1;
8709}
8710
James Smarte59058c2008-08-24 21:49:00 -04008711/**
James Smart3621a712009-04-06 18:47:14 -04008712 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008713 * @phba: Pointer to HBA context object.
8714 *
8715 * This function cleans up all iocb, buffers, mailbox commands
8716 * while shutting down the HBA. This function is called with no
8717 * lock held and always returns 1.
8718 * This function does the following to cleanup driver resources:
8719 * - Free discovery resources for each virtual port
8720 * - Cleanup any pending fabric iocbs
8721 * - Iterate through the iocb txq and free each entry
8722 * in the list.
8723 * - Free up any buffer posted to the HBA
8724 * - Free mailbox commands in the mailbox queue.
8725 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008726int
James Smart2e0fef82007-06-17 19:56:36 -05008727lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008728{
James Smart2534ba72007-04-25 09:52:20 -04008729 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008730 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008731 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008732 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008733 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008734 int i;
8735
8736 /* Shutdown the mailbox command sub-system */
8737 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008738
dea31012005-04-17 16:05:31 -05008739 lpfc_hba_down_prep(phba);
8740
James Smart92d7f7b2007-06-17 19:56:38 -05008741 lpfc_fabric_abort_hba(phba);
8742
James Smart2e0fef82007-06-17 19:56:36 -05008743 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008744 for (i = 0; i < psli->num_rings; i++) {
8745 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008746 /* Only slow rings */
8747 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008748 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008749 /* Set the lpfc data pending flag */
8750 set_bit(LPFC_DATA_READY, &phba->data_flags);
8751 }
dea31012005-04-17 16:05:31 -05008752
8753 /*
8754 * Error everything on the txq since these iocbs have not been
8755 * given to the FW yet.
8756 */
James Smart2534ba72007-04-25 09:52:20 -04008757 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008758 pring->txq_cnt = 0;
8759
dea31012005-04-17 16:05:31 -05008760 }
James Smart2e0fef82007-06-17 19:56:36 -05008761 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008762
James Smarta257bf92009-04-06 18:48:10 -04008763 /* Cancel all the IOCBs from the completions list */
8764 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8765 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008766
James Smart0ff10d42008-01-11 01:52:36 -05008767 spin_lock_irqsave(&phba->hbalock, flags);
8768 list_splice_init(&phba->elsbuf, &completions);
8769 phba->elsbuf_cnt = 0;
8770 phba->elsbuf_prev_cnt = 0;
8771 spin_unlock_irqrestore(&phba->hbalock, flags);
8772
8773 while (!list_empty(&completions)) {
8774 list_remove_head(&completions, buf_ptr,
8775 struct lpfc_dmabuf, list);
8776 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8777 kfree(buf_ptr);
8778 }
8779
dea31012005-04-17 16:05:31 -05008780 /* Return any active mbox cmds */
8781 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008782
James Smartda0436e2009-05-22 14:51:39 -04008783 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008784 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008785 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008786
James Smartda0436e2009-05-22 14:51:39 -04008787 return 1;
8788}
James Smart92d7f7b2007-06-17 19:56:38 -05008789
James Smartda0436e2009-05-22 14:51:39 -04008790/**
James Smart3621a712009-04-06 18:47:14 -04008791 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008792 * @srcp: Source memory pointer.
8793 * @destp: Destination memory pointer.
8794 * @cnt: Number of words required to be copied.
8795 *
8796 * This function is used for copying data between driver memory
8797 * and the SLI memory. This function also changes the endianness
8798 * of each word if native endianness is different from SLI
8799 * endianness. This function can be called with or without
8800 * lock.
8801 **/
dea31012005-04-17 16:05:31 -05008802void
8803lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8804{
8805 uint32_t *src = srcp;
8806 uint32_t *dest = destp;
8807 uint32_t ldata;
8808 int i;
8809
8810 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8811 ldata = *src;
8812 ldata = le32_to_cpu(ldata);
8813 *dest = ldata;
8814 src++;
8815 dest++;
8816 }
8817}
8818
James Smarte59058c2008-08-24 21:49:00 -04008819
8820/**
James Smarta0c87cb2009-07-19 10:01:10 -04008821 * lpfc_sli_bemem_bcopy - SLI memory copy function
8822 * @srcp: Source memory pointer.
8823 * @destp: Destination memory pointer.
8824 * @cnt: Number of words required to be copied.
8825 *
8826 * This function is used for copying data between a data structure
8827 * with big endian representation to local endianness.
8828 * This function can be called with or without lock.
8829 **/
8830void
8831lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8832{
8833 uint32_t *src = srcp;
8834 uint32_t *dest = destp;
8835 uint32_t ldata;
8836 int i;
8837
8838 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8839 ldata = *src;
8840 ldata = be32_to_cpu(ldata);
8841 *dest = ldata;
8842 src++;
8843 dest++;
8844 }
8845}
8846
8847/**
James Smart3621a712009-04-06 18:47:14 -04008848 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008849 * @phba: Pointer to HBA context object.
8850 * @pring: Pointer to driver SLI ring object.
8851 * @mp: Pointer to driver buffer object.
8852 *
8853 * This function is called with no lock held.
8854 * It always return zero after adding the buffer to the postbufq
8855 * buffer list.
8856 **/
dea31012005-04-17 16:05:31 -05008857int
James Smart2e0fef82007-06-17 19:56:36 -05008858lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8859 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008860{
8861 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8862 later */
James Smart2e0fef82007-06-17 19:56:36 -05008863 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008864 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008865 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008866 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008867 return 0;
8868}
8869
James Smarte59058c2008-08-24 21:49:00 -04008870/**
James Smart3621a712009-04-06 18:47:14 -04008871 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008872 * @phba: Pointer to HBA context object.
8873 *
8874 * When HBQ is enabled, buffers are searched based on tags. This function
8875 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8876 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8877 * does not conflict with tags of buffer posted for unsolicited events.
8878 * The function returns the allocated tag. The function is called with
8879 * no locks held.
8880 **/
James Smart76bb24e2007-10-27 13:38:00 -04008881uint32_t
8882lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8883{
8884 spin_lock_irq(&phba->hbalock);
8885 phba->buffer_tag_count++;
8886 /*
8887 * Always set the QUE_BUFTAG_BIT to distiguish between
8888 * a tag assigned by HBQ.
8889 */
8890 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8891 spin_unlock_irq(&phba->hbalock);
8892 return phba->buffer_tag_count;
8893}
8894
James Smarte59058c2008-08-24 21:49:00 -04008895/**
James Smart3621a712009-04-06 18:47:14 -04008896 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008897 * @phba: Pointer to HBA context object.
8898 * @pring: Pointer to driver SLI ring object.
8899 * @tag: Buffer tag.
8900 *
8901 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8902 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8903 * iocb is posted to the response ring with the tag of the buffer.
8904 * This function searches the pring->postbufq list using the tag
8905 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8906 * iocb. If the buffer is found then lpfc_dmabuf object of the
8907 * buffer is returned to the caller else NULL is returned.
8908 * This function is called with no lock held.
8909 **/
James Smart76bb24e2007-10-27 13:38:00 -04008910struct lpfc_dmabuf *
8911lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8912 uint32_t tag)
8913{
8914 struct lpfc_dmabuf *mp, *next_mp;
8915 struct list_head *slp = &pring->postbufq;
8916
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008917 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008918 spin_lock_irq(&phba->hbalock);
8919 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8920 if (mp->buffer_tag == tag) {
8921 list_del_init(&mp->list);
8922 pring->postbufq_cnt--;
8923 spin_unlock_irq(&phba->hbalock);
8924 return mp;
8925 }
8926 }
8927
8928 spin_unlock_irq(&phba->hbalock);
8929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008930 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008931 "ring %d Data x%lx x%p x%p x%x\n",
8932 pring->ringno, (unsigned long) tag,
8933 slp->next, slp->prev, pring->postbufq_cnt);
8934
8935 return NULL;
8936}
dea31012005-04-17 16:05:31 -05008937
James Smarte59058c2008-08-24 21:49:00 -04008938/**
James Smart3621a712009-04-06 18:47:14 -04008939 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008940 * @phba: Pointer to HBA context object.
8941 * @pring: Pointer to driver SLI ring object.
8942 * @phys: DMA address of the buffer.
8943 *
8944 * This function searches the buffer list using the dma_address
8945 * of unsolicited event to find the driver's lpfc_dmabuf object
8946 * corresponding to the dma_address. The function returns the
8947 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8948 * This function is called by the ct and els unsolicited event
8949 * handlers to get the buffer associated with the unsolicited
8950 * event.
8951 *
8952 * This function is called with no lock held.
8953 **/
dea31012005-04-17 16:05:31 -05008954struct lpfc_dmabuf *
8955lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8956 dma_addr_t phys)
8957{
8958 struct lpfc_dmabuf *mp, *next_mp;
8959 struct list_head *slp = &pring->postbufq;
8960
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008961 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05008962 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008963 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8964 if (mp->phys == phys) {
8965 list_del_init(&mp->list);
8966 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05008967 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008968 return mp;
8969 }
8970 }
8971
James Smart2e0fef82007-06-17 19:56:36 -05008972 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04008974 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05008975 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008976 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05008977 slp->next, slp->prev, pring->postbufq_cnt);
8978 return NULL;
8979}
8980
James Smarte59058c2008-08-24 21:49:00 -04008981/**
James Smart3621a712009-04-06 18:47:14 -04008982 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04008983 * @phba: Pointer to HBA context object.
8984 * @cmdiocb: Pointer to driver command iocb object.
8985 * @rspiocb: Pointer to driver response iocb object.
8986 *
8987 * This function is the completion handler for the abort iocbs for
8988 * ELS commands. This function is called from the ELS ring event
8989 * handler with no lock held. This function frees memory resources
8990 * associated with the abort iocb.
8991 **/
dea31012005-04-17 16:05:31 -05008992static void
James Smart2e0fef82007-06-17 19:56:36 -05008993lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8994 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05008995{
James Smart2e0fef82007-06-17 19:56:36 -05008996 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04008997 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05008998 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04008999
9000 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009001
9002 /*
9003 * Assume that the port already completed and returned, or
9004 * will return the iocb. Just Log the message.
9005 */
James Smart2680eea2007-04-25 09:52:55 -04009006 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9007 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9008
James Smart2e0fef82007-06-17 19:56:36 -05009009 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009010 if (phba->sli_rev < LPFC_SLI_REV4) {
9011 if (abort_iotag != 0 &&
9012 abort_iotag <= phba->sli.last_iotag)
9013 abort_iocb =
9014 phba->sli.iocbq_lookup[abort_iotag];
9015 } else
9016 /* For sli4 the abort_tag is the XRI,
9017 * so the abort routine puts the iotag of the iocb
9018 * being aborted in the context field of the abort
9019 * IOCB.
9020 */
9021 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009022
James Smart2a9bf3d2010-06-07 15:24:45 -04009023 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9024 "0327 Cannot abort els iocb %p "
9025 "with tag %x context %x, abort status %x, "
9026 "abort code %x\n",
9027 abort_iocb, abort_iotag, abort_context,
9028 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009029
James Smartff78d8f2011-12-13 13:21:35 -05009030 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009031 }
James Bottomley604a3e32005-10-29 10:28:33 -05009032 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009033 return;
9034}
9035
James Smarte59058c2008-08-24 21:49:00 -04009036/**
James Smart3621a712009-04-06 18:47:14 -04009037 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009038 * @phba: Pointer to HBA context object.
9039 * @cmdiocb: Pointer to driver command iocb object.
9040 * @rspiocb: Pointer to driver response iocb object.
9041 *
9042 * The function is called from SLI ring event handler with no
9043 * lock held. This function is the completion handler for ELS commands
9044 * which are aborted. The function frees memory resources used for
9045 * the aborted ELS commands.
9046 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009047static void
9048lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9049 struct lpfc_iocbq *rspiocb)
9050{
9051 IOCB_t *irsp = &rspiocb->iocb;
9052
9053 /* ELS cmd tag <ulpIoTag> completes */
9054 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009055 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009056 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009057 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009058 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009059 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9060 lpfc_ct_free_iocb(phba, cmdiocb);
9061 else
9062 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009063 return;
9064}
9065
James Smarte59058c2008-08-24 21:49:00 -04009066/**
James Smart5af5eee2010-10-22 11:06:38 -04009067 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009068 * @phba: Pointer to HBA context object.
9069 * @pring: Pointer to driver SLI ring object.
9070 * @cmdiocb: Pointer to driver command iocb object.
9071 *
James Smart5af5eee2010-10-22 11:06:38 -04009072 * This function issues an abort iocb for the provided command iocb down to
9073 * the port. Other than the case the outstanding command iocb is an abort
9074 * request, this function issues abort out unconditionally. This function is
9075 * called with hbalock held. The function returns 0 when it fails due to
9076 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009077 **/
James Smart5af5eee2010-10-22 11:06:38 -04009078static int
9079lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009080 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009081{
James Smart2e0fef82007-06-17 19:56:36 -05009082 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009083 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009084 IOCB_t *icmd = NULL;
9085 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009086 int retval;
James Smart07951072007-04-25 09:51:38 -04009087
James Smart92d7f7b2007-06-17 19:56:38 -05009088 /*
9089 * There are certain command types we don't want to abort. And we
9090 * don't want to abort commands that are already in the process of
9091 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009092 */
9093 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009094 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009095 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9096 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009097 return 0;
9098
dea31012005-04-17 16:05:31 -05009099 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009100 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009101 if (abtsiocbp == NULL)
9102 return 0;
dea31012005-04-17 16:05:31 -05009103
James Smart07951072007-04-25 09:51:38 -04009104 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009105 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009106 */
9107 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9108
dea31012005-04-17 16:05:31 -05009109 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009110 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9111 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009112 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009113 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009114 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9115 }
James Smartda0436e2009-05-22 14:51:39 -04009116 else
9117 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009118 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009119 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009120
James Smart5ffc2662009-11-18 15:39:44 -05009121 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9122 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009123 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9124 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009125
James Smart2e0fef82007-06-17 19:56:36 -05009126 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009127 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9128 else
9129 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9130
9131 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009132
James Smarte8b62012007-08-02 11:10:09 -04009133 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9134 "0339 Abort xri x%x, original iotag x%x, "
9135 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009136 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009137 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009138 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009139 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009140
James Smartd7c255b2008-08-24 21:50:00 -04009141 if (retval)
9142 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009143
9144 /*
9145 * Caller to this routine should check for IOCB_ERROR
9146 * and handle it properly. This routine no longer removes
9147 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9148 */
9149 return retval;
9150}
9151
9152/**
9153 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9154 * @phba: Pointer to HBA context object.
9155 * @pring: Pointer to driver SLI ring object.
9156 * @cmdiocb: Pointer to driver command iocb object.
9157 *
9158 * This function issues an abort iocb for the provided command iocb. In case
9159 * of unloading, the abort iocb will not be issued to commands on the ELS
9160 * ring. Instead, the callback function shall be changed to those commands
9161 * so that nothing happens when them finishes. This function is called with
9162 * hbalock held. The function returns 0 when the command iocb is an abort
9163 * request.
9164 **/
9165int
9166lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9167 struct lpfc_iocbq *cmdiocb)
9168{
9169 struct lpfc_vport *vport = cmdiocb->vport;
9170 int retval = IOCB_ERROR;
9171 IOCB_t *icmd = NULL;
9172
9173 /*
9174 * There are certain command types we don't want to abort. And we
9175 * don't want to abort commands that are already in the process of
9176 * being aborted.
9177 */
9178 icmd = &cmdiocb->iocb;
9179 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9180 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9181 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9182 return 0;
9183
9184 /*
9185 * If we're unloading, don't abort iocb on the ELS ring, but change
9186 * the callback so that nothing happens when it finishes.
9187 */
9188 if ((vport->load_flag & FC_UNLOADING) &&
9189 (pring->ringno == LPFC_ELS_RING)) {
9190 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9191 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9192 else
9193 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9194 goto abort_iotag_exit;
9195 }
9196
9197 /* Now, we try to issue the abort to the cmdiocb out */
9198 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9199
James Smart07951072007-04-25 09:51:38 -04009200abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009201 /*
9202 * Caller to this routine should check for IOCB_ERROR
9203 * and handle it properly. This routine no longer removes
9204 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009205 */
James Smart2e0fef82007-06-17 19:56:36 -05009206 return retval;
dea31012005-04-17 16:05:31 -05009207}
9208
James Smarte59058c2008-08-24 21:49:00 -04009209/**
James Smart5af5eee2010-10-22 11:06:38 -04009210 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9211 * @phba: Pointer to HBA context object.
9212 * @pring: Pointer to driver SLI ring object.
9213 *
9214 * This function aborts all iocbs in the given ring and frees all the iocb
9215 * objects in txq. This function issues abort iocbs unconditionally for all
9216 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9217 * to complete before the return of this function. The caller is not required
9218 * to hold any locks.
9219 **/
9220static void
9221lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9222{
9223 LIST_HEAD(completions);
9224 struct lpfc_iocbq *iocb, *next_iocb;
9225
9226 if (pring->ringno == LPFC_ELS_RING)
9227 lpfc_fabric_abort_hba(phba);
9228
9229 spin_lock_irq(&phba->hbalock);
9230
9231 /* Take off all the iocbs on txq for cancelling */
9232 list_splice_init(&pring->txq, &completions);
9233 pring->txq_cnt = 0;
9234
9235 /* Next issue ABTS for everything on the txcmplq */
9236 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9237 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9238
9239 spin_unlock_irq(&phba->hbalock);
9240
9241 /* Cancel all the IOCBs from the completions list */
9242 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9243 IOERR_SLI_ABORTED);
9244}
9245
9246/**
9247 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9248 * @phba: pointer to lpfc HBA data structure.
9249 *
9250 * This routine will abort all pending and outstanding iocbs to an HBA.
9251 **/
9252void
9253lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9254{
9255 struct lpfc_sli *psli = &phba->sli;
9256 struct lpfc_sli_ring *pring;
9257 int i;
9258
9259 for (i = 0; i < psli->num_rings; i++) {
9260 pring = &psli->ring[i];
9261 lpfc_sli_iocb_ring_abort(phba, pring);
9262 }
9263}
9264
9265/**
James Smart3621a712009-04-06 18:47:14 -04009266 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009267 * @iocbq: Pointer to driver iocb object.
9268 * @vport: Pointer to driver virtual port object.
9269 * @tgt_id: SCSI ID of the target.
9270 * @lun_id: LUN ID of the scsi device.
9271 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9272 *
James Smart3621a712009-04-06 18:47:14 -04009273 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009274 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9275 * 0 if the filtering criteria is met for the given iocb and will return
9276 * 1 if the filtering criteria is not met.
9277 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9278 * given iocb is for the SCSI device specified by vport, tgt_id and
9279 * lun_id parameter.
9280 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9281 * given iocb is for the SCSI target specified by vport and tgt_id
9282 * parameters.
9283 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9284 * given iocb is for the SCSI host associated with the given vport.
9285 * This function is called with no locks held.
9286 **/
dea31012005-04-17 16:05:31 -05009287static int
James Smart51ef4c22007-08-02 11:10:31 -04009288lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9289 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009290 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009291{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009292 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009293 int rc = 1;
9294
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009295 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9296 return rc;
9297
James Smart51ef4c22007-08-02 11:10:31 -04009298 if (iocbq->vport != vport)
9299 return rc;
9300
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009301 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009302
James Smart495a7142008-06-14 22:52:59 -04009303 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009304 return rc;
9305
9306 switch (ctx_cmd) {
9307 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009308 if ((lpfc_cmd->rdata->pnode) &&
9309 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9310 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009311 rc = 0;
9312 break;
9313 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009314 if ((lpfc_cmd->rdata->pnode) &&
9315 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009316 rc = 0;
9317 break;
dea31012005-04-17 16:05:31 -05009318 case LPFC_CTX_HOST:
9319 rc = 0;
9320 break;
9321 default:
9322 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009323 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009324 break;
9325 }
9326
9327 return rc;
9328}
9329
James Smarte59058c2008-08-24 21:49:00 -04009330/**
James Smart3621a712009-04-06 18:47:14 -04009331 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009332 * @vport: Pointer to virtual port.
9333 * @tgt_id: SCSI ID of the target.
9334 * @lun_id: LUN ID of the scsi device.
9335 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9336 *
9337 * This function returns number of FCP commands pending for the vport.
9338 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9339 * commands pending on the vport associated with SCSI device specified
9340 * by tgt_id and lun_id parameters.
9341 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9342 * commands pending on the vport associated with SCSI target specified
9343 * by tgt_id parameter.
9344 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9345 * commands pending on the vport.
9346 * This function returns the number of iocbs which satisfy the filter.
9347 * This function is called without any lock held.
9348 **/
dea31012005-04-17 16:05:31 -05009349int
James Smart51ef4c22007-08-02 11:10:31 -04009350lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9351 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009352{
James Smart51ef4c22007-08-02 11:10:31 -04009353 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009354 struct lpfc_iocbq *iocbq;
9355 int sum, i;
dea31012005-04-17 16:05:31 -05009356
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009357 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9358 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009359
James Smart51ef4c22007-08-02 11:10:31 -04009360 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9361 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009362 sum++;
dea31012005-04-17 16:05:31 -05009363 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009364
dea31012005-04-17 16:05:31 -05009365 return sum;
9366}
9367
James Smarte59058c2008-08-24 21:49:00 -04009368/**
James Smart3621a712009-04-06 18:47:14 -04009369 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009370 * @phba: Pointer to HBA context object
9371 * @cmdiocb: Pointer to command iocb object.
9372 * @rspiocb: Pointer to response iocb object.
9373 *
9374 * This function is called when an aborted FCP iocb completes. This
9375 * function is called by the ring event handler with no lock held.
9376 * This function frees the iocb.
9377 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009378void
James Smart2e0fef82007-06-17 19:56:36 -05009379lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9380 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009381{
James Smartcb69f7d2011-12-13 13:21:57 -05009382 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9383 "3096 ABORT_XRI_CN completing on xri x%x "
9384 "original iotag x%x, abort cmd iotag x%x "
9385 "status 0x%x, reason 0x%x\n",
9386 cmdiocb->iocb.un.acxri.abortContextTag,
9387 cmdiocb->iocb.un.acxri.abortIoTag,
9388 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9389 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009390 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009391 return;
9392}
9393
James Smarte59058c2008-08-24 21:49:00 -04009394/**
James Smart3621a712009-04-06 18:47:14 -04009395 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009396 * @vport: Pointer to virtual port.
9397 * @pring: Pointer to driver SLI ring object.
9398 * @tgt_id: SCSI ID of the target.
9399 * @lun_id: LUN ID of the scsi device.
9400 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9401 *
9402 * This function sends an abort command for every SCSI command
9403 * associated with the given virtual port pending on the ring
9404 * filtered by lpfc_sli_validate_fcp_iocb function.
9405 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9406 * FCP iocbs associated with lun specified by tgt_id and lun_id
9407 * parameters
9408 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9409 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9410 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9411 * FCP iocbs associated with virtual port.
9412 * This function returns number of iocbs it failed to abort.
9413 * This function is called with no locks held.
9414 **/
dea31012005-04-17 16:05:31 -05009415int
James Smart51ef4c22007-08-02 11:10:31 -04009416lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9417 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009418{
James Smart51ef4c22007-08-02 11:10:31 -04009419 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009420 struct lpfc_iocbq *iocbq;
9421 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009422 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009423 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009424 int i;
dea31012005-04-17 16:05:31 -05009425
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009426 for (i = 1; i <= phba->sli.last_iotag; i++) {
9427 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009428
James Smart51ef4c22007-08-02 11:10:31 -04009429 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009430 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009431 continue;
9432
9433 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009434 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009435 if (abtsiocb == NULL) {
9436 errcnt++;
9437 continue;
9438 }
dea31012005-04-17 16:05:31 -05009439
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009440 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009441 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9442 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009443 if (phba->sli_rev == LPFC_SLI_REV4)
9444 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9445 else
9446 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009447 abtsiocb->iocb.ulpLe = 1;
9448 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009449 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009450
James Smart5ffc2662009-11-18 15:39:44 -05009451 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9452 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009453 if (iocbq->iocb_flag & LPFC_IO_FCP)
9454 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009455
James Smart2e0fef82007-06-17 19:56:36 -05009456 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009457 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9458 else
9459 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9460
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009461 /* Setup callback routine and issue the command. */
9462 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009463 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9464 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009465 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009466 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009467 errcnt++;
9468 continue;
9469 }
9470 }
9471
9472 return errcnt;
9473}
9474
James Smarte59058c2008-08-24 21:49:00 -04009475/**
James Smart3621a712009-04-06 18:47:14 -04009476 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009477 * @phba: Pointer to HBA context object.
9478 * @cmdiocbq: Pointer to command iocb.
9479 * @rspiocbq: Pointer to response iocb.
9480 *
9481 * This function is the completion handler for iocbs issued using
9482 * lpfc_sli_issue_iocb_wait function. This function is called by the
9483 * ring event handler function without any lock held. This function
9484 * can be called from both worker thread context and interrupt
9485 * context. This function also can be called from other thread which
9486 * cleans up the SLI layer objects.
9487 * This function copy the contents of the response iocb to the
9488 * response iocb memory object provided by the caller of
9489 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9490 * sleeps for the iocb completion.
9491 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009492static void
9493lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9494 struct lpfc_iocbq *cmdiocbq,
9495 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009496{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009497 wait_queue_head_t *pdone_q;
9498 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009499 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009500
James Smart2e0fef82007-06-17 19:56:36 -05009501 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009502 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9503 if (cmdiocbq->context2 && rspiocbq)
9504 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9505 &rspiocbq->iocb, sizeof(IOCB_t));
9506
James Smart0f65ff62010-02-26 14:14:23 -05009507 /* Set the exchange busy flag for task management commands */
9508 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9509 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9510 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9511 cur_iocbq);
9512 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9513 }
9514
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009515 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009516 if (pdone_q)
9517 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009518 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009519 return;
9520}
9521
James Smarte59058c2008-08-24 21:49:00 -04009522/**
James Smartd11e31d2009-06-10 17:23:06 -04009523 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9524 * @phba: Pointer to HBA context object..
9525 * @piocbq: Pointer to command iocb.
9526 * @flag: Flag to test.
9527 *
9528 * This routine grabs the hbalock and then test the iocb_flag to
9529 * see if the passed in flag is set.
9530 * Returns:
9531 * 1 if flag is set.
9532 * 0 if flag is not set.
9533 **/
9534static int
9535lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9536 struct lpfc_iocbq *piocbq, uint32_t flag)
9537{
9538 unsigned long iflags;
9539 int ret;
9540
9541 spin_lock_irqsave(&phba->hbalock, iflags);
9542 ret = piocbq->iocb_flag & flag;
9543 spin_unlock_irqrestore(&phba->hbalock, iflags);
9544 return ret;
9545
9546}
9547
9548/**
James Smart3621a712009-04-06 18:47:14 -04009549 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009550 * @phba: Pointer to HBA context object..
9551 * @pring: Pointer to sli ring.
9552 * @piocb: Pointer to command iocb.
9553 * @prspiocbq: Pointer to response iocb.
9554 * @timeout: Timeout in number of seconds.
9555 *
9556 * This function issues the iocb to firmware and waits for the
9557 * iocb to complete. If the iocb command is not
9558 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9559 * Caller should not free the iocb resources if this function
9560 * returns IOCB_TIMEDOUT.
9561 * The function waits for the iocb completion using an
9562 * non-interruptible wait.
9563 * This function will sleep while waiting for iocb completion.
9564 * So, this function should not be called from any context which
9565 * does not allow sleeping. Due to the same reason, this function
9566 * cannot be called with interrupt disabled.
9567 * This function assumes that the iocb completions occur while
9568 * this function sleep. So, this function cannot be called from
9569 * the thread which process iocb completion for this ring.
9570 * This function clears the iocb_flag of the iocb object before
9571 * issuing the iocb and the iocb completion handler sets this
9572 * flag and wakes this thread when the iocb completes.
9573 * The contents of the response iocb will be copied to prspiocbq
9574 * by the completion handler when the command completes.
9575 * This function returns IOCB_SUCCESS when success.
9576 * This function is called with no lock held.
9577 **/
dea31012005-04-17 16:05:31 -05009578int
James Smart2e0fef82007-06-17 19:56:36 -05009579lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009580 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009581 struct lpfc_iocbq *piocb,
9582 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009583 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009584{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009585 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009586 long timeleft, timeout_req = 0;
9587 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009588 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009589 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009590 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009591 * If the caller has provided a response iocbq buffer, then context2
9592 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009593 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009594 if (prspiocbq) {
9595 if (piocb->context2)
9596 return IOCB_ERROR;
9597 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009598 }
9599
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009600 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9601 piocb->context_un.wait_queue = &done_q;
9602 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009603
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009604 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009605 if (lpfc_readl(phba->HCregaddr, &creg_val))
9606 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009607 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9608 writel(creg_val, phba->HCregaddr);
9609 readl(phba->HCregaddr); /* flush */
9610 }
9611
James Smart2a9bf3d2010-06-07 15:24:45 -04009612 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9613 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009614 if (retval == IOCB_SUCCESS) {
9615 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009616 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009617 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009618 timeout_req);
dea31012005-04-17 16:05:31 -05009619
James Smart7054a602007-04-25 09:52:34 -04009620 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9621 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009622 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009623 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009625 "0338 IOCB wait timeout error - no "
9626 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009627 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009628 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009630 "0330 IOCB wake NOT set, "
9631 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009632 timeout, (timeleft / jiffies));
9633 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009634 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009635 } else if (retval == IOCB_BUSY) {
9636 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9637 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9638 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9639 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009640 } else {
9641 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009642 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009643 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009644 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009645 }
9646
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009647 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009648 if (lpfc_readl(phba->HCregaddr, &creg_val))
9649 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009650 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9651 writel(creg_val, phba->HCregaddr);
9652 readl(phba->HCregaddr); /* flush */
9653 }
9654
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009655 if (prspiocbq)
9656 piocb->context2 = NULL;
9657
9658 piocb->context_un.wait_queue = NULL;
9659 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009660 return retval;
9661}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009662
James Smarte59058c2008-08-24 21:49:00 -04009663/**
James Smart3621a712009-04-06 18:47:14 -04009664 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009665 * @phba: Pointer to HBA context object.
9666 * @pmboxq: Pointer to driver mailbox object.
9667 * @timeout: Timeout in number of seconds.
9668 *
9669 * This function issues the mailbox to firmware and waits for the
9670 * mailbox command to complete. If the mailbox command is not
9671 * completed within timeout seconds, it returns MBX_TIMEOUT.
9672 * The function waits for the mailbox completion using an
9673 * interruptible wait. If the thread is woken up due to a
9674 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9675 * should not free the mailbox resources, if this function returns
9676 * MBX_TIMEOUT.
9677 * This function will sleep while waiting for mailbox completion.
9678 * So, this function should not be called from any context which
9679 * does not allow sleeping. Due to the same reason, this function
9680 * cannot be called with interrupt disabled.
9681 * This function assumes that the mailbox completion occurs while
9682 * this function sleep. So, this function cannot be called from
9683 * the worker thread which processes mailbox completion.
9684 * This function is called in the context of HBA management
9685 * applications.
9686 * This function returns MBX_SUCCESS when successful.
9687 * This function is called with no lock held.
9688 **/
dea31012005-04-17 16:05:31 -05009689int
James Smart2e0fef82007-06-17 19:56:36 -05009690lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009691 uint32_t timeout)
9692{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009693 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009694 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009695 unsigned long flag;
dea31012005-04-17 16:05:31 -05009696
9697 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009698 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009699 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009700
James Smart495a7142008-06-14 22:52:59 -04009701 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009702 /* setup wake call as IOCB callback */
9703 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9704 /* setup context field to pass wait_queue pointer to wake function */
9705 pmboxq->context1 = &done_q;
9706
dea31012005-04-17 16:05:31 -05009707 /* now issue the command */
9708 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009709 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009710 wait_event_interruptible_timeout(done_q,
9711 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9712 timeout * HZ);
9713
James Smart858c9f62007-06-17 19:56:39 -05009714 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009715 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009716 /*
9717 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9718 * else do not free the resources.
9719 */
James Smartd7c47992010-06-08 18:31:54 -04009720 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009721 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009722 lpfc_sli4_swap_str(phba, pmboxq);
9723 } else {
James Smart7054a602007-04-25 09:52:34 -04009724 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009725 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9726 }
9727 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009728 }
9729
dea31012005-04-17 16:05:31 -05009730 return retval;
9731}
9732
James Smarte59058c2008-08-24 21:49:00 -04009733/**
James Smart3772a992009-05-22 14:50:54 -04009734 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009735 * @phba: Pointer to HBA context.
9736 *
James Smart3772a992009-05-22 14:50:54 -04009737 * This function is called to shutdown the driver's mailbox sub-system.
9738 * It first marks the mailbox sub-system is in a block state to prevent
9739 * the asynchronous mailbox command from issued off the pending mailbox
9740 * command queue. If the mailbox command sub-system shutdown is due to
9741 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9742 * the mailbox sub-system flush routine to forcefully bring down the
9743 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9744 * as with offline or HBA function reset), this routine will wait for the
9745 * outstanding mailbox command to complete before invoking the mailbox
9746 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009747 **/
James Smart3772a992009-05-22 14:50:54 -04009748void
9749lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009750{
James Smart3772a992009-05-22 14:50:54 -04009751 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009752 unsigned long timeout;
9753
James Smarta183a152011-10-10 21:32:43 -04009754 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009755 spin_lock_irq(&phba->hbalock);
9756 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9757 spin_unlock_irq(&phba->hbalock);
9758
9759 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9760 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009761 /* Determine how long we might wait for the active mailbox
9762 * command to be gracefully completed by firmware.
9763 */
James Smarta183a152011-10-10 21:32:43 -04009764 if (phba->sli.mbox_active)
9765 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9766 phba->sli.mbox_active) *
9767 1000) + jiffies;
9768 spin_unlock_irq(&phba->hbalock);
9769
James Smart3772a992009-05-22 14:50:54 -04009770 while (phba->sli.mbox_active) {
9771 /* Check active mailbox complete status every 2ms */
9772 msleep(2);
9773 if (time_after(jiffies, timeout))
9774 /* Timeout, let the mailbox flush routine to
9775 * forcefully release active mailbox command
9776 */
9777 break;
9778 }
9779 }
9780 lpfc_sli_mbox_sys_flush(phba);
9781}
9782
9783/**
9784 * lpfc_sli_eratt_read - read sli-3 error attention events
9785 * @phba: Pointer to HBA context.
9786 *
9787 * This function is called to read the SLI3 device error attention registers
9788 * for possible error attention events. The caller must hold the hostlock
9789 * with spin_lock_irq().
9790 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009791 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009792 * Register and returns 0 otherwise.
9793 **/
9794static int
9795lpfc_sli_eratt_read(struct lpfc_hba *phba)
9796{
James Smarted957682007-06-17 19:56:37 -05009797 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009798
James Smart3772a992009-05-22 14:50:54 -04009799 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009800 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9801 goto unplug_err;
9802
James Smart3772a992009-05-22 14:50:54 -04009803 if (ha_copy & HA_ERATT) {
9804 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009805 if (lpfc_sli_read_hs(phba))
9806 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009807
James Smart3772a992009-05-22 14:50:54 -04009808 /* Check if there is a deferred error condition is active */
9809 if ((HS_FFER1 & phba->work_hs) &&
9810 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009811 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009812 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009813 /* Clear all interrupt enable conditions */
9814 writel(0, phba->HCregaddr);
9815 readl(phba->HCregaddr);
9816 }
9817
9818 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009819 phba->work_ha |= HA_ERATT;
9820 /* Indicate polling handles this ERATT */
9821 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009822 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009823 }
James Smart3772a992009-05-22 14:50:54 -04009824 return 0;
James Smart9940b972011-03-11 16:06:12 -05009825
9826unplug_err:
9827 /* Set the driver HS work bitmap */
9828 phba->work_hs |= UNPLUG_ERR;
9829 /* Set the driver HA work bitmap */
9830 phba->work_ha |= HA_ERATT;
9831 /* Indicate polling handles this ERATT */
9832 phba->hba_flag |= HBA_ERATT_HANDLED;
9833 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009834}
9835
James Smarte59058c2008-08-24 21:49:00 -04009836/**
James Smartda0436e2009-05-22 14:51:39 -04009837 * lpfc_sli4_eratt_read - read sli-4 error attention events
9838 * @phba: Pointer to HBA context.
9839 *
9840 * This function is called to read the SLI4 device error attention registers
9841 * for possible error attention events. The caller must hold the hostlock
9842 * with spin_lock_irq().
9843 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009844 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009845 * Register and returns 0 otherwise.
9846 **/
9847static int
9848lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9849{
9850 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009851 uint32_t if_type, portsmphr;
9852 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009853
James Smart2fcee4b2010-12-15 17:57:46 -05009854 /*
9855 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009856 * registers for error attention. This can be changed later.
9857 */
James Smart2fcee4b2010-12-15 17:57:46 -05009858 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9859 switch (if_type) {
9860 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009861 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9862 &uerr_sta_lo) ||
9863 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9864 &uerr_sta_hi)) {
9865 phba->work_hs |= UNPLUG_ERR;
9866 phba->work_ha |= HA_ERATT;
9867 phba->hba_flag |= HBA_ERATT_HANDLED;
9868 return 1;
9869 }
James Smart2fcee4b2010-12-15 17:57:46 -05009870 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9871 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9873 "1423 HBA Unrecoverable error: "
9874 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9875 "ue_mask_lo_reg=0x%x, "
9876 "ue_mask_hi_reg=0x%x\n",
9877 uerr_sta_lo, uerr_sta_hi,
9878 phba->sli4_hba.ue_mask_lo,
9879 phba->sli4_hba.ue_mask_hi);
9880 phba->work_status[0] = uerr_sta_lo;
9881 phba->work_status[1] = uerr_sta_hi;
9882 phba->work_ha |= HA_ERATT;
9883 phba->hba_flag |= HBA_ERATT_HANDLED;
9884 return 1;
9885 }
9886 break;
9887 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009888 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9889 &portstat_reg.word0) ||
9890 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9891 &portsmphr)){
9892 phba->work_hs |= UNPLUG_ERR;
9893 phba->work_ha |= HA_ERATT;
9894 phba->hba_flag |= HBA_ERATT_HANDLED;
9895 return 1;
9896 }
James Smart2fcee4b2010-12-15 17:57:46 -05009897 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9898 phba->work_status[0] =
9899 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9900 phba->work_status[1] =
9901 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9903 "2885 Port Error Detected: "
9904 "port status reg 0x%x, "
9905 "port smphr reg 0x%x, "
9906 "error 1=0x%x, error 2=0x%x\n",
9907 portstat_reg.word0,
9908 portsmphr,
9909 phba->work_status[0],
9910 phba->work_status[1]);
9911 phba->work_ha |= HA_ERATT;
9912 phba->hba_flag |= HBA_ERATT_HANDLED;
9913 return 1;
9914 }
9915 break;
9916 case LPFC_SLI_INTF_IF_TYPE_1:
9917 default:
James Smarta747c9c2009-11-18 15:41:10 -05009918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009919 "2886 HBA Error Attention on unsupported "
9920 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009921 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009922 }
James Smart2fcee4b2010-12-15 17:57:46 -05009923
James Smartda0436e2009-05-22 14:51:39 -04009924 return 0;
9925}
9926
9927/**
James Smart3621a712009-04-06 18:47:14 -04009928 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009929 * @phba: Pointer to HBA context.
9930 *
James Smart3772a992009-05-22 14:50:54 -04009931 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009932 * error attention register bit for error attention events.
9933 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009934 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009935 * Register and returns 0 otherwise.
9936 **/
9937int
9938lpfc_sli_check_eratt(struct lpfc_hba *phba)
9939{
9940 uint32_t ha_copy;
9941
9942 /* If somebody is waiting to handle an eratt, don't process it
9943 * here. The brdkill function will do this.
9944 */
9945 if (phba->link_flag & LS_IGNORE_ERATT)
9946 return 0;
9947
9948 /* Check if interrupt handler handles this ERATT */
9949 spin_lock_irq(&phba->hbalock);
9950 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9951 /* Interrupt handler has handled ERATT */
9952 spin_unlock_irq(&phba->hbalock);
9953 return 0;
9954 }
9955
James Smarta257bf92009-04-06 18:48:10 -04009956 /*
9957 * If there is deferred error attention, do not check for error
9958 * attention
9959 */
9960 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9961 spin_unlock_irq(&phba->hbalock);
9962 return 0;
9963 }
9964
James Smart3772a992009-05-22 14:50:54 -04009965 /* If PCI channel is offline, don't process it */
9966 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04009967 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009968 return 0;
9969 }
9970
9971 switch (phba->sli_rev) {
9972 case LPFC_SLI_REV2:
9973 case LPFC_SLI_REV3:
9974 /* Read chip Host Attention (HA) register */
9975 ha_copy = lpfc_sli_eratt_read(phba);
9976 break;
James Smartda0436e2009-05-22 14:51:39 -04009977 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05009978 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04009979 ha_copy = lpfc_sli4_eratt_read(phba);
9980 break;
James Smart3772a992009-05-22 14:50:54 -04009981 default:
9982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9983 "0299 Invalid SLI revision (%d)\n",
9984 phba->sli_rev);
9985 ha_copy = 0;
9986 break;
James Smart93996272008-08-24 21:50:30 -04009987 }
9988 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009989
9990 return ha_copy;
9991}
9992
9993/**
9994 * lpfc_intr_state_check - Check device state for interrupt handling
9995 * @phba: Pointer to HBA context.
9996 *
9997 * This inline routine checks whether a device or its PCI slot is in a state
9998 * that the interrupt should be handled.
9999 *
10000 * This function returns 0 if the device or the PCI slot is in a state that
10001 * interrupt should be handled, otherwise -EIO.
10002 */
10003static inline int
10004lpfc_intr_state_check(struct lpfc_hba *phba)
10005{
10006 /* If the pci channel is offline, ignore all the interrupts */
10007 if (unlikely(pci_channel_offline(phba->pcidev)))
10008 return -EIO;
10009
10010 /* Update device level interrupt statistics */
10011 phba->sli.slistat.sli_intr++;
10012
10013 /* Ignore all interrupts during initialization. */
10014 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10015 return -EIO;
10016
James Smart93996272008-08-24 21:50:30 -040010017 return 0;
10018}
10019
10020/**
James Smart3772a992009-05-22 14:50:54 -040010021 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010022 * @irq: Interrupt number.
10023 * @dev_id: The device context pointer.
10024 *
James Smart93996272008-08-24 21:50:30 -040010025 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010026 * service routine when device with SLI-3 interface spec is enabled with
10027 * MSI-X multi-message interrupt mode and there are slow-path events in
10028 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10029 * interrupt mode, this function is called as part of the device-level
10030 * interrupt handler. When the PCI slot is in error recovery or the HBA
10031 * is undergoing initialization, the interrupt handler will not process
10032 * the interrupt. The link attention and ELS ring attention events are
10033 * handled by the worker thread. The interrupt handler signals the worker
10034 * thread and returns for these events. This function is called without
10035 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010036 * structures.
10037 *
10038 * This function returns IRQ_HANDLED when interrupt is handled else it
10039 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010040 **/
dea31012005-04-17 16:05:31 -050010041irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010042lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010043{
James Smart2e0fef82007-06-17 19:56:36 -050010044 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010045 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010046 uint32_t work_ha_copy;
10047 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010048 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010049 uint32_t control;
10050
James Smart92d7f7b2007-06-17 19:56:38 -050010051 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010052 struct lpfc_vport *vport;
10053 struct lpfc_nodelist *ndlp;
10054 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010055 LPFC_MBOXQ_t *pmb;
10056 int rc;
10057
dea31012005-04-17 16:05:31 -050010058 /*
10059 * Get the driver's phba structure from the dev_id and
10060 * assume the HBA is not interrupting.
10061 */
James Smart93996272008-08-24 21:50:30 -040010062 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010063
10064 if (unlikely(!phba))
10065 return IRQ_NONE;
10066
dea31012005-04-17 16:05:31 -050010067 /*
James Smart93996272008-08-24 21:50:30 -040010068 * Stuff needs to be attented to when this function is invoked as an
10069 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010070 */
James Smart93996272008-08-24 21:50:30 -040010071 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010072 /* Check device state for handling interrupt */
10073 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010074 return IRQ_NONE;
10075 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010076 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010077 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10078 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010079 /* If somebody is waiting to handle an eratt don't process it
10080 * here. The brdkill function will do this.
10081 */
10082 if (phba->link_flag & LS_IGNORE_ERATT)
10083 ha_copy &= ~HA_ERATT;
10084 /* Check the need for handling ERATT in interrupt handler */
10085 if (ha_copy & HA_ERATT) {
10086 if (phba->hba_flag & HBA_ERATT_HANDLED)
10087 /* ERATT polling has handled ERATT */
10088 ha_copy &= ~HA_ERATT;
10089 else
10090 /* Indicate interrupt handler handles ERATT */
10091 phba->hba_flag |= HBA_ERATT_HANDLED;
10092 }
James Smarta257bf92009-04-06 18:48:10 -040010093
10094 /*
10095 * If there is deferred error attention, do not check for any
10096 * interrupt.
10097 */
10098 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010099 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010100 return IRQ_NONE;
10101 }
10102
James Smart93996272008-08-24 21:50:30 -040010103 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010104 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10105 goto unplug_error;
10106
James Smarta747c9c2009-11-18 15:41:10 -050010107 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10108 HC_LAINT_ENA | HC_ERINT_ENA),
10109 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010110 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10111 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010112 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010113 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010114 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010115 } else
10116 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010117
dea31012005-04-17 16:05:31 -050010118 work_ha_copy = ha_copy & phba->work_ha_mask;
10119
James Smart93996272008-08-24 21:50:30 -040010120 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010121 if (work_ha_copy & HA_LATT) {
10122 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10123 /*
10124 * Turn off Link Attention interrupts
10125 * until CLEAR_LA done
10126 */
James Smart5b75da22008-12-04 22:39:35 -050010127 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010128 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010129 if (lpfc_readl(phba->HCregaddr, &control))
10130 goto unplug_error;
dea31012005-04-17 16:05:31 -050010131 control &= ~HC_LAINT_ENA;
10132 writel(control, phba->HCregaddr);
10133 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010134 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010135 }
10136 else
10137 work_ha_copy &= ~HA_LATT;
10138 }
10139
James Smart93996272008-08-24 21:50:30 -040010140 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010141 /*
10142 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10143 * the only slow ring.
10144 */
10145 status = (work_ha_copy &
10146 (HA_RXMASK << (4*LPFC_ELS_RING)));
10147 status >>= (4*LPFC_ELS_RING);
10148 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010149 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010150 if (lpfc_readl(phba->HCregaddr, &control))
10151 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010152
10153 lpfc_debugfs_slow_ring_trc(phba,
10154 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10155 control, status,
10156 (uint32_t)phba->sli.slistat.sli_intr);
10157
James Smart858c9f62007-06-17 19:56:39 -050010158 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010159 lpfc_debugfs_slow_ring_trc(phba,
10160 "ISR Disable ring:"
10161 "pwork:x%x hawork:x%x wait:x%x",
10162 phba->work_ha, work_ha_copy,
10163 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010164 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010165
James Smart858c9f62007-06-17 19:56:39 -050010166 control &=
10167 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010168 writel(control, phba->HCregaddr);
10169 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010170 }
James Smarta58cbd52007-08-02 11:09:43 -040010171 else {
10172 lpfc_debugfs_slow_ring_trc(phba,
10173 "ISR slow ring: pwork:"
10174 "x%x hawork:x%x wait:x%x",
10175 phba->work_ha, work_ha_copy,
10176 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010177 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010178 }
James Smart5b75da22008-12-04 22:39:35 -050010179 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010180 }
10181 }
James Smart5b75da22008-12-04 22:39:35 -050010182 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010183 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010184 if (lpfc_sli_read_hs(phba))
10185 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010186 /*
10187 * Check if there is a deferred error condition
10188 * is active
10189 */
10190 if ((HS_FFER1 & phba->work_hs) &&
10191 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010192 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10193 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010194 phba->hba_flag |= DEFER_ERATT;
10195 /* Clear all interrupt enable conditions */
10196 writel(0, phba->HCregaddr);
10197 readl(phba->HCregaddr);
10198 }
10199 }
10200
James Smart93996272008-08-24 21:50:30 -040010201 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010202 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010203 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010204 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010205 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010206
10207 /* First check out the status word */
10208 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10209 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010210 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010211 /*
10212 * Stray Mailbox Interrupt, mbxCommand <cmd>
10213 * mbxStatus <status>
10214 */
James Smart09372822008-01-11 01:52:54 -050010215 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010216 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010217 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010218 "Interrupt mbxCommand x%x "
10219 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010220 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010221 pmbox->mbxCommand,
10222 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010223 /* clear mailbox attention bit */
10224 work_ha_copy &= ~HA_MBATT;
10225 } else {
James Smart97eab632008-04-07 10:16:05 -040010226 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010227 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010228 phba->last_completion_time = jiffies;
10229 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010230 if (pmb->mbox_cmpl) {
10231 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10232 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010233 if (pmb->out_ext_byte_len &&
10234 pmb->context2)
10235 lpfc_sli_pcimem_bcopy(
10236 phba->mbox_ext,
10237 pmb->context2,
10238 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010239 }
James Smart09372822008-01-11 01:52:54 -050010240 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10241 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10242
10243 lpfc_debugfs_disc_trc(vport,
10244 LPFC_DISC_TRC_MBOX_VPORT,
10245 "MBOX dflt rpi: : "
10246 "status:x%x rpi:x%x",
10247 (uint32_t)pmbox->mbxStatus,
10248 pmbox->un.varWords[0], 0);
10249
10250 if (!pmbox->mbxStatus) {
10251 mp = (struct lpfc_dmabuf *)
10252 (pmb->context1);
10253 ndlp = (struct lpfc_nodelist *)
10254 pmb->context2;
10255
10256 /* Reg_LOGIN of dflt RPI was
10257 * successful. new lets get
10258 * rid of the RPI using the
10259 * same mbox buffer.
10260 */
10261 lpfc_unreg_login(phba,
10262 vport->vpi,
10263 pmbox->un.varWords[0],
10264 pmb);
10265 pmb->mbox_cmpl =
10266 lpfc_mbx_cmpl_dflt_rpi;
10267 pmb->context1 = mp;
10268 pmb->context2 = ndlp;
10269 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010270 rc = lpfc_sli_issue_mbox(phba,
10271 pmb,
10272 MBX_NOWAIT);
10273 if (rc != MBX_BUSY)
10274 lpfc_printf_log(phba,
10275 KERN_ERR,
10276 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010277 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010278 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010279 if (rc != MBX_NOT_FINISHED)
10280 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010281 }
10282 }
James Smart5b75da22008-12-04 22:39:35 -050010283 spin_lock_irqsave(
10284 &phba->pport->work_port_lock,
10285 iflag);
James Smart09372822008-01-11 01:52:54 -050010286 phba->pport->work_port_events &=
10287 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010288 spin_unlock_irqrestore(
10289 &phba->pport->work_port_lock,
10290 iflag);
James Smart09372822008-01-11 01:52:54 -050010291 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010292 }
James Smart97eab632008-04-07 10:16:05 -040010293 } else
James Smart5b75da22008-12-04 22:39:35 -050010294 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010295
James Smart92d7f7b2007-06-17 19:56:38 -050010296 if ((work_ha_copy & HA_MBATT) &&
10297 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010298send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010299 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010300 do {
10301 rc = lpfc_sli_issue_mbox(phba, NULL,
10302 MBX_NOWAIT);
10303 } while (rc == MBX_NOT_FINISHED);
10304 if (rc != MBX_SUCCESS)
10305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10306 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010307 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010308 }
10309
James Smart5b75da22008-12-04 22:39:35 -050010310 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010311 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010313 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010314 }
James Smart93996272008-08-24 21:50:30 -040010315 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010316unplug_error:
10317 spin_unlock_irqrestore(&phba->hbalock, iflag);
10318 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010319
James Smart3772a992009-05-22 14:50:54 -040010320} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010321
10322/**
James Smart3772a992009-05-22 14:50:54 -040010323 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010324 * @irq: Interrupt number.
10325 * @dev_id: The device context pointer.
10326 *
10327 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010328 * service routine when device with SLI-3 interface spec is enabled with
10329 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10330 * ring event in the HBA. However, when the device is enabled with either
10331 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10332 * device-level interrupt handler. When the PCI slot is in error recovery
10333 * or the HBA is undergoing initialization, the interrupt handler will not
10334 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10335 * the intrrupt context. This function is called without any lock held.
10336 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010337 *
10338 * This function returns IRQ_HANDLED when interrupt is handled else it
10339 * returns IRQ_NONE.
10340 **/
10341irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010342lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010343{
10344 struct lpfc_hba *phba;
10345 uint32_t ha_copy;
10346 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010347 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010348
10349 /* Get the driver's phba structure from the dev_id and
10350 * assume the HBA is not interrupting.
10351 */
10352 phba = (struct lpfc_hba *) dev_id;
10353
10354 if (unlikely(!phba))
10355 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010356
10357 /*
James Smart93996272008-08-24 21:50:30 -040010358 * Stuff needs to be attented to when this function is invoked as an
10359 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010360 */
James Smart93996272008-08-24 21:50:30 -040010361 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010362 /* Check device state for handling interrupt */
10363 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010364 return IRQ_NONE;
10365 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010366 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10367 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010368 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010369 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010370 /*
10371 * If there is deferred error attention, do not check for
10372 * any interrupt.
10373 */
10374 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010375 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010376 return IRQ_NONE;
10377 }
James Smart93996272008-08-24 21:50:30 -040010378 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10379 phba->HAregaddr);
10380 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010381 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010382 } else
10383 ha_copy = phba->ha_copy;
10384
10385 /*
10386 * Process all events on FCP ring. Take the optimized path for FCP IO.
10387 */
10388 ha_copy &= ~(phba->work_ha_mask);
10389
10390 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010391 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010392 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010393 lpfc_sli_handle_fast_ring_event(phba,
10394 &phba->sli.ring[LPFC_FCP_RING],
10395 status);
James Smarta4bc3372006-12-02 13:34:16 -050010396
10397 if (phba->cfg_multi_ring_support == 2) {
10398 /*
James Smart93996272008-08-24 21:50:30 -040010399 * Process all events on extra ring. Take the optimized path
10400 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010401 */
James Smart93996272008-08-24 21:50:30 -040010402 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010403 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010404 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010405 lpfc_sli_handle_fast_ring_event(phba,
10406 &phba->sli.ring[LPFC_EXTRA_RING],
10407 status);
10408 }
10409 }
dea31012005-04-17 16:05:31 -050010410 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010411} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010412
James Smart93996272008-08-24 21:50:30 -040010413/**
James Smart3772a992009-05-22 14:50:54 -040010414 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010415 * @irq: Interrupt number.
10416 * @dev_id: The device context pointer.
10417 *
James Smart3772a992009-05-22 14:50:54 -040010418 * This function is the HBA device-level interrupt handler to device with
10419 * SLI-3 interface spec, called from the PCI layer when either MSI or
10420 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10421 * requires driver attention. This function invokes the slow-path interrupt
10422 * attention handling function and fast-path interrupt attention handling
10423 * function in turn to process the relevant HBA attention events. This
10424 * function is called without any lock held. It gets the hbalock to access
10425 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010426 *
10427 * This function returns IRQ_HANDLED when interrupt is handled, else it
10428 * returns IRQ_NONE.
10429 **/
10430irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010431lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010432{
10433 struct lpfc_hba *phba;
10434 irqreturn_t sp_irq_rc, fp_irq_rc;
10435 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010436 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010437
10438 /*
10439 * Get the driver's phba structure from the dev_id and
10440 * assume the HBA is not interrupting.
10441 */
10442 phba = (struct lpfc_hba *) dev_id;
10443
10444 if (unlikely(!phba))
10445 return IRQ_NONE;
10446
James Smart3772a992009-05-22 14:50:54 -040010447 /* Check device state for handling interrupt */
10448 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010449 return IRQ_NONE;
10450
10451 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010452 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10453 spin_unlock(&phba->hbalock);
10454 return IRQ_HANDLED;
10455 }
10456
James Smart93996272008-08-24 21:50:30 -040010457 if (unlikely(!phba->ha_copy)) {
10458 spin_unlock(&phba->hbalock);
10459 return IRQ_NONE;
10460 } else if (phba->ha_copy & HA_ERATT) {
10461 if (phba->hba_flag & HBA_ERATT_HANDLED)
10462 /* ERATT polling has handled ERATT */
10463 phba->ha_copy &= ~HA_ERATT;
10464 else
10465 /* Indicate interrupt handler handles ERATT */
10466 phba->hba_flag |= HBA_ERATT_HANDLED;
10467 }
10468
James Smarta257bf92009-04-06 18:48:10 -040010469 /*
10470 * If there is deferred error attention, do not check for any interrupt.
10471 */
10472 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010473 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010474 return IRQ_NONE;
10475 }
10476
James Smart93996272008-08-24 21:50:30 -040010477 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010478 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10479 spin_unlock(&phba->hbalock);
10480 return IRQ_HANDLED;
10481 }
James Smarta747c9c2009-11-18 15:41:10 -050010482 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10483 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10484 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010485 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010486 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010487 readl(phba->HAregaddr); /* flush */
10488 spin_unlock(&phba->hbalock);
10489
10490 /*
10491 * Invokes slow-path host attention interrupt handling as appropriate.
10492 */
10493
10494 /* status of events with mailbox and link attention */
10495 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10496
10497 /* status of events with ELS ring */
10498 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10499 status2 >>= (4*LPFC_ELS_RING);
10500
10501 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010502 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010503 else
10504 sp_irq_rc = IRQ_NONE;
10505
10506 /*
10507 * Invoke fast-path host attention interrupt handling as appropriate.
10508 */
10509
10510 /* status of events with FCP ring */
10511 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10512 status1 >>= (4*LPFC_FCP_RING);
10513
10514 /* status of events with extra ring */
10515 if (phba->cfg_multi_ring_support == 2) {
10516 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10517 status2 >>= (4*LPFC_EXTRA_RING);
10518 } else
10519 status2 = 0;
10520
10521 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010522 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010523 else
10524 fp_irq_rc = IRQ_NONE;
10525
10526 /* Return device-level interrupt handling status */
10527 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010528} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010529
10530/**
10531 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10532 * @phba: pointer to lpfc hba data structure.
10533 *
10534 * This routine is invoked by the worker thread to process all the pending
10535 * SLI4 FCP abort XRI events.
10536 **/
10537void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10538{
10539 struct lpfc_cq_event *cq_event;
10540
10541 /* First, declare the fcp xri abort event has been handled */
10542 spin_lock_irq(&phba->hbalock);
10543 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10544 spin_unlock_irq(&phba->hbalock);
10545 /* Now, handle all the fcp xri abort events */
10546 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10547 /* Get the first event from the head of the event queue */
10548 spin_lock_irq(&phba->hbalock);
10549 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10550 cq_event, struct lpfc_cq_event, list);
10551 spin_unlock_irq(&phba->hbalock);
10552 /* Notify aborted XRI for FCP work queue */
10553 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10554 /* Free the event processed back to the free pool */
10555 lpfc_sli4_cq_event_release(phba, cq_event);
10556 }
10557}
10558
10559/**
10560 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10561 * @phba: pointer to lpfc hba data structure.
10562 *
10563 * This routine is invoked by the worker thread to process all the pending
10564 * SLI4 els abort xri events.
10565 **/
10566void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10567{
10568 struct lpfc_cq_event *cq_event;
10569
10570 /* First, declare the els xri abort event has been handled */
10571 spin_lock_irq(&phba->hbalock);
10572 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10573 spin_unlock_irq(&phba->hbalock);
10574 /* Now, handle all the els xri abort events */
10575 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10576 /* Get the first event from the head of the event queue */
10577 spin_lock_irq(&phba->hbalock);
10578 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10579 cq_event, struct lpfc_cq_event, list);
10580 spin_unlock_irq(&phba->hbalock);
10581 /* Notify aborted XRI for ELS work queue */
10582 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10583 /* Free the event processed back to the free pool */
10584 lpfc_sli4_cq_event_release(phba, cq_event);
10585 }
10586}
10587
James Smart341af102010-01-26 23:07:37 -050010588/**
10589 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10590 * @phba: pointer to lpfc hba data structure
10591 * @pIocbIn: pointer to the rspiocbq
10592 * @pIocbOut: pointer to the cmdiocbq
10593 * @wcqe: pointer to the complete wcqe
10594 *
10595 * This routine transfers the fields of a command iocbq to a response iocbq
10596 * by copying all the IOCB fields from command iocbq and transferring the
10597 * completion status information from the complete wcqe.
10598 **/
James Smart4f774512009-05-22 14:52:35 -040010599static void
James Smart341af102010-01-26 23:07:37 -050010600lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10601 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010602 struct lpfc_iocbq *pIocbOut,
10603 struct lpfc_wcqe_complete *wcqe)
10604{
James Smart341af102010-01-26 23:07:37 -050010605 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010606 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10607
10608 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10609 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010610 /* Map WCQE parameters into irspiocb parameters */
10611 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10612 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10613 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10614 pIocbIn->iocb.un.fcpi.fcpi_parm =
10615 pIocbOut->iocb.un.fcpi.fcpi_parm -
10616 wcqe->total_data_placed;
10617 else
10618 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010619 else {
James Smart4f774512009-05-22 14:52:35 -040010620 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010621 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10622 }
James Smart341af102010-01-26 23:07:37 -050010623
10624 /* Pick up HBA exchange busy condition */
10625 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10626 spin_lock_irqsave(&phba->hbalock, iflags);
10627 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10628 spin_unlock_irqrestore(&phba->hbalock, iflags);
10629 }
James Smart4f774512009-05-22 14:52:35 -040010630}
10631
10632/**
James Smart45ed1192009-10-02 15:17:02 -040010633 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10634 * @phba: Pointer to HBA context object.
10635 * @wcqe: Pointer to work-queue completion queue entry.
10636 *
10637 * This routine handles an ELS work-queue completion event and construct
10638 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10639 * discovery engine to handle.
10640 *
10641 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10642 **/
10643static struct lpfc_iocbq *
10644lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10645 struct lpfc_iocbq *irspiocbq)
10646{
10647 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10648 struct lpfc_iocbq *cmdiocbq;
10649 struct lpfc_wcqe_complete *wcqe;
10650 unsigned long iflags;
10651
10652 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10653 spin_lock_irqsave(&phba->hbalock, iflags);
10654 pring->stats.iocb_event++;
10655 /* Look up the ELS command IOCB and create pseudo response IOCB */
10656 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10657 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10658 spin_unlock_irqrestore(&phba->hbalock, iflags);
10659
10660 if (unlikely(!cmdiocbq)) {
10661 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10662 "0386 ELS complete with no corresponding "
10663 "cmdiocb: iotag (%d)\n",
10664 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10665 lpfc_sli_release_iocbq(phba, irspiocbq);
10666 return NULL;
10667 }
10668
10669 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010670 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010671
10672 return irspiocbq;
10673}
10674
10675/**
James Smart04c68492009-05-22 14:52:52 -040010676 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10677 * @phba: Pointer to HBA context object.
10678 * @cqe: Pointer to mailbox completion queue entry.
10679 *
10680 * This routine process a mailbox completion queue entry with asynchrous
10681 * event.
10682 *
10683 * Return: true if work posted to worker thread, otherwise false.
10684 **/
10685static bool
10686lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10687{
10688 struct lpfc_cq_event *cq_event;
10689 unsigned long iflags;
10690
10691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10692 "0392 Async Event: word0:x%x, word1:x%x, "
10693 "word2:x%x, word3:x%x\n", mcqe->word0,
10694 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10695
10696 /* Allocate a new internal CQ_EVENT entry */
10697 cq_event = lpfc_sli4_cq_event_alloc(phba);
10698 if (!cq_event) {
10699 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10700 "0394 Failed to allocate CQ_EVENT entry\n");
10701 return false;
10702 }
10703
10704 /* Move the CQE into an asynchronous event entry */
10705 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10706 spin_lock_irqsave(&phba->hbalock, iflags);
10707 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10708 /* Set the async event flag */
10709 phba->hba_flag |= ASYNC_EVENT;
10710 spin_unlock_irqrestore(&phba->hbalock, iflags);
10711
10712 return true;
10713}
10714
10715/**
10716 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10717 * @phba: Pointer to HBA context object.
10718 * @cqe: Pointer to mailbox completion queue entry.
10719 *
10720 * This routine process a mailbox completion queue entry with mailbox
10721 * completion event.
10722 *
10723 * Return: true if work posted to worker thread, otherwise false.
10724 **/
10725static bool
10726lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10727{
10728 uint32_t mcqe_status;
10729 MAILBOX_t *mbox, *pmbox;
10730 struct lpfc_mqe *mqe;
10731 struct lpfc_vport *vport;
10732 struct lpfc_nodelist *ndlp;
10733 struct lpfc_dmabuf *mp;
10734 unsigned long iflags;
10735 LPFC_MBOXQ_t *pmb;
10736 bool workposted = false;
10737 int rc;
10738
10739 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10740 if (!bf_get(lpfc_trailer_completed, mcqe))
10741 goto out_no_mqe_complete;
10742
10743 /* Get the reference to the active mbox command */
10744 spin_lock_irqsave(&phba->hbalock, iflags);
10745 pmb = phba->sli.mbox_active;
10746 if (unlikely(!pmb)) {
10747 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10748 "1832 No pending MBOX command to handle\n");
10749 spin_unlock_irqrestore(&phba->hbalock, iflags);
10750 goto out_no_mqe_complete;
10751 }
10752 spin_unlock_irqrestore(&phba->hbalock, iflags);
10753 mqe = &pmb->u.mqe;
10754 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10755 mbox = phba->mbox;
10756 vport = pmb->vport;
10757
10758 /* Reset heartbeat timer */
10759 phba->last_completion_time = jiffies;
10760 del_timer(&phba->sli.mbox_tmo);
10761
10762 /* Move mbox data to caller's mailbox region, do endian swapping */
10763 if (pmb->mbox_cmpl && mbox)
10764 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010765
James Smart73d91e52011-10-10 21:32:10 -040010766 /*
10767 * For mcqe errors, conditionally move a modified error code to
10768 * the mbox so that the error will not be missed.
10769 */
10770 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10771 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10772 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10773 bf_set(lpfc_mqe_status, mqe,
10774 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10775 }
James Smart04c68492009-05-22 14:52:52 -040010776 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10777 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10778 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10779 "MBOX dflt rpi: status:x%x rpi:x%x",
10780 mcqe_status,
10781 pmbox->un.varWords[0], 0);
10782 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10783 mp = (struct lpfc_dmabuf *)(pmb->context1);
10784 ndlp = (struct lpfc_nodelist *)pmb->context2;
10785 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10786 * RID of the PPI using the same mbox buffer.
10787 */
10788 lpfc_unreg_login(phba, vport->vpi,
10789 pmbox->un.varWords[0], pmb);
10790 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10791 pmb->context1 = mp;
10792 pmb->context2 = ndlp;
10793 pmb->vport = vport;
10794 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10795 if (rc != MBX_BUSY)
10796 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10797 LOG_SLI, "0385 rc should "
10798 "have been MBX_BUSY\n");
10799 if (rc != MBX_NOT_FINISHED)
10800 goto send_current_mbox;
10801 }
10802 }
10803 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10804 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10805 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10806
10807 /* There is mailbox completion work to do */
10808 spin_lock_irqsave(&phba->hbalock, iflags);
10809 __lpfc_mbox_cmpl_put(phba, pmb);
10810 phba->work_ha |= HA_MBATT;
10811 spin_unlock_irqrestore(&phba->hbalock, iflags);
10812 workposted = true;
10813
10814send_current_mbox:
10815 spin_lock_irqsave(&phba->hbalock, iflags);
10816 /* Release the mailbox command posting token */
10817 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10818 /* Setting active mailbox pointer need to be in sync to flag clear */
10819 phba->sli.mbox_active = NULL;
10820 spin_unlock_irqrestore(&phba->hbalock, iflags);
10821 /* Wake up worker thread to post the next pending mailbox command */
10822 lpfc_worker_wake_up(phba);
10823out_no_mqe_complete:
10824 if (bf_get(lpfc_trailer_consumed, mcqe))
10825 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10826 return workposted;
10827}
10828
10829/**
10830 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10831 * @phba: Pointer to HBA context object.
10832 * @cqe: Pointer to mailbox completion queue entry.
10833 *
10834 * This routine process a mailbox completion queue entry, it invokes the
10835 * proper mailbox complete handling or asynchrous event handling routine
10836 * according to the MCQE's async bit.
10837 *
10838 * Return: true if work posted to worker thread, otherwise false.
10839 **/
10840static bool
10841lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10842{
10843 struct lpfc_mcqe mcqe;
10844 bool workposted;
10845
10846 /* Copy the mailbox MCQE and convert endian order as needed */
10847 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10848
10849 /* Invoke the proper event handling routine */
10850 if (!bf_get(lpfc_trailer_async, &mcqe))
10851 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10852 else
10853 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10854 return workposted;
10855}
10856
10857/**
James Smart4f774512009-05-22 14:52:35 -040010858 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10859 * @phba: Pointer to HBA context object.
10860 * @wcqe: Pointer to work-queue completion queue entry.
10861 *
10862 * This routine handles an ELS work-queue completion event.
10863 *
10864 * Return: true if work posted to worker thread, otherwise false.
10865 **/
10866static bool
10867lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10868 struct lpfc_wcqe_complete *wcqe)
10869{
James Smart4f774512009-05-22 14:52:35 -040010870 struct lpfc_iocbq *irspiocbq;
10871 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010872 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010873
James Smart45ed1192009-10-02 15:17:02 -040010874 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010875 irspiocbq = lpfc_sli_get_iocbq(phba);
10876 if (!irspiocbq) {
10877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010878 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10879 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10880 pring->txq_cnt, phba->iocb_cnt,
10881 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10882 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010883 return false;
James Smart4f774512009-05-22 14:52:35 -040010884 }
James Smart4f774512009-05-22 14:52:35 -040010885
James Smart45ed1192009-10-02 15:17:02 -040010886 /* Save off the slow-path queue event for work thread to process */
10887 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010888 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010889 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010890 &phba->sli4_hba.sp_queue_event);
10891 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010892 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010893
James Smart45ed1192009-10-02 15:17:02 -040010894 return true;
James Smart4f774512009-05-22 14:52:35 -040010895}
10896
10897/**
10898 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10899 * @phba: Pointer to HBA context object.
10900 * @wcqe: Pointer to work-queue completion queue entry.
10901 *
10902 * This routine handles slow-path WQ entry comsumed event by invoking the
10903 * proper WQ release routine to the slow-path WQ.
10904 **/
10905static void
10906lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10907 struct lpfc_wcqe_release *wcqe)
10908{
10909 /* Check for the slow-path ELS work queue */
10910 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10911 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10912 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10913 else
10914 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10915 "2579 Slow-path wqe consume event carries "
10916 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10917 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10918 phba->sli4_hba.els_wq->queue_id);
10919}
10920
10921/**
10922 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10923 * @phba: Pointer to HBA context object.
10924 * @cq: Pointer to a WQ completion queue.
10925 * @wcqe: Pointer to work-queue completion queue entry.
10926 *
10927 * This routine handles an XRI abort event.
10928 *
10929 * Return: true if work posted to worker thread, otherwise false.
10930 **/
10931static bool
10932lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10933 struct lpfc_queue *cq,
10934 struct sli4_wcqe_xri_aborted *wcqe)
10935{
10936 bool workposted = false;
10937 struct lpfc_cq_event *cq_event;
10938 unsigned long iflags;
10939
10940 /* Allocate a new internal CQ_EVENT entry */
10941 cq_event = lpfc_sli4_cq_event_alloc(phba);
10942 if (!cq_event) {
10943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10944 "0602 Failed to allocate CQ_EVENT entry\n");
10945 return false;
10946 }
10947
10948 /* Move the CQE into the proper xri abort event list */
10949 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10950 switch (cq->subtype) {
10951 case LPFC_FCP:
10952 spin_lock_irqsave(&phba->hbalock, iflags);
10953 list_add_tail(&cq_event->list,
10954 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10955 /* Set the fcp xri abort event flag */
10956 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10957 spin_unlock_irqrestore(&phba->hbalock, iflags);
10958 workposted = true;
10959 break;
10960 case LPFC_ELS:
10961 spin_lock_irqsave(&phba->hbalock, iflags);
10962 list_add_tail(&cq_event->list,
10963 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10964 /* Set the els xri abort event flag */
10965 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10966 spin_unlock_irqrestore(&phba->hbalock, iflags);
10967 workposted = true;
10968 break;
10969 default:
10970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10971 "0603 Invalid work queue CQE subtype (x%x)\n",
10972 cq->subtype);
10973 workposted = false;
10974 break;
10975 }
10976 return workposted;
10977}
10978
10979/**
James Smart4d9ab992009-10-02 15:16:39 -040010980 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040010981 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040010982 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010983 *
James Smart4d9ab992009-10-02 15:16:39 -040010984 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010985 *
10986 * Return: true if work posted to worker thread, otherwise false.
10987 **/
10988static bool
James Smart4d9ab992009-10-02 15:16:39 -040010989lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10990{
10991 bool workposted = false;
10992 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10993 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10994 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040010995 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040010996 unsigned long iflags;
10997
James Smart7851fe22011-07-22 18:36:52 -040010998 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10999 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11000 else
11001 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11002 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011003 goto out;
11004
11005 status = bf_get(lpfc_rcqe_status, rcqe);
11006 switch (status) {
11007 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11009 "2537 Receive Frame Truncated!!\n");
11010 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011011 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011012 spin_lock_irqsave(&phba->hbalock, iflags);
11013 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11014 if (!dma_buf) {
11015 spin_unlock_irqrestore(&phba->hbalock, iflags);
11016 goto out;
11017 }
11018 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11019 /* save off the frame for the word thread to process */
11020 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011021 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011022 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011023 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011024 spin_unlock_irqrestore(&phba->hbalock, iflags);
11025 workposted = true;
11026 break;
11027 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11028 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11029 /* Post more buffers if possible */
11030 spin_lock_irqsave(&phba->hbalock, iflags);
11031 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11032 spin_unlock_irqrestore(&phba->hbalock, iflags);
11033 workposted = true;
11034 break;
11035 }
11036out:
11037 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011038}
11039
11040/**
11041 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11042 * @phba: Pointer to HBA context object.
11043 * @cq: Pointer to the completion queue.
11044 * @wcqe: Pointer to a completion queue entry.
11045 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011046 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011047 * entry.
11048 *
11049 * Return: true if work posted to worker thread, otherwise false.
11050 **/
11051static bool
11052lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011053 struct lpfc_cqe *cqe)
11054{
James Smart45ed1192009-10-02 15:17:02 -040011055 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011056 bool workposted = false;
11057
11058 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011059 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011060
11061 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011062 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011063 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011064 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011065 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011066 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011067 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011068 break;
11069 case CQE_CODE_RELEASE_WQE:
11070 /* Process the WQ release event */
11071 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011072 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011073 break;
11074 case CQE_CODE_XRI_ABORTED:
11075 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011076 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011077 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011078 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011079 break;
James Smart4d9ab992009-10-02 15:16:39 -040011080 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011081 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011082 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011083 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011084 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011085 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011086 break;
James Smart4f774512009-05-22 14:52:35 -040011087 default:
11088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11089 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011090 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011091 break;
11092 }
11093 return workposted;
11094}
11095
11096/**
James Smart4f774512009-05-22 14:52:35 -040011097 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11098 * @phba: Pointer to HBA context object.
11099 * @eqe: Pointer to fast-path event queue entry.
11100 *
11101 * This routine process a event queue entry from the slow-path event queue.
11102 * It will check the MajorCode and MinorCode to determine this is for a
11103 * completion event on a completion queue, if not, an error shall be logged
11104 * and just return. Otherwise, it will get to the corresponding completion
11105 * queue and process all the entries on that completion queue, rearm the
11106 * completion queue, and then return.
11107 *
11108 **/
11109static void
11110lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11111{
11112 struct lpfc_queue *cq = NULL, *childq, *speq;
11113 struct lpfc_cqe *cqe;
11114 bool workposted = false;
11115 int ecount = 0;
11116 uint16_t cqid;
11117
James Smartcb5172e2010-03-15 11:25:07 -040011118 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011119 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11120 "0359 Not a valid slow-path completion "
11121 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011122 bf_get_le32(lpfc_eqe_major_code, eqe),
11123 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011124 return;
11125 }
11126
11127 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011128 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011129
11130 /* Search for completion queue pointer matching this cqid */
11131 speq = phba->sli4_hba.sp_eq;
11132 list_for_each_entry(childq, &speq->child_list, list) {
11133 if (childq->queue_id == cqid) {
11134 cq = childq;
11135 break;
11136 }
11137 }
11138 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011139 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11141 "0365 Slow-path CQ identifier "
11142 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011143 return;
11144 }
11145
11146 /* Process all the entries to the CQ */
11147 switch (cq->type) {
11148 case LPFC_MCQ:
11149 while ((cqe = lpfc_sli4_cq_get(cq))) {
11150 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011151 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011152 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11153 }
11154 break;
11155 case LPFC_WCQ:
11156 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011157 if (cq->subtype == LPFC_FCP)
11158 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11159 cqe);
11160 else
11161 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11162 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011163 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011164 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11165 }
11166 break;
11167 default:
11168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11169 "0370 Invalid completion queue type (%d)\n",
11170 cq->type);
11171 return;
11172 }
11173
11174 /* Catch the no cq entry condition, log an error */
11175 if (unlikely(ecount == 0))
11176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11177 "0371 No entry from the CQ: identifier "
11178 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11179
11180 /* In any case, flash and re-arm the RCQ */
11181 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11182
11183 /* wake up worker thread if there are works to be done */
11184 if (workposted)
11185 lpfc_worker_wake_up(phba);
11186}
11187
11188/**
11189 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11190 * @eqe: Pointer to fast-path completion queue entry.
11191 *
11192 * This routine process a fast-path work queue completion entry from fast-path
11193 * event queue for FCP command response completion.
11194 **/
11195static void
11196lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11197 struct lpfc_wcqe_complete *wcqe)
11198{
11199 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11200 struct lpfc_iocbq *cmdiocbq;
11201 struct lpfc_iocbq irspiocbq;
11202 unsigned long iflags;
11203
11204 spin_lock_irqsave(&phba->hbalock, iflags);
11205 pring->stats.iocb_event++;
11206 spin_unlock_irqrestore(&phba->hbalock, iflags);
11207
11208 /* Check for response status */
11209 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11210 /* If resource errors reported from HBA, reduce queue
11211 * depth of the SCSI device.
11212 */
11213 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11214 IOSTAT_LOCAL_REJECT) &&
11215 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11216 phba->lpfc_rampdown_queue_depth(phba);
11217 }
11218 /* Log the error status */
11219 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11220 "0373 FCP complete error: status=x%x, "
11221 "hw_status=x%x, total_data_specified=%d, "
11222 "parameter=x%x, word3=x%x\n",
11223 bf_get(lpfc_wcqe_c_status, wcqe),
11224 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11225 wcqe->total_data_placed, wcqe->parameter,
11226 wcqe->word3);
11227 }
11228
11229 /* Look up the FCP command IOCB and create pseudo response IOCB */
11230 spin_lock_irqsave(&phba->hbalock, iflags);
11231 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11232 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11233 spin_unlock_irqrestore(&phba->hbalock, iflags);
11234 if (unlikely(!cmdiocbq)) {
11235 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11236 "0374 FCP complete with no corresponding "
11237 "cmdiocb: iotag (%d)\n",
11238 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11239 return;
11240 }
11241 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11242 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11243 "0375 FCP cmdiocb not callback function "
11244 "iotag: (%d)\n",
11245 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11246 return;
11247 }
11248
11249 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011250 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011251
James Smart0f65ff62010-02-26 14:14:23 -050011252 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11253 spin_lock_irqsave(&phba->hbalock, iflags);
11254 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11255 spin_unlock_irqrestore(&phba->hbalock, iflags);
11256 }
11257
James Smart4f774512009-05-22 14:52:35 -040011258 /* Pass the cmd_iocb and the rsp state to the upper layer */
11259 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11260}
11261
11262/**
11263 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11264 * @phba: Pointer to HBA context object.
11265 * @cq: Pointer to completion queue.
11266 * @wcqe: Pointer to work-queue completion queue entry.
11267 *
11268 * This routine handles an fast-path WQ entry comsumed event by invoking the
11269 * proper WQ release routine to the slow-path WQ.
11270 **/
11271static void
11272lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11273 struct lpfc_wcqe_release *wcqe)
11274{
11275 struct lpfc_queue *childwq;
11276 bool wqid_matched = false;
11277 uint16_t fcp_wqid;
11278
11279 /* Check for fast-path FCP work queue release */
11280 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11281 list_for_each_entry(childwq, &cq->child_list, list) {
11282 if (childwq->queue_id == fcp_wqid) {
11283 lpfc_sli4_wq_release(childwq,
11284 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11285 wqid_matched = true;
11286 break;
11287 }
11288 }
11289 /* Report warning log message if no match found */
11290 if (wqid_matched != true)
11291 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11292 "2580 Fast-path wqe consume event carries "
11293 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11294}
11295
11296/**
11297 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11298 * @cq: Pointer to the completion queue.
11299 * @eqe: Pointer to fast-path completion queue entry.
11300 *
11301 * This routine process a fast-path work queue completion entry from fast-path
11302 * event queue for FCP command response completion.
11303 **/
11304static int
11305lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11306 struct lpfc_cqe *cqe)
11307{
11308 struct lpfc_wcqe_release wcqe;
11309 bool workposted = false;
11310
11311 /* Copy the work queue CQE and convert endian order if needed */
11312 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11313
11314 /* Check and process for different type of WCQE and dispatch */
11315 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11316 case CQE_CODE_COMPL_WQE:
11317 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011318 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011319 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11320 (struct lpfc_wcqe_complete *)&wcqe);
11321 break;
11322 case CQE_CODE_RELEASE_WQE:
11323 /* Process the WQ release event */
11324 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11325 (struct lpfc_wcqe_release *)&wcqe);
11326 break;
11327 case CQE_CODE_XRI_ABORTED:
11328 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011329 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011330 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11331 (struct sli4_wcqe_xri_aborted *)&wcqe);
11332 break;
11333 default:
11334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11335 "0144 Not a valid WCQE code: x%x\n",
11336 bf_get(lpfc_wcqe_c_code, &wcqe));
11337 break;
11338 }
11339 return workposted;
11340}
11341
11342/**
11343 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11344 * @phba: Pointer to HBA context object.
11345 * @eqe: Pointer to fast-path event queue entry.
11346 *
11347 * This routine process a event queue entry from the fast-path event queue.
11348 * It will check the MajorCode and MinorCode to determine this is for a
11349 * completion event on a completion queue, if not, an error shall be logged
11350 * and just return. Otherwise, it will get to the corresponding completion
11351 * queue and process all the entries on the completion queue, rearm the
11352 * completion queue, and then return.
11353 **/
11354static void
11355lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11356 uint32_t fcp_cqidx)
11357{
11358 struct lpfc_queue *cq;
11359 struct lpfc_cqe *cqe;
11360 bool workposted = false;
11361 uint16_t cqid;
11362 int ecount = 0;
11363
James Smartcb5172e2010-03-15 11:25:07 -040011364 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11366 "0366 Not a valid fast-path completion "
11367 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011368 bf_get_le32(lpfc_eqe_major_code, eqe),
11369 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011370 return;
11371 }
11372
11373 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11374 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011375 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11377 "0367 Fast-path completion queue "
11378 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040011379 return;
11380 }
11381
11382 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011383 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011384 if (unlikely(cqid != cq->queue_id)) {
11385 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11386 "0368 Miss-matched fast-path completion "
11387 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11388 cqid, cq->queue_id);
11389 return;
11390 }
11391
11392 /* Process all the entries to the CQ */
11393 while ((cqe = lpfc_sli4_cq_get(cq))) {
11394 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011395 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011396 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11397 }
11398
11399 /* Catch the no cq entry condition */
11400 if (unlikely(ecount == 0))
11401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11402 "0369 No entry from fast-path completion "
11403 "queue fcpcqid=%d\n", cq->queue_id);
11404
11405 /* In any case, flash and re-arm the CQ */
11406 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11407
11408 /* wake up worker thread if there are works to be done */
11409 if (workposted)
11410 lpfc_worker_wake_up(phba);
11411}
11412
11413static void
11414lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11415{
11416 struct lpfc_eqe *eqe;
11417
11418 /* walk all the EQ entries and drop on the floor */
11419 while ((eqe = lpfc_sli4_eq_get(eq)))
11420 ;
11421
11422 /* Clear and re-arm the EQ */
11423 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11424}
11425
11426/**
11427 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11428 * @irq: Interrupt number.
11429 * @dev_id: The device context pointer.
11430 *
11431 * This function is directly called from the PCI layer as an interrupt
11432 * service routine when device with SLI-4 interface spec is enabled with
11433 * MSI-X multi-message interrupt mode and there are slow-path events in
11434 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11435 * interrupt mode, this function is called as part of the device-level
11436 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11437 * undergoing initialization, the interrupt handler will not process the
11438 * interrupt. The link attention and ELS ring attention events are handled
11439 * by the worker thread. The interrupt handler signals the worker thread
11440 * and returns for these events. This function is called without any lock
11441 * held. It gets the hbalock to access and update SLI data structures.
11442 *
11443 * This function returns IRQ_HANDLED when interrupt is handled else it
11444 * returns IRQ_NONE.
11445 **/
11446irqreturn_t
11447lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11448{
11449 struct lpfc_hba *phba;
11450 struct lpfc_queue *speq;
11451 struct lpfc_eqe *eqe;
11452 unsigned long iflag;
11453 int ecount = 0;
11454
11455 /*
11456 * Get the driver's phba structure from the dev_id
11457 */
11458 phba = (struct lpfc_hba *)dev_id;
11459
11460 if (unlikely(!phba))
11461 return IRQ_NONE;
11462
11463 /* Get to the EQ struct associated with this vector */
11464 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011465 if (unlikely(!speq))
11466 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011467
11468 /* Check device state for handling interrupt */
11469 if (unlikely(lpfc_intr_state_check(phba))) {
11470 /* Check again for link_state with lock held */
11471 spin_lock_irqsave(&phba->hbalock, iflag);
11472 if (phba->link_state < LPFC_LINK_DOWN)
11473 /* Flush, clear interrupt, and rearm the EQ */
11474 lpfc_sli4_eq_flush(phba, speq);
11475 spin_unlock_irqrestore(&phba->hbalock, iflag);
11476 return IRQ_NONE;
11477 }
11478
11479 /*
11480 * Process all the event on FCP slow-path EQ
11481 */
11482 while ((eqe = lpfc_sli4_eq_get(speq))) {
11483 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011484 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011485 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11486 }
11487
11488 /* Always clear and re-arm the slow-path EQ */
11489 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11490
11491 /* Catch the no cq entry condition */
11492 if (unlikely(ecount == 0)) {
11493 if (phba->intr_type == MSIX)
11494 /* MSI-X treated interrupt served as no EQ share INT */
11495 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11496 "0357 MSI-X interrupt with no EQE\n");
11497 else
11498 /* Non MSI-X treated on interrupt as EQ share INT */
11499 return IRQ_NONE;
11500 }
11501
11502 return IRQ_HANDLED;
11503} /* lpfc_sli4_sp_intr_handler */
11504
11505/**
11506 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11507 * @irq: Interrupt number.
11508 * @dev_id: The device context pointer.
11509 *
11510 * This function is directly called from the PCI layer as an interrupt
11511 * service routine when device with SLI-4 interface spec is enabled with
11512 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11513 * ring event in the HBA. However, when the device is enabled with either
11514 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11515 * device-level interrupt handler. When the PCI slot is in error recovery
11516 * or the HBA is undergoing initialization, the interrupt handler will not
11517 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11518 * the intrrupt context. This function is called without any lock held.
11519 * It gets the hbalock to access and update SLI data structures. Note that,
11520 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11521 * equal to that of FCP CQ index.
11522 *
11523 * This function returns IRQ_HANDLED when interrupt is handled else it
11524 * returns IRQ_NONE.
11525 **/
11526irqreturn_t
11527lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11528{
11529 struct lpfc_hba *phba;
11530 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11531 struct lpfc_queue *fpeq;
11532 struct lpfc_eqe *eqe;
11533 unsigned long iflag;
11534 int ecount = 0;
11535 uint32_t fcp_eqidx;
11536
11537 /* Get the driver's phba structure from the dev_id */
11538 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11539 phba = fcp_eq_hdl->phba;
11540 fcp_eqidx = fcp_eq_hdl->idx;
11541
11542 if (unlikely(!phba))
11543 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011544 if (unlikely(!phba->sli4_hba.fp_eq))
11545 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011546
11547 /* Get to the EQ struct associated with this vector */
11548 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11549
11550 /* Check device state for handling interrupt */
11551 if (unlikely(lpfc_intr_state_check(phba))) {
11552 /* Check again for link_state with lock held */
11553 spin_lock_irqsave(&phba->hbalock, iflag);
11554 if (phba->link_state < LPFC_LINK_DOWN)
11555 /* Flush, clear interrupt, and rearm the EQ */
11556 lpfc_sli4_eq_flush(phba, fpeq);
11557 spin_unlock_irqrestore(&phba->hbalock, iflag);
11558 return IRQ_NONE;
11559 }
11560
11561 /*
11562 * Process all the event on FCP fast-path EQ
11563 */
11564 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11565 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011566 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011567 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11568 }
11569
11570 /* Always clear and re-arm the fast-path EQ */
11571 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11572
11573 if (unlikely(ecount == 0)) {
11574 if (phba->intr_type == MSIX)
11575 /* MSI-X treated interrupt served as no EQ share INT */
11576 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11577 "0358 MSI-X interrupt with no EQE\n");
11578 else
11579 /* Non MSI-X treated on interrupt as EQ share INT */
11580 return IRQ_NONE;
11581 }
11582
11583 return IRQ_HANDLED;
11584} /* lpfc_sli4_fp_intr_handler */
11585
11586/**
11587 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11588 * @irq: Interrupt number.
11589 * @dev_id: The device context pointer.
11590 *
11591 * This function is the device-level interrupt handler to device with SLI-4
11592 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11593 * interrupt mode is enabled and there is an event in the HBA which requires
11594 * driver attention. This function invokes the slow-path interrupt attention
11595 * handling function and fast-path interrupt attention handling function in
11596 * turn to process the relevant HBA attention events. This function is called
11597 * without any lock held. It gets the hbalock to access and update SLI data
11598 * structures.
11599 *
11600 * This function returns IRQ_HANDLED when interrupt is handled, else it
11601 * returns IRQ_NONE.
11602 **/
11603irqreturn_t
11604lpfc_sli4_intr_handler(int irq, void *dev_id)
11605{
11606 struct lpfc_hba *phba;
11607 irqreturn_t sp_irq_rc, fp_irq_rc;
11608 bool fp_handled = false;
11609 uint32_t fcp_eqidx;
11610
11611 /* Get the driver's phba structure from the dev_id */
11612 phba = (struct lpfc_hba *)dev_id;
11613
11614 if (unlikely(!phba))
11615 return IRQ_NONE;
11616
11617 /*
11618 * Invokes slow-path host attention interrupt handling as appropriate.
11619 */
11620 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11621
11622 /*
11623 * Invoke fast-path host attention interrupt handling as appropriate.
11624 */
11625 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11626 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11627 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11628 if (fp_irq_rc == IRQ_HANDLED)
11629 fp_handled |= true;
11630 }
11631
11632 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11633} /* lpfc_sli4_intr_handler */
11634
11635/**
11636 * lpfc_sli4_queue_free - free a queue structure and associated memory
11637 * @queue: The queue structure to free.
11638 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011639 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011640 * the host resident queue. This function must be called after destroying the
11641 * queue on the HBA.
11642 **/
11643void
11644lpfc_sli4_queue_free(struct lpfc_queue *queue)
11645{
11646 struct lpfc_dmabuf *dmabuf;
11647
11648 if (!queue)
11649 return;
11650
11651 while (!list_empty(&queue->page_list)) {
11652 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11653 list);
James Smart49198b32010-04-06 15:04:33 -040011654 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011655 dmabuf->virt, dmabuf->phys);
11656 kfree(dmabuf);
11657 }
11658 kfree(queue);
11659 return;
11660}
11661
11662/**
11663 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11664 * @phba: The HBA that this queue is being created on.
11665 * @entry_size: The size of each queue entry for this queue.
11666 * @entry count: The number of entries that this queue will handle.
11667 *
11668 * This function allocates a queue structure and the DMAable memory used for
11669 * the host resident queue. This function must be called before creating the
11670 * queue on the HBA.
11671 **/
11672struct lpfc_queue *
11673lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11674 uint32_t entry_count)
11675{
11676 struct lpfc_queue *queue;
11677 struct lpfc_dmabuf *dmabuf;
11678 int x, total_qe_count;
11679 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011680 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011681
James Smartcb5172e2010-03-15 11:25:07 -040011682 if (!phba->sli4_hba.pc_sli4_params.supported)
11683 hw_page_size = SLI4_PAGE_SIZE;
11684
James Smart4f774512009-05-22 14:52:35 -040011685 queue = kzalloc(sizeof(struct lpfc_queue) +
11686 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11687 if (!queue)
11688 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011689 queue->page_count = (ALIGN(entry_size * entry_count,
11690 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011691 INIT_LIST_HEAD(&queue->list);
11692 INIT_LIST_HEAD(&queue->page_list);
11693 INIT_LIST_HEAD(&queue->child_list);
11694 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11695 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11696 if (!dmabuf)
11697 goto out_fail;
11698 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011699 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011700 GFP_KERNEL);
11701 if (!dmabuf->virt) {
11702 kfree(dmabuf);
11703 goto out_fail;
11704 }
James Smartcb5172e2010-03-15 11:25:07 -040011705 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011706 dmabuf->buffer_tag = x;
11707 list_add_tail(&dmabuf->list, &queue->page_list);
11708 /* initialize queue's entry array */
11709 dma_pointer = dmabuf->virt;
11710 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011711 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011712 total_qe_count++, dma_pointer += entry_size) {
11713 queue->qe[total_qe_count].address = dma_pointer;
11714 }
11715 }
11716 queue->entry_size = entry_size;
11717 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011718
11719 /*
11720 * entry_repost is calculated based on the number of entries in the
11721 * queue. This works out except for RQs. If buffers are NOT initially
11722 * posted for every RQE, entry_repost should be adjusted accordingly.
11723 */
11724 queue->entry_repost = (entry_count >> 3);
11725 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11726 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011727 queue->phba = phba;
11728
11729 return queue;
11730out_fail:
11731 lpfc_sli4_queue_free(queue);
11732 return NULL;
11733}
11734
11735/**
11736 * lpfc_eq_create - Create an Event Queue on the HBA
11737 * @phba: HBA structure that indicates port to create a queue on.
11738 * @eq: The queue structure to use to create the event queue.
11739 * @imax: The maximum interrupt per second limit.
11740 *
11741 * This function creates an event queue, as detailed in @eq, on a port,
11742 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11743 *
11744 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11745 * is used to get the entry count and entry size that are necessary to
11746 * determine the number of pages to allocate and use for this queue. This
11747 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11748 * event queue. This function is asynchronous and will wait for the mailbox
11749 * command to finish before continuing.
11750 *
11751 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011752 * memory this function will return -ENOMEM. If the queue create mailbox command
11753 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011754 **/
11755uint32_t
11756lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11757{
11758 struct lpfc_mbx_eq_create *eq_create;
11759 LPFC_MBOXQ_t *mbox;
11760 int rc, length, status = 0;
11761 struct lpfc_dmabuf *dmabuf;
11762 uint32_t shdr_status, shdr_add_status;
11763 union lpfc_sli4_cfg_shdr *shdr;
11764 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011765 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11766
11767 if (!phba->sli4_hba.pc_sli4_params.supported)
11768 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011769
11770 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11771 if (!mbox)
11772 return -ENOMEM;
11773 length = (sizeof(struct lpfc_mbx_eq_create) -
11774 sizeof(struct lpfc_sli4_cfg_mhdr));
11775 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11776 LPFC_MBOX_OPCODE_EQ_CREATE,
11777 length, LPFC_SLI4_MBX_EMBED);
11778 eq_create = &mbox->u.mqe.un.eq_create;
11779 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11780 eq->page_count);
11781 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11782 LPFC_EQE_SIZE);
11783 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11784 /* Calculate delay multiper from maximum interrupt per second */
11785 dmult = LPFC_DMULT_CONST/imax - 1;
11786 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11787 dmult);
11788 switch (eq->entry_count) {
11789 default:
11790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11791 "0360 Unsupported EQ count. (%d)\n",
11792 eq->entry_count);
11793 if (eq->entry_count < 256)
11794 return -EINVAL;
11795 /* otherwise default to smallest count (drop through) */
11796 case 256:
11797 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11798 LPFC_EQ_CNT_256);
11799 break;
11800 case 512:
11801 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11802 LPFC_EQ_CNT_512);
11803 break;
11804 case 1024:
11805 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11806 LPFC_EQ_CNT_1024);
11807 break;
11808 case 2048:
11809 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11810 LPFC_EQ_CNT_2048);
11811 break;
11812 case 4096:
11813 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11814 LPFC_EQ_CNT_4096);
11815 break;
11816 }
11817 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011818 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011819 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11820 putPaddrLow(dmabuf->phys);
11821 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11822 putPaddrHigh(dmabuf->phys);
11823 }
11824 mbox->vport = phba->pport;
11825 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11826 mbox->context1 = NULL;
11827 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11828 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11829 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11830 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11831 if (shdr_status || shdr_add_status || rc) {
11832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11833 "2500 EQ_CREATE mailbox failed with "
11834 "status x%x add_status x%x, mbx status x%x\n",
11835 shdr_status, shdr_add_status, rc);
11836 status = -ENXIO;
11837 }
11838 eq->type = LPFC_EQ;
11839 eq->subtype = LPFC_NONE;
11840 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11841 if (eq->queue_id == 0xFFFF)
11842 status = -ENXIO;
11843 eq->host_index = 0;
11844 eq->hba_index = 0;
11845
James Smart8fa38512009-07-19 10:01:03 -040011846 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011847 return status;
11848}
11849
11850/**
11851 * lpfc_cq_create - Create a Completion Queue on the HBA
11852 * @phba: HBA structure that indicates port to create a queue on.
11853 * @cq: The queue structure to use to create the completion queue.
11854 * @eq: The event queue to bind this completion queue to.
11855 *
11856 * This function creates a completion queue, as detailed in @wq, on a port,
11857 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11858 *
11859 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11860 * is used to get the entry count and entry size that are necessary to
11861 * determine the number of pages to allocate and use for this queue. The @eq
11862 * is used to indicate which event queue to bind this completion queue to. This
11863 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11864 * completion queue. This function is asynchronous and will wait for the mailbox
11865 * command to finish before continuing.
11866 *
11867 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011868 * memory this function will return -ENOMEM. If the queue create mailbox command
11869 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011870 **/
11871uint32_t
11872lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11873 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11874{
11875 struct lpfc_mbx_cq_create *cq_create;
11876 struct lpfc_dmabuf *dmabuf;
11877 LPFC_MBOXQ_t *mbox;
11878 int rc, length, status = 0;
11879 uint32_t shdr_status, shdr_add_status;
11880 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011881 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11882
11883 if (!phba->sli4_hba.pc_sli4_params.supported)
11884 hw_page_size = SLI4_PAGE_SIZE;
11885
James Smart4f774512009-05-22 14:52:35 -040011886 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11887 if (!mbox)
11888 return -ENOMEM;
11889 length = (sizeof(struct lpfc_mbx_cq_create) -
11890 sizeof(struct lpfc_sli4_cfg_mhdr));
11891 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11892 LPFC_MBOX_OPCODE_CQ_CREATE,
11893 length, LPFC_SLI4_MBX_EMBED);
11894 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011895 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011896 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11897 cq->page_count);
11898 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11899 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011900 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11901 phba->sli4_hba.pc_sli4_params.cqv);
11902 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011903 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11904 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011905 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11906 eq->queue_id);
11907 } else {
11908 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11909 eq->queue_id);
11910 }
James Smart4f774512009-05-22 14:52:35 -040011911 switch (cq->entry_count) {
11912 default:
11913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11914 "0361 Unsupported CQ count. (%d)\n",
11915 cq->entry_count);
11916 if (cq->entry_count < 256)
11917 return -EINVAL;
11918 /* otherwise default to smallest count (drop through) */
11919 case 256:
11920 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11921 LPFC_CQ_CNT_256);
11922 break;
11923 case 512:
11924 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11925 LPFC_CQ_CNT_512);
11926 break;
11927 case 1024:
11928 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11929 LPFC_CQ_CNT_1024);
11930 break;
11931 }
11932 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011933 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011934 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11935 putPaddrLow(dmabuf->phys);
11936 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11937 putPaddrHigh(dmabuf->phys);
11938 }
11939 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11940
11941 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011942 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11943 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11944 if (shdr_status || shdr_add_status || rc) {
11945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11946 "2501 CQ_CREATE mailbox failed with "
11947 "status x%x add_status x%x, mbx status x%x\n",
11948 shdr_status, shdr_add_status, rc);
11949 status = -ENXIO;
11950 goto out;
11951 }
11952 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11953 if (cq->queue_id == 0xFFFF) {
11954 status = -ENXIO;
11955 goto out;
11956 }
11957 /* link the cq onto the parent eq child list */
11958 list_add_tail(&cq->list, &eq->child_list);
11959 /* Set up completion queue's type and subtype */
11960 cq->type = type;
11961 cq->subtype = subtype;
11962 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050011963 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011964 cq->host_index = 0;
11965 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040011966
James Smart8fa38512009-07-19 10:01:03 -040011967out:
11968 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011969 return status;
11970}
11971
11972/**
James Smartb19a0612010-04-06 14:48:51 -040011973 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040011974 * @phba: HBA structure that indicates port to create a queue on.
11975 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040011976 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11977 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040011978 *
James Smartb19a0612010-04-06 14:48:51 -040011979 * This function provides failback (fb) functionality when the
11980 * mq_create_ext fails on older FW generations. It's purpose is identical
11981 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040011982 *
James Smartb19a0612010-04-06 14:48:51 -040011983 * This routine cannot fail as all attributes were previously accessed and
11984 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040011985 **/
James Smartb19a0612010-04-06 14:48:51 -040011986static void
11987lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11988 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040011989{
11990 struct lpfc_mbx_mq_create *mq_create;
11991 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040011992 int length;
James Smart04c68492009-05-22 14:52:52 -040011993
James Smart04c68492009-05-22 14:52:52 -040011994 length = (sizeof(struct lpfc_mbx_mq_create) -
11995 sizeof(struct lpfc_sli4_cfg_mhdr));
11996 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11997 LPFC_MBOX_OPCODE_MQ_CREATE,
11998 length, LPFC_SLI4_MBX_EMBED);
11999 mq_create = &mbox->u.mqe.un.mq_create;
12000 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012001 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012002 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012003 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012004 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12005 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012006 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012007 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12008 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012009 break;
12010 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012011 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12012 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012013 break;
12014 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012015 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12016 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012017 break;
12018 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012019 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12020 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012021 break;
12022 }
12023 list_for_each_entry(dmabuf, &mq->page_list, list) {
12024 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012025 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012026 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012027 putPaddrHigh(dmabuf->phys);
12028 }
12029}
12030
12031/**
12032 * lpfc_mq_create - Create a mailbox Queue on the HBA
12033 * @phba: HBA structure that indicates port to create a queue on.
12034 * @mq: The queue structure to use to create the mailbox queue.
12035 * @cq: The completion queue to associate with this cq.
12036 * @subtype: The queue's subtype.
12037 *
12038 * This function creates a mailbox queue, as detailed in @mq, on a port,
12039 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12040 *
12041 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12042 * is used to get the entry count and entry size that are necessary to
12043 * determine the number of pages to allocate and use for this queue. This
12044 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12045 * mailbox queue. This function is asynchronous and will wait for the mailbox
12046 * command to finish before continuing.
12047 *
12048 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012049 * memory this function will return -ENOMEM. If the queue create mailbox command
12050 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012051 **/
12052int32_t
12053lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12054 struct lpfc_queue *cq, uint32_t subtype)
12055{
12056 struct lpfc_mbx_mq_create *mq_create;
12057 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12058 struct lpfc_dmabuf *dmabuf;
12059 LPFC_MBOXQ_t *mbox;
12060 int rc, length, status = 0;
12061 uint32_t shdr_status, shdr_add_status;
12062 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012063 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012064
James Smart49198b32010-04-06 15:04:33 -040012065 if (!phba->sli4_hba.pc_sli4_params.supported)
12066 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012067
12068 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12069 if (!mbox)
12070 return -ENOMEM;
12071 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12072 sizeof(struct lpfc_sli4_cfg_mhdr));
12073 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12074 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12075 length, LPFC_SLI4_MBX_EMBED);
12076
12077 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012078 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012079 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12080 &mq_create_ext->u.request, mq->page_count);
12081 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12082 &mq_create_ext->u.request, 1);
12083 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012084 &mq_create_ext->u.request, 1);
12085 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12086 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012087 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12088 &mq_create_ext->u.request, 1);
12089 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12090 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012091 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012092 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12093 phba->sli4_hba.pc_sli4_params.mqv);
12094 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12095 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12096 cq->queue_id);
12097 else
12098 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12099 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012100 switch (mq->entry_count) {
12101 default:
12102 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12103 "0362 Unsupported MQ count. (%d)\n",
12104 mq->entry_count);
12105 if (mq->entry_count < 16)
12106 return -EINVAL;
12107 /* otherwise default to smallest count (drop through) */
12108 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012109 bf_set(lpfc_mq_context_ring_size,
12110 &mq_create_ext->u.request.context,
12111 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012112 break;
12113 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012114 bf_set(lpfc_mq_context_ring_size,
12115 &mq_create_ext->u.request.context,
12116 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012117 break;
12118 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012119 bf_set(lpfc_mq_context_ring_size,
12120 &mq_create_ext->u.request.context,
12121 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012122 break;
12123 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012124 bf_set(lpfc_mq_context_ring_size,
12125 &mq_create_ext->u.request.context,
12126 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012127 break;
12128 }
12129 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012130 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012131 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12132 putPaddrLow(dmabuf->phys);
12133 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012134 putPaddrHigh(dmabuf->phys);
12135 }
12136 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012137 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12138 &mq_create_ext->u.response);
12139 if (rc != MBX_SUCCESS) {
12140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12141 "2795 MQ_CREATE_EXT failed with "
12142 "status x%x. Failback to MQ_CREATE.\n",
12143 rc);
12144 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12145 mq_create = &mbox->u.mqe.un.mq_create;
12146 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12147 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12148 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12149 &mq_create->u.response);
12150 }
12151
James Smart04c68492009-05-22 14:52:52 -040012152 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012153 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12154 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12155 if (shdr_status || shdr_add_status || rc) {
12156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12157 "2502 MQ_CREATE mailbox failed with "
12158 "status x%x add_status x%x, mbx status x%x\n",
12159 shdr_status, shdr_add_status, rc);
12160 status = -ENXIO;
12161 goto out;
12162 }
James Smart04c68492009-05-22 14:52:52 -040012163 if (mq->queue_id == 0xFFFF) {
12164 status = -ENXIO;
12165 goto out;
12166 }
12167 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012168 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012169 mq->subtype = subtype;
12170 mq->host_index = 0;
12171 mq->hba_index = 0;
12172
12173 /* link the mq onto the parent cq child list */
12174 list_add_tail(&mq->list, &cq->child_list);
12175out:
James Smart8fa38512009-07-19 10:01:03 -040012176 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012177 return status;
12178}
12179
12180/**
James Smart4f774512009-05-22 14:52:35 -040012181 * lpfc_wq_create - Create a Work Queue on the HBA
12182 * @phba: HBA structure that indicates port to create a queue on.
12183 * @wq: The queue structure to use to create the work queue.
12184 * @cq: The completion queue to bind this work queue to.
12185 * @subtype: The subtype of the work queue indicating its functionality.
12186 *
12187 * This function creates a work queue, as detailed in @wq, on a port, described
12188 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12189 *
12190 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12191 * is used to get the entry count and entry size that are necessary to
12192 * determine the number of pages to allocate and use for this queue. The @cq
12193 * is used to indicate which completion queue to bind this work queue to. This
12194 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12195 * work queue. This function is asynchronous and will wait for the mailbox
12196 * command to finish before continuing.
12197 *
12198 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012199 * memory this function will return -ENOMEM. If the queue create mailbox command
12200 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012201 **/
12202uint32_t
12203lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12204 struct lpfc_queue *cq, uint32_t subtype)
12205{
12206 struct lpfc_mbx_wq_create *wq_create;
12207 struct lpfc_dmabuf *dmabuf;
12208 LPFC_MBOXQ_t *mbox;
12209 int rc, length, status = 0;
12210 uint32_t shdr_status, shdr_add_status;
12211 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012212 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012213 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012214
12215 if (!phba->sli4_hba.pc_sli4_params.supported)
12216 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012217
12218 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12219 if (!mbox)
12220 return -ENOMEM;
12221 length = (sizeof(struct lpfc_mbx_wq_create) -
12222 sizeof(struct lpfc_sli4_cfg_mhdr));
12223 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12224 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12225 length, LPFC_SLI4_MBX_EMBED);
12226 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012227 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012228 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12229 wq->page_count);
12230 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12231 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012232 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12233 phba->sli4_hba.pc_sli4_params.wqv);
12234 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12235 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12236 wq->entry_count);
12237 switch (wq->entry_size) {
12238 default:
12239 case 64:
12240 bf_set(lpfc_mbx_wq_create_wqe_size,
12241 &wq_create->u.request_1,
12242 LPFC_WQ_WQE_SIZE_64);
12243 break;
12244 case 128:
12245 bf_set(lpfc_mbx_wq_create_wqe_size,
12246 &wq_create->u.request_1,
12247 LPFC_WQ_WQE_SIZE_128);
12248 break;
12249 }
12250 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12251 (PAGE_SIZE/SLI4_PAGE_SIZE));
12252 page = wq_create->u.request_1.page;
12253 } else {
12254 page = wq_create->u.request.page;
12255 }
James Smart4f774512009-05-22 14:52:35 -040012256 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012257 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012258 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12259 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012260 }
12261 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12262 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012263 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12264 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12265 if (shdr_status || shdr_add_status || rc) {
12266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12267 "2503 WQ_CREATE mailbox failed with "
12268 "status x%x add_status x%x, mbx status x%x\n",
12269 shdr_status, shdr_add_status, rc);
12270 status = -ENXIO;
12271 goto out;
12272 }
12273 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12274 if (wq->queue_id == 0xFFFF) {
12275 status = -ENXIO;
12276 goto out;
12277 }
12278 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012279 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012280 wq->subtype = subtype;
12281 wq->host_index = 0;
12282 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012283 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012284
12285 /* link the wq onto the parent cq child list */
12286 list_add_tail(&wq->list, &cq->child_list);
12287out:
James Smart8fa38512009-07-19 10:01:03 -040012288 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012289 return status;
12290}
12291
12292/**
James Smart73d91e52011-10-10 21:32:10 -040012293 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12294 * @phba: HBA structure that indicates port to create a queue on.
12295 * @rq: The queue structure to use for the receive queue.
12296 * @qno: The associated HBQ number
12297 *
12298 *
12299 * For SLI4 we need to adjust the RQ repost value based on
12300 * the number of buffers that are initially posted to the RQ.
12301 */
12302void
12303lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12304{
12305 uint32_t cnt;
12306
12307 cnt = lpfc_hbq_defs[qno]->entry_count;
12308
12309 /* Recalc repost for RQs based on buffers initially posted */
12310 cnt = (cnt >> 3);
12311 if (cnt < LPFC_QUEUE_MIN_REPOST)
12312 cnt = LPFC_QUEUE_MIN_REPOST;
12313
12314 rq->entry_repost = cnt;
12315}
12316
12317/**
James Smart4f774512009-05-22 14:52:35 -040012318 * lpfc_rq_create - Create a Receive Queue on the HBA
12319 * @phba: HBA structure that indicates port to create a queue on.
12320 * @hrq: The queue structure to use to create the header receive queue.
12321 * @drq: The queue structure to use to create the data receive queue.
12322 * @cq: The completion queue to bind this work queue to.
12323 *
12324 * This function creates a receive buffer queue pair , as detailed in @hrq and
12325 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12326 * to the HBA.
12327 *
12328 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12329 * struct is used to get the entry count that is necessary to determine the
12330 * number of pages to use for this queue. The @cq is used to indicate which
12331 * completion queue to bind received buffers that are posted to these queues to.
12332 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12333 * receive queue pair. This function is asynchronous and will wait for the
12334 * mailbox command to finish before continuing.
12335 *
12336 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012337 * memory this function will return -ENOMEM. If the queue create mailbox command
12338 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012339 **/
12340uint32_t
12341lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12342 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12343{
12344 struct lpfc_mbx_rq_create *rq_create;
12345 struct lpfc_dmabuf *dmabuf;
12346 LPFC_MBOXQ_t *mbox;
12347 int rc, length, status = 0;
12348 uint32_t shdr_status, shdr_add_status;
12349 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012350 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12351
12352 if (!phba->sli4_hba.pc_sli4_params.supported)
12353 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012354
12355 if (hrq->entry_count != drq->entry_count)
12356 return -EINVAL;
12357 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12358 if (!mbox)
12359 return -ENOMEM;
12360 length = (sizeof(struct lpfc_mbx_rq_create) -
12361 sizeof(struct lpfc_sli4_cfg_mhdr));
12362 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12363 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12364 length, LPFC_SLI4_MBX_EMBED);
12365 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012366 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12367 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12368 phba->sli4_hba.pc_sli4_params.rqv);
12369 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12370 bf_set(lpfc_rq_context_rqe_count_1,
12371 &rq_create->u.request.context,
12372 hrq->entry_count);
12373 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012374 bf_set(lpfc_rq_context_rqe_size,
12375 &rq_create->u.request.context,
12376 LPFC_RQE_SIZE_8);
12377 bf_set(lpfc_rq_context_page_size,
12378 &rq_create->u.request.context,
12379 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012380 } else {
12381 switch (hrq->entry_count) {
12382 default:
12383 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12384 "2535 Unsupported RQ count. (%d)\n",
12385 hrq->entry_count);
12386 if (hrq->entry_count < 512)
12387 return -EINVAL;
12388 /* otherwise default to smallest count (drop through) */
12389 case 512:
12390 bf_set(lpfc_rq_context_rqe_count,
12391 &rq_create->u.request.context,
12392 LPFC_RQ_RING_SIZE_512);
12393 break;
12394 case 1024:
12395 bf_set(lpfc_rq_context_rqe_count,
12396 &rq_create->u.request.context,
12397 LPFC_RQ_RING_SIZE_1024);
12398 break;
12399 case 2048:
12400 bf_set(lpfc_rq_context_rqe_count,
12401 &rq_create->u.request.context,
12402 LPFC_RQ_RING_SIZE_2048);
12403 break;
12404 case 4096:
12405 bf_set(lpfc_rq_context_rqe_count,
12406 &rq_create->u.request.context,
12407 LPFC_RQ_RING_SIZE_4096);
12408 break;
12409 }
12410 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12411 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012412 }
12413 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12414 cq->queue_id);
12415 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12416 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012417 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012418 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012419 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12420 putPaddrLow(dmabuf->phys);
12421 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12422 putPaddrHigh(dmabuf->phys);
12423 }
12424 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12425 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012426 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12427 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12428 if (shdr_status || shdr_add_status || rc) {
12429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12430 "2504 RQ_CREATE mailbox failed with "
12431 "status x%x add_status x%x, mbx status x%x\n",
12432 shdr_status, shdr_add_status, rc);
12433 status = -ENXIO;
12434 goto out;
12435 }
12436 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12437 if (hrq->queue_id == 0xFFFF) {
12438 status = -ENXIO;
12439 goto out;
12440 }
12441 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012442 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012443 hrq->subtype = subtype;
12444 hrq->host_index = 0;
12445 hrq->hba_index = 0;
12446
12447 /* now create the data queue */
12448 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12449 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12450 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012451 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12452 phba->sli4_hba.pc_sli4_params.rqv);
12453 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12454 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012455 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012456 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012457 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12458 LPFC_RQE_SIZE_8);
12459 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12460 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012461 } else {
12462 switch (drq->entry_count) {
12463 default:
12464 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12465 "2536 Unsupported RQ count. (%d)\n",
12466 drq->entry_count);
12467 if (drq->entry_count < 512)
12468 return -EINVAL;
12469 /* otherwise default to smallest count (drop through) */
12470 case 512:
12471 bf_set(lpfc_rq_context_rqe_count,
12472 &rq_create->u.request.context,
12473 LPFC_RQ_RING_SIZE_512);
12474 break;
12475 case 1024:
12476 bf_set(lpfc_rq_context_rqe_count,
12477 &rq_create->u.request.context,
12478 LPFC_RQ_RING_SIZE_1024);
12479 break;
12480 case 2048:
12481 bf_set(lpfc_rq_context_rqe_count,
12482 &rq_create->u.request.context,
12483 LPFC_RQ_RING_SIZE_2048);
12484 break;
12485 case 4096:
12486 bf_set(lpfc_rq_context_rqe_count,
12487 &rq_create->u.request.context,
12488 LPFC_RQ_RING_SIZE_4096);
12489 break;
12490 }
12491 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12492 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012493 }
12494 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12495 cq->queue_id);
12496 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12497 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012498 list_for_each_entry(dmabuf, &drq->page_list, list) {
12499 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12500 putPaddrLow(dmabuf->phys);
12501 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12502 putPaddrHigh(dmabuf->phys);
12503 }
12504 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12505 /* The IOCTL status is embedded in the mailbox subheader. */
12506 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12507 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12508 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12509 if (shdr_status || shdr_add_status || rc) {
12510 status = -ENXIO;
12511 goto out;
12512 }
12513 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12514 if (drq->queue_id == 0xFFFF) {
12515 status = -ENXIO;
12516 goto out;
12517 }
12518 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012519 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012520 drq->subtype = subtype;
12521 drq->host_index = 0;
12522 drq->hba_index = 0;
12523
12524 /* link the header and data RQs onto the parent cq child list */
12525 list_add_tail(&hrq->list, &cq->child_list);
12526 list_add_tail(&drq->list, &cq->child_list);
12527
12528out:
James Smart8fa38512009-07-19 10:01:03 -040012529 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012530 return status;
12531}
12532
12533/**
12534 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12535 * @eq: The queue structure associated with the queue to destroy.
12536 *
12537 * This function destroys a queue, as detailed in @eq by sending an mailbox
12538 * command, specific to the type of queue, to the HBA.
12539 *
12540 * The @eq struct is used to get the queue ID of the queue to destroy.
12541 *
12542 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012543 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012544 **/
12545uint32_t
12546lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12547{
12548 LPFC_MBOXQ_t *mbox;
12549 int rc, length, status = 0;
12550 uint32_t shdr_status, shdr_add_status;
12551 union lpfc_sli4_cfg_shdr *shdr;
12552
12553 if (!eq)
12554 return -ENODEV;
12555 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12556 if (!mbox)
12557 return -ENOMEM;
12558 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12559 sizeof(struct lpfc_sli4_cfg_mhdr));
12560 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12561 LPFC_MBOX_OPCODE_EQ_DESTROY,
12562 length, LPFC_SLI4_MBX_EMBED);
12563 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12564 eq->queue_id);
12565 mbox->vport = eq->phba->pport;
12566 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12567
12568 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12569 /* The IOCTL status is embedded in the mailbox subheader. */
12570 shdr = (union lpfc_sli4_cfg_shdr *)
12571 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12572 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12573 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12574 if (shdr_status || shdr_add_status || rc) {
12575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12576 "2505 EQ_DESTROY mailbox failed with "
12577 "status x%x add_status x%x, mbx status x%x\n",
12578 shdr_status, shdr_add_status, rc);
12579 status = -ENXIO;
12580 }
12581
12582 /* Remove eq from any list */
12583 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012584 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012585 return status;
12586}
12587
12588/**
12589 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12590 * @cq: The queue structure associated with the queue to destroy.
12591 *
12592 * This function destroys a queue, as detailed in @cq by sending an mailbox
12593 * command, specific to the type of queue, to the HBA.
12594 *
12595 * The @cq struct is used to get the queue ID of the queue to destroy.
12596 *
12597 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012598 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012599 **/
12600uint32_t
12601lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12602{
12603 LPFC_MBOXQ_t *mbox;
12604 int rc, length, status = 0;
12605 uint32_t shdr_status, shdr_add_status;
12606 union lpfc_sli4_cfg_shdr *shdr;
12607
12608 if (!cq)
12609 return -ENODEV;
12610 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12611 if (!mbox)
12612 return -ENOMEM;
12613 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12614 sizeof(struct lpfc_sli4_cfg_mhdr));
12615 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12616 LPFC_MBOX_OPCODE_CQ_DESTROY,
12617 length, LPFC_SLI4_MBX_EMBED);
12618 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12619 cq->queue_id);
12620 mbox->vport = cq->phba->pport;
12621 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12622 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12623 /* The IOCTL status is embedded in the mailbox subheader. */
12624 shdr = (union lpfc_sli4_cfg_shdr *)
12625 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12626 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12627 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12628 if (shdr_status || shdr_add_status || rc) {
12629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12630 "2506 CQ_DESTROY mailbox failed with "
12631 "status x%x add_status x%x, mbx status x%x\n",
12632 shdr_status, shdr_add_status, rc);
12633 status = -ENXIO;
12634 }
12635 /* Remove cq from any list */
12636 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012637 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012638 return status;
12639}
12640
12641/**
James Smart04c68492009-05-22 14:52:52 -040012642 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12643 * @qm: The queue structure associated with the queue to destroy.
12644 *
12645 * This function destroys a queue, as detailed in @mq by sending an mailbox
12646 * command, specific to the type of queue, to the HBA.
12647 *
12648 * The @mq struct is used to get the queue ID of the queue to destroy.
12649 *
12650 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012651 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012652 **/
12653uint32_t
12654lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12655{
12656 LPFC_MBOXQ_t *mbox;
12657 int rc, length, status = 0;
12658 uint32_t shdr_status, shdr_add_status;
12659 union lpfc_sli4_cfg_shdr *shdr;
12660
12661 if (!mq)
12662 return -ENODEV;
12663 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12664 if (!mbox)
12665 return -ENOMEM;
12666 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12667 sizeof(struct lpfc_sli4_cfg_mhdr));
12668 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12669 LPFC_MBOX_OPCODE_MQ_DESTROY,
12670 length, LPFC_SLI4_MBX_EMBED);
12671 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12672 mq->queue_id);
12673 mbox->vport = mq->phba->pport;
12674 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12675 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12676 /* The IOCTL status is embedded in the mailbox subheader. */
12677 shdr = (union lpfc_sli4_cfg_shdr *)
12678 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12679 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12680 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12681 if (shdr_status || shdr_add_status || rc) {
12682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12683 "2507 MQ_DESTROY mailbox failed with "
12684 "status x%x add_status x%x, mbx status x%x\n",
12685 shdr_status, shdr_add_status, rc);
12686 status = -ENXIO;
12687 }
12688 /* Remove mq from any list */
12689 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012690 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012691 return status;
12692}
12693
12694/**
James Smart4f774512009-05-22 14:52:35 -040012695 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12696 * @wq: The queue structure associated with the queue to destroy.
12697 *
12698 * This function destroys a queue, as detailed in @wq by sending an mailbox
12699 * command, specific to the type of queue, to the HBA.
12700 *
12701 * The @wq struct is used to get the queue ID of the queue to destroy.
12702 *
12703 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012704 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012705 **/
12706uint32_t
12707lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12708{
12709 LPFC_MBOXQ_t *mbox;
12710 int rc, length, status = 0;
12711 uint32_t shdr_status, shdr_add_status;
12712 union lpfc_sli4_cfg_shdr *shdr;
12713
12714 if (!wq)
12715 return -ENODEV;
12716 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12717 if (!mbox)
12718 return -ENOMEM;
12719 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12720 sizeof(struct lpfc_sli4_cfg_mhdr));
12721 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12722 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12723 length, LPFC_SLI4_MBX_EMBED);
12724 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12725 wq->queue_id);
12726 mbox->vport = wq->phba->pport;
12727 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12728 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12729 shdr = (union lpfc_sli4_cfg_shdr *)
12730 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12731 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12732 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12733 if (shdr_status || shdr_add_status || rc) {
12734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12735 "2508 WQ_DESTROY mailbox failed with "
12736 "status x%x add_status x%x, mbx status x%x\n",
12737 shdr_status, shdr_add_status, rc);
12738 status = -ENXIO;
12739 }
12740 /* Remove wq from any list */
12741 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012742 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012743 return status;
12744}
12745
12746/**
12747 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12748 * @rq: The queue structure associated with the queue to destroy.
12749 *
12750 * This function destroys a queue, as detailed in @rq by sending an mailbox
12751 * command, specific to the type of queue, to the HBA.
12752 *
12753 * The @rq struct is used to get the queue ID of the queue to destroy.
12754 *
12755 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012756 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012757 **/
12758uint32_t
12759lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12760 struct lpfc_queue *drq)
12761{
12762 LPFC_MBOXQ_t *mbox;
12763 int rc, length, status = 0;
12764 uint32_t shdr_status, shdr_add_status;
12765 union lpfc_sli4_cfg_shdr *shdr;
12766
12767 if (!hrq || !drq)
12768 return -ENODEV;
12769 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12770 if (!mbox)
12771 return -ENOMEM;
12772 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012773 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012774 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12775 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12776 length, LPFC_SLI4_MBX_EMBED);
12777 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12778 hrq->queue_id);
12779 mbox->vport = hrq->phba->pport;
12780 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12781 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12782 /* The IOCTL status is embedded in the mailbox subheader. */
12783 shdr = (union lpfc_sli4_cfg_shdr *)
12784 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12785 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12786 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12787 if (shdr_status || shdr_add_status || rc) {
12788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12789 "2509 RQ_DESTROY mailbox failed with "
12790 "status x%x add_status x%x, mbx status x%x\n",
12791 shdr_status, shdr_add_status, rc);
12792 if (rc != MBX_TIMEOUT)
12793 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12794 return -ENXIO;
12795 }
12796 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12797 drq->queue_id);
12798 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12799 shdr = (union lpfc_sli4_cfg_shdr *)
12800 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12801 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12802 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12803 if (shdr_status || shdr_add_status || rc) {
12804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12805 "2510 RQ_DESTROY mailbox failed with "
12806 "status x%x add_status x%x, mbx status x%x\n",
12807 shdr_status, shdr_add_status, rc);
12808 status = -ENXIO;
12809 }
12810 list_del_init(&hrq->list);
12811 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012812 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012813 return status;
12814}
12815
12816/**
12817 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12818 * @phba: The virtual port for which this call being executed.
12819 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12820 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12821 * @xritag: the xritag that ties this io to the SGL pages.
12822 *
12823 * This routine will post the sgl pages for the IO that has the xritag
12824 * that is in the iocbq structure. The xritag is assigned during iocbq
12825 * creation and persists for as long as the driver is loaded.
12826 * if the caller has fewer than 256 scatter gather segments to map then
12827 * pdma_phys_addr1 should be 0.
12828 * If the caller needs to map more than 256 scatter gather segment then
12829 * pdma_phys_addr1 should be a valid physical address.
12830 * physical address for SGLs must be 64 byte aligned.
12831 * If you are going to map 2 SGL's then the first one must have 256 entries
12832 * the second sgl can have between 1 and 256 entries.
12833 *
12834 * Return codes:
12835 * 0 - Success
12836 * -ENXIO, -ENOMEM - Failure
12837 **/
12838int
12839lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12840 dma_addr_t pdma_phys_addr0,
12841 dma_addr_t pdma_phys_addr1,
12842 uint16_t xritag)
12843{
12844 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12845 LPFC_MBOXQ_t *mbox;
12846 int rc;
12847 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012848 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012849 union lpfc_sli4_cfg_shdr *shdr;
12850
12851 if (xritag == NO_XRI) {
12852 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12853 "0364 Invalid param:\n");
12854 return -EINVAL;
12855 }
12856
12857 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12858 if (!mbox)
12859 return -ENOMEM;
12860
12861 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12862 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12863 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012864 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012865
12866 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12867 &mbox->u.mqe.un.post_sgl_pages;
12868 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12869 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12870
12871 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12872 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12873 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12874 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12875
12876 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12877 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12878 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12879 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12880 if (!phba->sli4_hba.intr_enable)
12881 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012882 else {
James Smarta183a152011-10-10 21:32:43 -040012883 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012884 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12885 }
James Smart4f774512009-05-22 14:52:35 -040012886 /* The IOCTL status is embedded in the mailbox subheader. */
12887 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12888 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12889 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12890 if (rc != MBX_TIMEOUT)
12891 mempool_free(mbox, phba->mbox_mem_pool);
12892 if (shdr_status || shdr_add_status || rc) {
12893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12894 "2511 POST_SGL mailbox failed with "
12895 "status x%x add_status x%x, mbx status x%x\n",
12896 shdr_status, shdr_add_status, rc);
12897 rc = -ENXIO;
12898 }
12899 return 0;
12900}
James Smart4f774512009-05-22 14:52:35 -040012901
12902/**
James Smart88a2cfb2011-07-22 18:36:33 -040012903 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012904 * @phba: pointer to lpfc hba data structure.
12905 *
12906 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012907 * HBA consistent with the SLI-4 interface spec. This routine
12908 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12909 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012910 *
James Smart88a2cfb2011-07-22 18:36:33 -040012911 * Returns
12912 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12913 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12914 **/
James Smart6d368e52011-05-24 11:44:12 -040012915uint16_t
12916lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12917{
12918 unsigned long xri;
12919
12920 /*
12921 * Fetch the next logical xri. Because this index is logical,
12922 * the driver starts at 0 each time.
12923 */
12924 spin_lock_irq(&phba->hbalock);
12925 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12926 phba->sli4_hba.max_cfg_param.max_xri, 0);
12927 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12928 spin_unlock_irq(&phba->hbalock);
12929 return NO_XRI;
12930 } else {
12931 set_bit(xri, phba->sli4_hba.xri_bmask);
12932 phba->sli4_hba.max_cfg_param.xri_used++;
12933 phba->sli4_hba.xri_count++;
12934 }
12935
12936 spin_unlock_irq(&phba->hbalock);
12937 return xri;
12938}
12939
12940/**
12941 * lpfc_sli4_free_xri - Release an xri for reuse.
12942 * @phba: pointer to lpfc hba data structure.
12943 *
12944 * This routine is invoked to release an xri to the pool of
12945 * available rpis maintained by the driver.
12946 **/
12947void
12948__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12949{
12950 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12951 phba->sli4_hba.xri_count--;
12952 phba->sli4_hba.max_cfg_param.xri_used--;
12953 }
12954}
12955
12956/**
12957 * lpfc_sli4_free_xri - Release an xri for reuse.
12958 * @phba: pointer to lpfc hba data structure.
12959 *
12960 * This routine is invoked to release an xri to the pool of
12961 * available rpis maintained by the driver.
12962 **/
12963void
12964lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12965{
12966 spin_lock_irq(&phba->hbalock);
12967 __lpfc_sli4_free_xri(phba, xri);
12968 spin_unlock_irq(&phba->hbalock);
12969}
12970
12971/**
James Smart4f774512009-05-22 14:52:35 -040012972 * lpfc_sli4_next_xritag - Get an xritag for the io
12973 * @phba: Pointer to HBA context object.
12974 *
12975 * This function gets an xritag for the iocb. If there is no unused xritag
12976 * it will return 0xffff.
12977 * The function returns the allocated xritag if successful, else returns zero.
12978 * Zero is not a valid xritag.
12979 * The caller is not required to hold any lock.
12980 **/
12981uint16_t
12982lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12983{
James Smart6d368e52011-05-24 11:44:12 -040012984 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040012985
James Smart6d368e52011-05-24 11:44:12 -040012986 xri_index = lpfc_sli4_alloc_xri(phba);
12987 if (xri_index != NO_XRI)
12988 return xri_index;
12989
12990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040012991 "2004 Failed to allocate XRI.last XRITAG is %d"
12992 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040012993 xri_index,
James Smart4f774512009-05-22 14:52:35 -040012994 phba->sli4_hba.max_cfg_param.max_xri,
12995 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040012996 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040012997}
12998
12999/**
James Smart6d368e52011-05-24 11:44:12 -040013000 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013001 * @phba: pointer to lpfc hba data structure.
13002 *
13003 * This routine is invoked to post a block of driver's sgl pages to the
13004 * HBA using non-embedded mailbox command. No Lock is held. This routine
13005 * is only called when the driver is loading and after all IO has been
13006 * stopped.
13007 **/
13008int
James Smart6d368e52011-05-24 11:44:12 -040013009lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040013010{
13011 struct lpfc_sglq *sglq_entry;
13012 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13013 struct sgl_page_pairs *sgl_pg_pairs;
13014 void *viraddr;
13015 LPFC_MBOXQ_t *mbox;
13016 uint32_t reqlen, alloclen, pg_pairs;
13017 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040013018 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040013019 int els_xri_cnt, rc = 0;
13020 uint32_t shdr_status, shdr_add_status;
13021 union lpfc_sli4_cfg_shdr *shdr;
13022
13023 /* The number of sgls to be posted */
13024 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13025
13026 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13027 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013028 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013029 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13030 "2559 Block sgl registration required DMA "
13031 "size (%d) great than a page\n", reqlen);
13032 return -ENOMEM;
13033 }
13034 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013035 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013036 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013037
13038 /* Allocate DMA memory and set up the non-embedded mailbox command */
13039 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13040 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13041 LPFC_SLI4_MBX_NEMBED);
13042
13043 if (alloclen < reqlen) {
13044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13045 "0285 Allocated DMA memory size (%d) is "
13046 "less than the requested DMA memory "
13047 "size (%d)\n", alloclen, reqlen);
13048 lpfc_sli4_mbox_cmd_free(phba, mbox);
13049 return -ENOMEM;
13050 }
James Smart4f774512009-05-22 14:52:35 -040013051 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013052 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013053 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13054 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13055
13056 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
13057 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040013058
13059 /*
13060 * Assign the sglq a physical xri only if the driver has not
13061 * initialized those resources. A port reset only needs
13062 * the sglq's posted.
13063 */
13064 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13065 LPFC_XRI_RSRC_RDY) {
13066 lxri = lpfc_sli4_next_xritag(phba);
13067 if (lxri == NO_XRI) {
13068 lpfc_sli4_mbox_cmd_free(phba, mbox);
13069 return -ENOMEM;
13070 }
13071 sglq_entry->sli4_lxritag = lxri;
13072 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
13073 }
13074
James Smart4f774512009-05-22 14:52:35 -040013075 /* Set up the sge entry */
13076 sgl_pg_pairs->sgl_pg0_addr_lo =
13077 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13078 sgl_pg_pairs->sgl_pg0_addr_hi =
13079 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13080 sgl_pg_pairs->sgl_pg1_addr_lo =
13081 cpu_to_le32(putPaddrLow(0));
13082 sgl_pg_pairs->sgl_pg1_addr_hi =
13083 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013084
James Smart4f774512009-05-22 14:52:35 -040013085 /* Keep the first xritag on the list */
13086 if (pg_pairs == 0)
13087 xritag_start = sglq_entry->sli4_xritag;
13088 sgl_pg_pairs++;
13089 }
James Smart6d368e52011-05-24 11:44:12 -040013090
13091 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013092 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040013093 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013094 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013095 if (!phba->sli4_hba.intr_enable)
13096 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13097 else {
James Smarta183a152011-10-10 21:32:43 -040013098 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013099 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13100 }
13101 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13102 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13103 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13104 if (rc != MBX_TIMEOUT)
13105 lpfc_sli4_mbox_cmd_free(phba, mbox);
13106 if (shdr_status || shdr_add_status || rc) {
13107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13108 "2513 POST_SGL_BLOCK mailbox command failed "
13109 "status x%x add_status x%x mbx status x%x\n",
13110 shdr_status, shdr_add_status, rc);
13111 rc = -ENXIO;
13112 }
James Smart6d368e52011-05-24 11:44:12 -040013113
13114 if (rc == 0)
13115 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13116 LPFC_XRI_RSRC_RDY);
13117 return rc;
13118}
13119
13120/**
13121 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13122 * @phba: pointer to lpfc hba data structure.
13123 *
13124 * This routine is invoked to post a block of driver's sgl pages to the
13125 * HBA using non-embedded mailbox command. No Lock is held. This routine
13126 * is only called when the driver is loading and after all IO has been
13127 * stopped.
13128 **/
13129int
13130lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13131{
13132 struct lpfc_sglq *sglq_entry;
13133 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13134 struct sgl_page_pairs *sgl_pg_pairs;
13135 void *viraddr;
13136 LPFC_MBOXQ_t *mbox;
13137 uint32_t reqlen, alloclen, index;
13138 uint32_t mbox_tmo;
13139 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13140 uint16_t xritag_start = 0, lxri = 0;
13141 struct lpfc_rsrc_blks *rsrc_blk;
13142 int cnt, ttl_cnt, rc = 0;
13143 int loop_cnt;
13144 uint32_t shdr_status, shdr_add_status;
13145 union lpfc_sli4_cfg_shdr *shdr;
13146
13147 /* The number of sgls to be posted */
13148 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13149
13150 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13151 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13152 if (reqlen > SLI4_PAGE_SIZE) {
13153 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13154 "2989 Block sgl registration required DMA "
13155 "size (%d) great than a page\n", reqlen);
13156 return -ENOMEM;
13157 }
13158
13159 cnt = 0;
13160 ttl_cnt = 0;
13161 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13162 list) {
13163 rsrc_start = rsrc_blk->rsrc_start;
13164 rsrc_size = rsrc_blk->rsrc_size;
13165
13166 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13167 "3014 Working ELS Extent start %d, cnt %d\n",
13168 rsrc_start, rsrc_size);
13169
13170 loop_cnt = min(els_xri_cnt, rsrc_size);
13171 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13172 loop_cnt = els_xri_cnt - ttl_cnt;
13173 ttl_cnt = els_xri_cnt;
13174 }
13175
13176 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13177 if (!mbox)
13178 return -ENOMEM;
13179 /*
13180 * Allocate DMA memory and set up the non-embedded mailbox
13181 * command.
13182 */
13183 alloclen = lpfc_sli4_config(phba, mbox,
13184 LPFC_MBOX_SUBSYSTEM_FCOE,
13185 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13186 reqlen, LPFC_SLI4_MBX_NEMBED);
13187 if (alloclen < reqlen) {
13188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13189 "2987 Allocated DMA memory size (%d) "
13190 "is less than the requested DMA memory "
13191 "size (%d)\n", alloclen, reqlen);
13192 lpfc_sli4_mbox_cmd_free(phba, mbox);
13193 return -ENOMEM;
13194 }
13195
13196 /* Set up the SGL pages in the non-embedded DMA pages */
13197 viraddr = mbox->sge_array->addr[0];
13198 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13199 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13200
13201 /*
13202 * The starting resource may not begin at zero. Control
13203 * the loop variants via the block resource parameters,
13204 * but handle the sge pointers with a zero-based index
13205 * that doesn't get reset per loop pass.
13206 */
13207 for (index = rsrc_start;
13208 index < rsrc_start + loop_cnt;
13209 index++) {
13210 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13211
13212 /*
13213 * Assign the sglq a physical xri only if the driver
13214 * has not initialized those resources. A port reset
13215 * only needs the sglq's posted.
13216 */
13217 if (bf_get(lpfc_xri_rsrc_rdy,
13218 &phba->sli4_hba.sli4_flags) !=
13219 LPFC_XRI_RSRC_RDY) {
13220 lxri = lpfc_sli4_next_xritag(phba);
13221 if (lxri == NO_XRI) {
13222 lpfc_sli4_mbox_cmd_free(phba, mbox);
13223 rc = -ENOMEM;
13224 goto err_exit;
13225 }
13226 sglq_entry->sli4_lxritag = lxri;
13227 sglq_entry->sli4_xritag =
13228 phba->sli4_hba.xri_ids[lxri];
13229 }
13230
13231 /* Set up the sge entry */
13232 sgl_pg_pairs->sgl_pg0_addr_lo =
13233 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13234 sgl_pg_pairs->sgl_pg0_addr_hi =
13235 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13236 sgl_pg_pairs->sgl_pg1_addr_lo =
13237 cpu_to_le32(putPaddrLow(0));
13238 sgl_pg_pairs->sgl_pg1_addr_hi =
13239 cpu_to_le32(putPaddrHigh(0));
13240
13241 /* Track the starting physical XRI for the mailbox. */
13242 if (index == rsrc_start)
13243 xritag_start = sglq_entry->sli4_xritag;
13244 sgl_pg_pairs++;
13245 cnt++;
13246 }
13247
13248 /* Complete initialization and perform endian conversion. */
13249 rsrc_blk->rsrc_used += loop_cnt;
13250 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13251 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13252 sgl->word0 = cpu_to_le32(sgl->word0);
13253
13254 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13255 "3015 Post ELS Extent SGL, start %d, "
13256 "cnt %d, used %d\n",
13257 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13258 if (!phba->sli4_hba.intr_enable)
13259 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13260 else {
James Smarta183a152011-10-10 21:32:43 -040013261 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013262 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13263 }
13264 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13265 shdr_status = bf_get(lpfc_mbox_hdr_status,
13266 &shdr->response);
13267 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13268 &shdr->response);
13269 if (rc != MBX_TIMEOUT)
13270 lpfc_sli4_mbox_cmd_free(phba, mbox);
13271 if (shdr_status || shdr_add_status || rc) {
13272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13273 "2988 POST_SGL_BLOCK mailbox "
13274 "command failed status x%x "
13275 "add_status x%x mbx status x%x\n",
13276 shdr_status, shdr_add_status, rc);
13277 rc = -ENXIO;
13278 goto err_exit;
13279 }
13280 if (ttl_cnt >= els_xri_cnt)
13281 break;
13282 }
13283
13284 err_exit:
13285 if (rc == 0)
13286 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13287 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040013288 return rc;
13289}
13290
13291/**
13292 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13293 * @phba: pointer to lpfc hba data structure.
13294 * @sblist: pointer to scsi buffer list.
13295 * @count: number of scsi buffers on the list.
13296 *
13297 * This routine is invoked to post a block of @count scsi sgl pages from a
13298 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13299 * No Lock is held.
13300 *
13301 **/
13302int
13303lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13304 int cnt)
13305{
13306 struct lpfc_scsi_buf *psb;
13307 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13308 struct sgl_page_pairs *sgl_pg_pairs;
13309 void *viraddr;
13310 LPFC_MBOXQ_t *mbox;
13311 uint32_t reqlen, alloclen, pg_pairs;
13312 uint32_t mbox_tmo;
13313 uint16_t xritag_start = 0;
13314 int rc = 0;
13315 uint32_t shdr_status, shdr_add_status;
13316 dma_addr_t pdma_phys_bpl1;
13317 union lpfc_sli4_cfg_shdr *shdr;
13318
13319 /* Calculate the requested length of the dma memory */
13320 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13321 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013322 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013323 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13324 "0217 Block sgl registration required DMA "
13325 "size (%d) great than a page\n", reqlen);
13326 return -ENOMEM;
13327 }
13328 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13329 if (!mbox) {
13330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13331 "0283 Failed to allocate mbox cmd memory\n");
13332 return -ENOMEM;
13333 }
13334
13335 /* Allocate DMA memory and set up the non-embedded mailbox command */
13336 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13337 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13338 LPFC_SLI4_MBX_NEMBED);
13339
13340 if (alloclen < reqlen) {
13341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13342 "2561 Allocated DMA memory size (%d) is "
13343 "less than the requested DMA memory "
13344 "size (%d)\n", alloclen, reqlen);
13345 lpfc_sli4_mbox_cmd_free(phba, mbox);
13346 return -ENOMEM;
13347 }
James Smart6d368e52011-05-24 11:44:12 -040013348
James Smart4f774512009-05-22 14:52:35 -040013349 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013350 viraddr = mbox->sge_array->addr[0];
13351
13352 /* Set up the SGL pages in the non-embedded DMA pages */
13353 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13354 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13355
13356 pg_pairs = 0;
13357 list_for_each_entry(psb, sblist, list) {
13358 /* Set up the sge entry */
13359 sgl_pg_pairs->sgl_pg0_addr_lo =
13360 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13361 sgl_pg_pairs->sgl_pg0_addr_hi =
13362 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13363 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13364 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13365 else
13366 pdma_phys_bpl1 = 0;
13367 sgl_pg_pairs->sgl_pg1_addr_lo =
13368 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13369 sgl_pg_pairs->sgl_pg1_addr_hi =
13370 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13371 /* Keep the first xritag on the list */
13372 if (pg_pairs == 0)
13373 xritag_start = psb->cur_iocbq.sli4_xritag;
13374 sgl_pg_pairs++;
13375 pg_pairs++;
13376 }
13377 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13378 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13379 /* Perform endian conversion if necessary */
13380 sgl->word0 = cpu_to_le32(sgl->word0);
13381
13382 if (!phba->sli4_hba.intr_enable)
13383 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13384 else {
James Smarta183a152011-10-10 21:32:43 -040013385 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013386 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13387 }
13388 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13389 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13390 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13391 if (rc != MBX_TIMEOUT)
13392 lpfc_sli4_mbox_cmd_free(phba, mbox);
13393 if (shdr_status || shdr_add_status || rc) {
13394 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13395 "2564 POST_SGL_BLOCK mailbox command failed "
13396 "status x%x add_status x%x mbx status x%x\n",
13397 shdr_status, shdr_add_status, rc);
13398 rc = -ENXIO;
13399 }
13400 return rc;
13401}
13402
13403/**
James Smart6d368e52011-05-24 11:44:12 -040013404 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13405 * @phba: pointer to lpfc hba data structure.
13406 * @sblist: pointer to scsi buffer list.
13407 * @count: number of scsi buffers on the list.
13408 *
13409 * This routine is invoked to post a block of @count scsi sgl pages from a
13410 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13411 * No Lock is held.
13412 *
13413 **/
13414int
13415lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13416 int cnt)
13417{
13418 struct lpfc_scsi_buf *psb = NULL;
13419 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13420 struct sgl_page_pairs *sgl_pg_pairs;
13421 void *viraddr;
13422 LPFC_MBOXQ_t *mbox;
13423 uint32_t reqlen, alloclen, pg_pairs;
13424 uint32_t mbox_tmo;
13425 uint16_t xri_start = 0, scsi_xri_start;
13426 uint16_t rsrc_range;
13427 int rc = 0, avail_cnt;
13428 uint32_t shdr_status, shdr_add_status;
13429 dma_addr_t pdma_phys_bpl1;
13430 union lpfc_sli4_cfg_shdr *shdr;
13431 struct lpfc_rsrc_blks *rsrc_blk;
13432 uint32_t xri_cnt = 0;
13433
13434 /* Calculate the total requested length of the dma memory */
13435 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13436 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13437 if (reqlen > SLI4_PAGE_SIZE) {
13438 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13439 "2932 Block sgl registration required DMA "
13440 "size (%d) great than a page\n", reqlen);
13441 return -ENOMEM;
13442 }
13443
13444 /*
13445 * The use of extents requires the driver to post the sgl headers
13446 * in multiple postings to meet the contiguous resource assignment.
13447 */
13448 psb = list_prepare_entry(psb, sblist, list);
13449 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13450 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13451 list) {
13452 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13453 if (rsrc_range < scsi_xri_start)
13454 continue;
13455 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13456 continue;
13457 else
13458 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13459
13460 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13461 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13462 /*
13463 * Allocate DMA memory and set up the non-embedded mailbox
13464 * command. The mbox is used to post an SGL page per loop
13465 * but the DMA memory has a use-once semantic so the mailbox
13466 * is used and freed per loop pass.
13467 */
13468 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13469 if (!mbox) {
13470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13471 "2933 Failed to allocate mbox cmd "
13472 "memory\n");
13473 return -ENOMEM;
13474 }
13475 alloclen = lpfc_sli4_config(phba, mbox,
13476 LPFC_MBOX_SUBSYSTEM_FCOE,
13477 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13478 reqlen,
13479 LPFC_SLI4_MBX_NEMBED);
13480 if (alloclen < reqlen) {
13481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13482 "2934 Allocated DMA memory size (%d) "
13483 "is less than the requested DMA memory "
13484 "size (%d)\n", alloclen, reqlen);
13485 lpfc_sli4_mbox_cmd_free(phba, mbox);
13486 return -ENOMEM;
13487 }
13488
13489 /* Get the first SGE entry from the non-embedded DMA memory */
13490 viraddr = mbox->sge_array->addr[0];
13491
13492 /* Set up the SGL pages in the non-embedded DMA pages */
13493 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13494 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13495
13496 /* pg_pairs tracks posted SGEs per loop iteration. */
13497 pg_pairs = 0;
13498 list_for_each_entry_continue(psb, sblist, list) {
13499 /* Set up the sge entry */
13500 sgl_pg_pairs->sgl_pg0_addr_lo =
13501 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13502 sgl_pg_pairs->sgl_pg0_addr_hi =
13503 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13504 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13505 pdma_phys_bpl1 = psb->dma_phys_bpl +
13506 SGL_PAGE_SIZE;
13507 else
13508 pdma_phys_bpl1 = 0;
13509 sgl_pg_pairs->sgl_pg1_addr_lo =
13510 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13511 sgl_pg_pairs->sgl_pg1_addr_hi =
13512 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13513 /* Keep the first xri for this extent. */
13514 if (pg_pairs == 0)
13515 xri_start = psb->cur_iocbq.sli4_xritag;
13516 sgl_pg_pairs++;
13517 pg_pairs++;
13518 xri_cnt++;
13519
13520 /*
13521 * Track two exit conditions - the loop has constructed
13522 * all of the caller's SGE pairs or all available
13523 * resource IDs in this extent are consumed.
13524 */
13525 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13526 break;
13527 }
13528 rsrc_blk->rsrc_used += pg_pairs;
13529 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13530 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13531
13532 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13533 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13534 "blk use %d\n",
13535 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13536 /* Perform endian conversion if necessary */
13537 sgl->word0 = cpu_to_le32(sgl->word0);
13538 if (!phba->sli4_hba.intr_enable)
13539 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13540 else {
James Smarta183a152011-10-10 21:32:43 -040013541 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013542 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13543 }
13544 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13545 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13546 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13547 &shdr->response);
13548 if (rc != MBX_TIMEOUT)
13549 lpfc_sli4_mbox_cmd_free(phba, mbox);
13550 if (shdr_status || shdr_add_status || rc) {
13551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13552 "2935 POST_SGL_BLOCK mailbox command "
13553 "failed status x%x add_status x%x "
13554 "mbx status x%x\n",
13555 shdr_status, shdr_add_status, rc);
13556 return -ENXIO;
13557 }
13558
13559 /* Post only what is requested. */
13560 if (xri_cnt >= cnt)
13561 break;
13562 }
13563 return rc;
13564}
13565
13566/**
James Smart4f774512009-05-22 14:52:35 -040013567 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13568 * @phba: pointer to lpfc_hba struct that the frame was received on
13569 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13570 *
13571 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13572 * valid type of frame that the LPFC driver will handle. This function will
13573 * return a zero if the frame is a valid frame or a non zero value when the
13574 * frame does not pass the check.
13575 **/
13576static int
13577lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13578{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013579 /* make rctl_names static to save stack space */
13580 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013581 char *type_names[] = FC_TYPE_NAMES_INIT;
13582 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013583 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013584
13585 switch (fc_hdr->fh_r_ctl) {
13586 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13587 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13588 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13589 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13590 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13591 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13592 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13593 case FC_RCTL_DD_CMD_STATUS: /* command status */
13594 case FC_RCTL_ELS_REQ: /* extended link services request */
13595 case FC_RCTL_ELS_REP: /* extended link services reply */
13596 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13597 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13598 case FC_RCTL_BA_NOP: /* basic link service NOP */
13599 case FC_RCTL_BA_ABTS: /* basic link service abort */
13600 case FC_RCTL_BA_RMC: /* remove connection */
13601 case FC_RCTL_BA_ACC: /* basic accept */
13602 case FC_RCTL_BA_RJT: /* basic reject */
13603 case FC_RCTL_BA_PRMT:
13604 case FC_RCTL_ACK_1: /* acknowledge_1 */
13605 case FC_RCTL_ACK_0: /* acknowledge_0 */
13606 case FC_RCTL_P_RJT: /* port reject */
13607 case FC_RCTL_F_RJT: /* fabric reject */
13608 case FC_RCTL_P_BSY: /* port busy */
13609 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13610 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13611 case FC_RCTL_LCR: /* link credit reset */
13612 case FC_RCTL_END: /* end */
13613 break;
13614 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13615 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13616 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13617 return lpfc_fc_frame_check(phba, fc_hdr);
13618 default:
13619 goto drop;
13620 }
13621 switch (fc_hdr->fh_type) {
13622 case FC_TYPE_BLS:
13623 case FC_TYPE_ELS:
13624 case FC_TYPE_FCP:
13625 case FC_TYPE_CT:
13626 break;
13627 case FC_TYPE_IP:
13628 case FC_TYPE_ILS:
13629 default:
13630 goto drop;
13631 }
James Smart546fc852011-03-11 16:06:29 -050013632
James Smart4f774512009-05-22 14:52:35 -040013633 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013634 "2538 Received frame rctl:%s type:%s "
13635 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013636 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013637 type_names[fc_hdr->fh_type],
13638 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13639 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13640 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013641 return 0;
13642drop:
13643 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13644 "2539 Dropped frame rctl:%s type:%s\n",
13645 rctl_names[fc_hdr->fh_r_ctl],
13646 type_names[fc_hdr->fh_type]);
13647 return 1;
13648}
13649
13650/**
13651 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13652 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13653 *
13654 * This function processes the FC header to retrieve the VFI from the VF
13655 * header, if one exists. This function will return the VFI if one exists
13656 * or 0 if no VSAN Header exists.
13657 **/
13658static uint32_t
13659lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13660{
13661 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13662
13663 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13664 return 0;
13665 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13666}
13667
13668/**
13669 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13670 * @phba: Pointer to the HBA structure to search for the vport on
13671 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13672 * @fcfi: The FC Fabric ID that the frame came from
13673 *
13674 * This function searches the @phba for a vport that matches the content of the
13675 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13676 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13677 * returns the matching vport pointer or NULL if unable to match frame to a
13678 * vport.
13679 **/
13680static struct lpfc_vport *
13681lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13682 uint16_t fcfi)
13683{
13684 struct lpfc_vport **vports;
13685 struct lpfc_vport *vport = NULL;
13686 int i;
13687 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13688 fc_hdr->fh_d_id[1] << 8 |
13689 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013690 if (did == Fabric_DID)
13691 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013692 vports = lpfc_create_vport_work_array(phba);
13693 if (vports != NULL)
13694 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13695 if (phba->fcf.fcfi == fcfi &&
13696 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13697 vports[i]->fc_myDID == did) {
13698 vport = vports[i];
13699 break;
13700 }
13701 }
13702 lpfc_destroy_vport_work_array(phba, vports);
13703 return vport;
13704}
13705
13706/**
James Smart45ed1192009-10-02 15:17:02 -040013707 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13708 * @vport: The vport to work on.
13709 *
13710 * This function updates the receive sequence time stamp for this vport. The
13711 * receive sequence time stamp indicates the time that the last frame of the
13712 * the sequence that has been idle for the longest amount of time was received.
13713 * the driver uses this time stamp to indicate if any received sequences have
13714 * timed out.
13715 **/
13716void
13717lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13718{
13719 struct lpfc_dmabuf *h_buf;
13720 struct hbq_dmabuf *dmabuf = NULL;
13721
13722 /* get the oldest sequence on the rcv list */
13723 h_buf = list_get_first(&vport->rcv_buffer_list,
13724 struct lpfc_dmabuf, list);
13725 if (!h_buf)
13726 return;
13727 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13728 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13729}
13730
13731/**
13732 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13733 * @vport: The vport that the received sequences were sent to.
13734 *
13735 * This function cleans up all outstanding received sequences. This is called
13736 * by the driver when a link event or user action invalidates all the received
13737 * sequences.
13738 **/
13739void
13740lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13741{
13742 struct lpfc_dmabuf *h_buf, *hnext;
13743 struct lpfc_dmabuf *d_buf, *dnext;
13744 struct hbq_dmabuf *dmabuf = NULL;
13745
13746 /* start with the oldest sequence on the rcv list */
13747 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13748 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13749 list_del_init(&dmabuf->hbuf.list);
13750 list_for_each_entry_safe(d_buf, dnext,
13751 &dmabuf->dbuf.list, list) {
13752 list_del_init(&d_buf->list);
13753 lpfc_in_buf_free(vport->phba, d_buf);
13754 }
13755 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13756 }
13757}
13758
13759/**
13760 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13761 * @vport: The vport that the received sequences were sent to.
13762 *
13763 * This function determines whether any received sequences have timed out by
13764 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13765 * indicates that there is at least one timed out sequence this routine will
13766 * go through the received sequences one at a time from most inactive to most
13767 * active to determine which ones need to be cleaned up. Once it has determined
13768 * that a sequence needs to be cleaned up it will simply free up the resources
13769 * without sending an abort.
13770 **/
13771void
13772lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13773{
13774 struct lpfc_dmabuf *h_buf, *hnext;
13775 struct lpfc_dmabuf *d_buf, *dnext;
13776 struct hbq_dmabuf *dmabuf = NULL;
13777 unsigned long timeout;
13778 int abort_count = 0;
13779
13780 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13781 vport->rcv_buffer_time_stamp);
13782 if (list_empty(&vport->rcv_buffer_list) ||
13783 time_before(jiffies, timeout))
13784 return;
13785 /* start with the oldest sequence on the rcv list */
13786 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13787 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13788 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13789 dmabuf->time_stamp);
13790 if (time_before(jiffies, timeout))
13791 break;
13792 abort_count++;
13793 list_del_init(&dmabuf->hbuf.list);
13794 list_for_each_entry_safe(d_buf, dnext,
13795 &dmabuf->dbuf.list, list) {
13796 list_del_init(&d_buf->list);
13797 lpfc_in_buf_free(vport->phba, d_buf);
13798 }
13799 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13800 }
13801 if (abort_count)
13802 lpfc_update_rcv_time_stamp(vport);
13803}
13804
13805/**
James Smart4f774512009-05-22 14:52:35 -040013806 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13807 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13808 *
13809 * This function searches through the existing incomplete sequences that have
13810 * been sent to this @vport. If the frame matches one of the incomplete
13811 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13812 * make up that sequence. If no sequence is found that matches this frame then
13813 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13814 * This function returns a pointer to the first dmabuf in the sequence list that
13815 * the frame was linked to.
13816 **/
13817static struct hbq_dmabuf *
13818lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13819{
13820 struct fc_frame_header *new_hdr;
13821 struct fc_frame_header *temp_hdr;
13822 struct lpfc_dmabuf *d_buf;
13823 struct lpfc_dmabuf *h_buf;
13824 struct hbq_dmabuf *seq_dmabuf = NULL;
13825 struct hbq_dmabuf *temp_dmabuf = NULL;
13826
James Smart4d9ab992009-10-02 15:16:39 -040013827 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013828 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013829 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13830 /* Use the hdr_buf to find the sequence that this frame belongs to */
13831 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13832 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13833 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13834 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13835 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13836 continue;
13837 /* found a pending sequence that matches this frame */
13838 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13839 break;
13840 }
13841 if (!seq_dmabuf) {
13842 /*
13843 * This indicates first frame received for this sequence.
13844 * Queue the buffer on the vport's rcv_buffer_list.
13845 */
13846 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013847 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013848 return dmabuf;
13849 }
13850 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013851 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13852 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013853 list_del_init(&seq_dmabuf->hbuf.list);
13854 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13855 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013856 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013857 return dmabuf;
13858 }
James Smart45ed1192009-10-02 15:17:02 -040013859 /* move this sequence to the tail to indicate a young sequence */
13860 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13861 seq_dmabuf->time_stamp = jiffies;
13862 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013863 if (list_empty(&seq_dmabuf->dbuf.list)) {
13864 temp_hdr = dmabuf->hbuf.virt;
13865 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13866 return seq_dmabuf;
13867 }
James Smart4f774512009-05-22 14:52:35 -040013868 /* find the correct place in the sequence to insert this frame */
13869 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13870 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13871 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13872 /*
13873 * If the frame's sequence count is greater than the frame on
13874 * the list then insert the frame right after this frame
13875 */
James Smarteeead812009-12-21 17:01:23 -050013876 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13877 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013878 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13879 return seq_dmabuf;
13880 }
13881 }
13882 return NULL;
13883}
13884
13885/**
James Smart6669f9b2009-10-02 15:16:45 -040013886 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13887 * @vport: pointer to a vitural port
13888 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13889 *
13890 * This function tries to abort from the partially assembed sequence, described
13891 * by the information from basic abbort @dmabuf. It checks to see whether such
13892 * partially assembled sequence held by the driver. If so, it shall free up all
13893 * the frames from the partially assembled sequence.
13894 *
13895 * Return
13896 * true -- if there is matching partially assembled sequence present and all
13897 * the frames freed with the sequence;
13898 * false -- if there is no matching partially assembled sequence present so
13899 * nothing got aborted in the lower layer driver
13900 **/
13901static bool
13902lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13903 struct hbq_dmabuf *dmabuf)
13904{
13905 struct fc_frame_header *new_hdr;
13906 struct fc_frame_header *temp_hdr;
13907 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13908 struct hbq_dmabuf *seq_dmabuf = NULL;
13909
13910 /* Use the hdr_buf to find the sequence that matches this frame */
13911 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13912 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13913 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13914 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13915 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13916 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13917 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13918 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13919 continue;
13920 /* found a pending sequence that matches this frame */
13921 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13922 break;
13923 }
13924
13925 /* Free up all the frames from the partially assembled sequence */
13926 if (seq_dmabuf) {
13927 list_for_each_entry_safe(d_buf, n_buf,
13928 &seq_dmabuf->dbuf.list, list) {
13929 list_del_init(&d_buf->list);
13930 lpfc_in_buf_free(vport->phba, d_buf);
13931 }
13932 return true;
13933 }
13934 return false;
13935}
13936
13937/**
James Smart546fc852011-03-11 16:06:29 -050013938 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013939 * @phba: Pointer to HBA context object.
13940 * @cmd_iocbq: pointer to the command iocbq structure.
13941 * @rsp_iocbq: pointer to the response iocbq structure.
13942 *
James Smart546fc852011-03-11 16:06:29 -050013943 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013944 * event. It properly releases the memory allocated to the sequence abort
13945 * accept iocb.
13946 **/
13947static void
James Smart546fc852011-03-11 16:06:29 -050013948lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013949 struct lpfc_iocbq *cmd_iocbq,
13950 struct lpfc_iocbq *rsp_iocbq)
13951{
13952 if (cmd_iocbq)
13953 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13954}
13955
13956/**
James Smart6d368e52011-05-24 11:44:12 -040013957 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13958 * @phba: Pointer to HBA context object.
13959 * @xri: xri id in transaction.
13960 *
13961 * This function validates the xri maps to the known range of XRIs allocated an
13962 * used by the driver.
13963 **/
James Smart7851fe22011-07-22 18:36:52 -040013964uint16_t
James Smart6d368e52011-05-24 11:44:12 -040013965lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13966 uint16_t xri)
13967{
13968 int i;
13969
13970 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13971 if (xri == phba->sli4_hba.xri_ids[i])
13972 return i;
13973 }
13974 return NO_XRI;
13975}
13976
13977
13978/**
James Smart546fc852011-03-11 16:06:29 -050013979 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013980 * @phba: Pointer to HBA context object.
13981 * @fc_hdr: pointer to a FC frame header.
13982 *
James Smart546fc852011-03-11 16:06:29 -050013983 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013984 * event after aborting the sequence handling.
13985 **/
13986static void
James Smart546fc852011-03-11 16:06:29 -050013987lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013988 struct fc_frame_header *fc_hdr)
13989{
13990 struct lpfc_iocbq *ctiocb = NULL;
13991 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050013992 uint16_t oxid, rxid;
13993 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013994 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013995 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013996
13997 if (!lpfc_is_link_up(phba))
13998 return;
13999
14000 sid = sli4_sid_from_fc_hdr(fc_hdr);
14001 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014002 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014003
14004 ndlp = lpfc_findnode_did(phba->pport, sid);
14005 if (!ndlp) {
14006 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14007 "1268 Find ndlp returned NULL for oxid:x%x "
14008 "SID:x%x\n", oxid, sid);
14009 return;
14010 }
James Smart6d368e52011-05-24 11:44:12 -040014011 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050014012 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040014013
James Smart546fc852011-03-11 16:06:29 -050014014 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014015 ctiocb = lpfc_sli_get_iocbq(phba);
14016 if (!ctiocb)
14017 return;
14018
James Smart5ffc2662009-11-18 15:39:44 -050014019 /* Extract the F_CTL field from FC_HDR */
14020 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14021
James Smart6669f9b2009-10-02 15:16:45 -040014022 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014023 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014024 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014025 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14026 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14027 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14028
14029 /* Fill in the rest of iocb fields */
14030 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14031 icmd->ulpBdeCount = 0;
14032 icmd->ulpLe = 1;
14033 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014034 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014035 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014036
James Smart6669f9b2009-10-02 15:16:45 -040014037 ctiocb->iocb_cmpl = NULL;
14038 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014039 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014040 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014041 ctiocb->sli4_xritag = NO_XRI;
14042
14043 /* If the oxid maps to the FCP XRI range or if it is out of range,
14044 * send a BLS_RJT. The driver no longer has that exchange.
14045 * Override the IOCB for a BA_RJT.
14046 */
14047 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
14048 phba->sli4_hba.max_cfg_param.xri_base) ||
14049 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
14050 phba->sli4_hba.max_cfg_param.xri_base)) {
14051 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14052 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14053 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14054 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14055 }
James Smart6669f9b2009-10-02 15:16:45 -040014056
James Smart5ffc2662009-11-18 15:39:44 -050014057 if (fctl & FC_FC_EX_CTX) {
14058 /* ABTS sent by responder to CT exchange, construction
14059 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14060 * field and RX_ID from ABTS for RX_ID field.
14061 */
James Smart546fc852011-03-11 16:06:29 -050014062 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14063 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050014064 } else {
14065 /* ABTS sent by initiator to CT exchange, construction
14066 * of BA_ACC will need to allocate a new XRI as for the
14067 * XRI_TAG and RX_ID fields.
14068 */
James Smart546fc852011-03-11 16:06:29 -050014069 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14070 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050014071 }
James Smart546fc852011-03-11 16:06:29 -050014072 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014073
James Smart546fc852011-03-11 16:06:29 -050014074 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014075 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014076 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14077 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14078
14079 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14080 if (rc == IOCB_ERROR) {
14081 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14082 "2925 Failed to issue CT ABTS RSP x%x on "
14083 "xri x%x, Data x%x\n",
14084 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14085 phba->link_state);
14086 lpfc_sli_release_iocbq(phba, ctiocb);
14087 }
James Smart6669f9b2009-10-02 15:16:45 -040014088}
14089
14090/**
14091 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14092 * @vport: Pointer to the vport on which this sequence was received
14093 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14094 *
14095 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14096 * receive sequence is only partially assembed by the driver, it shall abort
14097 * the partially assembled frames for the sequence. Otherwise, if the
14098 * unsolicited receive sequence has been completely assembled and passed to
14099 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14100 * unsolicited sequence has been aborted. After that, it will issue a basic
14101 * accept to accept the abort.
14102 **/
14103void
14104lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14105 struct hbq_dmabuf *dmabuf)
14106{
14107 struct lpfc_hba *phba = vport->phba;
14108 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014109 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014110 bool abts_par;
14111
James Smart6669f9b2009-10-02 15:16:45 -040014112 /* Make a copy of fc_hdr before the dmabuf being released */
14113 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014114 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014115
James Smart5ffc2662009-11-18 15:39:44 -050014116 if (fctl & FC_FC_EX_CTX) {
14117 /*
14118 * ABTS sent by responder to exchange, just free the buffer
14119 */
James Smart6669f9b2009-10-02 15:16:45 -040014120 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014121 } else {
14122 /*
14123 * ABTS sent by initiator to exchange, need to do cleanup
14124 */
14125 /* Try to abort partially assembled seq */
14126 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14127
14128 /* Send abort to ULP if partially seq abort failed */
14129 if (abts_par == false)
14130 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14131 else
14132 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14133 }
James Smart6669f9b2009-10-02 15:16:45 -040014134 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014135 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014136}
14137
14138/**
James Smart4f774512009-05-22 14:52:35 -040014139 * lpfc_seq_complete - Indicates if a sequence is complete
14140 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14141 *
14142 * This function checks the sequence, starting with the frame described by
14143 * @dmabuf, to see if all the frames associated with this sequence are present.
14144 * the frames associated with this sequence are linked to the @dmabuf using the
14145 * dbuf list. This function looks for two major things. 1) That the first frame
14146 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14147 * set. 3) That there are no holes in the sequence count. The function will
14148 * return 1 when the sequence is complete, otherwise it will return 0.
14149 **/
14150static int
14151lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14152{
14153 struct fc_frame_header *hdr;
14154 struct lpfc_dmabuf *d_buf;
14155 struct hbq_dmabuf *seq_dmabuf;
14156 uint32_t fctl;
14157 int seq_count = 0;
14158
14159 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14160 /* make sure first fame of sequence has a sequence count of zero */
14161 if (hdr->fh_seq_cnt != seq_count)
14162 return 0;
14163 fctl = (hdr->fh_f_ctl[0] << 16 |
14164 hdr->fh_f_ctl[1] << 8 |
14165 hdr->fh_f_ctl[2]);
14166 /* If last frame of sequence we can return success. */
14167 if (fctl & FC_FC_END_SEQ)
14168 return 1;
14169 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14170 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14171 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14172 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014173 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014174 return 0;
14175 fctl = (hdr->fh_f_ctl[0] << 16 |
14176 hdr->fh_f_ctl[1] << 8 |
14177 hdr->fh_f_ctl[2]);
14178 /* If last frame of sequence we can return success. */
14179 if (fctl & FC_FC_END_SEQ)
14180 return 1;
14181 }
14182 return 0;
14183}
14184
14185/**
14186 * lpfc_prep_seq - Prep sequence for ULP processing
14187 * @vport: Pointer to the vport on which this sequence was received
14188 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14189 *
14190 * This function takes a sequence, described by a list of frames, and creates
14191 * a list of iocbq structures to describe the sequence. This iocbq list will be
14192 * used to issue to the generic unsolicited sequence handler. This routine
14193 * returns a pointer to the first iocbq in the list. If the function is unable
14194 * to allocate an iocbq then it throw out the received frames that were not
14195 * able to be described and return a pointer to the first iocbq. If unable to
14196 * allocate any iocbqs (including the first) this function will return NULL.
14197 **/
14198static struct lpfc_iocbq *
14199lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14200{
James Smart7851fe22011-07-22 18:36:52 -040014201 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014202 struct lpfc_dmabuf *d_buf, *n_buf;
14203 struct lpfc_iocbq *first_iocbq, *iocbq;
14204 struct fc_frame_header *fc_hdr;
14205 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014206 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014207 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014208
14209 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14210 /* remove from receive buffer list */
14211 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014212 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014213 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014214 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014215 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014216 /* Get an iocbq struct to fill in. */
14217 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14218 if (first_iocbq) {
14219 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014220 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014221 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14222 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014223 first_iocbq->iocb.ulpContext = NO_XRI;
14224 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14225 be16_to_cpu(fc_hdr->fh_ox_id);
14226 /* iocbq is prepped for internal consumption. Physical vpi. */
14227 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14228 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014229 /* put the first buffer into the first IOCBq */
14230 first_iocbq->context2 = &seq_dmabuf->dbuf;
14231 first_iocbq->context3 = NULL;
14232 first_iocbq->iocb.ulpBdeCount = 1;
14233 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14234 LPFC_DATA_BUF_SIZE;
14235 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014236 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014237 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014238 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014239 }
14240 iocbq = first_iocbq;
14241 /*
14242 * Each IOCBq can have two Buffers assigned, so go through the list
14243 * of buffers for this sequence and save two buffers in each IOCBq
14244 */
14245 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14246 if (!iocbq) {
14247 lpfc_in_buf_free(vport->phba, d_buf);
14248 continue;
14249 }
14250 if (!iocbq->context3) {
14251 iocbq->context3 = d_buf;
14252 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014253 pbde = (struct ulp_bde64 *)
14254 &iocbq->iocb.unsli3.sli3Words[4];
14255 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014256
14257 /* We need to get the size out of the right CQE */
14258 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14259 len = bf_get(lpfc_rcqe_length,
14260 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14261 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14262 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014263 } else {
14264 iocbq = lpfc_sli_get_iocbq(vport->phba);
14265 if (!iocbq) {
14266 if (first_iocbq) {
14267 first_iocbq->iocb.ulpStatus =
14268 IOSTAT_FCP_RSP_ERROR;
14269 first_iocbq->iocb.un.ulpWord[4] =
14270 IOERR_NO_RESOURCES;
14271 }
14272 lpfc_in_buf_free(vport->phba, d_buf);
14273 continue;
14274 }
14275 iocbq->context2 = d_buf;
14276 iocbq->context3 = NULL;
14277 iocbq->iocb.ulpBdeCount = 1;
14278 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14279 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014280
14281 /* We need to get the size out of the right CQE */
14282 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14283 len = bf_get(lpfc_rcqe_length,
14284 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14285 tot_len += len;
14286 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14287
James Smart4f774512009-05-22 14:52:35 -040014288 iocbq->iocb.un.rcvels.remoteID = sid;
14289 list_add_tail(&iocbq->list, &first_iocbq->list);
14290 }
14291 }
14292 return first_iocbq;
14293}
14294
James Smart6669f9b2009-10-02 15:16:45 -040014295static void
14296lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14297 struct hbq_dmabuf *seq_dmabuf)
14298{
14299 struct fc_frame_header *fc_hdr;
14300 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14301 struct lpfc_hba *phba = vport->phba;
14302
14303 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14304 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14305 if (!iocbq) {
14306 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14307 "2707 Ring %d handler: Failed to allocate "
14308 "iocb Rctl x%x Type x%x received\n",
14309 LPFC_ELS_RING,
14310 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14311 return;
14312 }
14313 if (!lpfc_complete_unsol_iocb(phba,
14314 &phba->sli.ring[LPFC_ELS_RING],
14315 iocbq, fc_hdr->fh_r_ctl,
14316 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014318 "2540 Ring %d handler: unexpected Rctl "
14319 "x%x Type x%x received\n",
14320 LPFC_ELS_RING,
14321 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14322
14323 /* Free iocb created in lpfc_prep_seq */
14324 list_for_each_entry_safe(curr_iocb, next_iocb,
14325 &iocbq->list, list) {
14326 list_del_init(&curr_iocb->list);
14327 lpfc_sli_release_iocbq(phba, curr_iocb);
14328 }
14329 lpfc_sli_release_iocbq(phba, iocbq);
14330}
14331
James Smart4f774512009-05-22 14:52:35 -040014332/**
14333 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14334 * @phba: Pointer to HBA context object.
14335 *
14336 * This function is called with no lock held. This function processes all
14337 * the received buffers and gives it to upper layers when a received buffer
14338 * indicates that it is the final frame in the sequence. The interrupt
14339 * service routine processes received buffers at interrupt contexts and adds
14340 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14341 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14342 * appropriate receive function when the final frame in a sequence is received.
14343 **/
James Smart4d9ab992009-10-02 15:16:39 -040014344void
14345lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14346 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014347{
James Smart4d9ab992009-10-02 15:16:39 -040014348 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014349 struct fc_frame_header *fc_hdr;
14350 struct lpfc_vport *vport;
14351 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014352
James Smart4f774512009-05-22 14:52:35 -040014353 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014354 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14355 /* check to see if this a valid type of frame */
14356 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14357 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14358 return;
14359 }
James Smart7851fe22011-07-22 18:36:52 -040014360 if ((bf_get(lpfc_cqe_code,
14361 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14362 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14363 &dmabuf->cq_event.cqe.rcqe_cmpl);
14364 else
14365 fcfi = bf_get(lpfc_rcqe_fcf_id,
14366 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014367 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014368 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014369 /* throw out the frame */
14370 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14371 return;
14372 }
James Smart6669f9b2009-10-02 15:16:45 -040014373 /* Handle the basic abort sequence (BA_ABTS) event */
14374 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14375 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14376 return;
14377 }
14378
James Smart4d9ab992009-10-02 15:16:39 -040014379 /* Link this frame */
14380 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14381 if (!seq_dmabuf) {
14382 /* unable to add frame to vport - throw it out */
14383 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14384 return;
14385 }
14386 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014387 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014388 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014389
James Smart6669f9b2009-10-02 15:16:45 -040014390 /* Send the complete sequence to the upper layer protocol */
14391 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014392}
James Smart6fb120a2009-05-22 14:52:59 -040014393
14394/**
14395 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14396 * @phba: pointer to lpfc hba data structure.
14397 *
14398 * This routine is invoked to post rpi header templates to the
14399 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014400 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14401 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014402 *
14403 * This routine does not require any locks. It's usage is expected
14404 * to be driver load or reset recovery when the driver is
14405 * sequential.
14406 *
14407 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014408 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014409 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014410 * When this error occurs, the driver is not guaranteed
14411 * to have any rpi regions posted to the device and
14412 * must either attempt to repost the regions or take a
14413 * fatal error.
14414 **/
14415int
14416lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14417{
14418 struct lpfc_rpi_hdr *rpi_page;
14419 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014420 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014421
James Smart6d368e52011-05-24 11:44:12 -040014422 /* SLI4 ports that support extents do not require RPI headers. */
14423 if (!phba->sli4_hba.rpi_hdrs_in_use)
14424 goto exit;
14425 if (phba->sli4_hba.extents_in_use)
14426 return -EIO;
14427
James Smart6fb120a2009-05-22 14:52:59 -040014428 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014429 /*
14430 * Assign the rpi headers a physical rpi only if the driver
14431 * has not initialized those resources. A port reset only
14432 * needs the headers posted.
14433 */
14434 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14435 LPFC_RPI_RSRC_RDY)
14436 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14437
James Smart6fb120a2009-05-22 14:52:59 -040014438 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14439 if (rc != MBX_SUCCESS) {
14440 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14441 "2008 Error %d posting all rpi "
14442 "headers\n", rc);
14443 rc = -EIO;
14444 break;
14445 }
14446 }
14447
James Smart6d368e52011-05-24 11:44:12 -040014448 exit:
14449 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14450 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014451 return rc;
14452}
14453
14454/**
14455 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14456 * @phba: pointer to lpfc hba data structure.
14457 * @rpi_page: pointer to the rpi memory region.
14458 *
14459 * This routine is invoked to post a single rpi header to the
14460 * HBA consistent with the SLI-4 interface spec. This memory region
14461 * maps up to 64 rpi context regions.
14462 *
14463 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014464 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014465 * -ENOMEM - No available memory
14466 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014467 **/
14468int
14469lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14470{
14471 LPFC_MBOXQ_t *mboxq;
14472 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14473 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014474 uint32_t shdr_status, shdr_add_status;
14475 union lpfc_sli4_cfg_shdr *shdr;
14476
James Smart6d368e52011-05-24 11:44:12 -040014477 /* SLI4 ports that support extents do not require RPI headers. */
14478 if (!phba->sli4_hba.rpi_hdrs_in_use)
14479 return rc;
14480 if (phba->sli4_hba.extents_in_use)
14481 return -EIO;
14482
James Smart6fb120a2009-05-22 14:52:59 -040014483 /* The port is notified of the header region via a mailbox command. */
14484 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14485 if (!mboxq) {
14486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14487 "2001 Unable to allocate memory for issuing "
14488 "SLI_CONFIG_SPECIAL mailbox command\n");
14489 return -ENOMEM;
14490 }
14491
14492 /* Post all rpi memory regions to the port. */
14493 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014494 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14495 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14496 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014497 sizeof(struct lpfc_sli4_cfg_mhdr),
14498 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014499
14500
14501 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014502 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14503 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014504 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14505 hdr_tmpl, rpi_page->page_count);
14506
James Smart6fb120a2009-05-22 14:52:59 -040014507 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14508 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014509 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014510 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14511 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14512 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14513 if (rc != MBX_TIMEOUT)
14514 mempool_free(mboxq, phba->mbox_mem_pool);
14515 if (shdr_status || shdr_add_status || rc) {
14516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14517 "2514 POST_RPI_HDR mailbox failed with "
14518 "status x%x add_status x%x, mbx status x%x\n",
14519 shdr_status, shdr_add_status, rc);
14520 rc = -ENXIO;
14521 }
14522 return rc;
14523}
14524
14525/**
14526 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14527 * @phba: pointer to lpfc hba data structure.
14528 *
14529 * This routine is invoked to post rpi header templates to the
14530 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014531 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14532 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014533 *
14534 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014535 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014536 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14537 **/
14538int
14539lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14540{
James Smart6d368e52011-05-24 11:44:12 -040014541 unsigned long rpi;
14542 uint16_t max_rpi, rpi_limit;
14543 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014544 struct lpfc_rpi_hdr *rpi_hdr;
14545
14546 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014547 rpi_limit = phba->sli4_hba.next_rpi;
14548
14549 /*
James Smart6d368e52011-05-24 11:44:12 -040014550 * Fetch the next logical rpi. Because this index is logical,
14551 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014552 */
14553 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014554 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14555 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014556 rpi = LPFC_RPI_ALLOC_ERROR;
14557 else {
14558 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14559 phba->sli4_hba.max_cfg_param.rpi_used++;
14560 phba->sli4_hba.rpi_count++;
14561 }
14562
14563 /*
14564 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014565 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014566 */
14567 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14568 (phba->sli4_hba.rpi_count >= max_rpi)) {
14569 spin_unlock_irq(&phba->hbalock);
14570 return rpi;
14571 }
14572
14573 /*
James Smart6d368e52011-05-24 11:44:12 -040014574 * RPI header postings are not required for SLI4 ports capable of
14575 * extents.
14576 */
14577 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14578 spin_unlock_irq(&phba->hbalock);
14579 return rpi;
14580 }
14581
14582 /*
James Smart6fb120a2009-05-22 14:52:59 -040014583 * If the driver is running low on rpi resources, allocate another
14584 * page now. Note that the next_rpi value is used because
14585 * it represents how many are actually in use whereas max_rpi notes
14586 * how many are supported max by the device.
14587 */
James Smart6d368e52011-05-24 11:44:12 -040014588 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014589 spin_unlock_irq(&phba->hbalock);
14590 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14591 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14592 if (!rpi_hdr) {
14593 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14594 "2002 Error Could not grow rpi "
14595 "count\n");
14596 } else {
James Smart6d368e52011-05-24 11:44:12 -040014597 lrpi = rpi_hdr->start_rpi;
14598 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014599 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14600 }
14601 }
14602
14603 return rpi;
14604}
14605
14606/**
14607 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14608 * @phba: pointer to lpfc hba data structure.
14609 *
14610 * This routine is invoked to release an rpi to the pool of
14611 * available rpis maintained by the driver.
14612 **/
14613void
James Smartd7c47992010-06-08 18:31:54 -040014614__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14615{
14616 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14617 phba->sli4_hba.rpi_count--;
14618 phba->sli4_hba.max_cfg_param.rpi_used--;
14619 }
14620}
14621
14622/**
14623 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14624 * @phba: pointer to lpfc hba data structure.
14625 *
14626 * This routine is invoked to release an rpi to the pool of
14627 * available rpis maintained by the driver.
14628 **/
14629void
James Smart6fb120a2009-05-22 14:52:59 -040014630lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14631{
14632 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014633 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014634 spin_unlock_irq(&phba->hbalock);
14635}
14636
14637/**
14638 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14639 * @phba: pointer to lpfc hba data structure.
14640 *
14641 * This routine is invoked to remove the memory region that
14642 * provided rpi via a bitmask.
14643 **/
14644void
14645lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14646{
14647 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014648 kfree(phba->sli4_hba.rpi_ids);
14649 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014650}
14651
14652/**
14653 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14654 * @phba: pointer to lpfc hba data structure.
14655 *
14656 * This routine is invoked to remove the memory region that
14657 * provided rpi via a bitmask.
14658 **/
14659int
14660lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14661{
14662 LPFC_MBOXQ_t *mboxq;
14663 struct lpfc_hba *phba = ndlp->phba;
14664 int rc;
14665
14666 /* The port is notified of the header region via a mailbox command. */
14667 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14668 if (!mboxq)
14669 return -ENOMEM;
14670
14671 /* Post all rpi memory regions to the port. */
14672 lpfc_resume_rpi(mboxq, ndlp);
14673 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14674 if (rc == MBX_NOT_FINISHED) {
14675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14676 "2010 Resume RPI Mailbox failed "
14677 "status %d, mbxStatus x%x\n", rc,
14678 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14679 mempool_free(mboxq, phba->mbox_mem_pool);
14680 return -EIO;
14681 }
14682 return 0;
14683}
14684
14685/**
14686 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014687 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014688 *
James Smart76a95d72010-11-20 23:11:48 -050014689 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014690 *
14691 * Returns:
14692 * 0 success
14693 * -Evalue otherwise
14694 **/
14695int
James Smart76a95d72010-11-20 23:11:48 -050014696lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014697{
14698 LPFC_MBOXQ_t *mboxq;
14699 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014700 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014701 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014702 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014703 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14704 if (!mboxq)
14705 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014706 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014707 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014708 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014709 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014710 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014711 "2022 INIT VPI Mailbox failed "
14712 "status %d, mbxStatus x%x\n", rc,
14713 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014714 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014715 }
James Smart6a9c52c2009-10-02 15:16:51 -040014716 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014717 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014718
14719 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014720}
14721
14722/**
14723 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14724 * @phba: pointer to lpfc hba data structure.
14725 * @mboxq: Pointer to mailbox object.
14726 *
14727 * This routine is invoked to manually add a single FCF record. The caller
14728 * must pass a completely initialized FCF_Record. This routine takes
14729 * care of the nonembedded mailbox operations.
14730 **/
14731static void
14732lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14733{
14734 void *virt_addr;
14735 union lpfc_sli4_cfg_shdr *shdr;
14736 uint32_t shdr_status, shdr_add_status;
14737
14738 virt_addr = mboxq->sge_array->addr[0];
14739 /* The IOCTL status is embedded in the mailbox subheader. */
14740 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14741 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14742 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14743
14744 if ((shdr_status || shdr_add_status) &&
14745 (shdr_status != STATUS_FCF_IN_USE))
14746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14747 "2558 ADD_FCF_RECORD mailbox failed with "
14748 "status x%x add_status x%x\n",
14749 shdr_status, shdr_add_status);
14750
14751 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14752}
14753
14754/**
14755 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14756 * @phba: pointer to lpfc hba data structure.
14757 * @fcf_record: pointer to the initialized fcf record to add.
14758 *
14759 * This routine is invoked to manually add a single FCF record. The caller
14760 * must pass a completely initialized FCF_Record. This routine takes
14761 * care of the nonembedded mailbox operations.
14762 **/
14763int
14764lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14765{
14766 int rc = 0;
14767 LPFC_MBOXQ_t *mboxq;
14768 uint8_t *bytep;
14769 void *virt_addr;
14770 dma_addr_t phys_addr;
14771 struct lpfc_mbx_sge sge;
14772 uint32_t alloc_len, req_len;
14773 uint32_t fcfindex;
14774
14775 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14776 if (!mboxq) {
14777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14778 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14779 return -ENOMEM;
14780 }
14781
14782 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14783 sizeof(uint32_t);
14784
14785 /* Allocate DMA memory and set up the non-embedded mailbox command */
14786 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14787 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14788 req_len, LPFC_SLI4_MBX_NEMBED);
14789 if (alloc_len < req_len) {
14790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14791 "2523 Allocated DMA memory size (x%x) is "
14792 "less than the requested DMA memory "
14793 "size (x%x)\n", alloc_len, req_len);
14794 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14795 return -ENOMEM;
14796 }
14797
14798 /*
14799 * Get the first SGE entry from the non-embedded DMA memory. This
14800 * routine only uses a single SGE.
14801 */
14802 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14803 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014804 virt_addr = mboxq->sge_array->addr[0];
14805 /*
14806 * Configure the FCF record for FCFI 0. This is the driver's
14807 * hardcoded default and gets used in nonFIP mode.
14808 */
14809 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14810 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14811 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14812
14813 /*
14814 * Copy the fcf_index and the FCF Record Data. The data starts after
14815 * the FCoE header plus word10. The data copy needs to be endian
14816 * correct.
14817 */
14818 bytep += sizeof(uint32_t);
14819 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14820 mboxq->vport = phba->pport;
14821 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14822 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14823 if (rc == MBX_NOT_FINISHED) {
14824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14825 "2515 ADD_FCF_RECORD mailbox failed with "
14826 "status 0x%x\n", rc);
14827 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14828 rc = -EIO;
14829 } else
14830 rc = 0;
14831
14832 return rc;
14833}
14834
14835/**
14836 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14837 * @phba: pointer to lpfc hba data structure.
14838 * @fcf_record: pointer to the fcf record to write the default data.
14839 * @fcf_index: FCF table entry index.
14840 *
14841 * This routine is invoked to build the driver's default FCF record. The
14842 * values used are hardcoded. This routine handles memory initialization.
14843 *
14844 **/
14845void
14846lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14847 struct fcf_record *fcf_record,
14848 uint16_t fcf_index)
14849{
14850 memset(fcf_record, 0, sizeof(struct fcf_record));
14851 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14852 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14853 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14854 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14855 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14856 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14857 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14858 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14859 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14860 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14861 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14862 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14863 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014864 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014865 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14866 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14867 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14868 /* Set the VLAN bit map */
14869 if (phba->valid_vlan) {
14870 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14871 = 1 << (phba->vlan_id % 8);
14872 }
14873}
14874
14875/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014876 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014877 * @phba: pointer to lpfc hba data structure.
14878 * @fcf_index: FCF table entry offset.
14879 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014880 * This routine is invoked to scan the entire FCF table by reading FCF
14881 * record and processing it one at a time starting from the @fcf_index
14882 * for initial FCF discovery or fast FCF failover rediscovery.
14883 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014884 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014885 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014886 **/
14887int
James Smart0c9ab6f2010-02-26 14:15:57 -050014888lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014889{
14890 int rc = 0, error;
14891 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014892
James Smart32b97932009-07-19 10:01:21 -040014893 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014894 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14895 if (!mboxq) {
14896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14897 "2000 Failed to allocate mbox for "
14898 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014899 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014900 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014901 }
James Smartecfd03c2010-02-12 14:41:27 -050014902 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014903 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014904 if (rc) {
14905 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014906 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014907 }
James Smartecfd03c2010-02-12 14:41:27 -050014908 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014909 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014910 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014911
14912 spin_lock_irq(&phba->hbalock);
14913 phba->hba_flag |= FCF_TS_INPROG;
14914 spin_unlock_irq(&phba->hbalock);
14915
James Smart6fb120a2009-05-22 14:52:59 -040014916 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014917 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014918 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014919 else {
James Smart38b92ef2010-08-04 16:11:39 -040014920 /* Reset eligible FCF count for new scan */
14921 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014922 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014923 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014924 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014925fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014926 if (error) {
14927 if (mboxq)
14928 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014929 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014930 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014931 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014932 spin_unlock_irq(&phba->hbalock);
14933 }
James Smart6fb120a2009-05-22 14:52:59 -040014934 return error;
14935}
James Smarta0c87cb2009-07-19 10:01:10 -040014936
14937/**
James Smarta93ff372010-10-22 11:06:08 -040014938 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014939 * @phba: pointer to lpfc hba data structure.
14940 * @fcf_index: FCF table entry offset.
14941 *
14942 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014943 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014944 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014945 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014946 * otherwise.
14947 **/
14948int
14949lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14950{
14951 int rc = 0, error;
14952 LPFC_MBOXQ_t *mboxq;
14953
14954 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14955 if (!mboxq) {
14956 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14957 "2763 Failed to allocate mbox for "
14958 "READ_FCF cmd\n");
14959 error = -ENOMEM;
14960 goto fail_fcf_read;
14961 }
14962 /* Construct the read FCF record mailbox command */
14963 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14964 if (rc) {
14965 error = -EINVAL;
14966 goto fail_fcf_read;
14967 }
14968 /* Issue the mailbox command asynchronously */
14969 mboxq->vport = phba->pport;
14970 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14971 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14972 if (rc == MBX_NOT_FINISHED)
14973 error = -EIO;
14974 else
14975 error = 0;
14976
14977fail_fcf_read:
14978 if (error && mboxq)
14979 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14980 return error;
14981}
14982
14983/**
14984 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14985 * @phba: pointer to lpfc hba data structure.
14986 * @fcf_index: FCF table entry offset.
14987 *
14988 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040014989 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050014990 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014991 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014992 * otherwise.
14993 **/
14994int
14995lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14996{
14997 int rc = 0, error;
14998 LPFC_MBOXQ_t *mboxq;
14999
15000 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15001 if (!mboxq) {
15002 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15003 "2758 Failed to allocate mbox for "
15004 "READ_FCF cmd\n");
15005 error = -ENOMEM;
15006 goto fail_fcf_read;
15007 }
15008 /* Construct the read FCF record mailbox command */
15009 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15010 if (rc) {
15011 error = -EINVAL;
15012 goto fail_fcf_read;
15013 }
15014 /* Issue the mailbox command asynchronously */
15015 mboxq->vport = phba->pport;
15016 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15017 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15018 if (rc == MBX_NOT_FINISHED)
15019 error = -EIO;
15020 else
15021 error = 0;
15022
15023fail_fcf_read:
15024 if (error && mboxq)
15025 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15026 return error;
15027}
15028
15029/**
James Smart7d791df2011-07-22 18:37:52 -040015030 * lpfc_check_next_fcf_pri
15031 * phba pointer to the lpfc_hba struct for this port.
15032 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15033 * routine when the rr_bmask is empty. The FCF indecies are put into the
15034 * rr_bmask based on their priority level. Starting from the highest priority
15035 * to the lowest. The most likely FCF candidate will be in the highest
15036 * priority group. When this routine is called it searches the fcf_pri list for
15037 * next lowest priority group and repopulates the rr_bmask with only those
15038 * fcf_indexes.
15039 * returns:
15040 * 1=success 0=failure
15041 **/
15042int
15043lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15044{
15045 uint16_t next_fcf_pri;
15046 uint16_t last_index;
15047 struct lpfc_fcf_pri *fcf_pri;
15048 int rc;
15049 int ret = 0;
15050
15051 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15052 LPFC_SLI4_FCF_TBL_INDX_MAX);
15053 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15054 "3060 Last IDX %d\n", last_index);
15055 if (list_empty(&phba->fcf.fcf_pri_list)) {
15056 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15057 "3061 Last IDX %d\n", last_index);
15058 return 0; /* Empty rr list */
15059 }
15060 next_fcf_pri = 0;
15061 /*
15062 * Clear the rr_bmask and set all of the bits that are at this
15063 * priority.
15064 */
15065 memset(phba->fcf.fcf_rr_bmask, 0,
15066 sizeof(*phba->fcf.fcf_rr_bmask));
15067 spin_lock_irq(&phba->hbalock);
15068 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15069 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15070 continue;
15071 /*
15072 * the 1st priority that has not FLOGI failed
15073 * will be the highest.
15074 */
15075 if (!next_fcf_pri)
15076 next_fcf_pri = fcf_pri->fcf_rec.priority;
15077 spin_unlock_irq(&phba->hbalock);
15078 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15079 rc = lpfc_sli4_fcf_rr_index_set(phba,
15080 fcf_pri->fcf_rec.fcf_index);
15081 if (rc)
15082 return 0;
15083 }
15084 spin_lock_irq(&phba->hbalock);
15085 }
15086 /*
15087 * if next_fcf_pri was not set above and the list is not empty then
15088 * we have failed flogis on all of them. So reset flogi failed
15089 * and start at the begining.
15090 */
15091 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15092 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15093 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15094 /*
15095 * the 1st priority that has not FLOGI failed
15096 * will be the highest.
15097 */
15098 if (!next_fcf_pri)
15099 next_fcf_pri = fcf_pri->fcf_rec.priority;
15100 spin_unlock_irq(&phba->hbalock);
15101 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15102 rc = lpfc_sli4_fcf_rr_index_set(phba,
15103 fcf_pri->fcf_rec.fcf_index);
15104 if (rc)
15105 return 0;
15106 }
15107 spin_lock_irq(&phba->hbalock);
15108 }
15109 } else
15110 ret = 1;
15111 spin_unlock_irq(&phba->hbalock);
15112
15113 return ret;
15114}
15115/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015116 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15117 * @phba: pointer to lpfc hba data structure.
15118 *
15119 * This routine is to get the next eligible FCF record index in a round
15120 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015121 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015122 * shall be returned, otherwise, the next eligible FCF record's index
15123 * shall be returned.
15124 **/
15125uint16_t
15126lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15127{
15128 uint16_t next_fcf_index;
15129
James Smart3804dc82010-07-14 15:31:37 -040015130 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015131next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015132 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15133 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015134 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15135 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015136 next_fcf_index);
15137
James Smart0c9ab6f2010-02-26 14:15:57 -050015138 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015139 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15140 /*
15141 * If we have wrapped then we need to clear the bits that
15142 * have been tested so that we can detect when we should
15143 * change the priority level.
15144 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015145 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15146 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015147 }
15148
James Smart0c9ab6f2010-02-26 14:15:57 -050015149
James Smart3804dc82010-07-14 15:31:37 -040015150 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015151 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15152 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15153 /*
15154 * If next fcf index is not found check if there are lower
15155 * Priority level fcf's in the fcf_priority list.
15156 * Set up the rr_bmask with all of the avaiable fcf bits
15157 * at that level and continue the selection process.
15158 */
15159 if (lpfc_check_next_fcf_pri_level(phba))
15160 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015161 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15162 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015163 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15164 return LPFC_FCOE_FCF_NEXT_NONE;
15165 else {
15166 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15167 "3063 Only FCF available idx %d, flag %x\n",
15168 next_fcf_index,
15169 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15170 return next_fcf_index;
15171 }
James Smart3804dc82010-07-14 15:31:37 -040015172 }
15173
James Smart7d791df2011-07-22 18:37:52 -040015174 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15175 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15176 LPFC_FCF_FLOGI_FAILED)
15177 goto next_priority;
15178
James Smart3804dc82010-07-14 15:31:37 -040015179 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015180 "2845 Get next roundrobin failover FCF (x%x)\n",
15181 next_fcf_index);
15182
James Smart0c9ab6f2010-02-26 14:15:57 -050015183 return next_fcf_index;
15184}
15185
15186/**
15187 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15188 * @phba: pointer to lpfc hba data structure.
15189 *
15190 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015191 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015192 * does not go beyond the range of the driver allocated bmask dimension
15193 * before setting the bit.
15194 *
15195 * Returns 0 if the index bit successfully set, otherwise, it returns
15196 * -EINVAL.
15197 **/
15198int
15199lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15200{
15201 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15202 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015203 "2610 FCF (x%x) reached driver's book "
15204 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015205 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15206 return -EINVAL;
15207 }
15208 /* Set the eligible FCF record index bmask */
15209 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15210
James Smart3804dc82010-07-14 15:31:37 -040015211 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015212 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015213 "bmask\n", fcf_index);
15214
James Smart0c9ab6f2010-02-26 14:15:57 -050015215 return 0;
15216}
15217
15218/**
James Smart3804dc82010-07-14 15:31:37 -040015219 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015220 * @phba: pointer to lpfc hba data structure.
15221 *
15222 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015223 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015224 * does not go beyond the range of the driver allocated bmask dimension
15225 * before clearing the bit.
15226 **/
15227void
15228lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15229{
James Smart7d791df2011-07-22 18:37:52 -040015230 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015231 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15232 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015233 "2762 FCF (x%x) reached driver's book "
15234 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015235 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15236 return;
15237 }
15238 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015239 spin_lock_irq(&phba->hbalock);
15240 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15241 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15242 list_del_init(&fcf_pri->list);
15243 break;
15244 }
15245 }
15246 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015247 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015248
15249 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015250 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015251 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015252}
15253
15254/**
James Smartecfd03c2010-02-12 14:41:27 -050015255 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15256 * @phba: pointer to lpfc hba data structure.
15257 *
15258 * This routine is the completion routine for the rediscover FCF table mailbox
15259 * command. If the mailbox command returned failure, it will try to stop the
15260 * FCF rediscover wait timer.
15261 **/
15262void
15263lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15264{
15265 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15266 uint32_t shdr_status, shdr_add_status;
15267
15268 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15269
15270 shdr_status = bf_get(lpfc_mbox_hdr_status,
15271 &redisc_fcf->header.cfg_shdr.response);
15272 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15273 &redisc_fcf->header.cfg_shdr.response);
15274 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015275 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015276 "2746 Requesting for FCF rediscovery failed "
15277 "status x%x add_status x%x\n",
15278 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015279 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015280 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015281 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015282 spin_unlock_irq(&phba->hbalock);
15283 /*
15284 * CVL event triggered FCF rediscover request failed,
15285 * last resort to re-try current registered FCF entry.
15286 */
15287 lpfc_retry_pport_discovery(phba);
15288 } else {
15289 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015290 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015291 spin_unlock_irq(&phba->hbalock);
15292 /*
15293 * DEAD FCF event triggered FCF rediscover request
15294 * failed, last resort to fail over as a link down
15295 * to FCF registration.
15296 */
15297 lpfc_sli4_fcf_dead_failthrough(phba);
15298 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015299 } else {
15300 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015301 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015302 /*
15303 * Start FCF rediscovery wait timer for pending FCF
15304 * before rescan FCF record table.
15305 */
15306 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015307 }
James Smartecfd03c2010-02-12 14:41:27 -050015308
15309 mempool_free(mbox, phba->mbox_mem_pool);
15310}
15311
15312/**
James Smart3804dc82010-07-14 15:31:37 -040015313 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015314 * @phba: pointer to lpfc hba data structure.
15315 *
15316 * This routine is invoked to request for rediscovery of the entire FCF table
15317 * by the port.
15318 **/
15319int
15320lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15321{
15322 LPFC_MBOXQ_t *mbox;
15323 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15324 int rc, length;
15325
James Smart0c9ab6f2010-02-26 14:15:57 -050015326 /* Cancel retry delay timers to all vports before FCF rediscover */
15327 lpfc_cancel_all_vport_retry_delay_timer(phba);
15328
James Smartecfd03c2010-02-12 14:41:27 -050015329 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15330 if (!mbox) {
15331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15332 "2745 Failed to allocate mbox for "
15333 "requesting FCF rediscover.\n");
15334 return -ENOMEM;
15335 }
15336
15337 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15338 sizeof(struct lpfc_sli4_cfg_mhdr));
15339 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15340 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15341 length, LPFC_SLI4_MBX_EMBED);
15342
15343 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15344 /* Set count to 0 for invalidating the entire FCF database */
15345 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15346
15347 /* Issue the mailbox command asynchronously */
15348 mbox->vport = phba->pport;
15349 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15350 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15351
15352 if (rc == MBX_NOT_FINISHED) {
15353 mempool_free(mbox, phba->mbox_mem_pool);
15354 return -EIO;
15355 }
15356 return 0;
15357}
15358
15359/**
James Smartfc2b9892010-02-26 14:15:29 -050015360 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15361 * @phba: pointer to lpfc hba data structure.
15362 *
15363 * This function is the failover routine as a last resort to the FCF DEAD
15364 * event when driver failed to perform fast FCF failover.
15365 **/
15366void
15367lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15368{
15369 uint32_t link_state;
15370
15371 /*
15372 * Last resort as FCF DEAD event failover will treat this as
15373 * a link down, but save the link state because we don't want
15374 * it to be changed to Link Down unless it is already down.
15375 */
15376 link_state = phba->link_state;
15377 lpfc_linkdown(phba);
15378 phba->link_state = link_state;
15379
15380 /* Unregister FCF if no devices connected to it */
15381 lpfc_unregister_unused_fcf(phba);
15382}
15383
15384/**
James Smart026abb82011-12-13 13:20:45 -050015385 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015386 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015387 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015388 *
James Smart026abb82011-12-13 13:20:45 -050015389 * This function gets SLI3 port configure region 23 data through memory dump
15390 * mailbox command. When it successfully retrieves data, the size of the data
15391 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015392 **/
James Smart026abb82011-12-13 13:20:45 -050015393static uint32_t
15394lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015395{
15396 LPFC_MBOXQ_t *pmb = NULL;
15397 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015398 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015399 int rc;
15400
James Smart026abb82011-12-13 13:20:45 -050015401 if (!rgn23_data)
15402 return 0;
15403
James Smarta0c87cb2009-07-19 10:01:10 -040015404 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15405 if (!pmb) {
15406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015407 "2600 failed to allocate mailbox memory\n");
15408 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015409 }
15410 mb = &pmb->u.mb;
15411
James Smarta0c87cb2009-07-19 10:01:10 -040015412 do {
15413 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15414 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15415
15416 if (rc != MBX_SUCCESS) {
15417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015418 "2601 failed to read config "
15419 "region 23, rc 0x%x Status 0x%x\n",
15420 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015421 mb->un.varDmp.word_cnt = 0;
15422 }
15423 /*
15424 * dump mem may return a zero when finished or we got a
15425 * mailbox error, either way we are done.
15426 */
15427 if (mb->un.varDmp.word_cnt == 0)
15428 break;
15429 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15430 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15431
15432 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015433 rgn23_data + offset,
15434 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015435 offset += mb->un.varDmp.word_cnt;
15436 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15437
James Smart026abb82011-12-13 13:20:45 -050015438 mempool_free(pmb, phba->mbox_mem_pool);
15439 return offset;
15440}
15441
15442/**
15443 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15444 * @phba: pointer to lpfc hba data structure.
15445 * @rgn23_data: pointer to configure region 23 data.
15446 *
15447 * This function gets SLI4 port configure region 23 data through memory dump
15448 * mailbox command. When it successfully retrieves data, the size of the data
15449 * will be returned, otherwise, 0 will be returned.
15450 **/
15451static uint32_t
15452lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15453{
15454 LPFC_MBOXQ_t *mboxq = NULL;
15455 struct lpfc_dmabuf *mp = NULL;
15456 struct lpfc_mqe *mqe;
15457 uint32_t data_length = 0;
15458 int rc;
15459
15460 if (!rgn23_data)
15461 return 0;
15462
15463 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15464 if (!mboxq) {
15465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15466 "3105 failed to allocate mailbox memory\n");
15467 return 0;
15468 }
15469
15470 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15471 goto out;
15472 mqe = &mboxq->u.mqe;
15473 mp = (struct lpfc_dmabuf *) mboxq->context1;
15474 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15475 if (rc)
15476 goto out;
15477 data_length = mqe->un.mb_words[5];
15478 if (data_length == 0)
15479 goto out;
15480 if (data_length > DMP_RGN23_SIZE) {
15481 data_length = 0;
15482 goto out;
15483 }
15484 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15485out:
15486 mempool_free(mboxq, phba->mbox_mem_pool);
15487 if (mp) {
15488 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15489 kfree(mp);
15490 }
15491 return data_length;
15492}
15493
15494/**
15495 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15496 * @phba: pointer to lpfc hba data structure.
15497 *
15498 * This function read region 23 and parse TLV for port status to
15499 * decide if the user disaled the port. If the TLV indicates the
15500 * port is disabled, the hba_flag is set accordingly.
15501 **/
15502void
15503lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15504{
15505 uint8_t *rgn23_data = NULL;
15506 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15507 uint32_t offset = 0;
15508
15509 /* Get adapter Region 23 data */
15510 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15511 if (!rgn23_data)
15512 goto out;
15513
15514 if (phba->sli_rev < LPFC_SLI_REV4)
15515 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15516 else {
15517 if_type = bf_get(lpfc_sli_intf_if_type,
15518 &phba->sli4_hba.sli_intf);
15519 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15520 goto out;
15521 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15522 }
James Smarta0c87cb2009-07-19 10:01:10 -040015523
15524 if (!data_size)
15525 goto out;
15526
15527 /* Check the region signature first */
15528 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15530 "2619 Config region 23 has bad signature\n");
15531 goto out;
15532 }
15533 offset += 4;
15534
15535 /* Check the data structure version */
15536 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15538 "2620 Config region 23 has bad version\n");
15539 goto out;
15540 }
15541 offset += 4;
15542
15543 /* Parse TLV entries in the region */
15544 while (offset < data_size) {
15545 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15546 break;
15547 /*
15548 * If the TLV is not driver specific TLV or driver id is
15549 * not linux driver id, skip the record.
15550 */
15551 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15552 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15553 (rgn23_data[offset + 3] != 0)) {
15554 offset += rgn23_data[offset + 1] * 4 + 4;
15555 continue;
15556 }
15557
15558 /* Driver found a driver specific TLV in the config region */
15559 sub_tlv_len = rgn23_data[offset + 1] * 4;
15560 offset += 4;
15561 tlv_offset = 0;
15562
15563 /*
15564 * Search for configured port state sub-TLV.
15565 */
15566 while ((offset < data_size) &&
15567 (tlv_offset < sub_tlv_len)) {
15568 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15569 offset += 4;
15570 tlv_offset += 4;
15571 break;
15572 }
15573 if (rgn23_data[offset] != PORT_STE_TYPE) {
15574 offset += rgn23_data[offset + 1] * 4 + 4;
15575 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15576 continue;
15577 }
15578
15579 /* This HBA contains PORT_STE configured */
15580 if (!rgn23_data[offset + 2])
15581 phba->hba_flag |= LINK_DISABLED;
15582
15583 goto out;
15584 }
15585 }
James Smart026abb82011-12-13 13:20:45 -050015586
James Smarta0c87cb2009-07-19 10:01:10 -040015587out:
James Smarta0c87cb2009-07-19 10:01:10 -040015588 kfree(rgn23_data);
15589 return;
15590}
James Smart695a8142010-01-26 23:08:03 -050015591
15592/**
James Smart52d52442011-05-24 11:42:45 -040015593 * lpfc_wr_object - write an object to the firmware
15594 * @phba: HBA structure that indicates port to create a queue on.
15595 * @dmabuf_list: list of dmabufs to write to the port.
15596 * @size: the total byte value of the objects to write to the port.
15597 * @offset: the current offset to be used to start the transfer.
15598 *
15599 * This routine will create a wr_object mailbox command to send to the port.
15600 * the mailbox command will be constructed using the dma buffers described in
15601 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15602 * BDEs that the imbedded mailbox can support. The @offset variable will be
15603 * used to indicate the starting offset of the transfer and will also return
15604 * the offset after the write object mailbox has completed. @size is used to
15605 * determine the end of the object and whether the eof bit should be set.
15606 *
15607 * Return 0 is successful and offset will contain the the new offset to use
15608 * for the next write.
15609 * Return negative value for error cases.
15610 **/
15611int
15612lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15613 uint32_t size, uint32_t *offset)
15614{
15615 struct lpfc_mbx_wr_object *wr_object;
15616 LPFC_MBOXQ_t *mbox;
15617 int rc = 0, i = 0;
15618 uint32_t shdr_status, shdr_add_status;
15619 uint32_t mbox_tmo;
15620 union lpfc_sli4_cfg_shdr *shdr;
15621 struct lpfc_dmabuf *dmabuf;
15622 uint32_t written = 0;
15623
15624 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15625 if (!mbox)
15626 return -ENOMEM;
15627
15628 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15629 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15630 sizeof(struct lpfc_mbx_wr_object) -
15631 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15632
15633 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15634 wr_object->u.request.write_offset = *offset;
15635 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15636 wr_object->u.request.object_name[0] =
15637 cpu_to_le32(wr_object->u.request.object_name[0]);
15638 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15639 list_for_each_entry(dmabuf, dmabuf_list, list) {
15640 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15641 break;
15642 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15643 wr_object->u.request.bde[i].addrHigh =
15644 putPaddrHigh(dmabuf->phys);
15645 if (written + SLI4_PAGE_SIZE >= size) {
15646 wr_object->u.request.bde[i].tus.f.bdeSize =
15647 (size - written);
15648 written += (size - written);
15649 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15650 } else {
15651 wr_object->u.request.bde[i].tus.f.bdeSize =
15652 SLI4_PAGE_SIZE;
15653 written += SLI4_PAGE_SIZE;
15654 }
15655 i++;
15656 }
15657 wr_object->u.request.bde_count = i;
15658 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15659 if (!phba->sli4_hba.intr_enable)
15660 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15661 else {
James Smarta183a152011-10-10 21:32:43 -040015662 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015663 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15664 }
15665 /* The IOCTL status is embedded in the mailbox subheader. */
15666 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15667 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15668 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15669 if (rc != MBX_TIMEOUT)
15670 mempool_free(mbox, phba->mbox_mem_pool);
15671 if (shdr_status || shdr_add_status || rc) {
15672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15673 "3025 Write Object mailbox failed with "
15674 "status x%x add_status x%x, mbx status x%x\n",
15675 shdr_status, shdr_add_status, rc);
15676 rc = -ENXIO;
15677 } else
15678 *offset += wr_object->u.response.actual_write_length;
15679 return rc;
15680}
15681
15682/**
James Smart695a8142010-01-26 23:08:03 -050015683 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15684 * @vport: pointer to vport data structure.
15685 *
15686 * This function iterate through the mailboxq and clean up all REG_LOGIN
15687 * and REG_VPI mailbox commands associated with the vport. This function
15688 * is called when driver want to restart discovery of the vport due to
15689 * a Clear Virtual Link event.
15690 **/
15691void
15692lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15693{
15694 struct lpfc_hba *phba = vport->phba;
15695 LPFC_MBOXQ_t *mb, *nextmb;
15696 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015697 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015698 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015699 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015700 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015701 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015702
James Smartd439d282010-09-29 11:18:45 -040015703 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015704 spin_lock_irq(&phba->hbalock);
15705 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15706 if (mb->vport != vport)
15707 continue;
15708
15709 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15710 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15711 continue;
15712
James Smartd439d282010-09-29 11:18:45 -040015713 list_del(&mb->list);
15714 list_add_tail(&mb->list, &mbox_cmd_list);
15715 }
15716 /* Clean up active mailbox command with the vport */
15717 mb = phba->sli.mbox_active;
15718 if (mb && (mb->vport == vport)) {
15719 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15720 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15721 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15722 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15723 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15724 /* Put reference count for delayed processing */
15725 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15726 /* Unregister the RPI when mailbox complete */
15727 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15728 }
15729 }
James Smart63e801c2010-11-20 23:14:19 -050015730 /* Cleanup any mailbox completions which are not yet processed */
15731 do {
15732 restart_loop = 0;
15733 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15734 /*
15735 * If this mailox is already processed or it is
15736 * for another vport ignore it.
15737 */
15738 if ((mb->vport != vport) ||
15739 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15740 continue;
15741
15742 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15743 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15744 continue;
15745
15746 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15747 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15748 ndlp = (struct lpfc_nodelist *)mb->context2;
15749 /* Unregister the RPI when mailbox complete */
15750 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15751 restart_loop = 1;
15752 spin_unlock_irq(&phba->hbalock);
15753 spin_lock(shost->host_lock);
15754 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15755 spin_unlock(shost->host_lock);
15756 spin_lock_irq(&phba->hbalock);
15757 break;
15758 }
15759 }
15760 } while (restart_loop);
15761
James Smartd439d282010-09-29 11:18:45 -040015762 spin_unlock_irq(&phba->hbalock);
15763
15764 /* Release the cleaned-up mailbox commands */
15765 while (!list_empty(&mbox_cmd_list)) {
15766 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015767 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15768 mp = (struct lpfc_dmabuf *) (mb->context1);
15769 if (mp) {
15770 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15771 kfree(mp);
15772 }
James Smart78730cf2010-04-06 15:06:30 -040015773 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015774 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015775 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015776 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015777 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015778 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015779 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015780 }
James Smart695a8142010-01-26 23:08:03 -050015781 }
James Smart695a8142010-01-26 23:08:03 -050015782 mempool_free(mb, phba->mbox_mem_pool);
15783 }
James Smartd439d282010-09-29 11:18:45 -040015784
15785 /* Release the ndlp with the cleaned-up active mailbox command */
15786 if (act_mbx_ndlp) {
15787 spin_lock(shost->host_lock);
15788 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15789 spin_unlock(shost->host_lock);
15790 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015791 }
James Smart695a8142010-01-26 23:08:03 -050015792}
15793
James Smart2a9bf3d2010-06-07 15:24:45 -040015794/**
15795 * lpfc_drain_txq - Drain the txq
15796 * @phba: Pointer to HBA context object.
15797 *
15798 * This function attempt to submit IOCBs on the txq
15799 * to the adapter. For SLI4 adapters, the txq contains
15800 * ELS IOCBs that have been deferred because the there
15801 * are no SGLs. This congestion can occur with large
15802 * vport counts during node discovery.
15803 **/
15804
15805uint32_t
15806lpfc_drain_txq(struct lpfc_hba *phba)
15807{
15808 LIST_HEAD(completions);
15809 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15810 struct lpfc_iocbq *piocbq = 0;
15811 unsigned long iflags = 0;
15812 char *fail_msg = NULL;
15813 struct lpfc_sglq *sglq;
15814 union lpfc_wqe wqe;
15815
15816 spin_lock_irqsave(&phba->hbalock, iflags);
15817 if (pring->txq_cnt > pring->txq_max)
15818 pring->txq_max = pring->txq_cnt;
15819
15820 spin_unlock_irqrestore(&phba->hbalock, iflags);
15821
15822 while (pring->txq_cnt) {
15823 spin_lock_irqsave(&phba->hbalock, iflags);
15824
James Smart19ca7602010-11-20 23:11:55 -050015825 piocbq = lpfc_sli_ringtx_get(phba, pring);
15826 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015827 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015828 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015829 spin_unlock_irqrestore(&phba->hbalock, iflags);
15830 break;
15831 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015832 if (!piocbq) {
15833 /* The txq_cnt out of sync. This should
15834 * never happen
15835 */
15836 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015837 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015838 spin_unlock_irqrestore(&phba->hbalock, iflags);
15839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15840 "2823 txq empty and txq_cnt is %d\n ",
15841 pring->txq_cnt);
15842 break;
15843 }
15844 }
15845
15846 /* The xri and iocb resources secured,
15847 * attempt to issue request
15848 */
James Smart6d368e52011-05-24 11:44:12 -040015849 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015850 piocbq->sli4_xritag = sglq->sli4_xritag;
15851 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15852 fail_msg = "to convert bpl to sgl";
15853 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15854 fail_msg = "to convert iocb to wqe";
15855 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15856 fail_msg = " - Wq is full";
15857 else
15858 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15859
15860 if (fail_msg) {
15861 /* Failed means we can't issue and need to cancel */
15862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15863 "2822 IOCB failed %s iotag 0x%x "
15864 "xri 0x%x\n",
15865 fail_msg,
15866 piocbq->iotag, piocbq->sli4_xritag);
15867 list_add_tail(&piocbq->list, &completions);
15868 }
15869 spin_unlock_irqrestore(&phba->hbalock, iflags);
15870 }
15871
James Smart2a9bf3d2010-06-07 15:24:45 -040015872 /* Cancel all the IOCBs that cannot be issued */
15873 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15874 IOERR_SLI_ABORTED);
15875
15876 return pring->txq_cnt;
15877}