blob: 4d4104f38c9897d6be6414abf3db11c318c68e79 [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 */
100 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
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 Smart04c68492009-05-22 14:52:52 -04004342 }
dea31012005-04-17 16:05:31 -05004343 }
James Smarted957682007-06-17 19:56:37 -05004344 if (!done) {
4345 rc = -EINVAL;
4346 goto do_prep_failed;
4347 }
James Smart04c68492009-05-22 14:52:52 -04004348 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4349 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004350 rc = -ENXIO;
4351 goto do_prep_failed;
4352 }
James Smart04c68492009-05-22 14:52:52 -04004353 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004354 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004355 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4356 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4357 phba->max_vpi : phba->max_vports;
4358
James Smart34b02dc2008-08-24 21:49:55 -04004359 } else
4360 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004361 phba->fips_level = 0;
4362 phba->fips_spec_rev = 0;
4363 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004364 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004365 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4366 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4368 "2850 Security Crypto Active. FIPS x%d "
4369 "(Spec Rev: x%d)",
4370 phba->fips_level, phba->fips_spec_rev);
4371 }
4372 if (pmb->u.mb.un.varCfgPort.sec_err) {
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "2856 Config Port Security Crypto "
4375 "Error: x%x ",
4376 pmb->u.mb.un.varCfgPort.sec_err);
4377 }
James Smart04c68492009-05-22 14:52:52 -04004378 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004379 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004380 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004381 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004382
4383 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4384 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004385
4386 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004387 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004388 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4389 else
4390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4391 "0443 Adapter did not grant "
4392 "BlockGuard\n");
4393 }
James Smart34b02dc2008-08-24 21:49:55 -04004394 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004395 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004396 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004397 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004398 }
James Smart92d7f7b2007-06-17 19:56:38 -05004399do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004400 mempool_free(pmb, phba->mbox_mem_pool);
4401 return rc;
4402}
4403
James Smarte59058c2008-08-24 21:49:00 -04004404
4405/**
James Smart3621a712009-04-06 18:47:14 -04004406 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function is the main SLI intialization function. This function
4410 * is called by the HBA intialization code, HBA reset code and HBA
4411 * error attention handler code. Caller is not required to hold any
4412 * locks. This function issues config_port mailbox command to configure
4413 * the SLI, setup iocb rings and HBQ rings. In the end the function
4414 * calls the config_port_post function to issue init_link mailbox
4415 * command and to start the discovery. The function will return zero
4416 * if successful, else it will return negative error code.
4417 **/
James Smarted957682007-06-17 19:56:37 -05004418int
4419lpfc_sli_hba_setup(struct lpfc_hba *phba)
4420{
4421 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004422 int mode = 3, i;
4423 int longs;
James Smarted957682007-06-17 19:56:37 -05004424
4425 switch (lpfc_sli_mode) {
4426 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004427 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004429 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004430 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004431 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004432 break;
4433 }
James Smarted957682007-06-17 19:56:37 -05004434 mode = 2;
4435 break;
4436 case 0:
4437 case 3:
4438 break;
4439 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004441 "1819 Unrecognized lpfc_sli_mode "
4442 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004443
4444 break;
4445 }
4446
James Smart93996272008-08-24 21:50:30 -04004447 rc = lpfc_sli_config_port(phba, mode);
4448
James Smarted957682007-06-17 19:56:37 -05004449 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004451 "1820 Unable to select SLI-3. "
4452 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004453 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004454 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004455 if (rc)
dea31012005-04-17 16:05:31 -05004456 goto lpfc_sli_hba_setup_error;
4457
James Smart0d878412009-10-02 15:16:56 -04004458 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4459 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4460 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4461 if (!rc) {
4462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4463 "2709 This device supports "
4464 "Advanced Error Reporting (AER)\n");
4465 spin_lock_irq(&phba->hbalock);
4466 phba->hba_flag |= HBA_AER_ENABLED;
4467 spin_unlock_irq(&phba->hbalock);
4468 } else {
4469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4470 "2708 This device does not support "
4471 "Advanced Error Reporting (AER)\n");
4472 phba->cfg_aer_support = 0;
4473 }
4474 }
4475
James Smarted957682007-06-17 19:56:37 -05004476 if (phba->sli_rev == 3) {
4477 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4478 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004479 } else {
4480 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4481 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004482 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004483 }
4484
4485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004486 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4487 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004488 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004489
4490 if (rc)
4491 goto lpfc_sli_hba_setup_error;
4492
James Smart6d368e52011-05-24 11:44:12 -04004493 /* Initialize VPIs. */
4494 if (phba->sli_rev == LPFC_SLI_REV3) {
4495 /*
4496 * The VPI bitmask and physical ID array are allocated
4497 * and initialized once only - at driver load. A port
4498 * reset doesn't need to reinitialize this memory.
4499 */
4500 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4501 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4502 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4503 GFP_KERNEL);
4504 if (!phba->vpi_bmask) {
4505 rc = -ENOMEM;
4506 goto lpfc_sli_hba_setup_error;
4507 }
4508
4509 phba->vpi_ids = kzalloc(
4510 (phba->max_vpi+1) * sizeof(uint16_t),
4511 GFP_KERNEL);
4512 if (!phba->vpi_ids) {
4513 kfree(phba->vpi_bmask);
4514 rc = -ENOMEM;
4515 goto lpfc_sli_hba_setup_error;
4516 }
4517 for (i = 0; i < phba->max_vpi; i++)
4518 phba->vpi_ids[i] = i;
4519 }
4520 }
4521
James Smart93996272008-08-24 21:50:30 -04004522 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004523 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4524 rc = lpfc_sli_hbq_setup(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527 }
James Smart04c68492009-05-22 14:52:52 -04004528 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004529 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004530 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004531
4532 rc = lpfc_config_port_post(phba);
4533 if (rc)
4534 goto lpfc_sli_hba_setup_error;
4535
James Smarted957682007-06-17 19:56:37 -05004536 return rc;
4537
James Smart92d7f7b2007-06-17 19:56:38 -05004538lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004539 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004541 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004542 return rc;
4543}
4544
James Smartda0436e2009-05-22 14:51:39 -04004545/**
4546 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4547 * @phba: Pointer to HBA context object.
4548 * @mboxq: mailbox pointer.
4549 * This function issue a dump mailbox command to read config region
4550 * 23 and parse the records in the region and populate driver
4551 * data structure.
4552 **/
4553static int
4554lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4555 LPFC_MBOXQ_t *mboxq)
4556{
4557 struct lpfc_dmabuf *mp;
4558 struct lpfc_mqe *mqe;
4559 uint32_t data_length;
4560 int rc;
4561
4562 /* Program the default value of vlan_id and fc_map */
4563 phba->valid_vlan = 0;
4564 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4565 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4566 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4567
4568 mqe = &mboxq->u.mqe;
4569 if (lpfc_dump_fcoe_param(phba, mboxq))
4570 return -ENOMEM;
4571
4572 mp = (struct lpfc_dmabuf *) mboxq->context1;
4573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4574
4575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4576 "(%d):2571 Mailbox cmd x%x Status x%x "
4577 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4578 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4579 "CQ: x%x x%x x%x x%x\n",
4580 mboxq->vport ? mboxq->vport->vpi : 0,
4581 bf_get(lpfc_mqe_command, mqe),
4582 bf_get(lpfc_mqe_status, mqe),
4583 mqe->un.mb_words[0], mqe->un.mb_words[1],
4584 mqe->un.mb_words[2], mqe->un.mb_words[3],
4585 mqe->un.mb_words[4], mqe->un.mb_words[5],
4586 mqe->un.mb_words[6], mqe->un.mb_words[7],
4587 mqe->un.mb_words[8], mqe->un.mb_words[9],
4588 mqe->un.mb_words[10], mqe->un.mb_words[11],
4589 mqe->un.mb_words[12], mqe->un.mb_words[13],
4590 mqe->un.mb_words[14], mqe->un.mb_words[15],
4591 mqe->un.mb_words[16], mqe->un.mb_words[50],
4592 mboxq->mcqe.word0,
4593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4594 mboxq->mcqe.trailer);
4595
4596 if (rc) {
4597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
4599 return -EIO;
4600 }
4601 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004602 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004605 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004606 }
James Smartda0436e2009-05-22 14:51:39 -04004607
4608 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4609 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4610 kfree(mp);
4611 return 0;
4612}
4613
4614/**
4615 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4616 * @phba: pointer to lpfc hba data structure.
4617 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4618 * @vpd: pointer to the memory to hold resulting port vpd data.
4619 * @vpd_size: On input, the number of bytes allocated to @vpd.
4620 * On output, the number of data bytes in @vpd.
4621 *
4622 * This routine executes a READ_REV SLI4 mailbox command. In
4623 * addition, this routine gets the port vpd data.
4624 *
4625 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004626 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004627 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004628 **/
4629static int
4630lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4631 uint8_t *vpd, uint32_t *vpd_size)
4632{
4633 int rc = 0;
4634 uint32_t dma_size;
4635 struct lpfc_dmabuf *dmabuf;
4636 struct lpfc_mqe *mqe;
4637
4638 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4639 if (!dmabuf)
4640 return -ENOMEM;
4641
4642 /*
4643 * Get a DMA buffer for the vpd data resulting from the READ_REV
4644 * mailbox command.
4645 */
4646 dma_size = *vpd_size;
4647 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4648 dma_size,
4649 &dmabuf->phys,
4650 GFP_KERNEL);
4651 if (!dmabuf->virt) {
4652 kfree(dmabuf);
4653 return -ENOMEM;
4654 }
4655 memset(dmabuf->virt, 0, dma_size);
4656
4657 /*
4658 * The SLI4 implementation of READ_REV conflicts at word1,
4659 * bits 31:16 and SLI4 adds vpd functionality not present
4660 * in SLI3. This code corrects the conflicts.
4661 */
4662 lpfc_read_rev(phba, mboxq);
4663 mqe = &mboxq->u.mqe;
4664 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4665 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4666 mqe->un.read_rev.word1 &= 0x0000FFFF;
4667 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4668 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4669
4670 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4671 if (rc) {
4672 dma_free_coherent(&phba->pcidev->dev, dma_size,
4673 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004674 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004675 return -EIO;
4676 }
4677
James Smartda0436e2009-05-22 14:51:39 -04004678 /*
4679 * The available vpd length cannot be bigger than the
4680 * DMA buffer passed to the port. Catch the less than
4681 * case and update the caller's size.
4682 */
4683 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4684 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4685
James Smartd7c47992010-06-08 18:31:54 -04004686 memcpy(vpd, dmabuf->virt, *vpd_size);
4687
James Smartda0436e2009-05-22 14:51:39 -04004688 dma_free_coherent(&phba->pcidev->dev, dma_size,
4689 dmabuf->virt, dmabuf->phys);
4690 kfree(dmabuf);
4691 return 0;
4692}
4693
4694/**
James Smartcd1c8302011-10-10 21:33:25 -04004695 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4696 * @phba: pointer to lpfc hba data structure.
4697 *
4698 * This routine retrieves SLI4 device physical port name this PCI function
4699 * is attached to.
4700 *
4701 * Return codes
4702 * 0 - sucessful
4703 * otherwise - failed to retrieve physical port name
4704 **/
4705static int
4706lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4707{
4708 LPFC_MBOXQ_t *mboxq;
4709 struct lpfc_mbx_read_config *rd_config;
4710 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4711 struct lpfc_controller_attribute *cntl_attr;
4712 struct lpfc_mbx_get_port_name *get_port_name;
4713 void *virtaddr = NULL;
4714 uint32_t alloclen, reqlen;
4715 uint32_t shdr_status, shdr_add_status;
4716 union lpfc_sli4_cfg_shdr *shdr;
4717 char cport_name = 0;
4718 int rc;
4719
4720 /* We assume nothing at this point */
4721 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4722 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4723
4724 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4725 if (!mboxq)
4726 return -ENOMEM;
4727
4728 /* obtain link type and link number via READ_CONFIG */
4729 lpfc_read_config(phba, mboxq);
4730 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4731 if (rc == MBX_SUCCESS) {
4732 rd_config = &mboxq->u.mqe.un.rd_config;
4733 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
4734 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4735 phba->sli4_hba.lnk_info.lnk_tp =
4736 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
4737 phba->sli4_hba.lnk_info.lnk_no =
4738 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
4739 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4740 "3081 lnk_type:%d, lnk_numb:%d\n",
4741 phba->sli4_hba.lnk_info.lnk_tp,
4742 phba->sli4_hba.lnk_info.lnk_no);
4743 goto retrieve_ppname;
4744 } else
4745 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4746 "3082 Mailbox (x%x) returned ldv:x0\n",
4747 bf_get(lpfc_mqe_command,
4748 &mboxq->u.mqe));
4749 } else
4750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4751 "3083 Mailbox (x%x) failed, status:x%x\n",
4752 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4753 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4754
4755 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4756 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4757 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4758 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4759 LPFC_SLI4_MBX_NEMBED);
4760 if (alloclen < reqlen) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4762 "3084 Allocated DMA memory size (%d) is "
4763 "less than the requested DMA memory size "
4764 "(%d)\n", alloclen, reqlen);
4765 rc = -ENOMEM;
4766 goto out_free_mboxq;
4767 }
4768 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4769 virtaddr = mboxq->sge_array->addr[0];
4770 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4771 shdr = &mbx_cntl_attr->cfg_shdr;
4772 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4773 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4774 if (shdr_status || shdr_add_status || rc) {
4775 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4776 "3085 Mailbox x%x (x%x/x%x) failed, "
4777 "rc:x%x, status:x%x, add_status:x%x\n",
4778 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4779 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4780 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4781 rc, shdr_status, shdr_add_status);
4782 rc = -ENXIO;
4783 goto out_free_mboxq;
4784 }
4785 cntl_attr = &mbx_cntl_attr->cntl_attr;
4786 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4787 phba->sli4_hba.lnk_info.lnk_tp =
4788 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4789 phba->sli4_hba.lnk_info.lnk_no =
4790 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4791 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4792 "3086 lnk_type:%d, lnk_numb:%d\n",
4793 phba->sli4_hba.lnk_info.lnk_tp,
4794 phba->sli4_hba.lnk_info.lnk_no);
4795
4796retrieve_ppname:
4797 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4798 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4799 sizeof(struct lpfc_mbx_get_port_name) -
4800 sizeof(struct lpfc_sli4_cfg_mhdr),
4801 LPFC_SLI4_MBX_EMBED);
4802 get_port_name = &mboxq->u.mqe.un.get_port_name;
4803 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4804 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4805 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4806 phba->sli4_hba.lnk_info.lnk_tp);
4807 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4808 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4810 if (shdr_status || shdr_add_status || rc) {
4811 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4812 "3087 Mailbox x%x (x%x/x%x) failed: "
4813 "rc:x%x, status:x%x, add_status:x%x\n",
4814 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4815 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4816 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4817 rc, shdr_status, shdr_add_status);
4818 rc = -ENXIO;
4819 goto out_free_mboxq;
4820 }
4821 switch (phba->sli4_hba.lnk_info.lnk_no) {
4822 case LPFC_LINK_NUMBER_0:
4823 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4824 &get_port_name->u.response);
4825 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4826 break;
4827 case LPFC_LINK_NUMBER_1:
4828 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4829 &get_port_name->u.response);
4830 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4831 break;
4832 case LPFC_LINK_NUMBER_2:
4833 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4834 &get_port_name->u.response);
4835 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4836 break;
4837 case LPFC_LINK_NUMBER_3:
4838 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4839 &get_port_name->u.response);
4840 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4841 break;
4842 default:
4843 break;
4844 }
4845
4846 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4847 phba->Port[0] = cport_name;
4848 phba->Port[1] = '\0';
4849 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4850 "3091 SLI get port name: %s\n", phba->Port);
4851 }
4852
4853out_free_mboxq:
4854 if (rc != MBX_TIMEOUT) {
4855 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4856 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4857 else
4858 mempool_free(mboxq, phba->mbox_mem_pool);
4859 }
4860 return rc;
4861}
4862
4863/**
James Smartda0436e2009-05-22 14:51:39 -04004864 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4865 * @phba: pointer to lpfc hba data structure.
4866 *
4867 * This routine is called to explicitly arm the SLI4 device's completion and
4868 * event queues
4869 **/
4870static void
4871lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4872{
4873 uint8_t fcp_eqidx;
4874
4875 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4876 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004877 fcp_eqidx = 0;
4878 do
James Smartda0436e2009-05-22 14:51:39 -04004879 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4880 LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004881 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04004882 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4883 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4884 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4885 LPFC_QUEUE_REARM);
4886}
4887
4888/**
James Smart6d368e52011-05-24 11:44:12 -04004889 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4890 * @phba: Pointer to HBA context object.
4891 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004892 * @extnt_count: buffer to hold port available extent count.
4893 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004894 *
James Smartb76f2dc2011-07-22 18:37:42 -04004895 * This function calls the port and retrievs the number of available
4896 * extents and their size for a particular extent type.
4897 *
4898 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004899 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004900int
James Smart6d368e52011-05-24 11:44:12 -04004901lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4902 uint16_t *extnt_count, uint16_t *extnt_size)
4903{
4904 int rc = 0;
4905 uint32_t length;
4906 uint32_t mbox_tmo;
4907 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4908 LPFC_MBOXQ_t *mbox;
4909
4910 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4911 if (!mbox)
4912 return -ENOMEM;
4913
4914 /* Find out how many extents are available for this resource type */
4915 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4916 sizeof(struct lpfc_sli4_cfg_mhdr));
4917 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4918 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4919 length, LPFC_SLI4_MBX_EMBED);
4920
4921 /* Send an extents count of 0 - the GET doesn't use it. */
4922 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4923 LPFC_SLI4_MBX_EMBED);
4924 if (unlikely(rc)) {
4925 rc = -EIO;
4926 goto err_exit;
4927 }
4928
4929 if (!phba->sli4_hba.intr_enable)
4930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4931 else {
James Smarta183a152011-10-10 21:32:43 -04004932 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004933 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4934 }
4935 if (unlikely(rc)) {
4936 rc = -EIO;
4937 goto err_exit;
4938 }
4939
4940 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4941 if (bf_get(lpfc_mbox_hdr_status,
4942 &rsrc_info->header.cfg_shdr.response)) {
4943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4944 "2930 Failed to get resource extents "
4945 "Status 0x%x Add'l Status 0x%x\n",
4946 bf_get(lpfc_mbox_hdr_status,
4947 &rsrc_info->header.cfg_shdr.response),
4948 bf_get(lpfc_mbox_hdr_add_status,
4949 &rsrc_info->header.cfg_shdr.response));
4950 rc = -EIO;
4951 goto err_exit;
4952 }
4953
4954 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4955 &rsrc_info->u.rsp);
4956 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4957 &rsrc_info->u.rsp);
4958 err_exit:
4959 mempool_free(mbox, phba->mbox_mem_pool);
4960 return rc;
4961}
4962
4963/**
4964 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4965 * @phba: Pointer to HBA context object.
4966 * @type: The extent type to check.
4967 *
4968 * This function reads the current available extents from the port and checks
4969 * if the extent count or extent size has changed since the last access.
4970 * Callers use this routine post port reset to understand if there is a
4971 * extent reprovisioning requirement.
4972 *
4973 * Returns:
4974 * -Error: error indicates problem.
4975 * 1: Extent count or size has changed.
4976 * 0: No changes.
4977 **/
4978static int
4979lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4980{
4981 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4982 uint16_t size_diff, rsrc_ext_size;
4983 int rc = 0;
4984 struct lpfc_rsrc_blks *rsrc_entry;
4985 struct list_head *rsrc_blk_list = NULL;
4986
4987 size_diff = 0;
4988 curr_ext_cnt = 0;
4989 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4990 &rsrc_ext_cnt,
4991 &rsrc_ext_size);
4992 if (unlikely(rc))
4993 return -EIO;
4994
4995 switch (type) {
4996 case LPFC_RSC_TYPE_FCOE_RPI:
4997 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4998 break;
4999 case LPFC_RSC_TYPE_FCOE_VPI:
5000 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5001 break;
5002 case LPFC_RSC_TYPE_FCOE_XRI:
5003 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5004 break;
5005 case LPFC_RSC_TYPE_FCOE_VFI:
5006 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5007 break;
5008 default:
5009 break;
5010 }
5011
5012 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5013 curr_ext_cnt++;
5014 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5015 size_diff++;
5016 }
5017
5018 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5019 rc = 1;
5020
5021 return rc;
5022}
5023
5024/**
5025 * lpfc_sli4_cfg_post_extnts -
5026 * @phba: Pointer to HBA context object.
5027 * @extnt_cnt - number of available extents.
5028 * @type - the extent type (rpi, xri, vfi, vpi).
5029 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5030 * @mbox - pointer to the caller's allocated mailbox structure.
5031 *
5032 * This function executes the extents allocation request. It also
5033 * takes care of the amount of memory needed to allocate or get the
5034 * allocated extents. It is the caller's responsibility to evaluate
5035 * the response.
5036 *
5037 * Returns:
5038 * -Error: Error value describes the condition found.
5039 * 0: if successful
5040 **/
5041static int
5042lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5043 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5044{
5045 int rc = 0;
5046 uint32_t req_len;
5047 uint32_t emb_len;
5048 uint32_t alloc_len, mbox_tmo;
5049
5050 /* Calculate the total requested length of the dma memory */
5051 req_len = *extnt_cnt * sizeof(uint16_t);
5052
5053 /*
5054 * Calculate the size of an embedded mailbox. The uint32_t
5055 * accounts for extents-specific word.
5056 */
5057 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5058 sizeof(uint32_t);
5059
5060 /*
5061 * Presume the allocation and response will fit into an embedded
5062 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5063 */
5064 *emb = LPFC_SLI4_MBX_EMBED;
5065 if (req_len > emb_len) {
5066 req_len = *extnt_cnt * sizeof(uint16_t) +
5067 sizeof(union lpfc_sli4_cfg_shdr) +
5068 sizeof(uint32_t);
5069 *emb = LPFC_SLI4_MBX_NEMBED;
5070 }
5071
5072 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5073 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5074 req_len, *emb);
5075 if (alloc_len < req_len) {
5076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005077 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005078 "less than the requested DMA memory "
5079 "size (x%x)\n", alloc_len, req_len);
5080 return -ENOMEM;
5081 }
5082 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5083 if (unlikely(rc))
5084 return -EIO;
5085
5086 if (!phba->sli4_hba.intr_enable)
5087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5088 else {
James Smarta183a152011-10-10 21:32:43 -04005089 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005090 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5091 }
5092
5093 if (unlikely(rc))
5094 rc = -EIO;
5095 return rc;
5096}
5097
5098/**
5099 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5100 * @phba: Pointer to HBA context object.
5101 * @type: The resource extent type to allocate.
5102 *
5103 * This function allocates the number of elements for the specified
5104 * resource type.
5105 **/
5106static int
5107lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5108{
5109 bool emb = false;
5110 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5111 uint16_t rsrc_id, rsrc_start, j, k;
5112 uint16_t *ids;
5113 int i, rc;
5114 unsigned long longs;
5115 unsigned long *bmask;
5116 struct lpfc_rsrc_blks *rsrc_blks;
5117 LPFC_MBOXQ_t *mbox;
5118 uint32_t length;
5119 struct lpfc_id_range *id_array = NULL;
5120 void *virtaddr = NULL;
5121 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5122 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5123 struct list_head *ext_blk_list;
5124
5125 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5126 &rsrc_cnt,
5127 &rsrc_size);
5128 if (unlikely(rc))
5129 return -EIO;
5130
5131 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5133 "3009 No available Resource Extents "
5134 "for resource type 0x%x: Count: 0x%x, "
5135 "Size 0x%x\n", type, rsrc_cnt,
5136 rsrc_size);
5137 return -ENOMEM;
5138 }
5139
5140 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5141 "2903 Available Resource Extents "
5142 "for resource type 0x%x: Count: 0x%x, "
5143 "Size 0x%x\n", type, rsrc_cnt,
5144 rsrc_size);
5145
5146 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5147 if (!mbox)
5148 return -ENOMEM;
5149
5150 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5151 if (unlikely(rc)) {
5152 rc = -EIO;
5153 goto err_exit;
5154 }
5155
5156 /*
5157 * Figure out where the response is located. Then get local pointers
5158 * to the response data. The port does not guarantee to respond to
5159 * all extents counts request so update the local variable with the
5160 * allocated count from the port.
5161 */
5162 if (emb == LPFC_SLI4_MBX_EMBED) {
5163 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5164 id_array = &rsrc_ext->u.rsp.id[0];
5165 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5166 } else {
5167 virtaddr = mbox->sge_array->addr[0];
5168 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5169 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5170 id_array = &n_rsrc->id;
5171 }
5172
5173 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5174 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5175
5176 /*
5177 * Based on the resource size and count, correct the base and max
5178 * resource values.
5179 */
5180 length = sizeof(struct lpfc_rsrc_blks);
5181 switch (type) {
5182 case LPFC_RSC_TYPE_FCOE_RPI:
5183 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5184 sizeof(unsigned long),
5185 GFP_KERNEL);
5186 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5187 rc = -ENOMEM;
5188 goto err_exit;
5189 }
5190 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5191 sizeof(uint16_t),
5192 GFP_KERNEL);
5193 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5194 kfree(phba->sli4_hba.rpi_bmask);
5195 rc = -ENOMEM;
5196 goto err_exit;
5197 }
5198
5199 /*
5200 * The next_rpi was initialized with the maximum available
5201 * count but the port may allocate a smaller number. Catch
5202 * that case and update the next_rpi.
5203 */
5204 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5205
5206 /* Initialize local ptrs for common extent processing later. */
5207 bmask = phba->sli4_hba.rpi_bmask;
5208 ids = phba->sli4_hba.rpi_ids;
5209 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5210 break;
5211 case LPFC_RSC_TYPE_FCOE_VPI:
5212 phba->vpi_bmask = kzalloc(longs *
5213 sizeof(unsigned long),
5214 GFP_KERNEL);
5215 if (unlikely(!phba->vpi_bmask)) {
5216 rc = -ENOMEM;
5217 goto err_exit;
5218 }
5219 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5220 sizeof(uint16_t),
5221 GFP_KERNEL);
5222 if (unlikely(!phba->vpi_ids)) {
5223 kfree(phba->vpi_bmask);
5224 rc = -ENOMEM;
5225 goto err_exit;
5226 }
5227
5228 /* Initialize local ptrs for common extent processing later. */
5229 bmask = phba->vpi_bmask;
5230 ids = phba->vpi_ids;
5231 ext_blk_list = &phba->lpfc_vpi_blk_list;
5232 break;
5233 case LPFC_RSC_TYPE_FCOE_XRI:
5234 phba->sli4_hba.xri_bmask = kzalloc(longs *
5235 sizeof(unsigned long),
5236 GFP_KERNEL);
5237 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5238 rc = -ENOMEM;
5239 goto err_exit;
5240 }
5241 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5242 sizeof(uint16_t),
5243 GFP_KERNEL);
5244 if (unlikely(!phba->sli4_hba.xri_ids)) {
5245 kfree(phba->sli4_hba.xri_bmask);
5246 rc = -ENOMEM;
5247 goto err_exit;
5248 }
5249
5250 /* Initialize local ptrs for common extent processing later. */
5251 bmask = phba->sli4_hba.xri_bmask;
5252 ids = phba->sli4_hba.xri_ids;
5253 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_VFI:
5256 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5257 sizeof(unsigned long),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5260 rc = -ENOMEM;
5261 goto err_exit;
5262 }
5263 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5264 sizeof(uint16_t),
5265 GFP_KERNEL);
5266 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5267 kfree(phba->sli4_hba.vfi_bmask);
5268 rc = -ENOMEM;
5269 goto err_exit;
5270 }
5271
5272 /* Initialize local ptrs for common extent processing later. */
5273 bmask = phba->sli4_hba.vfi_bmask;
5274 ids = phba->sli4_hba.vfi_ids;
5275 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5276 break;
5277 default:
5278 /* Unsupported Opcode. Fail call. */
5279 id_array = NULL;
5280 bmask = NULL;
5281 ids = NULL;
5282 ext_blk_list = NULL;
5283 goto err_exit;
5284 }
5285
5286 /*
5287 * Complete initializing the extent configuration with the
5288 * allocated ids assigned to this function. The bitmask serves
5289 * as an index into the array and manages the available ids. The
5290 * array just stores the ids communicated to the port via the wqes.
5291 */
5292 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5293 if ((i % 2) == 0)
5294 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5295 &id_array[k]);
5296 else
5297 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5298 &id_array[k]);
5299
5300 rsrc_blks = kzalloc(length, GFP_KERNEL);
5301 if (unlikely(!rsrc_blks)) {
5302 rc = -ENOMEM;
5303 kfree(bmask);
5304 kfree(ids);
5305 goto err_exit;
5306 }
5307 rsrc_blks->rsrc_start = rsrc_id;
5308 rsrc_blks->rsrc_size = rsrc_size;
5309 list_add_tail(&rsrc_blks->list, ext_blk_list);
5310 rsrc_start = rsrc_id;
5311 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5312 phba->sli4_hba.scsi_xri_start = rsrc_start +
5313 lpfc_sli4_get_els_iocb_cnt(phba);
5314
5315 while (rsrc_id < (rsrc_start + rsrc_size)) {
5316 ids[j] = rsrc_id;
5317 rsrc_id++;
5318 j++;
5319 }
5320 /* Entire word processed. Get next word.*/
5321 if ((i % 2) == 1)
5322 k++;
5323 }
5324 err_exit:
5325 lpfc_sli4_mbox_cmd_free(phba, mbox);
5326 return rc;
5327}
5328
5329/**
5330 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5331 * @phba: Pointer to HBA context object.
5332 * @type: the extent's type.
5333 *
5334 * This function deallocates all extents of a particular resource type.
5335 * SLI4 does not allow for deallocating a particular extent range. It
5336 * is the caller's responsibility to release all kernel memory resources.
5337 **/
5338static int
5339lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5340{
5341 int rc;
5342 uint32_t length, mbox_tmo = 0;
5343 LPFC_MBOXQ_t *mbox;
5344 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5345 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5346
5347 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5348 if (!mbox)
5349 return -ENOMEM;
5350
5351 /*
5352 * This function sends an embedded mailbox because it only sends the
5353 * the resource type. All extents of this type are released by the
5354 * port.
5355 */
5356 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5357 sizeof(struct lpfc_sli4_cfg_mhdr));
5358 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5359 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5360 length, LPFC_SLI4_MBX_EMBED);
5361
5362 /* Send an extents count of 0 - the dealloc doesn't use it. */
5363 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5364 LPFC_SLI4_MBX_EMBED);
5365 if (unlikely(rc)) {
5366 rc = -EIO;
5367 goto out_free_mbox;
5368 }
5369 if (!phba->sli4_hba.intr_enable)
5370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5371 else {
James Smarta183a152011-10-10 21:32:43 -04005372 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005373 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5374 }
5375 if (unlikely(rc)) {
5376 rc = -EIO;
5377 goto out_free_mbox;
5378 }
5379
5380 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5381 if (bf_get(lpfc_mbox_hdr_status,
5382 &dealloc_rsrc->header.cfg_shdr.response)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "2919 Failed to release resource extents "
5385 "for type %d - Status 0x%x Add'l Status 0x%x. "
5386 "Resource memory not released.\n",
5387 type,
5388 bf_get(lpfc_mbox_hdr_status,
5389 &dealloc_rsrc->header.cfg_shdr.response),
5390 bf_get(lpfc_mbox_hdr_add_status,
5391 &dealloc_rsrc->header.cfg_shdr.response));
5392 rc = -EIO;
5393 goto out_free_mbox;
5394 }
5395
5396 /* Release kernel memory resources for the specific type. */
5397 switch (type) {
5398 case LPFC_RSC_TYPE_FCOE_VPI:
5399 kfree(phba->vpi_bmask);
5400 kfree(phba->vpi_ids);
5401 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5402 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5403 &phba->lpfc_vpi_blk_list, list) {
5404 list_del_init(&rsrc_blk->list);
5405 kfree(rsrc_blk);
5406 }
5407 break;
5408 case LPFC_RSC_TYPE_FCOE_XRI:
5409 kfree(phba->sli4_hba.xri_bmask);
5410 kfree(phba->sli4_hba.xri_ids);
5411 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5412 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5413 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5414 list_del_init(&rsrc_blk->list);
5415 kfree(rsrc_blk);
5416 }
5417 break;
5418 case LPFC_RSC_TYPE_FCOE_VFI:
5419 kfree(phba->sli4_hba.vfi_bmask);
5420 kfree(phba->sli4_hba.vfi_ids);
5421 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5422 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5423 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5424 list_del_init(&rsrc_blk->list);
5425 kfree(rsrc_blk);
5426 }
5427 break;
5428 case LPFC_RSC_TYPE_FCOE_RPI:
5429 /* RPI bitmask and physical id array are cleaned up earlier. */
5430 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5431 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5432 list_del_init(&rsrc_blk->list);
5433 kfree(rsrc_blk);
5434 }
5435 break;
5436 default:
5437 break;
5438 }
5439
5440 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5441
5442 out_free_mbox:
5443 mempool_free(mbox, phba->mbox_mem_pool);
5444 return rc;
5445}
5446
5447/**
5448 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5449 * @phba: Pointer to HBA context object.
5450 *
5451 * This function allocates all SLI4 resource identifiers.
5452 **/
5453int
5454lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5455{
5456 int i, rc, error = 0;
5457 uint16_t count, base;
5458 unsigned long longs;
5459
5460 if (phba->sli4_hba.extents_in_use) {
5461 /*
5462 * The port supports resource extents. The XRI, VPI, VFI, RPI
5463 * resource extent count must be read and allocated before
5464 * provisioning the resource id arrays.
5465 */
5466 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5467 LPFC_IDX_RSRC_RDY) {
5468 /*
5469 * Extent-based resources are set - the driver could
5470 * be in a port reset. Figure out if any corrective
5471 * actions need to be taken.
5472 */
5473 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5474 LPFC_RSC_TYPE_FCOE_VFI);
5475 if (rc != 0)
5476 error++;
5477 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5478 LPFC_RSC_TYPE_FCOE_VPI);
5479 if (rc != 0)
5480 error++;
5481 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5482 LPFC_RSC_TYPE_FCOE_XRI);
5483 if (rc != 0)
5484 error++;
5485 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5486 LPFC_RSC_TYPE_FCOE_RPI);
5487 if (rc != 0)
5488 error++;
5489
5490 /*
5491 * It's possible that the number of resources
5492 * provided to this port instance changed between
5493 * resets. Detect this condition and reallocate
5494 * resources. Otherwise, there is no action.
5495 */
5496 if (error) {
5497 lpfc_printf_log(phba, KERN_INFO,
5498 LOG_MBOX | LOG_INIT,
5499 "2931 Detected extent resource "
5500 "change. Reallocating all "
5501 "extents.\n");
5502 rc = lpfc_sli4_dealloc_extent(phba,
5503 LPFC_RSC_TYPE_FCOE_VFI);
5504 rc = lpfc_sli4_dealloc_extent(phba,
5505 LPFC_RSC_TYPE_FCOE_VPI);
5506 rc = lpfc_sli4_dealloc_extent(phba,
5507 LPFC_RSC_TYPE_FCOE_XRI);
5508 rc = lpfc_sli4_dealloc_extent(phba,
5509 LPFC_RSC_TYPE_FCOE_RPI);
5510 } else
5511 return 0;
5512 }
5513
5514 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5515 if (unlikely(rc))
5516 goto err_exit;
5517
5518 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5519 if (unlikely(rc))
5520 goto err_exit;
5521
5522 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5523 if (unlikely(rc))
5524 goto err_exit;
5525
5526 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5527 if (unlikely(rc))
5528 goto err_exit;
5529 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5530 LPFC_IDX_RSRC_RDY);
5531 return rc;
5532 } else {
5533 /*
5534 * The port does not support resource extents. The XRI, VPI,
5535 * VFI, RPI resource ids were determined from READ_CONFIG.
5536 * Just allocate the bitmasks and provision the resource id
5537 * arrays. If a port reset is active, the resources don't
5538 * need any action - just exit.
5539 */
5540 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5541 LPFC_IDX_RSRC_RDY)
5542 return 0;
5543
5544 /* RPIs. */
5545 count = phba->sli4_hba.max_cfg_param.max_rpi;
5546 base = phba->sli4_hba.max_cfg_param.rpi_base;
5547 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5548 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5549 sizeof(unsigned long),
5550 GFP_KERNEL);
5551 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5552 rc = -ENOMEM;
5553 goto err_exit;
5554 }
5555 phba->sli4_hba.rpi_ids = kzalloc(count *
5556 sizeof(uint16_t),
5557 GFP_KERNEL);
5558 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5559 rc = -ENOMEM;
5560 goto free_rpi_bmask;
5561 }
5562
5563 for (i = 0; i < count; i++)
5564 phba->sli4_hba.rpi_ids[i] = base + i;
5565
5566 /* VPIs. */
5567 count = phba->sli4_hba.max_cfg_param.max_vpi;
5568 base = phba->sli4_hba.max_cfg_param.vpi_base;
5569 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5570 phba->vpi_bmask = kzalloc(longs *
5571 sizeof(unsigned long),
5572 GFP_KERNEL);
5573 if (unlikely(!phba->vpi_bmask)) {
5574 rc = -ENOMEM;
5575 goto free_rpi_ids;
5576 }
5577 phba->vpi_ids = kzalloc(count *
5578 sizeof(uint16_t),
5579 GFP_KERNEL);
5580 if (unlikely(!phba->vpi_ids)) {
5581 rc = -ENOMEM;
5582 goto free_vpi_bmask;
5583 }
5584
5585 for (i = 0; i < count; i++)
5586 phba->vpi_ids[i] = base + i;
5587
5588 /* XRIs. */
5589 count = phba->sli4_hba.max_cfg_param.max_xri;
5590 base = phba->sli4_hba.max_cfg_param.xri_base;
5591 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5592 phba->sli4_hba.xri_bmask = kzalloc(longs *
5593 sizeof(unsigned long),
5594 GFP_KERNEL);
5595 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5596 rc = -ENOMEM;
5597 goto free_vpi_ids;
5598 }
5599 phba->sli4_hba.xri_ids = kzalloc(count *
5600 sizeof(uint16_t),
5601 GFP_KERNEL);
5602 if (unlikely(!phba->sli4_hba.xri_ids)) {
5603 rc = -ENOMEM;
5604 goto free_xri_bmask;
5605 }
5606
5607 for (i = 0; i < count; i++)
5608 phba->sli4_hba.xri_ids[i] = base + i;
5609
5610 /* VFIs. */
5611 count = phba->sli4_hba.max_cfg_param.max_vfi;
5612 base = phba->sli4_hba.max_cfg_param.vfi_base;
5613 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5614 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5615 sizeof(unsigned long),
5616 GFP_KERNEL);
5617 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5618 rc = -ENOMEM;
5619 goto free_xri_ids;
5620 }
5621 phba->sli4_hba.vfi_ids = kzalloc(count *
5622 sizeof(uint16_t),
5623 GFP_KERNEL);
5624 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5625 rc = -ENOMEM;
5626 goto free_vfi_bmask;
5627 }
5628
5629 for (i = 0; i < count; i++)
5630 phba->sli4_hba.vfi_ids[i] = base + i;
5631
5632 /*
5633 * Mark all resources ready. An HBA reset doesn't need
5634 * to reset the initialization.
5635 */
5636 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5637 LPFC_IDX_RSRC_RDY);
5638 return 0;
5639 }
5640
5641 free_vfi_bmask:
5642 kfree(phba->sli4_hba.vfi_bmask);
5643 free_xri_ids:
5644 kfree(phba->sli4_hba.xri_ids);
5645 free_xri_bmask:
5646 kfree(phba->sli4_hba.xri_bmask);
5647 free_vpi_ids:
5648 kfree(phba->vpi_ids);
5649 free_vpi_bmask:
5650 kfree(phba->vpi_bmask);
5651 free_rpi_ids:
5652 kfree(phba->sli4_hba.rpi_ids);
5653 free_rpi_bmask:
5654 kfree(phba->sli4_hba.rpi_bmask);
5655 err_exit:
5656 return rc;
5657}
5658
5659/**
5660 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5661 * @phba: Pointer to HBA context object.
5662 *
5663 * This function allocates the number of elements for the specified
5664 * resource type.
5665 **/
5666int
5667lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5668{
5669 if (phba->sli4_hba.extents_in_use) {
5670 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5671 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5672 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5673 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5674 } else {
5675 kfree(phba->vpi_bmask);
5676 kfree(phba->vpi_ids);
5677 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5678 kfree(phba->sli4_hba.xri_bmask);
5679 kfree(phba->sli4_hba.xri_ids);
5680 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5681 kfree(phba->sli4_hba.vfi_bmask);
5682 kfree(phba->sli4_hba.vfi_ids);
5683 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5684 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5685 }
5686
5687 return 0;
5688}
5689
5690/**
James Smartb76f2dc2011-07-22 18:37:42 -04005691 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5692 * @phba: Pointer to HBA context object.
5693 * @type: The resource extent type.
5694 * @extnt_count: buffer to hold port extent count response
5695 * @extnt_size: buffer to hold port extent size response.
5696 *
5697 * This function calls the port to read the host allocated extents
5698 * for a particular type.
5699 **/
5700int
5701lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5702 uint16_t *extnt_cnt, uint16_t *extnt_size)
5703{
5704 bool emb;
5705 int rc = 0;
5706 uint16_t curr_blks = 0;
5707 uint32_t req_len, emb_len;
5708 uint32_t alloc_len, mbox_tmo;
5709 struct list_head *blk_list_head;
5710 struct lpfc_rsrc_blks *rsrc_blk;
5711 LPFC_MBOXQ_t *mbox;
5712 void *virtaddr = NULL;
5713 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5714 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5715 union lpfc_sli4_cfg_shdr *shdr;
5716
5717 switch (type) {
5718 case LPFC_RSC_TYPE_FCOE_VPI:
5719 blk_list_head = &phba->lpfc_vpi_blk_list;
5720 break;
5721 case LPFC_RSC_TYPE_FCOE_XRI:
5722 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5723 break;
5724 case LPFC_RSC_TYPE_FCOE_VFI:
5725 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5726 break;
5727 case LPFC_RSC_TYPE_FCOE_RPI:
5728 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5729 break;
5730 default:
5731 return -EIO;
5732 }
5733
5734 /* Count the number of extents currently allocatd for this type. */
5735 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5736 if (curr_blks == 0) {
5737 /*
5738 * The GET_ALLOCATED mailbox does not return the size,
5739 * just the count. The size should be just the size
5740 * stored in the current allocated block and all sizes
5741 * for an extent type are the same so set the return
5742 * value now.
5743 */
5744 *extnt_size = rsrc_blk->rsrc_size;
5745 }
5746 curr_blks++;
5747 }
5748
5749 /* Calculate the total requested length of the dma memory. */
5750 req_len = curr_blks * sizeof(uint16_t);
5751
5752 /*
5753 * Calculate the size of an embedded mailbox. The uint32_t
5754 * accounts for extents-specific word.
5755 */
5756 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5757 sizeof(uint32_t);
5758
5759 /*
5760 * Presume the allocation and response will fit into an embedded
5761 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5762 */
5763 emb = LPFC_SLI4_MBX_EMBED;
5764 req_len = emb_len;
5765 if (req_len > emb_len) {
5766 req_len = curr_blks * sizeof(uint16_t) +
5767 sizeof(union lpfc_sli4_cfg_shdr) +
5768 sizeof(uint32_t);
5769 emb = LPFC_SLI4_MBX_NEMBED;
5770 }
5771
5772 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5773 if (!mbox)
5774 return -ENOMEM;
5775 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5776
5777 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5778 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5779 req_len, emb);
5780 if (alloc_len < req_len) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5782 "2983 Allocated DMA memory size (x%x) is "
5783 "less than the requested DMA memory "
5784 "size (x%x)\n", alloc_len, req_len);
5785 rc = -ENOMEM;
5786 goto err_exit;
5787 }
5788 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5789 if (unlikely(rc)) {
5790 rc = -EIO;
5791 goto err_exit;
5792 }
5793
5794 if (!phba->sli4_hba.intr_enable)
5795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5796 else {
James Smarta183a152011-10-10 21:32:43 -04005797 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005798 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5799 }
5800
5801 if (unlikely(rc)) {
5802 rc = -EIO;
5803 goto err_exit;
5804 }
5805
5806 /*
5807 * Figure out where the response is located. Then get local pointers
5808 * to the response data. The port does not guarantee to respond to
5809 * all extents counts request so update the local variable with the
5810 * allocated count from the port.
5811 */
5812 if (emb == LPFC_SLI4_MBX_EMBED) {
5813 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5814 shdr = &rsrc_ext->header.cfg_shdr;
5815 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5816 } else {
5817 virtaddr = mbox->sge_array->addr[0];
5818 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5819 shdr = &n_rsrc->cfg_shdr;
5820 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5821 }
5822
5823 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5825 "2984 Failed to read allocated resources "
5826 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5827 type,
5828 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5829 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5830 rc = -EIO;
5831 goto err_exit;
5832 }
5833 err_exit:
5834 lpfc_sli4_mbox_cmd_free(phba, mbox);
5835 return rc;
5836}
5837
5838/**
James Smartda0436e2009-05-22 14:51:39 -04005839 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5840 * @phba: Pointer to HBA context object.
5841 *
5842 * This function is the main SLI4 device intialization PCI function. This
5843 * function is called by the HBA intialization code, HBA reset code and
5844 * HBA error attention handler code. Caller is not required to hold any
5845 * locks.
5846 **/
5847int
5848lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5849{
5850 int rc;
5851 LPFC_MBOXQ_t *mboxq;
5852 struct lpfc_mqe *mqe;
5853 uint8_t *vpd;
5854 uint32_t vpd_size;
5855 uint32_t ftr_rsp = 0;
5856 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5857 struct lpfc_vport *vport = phba->pport;
5858 struct lpfc_dmabuf *mp;
5859
5860 /* Perform a PCI function reset to start from clean */
5861 rc = lpfc_pci_function_reset(phba);
5862 if (unlikely(rc))
5863 return -ENODEV;
5864
5865 /* Check the HBA Host Status Register for readyness */
5866 rc = lpfc_sli4_post_status_check(phba);
5867 if (unlikely(rc))
5868 return -ENODEV;
5869 else {
5870 spin_lock_irq(&phba->hbalock);
5871 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5872 spin_unlock_irq(&phba->hbalock);
5873 }
5874
5875 /*
5876 * Allocate a single mailbox container for initializing the
5877 * port.
5878 */
5879 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5880 if (!mboxq)
5881 return -ENOMEM;
5882
5883 /*
5884 * Continue initialization with default values even if driver failed
5885 * to read FCoE param config regions
5886 */
5887 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
James Smart05580562011-05-24 11:40:48 -04005888 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04005889 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04005890
5891 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005892 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005893 vpd = kzalloc(vpd_size, GFP_KERNEL);
5894 if (!vpd) {
5895 rc = -ENOMEM;
5896 goto out_free_mbox;
5897 }
5898
5899 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005900 if (unlikely(rc)) {
5901 kfree(vpd);
5902 goto out_free_mbox;
5903 }
James Smartda0436e2009-05-22 14:51:39 -04005904 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005905 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5906 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005907 phba->hba_flag |= HBA_FCOE_MODE;
5908 else
5909 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005910
5911 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5912 LPFC_DCBX_CEE_MODE)
5913 phba->hba_flag |= HBA_FIP_SUPPORT;
5914 else
5915 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5916
James Smartc31098c2011-04-16 11:03:33 -04005917 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5919 "0376 READ_REV Error. SLI Level %d "
5920 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005921 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005922 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005923 kfree(vpd);
5924 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005925 }
James Smartcd1c8302011-10-10 21:33:25 -04005926
5927 /*
5928 * Retrieve sli4 device physical port name, failure of doing it
5929 * is considered as non-fatal.
5930 */
5931 rc = lpfc_sli4_retrieve_pport_name(phba);
5932 if (!rc)
5933 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5934 "3080 Successful retrieving SLI4 device "
5935 "physical port name: %s.\n", phba->Port);
5936
James Smartda0436e2009-05-22 14:51:39 -04005937 /*
5938 * Evaluate the read rev and vpd data. Populate the driver
5939 * state with the results. If this routine fails, the failure
5940 * is not fatal as the driver will use generic values.
5941 */
5942 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5943 if (unlikely(!rc)) {
5944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5945 "0377 Error %d parsing vpd. "
5946 "Using defaults.\n", rc);
5947 rc = 0;
5948 }
James Smart76a95d72010-11-20 23:11:48 -05005949 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005950
James Smartf1126682009-06-10 17:22:44 -04005951 /* Save information as VPD data */
5952 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5953 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5954 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5955 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5956 &mqe->un.read_rev);
5957 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5958 &mqe->un.read_rev);
5959 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5960 &mqe->un.read_rev);
5961 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5962 &mqe->un.read_rev);
5963 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5964 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5965 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5966 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5967 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5968 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5969 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5970 "(%d):0380 READ_REV Status x%x "
5971 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5972 mboxq->vport ? mboxq->vport->vpi : 0,
5973 bf_get(lpfc_mqe_status, mqe),
5974 phba->vpd.rev.opFwName,
5975 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5976 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04005977
5978 /*
5979 * Discover the port's supported feature set and match it against the
5980 * hosts requests.
5981 */
5982 lpfc_request_features(phba, mboxq);
5983 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5984 if (unlikely(rc)) {
5985 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005986 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005987 }
5988
5989 /*
5990 * The port must support FCP initiator mode as this is the
5991 * only mode running in the host.
5992 */
5993 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5994 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5995 "0378 No support for fcpi mode.\n");
5996 ftr_rsp++;
5997 }
James Smartfedd3b72011-02-16 12:39:24 -05005998 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5999 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6000 else
6001 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006002 /*
6003 * If the port cannot support the host's requested features
6004 * then turn off the global config parameters to disable the
6005 * feature in the driver. This is not a fatal error.
6006 */
James Smartbf086112011-08-21 21:48:13 -04006007 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6008 if (phba->cfg_enable_bg) {
6009 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6010 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6011 else
6012 ftr_rsp++;
6013 }
James Smartda0436e2009-05-22 14:51:39 -04006014
6015 if (phba->max_vpi && phba->cfg_enable_npiv &&
6016 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6017 ftr_rsp++;
6018
6019 if (ftr_rsp) {
6020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6021 "0379 Feature Mismatch Data: x%08x %08x "
6022 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6023 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6024 phba->cfg_enable_npiv, phba->max_vpi);
6025 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6026 phba->cfg_enable_bg = 0;
6027 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6028 phba->cfg_enable_npiv = 0;
6029 }
6030
6031 /* These SLI3 features are assumed in SLI4 */
6032 spin_lock_irq(&phba->hbalock);
6033 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6034 spin_unlock_irq(&phba->hbalock);
6035
James Smart6d368e52011-05-24 11:44:12 -04006036 /*
6037 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6038 * calls depends on these resources to complete port setup.
6039 */
6040 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6041 if (rc) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6043 "2920 Failed to alloc Resource IDs "
6044 "rc = x%x\n", rc);
6045 goto out_free_mbox;
6046 }
6047
James Smartda0436e2009-05-22 14:51:39 -04006048 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006049 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6050 if (rc) {
6051 phba->link_state = LPFC_HBA_ERROR;
6052 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006053 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006054 }
6055
James Smartda0436e2009-05-22 14:51:39 -04006056 mboxq->vport = vport;
6057 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6058 mp = (struct lpfc_dmabuf *) mboxq->context1;
6059 if (rc == MBX_SUCCESS) {
6060 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6061 rc = 0;
6062 }
6063
6064 /*
6065 * This memory was allocated by the lpfc_read_sparam routine. Release
6066 * it to the mbuf pool.
6067 */
6068 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6069 kfree(mp);
6070 mboxq->context1 = NULL;
6071 if (unlikely(rc)) {
6072 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6073 "0382 READ_SPARAM command failed "
6074 "status %d, mbxStatus x%x\n",
6075 rc, bf_get(lpfc_mqe_status, mqe));
6076 phba->link_state = LPFC_HBA_ERROR;
6077 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006078 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006079 }
6080
James Smart05580562011-05-24 11:40:48 -04006081 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006082
6083 /* Update the fc_host data structures with new wwn. */
6084 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6085 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6086
6087 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04006088 if (!phba->sli4_hba.extents_in_use) {
6089 rc = lpfc_sli4_post_els_sgl_list(phba);
6090 if (unlikely(rc)) {
6091 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6092 "0582 Error %d during els sgl post "
6093 "operation\n", rc);
6094 rc = -ENODEV;
6095 goto out_free_mbox;
6096 }
6097 } else {
6098 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6099 if (unlikely(rc)) {
6100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6101 "2560 Error %d during els sgl post "
6102 "operation\n", rc);
6103 rc = -ENODEV;
6104 goto out_free_mbox;
6105 }
James Smartda0436e2009-05-22 14:51:39 -04006106 }
6107
6108 /* Register SCSI SGL pool to the device */
6109 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6110 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006112 "0383 Error %d during scsi sgl post "
6113 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006114 /* Some Scsi buffers were moved to the abort scsi list */
6115 /* A pci function reset will repost them */
6116 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006117 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006118 }
6119
6120 /* Post the rpi header region to the device. */
6121 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6122 if (unlikely(rc)) {
6123 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6124 "0393 Error %d during rpi post operation\n",
6125 rc);
6126 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006127 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006128 }
James Smartda0436e2009-05-22 14:51:39 -04006129
James Smart5350d872011-10-10 21:33:49 -04006130 /* Create all the SLI4 queues */
6131 rc = lpfc_sli4_queue_create(phba);
6132 if (rc) {
6133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6134 "3089 Failed to allocate queues\n");
6135 rc = -ENODEV;
6136 goto out_stop_timers;
6137 }
James Smartda0436e2009-05-22 14:51:39 -04006138 /* Set up all the queues to the device */
6139 rc = lpfc_sli4_queue_setup(phba);
6140 if (unlikely(rc)) {
6141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6142 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006143 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006144 }
6145
6146 /* Arm the CQs and then EQs on device */
6147 lpfc_sli4_arm_cqeq_intr(phba);
6148
6149 /* Indicate device interrupt mode */
6150 phba->sli4_hba.intr_enable = 1;
6151
6152 /* Allow asynchronous mailbox command to go through */
6153 spin_lock_irq(&phba->hbalock);
6154 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6155 spin_unlock_irq(&phba->hbalock);
6156
6157 /* Post receive buffers to the device */
6158 lpfc_sli4_rb_setup(phba);
6159
James Smartfc2b9892010-02-26 14:15:29 -05006160 /* Reset HBA FCF states after HBA reset */
6161 phba->fcf.fcf_flag = 0;
6162 phba->fcf.current_rec.flag = 0;
6163
James Smartda0436e2009-05-22 14:51:39 -04006164 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006165 mod_timer(&vport->els_tmofunc,
6166 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006167
6168 /* Start heart beat timer */
6169 mod_timer(&phba->hb_tmofunc,
6170 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6171 phba->hb_outstanding = 0;
6172 phba->last_completion_time = jiffies;
6173
6174 /* Start error attention (ERATT) polling timer */
6175 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6176
James Smart75baf692010-06-08 18:31:21 -04006177 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6178 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6179 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6180 if (!rc) {
6181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6182 "2829 This device supports "
6183 "Advanced Error Reporting (AER)\n");
6184 spin_lock_irq(&phba->hbalock);
6185 phba->hba_flag |= HBA_AER_ENABLED;
6186 spin_unlock_irq(&phba->hbalock);
6187 } else {
6188 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6189 "2830 This device does not support "
6190 "Advanced Error Reporting (AER)\n");
6191 phba->cfg_aer_support = 0;
6192 }
James Smart0a96e972011-07-22 18:37:28 -04006193 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006194 }
6195
James Smart76a95d72010-11-20 23:11:48 -05006196 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6197 /*
6198 * The FC Port needs to register FCFI (index 0)
6199 */
6200 lpfc_reg_fcfi(phba, mboxq);
6201 mboxq->vport = phba->pport;
6202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006203 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006204 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006205 rc = 0;
6206 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6207 &mboxq->u.mqe.un.reg_fcfi);
James Smart76a95d72010-11-20 23:11:48 -05006208 }
James Smartda0436e2009-05-22 14:51:39 -04006209 /*
6210 * The port is ready, set the host's link state to LINK_DOWN
6211 * in preparation for link interrupts.
6212 */
James Smartda0436e2009-05-22 14:51:39 -04006213 spin_lock_irq(&phba->hbalock);
6214 phba->link_state = LPFC_LINK_DOWN;
6215 spin_unlock_irq(&phba->hbalock);
James Smart5350d872011-10-10 21:33:49 -04006216 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smartfedd3b72011-02-16 12:39:24 -05006217 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart5350d872011-10-10 21:33:49 -04006218 if (rc)
6219 goto out_unset_queue;
6220 }
6221 mempool_free(mboxq, phba->mbox_mem_pool);
6222 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006223out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006224 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006225 lpfc_sli4_queue_unset(phba);
6226out_destroy_queue:
6227 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006228out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006229 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006230out_free_mbox:
6231 mempool_free(mboxq, phba->mbox_mem_pool);
6232 return rc;
6233}
James Smarte59058c2008-08-24 21:49:00 -04006234
6235/**
James Smart3621a712009-04-06 18:47:14 -04006236 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006237 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006238 *
James Smarte59058c2008-08-24 21:49:00 -04006239 * This is the callback function for mailbox timer. The mailbox
6240 * timer is armed when a new mailbox command is issued and the timer
6241 * is deleted when the mailbox complete. The function is called by
6242 * the kernel timer code when a mailbox does not complete within
6243 * expected time. This function wakes up the worker thread to
6244 * process the mailbox timeout and returns. All the processing is
6245 * done by the worker thread function lpfc_mbox_timeout_handler.
6246 **/
dea31012005-04-17 16:05:31 -05006247void
6248lpfc_mbox_timeout(unsigned long ptr)
6249{
James Smart92d7f7b2007-06-17 19:56:38 -05006250 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006251 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006252 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006253
James Smart2e0fef82007-06-17 19:56:36 -05006254 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006255 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006256 if (!tmo_posted)
6257 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6258 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6259
James Smart5e9d9b82008-06-14 22:52:53 -04006260 if (!tmo_posted)
6261 lpfc_worker_wake_up(phba);
6262 return;
dea31012005-04-17 16:05:31 -05006263}
6264
James Smarte59058c2008-08-24 21:49:00 -04006265
6266/**
James Smart3621a712009-04-06 18:47:14 -04006267 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006268 * @phba: Pointer to HBA context object.
6269 *
6270 * This function is called from worker thread when a mailbox command times out.
6271 * The caller is not required to hold any locks. This function will reset the
6272 * HBA and recover all the pending commands.
6273 **/
dea31012005-04-17 16:05:31 -05006274void
6275lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6276{
James Smart2e0fef82007-06-17 19:56:36 -05006277 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006278 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006279 struct lpfc_sli *psli = &phba->sli;
6280 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006281
James Smarta257bf92009-04-06 18:48:10 -04006282 /* Check the pmbox pointer first. There is a race condition
6283 * between the mbox timeout handler getting executed in the
6284 * worklist and the mailbox actually completing. When this
6285 * race condition occurs, the mbox_active will be NULL.
6286 */
6287 spin_lock_irq(&phba->hbalock);
6288 if (pmbox == NULL) {
6289 lpfc_printf_log(phba, KERN_WARNING,
6290 LOG_MBOX | LOG_SLI,
6291 "0353 Active Mailbox cleared - mailbox timeout "
6292 "exiting\n");
6293 spin_unlock_irq(&phba->hbalock);
6294 return;
6295 }
6296
dea31012005-04-17 16:05:31 -05006297 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006298 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006299 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006300 mb->mbxCommand,
6301 phba->pport->port_state,
6302 phba->sli.sli_flag,
6303 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006304 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006305
James Smart1dcb58e2007-04-25 09:51:30 -04006306 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6307 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006308 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006309 */
James Smart2e0fef82007-06-17 19:56:36 -05006310 spin_lock_irq(&phba->pport->work_port_lock);
6311 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6312 spin_unlock_irq(&phba->pport->work_port_lock);
6313 spin_lock_irq(&phba->hbalock);
6314 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006315 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006316 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006317
6318 pring = &psli->ring[psli->fcp_ring];
6319 lpfc_sli_abort_iocb_ring(phba, pring);
6320
6321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006322 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006323
6324 /* Reset the HBA device */
6325 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006326}
6327
James Smarte59058c2008-08-24 21:49:00 -04006328/**
James Smart3772a992009-05-22 14:50:54 -04006329 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006330 * @phba: Pointer to HBA context object.
6331 * @pmbox: Pointer to mailbox object.
6332 * @flag: Flag indicating how the mailbox need to be processed.
6333 *
6334 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006335 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6336 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006337 * The mailbox command can be submitted in polling mode, in which case
6338 * this function will wait in a polling loop for the completion of the
6339 * mailbox.
6340 * If the mailbox is submitted in no_wait mode (not polling) the
6341 * function will submit the command and returns immediately without waiting
6342 * for the mailbox completion. The no_wait is supported only when HBA
6343 * is in SLI2/SLI3 mode - interrupts are enabled.
6344 * The SLI interface allows only one mailbox pending at a time. If the
6345 * mailbox is issued in polling mode and there is already a mailbox
6346 * pending, then the function will return an error. If the mailbox is issued
6347 * in NO_WAIT mode and there is a mailbox pending already, the function
6348 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6349 * The sli layer owns the mailbox object until the completion of mailbox
6350 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6351 * return codes the caller owns the mailbox command after the return of
6352 * the function.
6353 **/
James Smart3772a992009-05-22 14:50:54 -04006354static int
6355lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6356 uint32_t flag)
dea31012005-04-17 16:05:31 -05006357{
dea31012005-04-17 16:05:31 -05006358 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006359 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006360 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006361 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006362 int i;
James Smart09372822008-01-11 01:52:54 -05006363 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006364 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006365 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006366 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006367 int processing_queue = 0;
6368
6369 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6370 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006371 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006372 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006373 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6374 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6375 return MBX_SUCCESS;
6376 }
James Smart58da1ff2008-04-07 10:15:56 -04006377 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006378 pmbox = lpfc_mbox_get(phba);
6379 if (!pmbox) {
6380 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6381 return MBX_SUCCESS;
6382 }
6383 }
dea31012005-04-17 16:05:31 -05006384
James Smarted957682007-06-17 19:56:37 -05006385 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006386 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006387 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006388 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006389 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006390 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006391 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006392 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006393 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006394 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006395 }
6396 }
6397
Linas Vepstas8d63f372007-02-14 14:28:36 -06006398 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006399 if (unlikely(pci_channel_offline(phba->pcidev))) {
6400 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6401 goto out_not_finished;
6402 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006403
James Smarta257bf92009-04-06 18:48:10 -04006404 /* If HBA has a deferred error attention, fail the iocb. */
6405 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6406 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6407 goto out_not_finished;
6408 }
6409
dea31012005-04-17 16:05:31 -05006410 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006411
James Smart3772a992009-05-22 14:50:54 -04006412 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006413 status = MBX_SUCCESS;
6414
James Smart2e0fef82007-06-17 19:56:36 -05006415 if (phba->link_state == LPFC_HBA_ERROR) {
6416 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006417
6418 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006419 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6420 "(%d):0311 Mailbox command x%x cannot "
6421 "issue Data: x%x x%x\n",
6422 pmbox->vport ? pmbox->vport->vpi : 0,
6423 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006424 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006425 }
6426
James Smart9940b972011-03-11 16:06:12 -05006427 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6428 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6429 !(hc_copy & HC_MBINT_ENA)) {
6430 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6431 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006432 "(%d):2528 Mailbox command x%x cannot "
6433 "issue Data: x%x x%x\n",
6434 pmbox->vport ? pmbox->vport->vpi : 0,
6435 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006436 goto out_not_finished;
6437 }
James Smart92908312006-03-07 15:04:13 -05006438 }
6439
dea31012005-04-17 16:05:31 -05006440 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6441 /* Polling for a mbox command when another one is already active
6442 * is not allowed in SLI. Also, the driver must have established
6443 * SLI2 mode to queue and process multiple mbox commands.
6444 */
6445
6446 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006447 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006448
6449 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006450 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6451 "(%d):2529 Mailbox command x%x "
6452 "cannot issue Data: x%x x%x\n",
6453 pmbox->vport ? pmbox->vport->vpi : 0,
6454 pmbox->u.mb.mbxCommand,
6455 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006456 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006457 }
6458
James Smart3772a992009-05-22 14:50:54 -04006459 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006460 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006461 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006462 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6463 "(%d):2530 Mailbox command x%x "
6464 "cannot issue Data: x%x x%x\n",
6465 pmbox->vport ? pmbox->vport->vpi : 0,
6466 pmbox->u.mb.mbxCommand,
6467 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006468 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006469 }
6470
dea31012005-04-17 16:05:31 -05006471 /* Another mailbox command is still being processed, queue this
6472 * command to be processed later.
6473 */
6474 lpfc_mbox_put(phba, pmbox);
6475
6476 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006477 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006478 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006479 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006480 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6481 mb->mbxCommand, phba->pport->port_state,
6482 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006483
6484 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006485 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006486
James Smart858c9f62007-06-17 19:56:39 -05006487 if (pmbox->vport) {
6488 lpfc_debugfs_disc_trc(pmbox->vport,
6489 LPFC_DISC_TRC_MBOX_VPORT,
6490 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6491 (uint32_t)mb->mbxCommand,
6492 mb->un.varWords[0], mb->un.varWords[1]);
6493 }
6494 else {
6495 lpfc_debugfs_disc_trc(phba->pport,
6496 LPFC_DISC_TRC_MBOX,
6497 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6498 (uint32_t)mb->mbxCommand,
6499 mb->un.varWords[0], mb->un.varWords[1]);
6500 }
6501
James Smart2e0fef82007-06-17 19:56:36 -05006502 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006503 }
6504
dea31012005-04-17 16:05:31 -05006505 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6506
6507 /* If we are not polling, we MUST be in SLI2 mode */
6508 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006509 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006510 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006511 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006512 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006513 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006514 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6515 "(%d):2531 Mailbox command x%x "
6516 "cannot issue Data: x%x x%x\n",
6517 pmbox->vport ? pmbox->vport->vpi : 0,
6518 pmbox->u.mb.mbxCommand,
6519 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006520 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006521 }
6522 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006523 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006524 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006525 }
6526
6527 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006528 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006529 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006530 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006531 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006532 mb->mbxCommand, phba->pport->port_state,
6533 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006534
James Smart858c9f62007-06-17 19:56:39 -05006535 if (mb->mbxCommand != MBX_HEARTBEAT) {
6536 if (pmbox->vport) {
6537 lpfc_debugfs_disc_trc(pmbox->vport,
6538 LPFC_DISC_TRC_MBOX_VPORT,
6539 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6540 (uint32_t)mb->mbxCommand,
6541 mb->un.varWords[0], mb->un.varWords[1]);
6542 }
6543 else {
6544 lpfc_debugfs_disc_trc(phba->pport,
6545 LPFC_DISC_TRC_MBOX,
6546 "MBOX Send: cmd:x%x mb:x%x x%x",
6547 (uint32_t)mb->mbxCommand,
6548 mb->un.varWords[0], mb->un.varWords[1]);
6549 }
6550 }
6551
dea31012005-04-17 16:05:31 -05006552 psli->slistat.mbox_cmd++;
6553 evtctr = psli->slistat.mbox_event;
6554
6555 /* next set own bit for the adapter and copy over command word */
6556 mb->mbxOwner = OWN_CHIP;
6557
James Smart3772a992009-05-22 14:50:54 -04006558 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006559 /* Populate mbox extension offset word. */
6560 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6561 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6562 = (uint8_t *)phba->mbox_ext
6563 - (uint8_t *)phba->mbox;
6564 }
6565
6566 /* Copy the mailbox extension data */
6567 if (pmbox->in_ext_byte_len && pmbox->context2) {
6568 lpfc_sli_pcimem_bcopy(pmbox->context2,
6569 (uint8_t *)phba->mbox_ext,
6570 pmbox->in_ext_byte_len);
6571 }
6572 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006573 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006574 } else {
James Smart7a470272010-03-15 11:25:20 -04006575 /* Populate mbox extension offset word. */
6576 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6577 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6578 = MAILBOX_HBA_EXT_OFFSET;
6579
6580 /* Copy the mailbox extension data */
6581 if (pmbox->in_ext_byte_len && pmbox->context2) {
6582 lpfc_memcpy_to_slim(phba->MBslimaddr +
6583 MAILBOX_HBA_EXT_OFFSET,
6584 pmbox->context2, pmbox->in_ext_byte_len);
6585
6586 }
James Smart92908312006-03-07 15:04:13 -05006587 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006588 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006589 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006590 }
6591
6592 /* First copy mbox command data to HBA SLIM, skip past first
6593 word */
6594 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6595 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6596 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6597
6598 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006599 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006600 to_slim = phba->MBslimaddr;
6601 writel(ldata, to_slim);
6602 readl(to_slim); /* flush */
6603
6604 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6605 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006606 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006607 }
6608 }
6609
6610 wmb();
dea31012005-04-17 16:05:31 -05006611
6612 switch (flag) {
6613 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006614 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006615 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006616 /* Interrupt board to do it */
6617 writel(CA_MBATT, phba->CAregaddr);
6618 readl(phba->CAregaddr); /* flush */
6619 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006620 break;
6621
6622 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006623 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006624 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006625 /* Interrupt board to do it */
6626 writel(CA_MBATT, phba->CAregaddr);
6627 readl(phba->CAregaddr); /* flush */
6628
James Smart3772a992009-05-22 14:50:54 -04006629 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006630 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006631 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006632 word0 = le32_to_cpu(word0);
6633 } else {
6634 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006635 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6636 spin_unlock_irqrestore(&phba->hbalock,
6637 drvr_flag);
6638 goto out_not_finished;
6639 }
dea31012005-04-17 16:05:31 -05006640 }
6641
6642 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006643 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6644 spin_unlock_irqrestore(&phba->hbalock,
6645 drvr_flag);
6646 goto out_not_finished;
6647 }
James Smarta183a152011-10-10 21:32:43 -04006648 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6649 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006650 i = 0;
dea31012005-04-17 16:05:31 -05006651 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006652 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6653 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006654 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006655 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006656 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006657 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006658 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006659 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006660 }
6661
6662 /* Check if we took a mbox interrupt while we were
6663 polling */
6664 if (((word0 & OWN_CHIP) != OWN_CHIP)
6665 && (evtctr != psli->slistat.mbox_event))
6666 break;
6667
James Smart09372822008-01-11 01:52:54 -05006668 if (i++ > 10) {
6669 spin_unlock_irqrestore(&phba->hbalock,
6670 drvr_flag);
6671 msleep(1);
6672 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6673 }
dea31012005-04-17 16:05:31 -05006674
James Smart3772a992009-05-22 14:50:54 -04006675 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006676 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006677 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006678 word0 = le32_to_cpu(word0);
6679 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6680 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006681 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006682 /* Check real SLIM for any errors */
6683 slimword0 = readl(phba->MBslimaddr);
6684 slimmb = (MAILBOX_t *) & slimword0;
6685 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6686 && slimmb->mbxStatus) {
6687 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006688 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006689 word0 = slimword0;
6690 }
6691 }
6692 } else {
6693 /* First copy command data */
6694 word0 = readl(phba->MBslimaddr);
6695 }
6696 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006697 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6698 spin_unlock_irqrestore(&phba->hbalock,
6699 drvr_flag);
6700 goto out_not_finished;
6701 }
dea31012005-04-17 16:05:31 -05006702 }
6703
James Smart3772a992009-05-22 14:50:54 -04006704 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006705 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006706 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006707 /* Copy the mailbox extension data */
6708 if (pmbox->out_ext_byte_len && pmbox->context2) {
6709 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6710 pmbox->context2,
6711 pmbox->out_ext_byte_len);
6712 }
dea31012005-04-17 16:05:31 -05006713 } else {
6714 /* First copy command data */
6715 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6716 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006717 /* Copy the mailbox extension data */
6718 if (pmbox->out_ext_byte_len && pmbox->context2) {
6719 lpfc_memcpy_from_slim(pmbox->context2,
6720 phba->MBslimaddr +
6721 MAILBOX_HBA_EXT_OFFSET,
6722 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006723 }
6724 }
6725
6726 writel(HA_MBATT, phba->HAregaddr);
6727 readl(phba->HAregaddr); /* flush */
6728
6729 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6730 status = mb->mbxStatus;
6731 }
6732
James Smart2e0fef82007-06-17 19:56:36 -05006733 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6734 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006735
6736out_not_finished:
6737 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006738 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006739 lpfc_mbox_cmpl_put(phba, pmbox);
6740 }
6741 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006742}
6743
James Smarte59058c2008-08-24 21:49:00 -04006744/**
James Smartf1126682009-06-10 17:22:44 -04006745 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6746 * @phba: Pointer to HBA context object.
6747 *
6748 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6749 * the driver internal pending mailbox queue. It will then try to wait out the
6750 * possible outstanding mailbox command before return.
6751 *
6752 * Returns:
6753 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6754 * the outstanding mailbox command timed out.
6755 **/
6756static int
6757lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6758{
6759 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006760 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006761 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006762
6763 /* Mark the asynchronous mailbox command posting as blocked */
6764 spin_lock_irq(&phba->hbalock);
6765 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006766 /* Determine how long we might wait for the active mailbox
6767 * command to be gracefully completed by firmware.
6768 */
James Smarta183a152011-10-10 21:32:43 -04006769 if (phba->sli.mbox_active)
6770 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6771 phba->sli.mbox_active) *
6772 1000) + jiffies;
6773 spin_unlock_irq(&phba->hbalock);
6774
James Smartf1126682009-06-10 17:22:44 -04006775 /* Wait for the outstnading mailbox command to complete */
6776 while (phba->sli.mbox_active) {
6777 /* Check active mailbox complete status every 2ms */
6778 msleep(2);
6779 if (time_after(jiffies, timeout)) {
6780 /* Timeout, marked the outstanding cmd not complete */
6781 rc = 1;
6782 break;
6783 }
6784 }
6785
6786 /* Can not cleanly block async mailbox command, fails it */
6787 if (rc) {
6788 spin_lock_irq(&phba->hbalock);
6789 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6790 spin_unlock_irq(&phba->hbalock);
6791 }
6792 return rc;
6793}
6794
6795/**
6796 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6797 * @phba: Pointer to HBA context object.
6798 *
6799 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6800 * commands from the driver internal pending mailbox queue. It makes sure
6801 * that there is no outstanding mailbox command before resuming posting
6802 * asynchronous mailbox commands. If, for any reason, there is outstanding
6803 * mailbox command, it will try to wait it out before resuming asynchronous
6804 * mailbox command posting.
6805 **/
6806static void
6807lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6808{
6809 struct lpfc_sli *psli = &phba->sli;
6810
6811 spin_lock_irq(&phba->hbalock);
6812 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6813 /* Asynchronous mailbox posting is not blocked, do nothing */
6814 spin_unlock_irq(&phba->hbalock);
6815 return;
6816 }
6817
6818 /* Outstanding synchronous mailbox command is guaranteed to be done,
6819 * successful or timeout, after timing-out the outstanding mailbox
6820 * command shall always be removed, so just unblock posting async
6821 * mailbox command and resume
6822 */
6823 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6824 spin_unlock_irq(&phba->hbalock);
6825
6826 /* wake up worker thread to post asynchronlous mailbox command */
6827 lpfc_worker_wake_up(phba);
6828}
6829
6830/**
James Smartda0436e2009-05-22 14:51:39 -04006831 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6832 * @phba: Pointer to HBA context object.
6833 * @mboxq: Pointer to mailbox object.
6834 *
6835 * The function posts a mailbox to the port. The mailbox is expected
6836 * to be comletely filled in and ready for the port to operate on it.
6837 * This routine executes a synchronous completion operation on the
6838 * mailbox by polling for its completion.
6839 *
6840 * The caller must not be holding any locks when calling this routine.
6841 *
6842 * Returns:
6843 * MBX_SUCCESS - mailbox posted successfully
6844 * Any of the MBX error values.
6845 **/
6846static int
6847lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6848{
6849 int rc = MBX_SUCCESS;
6850 unsigned long iflag;
6851 uint32_t db_ready;
6852 uint32_t mcqe_status;
6853 uint32_t mbx_cmnd;
6854 unsigned long timeout;
6855 struct lpfc_sli *psli = &phba->sli;
6856 struct lpfc_mqe *mb = &mboxq->u.mqe;
6857 struct lpfc_bmbx_create *mbox_rgn;
6858 struct dma_address *dma_address;
6859 struct lpfc_register bmbx_reg;
6860
6861 /*
6862 * Only one mailbox can be active to the bootstrap mailbox region
6863 * at a time and there is no queueing provided.
6864 */
6865 spin_lock_irqsave(&phba->hbalock, iflag);
6866 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6867 spin_unlock_irqrestore(&phba->hbalock, iflag);
6868 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006869 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006870 "cannot issue Data: x%x x%x\n",
6871 mboxq->vport ? mboxq->vport->vpi : 0,
6872 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006873 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6874 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006875 psli->sli_flag, MBX_POLL);
6876 return MBXERR_ERROR;
6877 }
6878 /* The server grabs the token and owns it until release */
6879 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6880 phba->sli.mbox_active = mboxq;
6881 spin_unlock_irqrestore(&phba->hbalock, iflag);
6882
6883 /*
6884 * Initialize the bootstrap memory region to avoid stale data areas
6885 * in the mailbox post. Then copy the caller's mailbox contents to
6886 * the bmbx mailbox region.
6887 */
6888 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6889 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6890 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6891 sizeof(struct lpfc_mqe));
6892
6893 /* Post the high mailbox dma address to the port and wait for ready. */
6894 dma_address = &phba->sli4_hba.bmbx.dma_address;
6895 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6896
James Smarta183a152011-10-10 21:32:43 -04006897 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006898 * 1000) + jiffies;
6899 do {
6900 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6901 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6902 if (!db_ready)
6903 msleep(2);
6904
6905 if (time_after(jiffies, timeout)) {
6906 rc = MBXERR_ERROR;
6907 goto exit;
6908 }
6909 } while (!db_ready);
6910
6911 /* Post the low mailbox dma address to the port. */
6912 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04006913 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006914 * 1000) + jiffies;
6915 do {
6916 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6917 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6918 if (!db_ready)
6919 msleep(2);
6920
6921 if (time_after(jiffies, timeout)) {
6922 rc = MBXERR_ERROR;
6923 goto exit;
6924 }
6925 } while (!db_ready);
6926
6927 /*
6928 * Read the CQ to ensure the mailbox has completed.
6929 * If so, update the mailbox status so that the upper layers
6930 * can complete the request normally.
6931 */
6932 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6933 sizeof(struct lpfc_mqe));
6934 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6935 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6936 sizeof(struct lpfc_mcqe));
6937 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006938 /*
6939 * When the CQE status indicates a failure and the mailbox status
6940 * indicates success then copy the CQE status into the mailbox status
6941 * (and prefix it with x4000).
6942 */
James Smartda0436e2009-05-22 14:51:39 -04006943 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006944 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6945 bf_set(lpfc_mqe_status, mb,
6946 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04006947 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04006948 } else
6949 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04006950
6951 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006952 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04006953 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6954 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04006955 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6956 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6957 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006958 bf_get(lpfc_mqe_status, mb),
6959 mb->un.mb_words[0], mb->un.mb_words[1],
6960 mb->un.mb_words[2], mb->un.mb_words[3],
6961 mb->un.mb_words[4], mb->un.mb_words[5],
6962 mb->un.mb_words[6], mb->un.mb_words[7],
6963 mb->un.mb_words[8], mb->un.mb_words[9],
6964 mb->un.mb_words[10], mb->un.mb_words[11],
6965 mb->un.mb_words[12], mboxq->mcqe.word0,
6966 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6967 mboxq->mcqe.trailer);
6968exit:
6969 /* We are holding the token, no needed for lock when release */
6970 spin_lock_irqsave(&phba->hbalock, iflag);
6971 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6972 phba->sli.mbox_active = NULL;
6973 spin_unlock_irqrestore(&phba->hbalock, iflag);
6974 return rc;
6975}
6976
6977/**
6978 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6979 * @phba: Pointer to HBA context object.
6980 * @pmbox: Pointer to mailbox object.
6981 * @flag: Flag indicating how the mailbox need to be processed.
6982 *
6983 * This function is called by discovery code and HBA management code to submit
6984 * a mailbox command to firmware with SLI-4 interface spec.
6985 *
6986 * Return codes the caller owns the mailbox command after the return of the
6987 * function.
6988 **/
6989static int
6990lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6991 uint32_t flag)
6992{
6993 struct lpfc_sli *psli = &phba->sli;
6994 unsigned long iflags;
6995 int rc;
6996
James Smartb76f2dc2011-07-22 18:37:42 -04006997 /* dump from issue mailbox command if setup */
6998 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
6999
James Smart8fa38512009-07-19 10:01:03 -04007000 rc = lpfc_mbox_dev_check(phba);
7001 if (unlikely(rc)) {
7002 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007003 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007004 "cannot issue Data: x%x x%x\n",
7005 mboxq->vport ? mboxq->vport->vpi : 0,
7006 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007007 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7008 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007009 psli->sli_flag, flag);
7010 goto out_not_finished;
7011 }
7012
James Smartda0436e2009-05-22 14:51:39 -04007013 /* Detect polling mode and jump to a handler */
7014 if (!phba->sli4_hba.intr_enable) {
7015 if (flag == MBX_POLL)
7016 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7017 else
7018 rc = -EIO;
7019 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007021 "(%d):2541 Mailbox command x%x "
James Smarta183a152011-10-10 21:32:43 -04007022 "(x%x/x%x) cannot issue Data: "
7023 "x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007024 mboxq->vport ? mboxq->vport->vpi : 0,
7025 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007026 lpfc_sli_config_mbox_subsys_get(phba,
7027 mboxq),
7028 lpfc_sli_config_mbox_opcode_get(phba,
7029 mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007030 psli->sli_flag, flag);
7031 return rc;
7032 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007033 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7034 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007035 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007036 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007037 mboxq->vport ? mboxq->vport->vpi : 0,
7038 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007039 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7040 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007041 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007042 /* Try to block the asynchronous mailbox posting */
7043 rc = lpfc_sli4_async_mbox_block(phba);
7044 if (!rc) {
7045 /* Successfully blocked, now issue sync mbox cmd */
7046 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7047 if (rc != MBX_SUCCESS)
7048 lpfc_printf_log(phba, KERN_ERR,
James Smarta183a152011-10-10 21:32:43 -04007049 LOG_MBOX | LOG_SLI,
7050 "(%d):2597 Mailbox command "
7051 "x%x (x%x/x%x) cannot issue "
7052 "Data: x%x x%x\n",
7053 mboxq->vport ?
7054 mboxq->vport->vpi : 0,
7055 mboxq->u.mb.mbxCommand,
7056 lpfc_sli_config_mbox_subsys_get(phba,
7057 mboxq),
7058 lpfc_sli_config_mbox_opcode_get(phba,
7059 mboxq),
7060 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007061 /* Unblock the async mailbox posting afterward */
7062 lpfc_sli4_async_mbox_unblock(phba);
7063 }
7064 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007065 }
7066
7067 /* Now, interrupt mode asynchrous mailbox command */
7068 rc = lpfc_mbox_cmd_check(phba, mboxq);
7069 if (rc) {
7070 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007071 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007072 "cannot issue Data: x%x x%x\n",
7073 mboxq->vport ? mboxq->vport->vpi : 0,
7074 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007075 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7076 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007077 psli->sli_flag, flag);
7078 goto out_not_finished;
7079 }
James Smartda0436e2009-05-22 14:51:39 -04007080
7081 /* Put the mailbox command to the driver internal FIFO */
7082 psli->slistat.mbox_busy++;
7083 spin_lock_irqsave(&phba->hbalock, iflags);
7084 lpfc_mbox_put(phba, mboxq);
7085 spin_unlock_irqrestore(&phba->hbalock, iflags);
7086 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7087 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007088 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007089 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7090 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007091 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7092 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007093 phba->pport->port_state,
7094 psli->sli_flag, MBX_NOWAIT);
7095 /* Wake up worker thread to transport mailbox command from head */
7096 lpfc_worker_wake_up(phba);
7097
7098 return MBX_BUSY;
7099
7100out_not_finished:
7101 return MBX_NOT_FINISHED;
7102}
7103
7104/**
7105 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7106 * @phba: Pointer to HBA context object.
7107 *
7108 * This function is called by worker thread to send a mailbox command to
7109 * SLI4 HBA firmware.
7110 *
7111 **/
7112int
7113lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7114{
7115 struct lpfc_sli *psli = &phba->sli;
7116 LPFC_MBOXQ_t *mboxq;
7117 int rc = MBX_SUCCESS;
7118 unsigned long iflags;
7119 struct lpfc_mqe *mqe;
7120 uint32_t mbx_cmnd;
7121
7122 /* Check interrupt mode before post async mailbox command */
7123 if (unlikely(!phba->sli4_hba.intr_enable))
7124 return MBX_NOT_FINISHED;
7125
7126 /* Check for mailbox command service token */
7127 spin_lock_irqsave(&phba->hbalock, iflags);
7128 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7129 spin_unlock_irqrestore(&phba->hbalock, iflags);
7130 return MBX_NOT_FINISHED;
7131 }
7132 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7133 spin_unlock_irqrestore(&phba->hbalock, iflags);
7134 return MBX_NOT_FINISHED;
7135 }
7136 if (unlikely(phba->sli.mbox_active)) {
7137 spin_unlock_irqrestore(&phba->hbalock, iflags);
7138 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7139 "0384 There is pending active mailbox cmd\n");
7140 return MBX_NOT_FINISHED;
7141 }
7142 /* Take the mailbox command service token */
7143 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7144
7145 /* Get the next mailbox command from head of queue */
7146 mboxq = lpfc_mbox_get(phba);
7147
7148 /* If no more mailbox command waiting for post, we're done */
7149 if (!mboxq) {
7150 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7151 spin_unlock_irqrestore(&phba->hbalock, iflags);
7152 return MBX_SUCCESS;
7153 }
7154 phba->sli.mbox_active = mboxq;
7155 spin_unlock_irqrestore(&phba->hbalock, iflags);
7156
7157 /* Check device readiness for posting mailbox command */
7158 rc = lpfc_mbox_dev_check(phba);
7159 if (unlikely(rc))
7160 /* Driver clean routine will clean up pending mailbox */
7161 goto out_not_finished;
7162
7163 /* Prepare the mbox command to be posted */
7164 mqe = &mboxq->u.mqe;
7165 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7166
7167 /* Start timer for the mbox_tmo and log some mailbox post messages */
7168 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007169 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007170
7171 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007172 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007173 "x%x x%x\n",
7174 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007175 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7176 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007177 phba->pport->port_state, psli->sli_flag);
7178
7179 if (mbx_cmnd != MBX_HEARTBEAT) {
7180 if (mboxq->vport) {
7181 lpfc_debugfs_disc_trc(mboxq->vport,
7182 LPFC_DISC_TRC_MBOX_VPORT,
7183 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7184 mbx_cmnd, mqe->un.mb_words[0],
7185 mqe->un.mb_words[1]);
7186 } else {
7187 lpfc_debugfs_disc_trc(phba->pport,
7188 LPFC_DISC_TRC_MBOX,
7189 "MBOX Send: cmd:x%x mb:x%x x%x",
7190 mbx_cmnd, mqe->un.mb_words[0],
7191 mqe->un.mb_words[1]);
7192 }
7193 }
7194 psli->slistat.mbox_cmd++;
7195
7196 /* Post the mailbox command to the port */
7197 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7198 if (rc != MBX_SUCCESS) {
7199 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007200 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007201 "cannot issue Data: x%x x%x\n",
7202 mboxq->vport ? mboxq->vport->vpi : 0,
7203 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007204 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7205 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007206 psli->sli_flag, MBX_NOWAIT);
7207 goto out_not_finished;
7208 }
7209
7210 return rc;
7211
7212out_not_finished:
7213 spin_lock_irqsave(&phba->hbalock, iflags);
7214 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7215 __lpfc_mbox_cmpl_put(phba, mboxq);
7216 /* Release the token */
7217 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7218 phba->sli.mbox_active = NULL;
7219 spin_unlock_irqrestore(&phba->hbalock, iflags);
7220
7221 return MBX_NOT_FINISHED;
7222}
7223
7224/**
7225 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7226 * @phba: Pointer to HBA context object.
7227 * @pmbox: Pointer to mailbox object.
7228 * @flag: Flag indicating how the mailbox need to be processed.
7229 *
7230 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7231 * the API jump table function pointer from the lpfc_hba struct.
7232 *
7233 * Return codes the caller owns the mailbox command after the return of the
7234 * function.
7235 **/
7236int
7237lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7238{
7239 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7240}
7241
7242/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007243 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007244 * @phba: The hba struct for which this call is being executed.
7245 * @dev_grp: The HBA PCI-Device group number.
7246 *
7247 * This routine sets up the mbox interface API function jump table in @phba
7248 * struct.
7249 * Returns: 0 - success, -ENODEV - failure.
7250 **/
7251int
7252lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7253{
7254
7255 switch (dev_grp) {
7256 case LPFC_PCI_DEV_LP:
7257 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7258 phba->lpfc_sli_handle_slow_ring_event =
7259 lpfc_sli_handle_slow_ring_event_s3;
7260 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7261 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7262 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7263 break;
7264 case LPFC_PCI_DEV_OC:
7265 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7266 phba->lpfc_sli_handle_slow_ring_event =
7267 lpfc_sli_handle_slow_ring_event_s4;
7268 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7269 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7270 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7271 break;
7272 default:
7273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7274 "1420 Invalid HBA PCI-device group: 0x%x\n",
7275 dev_grp);
7276 return -ENODEV;
7277 break;
7278 }
7279 return 0;
7280}
7281
7282/**
James Smart3621a712009-04-06 18:47:14 -04007283 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007284 * @phba: Pointer to HBA context object.
7285 * @pring: Pointer to driver SLI ring object.
7286 * @piocb: Pointer to address of newly added command iocb.
7287 *
7288 * This function is called with hbalock held to add a command
7289 * iocb to the txq when SLI layer cannot submit the command iocb
7290 * to the ring.
7291 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007292void
James Smart92d7f7b2007-06-17 19:56:38 -05007293__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007294 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007295{
7296 /* Insert the caller's iocb in the txq tail for later processing. */
7297 list_add_tail(&piocb->list, &pring->txq);
7298 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007299}
7300
James Smarte59058c2008-08-24 21:49:00 -04007301/**
James Smart3621a712009-04-06 18:47:14 -04007302 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007303 * @phba: Pointer to HBA context object.
7304 * @pring: Pointer to driver SLI ring object.
7305 * @piocb: Pointer to address of newly added command iocb.
7306 *
7307 * This function is called with hbalock held before a new
7308 * iocb is submitted to the firmware. This function checks
7309 * txq to flush the iocbs in txq to Firmware before
7310 * submitting new iocbs to the Firmware.
7311 * If there are iocbs in the txq which need to be submitted
7312 * to firmware, lpfc_sli_next_iocb returns the first element
7313 * of the txq after dequeuing it from txq.
7314 * If there is no iocb in the txq then the function will return
7315 * *piocb and *piocb is set to NULL. Caller needs to check
7316 * *piocb to find if there are more commands in the txq.
7317 **/
dea31012005-04-17 16:05:31 -05007318static struct lpfc_iocbq *
7319lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007320 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007321{
7322 struct lpfc_iocbq * nextiocb;
7323
7324 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7325 if (!nextiocb) {
7326 nextiocb = *piocb;
7327 *piocb = NULL;
7328 }
7329
7330 return nextiocb;
7331}
7332
James Smarte59058c2008-08-24 21:49:00 -04007333/**
James Smart3772a992009-05-22 14:50:54 -04007334 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007335 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007336 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007337 * @piocb: Pointer to command iocb.
7338 * @flag: Flag indicating if this command can be put into txq.
7339 *
James Smart3772a992009-05-22 14:50:54 -04007340 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7341 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7342 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7343 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7344 * this function allows only iocbs for posting buffers. This function finds
7345 * next available slot in the command ring and posts the command to the
7346 * available slot and writes the port attention register to request HBA start
7347 * processing new iocb. If there is no slot available in the ring and
7348 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7349 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007350 *
James Smart3772a992009-05-22 14:50:54 -04007351 * This function is called with hbalock held. The function will return success
7352 * after it successfully submit the iocb to firmware or after adding to the
7353 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007354 **/
James Smart98c9ea52007-10-27 13:37:33 -04007355static int
James Smart3772a992009-05-22 14:50:54 -04007356__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007357 struct lpfc_iocbq *piocb, uint32_t flag)
7358{
7359 struct lpfc_iocbq *nextiocb;
7360 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007361 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007362
James Smart92d7f7b2007-06-17 19:56:38 -05007363 if (piocb->iocb_cmpl && (!piocb->vport) &&
7364 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7365 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7366 lpfc_printf_log(phba, KERN_ERR,
7367 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007368 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007369 piocb->iocb.ulpCommand);
7370 dump_stack();
7371 return IOCB_ERROR;
7372 }
7373
7374
Linas Vepstas8d63f372007-02-14 14:28:36 -06007375 /* If the PCI channel is in offline state, do not post iocbs. */
7376 if (unlikely(pci_channel_offline(phba->pcidev)))
7377 return IOCB_ERROR;
7378
James Smarta257bf92009-04-06 18:48:10 -04007379 /* If HBA has a deferred error attention, fail the iocb. */
7380 if (unlikely(phba->hba_flag & DEFER_ERATT))
7381 return IOCB_ERROR;
7382
dea31012005-04-17 16:05:31 -05007383 /*
7384 * We should never get an IOCB if we are in a < LINK_DOWN state
7385 */
James Smart2e0fef82007-06-17 19:56:36 -05007386 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007387 return IOCB_ERROR;
7388
7389 /*
7390 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007391 * outstanding event.
dea31012005-04-17 16:05:31 -05007392 */
James Smart0b727fe2007-10-27 13:37:25 -04007393 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007394 goto iocb_busy;
7395
James Smart2e0fef82007-06-17 19:56:36 -05007396 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007397 /*
James Smart2680eea2007-04-25 09:52:55 -04007398 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007399 * can be issued if the link is not up.
7400 */
7401 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007402 case CMD_GEN_REQUEST64_CR:
7403 case CMD_GEN_REQUEST64_CX:
7404 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7405 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007406 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007407 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7408 MENLO_TRANSPORT_TYPE))
7409
7410 goto iocb_busy;
7411 break;
dea31012005-04-17 16:05:31 -05007412 case CMD_QUE_RING_BUF_CN:
7413 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007414 /*
7415 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7416 * completion, iocb_cmpl MUST be 0.
7417 */
7418 if (piocb->iocb_cmpl)
7419 piocb->iocb_cmpl = NULL;
7420 /*FALLTHROUGH*/
7421 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007422 case CMD_CLOSE_XRI_CN:
7423 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007424 break;
7425 default:
7426 goto iocb_busy;
7427 }
7428
7429 /*
7430 * For FCP commands, we must be in a state where we can process link
7431 * attention events.
7432 */
7433 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007434 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007435 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007436 }
dea31012005-04-17 16:05:31 -05007437
dea31012005-04-17 16:05:31 -05007438 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7439 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7440 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7441
7442 if (iocb)
7443 lpfc_sli_update_ring(phba, pring);
7444 else
7445 lpfc_sli_update_full_ring(phba, pring);
7446
7447 if (!piocb)
7448 return IOCB_SUCCESS;
7449
7450 goto out_busy;
7451
7452 iocb_busy:
7453 pring->stats.iocb_cmd_delay++;
7454
7455 out_busy:
7456
7457 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007458 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007459 return IOCB_SUCCESS;
7460 }
7461
7462 return IOCB_BUSY;
7463}
7464
James Smart3772a992009-05-22 14:50:54 -04007465/**
James Smart4f774512009-05-22 14:52:35 -04007466 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7467 * @phba: Pointer to HBA context object.
7468 * @piocb: Pointer to command iocb.
7469 * @sglq: Pointer to the scatter gather queue object.
7470 *
7471 * This routine converts the bpl or bde that is in the IOCB
7472 * to a sgl list for the sli4 hardware. The physical address
7473 * of the bpl/bde is converted back to a virtual address.
7474 * If the IOCB contains a BPL then the list of BDE's is
7475 * converted to sli4_sge's. If the IOCB contains a single
7476 * BDE then it is converted to a single sli_sge.
7477 * The IOCB is still in cpu endianess so the contents of
7478 * the bpl can be used without byte swapping.
7479 *
7480 * Returns valid XRI = Success, NO_XRI = Failure.
7481**/
7482static uint16_t
7483lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7484 struct lpfc_sglq *sglq)
7485{
7486 uint16_t xritag = NO_XRI;
7487 struct ulp_bde64 *bpl = NULL;
7488 struct ulp_bde64 bde;
7489 struct sli4_sge *sgl = NULL;
7490 IOCB_t *icmd;
7491 int numBdes = 0;
7492 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007493 uint32_t offset = 0; /* accumulated offset in the sg request list */
7494 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007495
7496 if (!piocbq || !sglq)
7497 return xritag;
7498
7499 sgl = (struct sli4_sge *)sglq->sgl;
7500 icmd = &piocbq->iocb;
7501 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7502 numBdes = icmd->un.genreq64.bdl.bdeSize /
7503 sizeof(struct ulp_bde64);
7504 /* The addrHigh and addrLow fields within the IOCB
7505 * have not been byteswapped yet so there is no
7506 * need to swap them back.
7507 */
7508 bpl = (struct ulp_bde64 *)
7509 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7510
7511 if (!bpl)
7512 return xritag;
7513
7514 for (i = 0; i < numBdes; i++) {
7515 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007516 sgl->addr_hi = bpl->addrHigh;
7517 sgl->addr_lo = bpl->addrLow;
7518
James Smart05580562011-05-24 11:40:48 -04007519 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007520 if ((i+1) == numBdes)
7521 bf_set(lpfc_sli4_sge_last, sgl, 1);
7522 else
7523 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007524 /* swap the size field back to the cpu so we
7525 * can assign it to the sgl.
7526 */
7527 bde.tus.w = le32_to_cpu(bpl->tus.w);
7528 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007529 /* The offsets in the sgl need to be accumulated
7530 * separately for the request and reply lists.
7531 * The request is always first, the reply follows.
7532 */
7533 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7534 /* add up the reply sg entries */
7535 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7536 inbound++;
7537 /* first inbound? reset the offset */
7538 if (inbound == 1)
7539 offset = 0;
7540 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007541 bf_set(lpfc_sli4_sge_type, sgl,
7542 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007543 offset += bde.tus.f.bdeSize;
7544 }
James Smart546fc852011-03-11 16:06:29 -05007545 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007546 bpl++;
7547 sgl++;
7548 }
7549 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7550 /* The addrHigh and addrLow fields of the BDE have not
7551 * been byteswapped yet so they need to be swapped
7552 * before putting them in the sgl.
7553 */
7554 sgl->addr_hi =
7555 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7556 sgl->addr_lo =
7557 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007558 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007559 bf_set(lpfc_sli4_sge_last, sgl, 1);
7560 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007561 sgl->sge_len =
7562 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007563 }
7564 return sglq->sli4_xritag;
7565}
7566
7567/**
7568 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7569 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007570 *
James Smarta93ff372010-10-22 11:06:08 -04007571 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007572 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7573 * held.
James Smart4f774512009-05-22 14:52:35 -04007574 *
7575 * Return: index into SLI4 fast-path FCP queue index.
7576 **/
7577static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007578lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007579{
James Smart8fa38512009-07-19 10:01:03 -04007580 ++phba->fcp_qidx;
7581 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7582 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007583
James Smart8fa38512009-07-19 10:01:03 -04007584 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007585}
7586
7587/**
7588 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7589 * @phba: Pointer to HBA context object.
7590 * @piocb: Pointer to command iocb.
7591 * @wqe: Pointer to the work queue entry.
7592 *
7593 * This routine converts the iocb command to its Work Queue Entry
7594 * equivalent. The wqe pointer should not have any fields set when
7595 * this routine is called because it will memcpy over them.
7596 * This routine does not set the CQ_ID or the WQEC bits in the
7597 * wqe.
7598 *
7599 * Returns: 0 = Success, IOCB_ERROR = Failure.
7600 **/
7601static int
7602lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7603 union lpfc_wqe *wqe)
7604{
James Smart5ffc2662009-11-18 15:39:44 -05007605 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007606 uint8_t ct = 0;
7607 uint32_t fip;
7608 uint32_t abort_tag;
7609 uint8_t command_type = ELS_COMMAND_NON_FIP;
7610 uint8_t cmnd;
7611 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007612 uint16_t abrt_iotag;
7613 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007614 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007615 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007616 int numBdes, i;
7617 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007618 struct lpfc_nodelist *ndlp;
James Smart4f774512009-05-22 14:52:35 -04007619
James Smart45ed1192009-10-02 15:17:02 -04007620 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007621 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007622 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007623 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007624 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007625 command_type = ELS_COMMAND_FIP;
7626 else
7627 command_type = ELS_COMMAND_NON_FIP;
7628
James Smart4f774512009-05-22 14:52:35 -04007629 /* Some of the fields are in the right position already */
7630 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7631 abort_tag = (uint32_t) iocbq->iotag;
7632 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007633 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007634 /* words0-2 bpl convert bde */
7635 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007636 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7637 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007638 bpl = (struct ulp_bde64 *)
7639 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7640 if (!bpl)
7641 return IOCB_ERROR;
7642
7643 /* Should already be byte swapped. */
7644 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7645 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7646 /* swap the size field back to the cpu so we
7647 * can assign it to the sgl.
7648 */
7649 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007650 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7651 total_len = 0;
7652 for (i = 0; i < numBdes; i++) {
7653 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7654 total_len += bde.tus.f.bdeSize;
7655 }
James Smart4f774512009-05-22 14:52:35 -04007656 } else
James Smart5ffc2662009-11-18 15:39:44 -05007657 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007658
7659 iocbq->iocb.ulpIoTag = iocbq->iotag;
7660 cmnd = iocbq->iocb.ulpCommand;
7661
7662 switch (iocbq->iocb.ulpCommand) {
7663 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007664 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007665 if (!iocbq->iocb.ulpLe) {
7666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7667 "2007 Only Limited Edition cmd Format"
7668 " supported 0x%x\n",
7669 iocbq->iocb.ulpCommand);
7670 return IOCB_ERROR;
7671 }
James Smart5ffc2662009-11-18 15:39:44 -05007672 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007673 /* Els_reguest64 has a TMO */
7674 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7675 iocbq->iocb.ulpTimeout);
7676 /* Need a VF for word 4 set the vf bit*/
7677 bf_set(els_req64_vf, &wqe->els_req, 0);
7678 /* And a VFID for word 12 */
7679 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007680 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007681 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7682 iocbq->iocb.ulpContext);
7683 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7684 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007685 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05007686 if (command_type == ELS_COMMAND_FIP) {
7687 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7688 >> LPFC_FIP_ELS_ID_SHIFT);
7689 }
James Smart6d368e52011-05-24 11:44:12 -04007690 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7691 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007692 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7693 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7694 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7695 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7696 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7697 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007698 break;
James Smart5ffc2662009-11-18 15:39:44 -05007699 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007700 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7701 iocbq->iocb.un.ulpWord[3]);
7702 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007703 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007704 /* The entire sequence is transmitted for this IOCB */
7705 xmit_len = total_len;
7706 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007707 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007708 /* word3 iocb=io_tag32 wqe=reserved */
7709 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007710 /* word4 relative_offset memcpy */
7711 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007712 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7713 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7714 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7715 LPFC_WQE_IOD_WRITE);
7716 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7717 LPFC_WQE_LENLOC_WORD12);
7718 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007719 wqe->xmit_sequence.xmit_len = xmit_len;
7720 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007721 break;
James Smart4f774512009-05-22 14:52:35 -04007722 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007723 /* word3 iocb=iotag32 wqe=seq_payload_len */
7724 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007725 /* word4 iocb=rsvd wqe=rsvd */
7726 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7727 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007728 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007729 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007730 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7731 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7732 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7733 LPFC_WQE_LENLOC_WORD3);
7734 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007735 break;
James Smart4f774512009-05-22 14:52:35 -04007736 case CMD_FCP_IWRITE64_CR:
7737 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007738 /* word3 iocb=iotag wqe=payload_offset_len */
7739 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7740 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007741 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007742 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7743 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7744 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7745 iocbq->iocb.ulpFCP2Rcvy);
7746 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7747 /* Always open the exchange */
7748 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7749 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7750 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7751 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7752 LPFC_WQE_LENLOC_WORD4);
7753 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7754 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007755 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007756 case CMD_FCP_IREAD64_CR:
7757 /* word3 iocb=iotag wqe=payload_offset_len */
7758 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7759 wqe->fcp_iread.payload_offset_len =
7760 xmit_len + sizeof(struct fcp_rsp);
7761 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7762 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7763 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7764 iocbq->iocb.ulpFCP2Rcvy);
7765 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007766 /* Always open the exchange */
7767 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007768 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7769 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7770 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7771 LPFC_WQE_LENLOC_WORD4);
7772 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7773 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007774 break;
James Smartf1126682009-06-10 17:22:44 -04007775 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007776 /* word3 iocb=IO_TAG wqe=reserved */
7777 wqe->fcp_icmd.rsrvd3 = 0;
7778 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007779 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007780 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7781 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7782 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7783 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7784 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7785 LPFC_WQE_LENLOC_NONE);
7786 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007787 break;
James Smart4f774512009-05-22 14:52:35 -04007788 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007789 /* For this command calculate the xmit length of the
7790 * request bde.
7791 */
7792 xmit_len = 0;
7793 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7794 sizeof(struct ulp_bde64);
7795 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007796 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007797 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7798 break;
James Smart63e801c2010-11-20 23:14:19 -05007799 xmit_len += bde.tus.f.bdeSize;
7800 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007801 /* word3 iocb=IO_TAG wqe=request_payload_len */
7802 wqe->gen_req.request_payload_len = xmit_len;
7803 /* word4 iocb=parameter wqe=relative_offset memcpy */
7804 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007805 /* word6 context tag copied in memcpy */
7806 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7807 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7809 "2015 Invalid CT %x command 0x%x\n",
7810 ct, iocbq->iocb.ulpCommand);
7811 return IOCB_ERROR;
7812 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007813 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7814 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7815 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7816 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7817 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7818 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7819 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7820 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007821 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007822 break;
James Smart4f774512009-05-22 14:52:35 -04007823 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007824 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007825 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007826 /* word3 iocb=iotag32 wqe=response_payload_len */
7827 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007828 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007829 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007830 /* word5 iocb=rsvd wge=did */
7831 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7832 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007833 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7834 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7835 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7836 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007837 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007838 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007839 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007840 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007841 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7842 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7843 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7844 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7845 LPFC_WQE_LENLOC_WORD3);
7846 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007847 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7848 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smart4f774512009-05-22 14:52:35 -04007849 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007850 break;
James Smart4f774512009-05-22 14:52:35 -04007851 case CMD_CLOSE_XRI_CN:
7852 case CMD_ABORT_XRI_CN:
7853 case CMD_ABORT_XRI_CX:
7854 /* words 0-2 memcpy should be 0 rserved */
7855 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007856 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7857 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7858 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7859 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7860 } else
7861 fip = 0;
7862
7863 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007864 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007865 * The link is down, or the command was ELS_FIP
7866 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007867 * on the wire.
7868 */
7869 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7870 else
7871 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7872 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007873 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7874 wqe->abort_cmd.rsrvd5 = 0;
7875 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007876 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7877 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007878 /*
7879 * The abort handler will send us CMD_ABORT_XRI_CN or
7880 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7881 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007882 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7883 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7884 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7885 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007886 cmnd = CMD_ABORT_XRI_CX;
7887 command_type = OTHER_COMMAND;
7888 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007889 break;
James Smart6669f9b2009-10-02 15:16:45 -04007890 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007891 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007892 * we re-construct this WQE here based on information in
7893 * iocbq from scratch.
7894 */
7895 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007896 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007897 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007898 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7899 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007900 LPFC_ABTS_UNSOL_INT) {
7901 /* ABTS sent by initiator to CT exchange, the
7902 * RX_ID field will be filled with the newly
7903 * allocated responder XRI.
7904 */
7905 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7906 iocbq->sli4_xritag);
7907 } else {
7908 /* ABTS sent by responder to CT exchange, the
7909 * RX_ID field will be filled with the responder
7910 * RX_ID from ABTS.
7911 */
7912 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007913 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007914 }
James Smart6669f9b2009-10-02 15:16:45 -04007915 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7916 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7917 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7918 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04007919 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7920 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7921 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04007922 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7923 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05007924 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7925 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7926 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7927 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7928 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7929 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7930 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7931 }
7932
James Smart7851fe22011-07-22 18:36:52 -04007933 break;
James Smart4f774512009-05-22 14:52:35 -04007934 case CMD_XRI_ABORTED_CX:
7935 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04007936 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7937 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7938 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7939 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7940 default:
7941 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7942 "2014 Invalid command 0x%x\n",
7943 iocbq->iocb.ulpCommand);
7944 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04007945 break;
James Smart4f774512009-05-22 14:52:35 -04007946 }
James Smart6d368e52011-05-24 11:44:12 -04007947
James Smartf0d9bcc2010-10-22 11:07:09 -04007948 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7949 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7950 wqe->generic.wqe_com.abort_tag = abort_tag;
7951 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7952 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7953 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7954 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04007955 return 0;
7956}
7957
7958/**
7959 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7960 * @phba: Pointer to HBA context object.
7961 * @ring_number: SLI ring number to issue iocb on.
7962 * @piocb: Pointer to command iocb.
7963 * @flag: Flag indicating if this command can be put into txq.
7964 *
7965 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7966 * an iocb command to an HBA with SLI-4 interface spec.
7967 *
7968 * This function is called with hbalock held. The function will return success
7969 * after it successfully submit the iocb to firmware or after adding to the
7970 * txq.
7971 **/
7972static int
7973__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7974 struct lpfc_iocbq *piocb, uint32_t flag)
7975{
7976 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04007977 union lpfc_wqe wqe;
7978 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04007979
7980 if (piocb->sli4_xritag == NO_XRI) {
7981 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05007982 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7983 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04007984 sglq = NULL;
7985 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04007986 if (pring->txq_cnt) {
7987 if (!(flag & SLI_IOCB_RET_IOCB)) {
7988 __lpfc_sli_ringtx_put(phba,
7989 pring, piocb);
7990 return IOCB_SUCCESS;
7991 } else {
7992 return IOCB_BUSY;
7993 }
7994 } else {
James Smart6d368e52011-05-24 11:44:12 -04007995 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04007996 if (!sglq) {
7997 if (!(flag & SLI_IOCB_RET_IOCB)) {
7998 __lpfc_sli_ringtx_put(phba,
7999 pring,
8000 piocb);
8001 return IOCB_SUCCESS;
8002 } else
8003 return IOCB_BUSY;
8004 }
8005 }
James Smart4f774512009-05-22 14:52:35 -04008006 }
8007 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008008 /* These IO's already have an XRI and a mapped sgl. */
8009 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008010 } else {
James Smart6d368e52011-05-24 11:44:12 -04008011 /*
8012 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008013 * sglq is on the active list
8014 */
8015 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8016 if (!sglq)
8017 return IOCB_ERROR;
8018 }
8019
8020 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008021 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008022 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008023 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008024 return IOCB_ERROR;
8025 }
8026
8027 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8028 return IOCB_ERROR;
8029
James Smart341af102010-01-26 23:07:37 -05008030 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8031 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008032 /*
8033 * For FCP command IOCB, get a new WQ index to distribute
8034 * WQE across the WQsr. On the other hand, for abort IOCB,
8035 * it carries the same WQ index to the original command
8036 * IOCB.
8037 */
James Smart341af102010-01-26 23:07:37 -05008038 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008039 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8040 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8041 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008042 return IOCB_ERROR;
8043 } else {
8044 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8045 return IOCB_ERROR;
8046 }
8047 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8048
8049 return 0;
8050}
8051
8052/**
James Smart3772a992009-05-22 14:50:54 -04008053 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8054 *
8055 * This routine wraps the actual lockless version for issusing IOCB function
8056 * pointer from the lpfc_hba struct.
8057 *
8058 * Return codes:
8059 * IOCB_ERROR - Error
8060 * IOCB_SUCCESS - Success
8061 * IOCB_BUSY - Busy
8062 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008063int
James Smart3772a992009-05-22 14:50:54 -04008064__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8065 struct lpfc_iocbq *piocb, uint32_t flag)
8066{
8067 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8068}
8069
8070/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008071 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008072 * @phba: The hba struct for which this call is being executed.
8073 * @dev_grp: The HBA PCI-Device group number.
8074 *
8075 * This routine sets up the SLI interface API function jump table in @phba
8076 * struct.
8077 * Returns: 0 - success, -ENODEV - failure.
8078 **/
8079int
8080lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8081{
8082
8083 switch (dev_grp) {
8084 case LPFC_PCI_DEV_LP:
8085 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8086 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8087 break;
James Smart4f774512009-05-22 14:52:35 -04008088 case LPFC_PCI_DEV_OC:
8089 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8090 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8091 break;
James Smart3772a992009-05-22 14:50:54 -04008092 default:
8093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8094 "1419 Invalid HBA PCI-device group: 0x%x\n",
8095 dev_grp);
8096 return -ENODEV;
8097 break;
8098 }
8099 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8100 return 0;
8101}
James Smart92d7f7b2007-06-17 19:56:38 -05008102
James Smarte59058c2008-08-24 21:49:00 -04008103/**
James Smart3621a712009-04-06 18:47:14 -04008104 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008105 * @phba: Pointer to HBA context object.
8106 * @pring: Pointer to driver SLI ring object.
8107 * @piocb: Pointer to command iocb.
8108 * @flag: Flag indicating if this command can be put into txq.
8109 *
8110 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8111 * function. This function gets the hbalock and calls
8112 * __lpfc_sli_issue_iocb function and will return the error returned
8113 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8114 * functions which do not hold hbalock.
8115 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008116int
James Smart3772a992009-05-22 14:50:54 -04008117lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008118 struct lpfc_iocbq *piocb, uint32_t flag)
8119{
8120 unsigned long iflags;
8121 int rc;
8122
8123 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008124 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008125 spin_unlock_irqrestore(&phba->hbalock, iflags);
8126
8127 return rc;
8128}
8129
James Smarte59058c2008-08-24 21:49:00 -04008130/**
James Smart3621a712009-04-06 18:47:14 -04008131 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008132 * @phba: Pointer to HBA context object.
8133 *
8134 * This function is called while driver attaches with the
8135 * HBA to setup the extra ring. The extra ring is used
8136 * only when driver needs to support target mode functionality
8137 * or IP over FC functionalities.
8138 *
8139 * This function is called with no lock held.
8140 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008141static int
8142lpfc_extra_ring_setup( struct lpfc_hba *phba)
8143{
8144 struct lpfc_sli *psli;
8145 struct lpfc_sli_ring *pring;
8146
8147 psli = &phba->sli;
8148
8149 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008150
8151 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008152 pring = &psli->ring[psli->fcp_ring];
8153 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8154 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8155 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8156 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8157
James Smarta4bc3372006-12-02 13:34:16 -05008158 /* and give them to the extra ring */
8159 pring = &psli->ring[psli->extra_ring];
8160
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008161 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8162 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8163 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8164 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8165
8166 /* Setup default profile for this ring */
8167 pring->iotag_max = 4096;
8168 pring->num_mask = 1;
8169 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008170 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8171 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008172 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8173 return 0;
8174}
8175
James Smarte59058c2008-08-24 21:49:00 -04008176/**
James Smart3621a712009-04-06 18:47:14 -04008177 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008178 * @phba: Pointer to HBA context object.
8179 * @pring: Pointer to driver SLI ring object.
8180 * @iocbq: Pointer to iocb object.
8181 *
8182 * This function is called by the slow ring event handler
8183 * function when there is an ASYNC event iocb in the ring.
8184 * This function is called with no lock held.
8185 * Currently this function handles only temperature related
8186 * ASYNC events. The function decodes the temperature sensor
8187 * event message and posts events for the management applications.
8188 **/
James Smart98c9ea52007-10-27 13:37:33 -04008189static void
James Smart57127f12007-10-27 13:37:05 -04008190lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8191 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8192{
8193 IOCB_t *icmd;
8194 uint16_t evt_code;
8195 uint16_t temp;
8196 struct temp_event temp_event_data;
8197 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008198 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008199
8200 icmd = &iocbq->iocb;
8201 evt_code = icmd->un.asyncstat.evt_code;
8202 temp = icmd->ulpContext;
8203
8204 if ((evt_code != ASYNC_TEMP_WARN) &&
8205 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04008206 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04008207 lpfc_printf_log(phba,
8208 KERN_ERR,
8209 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008210 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008211 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008212 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8213 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8214 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8215 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04008216 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04008217 icmd->un.asyncstat.evt_code,
8218 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8219 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8220 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8221 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8222
James Smart57127f12007-10-27 13:37:05 -04008223 return;
8224 }
8225 temp_event_data.data = (uint32_t)temp;
8226 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8227 if (evt_code == ASYNC_TEMP_WARN) {
8228 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8229 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008230 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008231 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04008232 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04008233 "corrective action. temperature : %d Celsius\n",
8234 temp);
8235 }
8236 if (evt_code == ASYNC_TEMP_SAFE) {
8237 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8238 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008239 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008240 LOG_TEMP,
8241 "0340 Adapter temperature is OK now. "
8242 "temperature : %d Celsius\n",
8243 temp);
8244 }
8245
8246 /* Send temperature change event to applications */
8247 shost = lpfc_shost_from_vport(phba->pport);
8248 fc_host_post_vendor_event(shost, fc_get_event_number(),
8249 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05008250 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04008251
8252}
8253
8254
James Smarte59058c2008-08-24 21:49:00 -04008255/**
James Smart3621a712009-04-06 18:47:14 -04008256 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008257 * @phba: Pointer to HBA context object.
8258 *
8259 * lpfc_sli_setup sets up rings of the SLI interface with
8260 * number of iocbs per ring and iotags. This function is
8261 * called while driver attach to the HBA and before the
8262 * interrupts are enabled. So there is no need for locking.
8263 *
8264 * This function always returns 0.
8265 **/
dea31012005-04-17 16:05:31 -05008266int
8267lpfc_sli_setup(struct lpfc_hba *phba)
8268{
James Smarted957682007-06-17 19:56:37 -05008269 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008270 struct lpfc_sli *psli = &phba->sli;
8271 struct lpfc_sli_ring *pring;
8272
8273 psli->num_rings = MAX_CONFIGURED_RINGS;
8274 psli->sli_flag = 0;
8275 psli->fcp_ring = LPFC_FCP_RING;
8276 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008277 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008278
James Bottomley604a3e32005-10-29 10:28:33 -05008279 psli->iocbq_lookup = NULL;
8280 psli->iocbq_lookup_len = 0;
8281 psli->last_iotag = 0;
8282
dea31012005-04-17 16:05:31 -05008283 for (i = 0; i < psli->num_rings; i++) {
8284 pring = &psli->ring[i];
8285 switch (i) {
8286 case LPFC_FCP_RING: /* ring 0 - FCP */
8287 /* numCiocb and numRiocb are used in config_port */
8288 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8289 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8290 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8291 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8292 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8293 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008294 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008295 SLI3_IOCB_CMD_SIZE :
8296 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008297 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008298 SLI3_IOCB_RSP_SIZE :
8299 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008300 pring->iotag_ctr = 0;
8301 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008302 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008303 pring->fast_iotag = pring->iotag_max;
8304 pring->num_mask = 0;
8305 break;
James Smarta4bc3372006-12-02 13:34:16 -05008306 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008307 /* numCiocb and numRiocb are used in config_port */
8308 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8309 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008310 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008311 SLI3_IOCB_CMD_SIZE :
8312 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008313 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008314 SLI3_IOCB_RSP_SIZE :
8315 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008316 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008317 pring->num_mask = 0;
8318 break;
8319 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8320 /* numCiocb and numRiocb are used in config_port */
8321 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8322 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008323 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008324 SLI3_IOCB_CMD_SIZE :
8325 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008326 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008327 SLI3_IOCB_RSP_SIZE :
8328 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008329 pring->fast_iotag = 0;
8330 pring->iotag_ctr = 0;
8331 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008332 pring->lpfc_sli_rcv_async_status =
8333 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008334 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008335 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008336 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8337 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008338 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008339 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008340 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008341 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8342 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008343 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008344 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008345 pring->prt[2].profile = 0; /* Mask 2 */
8346 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008347 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008348 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008349 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008350 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008351 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008352 pring->prt[3].profile = 0; /* Mask 3 */
8353 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008354 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008355 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008356 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008357 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008358 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008359 /* abort unsolicited sequence */
8360 pring->prt[4].profile = 0; /* Mask 4 */
8361 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8362 pring->prt[4].type = FC_TYPE_BLS;
8363 pring->prt[4].lpfc_sli_rcv_unsol_event =
8364 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008365 break;
8366 }
James Smarted957682007-06-17 19:56:37 -05008367 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008368 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008369 }
James Smarted957682007-06-17 19:56:37 -05008370 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008371 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008372 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8373 "SLI2 SLIM Data: x%x x%lx\n",
8374 phba->brd_no, totiocbsize,
8375 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008376 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008377 if (phba->cfg_multi_ring_support == 2)
8378 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008379
8380 return 0;
8381}
8382
James Smarte59058c2008-08-24 21:49:00 -04008383/**
James Smart3621a712009-04-06 18:47:14 -04008384 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008385 * @phba: Pointer to HBA context object.
8386 *
8387 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8388 * ring. This function also initializes ring indices of each ring.
8389 * This function is called during the initialization of the SLI
8390 * interface of an HBA.
8391 * This function is called with no lock held and always returns
8392 * 1.
8393 **/
dea31012005-04-17 16:05:31 -05008394int
James Smart2e0fef82007-06-17 19:56:36 -05008395lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008396{
8397 struct lpfc_sli *psli;
8398 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008399 int i;
dea31012005-04-17 16:05:31 -05008400
8401 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008402 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008403 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008404 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008405 /* Initialize list headers for txq and txcmplq as double linked lists */
8406 for (i = 0; i < psli->num_rings; i++) {
8407 pring = &psli->ring[i];
8408 pring->ringno = i;
8409 pring->next_cmdidx = 0;
8410 pring->local_getidx = 0;
8411 pring->cmdidx = 0;
8412 INIT_LIST_HEAD(&pring->txq);
8413 INIT_LIST_HEAD(&pring->txcmplq);
8414 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008415 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008416 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008417 }
James Smart2e0fef82007-06-17 19:56:36 -05008418 spin_unlock_irq(&phba->hbalock);
8419 return 1;
dea31012005-04-17 16:05:31 -05008420}
8421
James Smarte59058c2008-08-24 21:49:00 -04008422/**
James Smart04c68492009-05-22 14:52:52 -04008423 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8424 * @phba: Pointer to HBA context object.
8425 *
8426 * This routine flushes the mailbox command subsystem. It will unconditionally
8427 * flush all the mailbox commands in the three possible stages in the mailbox
8428 * command sub-system: pending mailbox command queue; the outstanding mailbox
8429 * command; and completed mailbox command queue. It is caller's responsibility
8430 * to make sure that the driver is in the proper state to flush the mailbox
8431 * command sub-system. Namely, the posting of mailbox commands into the
8432 * pending mailbox command queue from the various clients must be stopped;
8433 * either the HBA is in a state that it will never works on the outstanding
8434 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8435 * mailbox command has been completed.
8436 **/
8437static void
8438lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8439{
8440 LIST_HEAD(completions);
8441 struct lpfc_sli *psli = &phba->sli;
8442 LPFC_MBOXQ_t *pmb;
8443 unsigned long iflag;
8444
8445 /* Flush all the mailbox commands in the mbox system */
8446 spin_lock_irqsave(&phba->hbalock, iflag);
8447 /* The pending mailbox command queue */
8448 list_splice_init(&phba->sli.mboxq, &completions);
8449 /* The outstanding active mailbox command */
8450 if (psli->mbox_active) {
8451 list_add_tail(&psli->mbox_active->list, &completions);
8452 psli->mbox_active = NULL;
8453 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8454 }
8455 /* The completed mailbox command queue */
8456 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8457 spin_unlock_irqrestore(&phba->hbalock, iflag);
8458
8459 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8460 while (!list_empty(&completions)) {
8461 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8462 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8463 if (pmb->mbox_cmpl)
8464 pmb->mbox_cmpl(phba, pmb);
8465 }
8466}
8467
8468/**
James Smart3621a712009-04-06 18:47:14 -04008469 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008470 * @vport: Pointer to virtual port object.
8471 *
8472 * lpfc_sli_host_down is called to clean up the resources
8473 * associated with a vport before destroying virtual
8474 * port data structures.
8475 * This function does following operations:
8476 * - Free discovery resources associated with this virtual
8477 * port.
8478 * - Free iocbs associated with this virtual port in
8479 * the txq.
8480 * - Send abort for all iocb commands associated with this
8481 * vport in txcmplq.
8482 *
8483 * This function is called with no lock held and always returns 1.
8484 **/
dea31012005-04-17 16:05:31 -05008485int
James Smart92d7f7b2007-06-17 19:56:38 -05008486lpfc_sli_host_down(struct lpfc_vport *vport)
8487{
James Smart858c9f62007-06-17 19:56:39 -05008488 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008489 struct lpfc_hba *phba = vport->phba;
8490 struct lpfc_sli *psli = &phba->sli;
8491 struct lpfc_sli_ring *pring;
8492 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008493 int i;
8494 unsigned long flags = 0;
8495 uint16_t prev_pring_flag;
8496
8497 lpfc_cleanup_discovery_resources(vport);
8498
8499 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008500 for (i = 0; i < psli->num_rings; i++) {
8501 pring = &psli->ring[i];
8502 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008503 /* Only slow rings */
8504 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008505 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008506 /* Set the lpfc data pending flag */
8507 set_bit(LPFC_DATA_READY, &phba->data_flags);
8508 }
James Smart92d7f7b2007-06-17 19:56:38 -05008509 /*
8510 * Error everything on the txq since these iocbs have not been
8511 * given to the FW yet.
8512 */
James Smart92d7f7b2007-06-17 19:56:38 -05008513 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8514 if (iocb->vport != vport)
8515 continue;
James Smart858c9f62007-06-17 19:56:39 -05008516 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008517 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008518 }
8519
8520 /* Next issue ABTS for everything on the txcmplq */
8521 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8522 list) {
8523 if (iocb->vport != vport)
8524 continue;
8525 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8526 }
8527
8528 pring->flag = prev_pring_flag;
8529 }
8530
8531 spin_unlock_irqrestore(&phba->hbalock, flags);
8532
James Smarta257bf92009-04-06 18:48:10 -04008533 /* Cancel all the IOCBs from the completions list */
8534 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8535 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008536 return 1;
8537}
8538
James Smarte59058c2008-08-24 21:49:00 -04008539/**
James Smart3621a712009-04-06 18:47:14 -04008540 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008541 * @phba: Pointer to HBA context object.
8542 *
8543 * This function cleans up all iocb, buffers, mailbox commands
8544 * while shutting down the HBA. This function is called with no
8545 * lock held and always returns 1.
8546 * This function does the following to cleanup driver resources:
8547 * - Free discovery resources for each virtual port
8548 * - Cleanup any pending fabric iocbs
8549 * - Iterate through the iocb txq and free each entry
8550 * in the list.
8551 * - Free up any buffer posted to the HBA
8552 * - Free mailbox commands in the mailbox queue.
8553 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008554int
James Smart2e0fef82007-06-17 19:56:36 -05008555lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008556{
James Smart2534ba72007-04-25 09:52:20 -04008557 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008558 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008559 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008560 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008561 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008562 int i;
8563
8564 /* Shutdown the mailbox command sub-system */
8565 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008566
dea31012005-04-17 16:05:31 -05008567 lpfc_hba_down_prep(phba);
8568
James Smart92d7f7b2007-06-17 19:56:38 -05008569 lpfc_fabric_abort_hba(phba);
8570
James Smart2e0fef82007-06-17 19:56:36 -05008571 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008572 for (i = 0; i < psli->num_rings; i++) {
8573 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008574 /* Only slow rings */
8575 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008576 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008577 /* Set the lpfc data pending flag */
8578 set_bit(LPFC_DATA_READY, &phba->data_flags);
8579 }
dea31012005-04-17 16:05:31 -05008580
8581 /*
8582 * Error everything on the txq since these iocbs have not been
8583 * given to the FW yet.
8584 */
James Smart2534ba72007-04-25 09:52:20 -04008585 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008586 pring->txq_cnt = 0;
8587
dea31012005-04-17 16:05:31 -05008588 }
James Smart2e0fef82007-06-17 19:56:36 -05008589 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008590
James Smarta257bf92009-04-06 18:48:10 -04008591 /* Cancel all the IOCBs from the completions list */
8592 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8593 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008594
James Smart0ff10d42008-01-11 01:52:36 -05008595 spin_lock_irqsave(&phba->hbalock, flags);
8596 list_splice_init(&phba->elsbuf, &completions);
8597 phba->elsbuf_cnt = 0;
8598 phba->elsbuf_prev_cnt = 0;
8599 spin_unlock_irqrestore(&phba->hbalock, flags);
8600
8601 while (!list_empty(&completions)) {
8602 list_remove_head(&completions, buf_ptr,
8603 struct lpfc_dmabuf, list);
8604 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8605 kfree(buf_ptr);
8606 }
8607
dea31012005-04-17 16:05:31 -05008608 /* Return any active mbox cmds */
8609 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008610
James Smartda0436e2009-05-22 14:51:39 -04008611 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008612 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008613 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008614
James Smartda0436e2009-05-22 14:51:39 -04008615 return 1;
8616}
James Smart92d7f7b2007-06-17 19:56:38 -05008617
James Smartda0436e2009-05-22 14:51:39 -04008618/**
James Smart3621a712009-04-06 18:47:14 -04008619 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008620 * @srcp: Source memory pointer.
8621 * @destp: Destination memory pointer.
8622 * @cnt: Number of words required to be copied.
8623 *
8624 * This function is used for copying data between driver memory
8625 * and the SLI memory. This function also changes the endianness
8626 * of each word if native endianness is different from SLI
8627 * endianness. This function can be called with or without
8628 * lock.
8629 **/
dea31012005-04-17 16:05:31 -05008630void
8631lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8632{
8633 uint32_t *src = srcp;
8634 uint32_t *dest = destp;
8635 uint32_t ldata;
8636 int i;
8637
8638 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8639 ldata = *src;
8640 ldata = le32_to_cpu(ldata);
8641 *dest = ldata;
8642 src++;
8643 dest++;
8644 }
8645}
8646
James Smarte59058c2008-08-24 21:49:00 -04008647
8648/**
James Smarta0c87cb2009-07-19 10:01:10 -04008649 * lpfc_sli_bemem_bcopy - SLI memory copy function
8650 * @srcp: Source memory pointer.
8651 * @destp: Destination memory pointer.
8652 * @cnt: Number of words required to be copied.
8653 *
8654 * This function is used for copying data between a data structure
8655 * with big endian representation to local endianness.
8656 * This function can be called with or without lock.
8657 **/
8658void
8659lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8660{
8661 uint32_t *src = srcp;
8662 uint32_t *dest = destp;
8663 uint32_t ldata;
8664 int i;
8665
8666 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8667 ldata = *src;
8668 ldata = be32_to_cpu(ldata);
8669 *dest = ldata;
8670 src++;
8671 dest++;
8672 }
8673}
8674
8675/**
James Smart3621a712009-04-06 18:47:14 -04008676 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008677 * @phba: Pointer to HBA context object.
8678 * @pring: Pointer to driver SLI ring object.
8679 * @mp: Pointer to driver buffer object.
8680 *
8681 * This function is called with no lock held.
8682 * It always return zero after adding the buffer to the postbufq
8683 * buffer list.
8684 **/
dea31012005-04-17 16:05:31 -05008685int
James Smart2e0fef82007-06-17 19:56:36 -05008686lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8687 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008688{
8689 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8690 later */
James Smart2e0fef82007-06-17 19:56:36 -05008691 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008692 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008693 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008694 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008695 return 0;
8696}
8697
James Smarte59058c2008-08-24 21:49:00 -04008698/**
James Smart3621a712009-04-06 18:47:14 -04008699 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008700 * @phba: Pointer to HBA context object.
8701 *
8702 * When HBQ is enabled, buffers are searched based on tags. This function
8703 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8704 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8705 * does not conflict with tags of buffer posted for unsolicited events.
8706 * The function returns the allocated tag. The function is called with
8707 * no locks held.
8708 **/
James Smart76bb24e2007-10-27 13:38:00 -04008709uint32_t
8710lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8711{
8712 spin_lock_irq(&phba->hbalock);
8713 phba->buffer_tag_count++;
8714 /*
8715 * Always set the QUE_BUFTAG_BIT to distiguish between
8716 * a tag assigned by HBQ.
8717 */
8718 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8719 spin_unlock_irq(&phba->hbalock);
8720 return phba->buffer_tag_count;
8721}
8722
James Smarte59058c2008-08-24 21:49:00 -04008723/**
James Smart3621a712009-04-06 18:47:14 -04008724 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008725 * @phba: Pointer to HBA context object.
8726 * @pring: Pointer to driver SLI ring object.
8727 * @tag: Buffer tag.
8728 *
8729 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8730 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8731 * iocb is posted to the response ring with the tag of the buffer.
8732 * This function searches the pring->postbufq list using the tag
8733 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8734 * iocb. If the buffer is found then lpfc_dmabuf object of the
8735 * buffer is returned to the caller else NULL is returned.
8736 * This function is called with no lock held.
8737 **/
James Smart76bb24e2007-10-27 13:38:00 -04008738struct lpfc_dmabuf *
8739lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8740 uint32_t tag)
8741{
8742 struct lpfc_dmabuf *mp, *next_mp;
8743 struct list_head *slp = &pring->postbufq;
8744
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008745 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008746 spin_lock_irq(&phba->hbalock);
8747 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8748 if (mp->buffer_tag == tag) {
8749 list_del_init(&mp->list);
8750 pring->postbufq_cnt--;
8751 spin_unlock_irq(&phba->hbalock);
8752 return mp;
8753 }
8754 }
8755
8756 spin_unlock_irq(&phba->hbalock);
8757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008758 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008759 "ring %d Data x%lx x%p x%p x%x\n",
8760 pring->ringno, (unsigned long) tag,
8761 slp->next, slp->prev, pring->postbufq_cnt);
8762
8763 return NULL;
8764}
dea31012005-04-17 16:05:31 -05008765
James Smarte59058c2008-08-24 21:49:00 -04008766/**
James Smart3621a712009-04-06 18:47:14 -04008767 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008768 * @phba: Pointer to HBA context object.
8769 * @pring: Pointer to driver SLI ring object.
8770 * @phys: DMA address of the buffer.
8771 *
8772 * This function searches the buffer list using the dma_address
8773 * of unsolicited event to find the driver's lpfc_dmabuf object
8774 * corresponding to the dma_address. The function returns the
8775 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8776 * This function is called by the ct and els unsolicited event
8777 * handlers to get the buffer associated with the unsolicited
8778 * event.
8779 *
8780 * This function is called with no lock held.
8781 **/
dea31012005-04-17 16:05:31 -05008782struct lpfc_dmabuf *
8783lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8784 dma_addr_t phys)
8785{
8786 struct lpfc_dmabuf *mp, *next_mp;
8787 struct list_head *slp = &pring->postbufq;
8788
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008789 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05008790 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008791 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8792 if (mp->phys == phys) {
8793 list_del_init(&mp->list);
8794 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05008795 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008796 return mp;
8797 }
8798 }
8799
James Smart2e0fef82007-06-17 19:56:36 -05008800 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04008802 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05008803 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008804 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05008805 slp->next, slp->prev, pring->postbufq_cnt);
8806 return NULL;
8807}
8808
James Smarte59058c2008-08-24 21:49:00 -04008809/**
James Smart3621a712009-04-06 18:47:14 -04008810 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04008811 * @phba: Pointer to HBA context object.
8812 * @cmdiocb: Pointer to driver command iocb object.
8813 * @rspiocb: Pointer to driver response iocb object.
8814 *
8815 * This function is the completion handler for the abort iocbs for
8816 * ELS commands. This function is called from the ELS ring event
8817 * handler with no lock held. This function frees memory resources
8818 * associated with the abort iocb.
8819 **/
dea31012005-04-17 16:05:31 -05008820static void
James Smart2e0fef82007-06-17 19:56:36 -05008821lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8822 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05008823{
James Smart2e0fef82007-06-17 19:56:36 -05008824 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04008825 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05008826 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04008827 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8828
8829 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04008830
8831 if (irsp->ulpStatus) {
8832 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8833 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8834
James Smart2e0fef82007-06-17 19:56:36 -05008835 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04008836 if (phba->sli_rev < LPFC_SLI_REV4) {
8837 if (abort_iotag != 0 &&
8838 abort_iotag <= phba->sli.last_iotag)
8839 abort_iocb =
8840 phba->sli.iocbq_lookup[abort_iotag];
8841 } else
8842 /* For sli4 the abort_tag is the XRI,
8843 * so the abort routine puts the iotag of the iocb
8844 * being aborted in the context field of the abort
8845 * IOCB.
8846 */
8847 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04008848
James Smart2680eea2007-04-25 09:52:55 -04008849 /*
James Smart58da1ff2008-04-07 10:15:56 -04008850 * If the iocb is not found in Firmware queue the iocb
8851 * might have completed already. Do not free it again.
8852 */
James Smart9b379602008-04-07 10:16:00 -04008853 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04008854 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8855 spin_unlock_irq(&phba->hbalock);
8856 lpfc_sli_release_iocbq(phba, cmdiocb);
8857 return;
8858 }
8859 /* For SLI4 the ulpContext field for abort IOCB
8860 * holds the iotag of the IOCB being aborted so
8861 * the local abort_context needs to be reset to
8862 * match the aborted IOCBs ulpContext.
8863 */
8864 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8865 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04008866 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008867
8868 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8869 "0327 Cannot abort els iocb %p "
8870 "with tag %x context %x, abort status %x, "
8871 "abort code %x\n",
8872 abort_iocb, abort_iotag, abort_context,
8873 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04008874 /*
James Smart2680eea2007-04-25 09:52:55 -04008875 * make sure we have the right iocbq before taking it
8876 * off the txcmplq and try to call completion routine.
8877 */
James Smart2e0fef82007-06-17 19:56:36 -05008878 if (!abort_iocb ||
8879 abort_iocb->iocb.ulpContext != abort_context ||
8880 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8881 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05008882 else if (phba->sli_rev < LPFC_SLI_REV4) {
8883 /*
8884 * leave the SLI4 aborted command on the txcmplq
8885 * list and the command complete WCQE's XB bit
8886 * will tell whether the SGL (XRI) can be released
8887 * immediately or to the aborted SGL list for the
8888 * following abort XRI from the HBA.
8889 */
James Smart92d7f7b2007-06-17 19:56:38 -05008890 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04008891 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8892 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8893 pring->txcmplq_cnt--;
8894 }
James Smart2680eea2007-04-25 09:52:55 -04008895
James Smart0ff10d42008-01-11 01:52:36 -05008896 /* Firmware could still be in progress of DMAing
8897 * payload, so don't free data buffer till after
8898 * a hbeat.
8899 */
8900 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05008901 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05008902 spin_unlock_irq(&phba->hbalock);
8903
James Smart92d7f7b2007-06-17 19:56:38 -05008904 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05008905 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05008906 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04008907 } else
8908 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04008909 }
8910
James Bottomley604a3e32005-10-29 10:28:33 -05008911 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05008912 return;
8913}
8914
James Smarte59058c2008-08-24 21:49:00 -04008915/**
James Smart3621a712009-04-06 18:47:14 -04008916 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04008917 * @phba: Pointer to HBA context object.
8918 * @cmdiocb: Pointer to driver command iocb object.
8919 * @rspiocb: Pointer to driver response iocb object.
8920 *
8921 * The function is called from SLI ring event handler with no
8922 * lock held. This function is the completion handler for ELS commands
8923 * which are aborted. The function frees memory resources used for
8924 * the aborted ELS commands.
8925 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008926static void
8927lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8928 struct lpfc_iocbq *rspiocb)
8929{
8930 IOCB_t *irsp = &rspiocb->iocb;
8931
8932 /* ELS cmd tag <ulpIoTag> completes */
8933 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04008934 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008935 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008936 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05008937 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05008938 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8939 lpfc_ct_free_iocb(phba, cmdiocb);
8940 else
8941 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05008942 return;
8943}
8944
James Smarte59058c2008-08-24 21:49:00 -04008945/**
James Smart5af5eee2010-10-22 11:06:38 -04008946 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04008947 * @phba: Pointer to HBA context object.
8948 * @pring: Pointer to driver SLI ring object.
8949 * @cmdiocb: Pointer to driver command iocb object.
8950 *
James Smart5af5eee2010-10-22 11:06:38 -04008951 * This function issues an abort iocb for the provided command iocb down to
8952 * the port. Other than the case the outstanding command iocb is an abort
8953 * request, this function issues abort out unconditionally. This function is
8954 * called with hbalock held. The function returns 0 when it fails due to
8955 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04008956 **/
James Smart5af5eee2010-10-22 11:06:38 -04008957static int
8958lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008959 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05008960{
James Smart2e0fef82007-06-17 19:56:36 -05008961 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008962 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05008963 IOCB_t *icmd = NULL;
8964 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04008965 int retval;
James Smart07951072007-04-25 09:51:38 -04008966
James Smart92d7f7b2007-06-17 19:56:38 -05008967 /*
8968 * There are certain command types we don't want to abort. And we
8969 * don't want to abort commands that are already in the process of
8970 * being aborted.
James Smart07951072007-04-25 09:51:38 -04008971 */
8972 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05008973 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05008974 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8975 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04008976 return 0;
8977
dea31012005-04-17 16:05:31 -05008978 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05008979 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008980 if (abtsiocbp == NULL)
8981 return 0;
dea31012005-04-17 16:05:31 -05008982
James Smart07951072007-04-25 09:51:38 -04008983 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05008984 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04008985 */
8986 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8987
dea31012005-04-17 16:05:31 -05008988 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04008989 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8990 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04008991 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04008992 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04008993 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8994 }
James Smartda0436e2009-05-22 14:51:39 -04008995 else
8996 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008997 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04008998 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05008999
James Smart5ffc2662009-11-18 15:39:44 -05009000 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9001 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009002 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9003 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009004
James Smart2e0fef82007-06-17 19:56:36 -05009005 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009006 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9007 else
9008 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9009
9010 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009011
James Smarte8b62012007-08-02 11:10:09 -04009012 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9013 "0339 Abort xri x%x, original iotag x%x, "
9014 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009015 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009016 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009017 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009018 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009019
James Smartd7c255b2008-08-24 21:50:00 -04009020 if (retval)
9021 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009022
9023 /*
9024 * Caller to this routine should check for IOCB_ERROR
9025 * and handle it properly. This routine no longer removes
9026 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9027 */
9028 return retval;
9029}
9030
9031/**
9032 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9033 * @phba: Pointer to HBA context object.
9034 * @pring: Pointer to driver SLI ring object.
9035 * @cmdiocb: Pointer to driver command iocb object.
9036 *
9037 * This function issues an abort iocb for the provided command iocb. In case
9038 * of unloading, the abort iocb will not be issued to commands on the ELS
9039 * ring. Instead, the callback function shall be changed to those commands
9040 * so that nothing happens when them finishes. This function is called with
9041 * hbalock held. The function returns 0 when the command iocb is an abort
9042 * request.
9043 **/
9044int
9045lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9046 struct lpfc_iocbq *cmdiocb)
9047{
9048 struct lpfc_vport *vport = cmdiocb->vport;
9049 int retval = IOCB_ERROR;
9050 IOCB_t *icmd = NULL;
9051
9052 /*
9053 * There are certain command types we don't want to abort. And we
9054 * don't want to abort commands that are already in the process of
9055 * being aborted.
9056 */
9057 icmd = &cmdiocb->iocb;
9058 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9059 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9060 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9061 return 0;
9062
9063 /*
9064 * If we're unloading, don't abort iocb on the ELS ring, but change
9065 * the callback so that nothing happens when it finishes.
9066 */
9067 if ((vport->load_flag & FC_UNLOADING) &&
9068 (pring->ringno == LPFC_ELS_RING)) {
9069 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9070 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9071 else
9072 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9073 goto abort_iotag_exit;
9074 }
9075
9076 /* Now, we try to issue the abort to the cmdiocb out */
9077 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9078
James Smart07951072007-04-25 09:51:38 -04009079abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009080 /*
9081 * Caller to this routine should check for IOCB_ERROR
9082 * and handle it properly. This routine no longer removes
9083 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009084 */
James Smart2e0fef82007-06-17 19:56:36 -05009085 return retval;
dea31012005-04-17 16:05:31 -05009086}
9087
James Smarte59058c2008-08-24 21:49:00 -04009088/**
James Smart5af5eee2010-10-22 11:06:38 -04009089 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9090 * @phba: Pointer to HBA context object.
9091 * @pring: Pointer to driver SLI ring object.
9092 *
9093 * This function aborts all iocbs in the given ring and frees all the iocb
9094 * objects in txq. This function issues abort iocbs unconditionally for all
9095 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9096 * to complete before the return of this function. The caller is not required
9097 * to hold any locks.
9098 **/
9099static void
9100lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9101{
9102 LIST_HEAD(completions);
9103 struct lpfc_iocbq *iocb, *next_iocb;
9104
9105 if (pring->ringno == LPFC_ELS_RING)
9106 lpfc_fabric_abort_hba(phba);
9107
9108 spin_lock_irq(&phba->hbalock);
9109
9110 /* Take off all the iocbs on txq for cancelling */
9111 list_splice_init(&pring->txq, &completions);
9112 pring->txq_cnt = 0;
9113
9114 /* Next issue ABTS for everything on the txcmplq */
9115 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9116 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9117
9118 spin_unlock_irq(&phba->hbalock);
9119
9120 /* Cancel all the IOCBs from the completions list */
9121 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9122 IOERR_SLI_ABORTED);
9123}
9124
9125/**
9126 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9127 * @phba: pointer to lpfc HBA data structure.
9128 *
9129 * This routine will abort all pending and outstanding iocbs to an HBA.
9130 **/
9131void
9132lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9133{
9134 struct lpfc_sli *psli = &phba->sli;
9135 struct lpfc_sli_ring *pring;
9136 int i;
9137
9138 for (i = 0; i < psli->num_rings; i++) {
9139 pring = &psli->ring[i];
9140 lpfc_sli_iocb_ring_abort(phba, pring);
9141 }
9142}
9143
9144/**
James Smart3621a712009-04-06 18:47:14 -04009145 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009146 * @iocbq: Pointer to driver iocb object.
9147 * @vport: Pointer to driver virtual port object.
9148 * @tgt_id: SCSI ID of the target.
9149 * @lun_id: LUN ID of the scsi device.
9150 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9151 *
James Smart3621a712009-04-06 18:47:14 -04009152 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009153 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9154 * 0 if the filtering criteria is met for the given iocb and will return
9155 * 1 if the filtering criteria is not met.
9156 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9157 * given iocb is for the SCSI device specified by vport, tgt_id and
9158 * lun_id parameter.
9159 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9160 * given iocb is for the SCSI target specified by vport and tgt_id
9161 * parameters.
9162 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9163 * given iocb is for the SCSI host associated with the given vport.
9164 * This function is called with no locks held.
9165 **/
dea31012005-04-17 16:05:31 -05009166static int
James Smart51ef4c22007-08-02 11:10:31 -04009167lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9168 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009169 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009170{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009171 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009172 int rc = 1;
9173
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009174 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9175 return rc;
9176
James Smart51ef4c22007-08-02 11:10:31 -04009177 if (iocbq->vport != vport)
9178 return rc;
9179
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009180 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009181
James Smart495a7142008-06-14 22:52:59 -04009182 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009183 return rc;
9184
9185 switch (ctx_cmd) {
9186 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009187 if ((lpfc_cmd->rdata->pnode) &&
9188 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9189 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009190 rc = 0;
9191 break;
9192 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009193 if ((lpfc_cmd->rdata->pnode) &&
9194 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009195 rc = 0;
9196 break;
dea31012005-04-17 16:05:31 -05009197 case LPFC_CTX_HOST:
9198 rc = 0;
9199 break;
9200 default:
9201 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009202 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009203 break;
9204 }
9205
9206 return rc;
9207}
9208
James Smarte59058c2008-08-24 21:49:00 -04009209/**
James Smart3621a712009-04-06 18:47:14 -04009210 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009211 * @vport: Pointer to virtual port.
9212 * @tgt_id: SCSI ID of the target.
9213 * @lun_id: LUN ID of the scsi device.
9214 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9215 *
9216 * This function returns number of FCP commands pending for the vport.
9217 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9218 * commands pending on the vport associated with SCSI device specified
9219 * by tgt_id and lun_id parameters.
9220 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9221 * commands pending on the vport associated with SCSI target specified
9222 * by tgt_id parameter.
9223 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9224 * commands pending on the vport.
9225 * This function returns the number of iocbs which satisfy the filter.
9226 * This function is called without any lock held.
9227 **/
dea31012005-04-17 16:05:31 -05009228int
James Smart51ef4c22007-08-02 11:10:31 -04009229lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9230 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009231{
James Smart51ef4c22007-08-02 11:10:31 -04009232 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009233 struct lpfc_iocbq *iocbq;
9234 int sum, i;
dea31012005-04-17 16:05:31 -05009235
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009236 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9237 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009238
James Smart51ef4c22007-08-02 11:10:31 -04009239 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9240 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009241 sum++;
dea31012005-04-17 16:05:31 -05009242 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009243
dea31012005-04-17 16:05:31 -05009244 return sum;
9245}
9246
James Smarte59058c2008-08-24 21:49:00 -04009247/**
James Smart3621a712009-04-06 18:47:14 -04009248 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009249 * @phba: Pointer to HBA context object
9250 * @cmdiocb: Pointer to command iocb object.
9251 * @rspiocb: Pointer to response iocb object.
9252 *
9253 * This function is called when an aborted FCP iocb completes. This
9254 * function is called by the ring event handler with no lock held.
9255 * This function frees the iocb.
9256 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009257void
James Smart2e0fef82007-06-17 19:56:36 -05009258lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9259 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009260{
James Bottomley604a3e32005-10-29 10:28:33 -05009261 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009262 return;
9263}
9264
James Smarte59058c2008-08-24 21:49:00 -04009265/**
James Smart3621a712009-04-06 18:47:14 -04009266 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009267 * @vport: Pointer to virtual port.
9268 * @pring: Pointer to driver SLI ring object.
9269 * @tgt_id: SCSI ID of the target.
9270 * @lun_id: LUN ID of the scsi device.
9271 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9272 *
9273 * This function sends an abort command for every SCSI command
9274 * associated with the given virtual port pending on the ring
9275 * filtered by lpfc_sli_validate_fcp_iocb function.
9276 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9277 * FCP iocbs associated with lun specified by tgt_id and lun_id
9278 * parameters
9279 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9280 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9281 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9282 * FCP iocbs associated with virtual port.
9283 * This function returns number of iocbs it failed to abort.
9284 * This function is called with no locks held.
9285 **/
dea31012005-04-17 16:05:31 -05009286int
James Smart51ef4c22007-08-02 11:10:31 -04009287lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9288 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009289{
James Smart51ef4c22007-08-02 11:10:31 -04009290 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009291 struct lpfc_iocbq *iocbq;
9292 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009293 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009294 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009295 int i;
dea31012005-04-17 16:05:31 -05009296
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009297 for (i = 1; i <= phba->sli.last_iotag; i++) {
9298 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009299
James Smart51ef4c22007-08-02 11:10:31 -04009300 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009301 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009302 continue;
9303
9304 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009305 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009306 if (abtsiocb == NULL) {
9307 errcnt++;
9308 continue;
9309 }
dea31012005-04-17 16:05:31 -05009310
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009311 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009312 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9313 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009314 if (phba->sli_rev == LPFC_SLI_REV4)
9315 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9316 else
9317 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009318 abtsiocb->iocb.ulpLe = 1;
9319 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009320 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009321
James Smart5ffc2662009-11-18 15:39:44 -05009322 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9323 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009324 if (iocbq->iocb_flag & LPFC_IO_FCP)
9325 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009326
James Smart2e0fef82007-06-17 19:56:36 -05009327 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009328 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9329 else
9330 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9331
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009332 /* Setup callback routine and issue the command. */
9333 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009334 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9335 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009336 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009337 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009338 errcnt++;
9339 continue;
9340 }
9341 }
9342
9343 return errcnt;
9344}
9345
James Smarte59058c2008-08-24 21:49:00 -04009346/**
James Smart3621a712009-04-06 18:47:14 -04009347 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009348 * @phba: Pointer to HBA context object.
9349 * @cmdiocbq: Pointer to command iocb.
9350 * @rspiocbq: Pointer to response iocb.
9351 *
9352 * This function is the completion handler for iocbs issued using
9353 * lpfc_sli_issue_iocb_wait function. This function is called by the
9354 * ring event handler function without any lock held. This function
9355 * can be called from both worker thread context and interrupt
9356 * context. This function also can be called from other thread which
9357 * cleans up the SLI layer objects.
9358 * This function copy the contents of the response iocb to the
9359 * response iocb memory object provided by the caller of
9360 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9361 * sleeps for the iocb completion.
9362 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009363static void
9364lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9365 struct lpfc_iocbq *cmdiocbq,
9366 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009367{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009368 wait_queue_head_t *pdone_q;
9369 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009370 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009371
James Smart2e0fef82007-06-17 19:56:36 -05009372 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009373 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9374 if (cmdiocbq->context2 && rspiocbq)
9375 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9376 &rspiocbq->iocb, sizeof(IOCB_t));
9377
James Smart0f65ff62010-02-26 14:14:23 -05009378 /* Set the exchange busy flag for task management commands */
9379 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9380 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9381 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9382 cur_iocbq);
9383 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9384 }
9385
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009386 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009387 if (pdone_q)
9388 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009389 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009390 return;
9391}
9392
James Smarte59058c2008-08-24 21:49:00 -04009393/**
James Smartd11e31d2009-06-10 17:23:06 -04009394 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9395 * @phba: Pointer to HBA context object..
9396 * @piocbq: Pointer to command iocb.
9397 * @flag: Flag to test.
9398 *
9399 * This routine grabs the hbalock and then test the iocb_flag to
9400 * see if the passed in flag is set.
9401 * Returns:
9402 * 1 if flag is set.
9403 * 0 if flag is not set.
9404 **/
9405static int
9406lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9407 struct lpfc_iocbq *piocbq, uint32_t flag)
9408{
9409 unsigned long iflags;
9410 int ret;
9411
9412 spin_lock_irqsave(&phba->hbalock, iflags);
9413 ret = piocbq->iocb_flag & flag;
9414 spin_unlock_irqrestore(&phba->hbalock, iflags);
9415 return ret;
9416
9417}
9418
9419/**
James Smart3621a712009-04-06 18:47:14 -04009420 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009421 * @phba: Pointer to HBA context object..
9422 * @pring: Pointer to sli ring.
9423 * @piocb: Pointer to command iocb.
9424 * @prspiocbq: Pointer to response iocb.
9425 * @timeout: Timeout in number of seconds.
9426 *
9427 * This function issues the iocb to firmware and waits for the
9428 * iocb to complete. If the iocb command is not
9429 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9430 * Caller should not free the iocb resources if this function
9431 * returns IOCB_TIMEDOUT.
9432 * The function waits for the iocb completion using an
9433 * non-interruptible wait.
9434 * This function will sleep while waiting for iocb completion.
9435 * So, this function should not be called from any context which
9436 * does not allow sleeping. Due to the same reason, this function
9437 * cannot be called with interrupt disabled.
9438 * This function assumes that the iocb completions occur while
9439 * this function sleep. So, this function cannot be called from
9440 * the thread which process iocb completion for this ring.
9441 * This function clears the iocb_flag of the iocb object before
9442 * issuing the iocb and the iocb completion handler sets this
9443 * flag and wakes this thread when the iocb completes.
9444 * The contents of the response iocb will be copied to prspiocbq
9445 * by the completion handler when the command completes.
9446 * This function returns IOCB_SUCCESS when success.
9447 * This function is called with no lock held.
9448 **/
dea31012005-04-17 16:05:31 -05009449int
James Smart2e0fef82007-06-17 19:56:36 -05009450lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009451 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009452 struct lpfc_iocbq *piocb,
9453 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009454 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009455{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009456 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009457 long timeleft, timeout_req = 0;
9458 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009459 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009460 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009461 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009462 * If the caller has provided a response iocbq buffer, then context2
9463 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009464 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009465 if (prspiocbq) {
9466 if (piocb->context2)
9467 return IOCB_ERROR;
9468 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009469 }
9470
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009471 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9472 piocb->context_un.wait_queue = &done_q;
9473 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009474
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009475 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009476 if (lpfc_readl(phba->HCregaddr, &creg_val))
9477 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009478 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9479 writel(creg_val, phba->HCregaddr);
9480 readl(phba->HCregaddr); /* flush */
9481 }
9482
James Smart2a9bf3d2010-06-07 15:24:45 -04009483 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9484 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009485 if (retval == IOCB_SUCCESS) {
9486 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009487 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009488 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009489 timeout_req);
dea31012005-04-17 16:05:31 -05009490
James Smart7054a602007-04-25 09:52:34 -04009491 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9492 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009493 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009494 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009495 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009496 "0338 IOCB wait timeout error - no "
9497 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009498 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009499 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009501 "0330 IOCB wake NOT set, "
9502 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009503 timeout, (timeleft / jiffies));
9504 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009505 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009506 } else if (retval == IOCB_BUSY) {
9507 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9508 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9509 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9510 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009511 } else {
9512 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009513 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009514 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009515 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009516 }
9517
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009518 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009519 if (lpfc_readl(phba->HCregaddr, &creg_val))
9520 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009521 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9522 writel(creg_val, phba->HCregaddr);
9523 readl(phba->HCregaddr); /* flush */
9524 }
9525
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009526 if (prspiocbq)
9527 piocb->context2 = NULL;
9528
9529 piocb->context_un.wait_queue = NULL;
9530 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009531 return retval;
9532}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009533
James Smarte59058c2008-08-24 21:49:00 -04009534/**
James Smart3621a712009-04-06 18:47:14 -04009535 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009536 * @phba: Pointer to HBA context object.
9537 * @pmboxq: Pointer to driver mailbox object.
9538 * @timeout: Timeout in number of seconds.
9539 *
9540 * This function issues the mailbox to firmware and waits for the
9541 * mailbox command to complete. If the mailbox command is not
9542 * completed within timeout seconds, it returns MBX_TIMEOUT.
9543 * The function waits for the mailbox completion using an
9544 * interruptible wait. If the thread is woken up due to a
9545 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9546 * should not free the mailbox resources, if this function returns
9547 * MBX_TIMEOUT.
9548 * This function will sleep while waiting for mailbox completion.
9549 * So, this function should not be called from any context which
9550 * does not allow sleeping. Due to the same reason, this function
9551 * cannot be called with interrupt disabled.
9552 * This function assumes that the mailbox completion occurs while
9553 * this function sleep. So, this function cannot be called from
9554 * the worker thread which processes mailbox completion.
9555 * This function is called in the context of HBA management
9556 * applications.
9557 * This function returns MBX_SUCCESS when successful.
9558 * This function is called with no lock held.
9559 **/
dea31012005-04-17 16:05:31 -05009560int
James Smart2e0fef82007-06-17 19:56:36 -05009561lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009562 uint32_t timeout)
9563{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009564 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009565 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009566 unsigned long flag;
dea31012005-04-17 16:05:31 -05009567
9568 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009569 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009570 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009571
James Smart495a7142008-06-14 22:52:59 -04009572 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009573 /* setup wake call as IOCB callback */
9574 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9575 /* setup context field to pass wait_queue pointer to wake function */
9576 pmboxq->context1 = &done_q;
9577
dea31012005-04-17 16:05:31 -05009578 /* now issue the command */
9579 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009580 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009581 wait_event_interruptible_timeout(done_q,
9582 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9583 timeout * HZ);
9584
James Smart858c9f62007-06-17 19:56:39 -05009585 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009586 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009587 /*
9588 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9589 * else do not free the resources.
9590 */
James Smartd7c47992010-06-08 18:31:54 -04009591 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009592 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009593 lpfc_sli4_swap_str(phba, pmboxq);
9594 } else {
James Smart7054a602007-04-25 09:52:34 -04009595 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009596 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9597 }
9598 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009599 }
9600
dea31012005-04-17 16:05:31 -05009601 return retval;
9602}
9603
James Smarte59058c2008-08-24 21:49:00 -04009604/**
James Smart3772a992009-05-22 14:50:54 -04009605 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009606 * @phba: Pointer to HBA context.
9607 *
James Smart3772a992009-05-22 14:50:54 -04009608 * This function is called to shutdown the driver's mailbox sub-system.
9609 * It first marks the mailbox sub-system is in a block state to prevent
9610 * the asynchronous mailbox command from issued off the pending mailbox
9611 * command queue. If the mailbox command sub-system shutdown is due to
9612 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9613 * the mailbox sub-system flush routine to forcefully bring down the
9614 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9615 * as with offline or HBA function reset), this routine will wait for the
9616 * outstanding mailbox command to complete before invoking the mailbox
9617 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009618 **/
James Smart3772a992009-05-22 14:50:54 -04009619void
9620lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009621{
James Smart3772a992009-05-22 14:50:54 -04009622 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009623 unsigned long timeout;
9624
James Smarta183a152011-10-10 21:32:43 -04009625 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009626 spin_lock_irq(&phba->hbalock);
9627 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9628 spin_unlock_irq(&phba->hbalock);
9629
9630 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9631 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009632 /* Determine how long we might wait for the active mailbox
9633 * command to be gracefully completed by firmware.
9634 */
James Smarta183a152011-10-10 21:32:43 -04009635 if (phba->sli.mbox_active)
9636 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9637 phba->sli.mbox_active) *
9638 1000) + jiffies;
9639 spin_unlock_irq(&phba->hbalock);
9640
James Smart3772a992009-05-22 14:50:54 -04009641 while (phba->sli.mbox_active) {
9642 /* Check active mailbox complete status every 2ms */
9643 msleep(2);
9644 if (time_after(jiffies, timeout))
9645 /* Timeout, let the mailbox flush routine to
9646 * forcefully release active mailbox command
9647 */
9648 break;
9649 }
9650 }
9651 lpfc_sli_mbox_sys_flush(phba);
9652}
9653
9654/**
9655 * lpfc_sli_eratt_read - read sli-3 error attention events
9656 * @phba: Pointer to HBA context.
9657 *
9658 * This function is called to read the SLI3 device error attention registers
9659 * for possible error attention events. The caller must hold the hostlock
9660 * with spin_lock_irq().
9661 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009662 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009663 * Register and returns 0 otherwise.
9664 **/
9665static int
9666lpfc_sli_eratt_read(struct lpfc_hba *phba)
9667{
James Smarted957682007-06-17 19:56:37 -05009668 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009669
James Smart3772a992009-05-22 14:50:54 -04009670 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009671 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9672 goto unplug_err;
9673
James Smart3772a992009-05-22 14:50:54 -04009674 if (ha_copy & HA_ERATT) {
9675 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009676 if (lpfc_sli_read_hs(phba))
9677 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009678
James Smart3772a992009-05-22 14:50:54 -04009679 /* Check if there is a deferred error condition is active */
9680 if ((HS_FFER1 & phba->work_hs) &&
9681 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009682 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009683 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009684 /* Clear all interrupt enable conditions */
9685 writel(0, phba->HCregaddr);
9686 readl(phba->HCregaddr);
9687 }
9688
9689 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009690 phba->work_ha |= HA_ERATT;
9691 /* Indicate polling handles this ERATT */
9692 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009693 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009694 }
James Smart3772a992009-05-22 14:50:54 -04009695 return 0;
James Smart9940b972011-03-11 16:06:12 -05009696
9697unplug_err:
9698 /* Set the driver HS work bitmap */
9699 phba->work_hs |= UNPLUG_ERR;
9700 /* Set the driver HA work bitmap */
9701 phba->work_ha |= HA_ERATT;
9702 /* Indicate polling handles this ERATT */
9703 phba->hba_flag |= HBA_ERATT_HANDLED;
9704 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009705}
9706
James Smarte59058c2008-08-24 21:49:00 -04009707/**
James Smartda0436e2009-05-22 14:51:39 -04009708 * lpfc_sli4_eratt_read - read sli-4 error attention events
9709 * @phba: Pointer to HBA context.
9710 *
9711 * This function is called to read the SLI4 device error attention registers
9712 * for possible error attention events. The caller must hold the hostlock
9713 * with spin_lock_irq().
9714 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009715 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009716 * Register and returns 0 otherwise.
9717 **/
9718static int
9719lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9720{
9721 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009722 uint32_t if_type, portsmphr;
9723 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009724
James Smart2fcee4b2010-12-15 17:57:46 -05009725 /*
9726 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009727 * registers for error attention. This can be changed later.
9728 */
James Smart2fcee4b2010-12-15 17:57:46 -05009729 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9730 switch (if_type) {
9731 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009732 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9733 &uerr_sta_lo) ||
9734 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9735 &uerr_sta_hi)) {
9736 phba->work_hs |= UNPLUG_ERR;
9737 phba->work_ha |= HA_ERATT;
9738 phba->hba_flag |= HBA_ERATT_HANDLED;
9739 return 1;
9740 }
James Smart2fcee4b2010-12-15 17:57:46 -05009741 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9742 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9744 "1423 HBA Unrecoverable error: "
9745 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9746 "ue_mask_lo_reg=0x%x, "
9747 "ue_mask_hi_reg=0x%x\n",
9748 uerr_sta_lo, uerr_sta_hi,
9749 phba->sli4_hba.ue_mask_lo,
9750 phba->sli4_hba.ue_mask_hi);
9751 phba->work_status[0] = uerr_sta_lo;
9752 phba->work_status[1] = uerr_sta_hi;
9753 phba->work_ha |= HA_ERATT;
9754 phba->hba_flag |= HBA_ERATT_HANDLED;
9755 return 1;
9756 }
9757 break;
9758 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009759 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9760 &portstat_reg.word0) ||
9761 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9762 &portsmphr)){
9763 phba->work_hs |= UNPLUG_ERR;
9764 phba->work_ha |= HA_ERATT;
9765 phba->hba_flag |= HBA_ERATT_HANDLED;
9766 return 1;
9767 }
James Smart2fcee4b2010-12-15 17:57:46 -05009768 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9769 phba->work_status[0] =
9770 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9771 phba->work_status[1] =
9772 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9774 "2885 Port Error Detected: "
9775 "port status reg 0x%x, "
9776 "port smphr reg 0x%x, "
9777 "error 1=0x%x, error 2=0x%x\n",
9778 portstat_reg.word0,
9779 portsmphr,
9780 phba->work_status[0],
9781 phba->work_status[1]);
9782 phba->work_ha |= HA_ERATT;
9783 phba->hba_flag |= HBA_ERATT_HANDLED;
9784 return 1;
9785 }
9786 break;
9787 case LPFC_SLI_INTF_IF_TYPE_1:
9788 default:
James Smarta747c9c2009-11-18 15:41:10 -05009789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009790 "2886 HBA Error Attention on unsupported "
9791 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009792 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009793 }
James Smart2fcee4b2010-12-15 17:57:46 -05009794
James Smartda0436e2009-05-22 14:51:39 -04009795 return 0;
9796}
9797
9798/**
James Smart3621a712009-04-06 18:47:14 -04009799 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009800 * @phba: Pointer to HBA context.
9801 *
James Smart3772a992009-05-22 14:50:54 -04009802 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009803 * error attention register bit for error attention events.
9804 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009805 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009806 * Register and returns 0 otherwise.
9807 **/
9808int
9809lpfc_sli_check_eratt(struct lpfc_hba *phba)
9810{
9811 uint32_t ha_copy;
9812
9813 /* If somebody is waiting to handle an eratt, don't process it
9814 * here. The brdkill function will do this.
9815 */
9816 if (phba->link_flag & LS_IGNORE_ERATT)
9817 return 0;
9818
9819 /* Check if interrupt handler handles this ERATT */
9820 spin_lock_irq(&phba->hbalock);
9821 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9822 /* Interrupt handler has handled ERATT */
9823 spin_unlock_irq(&phba->hbalock);
9824 return 0;
9825 }
9826
James Smarta257bf92009-04-06 18:48:10 -04009827 /*
9828 * If there is deferred error attention, do not check for error
9829 * attention
9830 */
9831 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9832 spin_unlock_irq(&phba->hbalock);
9833 return 0;
9834 }
9835
James Smart3772a992009-05-22 14:50:54 -04009836 /* If PCI channel is offline, don't process it */
9837 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04009838 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009839 return 0;
9840 }
9841
9842 switch (phba->sli_rev) {
9843 case LPFC_SLI_REV2:
9844 case LPFC_SLI_REV3:
9845 /* Read chip Host Attention (HA) register */
9846 ha_copy = lpfc_sli_eratt_read(phba);
9847 break;
James Smartda0436e2009-05-22 14:51:39 -04009848 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05009849 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04009850 ha_copy = lpfc_sli4_eratt_read(phba);
9851 break;
James Smart3772a992009-05-22 14:50:54 -04009852 default:
9853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9854 "0299 Invalid SLI revision (%d)\n",
9855 phba->sli_rev);
9856 ha_copy = 0;
9857 break;
James Smart93996272008-08-24 21:50:30 -04009858 }
9859 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009860
9861 return ha_copy;
9862}
9863
9864/**
9865 * lpfc_intr_state_check - Check device state for interrupt handling
9866 * @phba: Pointer to HBA context.
9867 *
9868 * This inline routine checks whether a device or its PCI slot is in a state
9869 * that the interrupt should be handled.
9870 *
9871 * This function returns 0 if the device or the PCI slot is in a state that
9872 * interrupt should be handled, otherwise -EIO.
9873 */
9874static inline int
9875lpfc_intr_state_check(struct lpfc_hba *phba)
9876{
9877 /* If the pci channel is offline, ignore all the interrupts */
9878 if (unlikely(pci_channel_offline(phba->pcidev)))
9879 return -EIO;
9880
9881 /* Update device level interrupt statistics */
9882 phba->sli.slistat.sli_intr++;
9883
9884 /* Ignore all interrupts during initialization. */
9885 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9886 return -EIO;
9887
James Smart93996272008-08-24 21:50:30 -04009888 return 0;
9889}
9890
9891/**
James Smart3772a992009-05-22 14:50:54 -04009892 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04009893 * @irq: Interrupt number.
9894 * @dev_id: The device context pointer.
9895 *
James Smart93996272008-08-24 21:50:30 -04009896 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009897 * service routine when device with SLI-3 interface spec is enabled with
9898 * MSI-X multi-message interrupt mode and there are slow-path events in
9899 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9900 * interrupt mode, this function is called as part of the device-level
9901 * interrupt handler. When the PCI slot is in error recovery or the HBA
9902 * is undergoing initialization, the interrupt handler will not process
9903 * the interrupt. The link attention and ELS ring attention events are
9904 * handled by the worker thread. The interrupt handler signals the worker
9905 * thread and returns for these events. This function is called without
9906 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04009907 * structures.
9908 *
9909 * This function returns IRQ_HANDLED when interrupt is handled else it
9910 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04009911 **/
dea31012005-04-17 16:05:31 -05009912irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009913lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05009914{
James Smart2e0fef82007-06-17 19:56:36 -05009915 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05009916 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009917 uint32_t work_ha_copy;
9918 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05009919 unsigned long iflag;
dea31012005-04-17 16:05:31 -05009920 uint32_t control;
9921
James Smart92d7f7b2007-06-17 19:56:38 -05009922 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05009923 struct lpfc_vport *vport;
9924 struct lpfc_nodelist *ndlp;
9925 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05009926 LPFC_MBOXQ_t *pmb;
9927 int rc;
9928
dea31012005-04-17 16:05:31 -05009929 /*
9930 * Get the driver's phba structure from the dev_id and
9931 * assume the HBA is not interrupting.
9932 */
James Smart93996272008-08-24 21:50:30 -04009933 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05009934
9935 if (unlikely(!phba))
9936 return IRQ_NONE;
9937
dea31012005-04-17 16:05:31 -05009938 /*
James Smart93996272008-08-24 21:50:30 -04009939 * Stuff needs to be attented to when this function is invoked as an
9940 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009941 */
James Smart93996272008-08-24 21:50:30 -04009942 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009943 /* Check device state for handling interrupt */
9944 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009945 return IRQ_NONE;
9946 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05009947 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009948 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9949 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -04009950 /* If somebody is waiting to handle an eratt don't process it
9951 * here. The brdkill function will do this.
9952 */
9953 if (phba->link_flag & LS_IGNORE_ERATT)
9954 ha_copy &= ~HA_ERATT;
9955 /* Check the need for handling ERATT in interrupt handler */
9956 if (ha_copy & HA_ERATT) {
9957 if (phba->hba_flag & HBA_ERATT_HANDLED)
9958 /* ERATT polling has handled ERATT */
9959 ha_copy &= ~HA_ERATT;
9960 else
9961 /* Indicate interrupt handler handles ERATT */
9962 phba->hba_flag |= HBA_ERATT_HANDLED;
9963 }
James Smarta257bf92009-04-06 18:48:10 -04009964
9965 /*
9966 * If there is deferred error attention, do not check for any
9967 * interrupt.
9968 */
9969 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009970 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009971 return IRQ_NONE;
9972 }
9973
James Smart93996272008-08-24 21:50:30 -04009974 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -05009975 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9976 goto unplug_error;
9977
James Smarta747c9c2009-11-18 15:41:10 -05009978 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9979 HC_LAINT_ENA | HC_ERINT_ENA),
9980 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009981 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9982 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009983 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009984 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009985 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009986 } else
9987 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05009988
dea31012005-04-17 16:05:31 -05009989 work_ha_copy = ha_copy & phba->work_ha_mask;
9990
James Smart93996272008-08-24 21:50:30 -04009991 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05009992 if (work_ha_copy & HA_LATT) {
9993 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9994 /*
9995 * Turn off Link Attention interrupts
9996 * until CLEAR_LA done
9997 */
James Smart5b75da22008-12-04 22:39:35 -05009998 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009999 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010000 if (lpfc_readl(phba->HCregaddr, &control))
10001 goto unplug_error;
dea31012005-04-17 16:05:31 -050010002 control &= ~HC_LAINT_ENA;
10003 writel(control, phba->HCregaddr);
10004 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010005 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010006 }
10007 else
10008 work_ha_copy &= ~HA_LATT;
10009 }
10010
James Smart93996272008-08-24 21:50:30 -040010011 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010012 /*
10013 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10014 * the only slow ring.
10015 */
10016 status = (work_ha_copy &
10017 (HA_RXMASK << (4*LPFC_ELS_RING)));
10018 status >>= (4*LPFC_ELS_RING);
10019 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010020 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010021 if (lpfc_readl(phba->HCregaddr, &control))
10022 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010023
10024 lpfc_debugfs_slow_ring_trc(phba,
10025 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10026 control, status,
10027 (uint32_t)phba->sli.slistat.sli_intr);
10028
James Smart858c9f62007-06-17 19:56:39 -050010029 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010030 lpfc_debugfs_slow_ring_trc(phba,
10031 "ISR Disable ring:"
10032 "pwork:x%x hawork:x%x wait:x%x",
10033 phba->work_ha, work_ha_copy,
10034 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010035 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010036
James Smart858c9f62007-06-17 19:56:39 -050010037 control &=
10038 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010039 writel(control, phba->HCregaddr);
10040 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010041 }
James Smarta58cbd52007-08-02 11:09:43 -040010042 else {
10043 lpfc_debugfs_slow_ring_trc(phba,
10044 "ISR slow ring: pwork:"
10045 "x%x hawork:x%x wait:x%x",
10046 phba->work_ha, work_ha_copy,
10047 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010048 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010049 }
James Smart5b75da22008-12-04 22:39:35 -050010050 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010051 }
10052 }
James Smart5b75da22008-12-04 22:39:35 -050010053 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010054 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010055 if (lpfc_sli_read_hs(phba))
10056 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010057 /*
10058 * Check if there is a deferred error condition
10059 * is active
10060 */
10061 if ((HS_FFER1 & phba->work_hs) &&
10062 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010063 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10064 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010065 phba->hba_flag |= DEFER_ERATT;
10066 /* Clear all interrupt enable conditions */
10067 writel(0, phba->HCregaddr);
10068 readl(phba->HCregaddr);
10069 }
10070 }
10071
James Smart93996272008-08-24 21:50:30 -040010072 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010073 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010074 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010075 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010076 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010077
10078 /* First check out the status word */
10079 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10080 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010081 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010082 /*
10083 * Stray Mailbox Interrupt, mbxCommand <cmd>
10084 * mbxStatus <status>
10085 */
James Smart09372822008-01-11 01:52:54 -050010086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010087 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010088 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010089 "Interrupt mbxCommand x%x "
10090 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010091 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010092 pmbox->mbxCommand,
10093 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010094 /* clear mailbox attention bit */
10095 work_ha_copy &= ~HA_MBATT;
10096 } else {
James Smart97eab632008-04-07 10:16:05 -040010097 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010098 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010099 phba->last_completion_time = jiffies;
10100 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010101 if (pmb->mbox_cmpl) {
10102 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10103 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010104 if (pmb->out_ext_byte_len &&
10105 pmb->context2)
10106 lpfc_sli_pcimem_bcopy(
10107 phba->mbox_ext,
10108 pmb->context2,
10109 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010110 }
James Smart09372822008-01-11 01:52:54 -050010111 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10112 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10113
10114 lpfc_debugfs_disc_trc(vport,
10115 LPFC_DISC_TRC_MBOX_VPORT,
10116 "MBOX dflt rpi: : "
10117 "status:x%x rpi:x%x",
10118 (uint32_t)pmbox->mbxStatus,
10119 pmbox->un.varWords[0], 0);
10120
10121 if (!pmbox->mbxStatus) {
10122 mp = (struct lpfc_dmabuf *)
10123 (pmb->context1);
10124 ndlp = (struct lpfc_nodelist *)
10125 pmb->context2;
10126
10127 /* Reg_LOGIN of dflt RPI was
10128 * successful. new lets get
10129 * rid of the RPI using the
10130 * same mbox buffer.
10131 */
10132 lpfc_unreg_login(phba,
10133 vport->vpi,
10134 pmbox->un.varWords[0],
10135 pmb);
10136 pmb->mbox_cmpl =
10137 lpfc_mbx_cmpl_dflt_rpi;
10138 pmb->context1 = mp;
10139 pmb->context2 = ndlp;
10140 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010141 rc = lpfc_sli_issue_mbox(phba,
10142 pmb,
10143 MBX_NOWAIT);
10144 if (rc != MBX_BUSY)
10145 lpfc_printf_log(phba,
10146 KERN_ERR,
10147 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010148 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010149 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010150 if (rc != MBX_NOT_FINISHED)
10151 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010152 }
10153 }
James Smart5b75da22008-12-04 22:39:35 -050010154 spin_lock_irqsave(
10155 &phba->pport->work_port_lock,
10156 iflag);
James Smart09372822008-01-11 01:52:54 -050010157 phba->pport->work_port_events &=
10158 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010159 spin_unlock_irqrestore(
10160 &phba->pport->work_port_lock,
10161 iflag);
James Smart09372822008-01-11 01:52:54 -050010162 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010163 }
James Smart97eab632008-04-07 10:16:05 -040010164 } else
James Smart5b75da22008-12-04 22:39:35 -050010165 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010166
James Smart92d7f7b2007-06-17 19:56:38 -050010167 if ((work_ha_copy & HA_MBATT) &&
10168 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010169send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010170 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010171 do {
10172 rc = lpfc_sli_issue_mbox(phba, NULL,
10173 MBX_NOWAIT);
10174 } while (rc == MBX_NOT_FINISHED);
10175 if (rc != MBX_SUCCESS)
10176 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10177 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010178 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010179 }
10180
James Smart5b75da22008-12-04 22:39:35 -050010181 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010182 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010183 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010184 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010185 }
James Smart93996272008-08-24 21:50:30 -040010186 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010187unplug_error:
10188 spin_unlock_irqrestore(&phba->hbalock, iflag);
10189 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010190
James Smart3772a992009-05-22 14:50:54 -040010191} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010192
10193/**
James Smart3772a992009-05-22 14:50:54 -040010194 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010195 * @irq: Interrupt number.
10196 * @dev_id: The device context pointer.
10197 *
10198 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010199 * service routine when device with SLI-3 interface spec is enabled with
10200 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10201 * ring event in the HBA. However, when the device is enabled with either
10202 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10203 * device-level interrupt handler. When the PCI slot is in error recovery
10204 * or the HBA is undergoing initialization, the interrupt handler will not
10205 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10206 * the intrrupt context. This function is called without any lock held.
10207 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010208 *
10209 * This function returns IRQ_HANDLED when interrupt is handled else it
10210 * returns IRQ_NONE.
10211 **/
10212irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010213lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010214{
10215 struct lpfc_hba *phba;
10216 uint32_t ha_copy;
10217 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010218 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010219
10220 /* Get the driver's phba structure from the dev_id and
10221 * assume the HBA is not interrupting.
10222 */
10223 phba = (struct lpfc_hba *) dev_id;
10224
10225 if (unlikely(!phba))
10226 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010227
10228 /*
James Smart93996272008-08-24 21:50:30 -040010229 * Stuff needs to be attented to when this function is invoked as an
10230 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010231 */
James Smart93996272008-08-24 21:50:30 -040010232 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010233 /* Check device state for handling interrupt */
10234 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010235 return IRQ_NONE;
10236 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010237 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10238 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010239 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010240 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010241 /*
10242 * If there is deferred error attention, do not check for
10243 * any interrupt.
10244 */
10245 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010246 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010247 return IRQ_NONE;
10248 }
James Smart93996272008-08-24 21:50:30 -040010249 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10250 phba->HAregaddr);
10251 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010252 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010253 } else
10254 ha_copy = phba->ha_copy;
10255
10256 /*
10257 * Process all events on FCP ring. Take the optimized path for FCP IO.
10258 */
10259 ha_copy &= ~(phba->work_ha_mask);
10260
10261 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010262 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010263 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010264 lpfc_sli_handle_fast_ring_event(phba,
10265 &phba->sli.ring[LPFC_FCP_RING],
10266 status);
James Smarta4bc3372006-12-02 13:34:16 -050010267
10268 if (phba->cfg_multi_ring_support == 2) {
10269 /*
James Smart93996272008-08-24 21:50:30 -040010270 * Process all events on extra ring. Take the optimized path
10271 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010272 */
James Smart93996272008-08-24 21:50:30 -040010273 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010274 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010275 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010276 lpfc_sli_handle_fast_ring_event(phba,
10277 &phba->sli.ring[LPFC_EXTRA_RING],
10278 status);
10279 }
10280 }
dea31012005-04-17 16:05:31 -050010281 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010282} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010283
James Smart93996272008-08-24 21:50:30 -040010284/**
James Smart3772a992009-05-22 14:50:54 -040010285 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010286 * @irq: Interrupt number.
10287 * @dev_id: The device context pointer.
10288 *
James Smart3772a992009-05-22 14:50:54 -040010289 * This function is the HBA device-level interrupt handler to device with
10290 * SLI-3 interface spec, called from the PCI layer when either MSI or
10291 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10292 * requires driver attention. This function invokes the slow-path interrupt
10293 * attention handling function and fast-path interrupt attention handling
10294 * function in turn to process the relevant HBA attention events. This
10295 * function is called without any lock held. It gets the hbalock to access
10296 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010297 *
10298 * This function returns IRQ_HANDLED when interrupt is handled, else it
10299 * returns IRQ_NONE.
10300 **/
10301irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010302lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010303{
10304 struct lpfc_hba *phba;
10305 irqreturn_t sp_irq_rc, fp_irq_rc;
10306 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010307 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010308
10309 /*
10310 * Get the driver's phba structure from the dev_id and
10311 * assume the HBA is not interrupting.
10312 */
10313 phba = (struct lpfc_hba *) dev_id;
10314
10315 if (unlikely(!phba))
10316 return IRQ_NONE;
10317
James Smart3772a992009-05-22 14:50:54 -040010318 /* Check device state for handling interrupt */
10319 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010320 return IRQ_NONE;
10321
10322 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010323 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10324 spin_unlock(&phba->hbalock);
10325 return IRQ_HANDLED;
10326 }
10327
James Smart93996272008-08-24 21:50:30 -040010328 if (unlikely(!phba->ha_copy)) {
10329 spin_unlock(&phba->hbalock);
10330 return IRQ_NONE;
10331 } else if (phba->ha_copy & HA_ERATT) {
10332 if (phba->hba_flag & HBA_ERATT_HANDLED)
10333 /* ERATT polling has handled ERATT */
10334 phba->ha_copy &= ~HA_ERATT;
10335 else
10336 /* Indicate interrupt handler handles ERATT */
10337 phba->hba_flag |= HBA_ERATT_HANDLED;
10338 }
10339
James Smarta257bf92009-04-06 18:48:10 -040010340 /*
10341 * If there is deferred error attention, do not check for any interrupt.
10342 */
10343 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010344 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010345 return IRQ_NONE;
10346 }
10347
James Smart93996272008-08-24 21:50:30 -040010348 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010349 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10350 spin_unlock(&phba->hbalock);
10351 return IRQ_HANDLED;
10352 }
James Smarta747c9c2009-11-18 15:41:10 -050010353 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10354 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10355 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010356 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010357 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010358 readl(phba->HAregaddr); /* flush */
10359 spin_unlock(&phba->hbalock);
10360
10361 /*
10362 * Invokes slow-path host attention interrupt handling as appropriate.
10363 */
10364
10365 /* status of events with mailbox and link attention */
10366 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10367
10368 /* status of events with ELS ring */
10369 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10370 status2 >>= (4*LPFC_ELS_RING);
10371
10372 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010373 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010374 else
10375 sp_irq_rc = IRQ_NONE;
10376
10377 /*
10378 * Invoke fast-path host attention interrupt handling as appropriate.
10379 */
10380
10381 /* status of events with FCP ring */
10382 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10383 status1 >>= (4*LPFC_FCP_RING);
10384
10385 /* status of events with extra ring */
10386 if (phba->cfg_multi_ring_support == 2) {
10387 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10388 status2 >>= (4*LPFC_EXTRA_RING);
10389 } else
10390 status2 = 0;
10391
10392 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010393 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010394 else
10395 fp_irq_rc = IRQ_NONE;
10396
10397 /* Return device-level interrupt handling status */
10398 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010399} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010400
10401/**
10402 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10403 * @phba: pointer to lpfc hba data structure.
10404 *
10405 * This routine is invoked by the worker thread to process all the pending
10406 * SLI4 FCP abort XRI events.
10407 **/
10408void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10409{
10410 struct lpfc_cq_event *cq_event;
10411
10412 /* First, declare the fcp xri abort event has been handled */
10413 spin_lock_irq(&phba->hbalock);
10414 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10415 spin_unlock_irq(&phba->hbalock);
10416 /* Now, handle all the fcp xri abort events */
10417 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10418 /* Get the first event from the head of the event queue */
10419 spin_lock_irq(&phba->hbalock);
10420 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10421 cq_event, struct lpfc_cq_event, list);
10422 spin_unlock_irq(&phba->hbalock);
10423 /* Notify aborted XRI for FCP work queue */
10424 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10425 /* Free the event processed back to the free pool */
10426 lpfc_sli4_cq_event_release(phba, cq_event);
10427 }
10428}
10429
10430/**
10431 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10432 * @phba: pointer to lpfc hba data structure.
10433 *
10434 * This routine is invoked by the worker thread to process all the pending
10435 * SLI4 els abort xri events.
10436 **/
10437void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10438{
10439 struct lpfc_cq_event *cq_event;
10440
10441 /* First, declare the els xri abort event has been handled */
10442 spin_lock_irq(&phba->hbalock);
10443 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10444 spin_unlock_irq(&phba->hbalock);
10445 /* Now, handle all the els xri abort events */
10446 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10447 /* Get the first event from the head of the event queue */
10448 spin_lock_irq(&phba->hbalock);
10449 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10450 cq_event, struct lpfc_cq_event, list);
10451 spin_unlock_irq(&phba->hbalock);
10452 /* Notify aborted XRI for ELS work queue */
10453 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10454 /* Free the event processed back to the free pool */
10455 lpfc_sli4_cq_event_release(phba, cq_event);
10456 }
10457}
10458
James Smart341af102010-01-26 23:07:37 -050010459/**
10460 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10461 * @phba: pointer to lpfc hba data structure
10462 * @pIocbIn: pointer to the rspiocbq
10463 * @pIocbOut: pointer to the cmdiocbq
10464 * @wcqe: pointer to the complete wcqe
10465 *
10466 * This routine transfers the fields of a command iocbq to a response iocbq
10467 * by copying all the IOCB fields from command iocbq and transferring the
10468 * completion status information from the complete wcqe.
10469 **/
James Smart4f774512009-05-22 14:52:35 -040010470static void
James Smart341af102010-01-26 23:07:37 -050010471lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10472 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010473 struct lpfc_iocbq *pIocbOut,
10474 struct lpfc_wcqe_complete *wcqe)
10475{
James Smart341af102010-01-26 23:07:37 -050010476 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010477 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10478
10479 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10480 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010481 /* Map WCQE parameters into irspiocb parameters */
10482 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10483 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10484 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10485 pIocbIn->iocb.un.fcpi.fcpi_parm =
10486 pIocbOut->iocb.un.fcpi.fcpi_parm -
10487 wcqe->total_data_placed;
10488 else
10489 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010490 else {
James Smart4f774512009-05-22 14:52:35 -040010491 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010492 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10493 }
James Smart341af102010-01-26 23:07:37 -050010494
10495 /* Pick up HBA exchange busy condition */
10496 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10497 spin_lock_irqsave(&phba->hbalock, iflags);
10498 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10499 spin_unlock_irqrestore(&phba->hbalock, iflags);
10500 }
James Smart4f774512009-05-22 14:52:35 -040010501}
10502
10503/**
James Smart45ed1192009-10-02 15:17:02 -040010504 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10505 * @phba: Pointer to HBA context object.
10506 * @wcqe: Pointer to work-queue completion queue entry.
10507 *
10508 * This routine handles an ELS work-queue completion event and construct
10509 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10510 * discovery engine to handle.
10511 *
10512 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10513 **/
10514static struct lpfc_iocbq *
10515lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10516 struct lpfc_iocbq *irspiocbq)
10517{
10518 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10519 struct lpfc_iocbq *cmdiocbq;
10520 struct lpfc_wcqe_complete *wcqe;
10521 unsigned long iflags;
10522
10523 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10524 spin_lock_irqsave(&phba->hbalock, iflags);
10525 pring->stats.iocb_event++;
10526 /* Look up the ELS command IOCB and create pseudo response IOCB */
10527 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10528 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10529 spin_unlock_irqrestore(&phba->hbalock, iflags);
10530
10531 if (unlikely(!cmdiocbq)) {
10532 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10533 "0386 ELS complete with no corresponding "
10534 "cmdiocb: iotag (%d)\n",
10535 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10536 lpfc_sli_release_iocbq(phba, irspiocbq);
10537 return NULL;
10538 }
10539
10540 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010541 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010542
10543 return irspiocbq;
10544}
10545
10546/**
James Smart04c68492009-05-22 14:52:52 -040010547 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10548 * @phba: Pointer to HBA context object.
10549 * @cqe: Pointer to mailbox completion queue entry.
10550 *
10551 * This routine process a mailbox completion queue entry with asynchrous
10552 * event.
10553 *
10554 * Return: true if work posted to worker thread, otherwise false.
10555 **/
10556static bool
10557lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10558{
10559 struct lpfc_cq_event *cq_event;
10560 unsigned long iflags;
10561
10562 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10563 "0392 Async Event: word0:x%x, word1:x%x, "
10564 "word2:x%x, word3:x%x\n", mcqe->word0,
10565 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10566
10567 /* Allocate a new internal CQ_EVENT entry */
10568 cq_event = lpfc_sli4_cq_event_alloc(phba);
10569 if (!cq_event) {
10570 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10571 "0394 Failed to allocate CQ_EVENT entry\n");
10572 return false;
10573 }
10574
10575 /* Move the CQE into an asynchronous event entry */
10576 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10577 spin_lock_irqsave(&phba->hbalock, iflags);
10578 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10579 /* Set the async event flag */
10580 phba->hba_flag |= ASYNC_EVENT;
10581 spin_unlock_irqrestore(&phba->hbalock, iflags);
10582
10583 return true;
10584}
10585
10586/**
10587 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10588 * @phba: Pointer to HBA context object.
10589 * @cqe: Pointer to mailbox completion queue entry.
10590 *
10591 * This routine process a mailbox completion queue entry with mailbox
10592 * completion event.
10593 *
10594 * Return: true if work posted to worker thread, otherwise false.
10595 **/
10596static bool
10597lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10598{
10599 uint32_t mcqe_status;
10600 MAILBOX_t *mbox, *pmbox;
10601 struct lpfc_mqe *mqe;
10602 struct lpfc_vport *vport;
10603 struct lpfc_nodelist *ndlp;
10604 struct lpfc_dmabuf *mp;
10605 unsigned long iflags;
10606 LPFC_MBOXQ_t *pmb;
10607 bool workposted = false;
10608 int rc;
10609
10610 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10611 if (!bf_get(lpfc_trailer_completed, mcqe))
10612 goto out_no_mqe_complete;
10613
10614 /* Get the reference to the active mbox command */
10615 spin_lock_irqsave(&phba->hbalock, iflags);
10616 pmb = phba->sli.mbox_active;
10617 if (unlikely(!pmb)) {
10618 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10619 "1832 No pending MBOX command to handle\n");
10620 spin_unlock_irqrestore(&phba->hbalock, iflags);
10621 goto out_no_mqe_complete;
10622 }
10623 spin_unlock_irqrestore(&phba->hbalock, iflags);
10624 mqe = &pmb->u.mqe;
10625 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10626 mbox = phba->mbox;
10627 vport = pmb->vport;
10628
10629 /* Reset heartbeat timer */
10630 phba->last_completion_time = jiffies;
10631 del_timer(&phba->sli.mbox_tmo);
10632
10633 /* Move mbox data to caller's mailbox region, do endian swapping */
10634 if (pmb->mbox_cmpl && mbox)
10635 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010636
James Smart73d91e52011-10-10 21:32:10 -040010637 /*
10638 * For mcqe errors, conditionally move a modified error code to
10639 * the mbox so that the error will not be missed.
10640 */
10641 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10642 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10643 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10644 bf_set(lpfc_mqe_status, mqe,
10645 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10646 }
James Smart04c68492009-05-22 14:52:52 -040010647 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10648 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10649 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10650 "MBOX dflt rpi: status:x%x rpi:x%x",
10651 mcqe_status,
10652 pmbox->un.varWords[0], 0);
10653 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10654 mp = (struct lpfc_dmabuf *)(pmb->context1);
10655 ndlp = (struct lpfc_nodelist *)pmb->context2;
10656 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10657 * RID of the PPI using the same mbox buffer.
10658 */
10659 lpfc_unreg_login(phba, vport->vpi,
10660 pmbox->un.varWords[0], pmb);
10661 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10662 pmb->context1 = mp;
10663 pmb->context2 = ndlp;
10664 pmb->vport = vport;
10665 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10666 if (rc != MBX_BUSY)
10667 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10668 LOG_SLI, "0385 rc should "
10669 "have been MBX_BUSY\n");
10670 if (rc != MBX_NOT_FINISHED)
10671 goto send_current_mbox;
10672 }
10673 }
10674 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10675 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10676 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10677
10678 /* There is mailbox completion work to do */
10679 spin_lock_irqsave(&phba->hbalock, iflags);
10680 __lpfc_mbox_cmpl_put(phba, pmb);
10681 phba->work_ha |= HA_MBATT;
10682 spin_unlock_irqrestore(&phba->hbalock, iflags);
10683 workposted = true;
10684
10685send_current_mbox:
10686 spin_lock_irqsave(&phba->hbalock, iflags);
10687 /* Release the mailbox command posting token */
10688 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10689 /* Setting active mailbox pointer need to be in sync to flag clear */
10690 phba->sli.mbox_active = NULL;
10691 spin_unlock_irqrestore(&phba->hbalock, iflags);
10692 /* Wake up worker thread to post the next pending mailbox command */
10693 lpfc_worker_wake_up(phba);
10694out_no_mqe_complete:
10695 if (bf_get(lpfc_trailer_consumed, mcqe))
10696 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10697 return workposted;
10698}
10699
10700/**
10701 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10702 * @phba: Pointer to HBA context object.
10703 * @cqe: Pointer to mailbox completion queue entry.
10704 *
10705 * This routine process a mailbox completion queue entry, it invokes the
10706 * proper mailbox complete handling or asynchrous event handling routine
10707 * according to the MCQE's async bit.
10708 *
10709 * Return: true if work posted to worker thread, otherwise false.
10710 **/
10711static bool
10712lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10713{
10714 struct lpfc_mcqe mcqe;
10715 bool workposted;
10716
10717 /* Copy the mailbox MCQE and convert endian order as needed */
10718 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10719
10720 /* Invoke the proper event handling routine */
10721 if (!bf_get(lpfc_trailer_async, &mcqe))
10722 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10723 else
10724 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10725 return workposted;
10726}
10727
10728/**
James Smart4f774512009-05-22 14:52:35 -040010729 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10730 * @phba: Pointer to HBA context object.
10731 * @wcqe: Pointer to work-queue completion queue entry.
10732 *
10733 * This routine handles an ELS work-queue completion event.
10734 *
10735 * Return: true if work posted to worker thread, otherwise false.
10736 **/
10737static bool
10738lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10739 struct lpfc_wcqe_complete *wcqe)
10740{
James Smart4f774512009-05-22 14:52:35 -040010741 struct lpfc_iocbq *irspiocbq;
10742 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010743 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010744
James Smart45ed1192009-10-02 15:17:02 -040010745 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010746 irspiocbq = lpfc_sli_get_iocbq(phba);
10747 if (!irspiocbq) {
10748 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010749 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10750 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10751 pring->txq_cnt, phba->iocb_cnt,
10752 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10753 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010754 return false;
James Smart4f774512009-05-22 14:52:35 -040010755 }
James Smart4f774512009-05-22 14:52:35 -040010756
James Smart45ed1192009-10-02 15:17:02 -040010757 /* Save off the slow-path queue event for work thread to process */
10758 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010759 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010760 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010761 &phba->sli4_hba.sp_queue_event);
10762 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010763 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010764
James Smart45ed1192009-10-02 15:17:02 -040010765 return true;
James Smart4f774512009-05-22 14:52:35 -040010766}
10767
10768/**
10769 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10770 * @phba: Pointer to HBA context object.
10771 * @wcqe: Pointer to work-queue completion queue entry.
10772 *
10773 * This routine handles slow-path WQ entry comsumed event by invoking the
10774 * proper WQ release routine to the slow-path WQ.
10775 **/
10776static void
10777lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10778 struct lpfc_wcqe_release *wcqe)
10779{
10780 /* Check for the slow-path ELS work queue */
10781 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10782 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10783 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10784 else
10785 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10786 "2579 Slow-path wqe consume event carries "
10787 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10788 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10789 phba->sli4_hba.els_wq->queue_id);
10790}
10791
10792/**
10793 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10794 * @phba: Pointer to HBA context object.
10795 * @cq: Pointer to a WQ completion queue.
10796 * @wcqe: Pointer to work-queue completion queue entry.
10797 *
10798 * This routine handles an XRI abort event.
10799 *
10800 * Return: true if work posted to worker thread, otherwise false.
10801 **/
10802static bool
10803lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10804 struct lpfc_queue *cq,
10805 struct sli4_wcqe_xri_aborted *wcqe)
10806{
10807 bool workposted = false;
10808 struct lpfc_cq_event *cq_event;
10809 unsigned long iflags;
10810
10811 /* Allocate a new internal CQ_EVENT entry */
10812 cq_event = lpfc_sli4_cq_event_alloc(phba);
10813 if (!cq_event) {
10814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10815 "0602 Failed to allocate CQ_EVENT entry\n");
10816 return false;
10817 }
10818
10819 /* Move the CQE into the proper xri abort event list */
10820 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10821 switch (cq->subtype) {
10822 case LPFC_FCP:
10823 spin_lock_irqsave(&phba->hbalock, iflags);
10824 list_add_tail(&cq_event->list,
10825 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10826 /* Set the fcp xri abort event flag */
10827 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10828 spin_unlock_irqrestore(&phba->hbalock, iflags);
10829 workposted = true;
10830 break;
10831 case LPFC_ELS:
10832 spin_lock_irqsave(&phba->hbalock, iflags);
10833 list_add_tail(&cq_event->list,
10834 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10835 /* Set the els xri abort event flag */
10836 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10837 spin_unlock_irqrestore(&phba->hbalock, iflags);
10838 workposted = true;
10839 break;
10840 default:
10841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10842 "0603 Invalid work queue CQE subtype (x%x)\n",
10843 cq->subtype);
10844 workposted = false;
10845 break;
10846 }
10847 return workposted;
10848}
10849
10850/**
James Smart4d9ab992009-10-02 15:16:39 -040010851 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040010852 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040010853 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010854 *
James Smart4d9ab992009-10-02 15:16:39 -040010855 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010856 *
10857 * Return: true if work posted to worker thread, otherwise false.
10858 **/
10859static bool
James Smart4d9ab992009-10-02 15:16:39 -040010860lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10861{
10862 bool workposted = false;
10863 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10864 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10865 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040010866 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040010867 unsigned long iflags;
10868
James Smart7851fe22011-07-22 18:36:52 -040010869 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10870 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10871 else
10872 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10873 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040010874 goto out;
10875
10876 status = bf_get(lpfc_rcqe_status, rcqe);
10877 switch (status) {
10878 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10880 "2537 Receive Frame Truncated!!\n");
10881 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050010882 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040010883 spin_lock_irqsave(&phba->hbalock, iflags);
10884 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10885 if (!dma_buf) {
10886 spin_unlock_irqrestore(&phba->hbalock, iflags);
10887 goto out;
10888 }
10889 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10890 /* save off the frame for the word thread to process */
10891 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010892 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040010893 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040010894 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040010895 spin_unlock_irqrestore(&phba->hbalock, iflags);
10896 workposted = true;
10897 break;
10898 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10899 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10900 /* Post more buffers if possible */
10901 spin_lock_irqsave(&phba->hbalock, iflags);
10902 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10903 spin_unlock_irqrestore(&phba->hbalock, iflags);
10904 workposted = true;
10905 break;
10906 }
10907out:
10908 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040010909}
10910
10911/**
10912 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10913 * @phba: Pointer to HBA context object.
10914 * @cq: Pointer to the completion queue.
10915 * @wcqe: Pointer to a completion queue entry.
10916 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010917 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040010918 * entry.
10919 *
10920 * Return: true if work posted to worker thread, otherwise false.
10921 **/
10922static bool
10923lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040010924 struct lpfc_cqe *cqe)
10925{
James Smart45ed1192009-10-02 15:17:02 -040010926 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040010927 bool workposted = false;
10928
10929 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040010930 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040010931
10932 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040010933 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040010934 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040010935 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040010936 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010937 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010938 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010939 break;
10940 case CQE_CODE_RELEASE_WQE:
10941 /* Process the WQ release event */
10942 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010943 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010944 break;
10945 case CQE_CODE_XRI_ABORTED:
10946 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010947 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010948 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040010949 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010950 break;
James Smart4d9ab992009-10-02 15:16:39 -040010951 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040010952 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040010953 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040010954 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040010955 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010956 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040010957 break;
James Smart4f774512009-05-22 14:52:35 -040010958 default:
10959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10960 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040010961 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040010962 break;
10963 }
10964 return workposted;
10965}
10966
10967/**
James Smart4f774512009-05-22 14:52:35 -040010968 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10969 * @phba: Pointer to HBA context object.
10970 * @eqe: Pointer to fast-path event queue entry.
10971 *
10972 * This routine process a event queue entry from the slow-path event queue.
10973 * It will check the MajorCode and MinorCode to determine this is for a
10974 * completion event on a completion queue, if not, an error shall be logged
10975 * and just return. Otherwise, it will get to the corresponding completion
10976 * queue and process all the entries on that completion queue, rearm the
10977 * completion queue, and then return.
10978 *
10979 **/
10980static void
10981lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10982{
10983 struct lpfc_queue *cq = NULL, *childq, *speq;
10984 struct lpfc_cqe *cqe;
10985 bool workposted = false;
10986 int ecount = 0;
10987 uint16_t cqid;
10988
James Smartcb5172e2010-03-15 11:25:07 -040010989 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040010990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10991 "0359 Not a valid slow-path completion "
10992 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010993 bf_get_le32(lpfc_eqe_major_code, eqe),
10994 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010995 return;
10996 }
10997
10998 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010999 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011000
11001 /* Search for completion queue pointer matching this cqid */
11002 speq = phba->sli4_hba.sp_eq;
11003 list_for_each_entry(childq, &speq->child_list, list) {
11004 if (childq->queue_id == cqid) {
11005 cq = childq;
11006 break;
11007 }
11008 }
11009 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011010 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11012 "0365 Slow-path CQ identifier "
11013 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011014 return;
11015 }
11016
11017 /* Process all the entries to the CQ */
11018 switch (cq->type) {
11019 case LPFC_MCQ:
11020 while ((cqe = lpfc_sli4_cq_get(cq))) {
11021 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011022 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011023 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11024 }
11025 break;
11026 case LPFC_WCQ:
11027 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011028 if (cq->subtype == LPFC_FCP)
11029 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11030 cqe);
11031 else
11032 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11033 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011034 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011035 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11036 }
11037 break;
11038 default:
11039 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11040 "0370 Invalid completion queue type (%d)\n",
11041 cq->type);
11042 return;
11043 }
11044
11045 /* Catch the no cq entry condition, log an error */
11046 if (unlikely(ecount == 0))
11047 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11048 "0371 No entry from the CQ: identifier "
11049 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11050
11051 /* In any case, flash and re-arm the RCQ */
11052 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11053
11054 /* wake up worker thread if there are works to be done */
11055 if (workposted)
11056 lpfc_worker_wake_up(phba);
11057}
11058
11059/**
11060 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11061 * @eqe: Pointer to fast-path completion queue entry.
11062 *
11063 * This routine process a fast-path work queue completion entry from fast-path
11064 * event queue for FCP command response completion.
11065 **/
11066static void
11067lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11068 struct lpfc_wcqe_complete *wcqe)
11069{
11070 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11071 struct lpfc_iocbq *cmdiocbq;
11072 struct lpfc_iocbq irspiocbq;
11073 unsigned long iflags;
11074
11075 spin_lock_irqsave(&phba->hbalock, iflags);
11076 pring->stats.iocb_event++;
11077 spin_unlock_irqrestore(&phba->hbalock, iflags);
11078
11079 /* Check for response status */
11080 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11081 /* If resource errors reported from HBA, reduce queue
11082 * depth of the SCSI device.
11083 */
11084 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11085 IOSTAT_LOCAL_REJECT) &&
11086 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11087 phba->lpfc_rampdown_queue_depth(phba);
11088 }
11089 /* Log the error status */
11090 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11091 "0373 FCP complete error: status=x%x, "
11092 "hw_status=x%x, total_data_specified=%d, "
11093 "parameter=x%x, word3=x%x\n",
11094 bf_get(lpfc_wcqe_c_status, wcqe),
11095 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11096 wcqe->total_data_placed, wcqe->parameter,
11097 wcqe->word3);
11098 }
11099
11100 /* Look up the FCP command IOCB and create pseudo response IOCB */
11101 spin_lock_irqsave(&phba->hbalock, iflags);
11102 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11103 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11104 spin_unlock_irqrestore(&phba->hbalock, iflags);
11105 if (unlikely(!cmdiocbq)) {
11106 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11107 "0374 FCP complete with no corresponding "
11108 "cmdiocb: iotag (%d)\n",
11109 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11110 return;
11111 }
11112 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11113 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11114 "0375 FCP cmdiocb not callback function "
11115 "iotag: (%d)\n",
11116 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11117 return;
11118 }
11119
11120 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011121 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011122
James Smart0f65ff62010-02-26 14:14:23 -050011123 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11124 spin_lock_irqsave(&phba->hbalock, iflags);
11125 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11126 spin_unlock_irqrestore(&phba->hbalock, iflags);
11127 }
11128
James Smart4f774512009-05-22 14:52:35 -040011129 /* Pass the cmd_iocb and the rsp state to the upper layer */
11130 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11131}
11132
11133/**
11134 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11135 * @phba: Pointer to HBA context object.
11136 * @cq: Pointer to completion queue.
11137 * @wcqe: Pointer to work-queue completion queue entry.
11138 *
11139 * This routine handles an fast-path WQ entry comsumed event by invoking the
11140 * proper WQ release routine to the slow-path WQ.
11141 **/
11142static void
11143lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11144 struct lpfc_wcqe_release *wcqe)
11145{
11146 struct lpfc_queue *childwq;
11147 bool wqid_matched = false;
11148 uint16_t fcp_wqid;
11149
11150 /* Check for fast-path FCP work queue release */
11151 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11152 list_for_each_entry(childwq, &cq->child_list, list) {
11153 if (childwq->queue_id == fcp_wqid) {
11154 lpfc_sli4_wq_release(childwq,
11155 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11156 wqid_matched = true;
11157 break;
11158 }
11159 }
11160 /* Report warning log message if no match found */
11161 if (wqid_matched != true)
11162 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11163 "2580 Fast-path wqe consume event carries "
11164 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11165}
11166
11167/**
11168 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11169 * @cq: Pointer to the completion queue.
11170 * @eqe: Pointer to fast-path completion queue entry.
11171 *
11172 * This routine process a fast-path work queue completion entry from fast-path
11173 * event queue for FCP command response completion.
11174 **/
11175static int
11176lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11177 struct lpfc_cqe *cqe)
11178{
11179 struct lpfc_wcqe_release wcqe;
11180 bool workposted = false;
11181
11182 /* Copy the work queue CQE and convert endian order if needed */
11183 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11184
11185 /* Check and process for different type of WCQE and dispatch */
11186 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11187 case CQE_CODE_COMPL_WQE:
11188 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011189 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011190 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11191 (struct lpfc_wcqe_complete *)&wcqe);
11192 break;
11193 case CQE_CODE_RELEASE_WQE:
11194 /* Process the WQ release event */
11195 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11196 (struct lpfc_wcqe_release *)&wcqe);
11197 break;
11198 case CQE_CODE_XRI_ABORTED:
11199 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011200 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011201 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11202 (struct sli4_wcqe_xri_aborted *)&wcqe);
11203 break;
11204 default:
11205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11206 "0144 Not a valid WCQE code: x%x\n",
11207 bf_get(lpfc_wcqe_c_code, &wcqe));
11208 break;
11209 }
11210 return workposted;
11211}
11212
11213/**
11214 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11215 * @phba: Pointer to HBA context object.
11216 * @eqe: Pointer to fast-path event queue entry.
11217 *
11218 * This routine process a event queue entry from the fast-path event queue.
11219 * It will check the MajorCode and MinorCode to determine this is for a
11220 * completion event on a completion queue, if not, an error shall be logged
11221 * and just return. Otherwise, it will get to the corresponding completion
11222 * queue and process all the entries on the completion queue, rearm the
11223 * completion queue, and then return.
11224 **/
11225static void
11226lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11227 uint32_t fcp_cqidx)
11228{
11229 struct lpfc_queue *cq;
11230 struct lpfc_cqe *cqe;
11231 bool workposted = false;
11232 uint16_t cqid;
11233 int ecount = 0;
11234
James Smartcb5172e2010-03-15 11:25:07 -040011235 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011236 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11237 "0366 Not a valid fast-path completion "
11238 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011239 bf_get_le32(lpfc_eqe_major_code, eqe),
11240 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011241 return;
11242 }
11243
11244 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11245 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011246 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11248 "0367 Fast-path completion queue "
11249 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040011250 return;
11251 }
11252
11253 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011254 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011255 if (unlikely(cqid != cq->queue_id)) {
11256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11257 "0368 Miss-matched fast-path completion "
11258 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11259 cqid, cq->queue_id);
11260 return;
11261 }
11262
11263 /* Process all the entries to the CQ */
11264 while ((cqe = lpfc_sli4_cq_get(cq))) {
11265 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011266 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011267 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11268 }
11269
11270 /* Catch the no cq entry condition */
11271 if (unlikely(ecount == 0))
11272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11273 "0369 No entry from fast-path completion "
11274 "queue fcpcqid=%d\n", cq->queue_id);
11275
11276 /* In any case, flash and re-arm the CQ */
11277 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11278
11279 /* wake up worker thread if there are works to be done */
11280 if (workposted)
11281 lpfc_worker_wake_up(phba);
11282}
11283
11284static void
11285lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11286{
11287 struct lpfc_eqe *eqe;
11288
11289 /* walk all the EQ entries and drop on the floor */
11290 while ((eqe = lpfc_sli4_eq_get(eq)))
11291 ;
11292
11293 /* Clear and re-arm the EQ */
11294 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11295}
11296
11297/**
11298 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11299 * @irq: Interrupt number.
11300 * @dev_id: The device context pointer.
11301 *
11302 * This function is directly called from the PCI layer as an interrupt
11303 * service routine when device with SLI-4 interface spec is enabled with
11304 * MSI-X multi-message interrupt mode and there are slow-path events in
11305 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11306 * interrupt mode, this function is called as part of the device-level
11307 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11308 * undergoing initialization, the interrupt handler will not process the
11309 * interrupt. The link attention and ELS ring attention events are handled
11310 * by the worker thread. The interrupt handler signals the worker thread
11311 * and returns for these events. This function is called without any lock
11312 * held. It gets the hbalock to access and update SLI data structures.
11313 *
11314 * This function returns IRQ_HANDLED when interrupt is handled else it
11315 * returns IRQ_NONE.
11316 **/
11317irqreturn_t
11318lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11319{
11320 struct lpfc_hba *phba;
11321 struct lpfc_queue *speq;
11322 struct lpfc_eqe *eqe;
11323 unsigned long iflag;
11324 int ecount = 0;
11325
11326 /*
11327 * Get the driver's phba structure from the dev_id
11328 */
11329 phba = (struct lpfc_hba *)dev_id;
11330
11331 if (unlikely(!phba))
11332 return IRQ_NONE;
11333
11334 /* Get to the EQ struct associated with this vector */
11335 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011336 if (unlikely(!speq))
11337 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011338
11339 /* Check device state for handling interrupt */
11340 if (unlikely(lpfc_intr_state_check(phba))) {
11341 /* Check again for link_state with lock held */
11342 spin_lock_irqsave(&phba->hbalock, iflag);
11343 if (phba->link_state < LPFC_LINK_DOWN)
11344 /* Flush, clear interrupt, and rearm the EQ */
11345 lpfc_sli4_eq_flush(phba, speq);
11346 spin_unlock_irqrestore(&phba->hbalock, iflag);
11347 return IRQ_NONE;
11348 }
11349
11350 /*
11351 * Process all the event on FCP slow-path EQ
11352 */
11353 while ((eqe = lpfc_sli4_eq_get(speq))) {
11354 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011355 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011356 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11357 }
11358
11359 /* Always clear and re-arm the slow-path EQ */
11360 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11361
11362 /* Catch the no cq entry condition */
11363 if (unlikely(ecount == 0)) {
11364 if (phba->intr_type == MSIX)
11365 /* MSI-X treated interrupt served as no EQ share INT */
11366 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11367 "0357 MSI-X interrupt with no EQE\n");
11368 else
11369 /* Non MSI-X treated on interrupt as EQ share INT */
11370 return IRQ_NONE;
11371 }
11372
11373 return IRQ_HANDLED;
11374} /* lpfc_sli4_sp_intr_handler */
11375
11376/**
11377 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11378 * @irq: Interrupt number.
11379 * @dev_id: The device context pointer.
11380 *
11381 * This function is directly called from the PCI layer as an interrupt
11382 * service routine when device with SLI-4 interface spec is enabled with
11383 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11384 * ring event in the HBA. However, when the device is enabled with either
11385 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11386 * device-level interrupt handler. When the PCI slot is in error recovery
11387 * or the HBA is undergoing initialization, the interrupt handler will not
11388 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11389 * the intrrupt context. This function is called without any lock held.
11390 * It gets the hbalock to access and update SLI data structures. Note that,
11391 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11392 * equal to that of FCP CQ index.
11393 *
11394 * This function returns IRQ_HANDLED when interrupt is handled else it
11395 * returns IRQ_NONE.
11396 **/
11397irqreturn_t
11398lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11399{
11400 struct lpfc_hba *phba;
11401 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11402 struct lpfc_queue *fpeq;
11403 struct lpfc_eqe *eqe;
11404 unsigned long iflag;
11405 int ecount = 0;
11406 uint32_t fcp_eqidx;
11407
11408 /* Get the driver's phba structure from the dev_id */
11409 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11410 phba = fcp_eq_hdl->phba;
11411 fcp_eqidx = fcp_eq_hdl->idx;
11412
11413 if (unlikely(!phba))
11414 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011415 if (unlikely(!phba->sli4_hba.fp_eq))
11416 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011417
11418 /* Get to the EQ struct associated with this vector */
11419 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11420
11421 /* Check device state for handling interrupt */
11422 if (unlikely(lpfc_intr_state_check(phba))) {
11423 /* Check again for link_state with lock held */
11424 spin_lock_irqsave(&phba->hbalock, iflag);
11425 if (phba->link_state < LPFC_LINK_DOWN)
11426 /* Flush, clear interrupt, and rearm the EQ */
11427 lpfc_sli4_eq_flush(phba, fpeq);
11428 spin_unlock_irqrestore(&phba->hbalock, iflag);
11429 return IRQ_NONE;
11430 }
11431
11432 /*
11433 * Process all the event on FCP fast-path EQ
11434 */
11435 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11436 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011437 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011438 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11439 }
11440
11441 /* Always clear and re-arm the fast-path EQ */
11442 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11443
11444 if (unlikely(ecount == 0)) {
11445 if (phba->intr_type == MSIX)
11446 /* MSI-X treated interrupt served as no EQ share INT */
11447 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11448 "0358 MSI-X interrupt with no EQE\n");
11449 else
11450 /* Non MSI-X treated on interrupt as EQ share INT */
11451 return IRQ_NONE;
11452 }
11453
11454 return IRQ_HANDLED;
11455} /* lpfc_sli4_fp_intr_handler */
11456
11457/**
11458 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11459 * @irq: Interrupt number.
11460 * @dev_id: The device context pointer.
11461 *
11462 * This function is the device-level interrupt handler to device with SLI-4
11463 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11464 * interrupt mode is enabled and there is an event in the HBA which requires
11465 * driver attention. This function invokes the slow-path interrupt attention
11466 * handling function and fast-path interrupt attention handling function in
11467 * turn to process the relevant HBA attention events. This function is called
11468 * without any lock held. It gets the hbalock to access and update SLI data
11469 * structures.
11470 *
11471 * This function returns IRQ_HANDLED when interrupt is handled, else it
11472 * returns IRQ_NONE.
11473 **/
11474irqreturn_t
11475lpfc_sli4_intr_handler(int irq, void *dev_id)
11476{
11477 struct lpfc_hba *phba;
11478 irqreturn_t sp_irq_rc, fp_irq_rc;
11479 bool fp_handled = false;
11480 uint32_t fcp_eqidx;
11481
11482 /* Get the driver's phba structure from the dev_id */
11483 phba = (struct lpfc_hba *)dev_id;
11484
11485 if (unlikely(!phba))
11486 return IRQ_NONE;
11487
11488 /*
11489 * Invokes slow-path host attention interrupt handling as appropriate.
11490 */
11491 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11492
11493 /*
11494 * Invoke fast-path host attention interrupt handling as appropriate.
11495 */
11496 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11497 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11498 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11499 if (fp_irq_rc == IRQ_HANDLED)
11500 fp_handled |= true;
11501 }
11502
11503 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11504} /* lpfc_sli4_intr_handler */
11505
11506/**
11507 * lpfc_sli4_queue_free - free a queue structure and associated memory
11508 * @queue: The queue structure to free.
11509 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011510 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011511 * the host resident queue. This function must be called after destroying the
11512 * queue on the HBA.
11513 **/
11514void
11515lpfc_sli4_queue_free(struct lpfc_queue *queue)
11516{
11517 struct lpfc_dmabuf *dmabuf;
11518
11519 if (!queue)
11520 return;
11521
11522 while (!list_empty(&queue->page_list)) {
11523 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11524 list);
James Smart49198b32010-04-06 15:04:33 -040011525 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011526 dmabuf->virt, dmabuf->phys);
11527 kfree(dmabuf);
11528 }
11529 kfree(queue);
11530 return;
11531}
11532
11533/**
11534 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11535 * @phba: The HBA that this queue is being created on.
11536 * @entry_size: The size of each queue entry for this queue.
11537 * @entry count: The number of entries that this queue will handle.
11538 *
11539 * This function allocates a queue structure and the DMAable memory used for
11540 * the host resident queue. This function must be called before creating the
11541 * queue on the HBA.
11542 **/
11543struct lpfc_queue *
11544lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11545 uint32_t entry_count)
11546{
11547 struct lpfc_queue *queue;
11548 struct lpfc_dmabuf *dmabuf;
11549 int x, total_qe_count;
11550 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011551 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011552
James Smartcb5172e2010-03-15 11:25:07 -040011553 if (!phba->sli4_hba.pc_sli4_params.supported)
11554 hw_page_size = SLI4_PAGE_SIZE;
11555
James Smart4f774512009-05-22 14:52:35 -040011556 queue = kzalloc(sizeof(struct lpfc_queue) +
11557 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11558 if (!queue)
11559 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011560 queue->page_count = (ALIGN(entry_size * entry_count,
11561 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011562 INIT_LIST_HEAD(&queue->list);
11563 INIT_LIST_HEAD(&queue->page_list);
11564 INIT_LIST_HEAD(&queue->child_list);
11565 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11566 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11567 if (!dmabuf)
11568 goto out_fail;
11569 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011570 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011571 GFP_KERNEL);
11572 if (!dmabuf->virt) {
11573 kfree(dmabuf);
11574 goto out_fail;
11575 }
James Smartcb5172e2010-03-15 11:25:07 -040011576 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011577 dmabuf->buffer_tag = x;
11578 list_add_tail(&dmabuf->list, &queue->page_list);
11579 /* initialize queue's entry array */
11580 dma_pointer = dmabuf->virt;
11581 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011582 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011583 total_qe_count++, dma_pointer += entry_size) {
11584 queue->qe[total_qe_count].address = dma_pointer;
11585 }
11586 }
11587 queue->entry_size = entry_size;
11588 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011589
11590 /*
11591 * entry_repost is calculated based on the number of entries in the
11592 * queue. This works out except for RQs. If buffers are NOT initially
11593 * posted for every RQE, entry_repost should be adjusted accordingly.
11594 */
11595 queue->entry_repost = (entry_count >> 3);
11596 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11597 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011598 queue->phba = phba;
11599
11600 return queue;
11601out_fail:
11602 lpfc_sli4_queue_free(queue);
11603 return NULL;
11604}
11605
11606/**
11607 * lpfc_eq_create - Create an Event Queue on the HBA
11608 * @phba: HBA structure that indicates port to create a queue on.
11609 * @eq: The queue structure to use to create the event queue.
11610 * @imax: The maximum interrupt per second limit.
11611 *
11612 * This function creates an event queue, as detailed in @eq, on a port,
11613 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11614 *
11615 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11616 * is used to get the entry count and entry size that are necessary to
11617 * determine the number of pages to allocate and use for this queue. This
11618 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11619 * event queue. This function is asynchronous and will wait for the mailbox
11620 * command to finish before continuing.
11621 *
11622 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011623 * memory this function will return -ENOMEM. If the queue create mailbox command
11624 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011625 **/
11626uint32_t
11627lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11628{
11629 struct lpfc_mbx_eq_create *eq_create;
11630 LPFC_MBOXQ_t *mbox;
11631 int rc, length, status = 0;
11632 struct lpfc_dmabuf *dmabuf;
11633 uint32_t shdr_status, shdr_add_status;
11634 union lpfc_sli4_cfg_shdr *shdr;
11635 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011636 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11637
11638 if (!phba->sli4_hba.pc_sli4_params.supported)
11639 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011640
11641 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11642 if (!mbox)
11643 return -ENOMEM;
11644 length = (sizeof(struct lpfc_mbx_eq_create) -
11645 sizeof(struct lpfc_sli4_cfg_mhdr));
11646 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11647 LPFC_MBOX_OPCODE_EQ_CREATE,
11648 length, LPFC_SLI4_MBX_EMBED);
11649 eq_create = &mbox->u.mqe.un.eq_create;
11650 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11651 eq->page_count);
11652 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11653 LPFC_EQE_SIZE);
11654 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11655 /* Calculate delay multiper from maximum interrupt per second */
11656 dmult = LPFC_DMULT_CONST/imax - 1;
11657 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11658 dmult);
11659 switch (eq->entry_count) {
11660 default:
11661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11662 "0360 Unsupported EQ count. (%d)\n",
11663 eq->entry_count);
11664 if (eq->entry_count < 256)
11665 return -EINVAL;
11666 /* otherwise default to smallest count (drop through) */
11667 case 256:
11668 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11669 LPFC_EQ_CNT_256);
11670 break;
11671 case 512:
11672 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11673 LPFC_EQ_CNT_512);
11674 break;
11675 case 1024:
11676 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11677 LPFC_EQ_CNT_1024);
11678 break;
11679 case 2048:
11680 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11681 LPFC_EQ_CNT_2048);
11682 break;
11683 case 4096:
11684 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11685 LPFC_EQ_CNT_4096);
11686 break;
11687 }
11688 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011689 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011690 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11691 putPaddrLow(dmabuf->phys);
11692 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11693 putPaddrHigh(dmabuf->phys);
11694 }
11695 mbox->vport = phba->pport;
11696 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11697 mbox->context1 = NULL;
11698 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11699 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11702 if (shdr_status || shdr_add_status || rc) {
11703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11704 "2500 EQ_CREATE mailbox failed with "
11705 "status x%x add_status x%x, mbx status x%x\n",
11706 shdr_status, shdr_add_status, rc);
11707 status = -ENXIO;
11708 }
11709 eq->type = LPFC_EQ;
11710 eq->subtype = LPFC_NONE;
11711 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11712 if (eq->queue_id == 0xFFFF)
11713 status = -ENXIO;
11714 eq->host_index = 0;
11715 eq->hba_index = 0;
11716
James Smart8fa38512009-07-19 10:01:03 -040011717 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011718 return status;
11719}
11720
11721/**
11722 * lpfc_cq_create - Create a Completion Queue on the HBA
11723 * @phba: HBA structure that indicates port to create a queue on.
11724 * @cq: The queue structure to use to create the completion queue.
11725 * @eq: The event queue to bind this completion queue to.
11726 *
11727 * This function creates a completion queue, as detailed in @wq, on a port,
11728 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11729 *
11730 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11731 * is used to get the entry count and entry size that are necessary to
11732 * determine the number of pages to allocate and use for this queue. The @eq
11733 * is used to indicate which event queue to bind this completion queue to. This
11734 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11735 * completion queue. This function is asynchronous and will wait for the mailbox
11736 * command to finish before continuing.
11737 *
11738 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011739 * memory this function will return -ENOMEM. If the queue create mailbox command
11740 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011741 **/
11742uint32_t
11743lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11744 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11745{
11746 struct lpfc_mbx_cq_create *cq_create;
11747 struct lpfc_dmabuf *dmabuf;
11748 LPFC_MBOXQ_t *mbox;
11749 int rc, length, status = 0;
11750 uint32_t shdr_status, shdr_add_status;
11751 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011752 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11753
11754 if (!phba->sli4_hba.pc_sli4_params.supported)
11755 hw_page_size = SLI4_PAGE_SIZE;
11756
James Smart4f774512009-05-22 14:52:35 -040011757 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11758 if (!mbox)
11759 return -ENOMEM;
11760 length = (sizeof(struct lpfc_mbx_cq_create) -
11761 sizeof(struct lpfc_sli4_cfg_mhdr));
11762 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11763 LPFC_MBOX_OPCODE_CQ_CREATE,
11764 length, LPFC_SLI4_MBX_EMBED);
11765 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011766 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011767 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11768 cq->page_count);
11769 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11770 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011771 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11772 phba->sli4_hba.pc_sli4_params.cqv);
11773 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011774 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11775 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011776 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11777 eq->queue_id);
11778 } else {
11779 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11780 eq->queue_id);
11781 }
James Smart4f774512009-05-22 14:52:35 -040011782 switch (cq->entry_count) {
11783 default:
11784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11785 "0361 Unsupported CQ count. (%d)\n",
11786 cq->entry_count);
11787 if (cq->entry_count < 256)
11788 return -EINVAL;
11789 /* otherwise default to smallest count (drop through) */
11790 case 256:
11791 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11792 LPFC_CQ_CNT_256);
11793 break;
11794 case 512:
11795 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11796 LPFC_CQ_CNT_512);
11797 break;
11798 case 1024:
11799 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11800 LPFC_CQ_CNT_1024);
11801 break;
11802 }
11803 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011804 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011805 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11806 putPaddrLow(dmabuf->phys);
11807 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11808 putPaddrHigh(dmabuf->phys);
11809 }
11810 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11811
11812 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011813 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11814 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11815 if (shdr_status || shdr_add_status || rc) {
11816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11817 "2501 CQ_CREATE mailbox failed with "
11818 "status x%x add_status x%x, mbx status x%x\n",
11819 shdr_status, shdr_add_status, rc);
11820 status = -ENXIO;
11821 goto out;
11822 }
11823 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11824 if (cq->queue_id == 0xFFFF) {
11825 status = -ENXIO;
11826 goto out;
11827 }
11828 /* link the cq onto the parent eq child list */
11829 list_add_tail(&cq->list, &eq->child_list);
11830 /* Set up completion queue's type and subtype */
11831 cq->type = type;
11832 cq->subtype = subtype;
11833 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050011834 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011835 cq->host_index = 0;
11836 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040011837
James Smart8fa38512009-07-19 10:01:03 -040011838out:
11839 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011840 return status;
11841}
11842
11843/**
James Smartb19a0612010-04-06 14:48:51 -040011844 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040011845 * @phba: HBA structure that indicates port to create a queue on.
11846 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040011847 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11848 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040011849 *
James Smartb19a0612010-04-06 14:48:51 -040011850 * This function provides failback (fb) functionality when the
11851 * mq_create_ext fails on older FW generations. It's purpose is identical
11852 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040011853 *
James Smartb19a0612010-04-06 14:48:51 -040011854 * This routine cannot fail as all attributes were previously accessed and
11855 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040011856 **/
James Smartb19a0612010-04-06 14:48:51 -040011857static void
11858lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11859 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040011860{
11861 struct lpfc_mbx_mq_create *mq_create;
11862 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040011863 int length;
James Smart04c68492009-05-22 14:52:52 -040011864
James Smart04c68492009-05-22 14:52:52 -040011865 length = (sizeof(struct lpfc_mbx_mq_create) -
11866 sizeof(struct lpfc_sli4_cfg_mhdr));
11867 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11868 LPFC_MBOX_OPCODE_MQ_CREATE,
11869 length, LPFC_SLI4_MBX_EMBED);
11870 mq_create = &mbox->u.mqe.un.mq_create;
11871 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040011872 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040011873 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040011874 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040011875 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11876 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040011877 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011878 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11879 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040011880 break;
11881 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011882 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11883 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040011884 break;
11885 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011886 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11887 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040011888 break;
11889 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011890 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11891 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040011892 break;
11893 }
11894 list_for_each_entry(dmabuf, &mq->page_list, list) {
11895 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040011896 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040011897 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040011898 putPaddrHigh(dmabuf->phys);
11899 }
11900}
11901
11902/**
11903 * lpfc_mq_create - Create a mailbox Queue on the HBA
11904 * @phba: HBA structure that indicates port to create a queue on.
11905 * @mq: The queue structure to use to create the mailbox queue.
11906 * @cq: The completion queue to associate with this cq.
11907 * @subtype: The queue's subtype.
11908 *
11909 * This function creates a mailbox queue, as detailed in @mq, on a port,
11910 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11911 *
11912 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11913 * is used to get the entry count and entry size that are necessary to
11914 * determine the number of pages to allocate and use for this queue. This
11915 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11916 * mailbox queue. This function is asynchronous and will wait for the mailbox
11917 * command to finish before continuing.
11918 *
11919 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011920 * memory this function will return -ENOMEM. If the queue create mailbox command
11921 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040011922 **/
11923int32_t
11924lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11925 struct lpfc_queue *cq, uint32_t subtype)
11926{
11927 struct lpfc_mbx_mq_create *mq_create;
11928 struct lpfc_mbx_mq_create_ext *mq_create_ext;
11929 struct lpfc_dmabuf *dmabuf;
11930 LPFC_MBOXQ_t *mbox;
11931 int rc, length, status = 0;
11932 uint32_t shdr_status, shdr_add_status;
11933 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011934 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040011935
James Smart49198b32010-04-06 15:04:33 -040011936 if (!phba->sli4_hba.pc_sli4_params.supported)
11937 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040011938
11939 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11940 if (!mbox)
11941 return -ENOMEM;
11942 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11943 sizeof(struct lpfc_sli4_cfg_mhdr));
11944 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11945 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11946 length, LPFC_SLI4_MBX_EMBED);
11947
11948 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050011949 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050011950 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11951 &mq_create_ext->u.request, mq->page_count);
11952 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11953 &mq_create_ext->u.request, 1);
11954 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040011955 &mq_create_ext->u.request, 1);
11956 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11957 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050011958 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11959 &mq_create_ext->u.request, 1);
11960 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11961 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040011962 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011963 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11964 phba->sli4_hba.pc_sli4_params.mqv);
11965 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11966 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11967 cq->queue_id);
11968 else
11969 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11970 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040011971 switch (mq->entry_count) {
11972 default:
11973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11974 "0362 Unsupported MQ count. (%d)\n",
11975 mq->entry_count);
11976 if (mq->entry_count < 16)
11977 return -EINVAL;
11978 /* otherwise default to smallest count (drop through) */
11979 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011980 bf_set(lpfc_mq_context_ring_size,
11981 &mq_create_ext->u.request.context,
11982 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040011983 break;
11984 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011985 bf_set(lpfc_mq_context_ring_size,
11986 &mq_create_ext->u.request.context,
11987 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040011988 break;
11989 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011990 bf_set(lpfc_mq_context_ring_size,
11991 &mq_create_ext->u.request.context,
11992 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040011993 break;
11994 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011995 bf_set(lpfc_mq_context_ring_size,
11996 &mq_create_ext->u.request.context,
11997 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040011998 break;
11999 }
12000 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012001 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012002 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12003 putPaddrLow(dmabuf->phys);
12004 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012005 putPaddrHigh(dmabuf->phys);
12006 }
12007 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012008 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12009 &mq_create_ext->u.response);
12010 if (rc != MBX_SUCCESS) {
12011 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12012 "2795 MQ_CREATE_EXT failed with "
12013 "status x%x. Failback to MQ_CREATE.\n",
12014 rc);
12015 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12016 mq_create = &mbox->u.mqe.un.mq_create;
12017 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12018 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12019 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12020 &mq_create->u.response);
12021 }
12022
James Smart04c68492009-05-22 14:52:52 -040012023 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012024 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12025 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12026 if (shdr_status || shdr_add_status || rc) {
12027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12028 "2502 MQ_CREATE mailbox failed with "
12029 "status x%x add_status x%x, mbx status x%x\n",
12030 shdr_status, shdr_add_status, rc);
12031 status = -ENXIO;
12032 goto out;
12033 }
James Smart04c68492009-05-22 14:52:52 -040012034 if (mq->queue_id == 0xFFFF) {
12035 status = -ENXIO;
12036 goto out;
12037 }
12038 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012039 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012040 mq->subtype = subtype;
12041 mq->host_index = 0;
12042 mq->hba_index = 0;
12043
12044 /* link the mq onto the parent cq child list */
12045 list_add_tail(&mq->list, &cq->child_list);
12046out:
James Smart8fa38512009-07-19 10:01:03 -040012047 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012048 return status;
12049}
12050
12051/**
James Smart4f774512009-05-22 14:52:35 -040012052 * lpfc_wq_create - Create a Work Queue on the HBA
12053 * @phba: HBA structure that indicates port to create a queue on.
12054 * @wq: The queue structure to use to create the work queue.
12055 * @cq: The completion queue to bind this work queue to.
12056 * @subtype: The subtype of the work queue indicating its functionality.
12057 *
12058 * This function creates a work queue, as detailed in @wq, on a port, described
12059 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12060 *
12061 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12062 * is used to get the entry count and entry size that are necessary to
12063 * determine the number of pages to allocate and use for this queue. The @cq
12064 * is used to indicate which completion queue to bind this work queue to. This
12065 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12066 * work queue. This function is asynchronous and will wait for the mailbox
12067 * command to finish before continuing.
12068 *
12069 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012070 * memory this function will return -ENOMEM. If the queue create mailbox command
12071 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012072 **/
12073uint32_t
12074lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12075 struct lpfc_queue *cq, uint32_t subtype)
12076{
12077 struct lpfc_mbx_wq_create *wq_create;
12078 struct lpfc_dmabuf *dmabuf;
12079 LPFC_MBOXQ_t *mbox;
12080 int rc, length, status = 0;
12081 uint32_t shdr_status, shdr_add_status;
12082 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012083 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012084 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012085
12086 if (!phba->sli4_hba.pc_sli4_params.supported)
12087 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012088
12089 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12090 if (!mbox)
12091 return -ENOMEM;
12092 length = (sizeof(struct lpfc_mbx_wq_create) -
12093 sizeof(struct lpfc_sli4_cfg_mhdr));
12094 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12095 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12096 length, LPFC_SLI4_MBX_EMBED);
12097 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012098 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012099 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12100 wq->page_count);
12101 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12102 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012103 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12104 phba->sli4_hba.pc_sli4_params.wqv);
12105 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12106 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12107 wq->entry_count);
12108 switch (wq->entry_size) {
12109 default:
12110 case 64:
12111 bf_set(lpfc_mbx_wq_create_wqe_size,
12112 &wq_create->u.request_1,
12113 LPFC_WQ_WQE_SIZE_64);
12114 break;
12115 case 128:
12116 bf_set(lpfc_mbx_wq_create_wqe_size,
12117 &wq_create->u.request_1,
12118 LPFC_WQ_WQE_SIZE_128);
12119 break;
12120 }
12121 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12122 (PAGE_SIZE/SLI4_PAGE_SIZE));
12123 page = wq_create->u.request_1.page;
12124 } else {
12125 page = wq_create->u.request.page;
12126 }
James Smart4f774512009-05-22 14:52:35 -040012127 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012128 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012129 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12130 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012131 }
12132 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12133 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012134 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12135 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12136 if (shdr_status || shdr_add_status || rc) {
12137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12138 "2503 WQ_CREATE mailbox failed with "
12139 "status x%x add_status x%x, mbx status x%x\n",
12140 shdr_status, shdr_add_status, rc);
12141 status = -ENXIO;
12142 goto out;
12143 }
12144 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12145 if (wq->queue_id == 0xFFFF) {
12146 status = -ENXIO;
12147 goto out;
12148 }
12149 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012150 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012151 wq->subtype = subtype;
12152 wq->host_index = 0;
12153 wq->hba_index = 0;
12154
12155 /* link the wq onto the parent cq child list */
12156 list_add_tail(&wq->list, &cq->child_list);
12157out:
James Smart8fa38512009-07-19 10:01:03 -040012158 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012159 return status;
12160}
12161
12162/**
James Smart73d91e52011-10-10 21:32:10 -040012163 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12164 * @phba: HBA structure that indicates port to create a queue on.
12165 * @rq: The queue structure to use for the receive queue.
12166 * @qno: The associated HBQ number
12167 *
12168 *
12169 * For SLI4 we need to adjust the RQ repost value based on
12170 * the number of buffers that are initially posted to the RQ.
12171 */
12172void
12173lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12174{
12175 uint32_t cnt;
12176
12177 cnt = lpfc_hbq_defs[qno]->entry_count;
12178
12179 /* Recalc repost for RQs based on buffers initially posted */
12180 cnt = (cnt >> 3);
12181 if (cnt < LPFC_QUEUE_MIN_REPOST)
12182 cnt = LPFC_QUEUE_MIN_REPOST;
12183
12184 rq->entry_repost = cnt;
12185}
12186
12187/**
James Smart4f774512009-05-22 14:52:35 -040012188 * lpfc_rq_create - Create a Receive Queue on the HBA
12189 * @phba: HBA structure that indicates port to create a queue on.
12190 * @hrq: The queue structure to use to create the header receive queue.
12191 * @drq: The queue structure to use to create the data receive queue.
12192 * @cq: The completion queue to bind this work queue to.
12193 *
12194 * This function creates a receive buffer queue pair , as detailed in @hrq and
12195 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12196 * to the HBA.
12197 *
12198 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12199 * struct is used to get the entry count that is necessary to determine the
12200 * number of pages to use for this queue. The @cq is used to indicate which
12201 * completion queue to bind received buffers that are posted to these queues to.
12202 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12203 * receive queue pair. This function is asynchronous and will wait for the
12204 * mailbox command to finish before continuing.
12205 *
12206 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012207 * memory this function will return -ENOMEM. If the queue create mailbox command
12208 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012209 **/
12210uint32_t
12211lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12212 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12213{
12214 struct lpfc_mbx_rq_create *rq_create;
12215 struct lpfc_dmabuf *dmabuf;
12216 LPFC_MBOXQ_t *mbox;
12217 int rc, length, status = 0;
12218 uint32_t shdr_status, shdr_add_status;
12219 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012220 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12221
12222 if (!phba->sli4_hba.pc_sli4_params.supported)
12223 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012224
12225 if (hrq->entry_count != drq->entry_count)
12226 return -EINVAL;
12227 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12228 if (!mbox)
12229 return -ENOMEM;
12230 length = (sizeof(struct lpfc_mbx_rq_create) -
12231 sizeof(struct lpfc_sli4_cfg_mhdr));
12232 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12233 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12234 length, LPFC_SLI4_MBX_EMBED);
12235 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012236 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12237 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12238 phba->sli4_hba.pc_sli4_params.rqv);
12239 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12240 bf_set(lpfc_rq_context_rqe_count_1,
12241 &rq_create->u.request.context,
12242 hrq->entry_count);
12243 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012244 bf_set(lpfc_rq_context_rqe_size,
12245 &rq_create->u.request.context,
12246 LPFC_RQE_SIZE_8);
12247 bf_set(lpfc_rq_context_page_size,
12248 &rq_create->u.request.context,
12249 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012250 } else {
12251 switch (hrq->entry_count) {
12252 default:
12253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12254 "2535 Unsupported RQ count. (%d)\n",
12255 hrq->entry_count);
12256 if (hrq->entry_count < 512)
12257 return -EINVAL;
12258 /* otherwise default to smallest count (drop through) */
12259 case 512:
12260 bf_set(lpfc_rq_context_rqe_count,
12261 &rq_create->u.request.context,
12262 LPFC_RQ_RING_SIZE_512);
12263 break;
12264 case 1024:
12265 bf_set(lpfc_rq_context_rqe_count,
12266 &rq_create->u.request.context,
12267 LPFC_RQ_RING_SIZE_1024);
12268 break;
12269 case 2048:
12270 bf_set(lpfc_rq_context_rqe_count,
12271 &rq_create->u.request.context,
12272 LPFC_RQ_RING_SIZE_2048);
12273 break;
12274 case 4096:
12275 bf_set(lpfc_rq_context_rqe_count,
12276 &rq_create->u.request.context,
12277 LPFC_RQ_RING_SIZE_4096);
12278 break;
12279 }
12280 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12281 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012282 }
12283 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12284 cq->queue_id);
12285 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12286 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012287 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012288 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012289 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12290 putPaddrLow(dmabuf->phys);
12291 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12292 putPaddrHigh(dmabuf->phys);
12293 }
12294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12295 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012296 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12297 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12298 if (shdr_status || shdr_add_status || rc) {
12299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12300 "2504 RQ_CREATE mailbox failed with "
12301 "status x%x add_status x%x, mbx status x%x\n",
12302 shdr_status, shdr_add_status, rc);
12303 status = -ENXIO;
12304 goto out;
12305 }
12306 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12307 if (hrq->queue_id == 0xFFFF) {
12308 status = -ENXIO;
12309 goto out;
12310 }
12311 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012312 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012313 hrq->subtype = subtype;
12314 hrq->host_index = 0;
12315 hrq->hba_index = 0;
12316
12317 /* now create the data queue */
12318 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12319 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12320 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012321 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12322 phba->sli4_hba.pc_sli4_params.rqv);
12323 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12324 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012325 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012326 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012327 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12328 LPFC_RQE_SIZE_8);
12329 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12330 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012331 } else {
12332 switch (drq->entry_count) {
12333 default:
12334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12335 "2536 Unsupported RQ count. (%d)\n",
12336 drq->entry_count);
12337 if (drq->entry_count < 512)
12338 return -EINVAL;
12339 /* otherwise default to smallest count (drop through) */
12340 case 512:
12341 bf_set(lpfc_rq_context_rqe_count,
12342 &rq_create->u.request.context,
12343 LPFC_RQ_RING_SIZE_512);
12344 break;
12345 case 1024:
12346 bf_set(lpfc_rq_context_rqe_count,
12347 &rq_create->u.request.context,
12348 LPFC_RQ_RING_SIZE_1024);
12349 break;
12350 case 2048:
12351 bf_set(lpfc_rq_context_rqe_count,
12352 &rq_create->u.request.context,
12353 LPFC_RQ_RING_SIZE_2048);
12354 break;
12355 case 4096:
12356 bf_set(lpfc_rq_context_rqe_count,
12357 &rq_create->u.request.context,
12358 LPFC_RQ_RING_SIZE_4096);
12359 break;
12360 }
12361 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12362 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012363 }
12364 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12365 cq->queue_id);
12366 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12367 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012368 list_for_each_entry(dmabuf, &drq->page_list, list) {
12369 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12370 putPaddrLow(dmabuf->phys);
12371 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12372 putPaddrHigh(dmabuf->phys);
12373 }
12374 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12375 /* The IOCTL status is embedded in the mailbox subheader. */
12376 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12377 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12378 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12379 if (shdr_status || shdr_add_status || rc) {
12380 status = -ENXIO;
12381 goto out;
12382 }
12383 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12384 if (drq->queue_id == 0xFFFF) {
12385 status = -ENXIO;
12386 goto out;
12387 }
12388 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012389 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012390 drq->subtype = subtype;
12391 drq->host_index = 0;
12392 drq->hba_index = 0;
12393
12394 /* link the header and data RQs onto the parent cq child list */
12395 list_add_tail(&hrq->list, &cq->child_list);
12396 list_add_tail(&drq->list, &cq->child_list);
12397
12398out:
James Smart8fa38512009-07-19 10:01:03 -040012399 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012400 return status;
12401}
12402
12403/**
12404 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12405 * @eq: The queue structure associated with the queue to destroy.
12406 *
12407 * This function destroys a queue, as detailed in @eq by sending an mailbox
12408 * command, specific to the type of queue, to the HBA.
12409 *
12410 * The @eq struct is used to get the queue ID of the queue to destroy.
12411 *
12412 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012413 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012414 **/
12415uint32_t
12416lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12417{
12418 LPFC_MBOXQ_t *mbox;
12419 int rc, length, status = 0;
12420 uint32_t shdr_status, shdr_add_status;
12421 union lpfc_sli4_cfg_shdr *shdr;
12422
12423 if (!eq)
12424 return -ENODEV;
12425 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12426 if (!mbox)
12427 return -ENOMEM;
12428 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12429 sizeof(struct lpfc_sli4_cfg_mhdr));
12430 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12431 LPFC_MBOX_OPCODE_EQ_DESTROY,
12432 length, LPFC_SLI4_MBX_EMBED);
12433 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12434 eq->queue_id);
12435 mbox->vport = eq->phba->pport;
12436 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12437
12438 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12439 /* The IOCTL status is embedded in the mailbox subheader. */
12440 shdr = (union lpfc_sli4_cfg_shdr *)
12441 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12442 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12443 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12444 if (shdr_status || shdr_add_status || rc) {
12445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12446 "2505 EQ_DESTROY mailbox failed with "
12447 "status x%x add_status x%x, mbx status x%x\n",
12448 shdr_status, shdr_add_status, rc);
12449 status = -ENXIO;
12450 }
12451
12452 /* Remove eq from any list */
12453 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012454 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012455 return status;
12456}
12457
12458/**
12459 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12460 * @cq: The queue structure associated with the queue to destroy.
12461 *
12462 * This function destroys a queue, as detailed in @cq by sending an mailbox
12463 * command, specific to the type of queue, to the HBA.
12464 *
12465 * The @cq struct is used to get the queue ID of the queue to destroy.
12466 *
12467 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012468 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012469 **/
12470uint32_t
12471lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12472{
12473 LPFC_MBOXQ_t *mbox;
12474 int rc, length, status = 0;
12475 uint32_t shdr_status, shdr_add_status;
12476 union lpfc_sli4_cfg_shdr *shdr;
12477
12478 if (!cq)
12479 return -ENODEV;
12480 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12481 if (!mbox)
12482 return -ENOMEM;
12483 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12484 sizeof(struct lpfc_sli4_cfg_mhdr));
12485 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12486 LPFC_MBOX_OPCODE_CQ_DESTROY,
12487 length, LPFC_SLI4_MBX_EMBED);
12488 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12489 cq->queue_id);
12490 mbox->vport = cq->phba->pport;
12491 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12492 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12493 /* The IOCTL status is embedded in the mailbox subheader. */
12494 shdr = (union lpfc_sli4_cfg_shdr *)
12495 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12498 if (shdr_status || shdr_add_status || rc) {
12499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12500 "2506 CQ_DESTROY mailbox failed with "
12501 "status x%x add_status x%x, mbx status x%x\n",
12502 shdr_status, shdr_add_status, rc);
12503 status = -ENXIO;
12504 }
12505 /* Remove cq from any list */
12506 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012507 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012508 return status;
12509}
12510
12511/**
James Smart04c68492009-05-22 14:52:52 -040012512 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12513 * @qm: The queue structure associated with the queue to destroy.
12514 *
12515 * This function destroys a queue, as detailed in @mq by sending an mailbox
12516 * command, specific to the type of queue, to the HBA.
12517 *
12518 * The @mq struct is used to get the queue ID of the queue to destroy.
12519 *
12520 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012521 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012522 **/
12523uint32_t
12524lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12525{
12526 LPFC_MBOXQ_t *mbox;
12527 int rc, length, status = 0;
12528 uint32_t shdr_status, shdr_add_status;
12529 union lpfc_sli4_cfg_shdr *shdr;
12530
12531 if (!mq)
12532 return -ENODEV;
12533 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12534 if (!mbox)
12535 return -ENOMEM;
12536 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12537 sizeof(struct lpfc_sli4_cfg_mhdr));
12538 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12539 LPFC_MBOX_OPCODE_MQ_DESTROY,
12540 length, LPFC_SLI4_MBX_EMBED);
12541 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12542 mq->queue_id);
12543 mbox->vport = mq->phba->pport;
12544 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12545 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12546 /* The IOCTL status is embedded in the mailbox subheader. */
12547 shdr = (union lpfc_sli4_cfg_shdr *)
12548 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12549 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12550 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12551 if (shdr_status || shdr_add_status || rc) {
12552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12553 "2507 MQ_DESTROY mailbox failed with "
12554 "status x%x add_status x%x, mbx status x%x\n",
12555 shdr_status, shdr_add_status, rc);
12556 status = -ENXIO;
12557 }
12558 /* Remove mq from any list */
12559 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012560 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012561 return status;
12562}
12563
12564/**
James Smart4f774512009-05-22 14:52:35 -040012565 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12566 * @wq: The queue structure associated with the queue to destroy.
12567 *
12568 * This function destroys a queue, as detailed in @wq by sending an mailbox
12569 * command, specific to the type of queue, to the HBA.
12570 *
12571 * The @wq struct is used to get the queue ID of the queue to destroy.
12572 *
12573 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012574 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012575 **/
12576uint32_t
12577lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12578{
12579 LPFC_MBOXQ_t *mbox;
12580 int rc, length, status = 0;
12581 uint32_t shdr_status, shdr_add_status;
12582 union lpfc_sli4_cfg_shdr *shdr;
12583
12584 if (!wq)
12585 return -ENODEV;
12586 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12587 if (!mbox)
12588 return -ENOMEM;
12589 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12590 sizeof(struct lpfc_sli4_cfg_mhdr));
12591 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12592 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12593 length, LPFC_SLI4_MBX_EMBED);
12594 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12595 wq->queue_id);
12596 mbox->vport = wq->phba->pport;
12597 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12598 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12599 shdr = (union lpfc_sli4_cfg_shdr *)
12600 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12601 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12602 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12603 if (shdr_status || shdr_add_status || rc) {
12604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12605 "2508 WQ_DESTROY mailbox failed with "
12606 "status x%x add_status x%x, mbx status x%x\n",
12607 shdr_status, shdr_add_status, rc);
12608 status = -ENXIO;
12609 }
12610 /* Remove wq from any list */
12611 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012612 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012613 return status;
12614}
12615
12616/**
12617 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12618 * @rq: The queue structure associated with the queue to destroy.
12619 *
12620 * This function destroys a queue, as detailed in @rq by sending an mailbox
12621 * command, specific to the type of queue, to the HBA.
12622 *
12623 * The @rq struct is used to get the queue ID of the queue to destroy.
12624 *
12625 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012626 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012627 **/
12628uint32_t
12629lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12630 struct lpfc_queue *drq)
12631{
12632 LPFC_MBOXQ_t *mbox;
12633 int rc, length, status = 0;
12634 uint32_t shdr_status, shdr_add_status;
12635 union lpfc_sli4_cfg_shdr *shdr;
12636
12637 if (!hrq || !drq)
12638 return -ENODEV;
12639 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12640 if (!mbox)
12641 return -ENOMEM;
12642 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012643 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012644 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12645 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12646 length, LPFC_SLI4_MBX_EMBED);
12647 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12648 hrq->queue_id);
12649 mbox->vport = hrq->phba->pport;
12650 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12651 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12652 /* The IOCTL status is embedded in the mailbox subheader. */
12653 shdr = (union lpfc_sli4_cfg_shdr *)
12654 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12655 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12656 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12657 if (shdr_status || shdr_add_status || rc) {
12658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12659 "2509 RQ_DESTROY mailbox failed with "
12660 "status x%x add_status x%x, mbx status x%x\n",
12661 shdr_status, shdr_add_status, rc);
12662 if (rc != MBX_TIMEOUT)
12663 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12664 return -ENXIO;
12665 }
12666 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12667 drq->queue_id);
12668 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12669 shdr = (union lpfc_sli4_cfg_shdr *)
12670 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12671 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12672 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12673 if (shdr_status || shdr_add_status || rc) {
12674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12675 "2510 RQ_DESTROY mailbox failed with "
12676 "status x%x add_status x%x, mbx status x%x\n",
12677 shdr_status, shdr_add_status, rc);
12678 status = -ENXIO;
12679 }
12680 list_del_init(&hrq->list);
12681 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012682 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012683 return status;
12684}
12685
12686/**
12687 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12688 * @phba: The virtual port for which this call being executed.
12689 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12690 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12691 * @xritag: the xritag that ties this io to the SGL pages.
12692 *
12693 * This routine will post the sgl pages for the IO that has the xritag
12694 * that is in the iocbq structure. The xritag is assigned during iocbq
12695 * creation and persists for as long as the driver is loaded.
12696 * if the caller has fewer than 256 scatter gather segments to map then
12697 * pdma_phys_addr1 should be 0.
12698 * If the caller needs to map more than 256 scatter gather segment then
12699 * pdma_phys_addr1 should be a valid physical address.
12700 * physical address for SGLs must be 64 byte aligned.
12701 * If you are going to map 2 SGL's then the first one must have 256 entries
12702 * the second sgl can have between 1 and 256 entries.
12703 *
12704 * Return codes:
12705 * 0 - Success
12706 * -ENXIO, -ENOMEM - Failure
12707 **/
12708int
12709lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12710 dma_addr_t pdma_phys_addr0,
12711 dma_addr_t pdma_phys_addr1,
12712 uint16_t xritag)
12713{
12714 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12715 LPFC_MBOXQ_t *mbox;
12716 int rc;
12717 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012718 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012719 union lpfc_sli4_cfg_shdr *shdr;
12720
12721 if (xritag == NO_XRI) {
12722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12723 "0364 Invalid param:\n");
12724 return -EINVAL;
12725 }
12726
12727 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12728 if (!mbox)
12729 return -ENOMEM;
12730
12731 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12732 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12733 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012734 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012735
12736 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12737 &mbox->u.mqe.un.post_sgl_pages;
12738 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12739 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12740
12741 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12742 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12743 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12744 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12745
12746 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12747 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12748 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12749 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12750 if (!phba->sli4_hba.intr_enable)
12751 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012752 else {
James Smarta183a152011-10-10 21:32:43 -040012753 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012754 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12755 }
James Smart4f774512009-05-22 14:52:35 -040012756 /* The IOCTL status is embedded in the mailbox subheader. */
12757 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12758 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12759 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12760 if (rc != MBX_TIMEOUT)
12761 mempool_free(mbox, phba->mbox_mem_pool);
12762 if (shdr_status || shdr_add_status || rc) {
12763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12764 "2511 POST_SGL mailbox failed with "
12765 "status x%x add_status x%x, mbx status x%x\n",
12766 shdr_status, shdr_add_status, rc);
12767 rc = -ENXIO;
12768 }
12769 return 0;
12770}
James Smart4f774512009-05-22 14:52:35 -040012771
12772/**
James Smart88a2cfb2011-07-22 18:36:33 -040012773 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012774 * @phba: pointer to lpfc hba data structure.
12775 *
12776 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012777 * HBA consistent with the SLI-4 interface spec. This routine
12778 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12779 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012780 *
James Smart88a2cfb2011-07-22 18:36:33 -040012781 * Returns
12782 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12783 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12784 **/
James Smart6d368e52011-05-24 11:44:12 -040012785uint16_t
12786lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12787{
12788 unsigned long xri;
12789
12790 /*
12791 * Fetch the next logical xri. Because this index is logical,
12792 * the driver starts at 0 each time.
12793 */
12794 spin_lock_irq(&phba->hbalock);
12795 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12796 phba->sli4_hba.max_cfg_param.max_xri, 0);
12797 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12798 spin_unlock_irq(&phba->hbalock);
12799 return NO_XRI;
12800 } else {
12801 set_bit(xri, phba->sli4_hba.xri_bmask);
12802 phba->sli4_hba.max_cfg_param.xri_used++;
12803 phba->sli4_hba.xri_count++;
12804 }
12805
12806 spin_unlock_irq(&phba->hbalock);
12807 return xri;
12808}
12809
12810/**
12811 * lpfc_sli4_free_xri - Release an xri for reuse.
12812 * @phba: pointer to lpfc hba data structure.
12813 *
12814 * This routine is invoked to release an xri to the pool of
12815 * available rpis maintained by the driver.
12816 **/
12817void
12818__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12819{
12820 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12821 phba->sli4_hba.xri_count--;
12822 phba->sli4_hba.max_cfg_param.xri_used--;
12823 }
12824}
12825
12826/**
12827 * lpfc_sli4_free_xri - Release an xri for reuse.
12828 * @phba: pointer to lpfc hba data structure.
12829 *
12830 * This routine is invoked to release an xri to the pool of
12831 * available rpis maintained by the driver.
12832 **/
12833void
12834lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12835{
12836 spin_lock_irq(&phba->hbalock);
12837 __lpfc_sli4_free_xri(phba, xri);
12838 spin_unlock_irq(&phba->hbalock);
12839}
12840
12841/**
James Smart4f774512009-05-22 14:52:35 -040012842 * lpfc_sli4_next_xritag - Get an xritag for the io
12843 * @phba: Pointer to HBA context object.
12844 *
12845 * This function gets an xritag for the iocb. If there is no unused xritag
12846 * it will return 0xffff.
12847 * The function returns the allocated xritag if successful, else returns zero.
12848 * Zero is not a valid xritag.
12849 * The caller is not required to hold any lock.
12850 **/
12851uint16_t
12852lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12853{
James Smart6d368e52011-05-24 11:44:12 -040012854 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040012855
James Smart6d368e52011-05-24 11:44:12 -040012856 xri_index = lpfc_sli4_alloc_xri(phba);
12857 if (xri_index != NO_XRI)
12858 return xri_index;
12859
12860 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040012861 "2004 Failed to allocate XRI.last XRITAG is %d"
12862 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040012863 xri_index,
James Smart4f774512009-05-22 14:52:35 -040012864 phba->sli4_hba.max_cfg_param.max_xri,
12865 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040012866 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040012867}
12868
12869/**
James Smart6d368e52011-05-24 11:44:12 -040012870 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040012871 * @phba: pointer to lpfc hba data structure.
12872 *
12873 * This routine is invoked to post a block of driver's sgl pages to the
12874 * HBA using non-embedded mailbox command. No Lock is held. This routine
12875 * is only called when the driver is loading and after all IO has been
12876 * stopped.
12877 **/
12878int
James Smart6d368e52011-05-24 11:44:12 -040012879lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040012880{
12881 struct lpfc_sglq *sglq_entry;
12882 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12883 struct sgl_page_pairs *sgl_pg_pairs;
12884 void *viraddr;
12885 LPFC_MBOXQ_t *mbox;
12886 uint32_t reqlen, alloclen, pg_pairs;
12887 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040012888 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040012889 int els_xri_cnt, rc = 0;
12890 uint32_t shdr_status, shdr_add_status;
12891 union lpfc_sli4_cfg_shdr *shdr;
12892
12893 /* The number of sgls to be posted */
12894 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12895
12896 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12897 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012898 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012899 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12900 "2559 Block sgl registration required DMA "
12901 "size (%d) great than a page\n", reqlen);
12902 return -ENOMEM;
12903 }
12904 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040012905 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040012906 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040012907
12908 /* Allocate DMA memory and set up the non-embedded mailbox command */
12909 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12910 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12911 LPFC_SLI4_MBX_NEMBED);
12912
12913 if (alloclen < reqlen) {
12914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12915 "0285 Allocated DMA memory size (%d) is "
12916 "less than the requested DMA memory "
12917 "size (%d)\n", alloclen, reqlen);
12918 lpfc_sli4_mbox_cmd_free(phba, mbox);
12919 return -ENOMEM;
12920 }
James Smart4f774512009-05-22 14:52:35 -040012921 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040012922 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040012923 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12924 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12925
12926 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12927 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040012928
12929 /*
12930 * Assign the sglq a physical xri only if the driver has not
12931 * initialized those resources. A port reset only needs
12932 * the sglq's posted.
12933 */
12934 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12935 LPFC_XRI_RSRC_RDY) {
12936 lxri = lpfc_sli4_next_xritag(phba);
12937 if (lxri == NO_XRI) {
12938 lpfc_sli4_mbox_cmd_free(phba, mbox);
12939 return -ENOMEM;
12940 }
12941 sglq_entry->sli4_lxritag = lxri;
12942 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12943 }
12944
James Smart4f774512009-05-22 14:52:35 -040012945 /* Set up the sge entry */
12946 sgl_pg_pairs->sgl_pg0_addr_lo =
12947 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12948 sgl_pg_pairs->sgl_pg0_addr_hi =
12949 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12950 sgl_pg_pairs->sgl_pg1_addr_lo =
12951 cpu_to_le32(putPaddrLow(0));
12952 sgl_pg_pairs->sgl_pg1_addr_hi =
12953 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040012954
James Smart4f774512009-05-22 14:52:35 -040012955 /* Keep the first xritag on the list */
12956 if (pg_pairs == 0)
12957 xritag_start = sglq_entry->sli4_xritag;
12958 sgl_pg_pairs++;
12959 }
James Smart6d368e52011-05-24 11:44:12 -040012960
12961 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040012962 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040012963 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040012964 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040012965 if (!phba->sli4_hba.intr_enable)
12966 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12967 else {
James Smarta183a152011-10-10 21:32:43 -040012968 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040012969 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12970 }
12971 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12972 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12973 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12974 if (rc != MBX_TIMEOUT)
12975 lpfc_sli4_mbox_cmd_free(phba, mbox);
12976 if (shdr_status || shdr_add_status || rc) {
12977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12978 "2513 POST_SGL_BLOCK mailbox command failed "
12979 "status x%x add_status x%x mbx status x%x\n",
12980 shdr_status, shdr_add_status, rc);
12981 rc = -ENXIO;
12982 }
James Smart6d368e52011-05-24 11:44:12 -040012983
12984 if (rc == 0)
12985 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12986 LPFC_XRI_RSRC_RDY);
12987 return rc;
12988}
12989
12990/**
12991 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12992 * @phba: pointer to lpfc hba data structure.
12993 *
12994 * This routine is invoked to post a block of driver's sgl pages to the
12995 * HBA using non-embedded mailbox command. No Lock is held. This routine
12996 * is only called when the driver is loading and after all IO has been
12997 * stopped.
12998 **/
12999int
13000lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13001{
13002 struct lpfc_sglq *sglq_entry;
13003 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13004 struct sgl_page_pairs *sgl_pg_pairs;
13005 void *viraddr;
13006 LPFC_MBOXQ_t *mbox;
13007 uint32_t reqlen, alloclen, index;
13008 uint32_t mbox_tmo;
13009 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13010 uint16_t xritag_start = 0, lxri = 0;
13011 struct lpfc_rsrc_blks *rsrc_blk;
13012 int cnt, ttl_cnt, rc = 0;
13013 int loop_cnt;
13014 uint32_t shdr_status, shdr_add_status;
13015 union lpfc_sli4_cfg_shdr *shdr;
13016
13017 /* The number of sgls to be posted */
13018 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13019
13020 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13021 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13022 if (reqlen > SLI4_PAGE_SIZE) {
13023 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13024 "2989 Block sgl registration required DMA "
13025 "size (%d) great than a page\n", reqlen);
13026 return -ENOMEM;
13027 }
13028
13029 cnt = 0;
13030 ttl_cnt = 0;
13031 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13032 list) {
13033 rsrc_start = rsrc_blk->rsrc_start;
13034 rsrc_size = rsrc_blk->rsrc_size;
13035
13036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13037 "3014 Working ELS Extent start %d, cnt %d\n",
13038 rsrc_start, rsrc_size);
13039
13040 loop_cnt = min(els_xri_cnt, rsrc_size);
13041 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13042 loop_cnt = els_xri_cnt - ttl_cnt;
13043 ttl_cnt = els_xri_cnt;
13044 }
13045
13046 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13047 if (!mbox)
13048 return -ENOMEM;
13049 /*
13050 * Allocate DMA memory and set up the non-embedded mailbox
13051 * command.
13052 */
13053 alloclen = lpfc_sli4_config(phba, mbox,
13054 LPFC_MBOX_SUBSYSTEM_FCOE,
13055 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13056 reqlen, LPFC_SLI4_MBX_NEMBED);
13057 if (alloclen < reqlen) {
13058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13059 "2987 Allocated DMA memory size (%d) "
13060 "is less than the requested DMA memory "
13061 "size (%d)\n", alloclen, reqlen);
13062 lpfc_sli4_mbox_cmd_free(phba, mbox);
13063 return -ENOMEM;
13064 }
13065
13066 /* Set up the SGL pages in the non-embedded DMA pages */
13067 viraddr = mbox->sge_array->addr[0];
13068 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13069 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13070
13071 /*
13072 * The starting resource may not begin at zero. Control
13073 * the loop variants via the block resource parameters,
13074 * but handle the sge pointers with a zero-based index
13075 * that doesn't get reset per loop pass.
13076 */
13077 for (index = rsrc_start;
13078 index < rsrc_start + loop_cnt;
13079 index++) {
13080 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13081
13082 /*
13083 * Assign the sglq a physical xri only if the driver
13084 * has not initialized those resources. A port reset
13085 * only needs the sglq's posted.
13086 */
13087 if (bf_get(lpfc_xri_rsrc_rdy,
13088 &phba->sli4_hba.sli4_flags) !=
13089 LPFC_XRI_RSRC_RDY) {
13090 lxri = lpfc_sli4_next_xritag(phba);
13091 if (lxri == NO_XRI) {
13092 lpfc_sli4_mbox_cmd_free(phba, mbox);
13093 rc = -ENOMEM;
13094 goto err_exit;
13095 }
13096 sglq_entry->sli4_lxritag = lxri;
13097 sglq_entry->sli4_xritag =
13098 phba->sli4_hba.xri_ids[lxri];
13099 }
13100
13101 /* Set up the sge entry */
13102 sgl_pg_pairs->sgl_pg0_addr_lo =
13103 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13104 sgl_pg_pairs->sgl_pg0_addr_hi =
13105 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13106 sgl_pg_pairs->sgl_pg1_addr_lo =
13107 cpu_to_le32(putPaddrLow(0));
13108 sgl_pg_pairs->sgl_pg1_addr_hi =
13109 cpu_to_le32(putPaddrHigh(0));
13110
13111 /* Track the starting physical XRI for the mailbox. */
13112 if (index == rsrc_start)
13113 xritag_start = sglq_entry->sli4_xritag;
13114 sgl_pg_pairs++;
13115 cnt++;
13116 }
13117
13118 /* Complete initialization and perform endian conversion. */
13119 rsrc_blk->rsrc_used += loop_cnt;
13120 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13121 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13122 sgl->word0 = cpu_to_le32(sgl->word0);
13123
13124 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13125 "3015 Post ELS Extent SGL, start %d, "
13126 "cnt %d, used %d\n",
13127 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13128 if (!phba->sli4_hba.intr_enable)
13129 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13130 else {
James Smarta183a152011-10-10 21:32:43 -040013131 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013132 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13133 }
13134 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13135 shdr_status = bf_get(lpfc_mbox_hdr_status,
13136 &shdr->response);
13137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13138 &shdr->response);
13139 if (rc != MBX_TIMEOUT)
13140 lpfc_sli4_mbox_cmd_free(phba, mbox);
13141 if (shdr_status || shdr_add_status || rc) {
13142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13143 "2988 POST_SGL_BLOCK mailbox "
13144 "command failed status x%x "
13145 "add_status x%x mbx status x%x\n",
13146 shdr_status, shdr_add_status, rc);
13147 rc = -ENXIO;
13148 goto err_exit;
13149 }
13150 if (ttl_cnt >= els_xri_cnt)
13151 break;
13152 }
13153
13154 err_exit:
13155 if (rc == 0)
13156 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13157 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040013158 return rc;
13159}
13160
13161/**
13162 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13163 * @phba: pointer to lpfc hba data structure.
13164 * @sblist: pointer to scsi buffer list.
13165 * @count: number of scsi buffers on the list.
13166 *
13167 * This routine is invoked to post a block of @count scsi sgl pages from a
13168 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13169 * No Lock is held.
13170 *
13171 **/
13172int
13173lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13174 int cnt)
13175{
13176 struct lpfc_scsi_buf *psb;
13177 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13178 struct sgl_page_pairs *sgl_pg_pairs;
13179 void *viraddr;
13180 LPFC_MBOXQ_t *mbox;
13181 uint32_t reqlen, alloclen, pg_pairs;
13182 uint32_t mbox_tmo;
13183 uint16_t xritag_start = 0;
13184 int rc = 0;
13185 uint32_t shdr_status, shdr_add_status;
13186 dma_addr_t pdma_phys_bpl1;
13187 union lpfc_sli4_cfg_shdr *shdr;
13188
13189 /* Calculate the requested length of the dma memory */
13190 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13191 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013192 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013193 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13194 "0217 Block sgl registration required DMA "
13195 "size (%d) great than a page\n", reqlen);
13196 return -ENOMEM;
13197 }
13198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13199 if (!mbox) {
13200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13201 "0283 Failed to allocate mbox cmd memory\n");
13202 return -ENOMEM;
13203 }
13204
13205 /* Allocate DMA memory and set up the non-embedded mailbox command */
13206 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13207 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13208 LPFC_SLI4_MBX_NEMBED);
13209
13210 if (alloclen < reqlen) {
13211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13212 "2561 Allocated DMA memory size (%d) is "
13213 "less than the requested DMA memory "
13214 "size (%d)\n", alloclen, reqlen);
13215 lpfc_sli4_mbox_cmd_free(phba, mbox);
13216 return -ENOMEM;
13217 }
James Smart6d368e52011-05-24 11:44:12 -040013218
James Smart4f774512009-05-22 14:52:35 -040013219 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013220 viraddr = mbox->sge_array->addr[0];
13221
13222 /* Set up the SGL pages in the non-embedded DMA pages */
13223 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13224 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13225
13226 pg_pairs = 0;
13227 list_for_each_entry(psb, sblist, list) {
13228 /* Set up the sge entry */
13229 sgl_pg_pairs->sgl_pg0_addr_lo =
13230 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13231 sgl_pg_pairs->sgl_pg0_addr_hi =
13232 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13233 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13234 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13235 else
13236 pdma_phys_bpl1 = 0;
13237 sgl_pg_pairs->sgl_pg1_addr_lo =
13238 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13239 sgl_pg_pairs->sgl_pg1_addr_hi =
13240 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13241 /* Keep the first xritag on the list */
13242 if (pg_pairs == 0)
13243 xritag_start = psb->cur_iocbq.sli4_xritag;
13244 sgl_pg_pairs++;
13245 pg_pairs++;
13246 }
13247 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13248 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13249 /* Perform endian conversion if necessary */
13250 sgl->word0 = cpu_to_le32(sgl->word0);
13251
13252 if (!phba->sli4_hba.intr_enable)
13253 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13254 else {
James Smarta183a152011-10-10 21:32:43 -040013255 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013256 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13257 }
13258 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13259 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13260 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13261 if (rc != MBX_TIMEOUT)
13262 lpfc_sli4_mbox_cmd_free(phba, mbox);
13263 if (shdr_status || shdr_add_status || rc) {
13264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13265 "2564 POST_SGL_BLOCK mailbox command failed "
13266 "status x%x add_status x%x mbx status x%x\n",
13267 shdr_status, shdr_add_status, rc);
13268 rc = -ENXIO;
13269 }
13270 return rc;
13271}
13272
13273/**
James Smart6d368e52011-05-24 11:44:12 -040013274 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13275 * @phba: pointer to lpfc hba data structure.
13276 * @sblist: pointer to scsi buffer list.
13277 * @count: number of scsi buffers on the list.
13278 *
13279 * This routine is invoked to post a block of @count scsi sgl pages from a
13280 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13281 * No Lock is held.
13282 *
13283 **/
13284int
13285lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13286 int cnt)
13287{
13288 struct lpfc_scsi_buf *psb = NULL;
13289 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13290 struct sgl_page_pairs *sgl_pg_pairs;
13291 void *viraddr;
13292 LPFC_MBOXQ_t *mbox;
13293 uint32_t reqlen, alloclen, pg_pairs;
13294 uint32_t mbox_tmo;
13295 uint16_t xri_start = 0, scsi_xri_start;
13296 uint16_t rsrc_range;
13297 int rc = 0, avail_cnt;
13298 uint32_t shdr_status, shdr_add_status;
13299 dma_addr_t pdma_phys_bpl1;
13300 union lpfc_sli4_cfg_shdr *shdr;
13301 struct lpfc_rsrc_blks *rsrc_blk;
13302 uint32_t xri_cnt = 0;
13303
13304 /* Calculate the total requested length of the dma memory */
13305 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13306 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13307 if (reqlen > SLI4_PAGE_SIZE) {
13308 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13309 "2932 Block sgl registration required DMA "
13310 "size (%d) great than a page\n", reqlen);
13311 return -ENOMEM;
13312 }
13313
13314 /*
13315 * The use of extents requires the driver to post the sgl headers
13316 * in multiple postings to meet the contiguous resource assignment.
13317 */
13318 psb = list_prepare_entry(psb, sblist, list);
13319 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13320 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13321 list) {
13322 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13323 if (rsrc_range < scsi_xri_start)
13324 continue;
13325 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13326 continue;
13327 else
13328 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13329
13330 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13331 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13332 /*
13333 * Allocate DMA memory and set up the non-embedded mailbox
13334 * command. The mbox is used to post an SGL page per loop
13335 * but the DMA memory has a use-once semantic so the mailbox
13336 * is used and freed per loop pass.
13337 */
13338 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13339 if (!mbox) {
13340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13341 "2933 Failed to allocate mbox cmd "
13342 "memory\n");
13343 return -ENOMEM;
13344 }
13345 alloclen = lpfc_sli4_config(phba, mbox,
13346 LPFC_MBOX_SUBSYSTEM_FCOE,
13347 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13348 reqlen,
13349 LPFC_SLI4_MBX_NEMBED);
13350 if (alloclen < reqlen) {
13351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13352 "2934 Allocated DMA memory size (%d) "
13353 "is less than the requested DMA memory "
13354 "size (%d)\n", alloclen, reqlen);
13355 lpfc_sli4_mbox_cmd_free(phba, mbox);
13356 return -ENOMEM;
13357 }
13358
13359 /* Get the first SGE entry from the non-embedded DMA memory */
13360 viraddr = mbox->sge_array->addr[0];
13361
13362 /* Set up the SGL pages in the non-embedded DMA pages */
13363 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13364 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13365
13366 /* pg_pairs tracks posted SGEs per loop iteration. */
13367 pg_pairs = 0;
13368 list_for_each_entry_continue(psb, sblist, list) {
13369 /* Set up the sge entry */
13370 sgl_pg_pairs->sgl_pg0_addr_lo =
13371 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13372 sgl_pg_pairs->sgl_pg0_addr_hi =
13373 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13374 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13375 pdma_phys_bpl1 = psb->dma_phys_bpl +
13376 SGL_PAGE_SIZE;
13377 else
13378 pdma_phys_bpl1 = 0;
13379 sgl_pg_pairs->sgl_pg1_addr_lo =
13380 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13381 sgl_pg_pairs->sgl_pg1_addr_hi =
13382 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13383 /* Keep the first xri for this extent. */
13384 if (pg_pairs == 0)
13385 xri_start = psb->cur_iocbq.sli4_xritag;
13386 sgl_pg_pairs++;
13387 pg_pairs++;
13388 xri_cnt++;
13389
13390 /*
13391 * Track two exit conditions - the loop has constructed
13392 * all of the caller's SGE pairs or all available
13393 * resource IDs in this extent are consumed.
13394 */
13395 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13396 break;
13397 }
13398 rsrc_blk->rsrc_used += pg_pairs;
13399 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13400 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13401
13402 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13403 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13404 "blk use %d\n",
13405 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13406 /* Perform endian conversion if necessary */
13407 sgl->word0 = cpu_to_le32(sgl->word0);
13408 if (!phba->sli4_hba.intr_enable)
13409 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13410 else {
James Smarta183a152011-10-10 21:32:43 -040013411 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013412 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13413 }
13414 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13415 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13416 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13417 &shdr->response);
13418 if (rc != MBX_TIMEOUT)
13419 lpfc_sli4_mbox_cmd_free(phba, mbox);
13420 if (shdr_status || shdr_add_status || rc) {
13421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13422 "2935 POST_SGL_BLOCK mailbox command "
13423 "failed status x%x add_status x%x "
13424 "mbx status x%x\n",
13425 shdr_status, shdr_add_status, rc);
13426 return -ENXIO;
13427 }
13428
13429 /* Post only what is requested. */
13430 if (xri_cnt >= cnt)
13431 break;
13432 }
13433 return rc;
13434}
13435
13436/**
James Smart4f774512009-05-22 14:52:35 -040013437 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13438 * @phba: pointer to lpfc_hba struct that the frame was received on
13439 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13440 *
13441 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13442 * valid type of frame that the LPFC driver will handle. This function will
13443 * return a zero if the frame is a valid frame or a non zero value when the
13444 * frame does not pass the check.
13445 **/
13446static int
13447lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13448{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013449 /* make rctl_names static to save stack space */
13450 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013451 char *type_names[] = FC_TYPE_NAMES_INIT;
13452 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013453 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013454
13455 switch (fc_hdr->fh_r_ctl) {
13456 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13457 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13458 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13459 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13460 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13461 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13462 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13463 case FC_RCTL_DD_CMD_STATUS: /* command status */
13464 case FC_RCTL_ELS_REQ: /* extended link services request */
13465 case FC_RCTL_ELS_REP: /* extended link services reply */
13466 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13467 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13468 case FC_RCTL_BA_NOP: /* basic link service NOP */
13469 case FC_RCTL_BA_ABTS: /* basic link service abort */
13470 case FC_RCTL_BA_RMC: /* remove connection */
13471 case FC_RCTL_BA_ACC: /* basic accept */
13472 case FC_RCTL_BA_RJT: /* basic reject */
13473 case FC_RCTL_BA_PRMT:
13474 case FC_RCTL_ACK_1: /* acknowledge_1 */
13475 case FC_RCTL_ACK_0: /* acknowledge_0 */
13476 case FC_RCTL_P_RJT: /* port reject */
13477 case FC_RCTL_F_RJT: /* fabric reject */
13478 case FC_RCTL_P_BSY: /* port busy */
13479 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13480 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13481 case FC_RCTL_LCR: /* link credit reset */
13482 case FC_RCTL_END: /* end */
13483 break;
13484 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13485 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13486 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13487 return lpfc_fc_frame_check(phba, fc_hdr);
13488 default:
13489 goto drop;
13490 }
13491 switch (fc_hdr->fh_type) {
13492 case FC_TYPE_BLS:
13493 case FC_TYPE_ELS:
13494 case FC_TYPE_FCP:
13495 case FC_TYPE_CT:
13496 break;
13497 case FC_TYPE_IP:
13498 case FC_TYPE_ILS:
13499 default:
13500 goto drop;
13501 }
James Smart546fc852011-03-11 16:06:29 -050013502
James Smart4f774512009-05-22 14:52:35 -040013503 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013504 "2538 Received frame rctl:%s type:%s "
13505 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013506 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013507 type_names[fc_hdr->fh_type],
13508 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13509 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13510 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013511 return 0;
13512drop:
13513 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13514 "2539 Dropped frame rctl:%s type:%s\n",
13515 rctl_names[fc_hdr->fh_r_ctl],
13516 type_names[fc_hdr->fh_type]);
13517 return 1;
13518}
13519
13520/**
13521 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13522 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13523 *
13524 * This function processes the FC header to retrieve the VFI from the VF
13525 * header, if one exists. This function will return the VFI if one exists
13526 * or 0 if no VSAN Header exists.
13527 **/
13528static uint32_t
13529lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13530{
13531 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13532
13533 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13534 return 0;
13535 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13536}
13537
13538/**
13539 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13540 * @phba: Pointer to the HBA structure to search for the vport on
13541 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13542 * @fcfi: The FC Fabric ID that the frame came from
13543 *
13544 * This function searches the @phba for a vport that matches the content of the
13545 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13546 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13547 * returns the matching vport pointer or NULL if unable to match frame to a
13548 * vport.
13549 **/
13550static struct lpfc_vport *
13551lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13552 uint16_t fcfi)
13553{
13554 struct lpfc_vport **vports;
13555 struct lpfc_vport *vport = NULL;
13556 int i;
13557 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13558 fc_hdr->fh_d_id[1] << 8 |
13559 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013560 if (did == Fabric_DID)
13561 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013562 vports = lpfc_create_vport_work_array(phba);
13563 if (vports != NULL)
13564 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13565 if (phba->fcf.fcfi == fcfi &&
13566 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13567 vports[i]->fc_myDID == did) {
13568 vport = vports[i];
13569 break;
13570 }
13571 }
13572 lpfc_destroy_vport_work_array(phba, vports);
13573 return vport;
13574}
13575
13576/**
James Smart45ed1192009-10-02 15:17:02 -040013577 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13578 * @vport: The vport to work on.
13579 *
13580 * This function updates the receive sequence time stamp for this vport. The
13581 * receive sequence time stamp indicates the time that the last frame of the
13582 * the sequence that has been idle for the longest amount of time was received.
13583 * the driver uses this time stamp to indicate if any received sequences have
13584 * timed out.
13585 **/
13586void
13587lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13588{
13589 struct lpfc_dmabuf *h_buf;
13590 struct hbq_dmabuf *dmabuf = NULL;
13591
13592 /* get the oldest sequence on the rcv list */
13593 h_buf = list_get_first(&vport->rcv_buffer_list,
13594 struct lpfc_dmabuf, list);
13595 if (!h_buf)
13596 return;
13597 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13598 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13599}
13600
13601/**
13602 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13603 * @vport: The vport that the received sequences were sent to.
13604 *
13605 * This function cleans up all outstanding received sequences. This is called
13606 * by the driver when a link event or user action invalidates all the received
13607 * sequences.
13608 **/
13609void
13610lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13611{
13612 struct lpfc_dmabuf *h_buf, *hnext;
13613 struct lpfc_dmabuf *d_buf, *dnext;
13614 struct hbq_dmabuf *dmabuf = NULL;
13615
13616 /* start with the oldest sequence on the rcv list */
13617 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13618 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13619 list_del_init(&dmabuf->hbuf.list);
13620 list_for_each_entry_safe(d_buf, dnext,
13621 &dmabuf->dbuf.list, list) {
13622 list_del_init(&d_buf->list);
13623 lpfc_in_buf_free(vport->phba, d_buf);
13624 }
13625 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13626 }
13627}
13628
13629/**
13630 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13631 * @vport: The vport that the received sequences were sent to.
13632 *
13633 * This function determines whether any received sequences have timed out by
13634 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13635 * indicates that there is at least one timed out sequence this routine will
13636 * go through the received sequences one at a time from most inactive to most
13637 * active to determine which ones need to be cleaned up. Once it has determined
13638 * that a sequence needs to be cleaned up it will simply free up the resources
13639 * without sending an abort.
13640 **/
13641void
13642lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13643{
13644 struct lpfc_dmabuf *h_buf, *hnext;
13645 struct lpfc_dmabuf *d_buf, *dnext;
13646 struct hbq_dmabuf *dmabuf = NULL;
13647 unsigned long timeout;
13648 int abort_count = 0;
13649
13650 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13651 vport->rcv_buffer_time_stamp);
13652 if (list_empty(&vport->rcv_buffer_list) ||
13653 time_before(jiffies, timeout))
13654 return;
13655 /* start with the oldest sequence on the rcv list */
13656 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13657 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13658 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13659 dmabuf->time_stamp);
13660 if (time_before(jiffies, timeout))
13661 break;
13662 abort_count++;
13663 list_del_init(&dmabuf->hbuf.list);
13664 list_for_each_entry_safe(d_buf, dnext,
13665 &dmabuf->dbuf.list, list) {
13666 list_del_init(&d_buf->list);
13667 lpfc_in_buf_free(vport->phba, d_buf);
13668 }
13669 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13670 }
13671 if (abort_count)
13672 lpfc_update_rcv_time_stamp(vport);
13673}
13674
13675/**
James Smart4f774512009-05-22 14:52:35 -040013676 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13677 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13678 *
13679 * This function searches through the existing incomplete sequences that have
13680 * been sent to this @vport. If the frame matches one of the incomplete
13681 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13682 * make up that sequence. If no sequence is found that matches this frame then
13683 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13684 * This function returns a pointer to the first dmabuf in the sequence list that
13685 * the frame was linked to.
13686 **/
13687static struct hbq_dmabuf *
13688lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13689{
13690 struct fc_frame_header *new_hdr;
13691 struct fc_frame_header *temp_hdr;
13692 struct lpfc_dmabuf *d_buf;
13693 struct lpfc_dmabuf *h_buf;
13694 struct hbq_dmabuf *seq_dmabuf = NULL;
13695 struct hbq_dmabuf *temp_dmabuf = NULL;
13696
James Smart4d9ab992009-10-02 15:16:39 -040013697 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013698 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013699 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13700 /* Use the hdr_buf to find the sequence that this frame belongs to */
13701 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13702 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13703 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13704 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13705 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13706 continue;
13707 /* found a pending sequence that matches this frame */
13708 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13709 break;
13710 }
13711 if (!seq_dmabuf) {
13712 /*
13713 * This indicates first frame received for this sequence.
13714 * Queue the buffer on the vport's rcv_buffer_list.
13715 */
13716 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013717 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013718 return dmabuf;
13719 }
13720 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013721 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13722 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013723 list_del_init(&seq_dmabuf->hbuf.list);
13724 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13725 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013726 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013727 return dmabuf;
13728 }
James Smart45ed1192009-10-02 15:17:02 -040013729 /* move this sequence to the tail to indicate a young sequence */
13730 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13731 seq_dmabuf->time_stamp = jiffies;
13732 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013733 if (list_empty(&seq_dmabuf->dbuf.list)) {
13734 temp_hdr = dmabuf->hbuf.virt;
13735 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13736 return seq_dmabuf;
13737 }
James Smart4f774512009-05-22 14:52:35 -040013738 /* find the correct place in the sequence to insert this frame */
13739 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13740 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13741 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13742 /*
13743 * If the frame's sequence count is greater than the frame on
13744 * the list then insert the frame right after this frame
13745 */
James Smarteeead812009-12-21 17:01:23 -050013746 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13747 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013748 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13749 return seq_dmabuf;
13750 }
13751 }
13752 return NULL;
13753}
13754
13755/**
James Smart6669f9b2009-10-02 15:16:45 -040013756 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13757 * @vport: pointer to a vitural port
13758 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13759 *
13760 * This function tries to abort from the partially assembed sequence, described
13761 * by the information from basic abbort @dmabuf. It checks to see whether such
13762 * partially assembled sequence held by the driver. If so, it shall free up all
13763 * the frames from the partially assembled sequence.
13764 *
13765 * Return
13766 * true -- if there is matching partially assembled sequence present and all
13767 * the frames freed with the sequence;
13768 * false -- if there is no matching partially assembled sequence present so
13769 * nothing got aborted in the lower layer driver
13770 **/
13771static bool
13772lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13773 struct hbq_dmabuf *dmabuf)
13774{
13775 struct fc_frame_header *new_hdr;
13776 struct fc_frame_header *temp_hdr;
13777 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13778 struct hbq_dmabuf *seq_dmabuf = NULL;
13779
13780 /* Use the hdr_buf to find the sequence that matches this frame */
13781 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13782 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13783 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13784 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13785 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13786 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13787 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13788 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13789 continue;
13790 /* found a pending sequence that matches this frame */
13791 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13792 break;
13793 }
13794
13795 /* Free up all the frames from the partially assembled sequence */
13796 if (seq_dmabuf) {
13797 list_for_each_entry_safe(d_buf, n_buf,
13798 &seq_dmabuf->dbuf.list, list) {
13799 list_del_init(&d_buf->list);
13800 lpfc_in_buf_free(vport->phba, d_buf);
13801 }
13802 return true;
13803 }
13804 return false;
13805}
13806
13807/**
James Smart546fc852011-03-11 16:06:29 -050013808 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013809 * @phba: Pointer to HBA context object.
13810 * @cmd_iocbq: pointer to the command iocbq structure.
13811 * @rsp_iocbq: pointer to the response iocbq structure.
13812 *
James Smart546fc852011-03-11 16:06:29 -050013813 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013814 * event. It properly releases the memory allocated to the sequence abort
13815 * accept iocb.
13816 **/
13817static void
James Smart546fc852011-03-11 16:06:29 -050013818lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013819 struct lpfc_iocbq *cmd_iocbq,
13820 struct lpfc_iocbq *rsp_iocbq)
13821{
13822 if (cmd_iocbq)
13823 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13824}
13825
13826/**
James Smart6d368e52011-05-24 11:44:12 -040013827 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13828 * @phba: Pointer to HBA context object.
13829 * @xri: xri id in transaction.
13830 *
13831 * This function validates the xri maps to the known range of XRIs allocated an
13832 * used by the driver.
13833 **/
James Smart7851fe22011-07-22 18:36:52 -040013834uint16_t
James Smart6d368e52011-05-24 11:44:12 -040013835lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13836 uint16_t xri)
13837{
13838 int i;
13839
13840 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13841 if (xri == phba->sli4_hba.xri_ids[i])
13842 return i;
13843 }
13844 return NO_XRI;
13845}
13846
13847
13848/**
James Smart546fc852011-03-11 16:06:29 -050013849 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013850 * @phba: Pointer to HBA context object.
13851 * @fc_hdr: pointer to a FC frame header.
13852 *
James Smart546fc852011-03-11 16:06:29 -050013853 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013854 * event after aborting the sequence handling.
13855 **/
13856static void
James Smart546fc852011-03-11 16:06:29 -050013857lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013858 struct fc_frame_header *fc_hdr)
13859{
13860 struct lpfc_iocbq *ctiocb = NULL;
13861 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050013862 uint16_t oxid, rxid;
13863 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013864 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013865 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013866
13867 if (!lpfc_is_link_up(phba))
13868 return;
13869
13870 sid = sli4_sid_from_fc_hdr(fc_hdr);
13871 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050013872 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040013873
13874 ndlp = lpfc_findnode_did(phba->pport, sid);
13875 if (!ndlp) {
13876 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13877 "1268 Find ndlp returned NULL for oxid:x%x "
13878 "SID:x%x\n", oxid, sid);
13879 return;
13880 }
James Smart6d368e52011-05-24 11:44:12 -040013881 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050013882 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040013883
James Smart546fc852011-03-11 16:06:29 -050013884 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040013885 ctiocb = lpfc_sli_get_iocbq(phba);
13886 if (!ctiocb)
13887 return;
13888
James Smart5ffc2662009-11-18 15:39:44 -050013889 /* Extract the F_CTL field from FC_HDR */
13890 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13891
James Smart6669f9b2009-10-02 15:16:45 -040013892 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040013893 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050013894 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040013895 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13896 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13897 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13898
13899 /* Fill in the rest of iocb fields */
13900 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13901 icmd->ulpBdeCount = 0;
13902 icmd->ulpLe = 1;
13903 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040013904 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050013905 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040013906
James Smart6669f9b2009-10-02 15:16:45 -040013907 ctiocb->iocb_cmpl = NULL;
13908 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050013909 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040013910 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050013911 ctiocb->sli4_xritag = NO_XRI;
13912
13913 /* If the oxid maps to the FCP XRI range or if it is out of range,
13914 * send a BLS_RJT. The driver no longer has that exchange.
13915 * Override the IOCB for a BA_RJT.
13916 */
13917 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13918 phba->sli4_hba.max_cfg_param.xri_base) ||
13919 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13920 phba->sli4_hba.max_cfg_param.xri_base)) {
13921 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13922 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13923 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13924 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13925 }
James Smart6669f9b2009-10-02 15:16:45 -040013926
James Smart5ffc2662009-11-18 15:39:44 -050013927 if (fctl & FC_FC_EX_CTX) {
13928 /* ABTS sent by responder to CT exchange, construction
13929 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13930 * field and RX_ID from ABTS for RX_ID field.
13931 */
James Smart546fc852011-03-11 16:06:29 -050013932 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13933 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050013934 } else {
13935 /* ABTS sent by initiator to CT exchange, construction
13936 * of BA_ACC will need to allocate a new XRI as for the
13937 * XRI_TAG and RX_ID fields.
13938 */
James Smart546fc852011-03-11 16:06:29 -050013939 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13940 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050013941 }
James Smart546fc852011-03-11 16:06:29 -050013942 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050013943
James Smart546fc852011-03-11 16:06:29 -050013944 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040013945 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013946 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13947 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13948
13949 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13950 if (rc == IOCB_ERROR) {
13951 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13952 "2925 Failed to issue CT ABTS RSP x%x on "
13953 "xri x%x, Data x%x\n",
13954 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13955 phba->link_state);
13956 lpfc_sli_release_iocbq(phba, ctiocb);
13957 }
James Smart6669f9b2009-10-02 15:16:45 -040013958}
13959
13960/**
13961 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13962 * @vport: Pointer to the vport on which this sequence was received
13963 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13964 *
13965 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13966 * receive sequence is only partially assembed by the driver, it shall abort
13967 * the partially assembled frames for the sequence. Otherwise, if the
13968 * unsolicited receive sequence has been completely assembled and passed to
13969 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13970 * unsolicited sequence has been aborted. After that, it will issue a basic
13971 * accept to accept the abort.
13972 **/
13973void
13974lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13975 struct hbq_dmabuf *dmabuf)
13976{
13977 struct lpfc_hba *phba = vport->phba;
13978 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050013979 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013980 bool abts_par;
13981
James Smart6669f9b2009-10-02 15:16:45 -040013982 /* Make a copy of fc_hdr before the dmabuf being released */
13983 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050013984 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013985
James Smart5ffc2662009-11-18 15:39:44 -050013986 if (fctl & FC_FC_EX_CTX) {
13987 /*
13988 * ABTS sent by responder to exchange, just free the buffer
13989 */
James Smart6669f9b2009-10-02 15:16:45 -040013990 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050013991 } else {
13992 /*
13993 * ABTS sent by initiator to exchange, need to do cleanup
13994 */
13995 /* Try to abort partially assembled seq */
13996 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13997
13998 /* Send abort to ULP if partially seq abort failed */
13999 if (abts_par == false)
14000 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14001 else
14002 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14003 }
James Smart6669f9b2009-10-02 15:16:45 -040014004 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014005 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014006}
14007
14008/**
James Smart4f774512009-05-22 14:52:35 -040014009 * lpfc_seq_complete - Indicates if a sequence is complete
14010 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14011 *
14012 * This function checks the sequence, starting with the frame described by
14013 * @dmabuf, to see if all the frames associated with this sequence are present.
14014 * the frames associated with this sequence are linked to the @dmabuf using the
14015 * dbuf list. This function looks for two major things. 1) That the first frame
14016 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14017 * set. 3) That there are no holes in the sequence count. The function will
14018 * return 1 when the sequence is complete, otherwise it will return 0.
14019 **/
14020static int
14021lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14022{
14023 struct fc_frame_header *hdr;
14024 struct lpfc_dmabuf *d_buf;
14025 struct hbq_dmabuf *seq_dmabuf;
14026 uint32_t fctl;
14027 int seq_count = 0;
14028
14029 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14030 /* make sure first fame of sequence has a sequence count of zero */
14031 if (hdr->fh_seq_cnt != seq_count)
14032 return 0;
14033 fctl = (hdr->fh_f_ctl[0] << 16 |
14034 hdr->fh_f_ctl[1] << 8 |
14035 hdr->fh_f_ctl[2]);
14036 /* If last frame of sequence we can return success. */
14037 if (fctl & FC_FC_END_SEQ)
14038 return 1;
14039 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14040 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14041 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14042 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014043 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014044 return 0;
14045 fctl = (hdr->fh_f_ctl[0] << 16 |
14046 hdr->fh_f_ctl[1] << 8 |
14047 hdr->fh_f_ctl[2]);
14048 /* If last frame of sequence we can return success. */
14049 if (fctl & FC_FC_END_SEQ)
14050 return 1;
14051 }
14052 return 0;
14053}
14054
14055/**
14056 * lpfc_prep_seq - Prep sequence for ULP processing
14057 * @vport: Pointer to the vport on which this sequence was received
14058 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14059 *
14060 * This function takes a sequence, described by a list of frames, and creates
14061 * a list of iocbq structures to describe the sequence. This iocbq list will be
14062 * used to issue to the generic unsolicited sequence handler. This routine
14063 * returns a pointer to the first iocbq in the list. If the function is unable
14064 * to allocate an iocbq then it throw out the received frames that were not
14065 * able to be described and return a pointer to the first iocbq. If unable to
14066 * allocate any iocbqs (including the first) this function will return NULL.
14067 **/
14068static struct lpfc_iocbq *
14069lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14070{
James Smart7851fe22011-07-22 18:36:52 -040014071 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014072 struct lpfc_dmabuf *d_buf, *n_buf;
14073 struct lpfc_iocbq *first_iocbq, *iocbq;
14074 struct fc_frame_header *fc_hdr;
14075 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014076 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014077 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014078
14079 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14080 /* remove from receive buffer list */
14081 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014082 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014083 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014084 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014085 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014086 /* Get an iocbq struct to fill in. */
14087 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14088 if (first_iocbq) {
14089 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014090 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014091 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14092 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014093 first_iocbq->iocb.ulpContext = NO_XRI;
14094 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14095 be16_to_cpu(fc_hdr->fh_ox_id);
14096 /* iocbq is prepped for internal consumption. Physical vpi. */
14097 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14098 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014099 /* put the first buffer into the first IOCBq */
14100 first_iocbq->context2 = &seq_dmabuf->dbuf;
14101 first_iocbq->context3 = NULL;
14102 first_iocbq->iocb.ulpBdeCount = 1;
14103 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14104 LPFC_DATA_BUF_SIZE;
14105 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014106 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014107 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014108 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014109 }
14110 iocbq = first_iocbq;
14111 /*
14112 * Each IOCBq can have two Buffers assigned, so go through the list
14113 * of buffers for this sequence and save two buffers in each IOCBq
14114 */
14115 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14116 if (!iocbq) {
14117 lpfc_in_buf_free(vport->phba, d_buf);
14118 continue;
14119 }
14120 if (!iocbq->context3) {
14121 iocbq->context3 = d_buf;
14122 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014123 pbde = (struct ulp_bde64 *)
14124 &iocbq->iocb.unsli3.sli3Words[4];
14125 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014126
14127 /* We need to get the size out of the right CQE */
14128 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14129 len = bf_get(lpfc_rcqe_length,
14130 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14131 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14132 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014133 } else {
14134 iocbq = lpfc_sli_get_iocbq(vport->phba);
14135 if (!iocbq) {
14136 if (first_iocbq) {
14137 first_iocbq->iocb.ulpStatus =
14138 IOSTAT_FCP_RSP_ERROR;
14139 first_iocbq->iocb.un.ulpWord[4] =
14140 IOERR_NO_RESOURCES;
14141 }
14142 lpfc_in_buf_free(vport->phba, d_buf);
14143 continue;
14144 }
14145 iocbq->context2 = d_buf;
14146 iocbq->context3 = NULL;
14147 iocbq->iocb.ulpBdeCount = 1;
14148 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14149 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014150
14151 /* We need to get the size out of the right CQE */
14152 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14153 len = bf_get(lpfc_rcqe_length,
14154 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14155 tot_len += len;
14156 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14157
James Smart4f774512009-05-22 14:52:35 -040014158 iocbq->iocb.un.rcvels.remoteID = sid;
14159 list_add_tail(&iocbq->list, &first_iocbq->list);
14160 }
14161 }
14162 return first_iocbq;
14163}
14164
James Smart6669f9b2009-10-02 15:16:45 -040014165static void
14166lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14167 struct hbq_dmabuf *seq_dmabuf)
14168{
14169 struct fc_frame_header *fc_hdr;
14170 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14171 struct lpfc_hba *phba = vport->phba;
14172
14173 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14174 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14175 if (!iocbq) {
14176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14177 "2707 Ring %d handler: Failed to allocate "
14178 "iocb Rctl x%x Type x%x received\n",
14179 LPFC_ELS_RING,
14180 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14181 return;
14182 }
14183 if (!lpfc_complete_unsol_iocb(phba,
14184 &phba->sli.ring[LPFC_ELS_RING],
14185 iocbq, fc_hdr->fh_r_ctl,
14186 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014188 "2540 Ring %d handler: unexpected Rctl "
14189 "x%x Type x%x received\n",
14190 LPFC_ELS_RING,
14191 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14192
14193 /* Free iocb created in lpfc_prep_seq */
14194 list_for_each_entry_safe(curr_iocb, next_iocb,
14195 &iocbq->list, list) {
14196 list_del_init(&curr_iocb->list);
14197 lpfc_sli_release_iocbq(phba, curr_iocb);
14198 }
14199 lpfc_sli_release_iocbq(phba, iocbq);
14200}
14201
James Smart4f774512009-05-22 14:52:35 -040014202/**
14203 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14204 * @phba: Pointer to HBA context object.
14205 *
14206 * This function is called with no lock held. This function processes all
14207 * the received buffers and gives it to upper layers when a received buffer
14208 * indicates that it is the final frame in the sequence. The interrupt
14209 * service routine processes received buffers at interrupt contexts and adds
14210 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14211 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14212 * appropriate receive function when the final frame in a sequence is received.
14213 **/
James Smart4d9ab992009-10-02 15:16:39 -040014214void
14215lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14216 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014217{
James Smart4d9ab992009-10-02 15:16:39 -040014218 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014219 struct fc_frame_header *fc_hdr;
14220 struct lpfc_vport *vport;
14221 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014222
James Smart4f774512009-05-22 14:52:35 -040014223 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014224 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14225 /* check to see if this a valid type of frame */
14226 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14227 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14228 return;
14229 }
James Smart7851fe22011-07-22 18:36:52 -040014230 if ((bf_get(lpfc_cqe_code,
14231 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14232 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14233 &dmabuf->cq_event.cqe.rcqe_cmpl);
14234 else
14235 fcfi = bf_get(lpfc_rcqe_fcf_id,
14236 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014237 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014238 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014239 /* throw out the frame */
14240 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14241 return;
14242 }
James Smart6669f9b2009-10-02 15:16:45 -040014243 /* Handle the basic abort sequence (BA_ABTS) event */
14244 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14245 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14246 return;
14247 }
14248
James Smart4d9ab992009-10-02 15:16:39 -040014249 /* Link this frame */
14250 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14251 if (!seq_dmabuf) {
14252 /* unable to add frame to vport - throw it out */
14253 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14254 return;
14255 }
14256 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014257 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014258 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014259
James Smart6669f9b2009-10-02 15:16:45 -040014260 /* Send the complete sequence to the upper layer protocol */
14261 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014262}
James Smart6fb120a2009-05-22 14:52:59 -040014263
14264/**
14265 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14266 * @phba: pointer to lpfc hba data structure.
14267 *
14268 * This routine is invoked to post rpi header templates to the
14269 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014270 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14271 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014272 *
14273 * This routine does not require any locks. It's usage is expected
14274 * to be driver load or reset recovery when the driver is
14275 * sequential.
14276 *
14277 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014278 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014279 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014280 * When this error occurs, the driver is not guaranteed
14281 * to have any rpi regions posted to the device and
14282 * must either attempt to repost the regions or take a
14283 * fatal error.
14284 **/
14285int
14286lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14287{
14288 struct lpfc_rpi_hdr *rpi_page;
14289 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014290 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014291
James Smart6d368e52011-05-24 11:44:12 -040014292 /* SLI4 ports that support extents do not require RPI headers. */
14293 if (!phba->sli4_hba.rpi_hdrs_in_use)
14294 goto exit;
14295 if (phba->sli4_hba.extents_in_use)
14296 return -EIO;
14297
James Smart6fb120a2009-05-22 14:52:59 -040014298 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014299 /*
14300 * Assign the rpi headers a physical rpi only if the driver
14301 * has not initialized those resources. A port reset only
14302 * needs the headers posted.
14303 */
14304 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14305 LPFC_RPI_RSRC_RDY)
14306 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14307
James Smart6fb120a2009-05-22 14:52:59 -040014308 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14309 if (rc != MBX_SUCCESS) {
14310 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14311 "2008 Error %d posting all rpi "
14312 "headers\n", rc);
14313 rc = -EIO;
14314 break;
14315 }
14316 }
14317
James Smart6d368e52011-05-24 11:44:12 -040014318 exit:
14319 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14320 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014321 return rc;
14322}
14323
14324/**
14325 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14326 * @phba: pointer to lpfc hba data structure.
14327 * @rpi_page: pointer to the rpi memory region.
14328 *
14329 * This routine is invoked to post a single rpi header to the
14330 * HBA consistent with the SLI-4 interface spec. This memory region
14331 * maps up to 64 rpi context regions.
14332 *
14333 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014334 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014335 * -ENOMEM - No available memory
14336 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014337 **/
14338int
14339lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14340{
14341 LPFC_MBOXQ_t *mboxq;
14342 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14343 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014344 uint32_t shdr_status, shdr_add_status;
14345 union lpfc_sli4_cfg_shdr *shdr;
14346
James Smart6d368e52011-05-24 11:44:12 -040014347 /* SLI4 ports that support extents do not require RPI headers. */
14348 if (!phba->sli4_hba.rpi_hdrs_in_use)
14349 return rc;
14350 if (phba->sli4_hba.extents_in_use)
14351 return -EIO;
14352
James Smart6fb120a2009-05-22 14:52:59 -040014353 /* The port is notified of the header region via a mailbox command. */
14354 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14355 if (!mboxq) {
14356 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14357 "2001 Unable to allocate memory for issuing "
14358 "SLI_CONFIG_SPECIAL mailbox command\n");
14359 return -ENOMEM;
14360 }
14361
14362 /* Post all rpi memory regions to the port. */
14363 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014364 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14365 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14366 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014367 sizeof(struct lpfc_sli4_cfg_mhdr),
14368 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014369
14370
14371 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014372 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14373 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014374 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14375 hdr_tmpl, rpi_page->page_count);
14376
James Smart6fb120a2009-05-22 14:52:59 -040014377 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14378 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014379 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014380 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14381 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14382 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14383 if (rc != MBX_TIMEOUT)
14384 mempool_free(mboxq, phba->mbox_mem_pool);
14385 if (shdr_status || shdr_add_status || rc) {
14386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14387 "2514 POST_RPI_HDR mailbox failed with "
14388 "status x%x add_status x%x, mbx status x%x\n",
14389 shdr_status, shdr_add_status, rc);
14390 rc = -ENXIO;
14391 }
14392 return rc;
14393}
14394
14395/**
14396 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14397 * @phba: pointer to lpfc hba data structure.
14398 *
14399 * This routine is invoked to post rpi header templates to the
14400 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014401 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14402 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014403 *
14404 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014405 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014406 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14407 **/
14408int
14409lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14410{
James Smart6d368e52011-05-24 11:44:12 -040014411 unsigned long rpi;
14412 uint16_t max_rpi, rpi_limit;
14413 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014414 struct lpfc_rpi_hdr *rpi_hdr;
14415
14416 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014417 rpi_limit = phba->sli4_hba.next_rpi;
14418
14419 /*
James Smart6d368e52011-05-24 11:44:12 -040014420 * Fetch the next logical rpi. Because this index is logical,
14421 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014422 */
14423 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014424 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14425 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014426 rpi = LPFC_RPI_ALLOC_ERROR;
14427 else {
14428 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14429 phba->sli4_hba.max_cfg_param.rpi_used++;
14430 phba->sli4_hba.rpi_count++;
14431 }
14432
14433 /*
14434 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014435 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014436 */
14437 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14438 (phba->sli4_hba.rpi_count >= max_rpi)) {
14439 spin_unlock_irq(&phba->hbalock);
14440 return rpi;
14441 }
14442
14443 /*
James Smart6d368e52011-05-24 11:44:12 -040014444 * RPI header postings are not required for SLI4 ports capable of
14445 * extents.
14446 */
14447 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14448 spin_unlock_irq(&phba->hbalock);
14449 return rpi;
14450 }
14451
14452 /*
James Smart6fb120a2009-05-22 14:52:59 -040014453 * If the driver is running low on rpi resources, allocate another
14454 * page now. Note that the next_rpi value is used because
14455 * it represents how many are actually in use whereas max_rpi notes
14456 * how many are supported max by the device.
14457 */
James Smart6d368e52011-05-24 11:44:12 -040014458 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014459 spin_unlock_irq(&phba->hbalock);
14460 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14461 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14462 if (!rpi_hdr) {
14463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14464 "2002 Error Could not grow rpi "
14465 "count\n");
14466 } else {
James Smart6d368e52011-05-24 11:44:12 -040014467 lrpi = rpi_hdr->start_rpi;
14468 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014469 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14470 }
14471 }
14472
14473 return rpi;
14474}
14475
14476/**
14477 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14478 * @phba: pointer to lpfc hba data structure.
14479 *
14480 * This routine is invoked to release an rpi to the pool of
14481 * available rpis maintained by the driver.
14482 **/
14483void
James Smartd7c47992010-06-08 18:31:54 -040014484__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14485{
14486 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14487 phba->sli4_hba.rpi_count--;
14488 phba->sli4_hba.max_cfg_param.rpi_used--;
14489 }
14490}
14491
14492/**
14493 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14494 * @phba: pointer to lpfc hba data structure.
14495 *
14496 * This routine is invoked to release an rpi to the pool of
14497 * available rpis maintained by the driver.
14498 **/
14499void
James Smart6fb120a2009-05-22 14:52:59 -040014500lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14501{
14502 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014503 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014504 spin_unlock_irq(&phba->hbalock);
14505}
14506
14507/**
14508 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14509 * @phba: pointer to lpfc hba data structure.
14510 *
14511 * This routine is invoked to remove the memory region that
14512 * provided rpi via a bitmask.
14513 **/
14514void
14515lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14516{
14517 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014518 kfree(phba->sli4_hba.rpi_ids);
14519 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014520}
14521
14522/**
14523 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14524 * @phba: pointer to lpfc hba data structure.
14525 *
14526 * This routine is invoked to remove the memory region that
14527 * provided rpi via a bitmask.
14528 **/
14529int
14530lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14531{
14532 LPFC_MBOXQ_t *mboxq;
14533 struct lpfc_hba *phba = ndlp->phba;
14534 int rc;
14535
14536 /* The port is notified of the header region via a mailbox command. */
14537 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14538 if (!mboxq)
14539 return -ENOMEM;
14540
14541 /* Post all rpi memory regions to the port. */
14542 lpfc_resume_rpi(mboxq, ndlp);
14543 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14544 if (rc == MBX_NOT_FINISHED) {
14545 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14546 "2010 Resume RPI Mailbox failed "
14547 "status %d, mbxStatus x%x\n", rc,
14548 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14549 mempool_free(mboxq, phba->mbox_mem_pool);
14550 return -EIO;
14551 }
14552 return 0;
14553}
14554
14555/**
14556 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014557 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014558 *
James Smart76a95d72010-11-20 23:11:48 -050014559 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014560 *
14561 * Returns:
14562 * 0 success
14563 * -Evalue otherwise
14564 **/
14565int
James Smart76a95d72010-11-20 23:11:48 -050014566lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014567{
14568 LPFC_MBOXQ_t *mboxq;
14569 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014570 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014571 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014572 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014573 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14574 if (!mboxq)
14575 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014576 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014577 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014578 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014579 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014580 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014581 "2022 INIT VPI Mailbox failed "
14582 "status %d, mbxStatus x%x\n", rc,
14583 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014584 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014585 }
James Smart6a9c52c2009-10-02 15:16:51 -040014586 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014587 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014588
14589 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014590}
14591
14592/**
14593 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14594 * @phba: pointer to lpfc hba data structure.
14595 * @mboxq: Pointer to mailbox object.
14596 *
14597 * This routine is invoked to manually add a single FCF record. The caller
14598 * must pass a completely initialized FCF_Record. This routine takes
14599 * care of the nonembedded mailbox operations.
14600 **/
14601static void
14602lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14603{
14604 void *virt_addr;
14605 union lpfc_sli4_cfg_shdr *shdr;
14606 uint32_t shdr_status, shdr_add_status;
14607
14608 virt_addr = mboxq->sge_array->addr[0];
14609 /* The IOCTL status is embedded in the mailbox subheader. */
14610 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14611 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14612 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14613
14614 if ((shdr_status || shdr_add_status) &&
14615 (shdr_status != STATUS_FCF_IN_USE))
14616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14617 "2558 ADD_FCF_RECORD mailbox failed with "
14618 "status x%x add_status x%x\n",
14619 shdr_status, shdr_add_status);
14620
14621 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14622}
14623
14624/**
14625 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14626 * @phba: pointer to lpfc hba data structure.
14627 * @fcf_record: pointer to the initialized fcf record to add.
14628 *
14629 * This routine is invoked to manually add a single FCF record. The caller
14630 * must pass a completely initialized FCF_Record. This routine takes
14631 * care of the nonembedded mailbox operations.
14632 **/
14633int
14634lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14635{
14636 int rc = 0;
14637 LPFC_MBOXQ_t *mboxq;
14638 uint8_t *bytep;
14639 void *virt_addr;
14640 dma_addr_t phys_addr;
14641 struct lpfc_mbx_sge sge;
14642 uint32_t alloc_len, req_len;
14643 uint32_t fcfindex;
14644
14645 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14646 if (!mboxq) {
14647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14648 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14649 return -ENOMEM;
14650 }
14651
14652 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14653 sizeof(uint32_t);
14654
14655 /* Allocate DMA memory and set up the non-embedded mailbox command */
14656 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14657 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14658 req_len, LPFC_SLI4_MBX_NEMBED);
14659 if (alloc_len < req_len) {
14660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14661 "2523 Allocated DMA memory size (x%x) is "
14662 "less than the requested DMA memory "
14663 "size (x%x)\n", alloc_len, req_len);
14664 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14665 return -ENOMEM;
14666 }
14667
14668 /*
14669 * Get the first SGE entry from the non-embedded DMA memory. This
14670 * routine only uses a single SGE.
14671 */
14672 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14673 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014674 virt_addr = mboxq->sge_array->addr[0];
14675 /*
14676 * Configure the FCF record for FCFI 0. This is the driver's
14677 * hardcoded default and gets used in nonFIP mode.
14678 */
14679 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14680 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14681 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14682
14683 /*
14684 * Copy the fcf_index and the FCF Record Data. The data starts after
14685 * the FCoE header plus word10. The data copy needs to be endian
14686 * correct.
14687 */
14688 bytep += sizeof(uint32_t);
14689 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14690 mboxq->vport = phba->pport;
14691 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14692 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14693 if (rc == MBX_NOT_FINISHED) {
14694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14695 "2515 ADD_FCF_RECORD mailbox failed with "
14696 "status 0x%x\n", rc);
14697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14698 rc = -EIO;
14699 } else
14700 rc = 0;
14701
14702 return rc;
14703}
14704
14705/**
14706 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14707 * @phba: pointer to lpfc hba data structure.
14708 * @fcf_record: pointer to the fcf record to write the default data.
14709 * @fcf_index: FCF table entry index.
14710 *
14711 * This routine is invoked to build the driver's default FCF record. The
14712 * values used are hardcoded. This routine handles memory initialization.
14713 *
14714 **/
14715void
14716lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14717 struct fcf_record *fcf_record,
14718 uint16_t fcf_index)
14719{
14720 memset(fcf_record, 0, sizeof(struct fcf_record));
14721 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14722 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14723 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14724 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14725 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14726 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14727 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14728 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14729 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14730 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14731 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14732 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14733 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014734 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014735 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14736 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14737 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14738 /* Set the VLAN bit map */
14739 if (phba->valid_vlan) {
14740 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14741 = 1 << (phba->vlan_id % 8);
14742 }
14743}
14744
14745/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014746 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014747 * @phba: pointer to lpfc hba data structure.
14748 * @fcf_index: FCF table entry offset.
14749 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014750 * This routine is invoked to scan the entire FCF table by reading FCF
14751 * record and processing it one at a time starting from the @fcf_index
14752 * for initial FCF discovery or fast FCF failover rediscovery.
14753 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014754 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014755 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014756 **/
14757int
James Smart0c9ab6f2010-02-26 14:15:57 -050014758lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014759{
14760 int rc = 0, error;
14761 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014762
James Smart32b97932009-07-19 10:01:21 -040014763 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014764 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14765 if (!mboxq) {
14766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14767 "2000 Failed to allocate mbox for "
14768 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014769 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014770 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014771 }
James Smartecfd03c2010-02-12 14:41:27 -050014772 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014773 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014774 if (rc) {
14775 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014776 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014777 }
James Smartecfd03c2010-02-12 14:41:27 -050014778 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014779 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014780 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014781
14782 spin_lock_irq(&phba->hbalock);
14783 phba->hba_flag |= FCF_TS_INPROG;
14784 spin_unlock_irq(&phba->hbalock);
14785
James Smart6fb120a2009-05-22 14:52:59 -040014786 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014787 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014788 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014789 else {
James Smart38b92ef2010-08-04 16:11:39 -040014790 /* Reset eligible FCF count for new scan */
14791 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014792 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014793 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014794 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014795fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014796 if (error) {
14797 if (mboxq)
14798 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014799 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014800 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014801 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014802 spin_unlock_irq(&phba->hbalock);
14803 }
James Smart6fb120a2009-05-22 14:52:59 -040014804 return error;
14805}
James Smarta0c87cb2009-07-19 10:01:10 -040014806
14807/**
James Smarta93ff372010-10-22 11:06:08 -040014808 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014809 * @phba: pointer to lpfc hba data structure.
14810 * @fcf_index: FCF table entry offset.
14811 *
14812 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014813 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014814 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014815 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014816 * otherwise.
14817 **/
14818int
14819lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14820{
14821 int rc = 0, error;
14822 LPFC_MBOXQ_t *mboxq;
14823
14824 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14825 if (!mboxq) {
14826 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14827 "2763 Failed to allocate mbox for "
14828 "READ_FCF cmd\n");
14829 error = -ENOMEM;
14830 goto fail_fcf_read;
14831 }
14832 /* Construct the read FCF record mailbox command */
14833 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14834 if (rc) {
14835 error = -EINVAL;
14836 goto fail_fcf_read;
14837 }
14838 /* Issue the mailbox command asynchronously */
14839 mboxq->vport = phba->pport;
14840 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14841 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14842 if (rc == MBX_NOT_FINISHED)
14843 error = -EIO;
14844 else
14845 error = 0;
14846
14847fail_fcf_read:
14848 if (error && mboxq)
14849 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14850 return error;
14851}
14852
14853/**
14854 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14855 * @phba: pointer to lpfc hba data structure.
14856 * @fcf_index: FCF table entry offset.
14857 *
14858 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040014859 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050014860 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014861 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014862 * otherwise.
14863 **/
14864int
14865lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14866{
14867 int rc = 0, error;
14868 LPFC_MBOXQ_t *mboxq;
14869
14870 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14871 if (!mboxq) {
14872 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14873 "2758 Failed to allocate mbox for "
14874 "READ_FCF cmd\n");
14875 error = -ENOMEM;
14876 goto fail_fcf_read;
14877 }
14878 /* Construct the read FCF record mailbox command */
14879 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14880 if (rc) {
14881 error = -EINVAL;
14882 goto fail_fcf_read;
14883 }
14884 /* Issue the mailbox command asynchronously */
14885 mboxq->vport = phba->pport;
14886 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14887 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14888 if (rc == MBX_NOT_FINISHED)
14889 error = -EIO;
14890 else
14891 error = 0;
14892
14893fail_fcf_read:
14894 if (error && mboxq)
14895 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14896 return error;
14897}
14898
14899/**
James Smart7d791df2011-07-22 18:37:52 -040014900 * lpfc_check_next_fcf_pri
14901 * phba pointer to the lpfc_hba struct for this port.
14902 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14903 * routine when the rr_bmask is empty. The FCF indecies are put into the
14904 * rr_bmask based on their priority level. Starting from the highest priority
14905 * to the lowest. The most likely FCF candidate will be in the highest
14906 * priority group. When this routine is called it searches the fcf_pri list for
14907 * next lowest priority group and repopulates the rr_bmask with only those
14908 * fcf_indexes.
14909 * returns:
14910 * 1=success 0=failure
14911 **/
14912int
14913lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14914{
14915 uint16_t next_fcf_pri;
14916 uint16_t last_index;
14917 struct lpfc_fcf_pri *fcf_pri;
14918 int rc;
14919 int ret = 0;
14920
14921 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14922 LPFC_SLI4_FCF_TBL_INDX_MAX);
14923 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14924 "3060 Last IDX %d\n", last_index);
14925 if (list_empty(&phba->fcf.fcf_pri_list)) {
14926 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14927 "3061 Last IDX %d\n", last_index);
14928 return 0; /* Empty rr list */
14929 }
14930 next_fcf_pri = 0;
14931 /*
14932 * Clear the rr_bmask and set all of the bits that are at this
14933 * priority.
14934 */
14935 memset(phba->fcf.fcf_rr_bmask, 0,
14936 sizeof(*phba->fcf.fcf_rr_bmask));
14937 spin_lock_irq(&phba->hbalock);
14938 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14939 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14940 continue;
14941 /*
14942 * the 1st priority that has not FLOGI failed
14943 * will be the highest.
14944 */
14945 if (!next_fcf_pri)
14946 next_fcf_pri = fcf_pri->fcf_rec.priority;
14947 spin_unlock_irq(&phba->hbalock);
14948 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14949 rc = lpfc_sli4_fcf_rr_index_set(phba,
14950 fcf_pri->fcf_rec.fcf_index);
14951 if (rc)
14952 return 0;
14953 }
14954 spin_lock_irq(&phba->hbalock);
14955 }
14956 /*
14957 * if next_fcf_pri was not set above and the list is not empty then
14958 * we have failed flogis on all of them. So reset flogi failed
14959 * and start at the begining.
14960 */
14961 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14962 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14963 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14964 /*
14965 * the 1st priority that has not FLOGI failed
14966 * will be the highest.
14967 */
14968 if (!next_fcf_pri)
14969 next_fcf_pri = fcf_pri->fcf_rec.priority;
14970 spin_unlock_irq(&phba->hbalock);
14971 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14972 rc = lpfc_sli4_fcf_rr_index_set(phba,
14973 fcf_pri->fcf_rec.fcf_index);
14974 if (rc)
14975 return 0;
14976 }
14977 spin_lock_irq(&phba->hbalock);
14978 }
14979 } else
14980 ret = 1;
14981 spin_unlock_irq(&phba->hbalock);
14982
14983 return ret;
14984}
14985/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014986 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14987 * @phba: pointer to lpfc hba data structure.
14988 *
14989 * This routine is to get the next eligible FCF record index in a round
14990 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040014991 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050014992 * shall be returned, otherwise, the next eligible FCF record's index
14993 * shall be returned.
14994 **/
14995uint16_t
14996lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14997{
14998 uint16_t next_fcf_index;
14999
James Smart3804dc82010-07-14 15:31:37 -040015000 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015001next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015002 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15003 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015004 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15005 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015006 next_fcf_index);
15007
James Smart0c9ab6f2010-02-26 14:15:57 -050015008 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015009 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15010 /*
15011 * If we have wrapped then we need to clear the bits that
15012 * have been tested so that we can detect when we should
15013 * change the priority level.
15014 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015015 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15016 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015017 }
15018
James Smart0c9ab6f2010-02-26 14:15:57 -050015019
James Smart3804dc82010-07-14 15:31:37 -040015020 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015021 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15022 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15023 /*
15024 * If next fcf index is not found check if there are lower
15025 * Priority level fcf's in the fcf_priority list.
15026 * Set up the rr_bmask with all of the avaiable fcf bits
15027 * at that level and continue the selection process.
15028 */
15029 if (lpfc_check_next_fcf_pri_level(phba))
15030 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015031 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15032 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015033 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15034 return LPFC_FCOE_FCF_NEXT_NONE;
15035 else {
15036 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15037 "3063 Only FCF available idx %d, flag %x\n",
15038 next_fcf_index,
15039 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15040 return next_fcf_index;
15041 }
James Smart3804dc82010-07-14 15:31:37 -040015042 }
15043
James Smart7d791df2011-07-22 18:37:52 -040015044 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15045 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15046 LPFC_FCF_FLOGI_FAILED)
15047 goto next_priority;
15048
James Smart3804dc82010-07-14 15:31:37 -040015049 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015050 "2845 Get next roundrobin failover FCF (x%x)\n",
15051 next_fcf_index);
15052
James Smart0c9ab6f2010-02-26 14:15:57 -050015053 return next_fcf_index;
15054}
15055
15056/**
15057 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15058 * @phba: pointer to lpfc hba data structure.
15059 *
15060 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015061 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015062 * does not go beyond the range of the driver allocated bmask dimension
15063 * before setting the bit.
15064 *
15065 * Returns 0 if the index bit successfully set, otherwise, it returns
15066 * -EINVAL.
15067 **/
15068int
15069lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15070{
15071 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15072 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015073 "2610 FCF (x%x) reached driver's book "
15074 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015075 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15076 return -EINVAL;
15077 }
15078 /* Set the eligible FCF record index bmask */
15079 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15080
James Smart3804dc82010-07-14 15:31:37 -040015081 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015082 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015083 "bmask\n", fcf_index);
15084
James Smart0c9ab6f2010-02-26 14:15:57 -050015085 return 0;
15086}
15087
15088/**
James Smart3804dc82010-07-14 15:31:37 -040015089 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015090 * @phba: pointer to lpfc hba data structure.
15091 *
15092 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015093 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015094 * does not go beyond the range of the driver allocated bmask dimension
15095 * before clearing the bit.
15096 **/
15097void
15098lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15099{
James Smart7d791df2011-07-22 18:37:52 -040015100 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015101 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15102 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015103 "2762 FCF (x%x) reached driver's book "
15104 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015105 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15106 return;
15107 }
15108 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015109 spin_lock_irq(&phba->hbalock);
15110 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15111 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15112 list_del_init(&fcf_pri->list);
15113 break;
15114 }
15115 }
15116 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015117 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015118
15119 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015120 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015121 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015122}
15123
15124/**
James Smartecfd03c2010-02-12 14:41:27 -050015125 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15126 * @phba: pointer to lpfc hba data structure.
15127 *
15128 * This routine is the completion routine for the rediscover FCF table mailbox
15129 * command. If the mailbox command returned failure, it will try to stop the
15130 * FCF rediscover wait timer.
15131 **/
15132void
15133lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15134{
15135 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15136 uint32_t shdr_status, shdr_add_status;
15137
15138 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15139
15140 shdr_status = bf_get(lpfc_mbox_hdr_status,
15141 &redisc_fcf->header.cfg_shdr.response);
15142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15143 &redisc_fcf->header.cfg_shdr.response);
15144 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015145 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015146 "2746 Requesting for FCF rediscovery failed "
15147 "status x%x add_status x%x\n",
15148 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015149 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015150 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015151 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015152 spin_unlock_irq(&phba->hbalock);
15153 /*
15154 * CVL event triggered FCF rediscover request failed,
15155 * last resort to re-try current registered FCF entry.
15156 */
15157 lpfc_retry_pport_discovery(phba);
15158 } else {
15159 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015160 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015161 spin_unlock_irq(&phba->hbalock);
15162 /*
15163 * DEAD FCF event triggered FCF rediscover request
15164 * failed, last resort to fail over as a link down
15165 * to FCF registration.
15166 */
15167 lpfc_sli4_fcf_dead_failthrough(phba);
15168 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015169 } else {
15170 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015171 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015172 /*
15173 * Start FCF rediscovery wait timer for pending FCF
15174 * before rescan FCF record table.
15175 */
15176 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015177 }
James Smartecfd03c2010-02-12 14:41:27 -050015178
15179 mempool_free(mbox, phba->mbox_mem_pool);
15180}
15181
15182/**
James Smart3804dc82010-07-14 15:31:37 -040015183 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015184 * @phba: pointer to lpfc hba data structure.
15185 *
15186 * This routine is invoked to request for rediscovery of the entire FCF table
15187 * by the port.
15188 **/
15189int
15190lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15191{
15192 LPFC_MBOXQ_t *mbox;
15193 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15194 int rc, length;
15195
James Smart0c9ab6f2010-02-26 14:15:57 -050015196 /* Cancel retry delay timers to all vports before FCF rediscover */
15197 lpfc_cancel_all_vport_retry_delay_timer(phba);
15198
James Smartecfd03c2010-02-12 14:41:27 -050015199 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15200 if (!mbox) {
15201 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15202 "2745 Failed to allocate mbox for "
15203 "requesting FCF rediscover.\n");
15204 return -ENOMEM;
15205 }
15206
15207 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15208 sizeof(struct lpfc_sli4_cfg_mhdr));
15209 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15210 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15211 length, LPFC_SLI4_MBX_EMBED);
15212
15213 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15214 /* Set count to 0 for invalidating the entire FCF database */
15215 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15216
15217 /* Issue the mailbox command asynchronously */
15218 mbox->vport = phba->pport;
15219 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15220 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15221
15222 if (rc == MBX_NOT_FINISHED) {
15223 mempool_free(mbox, phba->mbox_mem_pool);
15224 return -EIO;
15225 }
15226 return 0;
15227}
15228
15229/**
James Smartfc2b9892010-02-26 14:15:29 -050015230 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15231 * @phba: pointer to lpfc hba data structure.
15232 *
15233 * This function is the failover routine as a last resort to the FCF DEAD
15234 * event when driver failed to perform fast FCF failover.
15235 **/
15236void
15237lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15238{
15239 uint32_t link_state;
15240
15241 /*
15242 * Last resort as FCF DEAD event failover will treat this as
15243 * a link down, but save the link state because we don't want
15244 * it to be changed to Link Down unless it is already down.
15245 */
15246 link_state = phba->link_state;
15247 lpfc_linkdown(phba);
15248 phba->link_state = link_state;
15249
15250 /* Unregister FCF if no devices connected to it */
15251 lpfc_unregister_unused_fcf(phba);
15252}
15253
15254/**
James Smarta0c87cb2009-07-19 10:01:10 -040015255 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15256 * @phba: pointer to lpfc hba data structure.
15257 *
15258 * This function read region 23 and parse TLV for port status to
15259 * decide if the user disaled the port. If the TLV indicates the
15260 * port is disabled, the hba_flag is set accordingly.
15261 **/
15262void
15263lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15264{
15265 LPFC_MBOXQ_t *pmb = NULL;
15266 MAILBOX_t *mb;
15267 uint8_t *rgn23_data = NULL;
15268 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
15269 int rc;
15270
15271 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15272 if (!pmb) {
15273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15274 "2600 lpfc_sli_read_serdes_param failed to"
15275 " allocate mailbox memory\n");
15276 goto out;
15277 }
15278 mb = &pmb->u.mb;
15279
15280 /* Get adapter Region 23 data */
15281 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15282 if (!rgn23_data)
15283 goto out;
15284
15285 do {
15286 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15287 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15288
15289 if (rc != MBX_SUCCESS) {
15290 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15291 "2601 lpfc_sli_read_link_ste failed to"
15292 " read config region 23 rc 0x%x Status 0x%x\n",
15293 rc, mb->mbxStatus);
15294 mb->un.varDmp.word_cnt = 0;
15295 }
15296 /*
15297 * dump mem may return a zero when finished or we got a
15298 * mailbox error, either way we are done.
15299 */
15300 if (mb->un.varDmp.word_cnt == 0)
15301 break;
15302 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15303 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15304
15305 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15306 rgn23_data + offset,
15307 mb->un.varDmp.word_cnt);
15308 offset += mb->un.varDmp.word_cnt;
15309 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15310
15311 data_size = offset;
15312 offset = 0;
15313
15314 if (!data_size)
15315 goto out;
15316
15317 /* Check the region signature first */
15318 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15320 "2619 Config region 23 has bad signature\n");
15321 goto out;
15322 }
15323 offset += 4;
15324
15325 /* Check the data structure version */
15326 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15328 "2620 Config region 23 has bad version\n");
15329 goto out;
15330 }
15331 offset += 4;
15332
15333 /* Parse TLV entries in the region */
15334 while (offset < data_size) {
15335 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15336 break;
15337 /*
15338 * If the TLV is not driver specific TLV or driver id is
15339 * not linux driver id, skip the record.
15340 */
15341 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15342 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15343 (rgn23_data[offset + 3] != 0)) {
15344 offset += rgn23_data[offset + 1] * 4 + 4;
15345 continue;
15346 }
15347
15348 /* Driver found a driver specific TLV in the config region */
15349 sub_tlv_len = rgn23_data[offset + 1] * 4;
15350 offset += 4;
15351 tlv_offset = 0;
15352
15353 /*
15354 * Search for configured port state sub-TLV.
15355 */
15356 while ((offset < data_size) &&
15357 (tlv_offset < sub_tlv_len)) {
15358 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15359 offset += 4;
15360 tlv_offset += 4;
15361 break;
15362 }
15363 if (rgn23_data[offset] != PORT_STE_TYPE) {
15364 offset += rgn23_data[offset + 1] * 4 + 4;
15365 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15366 continue;
15367 }
15368
15369 /* This HBA contains PORT_STE configured */
15370 if (!rgn23_data[offset + 2])
15371 phba->hba_flag |= LINK_DISABLED;
15372
15373 goto out;
15374 }
15375 }
15376out:
15377 if (pmb)
15378 mempool_free(pmb, phba->mbox_mem_pool);
15379 kfree(rgn23_data);
15380 return;
15381}
James Smart695a8142010-01-26 23:08:03 -050015382
15383/**
James Smart52d52442011-05-24 11:42:45 -040015384 * lpfc_wr_object - write an object to the firmware
15385 * @phba: HBA structure that indicates port to create a queue on.
15386 * @dmabuf_list: list of dmabufs to write to the port.
15387 * @size: the total byte value of the objects to write to the port.
15388 * @offset: the current offset to be used to start the transfer.
15389 *
15390 * This routine will create a wr_object mailbox command to send to the port.
15391 * the mailbox command will be constructed using the dma buffers described in
15392 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15393 * BDEs that the imbedded mailbox can support. The @offset variable will be
15394 * used to indicate the starting offset of the transfer and will also return
15395 * the offset after the write object mailbox has completed. @size is used to
15396 * determine the end of the object and whether the eof bit should be set.
15397 *
15398 * Return 0 is successful and offset will contain the the new offset to use
15399 * for the next write.
15400 * Return negative value for error cases.
15401 **/
15402int
15403lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15404 uint32_t size, uint32_t *offset)
15405{
15406 struct lpfc_mbx_wr_object *wr_object;
15407 LPFC_MBOXQ_t *mbox;
15408 int rc = 0, i = 0;
15409 uint32_t shdr_status, shdr_add_status;
15410 uint32_t mbox_tmo;
15411 union lpfc_sli4_cfg_shdr *shdr;
15412 struct lpfc_dmabuf *dmabuf;
15413 uint32_t written = 0;
15414
15415 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15416 if (!mbox)
15417 return -ENOMEM;
15418
15419 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15420 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15421 sizeof(struct lpfc_mbx_wr_object) -
15422 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15423
15424 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15425 wr_object->u.request.write_offset = *offset;
15426 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15427 wr_object->u.request.object_name[0] =
15428 cpu_to_le32(wr_object->u.request.object_name[0]);
15429 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15430 list_for_each_entry(dmabuf, dmabuf_list, list) {
15431 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15432 break;
15433 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15434 wr_object->u.request.bde[i].addrHigh =
15435 putPaddrHigh(dmabuf->phys);
15436 if (written + SLI4_PAGE_SIZE >= size) {
15437 wr_object->u.request.bde[i].tus.f.bdeSize =
15438 (size - written);
15439 written += (size - written);
15440 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15441 } else {
15442 wr_object->u.request.bde[i].tus.f.bdeSize =
15443 SLI4_PAGE_SIZE;
15444 written += SLI4_PAGE_SIZE;
15445 }
15446 i++;
15447 }
15448 wr_object->u.request.bde_count = i;
15449 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15450 if (!phba->sli4_hba.intr_enable)
15451 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15452 else {
James Smarta183a152011-10-10 21:32:43 -040015453 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015454 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15455 }
15456 /* The IOCTL status is embedded in the mailbox subheader. */
15457 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15458 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15459 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15460 if (rc != MBX_TIMEOUT)
15461 mempool_free(mbox, phba->mbox_mem_pool);
15462 if (shdr_status || shdr_add_status || rc) {
15463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15464 "3025 Write Object mailbox failed with "
15465 "status x%x add_status x%x, mbx status x%x\n",
15466 shdr_status, shdr_add_status, rc);
15467 rc = -ENXIO;
15468 } else
15469 *offset += wr_object->u.response.actual_write_length;
15470 return rc;
15471}
15472
15473/**
James Smart695a8142010-01-26 23:08:03 -050015474 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15475 * @vport: pointer to vport data structure.
15476 *
15477 * This function iterate through the mailboxq and clean up all REG_LOGIN
15478 * and REG_VPI mailbox commands associated with the vport. This function
15479 * is called when driver want to restart discovery of the vport due to
15480 * a Clear Virtual Link event.
15481 **/
15482void
15483lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15484{
15485 struct lpfc_hba *phba = vport->phba;
15486 LPFC_MBOXQ_t *mb, *nextmb;
15487 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015488 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015489 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015490 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015491 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015492 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015493
James Smartd439d282010-09-29 11:18:45 -040015494 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015495 spin_lock_irq(&phba->hbalock);
15496 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15497 if (mb->vport != vport)
15498 continue;
15499
15500 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15501 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15502 continue;
15503
James Smartd439d282010-09-29 11:18:45 -040015504 list_del(&mb->list);
15505 list_add_tail(&mb->list, &mbox_cmd_list);
15506 }
15507 /* Clean up active mailbox command with the vport */
15508 mb = phba->sli.mbox_active;
15509 if (mb && (mb->vport == vport)) {
15510 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15511 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15512 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15513 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15514 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15515 /* Put reference count for delayed processing */
15516 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15517 /* Unregister the RPI when mailbox complete */
15518 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15519 }
15520 }
James Smart63e801c2010-11-20 23:14:19 -050015521 /* Cleanup any mailbox completions which are not yet processed */
15522 do {
15523 restart_loop = 0;
15524 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15525 /*
15526 * If this mailox is already processed or it is
15527 * for another vport ignore it.
15528 */
15529 if ((mb->vport != vport) ||
15530 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15531 continue;
15532
15533 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15534 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15535 continue;
15536
15537 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15538 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15539 ndlp = (struct lpfc_nodelist *)mb->context2;
15540 /* Unregister the RPI when mailbox complete */
15541 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15542 restart_loop = 1;
15543 spin_unlock_irq(&phba->hbalock);
15544 spin_lock(shost->host_lock);
15545 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15546 spin_unlock(shost->host_lock);
15547 spin_lock_irq(&phba->hbalock);
15548 break;
15549 }
15550 }
15551 } while (restart_loop);
15552
James Smartd439d282010-09-29 11:18:45 -040015553 spin_unlock_irq(&phba->hbalock);
15554
15555 /* Release the cleaned-up mailbox commands */
15556 while (!list_empty(&mbox_cmd_list)) {
15557 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015558 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15559 mp = (struct lpfc_dmabuf *) (mb->context1);
15560 if (mp) {
15561 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15562 kfree(mp);
15563 }
James Smart78730cf2010-04-06 15:06:30 -040015564 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015565 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015566 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015567 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015568 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015569 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015570 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015571 }
James Smart695a8142010-01-26 23:08:03 -050015572 }
James Smart695a8142010-01-26 23:08:03 -050015573 mempool_free(mb, phba->mbox_mem_pool);
15574 }
James Smartd439d282010-09-29 11:18:45 -040015575
15576 /* Release the ndlp with the cleaned-up active mailbox command */
15577 if (act_mbx_ndlp) {
15578 spin_lock(shost->host_lock);
15579 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15580 spin_unlock(shost->host_lock);
15581 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015582 }
James Smart695a8142010-01-26 23:08:03 -050015583}
15584
James Smart2a9bf3d2010-06-07 15:24:45 -040015585/**
15586 * lpfc_drain_txq - Drain the txq
15587 * @phba: Pointer to HBA context object.
15588 *
15589 * This function attempt to submit IOCBs on the txq
15590 * to the adapter. For SLI4 adapters, the txq contains
15591 * ELS IOCBs that have been deferred because the there
15592 * are no SGLs. This congestion can occur with large
15593 * vport counts during node discovery.
15594 **/
15595
15596uint32_t
15597lpfc_drain_txq(struct lpfc_hba *phba)
15598{
15599 LIST_HEAD(completions);
15600 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15601 struct lpfc_iocbq *piocbq = 0;
15602 unsigned long iflags = 0;
15603 char *fail_msg = NULL;
15604 struct lpfc_sglq *sglq;
15605 union lpfc_wqe wqe;
15606
15607 spin_lock_irqsave(&phba->hbalock, iflags);
15608 if (pring->txq_cnt > pring->txq_max)
15609 pring->txq_max = pring->txq_cnt;
15610
15611 spin_unlock_irqrestore(&phba->hbalock, iflags);
15612
15613 while (pring->txq_cnt) {
15614 spin_lock_irqsave(&phba->hbalock, iflags);
15615
James Smart19ca7602010-11-20 23:11:55 -050015616 piocbq = lpfc_sli_ringtx_get(phba, pring);
15617 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015618 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015619 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015620 spin_unlock_irqrestore(&phba->hbalock, iflags);
15621 break;
15622 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015623 if (!piocbq) {
15624 /* The txq_cnt out of sync. This should
15625 * never happen
15626 */
15627 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015628 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015629 spin_unlock_irqrestore(&phba->hbalock, iflags);
15630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15631 "2823 txq empty and txq_cnt is %d\n ",
15632 pring->txq_cnt);
15633 break;
15634 }
15635 }
15636
15637 /* The xri and iocb resources secured,
15638 * attempt to issue request
15639 */
James Smart6d368e52011-05-24 11:44:12 -040015640 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015641 piocbq->sli4_xritag = sglq->sli4_xritag;
15642 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15643 fail_msg = "to convert bpl to sgl";
15644 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15645 fail_msg = "to convert iocb to wqe";
15646 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15647 fail_msg = " - Wq is full";
15648 else
15649 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15650
15651 if (fail_msg) {
15652 /* Failed means we can't issue and need to cancel */
15653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15654 "2822 IOCB failed %s iotag 0x%x "
15655 "xri 0x%x\n",
15656 fail_msg,
15657 piocbq->iotag, piocbq->sli4_xritag);
15658 list_add_tail(&piocbq->list, &completions);
15659 }
15660 spin_unlock_irqrestore(&phba->hbalock, iflags);
15661 }
15662
James Smart2a9bf3d2010-06-07 15:24:45 -040015663 /* Cancel all the IOCBs that cannot be issued */
15664 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15665 IOERR_SLI_ABORTED);
15666
15667 return pring->txq_cnt;
15668}