blob: 6740c52e99c4c9f91f0eeb7e7be539bc64e85b5b [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 */
382 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
385 LPFC_RQ_POST_BATCH);
386 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 Smart6d368e52011-05-24 11:44:12 -0400563 rrq->xritag = phba->sli4_hba.xri_ids[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,
1867 lpfc_hbq_defs[qno]->entry_count);
1868 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 Smart04c68492009-05-22 14:52:52 -04002203 "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,
2206 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002207 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002208 phba->work_hs = HS_FFER3;
2209 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002210 continue;
dea31012005-04-17 16:05:31 -05002211 }
2212
dea31012005-04-17 16:05:31 -05002213 if (pmbox->mbxStatus) {
2214 phba->sli.slistat.mbox_stat_err++;
2215 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2216 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002217 lpfc_printf_log(phba, KERN_INFO,
2218 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002219 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002220 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002221 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002222 pmb->vport ? pmb->vport->vpi :0,
2223 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002224 lpfc_sli4_mbox_opcode_get(phba,
2225 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002226 pmbox->mbxStatus,
2227 pmbox->un.varWords[0],
2228 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002229 pmbox->mbxStatus = 0;
2230 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002231 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002232 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002233 continue;
dea31012005-04-17 16:05:31 -05002234 }
2235 }
2236
2237 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002238 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002239 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002240 "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 -05002241 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002242 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002243 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002244 pmb->mbox_cmpl,
2245 *((uint32_t *) pmbox),
2246 pmbox->un.varWords[0],
2247 pmbox->un.varWords[1],
2248 pmbox->un.varWords[2],
2249 pmbox->un.varWords[3],
2250 pmbox->un.varWords[4],
2251 pmbox->un.varWords[5],
2252 pmbox->un.varWords[6],
2253 pmbox->un.varWords[7]);
2254
James Smart92d7f7b2007-06-17 19:56:38 -05002255 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002256 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002257 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002258 return 0;
dea31012005-04-17 16:05:31 -05002259}
James Smart92d7f7b2007-06-17 19:56:38 -05002260
James Smarte59058c2008-08-24 21:49:00 -04002261/**
James Smart3621a712009-04-06 18:47:14 -04002262 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002263 * @phba: Pointer to HBA context object.
2264 * @pring: Pointer to driver SLI ring object.
2265 * @tag: buffer tag.
2266 *
2267 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2268 * is set in the tag the buffer is posted for a particular exchange,
2269 * the function will return the buffer without replacing the buffer.
2270 * If the buffer is for unsolicited ELS or CT traffic, this function
2271 * returns the buffer and also posts another buffer to the firmware.
2272 **/
James Smart76bb24e2007-10-27 13:38:00 -04002273static struct lpfc_dmabuf *
2274lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002275 struct lpfc_sli_ring *pring,
2276 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002277{
James Smart9f1e1b52008-12-04 22:39:40 -05002278 struct hbq_dmabuf *hbq_entry;
2279
James Smart76bb24e2007-10-27 13:38:00 -04002280 if (tag & QUE_BUFTAG_BIT)
2281 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002282 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2283 if (!hbq_entry)
2284 return NULL;
2285 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002286}
James Smart57127f12007-10-27 13:37:05 -04002287
James Smart3772a992009-05-22 14:50:54 -04002288/**
2289 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2290 * @phba: Pointer to HBA context object.
2291 * @pring: Pointer to driver SLI ring object.
2292 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2293 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2294 * @fch_type: the type for the first frame of the sequence.
2295 *
2296 * This function is called with no lock held. This function uses the r_ctl and
2297 * type of the received sequence to find the correct callback function to call
2298 * to process the sequence.
2299 **/
2300static int
2301lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2302 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2303 uint32_t fch_type)
2304{
2305 int i;
2306
2307 /* unSolicited Responses */
2308 if (pring->prt[0].profile) {
2309 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2310 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2311 saveq);
2312 return 1;
2313 }
2314 /* We must search, based on rctl / type
2315 for the right routine */
2316 for (i = 0; i < pring->num_mask; i++) {
2317 if ((pring->prt[i].rctl == fch_r_ctl) &&
2318 (pring->prt[i].type == fch_type)) {
2319 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2320 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2321 (phba, pring, saveq);
2322 return 1;
2323 }
2324 }
2325 return 0;
2326}
James Smarte59058c2008-08-24 21:49:00 -04002327
2328/**
James Smart3621a712009-04-06 18:47:14 -04002329 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002330 * @phba: Pointer to HBA context object.
2331 * @pring: Pointer to driver SLI ring object.
2332 * @saveq: Pointer to the unsolicited iocb.
2333 *
2334 * This function is called with no lock held by the ring event handler
2335 * when there is an unsolicited iocb posted to the response ring by the
2336 * firmware. This function gets the buffer associated with the iocbs
2337 * and calls the event handler for the ring. This function handles both
2338 * qring buffers and hbq buffers.
2339 * When the function returns 1 the caller can free the iocb object otherwise
2340 * upper layer functions will free the iocb objects.
2341 **/
dea31012005-04-17 16:05:31 -05002342static int
2343lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2344 struct lpfc_iocbq *saveq)
2345{
2346 IOCB_t * irsp;
2347 WORD5 * w5p;
2348 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002349 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002350 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002351 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002352
2353 match = 0;
2354 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002355
2356 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2357 if (pring->lpfc_sli_rcv_async_status)
2358 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2359 else
2360 lpfc_printf_log(phba,
2361 KERN_WARNING,
2362 LOG_SLI,
2363 "0316 Ring %d handler: unexpected "
2364 "ASYNC_STATUS iocb received evt_code "
2365 "0x%x\n",
2366 pring->ringno,
2367 irsp->un.asyncstat.evt_code);
2368 return 1;
2369 }
2370
James Smart3163f722008-02-08 18:50:25 -05002371 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2372 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2373 if (irsp->ulpBdeCount > 0) {
2374 dmzbuf = lpfc_sli_get_buff(phba, pring,
2375 irsp->un.ulpWord[3]);
2376 lpfc_in_buf_free(phba, dmzbuf);
2377 }
2378
2379 if (irsp->ulpBdeCount > 1) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->unsli3.sli3Words[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 2) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[7]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 return 1;
2392 }
2393
James Smart92d7f7b2007-06-17 19:56:38 -05002394 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002395 if (irsp->ulpBdeCount != 0) {
2396 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002397 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002398 if (!saveq->context2)
2399 lpfc_printf_log(phba,
2400 KERN_ERR,
2401 LOG_SLI,
2402 "0341 Ring %d Cannot find buffer for "
2403 "an unsolicited iocb. tag 0x%x\n",
2404 pring->ringno,
2405 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002406 }
2407 if (irsp->ulpBdeCount == 2) {
2408 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002409 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002410 if (!saveq->context3)
2411 lpfc_printf_log(phba,
2412 KERN_ERR,
2413 LOG_SLI,
2414 "0342 Ring %d Cannot find buffer for an"
2415 " unsolicited iocb. tag 0x%x\n",
2416 pring->ringno,
2417 irsp->unsli3.sli3Words[7]);
2418 }
2419 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002420 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002421 if (irsp->ulpBdeCount != 0) {
2422 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2423 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002424 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002425 lpfc_printf_log(phba,
2426 KERN_ERR,
2427 LOG_SLI,
2428 "0343 Ring %d Cannot find "
2429 "buffer for an unsolicited iocb"
2430 ". tag 0x%x\n", pring->ringno,
2431 irsp->un.ulpWord[3]);
2432 }
2433 if (irsp->ulpBdeCount == 2) {
2434 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2435 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002436 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002437 lpfc_printf_log(phba,
2438 KERN_ERR,
2439 LOG_SLI,
2440 "0344 Ring %d Cannot find "
2441 "buffer for an unsolicited "
2442 "iocb. tag 0x%x\n",
2443 pring->ringno,
2444 irsp->unsli3.sli3Words[7]);
2445 }
2446 }
James Smart92d7f7b2007-06-17 19:56:38 -05002447 }
James Smart9c2face2008-01-11 01:53:18 -05002448 if (irsp->ulpBdeCount != 0 &&
2449 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2450 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2451 int found = 0;
2452
2453 /* search continue save q for same XRI */
2454 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2455 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2456 list_add_tail(&saveq->list, &iocbq->list);
2457 found = 1;
2458 break;
2459 }
2460 }
2461 if (!found)
2462 list_add_tail(&saveq->clist,
2463 &pring->iocb_continue_saveq);
2464 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2465 list_del_init(&iocbq->clist);
2466 saveq = iocbq;
2467 irsp = &(saveq->iocb);
2468 } else
2469 return 0;
2470 }
2471 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2472 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2473 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002474 Rctl = FC_RCTL_ELS_REQ;
2475 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002476 } else {
2477 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2478 Rctl = w5p->hcsw.Rctl;
2479 Type = w5p->hcsw.Type;
2480
2481 /* Firmware Workaround */
2482 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2483 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2484 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002485 Rctl = FC_RCTL_ELS_REQ;
2486 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002487 w5p->hcsw.Rctl = Rctl;
2488 w5p->hcsw.Type = Type;
2489 }
2490 }
James Smart92d7f7b2007-06-17 19:56:38 -05002491
James Smart3772a992009-05-22 14:50:54 -04002492 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002493 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002494 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002495 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002496 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002497
James Smart92d7f7b2007-06-17 19:56:38 -05002498 return 1;
dea31012005-04-17 16:05:31 -05002499}
2500
James Smarte59058c2008-08-24 21:49:00 -04002501/**
James Smart3621a712009-04-06 18:47:14 -04002502 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002503 * @phba: Pointer to HBA context object.
2504 * @pring: Pointer to driver SLI ring object.
2505 * @prspiocb: Pointer to response iocb object.
2506 *
2507 * This function looks up the iocb_lookup table to get the command iocb
2508 * corresponding to the given response iocb using the iotag of the
2509 * response iocb. This function is called with the hbalock held.
2510 * This function returns the command iocb object if it finds the command
2511 * iocb else returns NULL.
2512 **/
dea31012005-04-17 16:05:31 -05002513static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002514lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2515 struct lpfc_sli_ring *pring,
2516 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002517{
dea31012005-04-17 16:05:31 -05002518 struct lpfc_iocbq *cmd_iocb = NULL;
2519 uint16_t iotag;
2520
James Bottomley604a3e32005-10-29 10:28:33 -05002521 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002522
James Bottomley604a3e32005-10-29 10:28:33 -05002523 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2524 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002525 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002526 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2527 pring->txcmplq_cnt--;
2528 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2529 }
James Bottomley604a3e32005-10-29 10:28:33 -05002530 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002531 }
2532
dea31012005-04-17 16:05:31 -05002533 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002534 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002535 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002536 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002537 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002538 return NULL;
2539}
2540
James Smarte59058c2008-08-24 21:49:00 -04002541/**
James Smart3772a992009-05-22 14:50:54 -04002542 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2543 * @phba: Pointer to HBA context object.
2544 * @pring: Pointer to driver SLI ring object.
2545 * @iotag: IOCB tag.
2546 *
2547 * This function looks up the iocb_lookup table to get the command iocb
2548 * corresponding to the given iotag. This function is called with the
2549 * hbalock held.
2550 * This function returns the command iocb object if it finds the command
2551 * iocb else returns NULL.
2552 **/
2553static struct lpfc_iocbq *
2554lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2555 struct lpfc_sli_ring *pring, uint16_t iotag)
2556{
2557 struct lpfc_iocbq *cmd_iocb;
2558
2559 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2560 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2561 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002562 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2563 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2564 pring->txcmplq_cnt--;
2565 }
James Smart3772a992009-05-22 14:50:54 -04002566 return cmd_iocb;
2567 }
2568
2569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2570 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2571 iotag, phba->sli.last_iotag);
2572 return NULL;
2573}
2574
2575/**
James Smart3621a712009-04-06 18:47:14 -04002576 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002577 * @phba: Pointer to HBA context object.
2578 * @pring: Pointer to driver SLI ring object.
2579 * @saveq: Pointer to the response iocb to be processed.
2580 *
2581 * This function is called by the ring event handler for non-fcp
2582 * rings when there is a new response iocb in the response ring.
2583 * The caller is not required to hold any locks. This function
2584 * gets the command iocb associated with the response iocb and
2585 * calls the completion handler for the command iocb. If there
2586 * is no completion handler, the function will free the resources
2587 * associated with command iocb. If the response iocb is for
2588 * an already aborted command iocb, the status of the completion
2589 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2590 * This function always returns 1.
2591 **/
dea31012005-04-17 16:05:31 -05002592static int
James Smart2e0fef82007-06-17 19:56:36 -05002593lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002594 struct lpfc_iocbq *saveq)
2595{
James Smart2e0fef82007-06-17 19:56:36 -05002596 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002597 int rc = 1;
2598 unsigned long iflag;
2599
2600 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002601 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002602 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002603 spin_unlock_irqrestore(&phba->hbalock, iflag);
2604
dea31012005-04-17 16:05:31 -05002605 if (cmdiocbp) {
2606 if (cmdiocbp->iocb_cmpl) {
2607 /*
James Smartea2151b2008-09-07 11:52:10 -04002608 * If an ELS command failed send an event to mgmt
2609 * application.
2610 */
2611 if (saveq->iocb.ulpStatus &&
2612 (pring->ringno == LPFC_ELS_RING) &&
2613 (cmdiocbp->iocb.ulpCommand ==
2614 CMD_ELS_REQUEST64_CR))
2615 lpfc_send_els_failure_event(phba,
2616 cmdiocbp, saveq);
2617
2618 /*
dea31012005-04-17 16:05:31 -05002619 * Post all ELS completions to the worker thread.
2620 * All other are passed to the completion callback.
2621 */
2622 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002623 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2624 (cmdiocbp->iocb_flag &
2625 LPFC_DRIVER_ABORTED)) {
2626 spin_lock_irqsave(&phba->hbalock,
2627 iflag);
James Smart07951072007-04-25 09:51:38 -04002628 cmdiocbp->iocb_flag &=
2629 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002630 spin_unlock_irqrestore(&phba->hbalock,
2631 iflag);
James Smart07951072007-04-25 09:51:38 -04002632 saveq->iocb.ulpStatus =
2633 IOSTAT_LOCAL_REJECT;
2634 saveq->iocb.un.ulpWord[4] =
2635 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002636
2637 /* Firmware could still be in progress
2638 * of DMAing payload, so don't free data
2639 * buffer till after a hbeat.
2640 */
James Smart341af102010-01-26 23:07:37 -05002641 spin_lock_irqsave(&phba->hbalock,
2642 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002643 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002644 spin_unlock_irqrestore(&phba->hbalock,
2645 iflag);
2646 }
James Smart0f65ff62010-02-26 14:14:23 -05002647 if (phba->sli_rev == LPFC_SLI_REV4) {
2648 if (saveq->iocb_flag &
2649 LPFC_EXCHANGE_BUSY) {
2650 /* Set cmdiocb flag for the
2651 * exchange busy so sgl (xri)
2652 * will not be released until
2653 * the abort xri is received
2654 * from hba.
2655 */
2656 spin_lock_irqsave(
2657 &phba->hbalock, iflag);
2658 cmdiocbp->iocb_flag |=
2659 LPFC_EXCHANGE_BUSY;
2660 spin_unlock_irqrestore(
2661 &phba->hbalock, iflag);
2662 }
2663 if (cmdiocbp->iocb_flag &
2664 LPFC_DRIVER_ABORTED) {
2665 /*
2666 * Clear LPFC_DRIVER_ABORTED
2667 * bit in case it was driver
2668 * initiated abort.
2669 */
2670 spin_lock_irqsave(
2671 &phba->hbalock, iflag);
2672 cmdiocbp->iocb_flag &=
2673 ~LPFC_DRIVER_ABORTED;
2674 spin_unlock_irqrestore(
2675 &phba->hbalock, iflag);
2676 cmdiocbp->iocb.ulpStatus =
2677 IOSTAT_LOCAL_REJECT;
2678 cmdiocbp->iocb.un.ulpWord[4] =
2679 IOERR_ABORT_REQUESTED;
2680 /*
2681 * For SLI4, irsiocb contains
2682 * NO_XRI in sli_xritag, it
2683 * shall not affect releasing
2684 * sgl (xri) process.
2685 */
2686 saveq->iocb.ulpStatus =
2687 IOSTAT_LOCAL_REJECT;
2688 saveq->iocb.un.ulpWord[4] =
2689 IOERR_SLI_ABORTED;
2690 spin_lock_irqsave(
2691 &phba->hbalock, iflag);
2692 saveq->iocb_flag |=
2693 LPFC_DELAY_MEM_FREE;
2694 spin_unlock_irqrestore(
2695 &phba->hbalock, iflag);
2696 }
James Smart07951072007-04-25 09:51:38 -04002697 }
dea31012005-04-17 16:05:31 -05002698 }
James Smart2e0fef82007-06-17 19:56:36 -05002699 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002700 } else
2701 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002702 } else {
2703 /*
2704 * Unknown initiating command based on the response iotag.
2705 * This could be the case on the ELS ring because of
2706 * lpfc_els_abort().
2707 */
2708 if (pring->ringno != LPFC_ELS_RING) {
2709 /*
2710 * Ring <ringno> handler: unexpected completion IoTag
2711 * <IoTag>
2712 */
James Smarta257bf92009-04-06 18:48:10 -04002713 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002714 "0322 Ring %d handler: "
2715 "unexpected completion IoTag x%x "
2716 "Data: x%x x%x x%x x%x\n",
2717 pring->ringno,
2718 saveq->iocb.ulpIoTag,
2719 saveq->iocb.ulpStatus,
2720 saveq->iocb.un.ulpWord[4],
2721 saveq->iocb.ulpCommand,
2722 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002723 }
2724 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002725
dea31012005-04-17 16:05:31 -05002726 return rc;
2727}
2728
James Smarte59058c2008-08-24 21:49:00 -04002729/**
James Smart3621a712009-04-06 18:47:14 -04002730 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002731 * @phba: Pointer to HBA context object.
2732 * @pring: Pointer to driver SLI ring object.
2733 *
2734 * This function is called from the iocb ring event handlers when
2735 * put pointer is ahead of the get pointer for a ring. This function signal
2736 * an error attention condition to the worker thread and the worker
2737 * thread will transition the HBA to offline state.
2738 **/
James Smart2e0fef82007-06-17 19:56:36 -05002739static void
2740lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002741{
James Smart34b02dc2008-08-24 21:49:55 -04002742 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002743 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002744 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002745 * rsp ring <portRspMax>
2746 */
2747 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002748 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002749 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002750 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002751 pring->numRiocb);
2752
James Smart2e0fef82007-06-17 19:56:36 -05002753 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002754
2755 /*
2756 * All error attention handlers are posted to
2757 * worker thread
2758 */
2759 phba->work_ha |= HA_ERATT;
2760 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002761
James Smart5e9d9b82008-06-14 22:52:53 -04002762 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002763
2764 return;
2765}
2766
James Smarte59058c2008-08-24 21:49:00 -04002767/**
James Smart3621a712009-04-06 18:47:14 -04002768 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002769 * @ptr: Pointer to address of HBA context object.
2770 *
2771 * This function is invoked by the Error Attention polling timer when the
2772 * timer times out. It will check the SLI Error Attention register for
2773 * possible attention events. If so, it will post an Error Attention event
2774 * and wake up worker thread to process it. Otherwise, it will set up the
2775 * Error Attention polling timer for the next poll.
2776 **/
2777void lpfc_poll_eratt(unsigned long ptr)
2778{
2779 struct lpfc_hba *phba;
2780 uint32_t eratt = 0;
2781
2782 phba = (struct lpfc_hba *)ptr;
2783
2784 /* Check chip HA register for error event */
2785 eratt = lpfc_sli_check_eratt(phba);
2786
2787 if (eratt)
2788 /* Tell the worker thread there is work to do */
2789 lpfc_worker_wake_up(phba);
2790 else
2791 /* Restart the timer for next eratt poll */
2792 mod_timer(&phba->eratt_poll, jiffies +
2793 HZ * LPFC_ERATT_POLL_INTERVAL);
2794 return;
2795}
2796
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002800 * @phba: Pointer to HBA context object.
2801 * @pring: Pointer to driver SLI ring object.
2802 * @mask: Host attention register mask for this ring.
2803 *
2804 * This function is called from the interrupt context when there is a ring
2805 * event for the fcp ring. The caller does not hold any lock.
2806 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002807 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002808 * LE bit set. The function will call the completion handler of the command iocb
2809 * if the response iocb indicates a completion for a command iocb or it is
2810 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2811 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002812 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002813 * to check it explicitly.
2814 */
2815int
James Smart2e0fef82007-06-17 19:56:36 -05002816lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2817 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002818{
James Smart34b02dc2008-08-24 21:49:55 -04002819 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002820 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002821 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002822 struct lpfc_iocbq *cmdiocbq = NULL;
2823 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002824 uint32_t status;
2825 uint32_t portRspPut, portRspMax;
2826 int rc = 1;
2827 lpfc_iocb_type type;
2828 unsigned long iflag;
2829 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002830
James Smart2e0fef82007-06-17 19:56:36 -05002831 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002832 pring->stats.iocb_event++;
2833
dea31012005-04-17 16:05:31 -05002834 /*
2835 * The next available response entry should never exceed the maximum
2836 * entries. If it does, treat it as an adapter hardware error.
2837 */
2838 portRspMax = pring->numRiocb;
2839 portRspPut = le32_to_cpu(pgp->rspPutInx);
2840 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002841 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002842 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002843 return 1;
2844 }
James Smart45ed1192009-10-02 15:17:02 -04002845 if (phba->fcp_ring_in_use) {
2846 spin_unlock_irqrestore(&phba->hbalock, iflag);
2847 return 1;
2848 } else
2849 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002850
2851 rmb();
2852 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002853 /*
2854 * Fetch an entry off the ring and copy it into a local data
2855 * structure. The copy involves a byte-swap since the
2856 * network byte order and pci byte orders are different.
2857 */
James Smarted957682007-06-17 19:56:37 -05002858 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002859 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002860
2861 if (++pring->rspidx >= portRspMax)
2862 pring->rspidx = 0;
2863
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002864 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2865 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002866 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002867 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002868 irsp = &rspiocbq.iocb;
2869
dea31012005-04-17 16:05:31 -05002870 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2871 pring->stats.iocb_rsp++;
2872 rsp_cmpl++;
2873
2874 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002875 /*
2876 * If resource errors reported from HBA, reduce
2877 * queuedepths of the SCSI device.
2878 */
2879 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2880 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2881 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002882 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002883 spin_lock_irqsave(&phba->hbalock, iflag);
2884 }
2885
dea31012005-04-17 16:05:31 -05002886 /* Rsp ring <ringno> error: IOCB */
2887 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002888 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002889 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002890 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002891 irsp->un.ulpWord[0],
2892 irsp->un.ulpWord[1],
2893 irsp->un.ulpWord[2],
2894 irsp->un.ulpWord[3],
2895 irsp->un.ulpWord[4],
2896 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002897 *(uint32_t *)&irsp->un1,
2898 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002899 }
2900
2901 switch (type) {
2902 case LPFC_ABORT_IOCB:
2903 case LPFC_SOL_IOCB:
2904 /*
2905 * Idle exchange closed via ABTS from port. No iocb
2906 * resources need to be recovered.
2907 */
2908 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002909 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002910 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002911 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002912 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002913 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002914 break;
2915 }
2916
James Bottomley604a3e32005-10-29 10:28:33 -05002917 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2918 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002919 if (unlikely(!cmdiocbq))
2920 break;
2921 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2922 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2923 if (cmdiocbq->iocb_cmpl) {
2924 spin_unlock_irqrestore(&phba->hbalock, iflag);
2925 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2926 &rspiocbq);
2927 spin_lock_irqsave(&phba->hbalock, iflag);
2928 }
dea31012005-04-17 16:05:31 -05002929 break;
James Smarta4bc3372006-12-02 13:34:16 -05002930 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002931 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002932 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002933 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002934 break;
dea31012005-04-17 16:05:31 -05002935 default:
2936 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2937 char adaptermsg[LPFC_MAX_ADPTMSG];
2938 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2939 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2940 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002941 dev_warn(&((phba->pcidev)->dev),
2942 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002943 phba->brd_no, adaptermsg);
2944 } else {
2945 /* Unknown IOCB command */
2946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002947 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002948 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002949 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002950 irsp->ulpStatus,
2951 irsp->ulpIoTag,
2952 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002953 }
2954 break;
2955 }
2956
2957 /*
2958 * The response IOCB has been processed. Update the ring
2959 * pointer in SLIM. If the port response put pointer has not
2960 * been updated, sync the pgp->rspPutInx and fetch the new port
2961 * response put pointer.
2962 */
James Smarted957682007-06-17 19:56:37 -05002963 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002964
2965 if (pring->rspidx == portRspPut)
2966 portRspPut = le32_to_cpu(pgp->rspPutInx);
2967 }
2968
2969 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2970 pring->stats.iocb_rsp_full++;
2971 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2972 writel(status, phba->CAregaddr);
2973 readl(phba->CAregaddr);
2974 }
2975 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2976 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2977 pring->stats.iocb_cmd_empty++;
2978
2979 /* Force update of the local copy of cmdGetInx */
2980 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2981 lpfc_sli_resume_iocb(phba, pring);
2982
2983 if ((pring->lpfc_sli_cmd_available))
2984 (pring->lpfc_sli_cmd_available) (phba, pring);
2985
2986 }
2987
James Smart45ed1192009-10-02 15:17:02 -04002988 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002989 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002990 return rc;
2991}
2992
James Smarte59058c2008-08-24 21:49:00 -04002993/**
James Smart3772a992009-05-22 14:50:54 -04002994 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2995 * @phba: Pointer to HBA context object.
2996 * @pring: Pointer to driver SLI ring object.
2997 * @rspiocbp: Pointer to driver response IOCB object.
2998 *
2999 * This function is called from the worker thread when there is a slow-path
3000 * response IOCB to process. This function chains all the response iocbs until
3001 * seeing the iocb with the LE bit set. The function will call
3002 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3003 * completion of a command iocb. The function will call the
3004 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3005 * The function frees the resources or calls the completion handler if this
3006 * iocb is an abort completion. The function returns NULL when the response
3007 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3008 * this function shall chain the iocb on to the iocb_continueq and return the
3009 * response iocb passed in.
3010 **/
3011static struct lpfc_iocbq *
3012lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3013 struct lpfc_iocbq *rspiocbp)
3014{
3015 struct lpfc_iocbq *saveq;
3016 struct lpfc_iocbq *cmdiocbp;
3017 struct lpfc_iocbq *next_iocb;
3018 IOCB_t *irsp = NULL;
3019 uint32_t free_saveq;
3020 uint8_t iocb_cmd_type;
3021 lpfc_iocb_type type;
3022 unsigned long iflag;
3023 int rc;
3024
3025 spin_lock_irqsave(&phba->hbalock, iflag);
3026 /* First add the response iocb to the countinueq list */
3027 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3028 pring->iocb_continueq_cnt++;
3029
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003030 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003031 irsp = &rspiocbp->iocb;
3032 if (irsp->ulpLe) {
3033 /*
3034 * By default, the driver expects to free all resources
3035 * associated with this iocb completion.
3036 */
3037 free_saveq = 1;
3038 saveq = list_get_first(&pring->iocb_continueq,
3039 struct lpfc_iocbq, list);
3040 irsp = &(saveq->iocb);
3041 list_del_init(&pring->iocb_continueq);
3042 pring->iocb_continueq_cnt = 0;
3043
3044 pring->stats.iocb_rsp++;
3045
3046 /*
3047 * If resource errors reported from HBA, reduce
3048 * queuedepths of the SCSI device.
3049 */
3050 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3051 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3052 spin_unlock_irqrestore(&phba->hbalock, iflag);
3053 phba->lpfc_rampdown_queue_depth(phba);
3054 spin_lock_irqsave(&phba->hbalock, iflag);
3055 }
3056
3057 if (irsp->ulpStatus) {
3058 /* Rsp ring <ringno> error: IOCB */
3059 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3060 "0328 Rsp Ring %d error: "
3061 "IOCB Data: "
3062 "x%x x%x x%x x%x "
3063 "x%x x%x x%x x%x "
3064 "x%x x%x x%x x%x "
3065 "x%x x%x x%x x%x\n",
3066 pring->ringno,
3067 irsp->un.ulpWord[0],
3068 irsp->un.ulpWord[1],
3069 irsp->un.ulpWord[2],
3070 irsp->un.ulpWord[3],
3071 irsp->un.ulpWord[4],
3072 irsp->un.ulpWord[5],
3073 *(((uint32_t *) irsp) + 6),
3074 *(((uint32_t *) irsp) + 7),
3075 *(((uint32_t *) irsp) + 8),
3076 *(((uint32_t *) irsp) + 9),
3077 *(((uint32_t *) irsp) + 10),
3078 *(((uint32_t *) irsp) + 11),
3079 *(((uint32_t *) irsp) + 12),
3080 *(((uint32_t *) irsp) + 13),
3081 *(((uint32_t *) irsp) + 14),
3082 *(((uint32_t *) irsp) + 15));
3083 }
3084
3085 /*
3086 * Fetch the IOCB command type and call the correct completion
3087 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3088 * get freed back to the lpfc_iocb_list by the discovery
3089 * kernel thread.
3090 */
3091 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3092 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3093 switch (type) {
3094 case LPFC_SOL_IOCB:
3095 spin_unlock_irqrestore(&phba->hbalock, iflag);
3096 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3097 spin_lock_irqsave(&phba->hbalock, iflag);
3098 break;
3099
3100 case LPFC_UNSOL_IOCB:
3101 spin_unlock_irqrestore(&phba->hbalock, iflag);
3102 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3103 spin_lock_irqsave(&phba->hbalock, iflag);
3104 if (!rc)
3105 free_saveq = 0;
3106 break;
3107
3108 case LPFC_ABORT_IOCB:
3109 cmdiocbp = NULL;
3110 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3111 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3112 saveq);
3113 if (cmdiocbp) {
3114 /* Call the specified completion routine */
3115 if (cmdiocbp->iocb_cmpl) {
3116 spin_unlock_irqrestore(&phba->hbalock,
3117 iflag);
3118 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3119 saveq);
3120 spin_lock_irqsave(&phba->hbalock,
3121 iflag);
3122 } else
3123 __lpfc_sli_release_iocbq(phba,
3124 cmdiocbp);
3125 }
3126 break;
3127
3128 case LPFC_UNKNOWN_IOCB:
3129 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3130 char adaptermsg[LPFC_MAX_ADPTMSG];
3131 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3132 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3133 MAX_MSG_DATA);
3134 dev_warn(&((phba->pcidev)->dev),
3135 "lpfc%d: %s\n",
3136 phba->brd_no, adaptermsg);
3137 } else {
3138 /* Unknown IOCB command */
3139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3140 "0335 Unknown IOCB "
3141 "command Data: x%x "
3142 "x%x x%x x%x\n",
3143 irsp->ulpCommand,
3144 irsp->ulpStatus,
3145 irsp->ulpIoTag,
3146 irsp->ulpContext);
3147 }
3148 break;
3149 }
3150
3151 if (free_saveq) {
3152 list_for_each_entry_safe(rspiocbp, next_iocb,
3153 &saveq->list, list) {
3154 list_del(&rspiocbp->list);
3155 __lpfc_sli_release_iocbq(phba, rspiocbp);
3156 }
3157 __lpfc_sli_release_iocbq(phba, saveq);
3158 }
3159 rspiocbp = NULL;
3160 }
3161 spin_unlock_irqrestore(&phba->hbalock, iflag);
3162 return rspiocbp;
3163}
3164
3165/**
3166 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003167 * @phba: Pointer to HBA context object.
3168 * @pring: Pointer to driver SLI ring object.
3169 * @mask: Host attention register mask for this ring.
3170 *
James Smart3772a992009-05-22 14:50:54 -04003171 * This routine wraps the actual slow_ring event process routine from the
3172 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003173 **/
James Smart3772a992009-05-22 14:50:54 -04003174void
James Smart2e0fef82007-06-17 19:56:36 -05003175lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3176 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003177{
James Smart3772a992009-05-22 14:50:54 -04003178 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3179}
3180
3181/**
3182 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3183 * @phba: Pointer to HBA context object.
3184 * @pring: Pointer to driver SLI ring object.
3185 * @mask: Host attention register mask for this ring.
3186 *
3187 * This function is called from the worker thread when there is a ring event
3188 * for non-fcp rings. The caller does not hold any lock. The function will
3189 * remove each response iocb in the response ring and calls the handle
3190 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3191 **/
3192static void
3193lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3194 struct lpfc_sli_ring *pring, uint32_t mask)
3195{
James Smart34b02dc2008-08-24 21:49:55 -04003196 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003197 IOCB_t *entry;
3198 IOCB_t *irsp = NULL;
3199 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003200 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003201 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003202 uint32_t status;
dea31012005-04-17 16:05:31 -05003203
James Smart34b02dc2008-08-24 21:49:55 -04003204 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003205 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003206 pring->stats.iocb_event++;
3207
dea31012005-04-17 16:05:31 -05003208 /*
3209 * The next available response entry should never exceed the maximum
3210 * entries. If it does, treat it as an adapter hardware error.
3211 */
3212 portRspMax = pring->numRiocb;
3213 portRspPut = le32_to_cpu(pgp->rspPutInx);
3214 if (portRspPut >= portRspMax) {
3215 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003216 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003217 * rsp ring <portRspMax>
3218 */
James Smarted957682007-06-17 19:56:37 -05003219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003220 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003221 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003222 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003223
James Smart2e0fef82007-06-17 19:56:36 -05003224 phba->link_state = LPFC_HBA_ERROR;
3225 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003226
3227 phba->work_hs = HS_FFER3;
3228 lpfc_handle_eratt(phba);
3229
James Smart3772a992009-05-22 14:50:54 -04003230 return;
dea31012005-04-17 16:05:31 -05003231 }
3232
3233 rmb();
dea31012005-04-17 16:05:31 -05003234 while (pring->rspidx != portRspPut) {
3235 /*
3236 * Build a completion list and call the appropriate handler.
3237 * The process is to get the next available response iocb, get
3238 * a free iocb from the list, copy the response data into the
3239 * free iocb, insert to the continuation list, and update the
3240 * next response index to slim. This process makes response
3241 * iocb's in the ring available to DMA as fast as possible but
3242 * pays a penalty for a copy operation. Since the iocb is
3243 * only 32 bytes, this penalty is considered small relative to
3244 * the PCI reads for register values and a slim write. When
3245 * the ulpLe field is set, the entire Command has been
3246 * received.
3247 */
James Smarted957682007-06-17 19:56:37 -05003248 entry = lpfc_resp_iocb(phba, pring);
3249
James Smart858c9f62007-06-17 19:56:39 -05003250 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003251 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003252 if (rspiocbp == NULL) {
3253 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003254 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003255 break;
3256 }
3257
James Smarted957682007-06-17 19:56:37 -05003258 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3259 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003260 irsp = &rspiocbp->iocb;
3261
3262 if (++pring->rspidx >= portRspMax)
3263 pring->rspidx = 0;
3264
James Smarta58cbd52007-08-02 11:09:43 -04003265 if (pring->ringno == LPFC_ELS_RING) {
3266 lpfc_debugfs_slow_ring_trc(phba,
3267 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3268 *(((uint32_t *) irsp) + 4),
3269 *(((uint32_t *) irsp) + 6),
3270 *(((uint32_t *) irsp) + 7));
3271 }
3272
James Smarted957682007-06-17 19:56:37 -05003273 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003274
James Smart3772a992009-05-22 14:50:54 -04003275 spin_unlock_irqrestore(&phba->hbalock, iflag);
3276 /* Handle the response IOCB */
3277 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3278 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003279
3280 /*
3281 * If the port response put pointer has not been updated, sync
3282 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3283 * response put pointer.
3284 */
3285 if (pring->rspidx == portRspPut) {
3286 portRspPut = le32_to_cpu(pgp->rspPutInx);
3287 }
3288 } /* while (pring->rspidx != portRspPut) */
3289
James Smart92d7f7b2007-06-17 19:56:38 -05003290 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003291 /* At least one response entry has been freed */
3292 pring->stats.iocb_rsp_full++;
3293 /* SET RxRE_RSP in Chip Att register */
3294 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3295 writel(status, phba->CAregaddr);
3296 readl(phba->CAregaddr); /* flush */
3297 }
3298 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3299 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3300 pring->stats.iocb_cmd_empty++;
3301
3302 /* Force update of the local copy of cmdGetInx */
3303 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3304 lpfc_sli_resume_iocb(phba, pring);
3305
3306 if ((pring->lpfc_sli_cmd_available))
3307 (pring->lpfc_sli_cmd_available) (phba, pring);
3308
3309 }
3310
James Smart2e0fef82007-06-17 19:56:36 -05003311 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003312 return;
dea31012005-04-17 16:05:31 -05003313}
3314
James Smarte59058c2008-08-24 21:49:00 -04003315/**
James Smart4f774512009-05-22 14:52:35 -04003316 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3317 * @phba: Pointer to HBA context object.
3318 * @pring: Pointer to driver SLI ring object.
3319 * @mask: Host attention register mask for this ring.
3320 *
3321 * This function is called from the worker thread when there is a pending
3322 * ELS response iocb on the driver internal slow-path response iocb worker
3323 * queue. The caller does not hold any lock. The function will remove each
3324 * response iocb from the response worker queue and calls the handle
3325 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3326 **/
3327static void
3328lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3329 struct lpfc_sli_ring *pring, uint32_t mask)
3330{
3331 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003332 struct hbq_dmabuf *dmabuf;
3333 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003334 unsigned long iflag;
3335
James Smart45ed1192009-10-02 15:17:02 -04003336 spin_lock_irqsave(&phba->hbalock, iflag);
3337 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3338 spin_unlock_irqrestore(&phba->hbalock, iflag);
3339 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003340 /* Get the response iocb from the head of work queue */
3341 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003342 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003343 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003344 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003345
3346 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3347 case CQE_CODE_COMPL_WQE:
3348 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3349 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003350 /* Translate ELS WCQE to response IOCBQ */
3351 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3352 irspiocbq);
3353 if (irspiocbq)
3354 lpfc_sli_sp_handle_rspiocb(phba, pring,
3355 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003356 break;
3357 case CQE_CODE_RECEIVE:
3358 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3359 cq_event);
3360 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3361 break;
3362 default:
3363 break;
3364 }
James Smart4f774512009-05-22 14:52:35 -04003365 }
3366}
3367
3368/**
James Smart3621a712009-04-06 18:47:14 -04003369 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003370 * @phba: Pointer to HBA context object.
3371 * @pring: Pointer to driver SLI ring object.
3372 *
3373 * This function aborts all iocbs in the given ring and frees all the iocb
3374 * objects in txq. This function issues an abort iocb for all the iocb commands
3375 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3376 * the return of this function. The caller is not required to hold any locks.
3377 **/
James Smart2e0fef82007-06-17 19:56:36 -05003378void
dea31012005-04-17 16:05:31 -05003379lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3380{
James Smart2534ba72007-04-25 09:52:20 -04003381 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003382 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003383
James Smart92d7f7b2007-06-17 19:56:38 -05003384 if (pring->ringno == LPFC_ELS_RING) {
3385 lpfc_fabric_abort_hba(phba);
3386 }
3387
dea31012005-04-17 16:05:31 -05003388 /* Error everything on txq and txcmplq
3389 * First do the txq.
3390 */
James Smart2e0fef82007-06-17 19:56:36 -05003391 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003392 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003393 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003394
3395 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003396 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3397 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3398
James Smart2e0fef82007-06-17 19:56:36 -05003399 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003400
James Smarta257bf92009-04-06 18:48:10 -04003401 /* Cancel all the IOCBs from the completions list */
3402 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3403 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003404}
3405
James Smarte59058c2008-08-24 21:49:00 -04003406/**
James Smart3621a712009-04-06 18:47:14 -04003407 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003408 * @phba: Pointer to HBA context object.
3409 *
3410 * This function flushes all iocbs in the fcp ring and frees all the iocb
3411 * objects in txq and txcmplq. This function will not issue abort iocbs
3412 * for all the iocb commands in txcmplq, they will just be returned with
3413 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3414 * slot has been permanently disabled.
3415 **/
3416void
3417lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3418{
3419 LIST_HEAD(txq);
3420 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003421 struct lpfc_sli *psli = &phba->sli;
3422 struct lpfc_sli_ring *pring;
3423
3424 /* Currently, only one fcp ring */
3425 pring = &psli->ring[psli->fcp_ring];
3426
3427 spin_lock_irq(&phba->hbalock);
3428 /* Retrieve everything on txq */
3429 list_splice_init(&pring->txq, &txq);
3430 pring->txq_cnt = 0;
3431
3432 /* Retrieve everything on the txcmplq */
3433 list_splice_init(&pring->txcmplq, &txcmplq);
3434 pring->txcmplq_cnt = 0;
3435 spin_unlock_irq(&phba->hbalock);
3436
3437 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003438 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3439 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003440
3441 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003442 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3443 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003444}
3445
3446/**
James Smart3772a992009-05-22 14:50:54 -04003447 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003448 * @phba: Pointer to HBA context object.
3449 * @mask: Bit mask to be checked.
3450 *
3451 * This function reads the host status register and compares
3452 * with the provided bit mask to check if HBA completed
3453 * the restart. This function will wait in a loop for the
3454 * HBA to complete restart. If the HBA does not restart within
3455 * 15 iterations, the function will reset the HBA again. The
3456 * function returns 1 when HBA fail to restart otherwise returns
3457 * zero.
3458 **/
James Smart3772a992009-05-22 14:50:54 -04003459static int
3460lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003461{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003462 uint32_t status;
3463 int i = 0;
3464 int retval = 0;
dea31012005-04-17 16:05:31 -05003465
Jamie Wellnitz41415862006-02-28 19:25:27 -05003466 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003467 if (lpfc_readl(phba->HSregaddr, &status))
3468 return 1;
dea31012005-04-17 16:05:31 -05003469
Jamie Wellnitz41415862006-02-28 19:25:27 -05003470 /*
3471 * Check status register every 100ms for 5 retries, then every
3472 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3473 * every 2.5 sec for 4.
3474 * Break our of the loop if errors occurred during init.
3475 */
3476 while (((status & mask) != mask) &&
3477 !(status & HS_FFERM) &&
3478 i++ < 20) {
dea31012005-04-17 16:05:31 -05003479
Jamie Wellnitz41415862006-02-28 19:25:27 -05003480 if (i <= 5)
3481 msleep(10);
3482 else if (i <= 10)
3483 msleep(500);
3484 else
3485 msleep(2500);
dea31012005-04-17 16:05:31 -05003486
Jamie Wellnitz41415862006-02-28 19:25:27 -05003487 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003488 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003489 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003490 lpfc_sli_brdrestart(phba);
3491 }
3492 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003493 if (lpfc_readl(phba->HSregaddr, &status)) {
3494 retval = 1;
3495 break;
3496 }
dea31012005-04-17 16:05:31 -05003497 }
dea31012005-04-17 16:05:31 -05003498
Jamie Wellnitz41415862006-02-28 19:25:27 -05003499 /* Check to see if any errors occurred during init */
3500 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3502 "2751 Adapter failed to restart, "
3503 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3504 status,
3505 readl(phba->MBslimaddr + 0xa8),
3506 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003507 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003508 retval = 1;
3509 }
dea31012005-04-17 16:05:31 -05003510
Jamie Wellnitz41415862006-02-28 19:25:27 -05003511 return retval;
dea31012005-04-17 16:05:31 -05003512}
3513
James Smartda0436e2009-05-22 14:51:39 -04003514/**
3515 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3516 * @phba: Pointer to HBA context object.
3517 * @mask: Bit mask to be checked.
3518 *
3519 * This function checks the host status register to check if HBA is
3520 * ready. This function will wait in a loop for the HBA to be ready
3521 * If the HBA is not ready , the function will will reset the HBA PCI
3522 * function again. The function returns 1 when HBA fail to be ready
3523 * otherwise returns zero.
3524 **/
3525static int
3526lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3527{
3528 uint32_t status;
3529 int retval = 0;
3530
3531 /* Read the HBA Host Status Register */
3532 status = lpfc_sli4_post_status_check(phba);
3533
3534 if (status) {
3535 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3536 lpfc_sli_brdrestart(phba);
3537 status = lpfc_sli4_post_status_check(phba);
3538 }
3539
3540 /* Check to see if any errors occurred during init */
3541 if (status) {
3542 phba->link_state = LPFC_HBA_ERROR;
3543 retval = 1;
3544 } else
3545 phba->sli4_hba.intr_enable = 0;
3546
3547 return retval;
3548}
3549
3550/**
3551 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3552 * @phba: Pointer to HBA context object.
3553 * @mask: Bit mask to be checked.
3554 *
3555 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3556 * from the API jump table function pointer from the lpfc_hba struct.
3557 **/
3558int
3559lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3560{
3561 return phba->lpfc_sli_brdready(phba, mask);
3562}
3563
James Smart92908312006-03-07 15:04:13 -05003564#define BARRIER_TEST_PATTERN (0xdeadbeef)
3565
James Smarte59058c2008-08-24 21:49:00 -04003566/**
James Smart3621a712009-04-06 18:47:14 -04003567 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003568 * @phba: Pointer to HBA context object.
3569 *
3570 * This function is called before resetting an HBA. This
3571 * function requests HBA to quiesce DMAs before a reset.
3572 **/
James Smart2e0fef82007-06-17 19:56:36 -05003573void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003574{
James Smart65a29c12006-07-06 15:50:50 -04003575 uint32_t __iomem *resp_buf;
3576 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003577 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003578 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003579 int i;
3580 uint8_t hdrtype;
3581
3582 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3583 if (hdrtype != 0x80 ||
3584 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3585 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3586 return;
3587
3588 /*
3589 * Tell the other part of the chip to suspend temporarily all
3590 * its DMA activity.
3591 */
James Smart65a29c12006-07-06 15:50:50 -04003592 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003593
3594 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003595 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3596 return;
James Smart92908312006-03-07 15:04:13 -05003597 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3598 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003599 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003600
James Smart9940b972011-03-11 16:06:12 -05003601 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3602 return;
3603 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003604 /* Clear Chip error bit */
3605 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003606 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003607 }
3608
3609 mbox = 0;
3610 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3611 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3612
3613 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003614 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003615 writel(mbox, mbox_buf);
3616
James Smart9940b972011-03-11 16:06:12 -05003617 for (i = 0; i < 50; i++) {
3618 if (lpfc_readl((resp_buf + 1), &resp_data))
3619 return;
3620 if (resp_data != ~(BARRIER_TEST_PATTERN))
3621 mdelay(1);
3622 else
3623 break;
3624 }
3625 resp_data = 0;
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003629 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003630 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003631 goto restore_hc;
3632 else
3633 goto clear_errat;
3634 }
3635
3636 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003637 resp_data = 0;
3638 for (i = 0; i < 500; i++) {
3639 if (lpfc_readl(resp_buf, &resp_data))
3640 return;
3641 if (resp_data != mbox)
3642 mdelay(1);
3643 else
3644 break;
3645 }
James Smart92908312006-03-07 15:04:13 -05003646
3647clear_errat:
3648
James Smart9940b972011-03-11 16:06:12 -05003649 while (++i < 500) {
3650 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3651 return;
3652 if (!(ha_copy & HA_ERATT))
3653 mdelay(1);
3654 else
3655 break;
3656 }
James Smart92908312006-03-07 15:04:13 -05003657
3658 if (readl(phba->HAregaddr) & HA_ERATT) {
3659 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003660 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003661 }
3662
3663restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003664 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003665 writel(hc_copy, phba->HCregaddr);
3666 readl(phba->HCregaddr); /* flush */
3667}
3668
James Smarte59058c2008-08-24 21:49:00 -04003669/**
James Smart3621a712009-04-06 18:47:14 -04003670 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003671 * @phba: Pointer to HBA context object.
3672 *
3673 * This function issues a kill_board mailbox command and waits for
3674 * the error attention interrupt. This function is called for stopping
3675 * the firmware processing. The caller is not required to hold any
3676 * locks. This function calls lpfc_hba_down_post function to free
3677 * any pending commands after the kill. The function will return 1 when it
3678 * fails to kill the board else will return 0.
3679 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003680int
James Smart2e0fef82007-06-17 19:56:36 -05003681lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003682{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003683 struct lpfc_sli *psli;
3684 LPFC_MBOXQ_t *pmb;
3685 uint32_t status;
3686 uint32_t ha_copy;
3687 int retval;
3688 int i = 0;
3689
3690 psli = &phba->sli;
3691
3692 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003693 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003694 "0329 Kill HBA Data: x%x x%x\n",
3695 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003696
James Smart98c9ea52007-10-27 13:37:33 -04003697 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3698 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003699 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003700
3701 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003702 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003703 if (lpfc_readl(phba->HCregaddr, &status)) {
3704 spin_unlock_irq(&phba->hbalock);
3705 mempool_free(pmb, phba->mbox_mem_pool);
3706 return 1;
3707 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003708 status &= ~HC_ERINT_ENA;
3709 writel(status, phba->HCregaddr);
3710 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003711 phba->link_flag |= LS_IGNORE_ERATT;
3712 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713
3714 lpfc_kill_board(phba, pmb);
3715 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3716 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3717
3718 if (retval != MBX_SUCCESS) {
3719 if (retval != MBX_BUSY)
3720 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3722 "2752 KILL_BOARD command failed retval %d\n",
3723 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003724 spin_lock_irq(&phba->hbalock);
3725 phba->link_flag &= ~LS_IGNORE_ERATT;
3726 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003727 return 1;
3728 }
3729
James Smartf4b4c682009-05-22 14:53:12 -04003730 spin_lock_irq(&phba->hbalock);
3731 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3732 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003733
Jamie Wellnitz41415862006-02-28 19:25:27 -05003734 mempool_free(pmb, phba->mbox_mem_pool);
3735
3736 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3737 * attention every 100ms for 3 seconds. If we don't get ERATT after
3738 * 3 seconds we still set HBA_ERROR state because the status of the
3739 * board is now undefined.
3740 */
James Smart9940b972011-03-11 16:06:12 -05003741 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3742 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003743 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3744 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003745 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3746 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003747 }
3748
3749 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003750 if (ha_copy & HA_ERATT) {
3751 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003752 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003753 }
James Smart2e0fef82007-06-17 19:56:36 -05003754 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003755 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003756 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003757 phba->link_flag &= ~LS_IGNORE_ERATT;
3758 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759
Jamie Wellnitz41415862006-02-28 19:25:27 -05003760 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003761 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762
James Smart2e0fef82007-06-17 19:56:36 -05003763 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003764}
3765
James Smarte59058c2008-08-24 21:49:00 -04003766/**
James Smart3772a992009-05-22 14:50:54 -04003767 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003768 * @phba: Pointer to HBA context object.
3769 *
3770 * This function resets the HBA by writing HC_INITFF to the control
3771 * register. After the HBA resets, this function resets all the iocb ring
3772 * indices. This function disables PCI layer parity checking during
3773 * the reset.
3774 * This function returns 0 always.
3775 * The caller is not required to hold any locks.
3776 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003777int
James Smart2e0fef82007-06-17 19:56:36 -05003778lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779{
3780 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003781 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003782 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003783 int i;
dea31012005-04-17 16:05:31 -05003784
Jamie Wellnitz41415862006-02-28 19:25:27 -05003785 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003786
Jamie Wellnitz41415862006-02-28 19:25:27 -05003787 /* Reset HBA */
3788 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003789 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003790 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003791
3792 /* perform board reset */
3793 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003794 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003795 phba->pport->fc_myDID = 0;
3796 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003797
Jamie Wellnitz41415862006-02-28 19:25:27 -05003798 /* Turn off parity checking and serr during the physical reset */
3799 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3800 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3801 (cfg_value &
3802 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3803
James Smart3772a992009-05-22 14:50:54 -04003804 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3805
Jamie Wellnitz41415862006-02-28 19:25:27 -05003806 /* Now toggle INITFF bit in the Host Control Register */
3807 writel(HC_INITFF, phba->HCregaddr);
3808 mdelay(1);
3809 readl(phba->HCregaddr); /* flush */
3810 writel(0, phba->HCregaddr);
3811 readl(phba->HCregaddr); /* flush */
3812
3813 /* Restore PCI cmd register */
3814 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003815
3816 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003817 for (i = 0; i < psli->num_rings; i++) {
3818 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003819 pring->flag = 0;
3820 pring->rspidx = 0;
3821 pring->next_cmdidx = 0;
3822 pring->local_getidx = 0;
3823 pring->cmdidx = 0;
3824 pring->missbufcnt = 0;
3825 }
dea31012005-04-17 16:05:31 -05003826
James Smart2e0fef82007-06-17 19:56:36 -05003827 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003828 return 0;
3829}
3830
James Smarte59058c2008-08-24 21:49:00 -04003831/**
James Smartda0436e2009-05-22 14:51:39 -04003832 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3833 * @phba: Pointer to HBA context object.
3834 *
3835 * This function resets a SLI4 HBA. This function disables PCI layer parity
3836 * checking during resets the device. The caller is not required to hold
3837 * any locks.
3838 *
3839 * This function returns 0 always.
3840 **/
3841int
3842lpfc_sli4_brdreset(struct lpfc_hba *phba)
3843{
3844 struct lpfc_sli *psli = &phba->sli;
3845 uint16_t cfg_value;
3846 uint8_t qindx;
3847
3848 /* Reset HBA */
3849 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3850 "0295 Reset HBA Data: x%x x%x\n",
3851 phba->pport->port_state, psli->sli_flag);
3852
3853 /* perform board reset */
3854 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003855 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003856 phba->pport->fc_myDID = 0;
3857 phba->pport->fc_prevDID = 0;
3858
James Smartda0436e2009-05-22 14:51:39 -04003859 spin_lock_irq(&phba->hbalock);
3860 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3861 phba->fcf.fcf_flag = 0;
3862 /* Clean up the child queue list for the CQs */
3863 list_del_init(&phba->sli4_hba.mbx_wq->list);
3864 list_del_init(&phba->sli4_hba.els_wq->list);
3865 list_del_init(&phba->sli4_hba.hdr_rq->list);
3866 list_del_init(&phba->sli4_hba.dat_rq->list);
3867 list_del_init(&phba->sli4_hba.mbx_cq->list);
3868 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003869 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3870 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003871 qindx = 0;
3872 do
James Smartda0436e2009-05-22 14:51:39 -04003873 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003874 while (++qindx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04003875 spin_unlock_irq(&phba->hbalock);
3876
3877 /* Now physically reset the device */
3878 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3879 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003880
3881 /* Turn off parity checking and serr during the physical reset */
3882 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3883 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3884 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3885
James Smartda0436e2009-05-22 14:51:39 -04003886 /* Perform FCoE PCI function reset */
3887 lpfc_pci_function_reset(phba);
3888
James Smartbe858b62010-12-15 17:57:20 -05003889 /* Restore PCI cmd register */
3890 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3891
James Smartda0436e2009-05-22 14:51:39 -04003892 return 0;
3893}
3894
3895/**
3896 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003897 * @phba: Pointer to HBA context object.
3898 *
3899 * This function is called in the SLI initialization code path to
3900 * restart the HBA. The caller is not required to hold any lock.
3901 * This function writes MBX_RESTART mailbox command to the SLIM and
3902 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3903 * function to free any pending commands. The function enables
3904 * POST only during the first initialization. The function returns zero.
3905 * The function does not guarantee completion of MBX_RESTART mailbox
3906 * command before the return of this function.
3907 **/
James Smartda0436e2009-05-22 14:51:39 -04003908static int
3909lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910{
3911 MAILBOX_t *mb;
3912 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003913 volatile uint32_t word0;
3914 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003915 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003916
James Smart2e0fef82007-06-17 19:56:36 -05003917 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003918
James Smart0d878412009-10-02 15:16:56 -04003919 /* Take PCIe device Advanced Error Reporting (AER) state */
3920 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3921
Jamie Wellnitz41415862006-02-28 19:25:27 -05003922 psli = &phba->sli;
3923
3924 /* Restart HBA */
3925 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003926 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003927 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003928
3929 word0 = 0;
3930 mb = (MAILBOX_t *) &word0;
3931 mb->mbxCommand = MBX_RESTART;
3932 mb->mbxHc = 1;
3933
James Smart92908312006-03-07 15:04:13 -05003934 lpfc_reset_barrier(phba);
3935
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 to_slim = phba->MBslimaddr;
3937 writel(*(uint32_t *) mb, to_slim);
3938 readl(to_slim); /* flush */
3939
3940 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003941 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003942 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003943 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003944 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003945 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946 writel(*(uint32_t *) mb, to_slim);
3947 readl(to_slim); /* flush */
3948
3949 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003950 phba->pport->stopped = 0;
3951 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003952 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003953 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003954
James Smart64ba8812006-08-02 15:24:34 -04003955 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3956 psli->stats_start = get_seconds();
3957
James Smarteaf15d52008-12-04 22:39:29 -05003958 /* Give the INITFF and Post time to settle. */
3959 mdelay(100);
dea31012005-04-17 16:05:31 -05003960
James Smart0d878412009-10-02 15:16:56 -04003961 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3962 if (hba_aer_enabled)
3963 pci_disable_pcie_error_reporting(phba->pcidev);
3964
Jamie Wellnitz41415862006-02-28 19:25:27 -05003965 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003966
3967 return 0;
3968}
3969
James Smarte59058c2008-08-24 21:49:00 -04003970/**
James Smartda0436e2009-05-22 14:51:39 -04003971 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3972 * @phba: Pointer to HBA context object.
3973 *
3974 * This function is called in the SLI initialization code path to restart
3975 * a SLI4 HBA. The caller is not required to hold any lock.
3976 * At the end of the function, it calls lpfc_hba_down_post function to
3977 * free any pending commands.
3978 **/
3979static int
3980lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3981{
3982 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003983 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003984
3985 /* Restart HBA */
3986 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3987 "0296 Restart HBA Data: x%x x%x\n",
3988 phba->pport->port_state, psli->sli_flag);
3989
James Smart75baf692010-06-08 18:31:21 -04003990 /* Take PCIe device Advanced Error Reporting (AER) state */
3991 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3992
James Smartda0436e2009-05-22 14:51:39 -04003993 lpfc_sli4_brdreset(phba);
3994
3995 spin_lock_irq(&phba->hbalock);
3996 phba->pport->stopped = 0;
3997 phba->link_state = LPFC_INIT_START;
3998 phba->hba_flag = 0;
3999 spin_unlock_irq(&phba->hbalock);
4000
4001 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4002 psli->stats_start = get_seconds();
4003
James Smart75baf692010-06-08 18:31:21 -04004004 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4005 if (hba_aer_enabled)
4006 pci_disable_pcie_error_reporting(phba->pcidev);
4007
James Smartda0436e2009-05-22 14:51:39 -04004008 lpfc_hba_down_post(phba);
4009
4010 return 0;
4011}
4012
4013/**
4014 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4015 * @phba: Pointer to HBA context object.
4016 *
4017 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4018 * API jump table function pointer from the lpfc_hba struct.
4019**/
4020int
4021lpfc_sli_brdrestart(struct lpfc_hba *phba)
4022{
4023 return phba->lpfc_sli_brdrestart(phba);
4024}
4025
4026/**
James Smart3621a712009-04-06 18:47:14 -04004027 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004028 * @phba: Pointer to HBA context object.
4029 *
4030 * This function is called after a HBA restart to wait for successful
4031 * restart of the HBA. Successful restart of the HBA is indicated by
4032 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4033 * iteration, the function will restart the HBA again. The function returns
4034 * zero if HBA successfully restarted else returns negative error code.
4035 **/
dea31012005-04-17 16:05:31 -05004036static int
4037lpfc_sli_chipset_init(struct lpfc_hba *phba)
4038{
4039 uint32_t status, i = 0;
4040
4041 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004042 if (lpfc_readl(phba->HSregaddr, &status))
4043 return -EIO;
dea31012005-04-17 16:05:31 -05004044
4045 /* Check status register to see what current state is */
4046 i = 0;
4047 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4048
James Smartdcf2a4e2010-09-29 11:18:53 -04004049 /* Check every 10ms for 10 retries, then every 100ms for 90
4050 * retries, then every 1 sec for 50 retires for a total of
4051 * ~60 seconds before reset the board again and check every
4052 * 1 sec for 50 retries. The up to 60 seconds before the
4053 * board ready is required by the Falcon FIPS zeroization
4054 * complete, and any reset the board in between shall cause
4055 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004056 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004057 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004058 /* Adapter failed to init, timeout, status reg
4059 <status> */
James Smarted957682007-06-17 19:56:37 -05004060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004061 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004062 "timeout, status reg x%x, "
4063 "FW Data: A8 x%x AC x%x\n", status,
4064 readl(phba->MBslimaddr + 0xa8),
4065 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004066 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004067 return -ETIMEDOUT;
4068 }
4069
4070 /* Check to see if any errors occurred during init */
4071 if (status & HS_FFERM) {
4072 /* ERROR: During chipset initialization */
4073 /* Adapter failed to init, chipset, status reg
4074 <status> */
James Smarted957682007-06-17 19:56:37 -05004075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004076 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004077 "chipset, status reg x%x, "
4078 "FW Data: A8 x%x AC x%x\n", status,
4079 readl(phba->MBslimaddr + 0xa8),
4080 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004081 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004082 return -EIO;
4083 }
4084
James Smartdcf2a4e2010-09-29 11:18:53 -04004085 if (i <= 10)
dea31012005-04-17 16:05:31 -05004086 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004087 else if (i <= 100)
4088 msleep(100);
4089 else
4090 msleep(1000);
dea31012005-04-17 16:05:31 -05004091
James Smartdcf2a4e2010-09-29 11:18:53 -04004092 if (i == 150) {
4093 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004094 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004095 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004096 }
4097 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004098 if (lpfc_readl(phba->HSregaddr, &status))
4099 return -EIO;
dea31012005-04-17 16:05:31 -05004100 }
4101
4102 /* Check to see if any errors occurred during init */
4103 if (status & HS_FFERM) {
4104 /* ERROR: During chipset initialization */
4105 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004107 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004108 "status reg x%x, "
4109 "FW Data: A8 x%x AC x%x\n", status,
4110 readl(phba->MBslimaddr + 0xa8),
4111 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004113 return -EIO;
4114 }
4115
4116 /* Clear all interrupt enable conditions */
4117 writel(0, phba->HCregaddr);
4118 readl(phba->HCregaddr); /* flush */
4119
4120 /* setup host attn register */
4121 writel(0xffffffff, phba->HAregaddr);
4122 readl(phba->HAregaddr); /* flush */
4123 return 0;
4124}
4125
James Smarte59058c2008-08-24 21:49:00 -04004126/**
James Smart3621a712009-04-06 18:47:14 -04004127 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004128 *
4129 * This function calculates and returns the number of HBQs required to be
4130 * configured.
4131 **/
James Smart78b2d852007-08-02 11:10:21 -04004132int
James Smarted957682007-06-17 19:56:37 -05004133lpfc_sli_hbq_count(void)
4134{
James Smart92d7f7b2007-06-17 19:56:38 -05004135 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004136}
4137
James Smarte59058c2008-08-24 21:49:00 -04004138/**
James Smart3621a712009-04-06 18:47:14 -04004139 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004140 *
4141 * This function adds the number of hbq entries in every HBQ to get
4142 * the total number of hbq entries required for the HBA and returns
4143 * the total count.
4144 **/
James Smarted957682007-06-17 19:56:37 -05004145static int
4146lpfc_sli_hbq_entry_count(void)
4147{
4148 int hbq_count = lpfc_sli_hbq_count();
4149 int count = 0;
4150 int i;
4151
4152 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004153 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004154 return count;
4155}
4156
James Smarte59058c2008-08-24 21:49:00 -04004157/**
James Smart3621a712009-04-06 18:47:14 -04004158 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004159 *
4160 * This function calculates amount of memory required for all hbq entries
4161 * to be configured and returns the total memory required.
4162 **/
dea31012005-04-17 16:05:31 -05004163int
James Smarted957682007-06-17 19:56:37 -05004164lpfc_sli_hbq_size(void)
4165{
4166 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4167}
4168
James Smarte59058c2008-08-24 21:49:00 -04004169/**
James Smart3621a712009-04-06 18:47:14 -04004170 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004171 * @phba: Pointer to HBA context object.
4172 *
4173 * This function is called during the SLI initialization to configure
4174 * all the HBQs and post buffers to the HBQ. The caller is not
4175 * required to hold any locks. This function will return zero if successful
4176 * else it will return negative error code.
4177 **/
James Smarted957682007-06-17 19:56:37 -05004178static int
4179lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4180{
4181 int hbq_count = lpfc_sli_hbq_count();
4182 LPFC_MBOXQ_t *pmb;
4183 MAILBOX_t *pmbox;
4184 uint32_t hbqno;
4185 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004186
James Smart92d7f7b2007-06-17 19:56:38 -05004187 /* Get a Mailbox buffer to setup mailbox
4188 * commands for HBA initialization
4189 */
James Smarted957682007-06-17 19:56:37 -05004190 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4191
4192 if (!pmb)
4193 return -ENOMEM;
4194
James Smart04c68492009-05-22 14:52:52 -04004195 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004196
4197 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4198 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004199 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004200
4201 hbq_entry_index = 0;
4202 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4203 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4204 phba->hbqs[hbqno].hbqPutIdx = 0;
4205 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4206 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004207 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004208 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4209 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004210 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4211
4212 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4213 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4214 mbxStatus <status>, ring <num> */
4215
4216 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004217 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004218 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004219 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004220 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004221 pmbox->mbxStatus, hbqno);
4222
4223 phba->link_state = LPFC_HBA_ERROR;
4224 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004225 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004226 }
4227 }
4228 phba->hbq_count = hbq_count;
4229
James Smarted957682007-06-17 19:56:37 -05004230 mempool_free(pmb, phba->mbox_mem_pool);
4231
James Smart92d7f7b2007-06-17 19:56:38 -05004232 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004233 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4234 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004235 return 0;
4236}
4237
James Smarte59058c2008-08-24 21:49:00 -04004238/**
James Smart4f774512009-05-22 14:52:35 -04004239 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4240 * @phba: Pointer to HBA context object.
4241 *
4242 * This function is called during the SLI initialization to configure
4243 * all the HBQs and post buffers to the HBQ. The caller is not
4244 * required to hold any locks. This function will return zero if successful
4245 * else it will return negative error code.
4246 **/
4247static int
4248lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4249{
4250 phba->hbq_in_use = 1;
4251 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4252 phba->hbq_count = 1;
4253 /* Initially populate or replenish the HBQs */
4254 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4255 return 0;
4256}
4257
4258/**
James Smart3621a712009-04-06 18:47:14 -04004259 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004260 * @phba: Pointer to HBA context object.
4261 * @sli_mode: sli mode - 2/3
4262 *
4263 * This function is called by the sli intialization code path
4264 * to issue config_port mailbox command. This function restarts the
4265 * HBA firmware and issues a config_port mailbox command to configure
4266 * the SLI interface in the sli mode specified by sli_mode
4267 * variable. The caller is not required to hold any locks.
4268 * The function returns 0 if successful, else returns negative error
4269 * code.
4270 **/
James Smart93996272008-08-24 21:50:30 -04004271int
4272lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004273{
4274 LPFC_MBOXQ_t *pmb;
4275 uint32_t resetcount = 0, rc = 0, done = 0;
4276
4277 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4278 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004279 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004280 return -ENOMEM;
4281 }
4282
James Smarted957682007-06-17 19:56:37 -05004283 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004284 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004285 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004286 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004287 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004288 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004289 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004290 rc = lpfc_sli_chipset_init(phba);
4291 if (rc)
4292 break;
4293
James Smart2e0fef82007-06-17 19:56:36 -05004294 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004295 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004296 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004297 resetcount++;
4298
James Smarted957682007-06-17 19:56:37 -05004299 /* Call pre CONFIG_PORT mailbox command initialization. A
4300 * value of 0 means the call was successful. Any other
4301 * nonzero value is a failure, but if ERESTART is returned,
4302 * the driver may reset the HBA and try again.
4303 */
dea31012005-04-17 16:05:31 -05004304 rc = lpfc_config_port_prep(phba);
4305 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004306 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004307 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004308 } else if (rc)
dea31012005-04-17 16:05:31 -05004309 break;
James Smart6d368e52011-05-24 11:44:12 -04004310
James Smart2e0fef82007-06-17 19:56:36 -05004311 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004312 lpfc_config_port(phba, pmb);
4313 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004314 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4315 LPFC_SLI3_HBQ_ENABLED |
4316 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004317 LPFC_SLI3_BG_ENABLED |
4318 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004319 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004321 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004322 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004323 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004324 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004325 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004326 spin_unlock_irq(&phba->hbalock);
4327 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004328 } else {
4329 /* Allow asynchronous mailbox command to go through */
4330 spin_lock_irq(&phba->hbalock);
4331 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4332 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004333 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004334 }
dea31012005-04-17 16:05:31 -05004335 }
James Smarted957682007-06-17 19:56:37 -05004336 if (!done) {
4337 rc = -EINVAL;
4338 goto do_prep_failed;
4339 }
James Smart04c68492009-05-22 14:52:52 -04004340 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4341 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004342 rc = -ENXIO;
4343 goto do_prep_failed;
4344 }
James Smart04c68492009-05-22 14:52:52 -04004345 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004346 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004347 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4348 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4349 phba->max_vpi : phba->max_vports;
4350
James Smart34b02dc2008-08-24 21:49:55 -04004351 } else
4352 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004353 phba->fips_level = 0;
4354 phba->fips_spec_rev = 0;
4355 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004356 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004357 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4358 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4359 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4360 "2850 Security Crypto Active. FIPS x%d "
4361 "(Spec Rev: x%d)",
4362 phba->fips_level, phba->fips_spec_rev);
4363 }
4364 if (pmb->u.mb.un.varCfgPort.sec_err) {
4365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4366 "2856 Config Port Security Crypto "
4367 "Error: x%x ",
4368 pmb->u.mb.un.varCfgPort.sec_err);
4369 }
James Smart04c68492009-05-22 14:52:52 -04004370 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004371 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004372 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004373 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004374
4375 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4376 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004377
4378 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004379 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004380 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4381 else
4382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4383 "0443 Adapter did not grant "
4384 "BlockGuard\n");
4385 }
James Smart34b02dc2008-08-24 21:49:55 -04004386 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004387 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004388 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004389 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004390 }
James Smart92d7f7b2007-06-17 19:56:38 -05004391do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004392 mempool_free(pmb, phba->mbox_mem_pool);
4393 return rc;
4394}
4395
James Smarte59058c2008-08-24 21:49:00 -04004396
4397/**
James Smart3621a712009-04-06 18:47:14 -04004398 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004399 * @phba: Pointer to HBA context object.
4400 *
4401 * This function is the main SLI intialization function. This function
4402 * is called by the HBA intialization code, HBA reset code and HBA
4403 * error attention handler code. Caller is not required to hold any
4404 * locks. This function issues config_port mailbox command to configure
4405 * the SLI, setup iocb rings and HBQ rings. In the end the function
4406 * calls the config_port_post function to issue init_link mailbox
4407 * command and to start the discovery. The function will return zero
4408 * if successful, else it will return negative error code.
4409 **/
James Smarted957682007-06-17 19:56:37 -05004410int
4411lpfc_sli_hba_setup(struct lpfc_hba *phba)
4412{
4413 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004414 int mode = 3, i;
4415 int longs;
James Smarted957682007-06-17 19:56:37 -05004416
4417 switch (lpfc_sli_mode) {
4418 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004419 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004421 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004422 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004423 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004424 break;
4425 }
James Smarted957682007-06-17 19:56:37 -05004426 mode = 2;
4427 break;
4428 case 0:
4429 case 3:
4430 break;
4431 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004433 "1819 Unrecognized lpfc_sli_mode "
4434 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004435
4436 break;
4437 }
4438
James Smart93996272008-08-24 21:50:30 -04004439 rc = lpfc_sli_config_port(phba, mode);
4440
James Smarted957682007-06-17 19:56:37 -05004441 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004443 "1820 Unable to select SLI-3. "
4444 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004445 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004446 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004447 if (rc)
dea31012005-04-17 16:05:31 -05004448 goto lpfc_sli_hba_setup_error;
4449
James Smart0d878412009-10-02 15:16:56 -04004450 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4451 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4452 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4453 if (!rc) {
4454 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4455 "2709 This device supports "
4456 "Advanced Error Reporting (AER)\n");
4457 spin_lock_irq(&phba->hbalock);
4458 phba->hba_flag |= HBA_AER_ENABLED;
4459 spin_unlock_irq(&phba->hbalock);
4460 } else {
4461 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4462 "2708 This device does not support "
4463 "Advanced Error Reporting (AER)\n");
4464 phba->cfg_aer_support = 0;
4465 }
4466 }
4467
James Smarted957682007-06-17 19:56:37 -05004468 if (phba->sli_rev == 3) {
4469 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4470 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004471 } else {
4472 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4473 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004474 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004475 }
4476
4477 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004478 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4479 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004480 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004481
4482 if (rc)
4483 goto lpfc_sli_hba_setup_error;
4484
James Smart6d368e52011-05-24 11:44:12 -04004485 /* Initialize VPIs. */
4486 if (phba->sli_rev == LPFC_SLI_REV3) {
4487 /*
4488 * The VPI bitmask and physical ID array are allocated
4489 * and initialized once only - at driver load. A port
4490 * reset doesn't need to reinitialize this memory.
4491 */
4492 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4493 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4494 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4495 GFP_KERNEL);
4496 if (!phba->vpi_bmask) {
4497 rc = -ENOMEM;
4498 goto lpfc_sli_hba_setup_error;
4499 }
4500
4501 phba->vpi_ids = kzalloc(
4502 (phba->max_vpi+1) * sizeof(uint16_t),
4503 GFP_KERNEL);
4504 if (!phba->vpi_ids) {
4505 kfree(phba->vpi_bmask);
4506 rc = -ENOMEM;
4507 goto lpfc_sli_hba_setup_error;
4508 }
4509 for (i = 0; i < phba->max_vpi; i++)
4510 phba->vpi_ids[i] = i;
4511 }
4512 }
4513
James Smart93996272008-08-24 21:50:30 -04004514 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004515 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4516 rc = lpfc_sli_hbq_setup(phba);
4517 if (rc)
4518 goto lpfc_sli_hba_setup_error;
4519 }
James Smart04c68492009-05-22 14:52:52 -04004520 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004521 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004522 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004523
4524 rc = lpfc_config_port_post(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527
James Smarted957682007-06-17 19:56:37 -05004528 return rc;
4529
James Smart92d7f7b2007-06-17 19:56:38 -05004530lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004531 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004533 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004534 return rc;
4535}
4536
James Smartda0436e2009-05-22 14:51:39 -04004537/**
4538 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4539 * @phba: Pointer to HBA context object.
4540 * @mboxq: mailbox pointer.
4541 * This function issue a dump mailbox command to read config region
4542 * 23 and parse the records in the region and populate driver
4543 * data structure.
4544 **/
4545static int
4546lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4547 LPFC_MBOXQ_t *mboxq)
4548{
4549 struct lpfc_dmabuf *mp;
4550 struct lpfc_mqe *mqe;
4551 uint32_t data_length;
4552 int rc;
4553
4554 /* Program the default value of vlan_id and fc_map */
4555 phba->valid_vlan = 0;
4556 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4557 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4558 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4559
4560 mqe = &mboxq->u.mqe;
4561 if (lpfc_dump_fcoe_param(phba, mboxq))
4562 return -ENOMEM;
4563
4564 mp = (struct lpfc_dmabuf *) mboxq->context1;
4565 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4566
4567 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4568 "(%d):2571 Mailbox cmd x%x Status x%x "
4569 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4570 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4571 "CQ: x%x x%x x%x x%x\n",
4572 mboxq->vport ? mboxq->vport->vpi : 0,
4573 bf_get(lpfc_mqe_command, mqe),
4574 bf_get(lpfc_mqe_status, mqe),
4575 mqe->un.mb_words[0], mqe->un.mb_words[1],
4576 mqe->un.mb_words[2], mqe->un.mb_words[3],
4577 mqe->un.mb_words[4], mqe->un.mb_words[5],
4578 mqe->un.mb_words[6], mqe->un.mb_words[7],
4579 mqe->un.mb_words[8], mqe->un.mb_words[9],
4580 mqe->un.mb_words[10], mqe->un.mb_words[11],
4581 mqe->un.mb_words[12], mqe->un.mb_words[13],
4582 mqe->un.mb_words[14], mqe->un.mb_words[15],
4583 mqe->un.mb_words[16], mqe->un.mb_words[50],
4584 mboxq->mcqe.word0,
4585 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4586 mboxq->mcqe.trailer);
4587
4588 if (rc) {
4589 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4590 kfree(mp);
4591 return -EIO;
4592 }
4593 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004594 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004595 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4596 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004597 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004598 }
James Smartda0436e2009-05-22 14:51:39 -04004599
4600 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4601 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4602 kfree(mp);
4603 return 0;
4604}
4605
4606/**
4607 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4608 * @phba: pointer to lpfc hba data structure.
4609 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4610 * @vpd: pointer to the memory to hold resulting port vpd data.
4611 * @vpd_size: On input, the number of bytes allocated to @vpd.
4612 * On output, the number of data bytes in @vpd.
4613 *
4614 * This routine executes a READ_REV SLI4 mailbox command. In
4615 * addition, this routine gets the port vpd data.
4616 *
4617 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004618 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004619 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004620 **/
4621static int
4622lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4623 uint8_t *vpd, uint32_t *vpd_size)
4624{
4625 int rc = 0;
4626 uint32_t dma_size;
4627 struct lpfc_dmabuf *dmabuf;
4628 struct lpfc_mqe *mqe;
4629
4630 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4631 if (!dmabuf)
4632 return -ENOMEM;
4633
4634 /*
4635 * Get a DMA buffer for the vpd data resulting from the READ_REV
4636 * mailbox command.
4637 */
4638 dma_size = *vpd_size;
4639 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4640 dma_size,
4641 &dmabuf->phys,
4642 GFP_KERNEL);
4643 if (!dmabuf->virt) {
4644 kfree(dmabuf);
4645 return -ENOMEM;
4646 }
4647 memset(dmabuf->virt, 0, dma_size);
4648
4649 /*
4650 * The SLI4 implementation of READ_REV conflicts at word1,
4651 * bits 31:16 and SLI4 adds vpd functionality not present
4652 * in SLI3. This code corrects the conflicts.
4653 */
4654 lpfc_read_rev(phba, mboxq);
4655 mqe = &mboxq->u.mqe;
4656 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4657 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4658 mqe->un.read_rev.word1 &= 0x0000FFFF;
4659 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4660 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4661
4662 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4663 if (rc) {
4664 dma_free_coherent(&phba->pcidev->dev, dma_size,
4665 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004666 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004667 return -EIO;
4668 }
4669
James Smartda0436e2009-05-22 14:51:39 -04004670 /*
4671 * The available vpd length cannot be bigger than the
4672 * DMA buffer passed to the port. Catch the less than
4673 * case and update the caller's size.
4674 */
4675 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4676 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4677
James Smartd7c47992010-06-08 18:31:54 -04004678 memcpy(vpd, dmabuf->virt, *vpd_size);
4679
James Smartda0436e2009-05-22 14:51:39 -04004680 dma_free_coherent(&phba->pcidev->dev, dma_size,
4681 dmabuf->virt, dmabuf->phys);
4682 kfree(dmabuf);
4683 return 0;
4684}
4685
4686/**
4687 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4688 * @phba: pointer to lpfc hba data structure.
4689 *
4690 * This routine is called to explicitly arm the SLI4 device's completion and
4691 * event queues
4692 **/
4693static void
4694lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4695{
4696 uint8_t fcp_eqidx;
4697
4698 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4699 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004700 fcp_eqidx = 0;
4701 do
James Smartda0436e2009-05-22 14:51:39 -04004702 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4703 LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004704 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04004705 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4706 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4707 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4708 LPFC_QUEUE_REARM);
4709}
4710
4711/**
James Smart6d368e52011-05-24 11:44:12 -04004712 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4713 * @phba: Pointer to HBA context object.
4714 * @type: The resource extent type.
4715 *
4716 * This function allocates all SLI4 resource identifiers.
4717 **/
4718static int
4719lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4720 uint16_t *extnt_count, uint16_t *extnt_size)
4721{
4722 int rc = 0;
4723 uint32_t length;
4724 uint32_t mbox_tmo;
4725 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4726 LPFC_MBOXQ_t *mbox;
4727
4728 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4729 if (!mbox)
4730 return -ENOMEM;
4731
4732 /* Find out how many extents are available for this resource type */
4733 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4734 sizeof(struct lpfc_sli4_cfg_mhdr));
4735 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4736 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4737 length, LPFC_SLI4_MBX_EMBED);
4738
4739 /* Send an extents count of 0 - the GET doesn't use it. */
4740 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4741 LPFC_SLI4_MBX_EMBED);
4742 if (unlikely(rc)) {
4743 rc = -EIO;
4744 goto err_exit;
4745 }
4746
4747 if (!phba->sli4_hba.intr_enable)
4748 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4749 else {
4750 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4751 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4752 }
4753 if (unlikely(rc)) {
4754 rc = -EIO;
4755 goto err_exit;
4756 }
4757
4758 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4759 if (bf_get(lpfc_mbox_hdr_status,
4760 &rsrc_info->header.cfg_shdr.response)) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4762 "2930 Failed to get resource extents "
4763 "Status 0x%x Add'l Status 0x%x\n",
4764 bf_get(lpfc_mbox_hdr_status,
4765 &rsrc_info->header.cfg_shdr.response),
4766 bf_get(lpfc_mbox_hdr_add_status,
4767 &rsrc_info->header.cfg_shdr.response));
4768 rc = -EIO;
4769 goto err_exit;
4770 }
4771
4772 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4773 &rsrc_info->u.rsp);
4774 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4775 &rsrc_info->u.rsp);
4776 err_exit:
4777 mempool_free(mbox, phba->mbox_mem_pool);
4778 return rc;
4779}
4780
4781/**
4782 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4783 * @phba: Pointer to HBA context object.
4784 * @type: The extent type to check.
4785 *
4786 * This function reads the current available extents from the port and checks
4787 * if the extent count or extent size has changed since the last access.
4788 * Callers use this routine post port reset to understand if there is a
4789 * extent reprovisioning requirement.
4790 *
4791 * Returns:
4792 * -Error: error indicates problem.
4793 * 1: Extent count or size has changed.
4794 * 0: No changes.
4795 **/
4796static int
4797lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4798{
4799 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4800 uint16_t size_diff, rsrc_ext_size;
4801 int rc = 0;
4802 struct lpfc_rsrc_blks *rsrc_entry;
4803 struct list_head *rsrc_blk_list = NULL;
4804
4805 size_diff = 0;
4806 curr_ext_cnt = 0;
4807 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4808 &rsrc_ext_cnt,
4809 &rsrc_ext_size);
4810 if (unlikely(rc))
4811 return -EIO;
4812
4813 switch (type) {
4814 case LPFC_RSC_TYPE_FCOE_RPI:
4815 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4816 break;
4817 case LPFC_RSC_TYPE_FCOE_VPI:
4818 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4819 break;
4820 case LPFC_RSC_TYPE_FCOE_XRI:
4821 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4822 break;
4823 case LPFC_RSC_TYPE_FCOE_VFI:
4824 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
4825 break;
4826 default:
4827 break;
4828 }
4829
4830 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
4831 curr_ext_cnt++;
4832 if (rsrc_entry->rsrc_size != rsrc_ext_size)
4833 size_diff++;
4834 }
4835
4836 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
4837 rc = 1;
4838
4839 return rc;
4840}
4841
4842/**
4843 * lpfc_sli4_cfg_post_extnts -
4844 * @phba: Pointer to HBA context object.
4845 * @extnt_cnt - number of available extents.
4846 * @type - the extent type (rpi, xri, vfi, vpi).
4847 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
4848 * @mbox - pointer to the caller's allocated mailbox structure.
4849 *
4850 * This function executes the extents allocation request. It also
4851 * takes care of the amount of memory needed to allocate or get the
4852 * allocated extents. It is the caller's responsibility to evaluate
4853 * the response.
4854 *
4855 * Returns:
4856 * -Error: Error value describes the condition found.
4857 * 0: if successful
4858 **/
4859static int
4860lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
4861 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
4862{
4863 int rc = 0;
4864 uint32_t req_len;
4865 uint32_t emb_len;
4866 uint32_t alloc_len, mbox_tmo;
4867
4868 /* Calculate the total requested length of the dma memory */
4869 req_len = *extnt_cnt * sizeof(uint16_t);
4870
4871 /*
4872 * Calculate the size of an embedded mailbox. The uint32_t
4873 * accounts for extents-specific word.
4874 */
4875 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
4876 sizeof(uint32_t);
4877
4878 /*
4879 * Presume the allocation and response will fit into an embedded
4880 * mailbox. If not true, reconfigure to a non-embedded mailbox.
4881 */
4882 *emb = LPFC_SLI4_MBX_EMBED;
4883 if (req_len > emb_len) {
4884 req_len = *extnt_cnt * sizeof(uint16_t) +
4885 sizeof(union lpfc_sli4_cfg_shdr) +
4886 sizeof(uint32_t);
4887 *emb = LPFC_SLI4_MBX_NEMBED;
4888 }
4889
4890 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4891 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
4892 req_len, *emb);
4893 if (alloc_len < req_len) {
4894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4895 "9000 Allocated DMA memory size (x%x) is "
4896 "less than the requested DMA memory "
4897 "size (x%x)\n", alloc_len, req_len);
4898 return -ENOMEM;
4899 }
4900 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
4901 if (unlikely(rc))
4902 return -EIO;
4903
4904 if (!phba->sli4_hba.intr_enable)
4905 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4906 else {
4907 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4908 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4909 }
4910
4911 if (unlikely(rc))
4912 rc = -EIO;
4913 return rc;
4914}
4915
4916/**
4917 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
4918 * @phba: Pointer to HBA context object.
4919 * @type: The resource extent type to allocate.
4920 *
4921 * This function allocates the number of elements for the specified
4922 * resource type.
4923 **/
4924static int
4925lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
4926{
4927 bool emb = false;
4928 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
4929 uint16_t rsrc_id, rsrc_start, j, k;
4930 uint16_t *ids;
4931 int i, rc;
4932 unsigned long longs;
4933 unsigned long *bmask;
4934 struct lpfc_rsrc_blks *rsrc_blks;
4935 LPFC_MBOXQ_t *mbox;
4936 uint32_t length;
4937 struct lpfc_id_range *id_array = NULL;
4938 void *virtaddr = NULL;
4939 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
4940 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
4941 struct list_head *ext_blk_list;
4942
4943 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4944 &rsrc_cnt,
4945 &rsrc_size);
4946 if (unlikely(rc))
4947 return -EIO;
4948
4949 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
4950 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4951 "3009 No available Resource Extents "
4952 "for resource type 0x%x: Count: 0x%x, "
4953 "Size 0x%x\n", type, rsrc_cnt,
4954 rsrc_size);
4955 return -ENOMEM;
4956 }
4957
4958 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
4959 "2903 Available Resource Extents "
4960 "for resource type 0x%x: Count: 0x%x, "
4961 "Size 0x%x\n", type, rsrc_cnt,
4962 rsrc_size);
4963
4964 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4965 if (!mbox)
4966 return -ENOMEM;
4967
4968 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
4969 if (unlikely(rc)) {
4970 rc = -EIO;
4971 goto err_exit;
4972 }
4973
4974 /*
4975 * Figure out where the response is located. Then get local pointers
4976 * to the response data. The port does not guarantee to respond to
4977 * all extents counts request so update the local variable with the
4978 * allocated count from the port.
4979 */
4980 if (emb == LPFC_SLI4_MBX_EMBED) {
4981 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
4982 id_array = &rsrc_ext->u.rsp.id[0];
4983 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
4984 } else {
4985 virtaddr = mbox->sge_array->addr[0];
4986 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
4987 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
4988 id_array = &n_rsrc->id;
4989 }
4990
4991 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4992 rsrc_id_cnt = rsrc_cnt * rsrc_size;
4993
4994 /*
4995 * Based on the resource size and count, correct the base and max
4996 * resource values.
4997 */
4998 length = sizeof(struct lpfc_rsrc_blks);
4999 switch (type) {
5000 case LPFC_RSC_TYPE_FCOE_RPI:
5001 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5002 sizeof(unsigned long),
5003 GFP_KERNEL);
5004 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5005 rc = -ENOMEM;
5006 goto err_exit;
5007 }
5008 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5009 sizeof(uint16_t),
5010 GFP_KERNEL);
5011 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5012 kfree(phba->sli4_hba.rpi_bmask);
5013 rc = -ENOMEM;
5014 goto err_exit;
5015 }
5016
5017 /*
5018 * The next_rpi was initialized with the maximum available
5019 * count but the port may allocate a smaller number. Catch
5020 * that case and update the next_rpi.
5021 */
5022 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5023
5024 /* Initialize local ptrs for common extent processing later. */
5025 bmask = phba->sli4_hba.rpi_bmask;
5026 ids = phba->sli4_hba.rpi_ids;
5027 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5028 break;
5029 case LPFC_RSC_TYPE_FCOE_VPI:
5030 phba->vpi_bmask = kzalloc(longs *
5031 sizeof(unsigned long),
5032 GFP_KERNEL);
5033 if (unlikely(!phba->vpi_bmask)) {
5034 rc = -ENOMEM;
5035 goto err_exit;
5036 }
5037 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5038 sizeof(uint16_t),
5039 GFP_KERNEL);
5040 if (unlikely(!phba->vpi_ids)) {
5041 kfree(phba->vpi_bmask);
5042 rc = -ENOMEM;
5043 goto err_exit;
5044 }
5045
5046 /* Initialize local ptrs for common extent processing later. */
5047 bmask = phba->vpi_bmask;
5048 ids = phba->vpi_ids;
5049 ext_blk_list = &phba->lpfc_vpi_blk_list;
5050 break;
5051 case LPFC_RSC_TYPE_FCOE_XRI:
5052 phba->sli4_hba.xri_bmask = kzalloc(longs *
5053 sizeof(unsigned long),
5054 GFP_KERNEL);
5055 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5056 rc = -ENOMEM;
5057 goto err_exit;
5058 }
5059 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5060 sizeof(uint16_t),
5061 GFP_KERNEL);
5062 if (unlikely(!phba->sli4_hba.xri_ids)) {
5063 kfree(phba->sli4_hba.xri_bmask);
5064 rc = -ENOMEM;
5065 goto err_exit;
5066 }
5067
5068 /* Initialize local ptrs for common extent processing later. */
5069 bmask = phba->sli4_hba.xri_bmask;
5070 ids = phba->sli4_hba.xri_ids;
5071 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5072 break;
5073 case LPFC_RSC_TYPE_FCOE_VFI:
5074 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5075 sizeof(unsigned long),
5076 GFP_KERNEL);
5077 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5078 rc = -ENOMEM;
5079 goto err_exit;
5080 }
5081 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5082 sizeof(uint16_t),
5083 GFP_KERNEL);
5084 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5085 kfree(phba->sli4_hba.vfi_bmask);
5086 rc = -ENOMEM;
5087 goto err_exit;
5088 }
5089
5090 /* Initialize local ptrs for common extent processing later. */
5091 bmask = phba->sli4_hba.vfi_bmask;
5092 ids = phba->sli4_hba.vfi_ids;
5093 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5094 break;
5095 default:
5096 /* Unsupported Opcode. Fail call. */
5097 id_array = NULL;
5098 bmask = NULL;
5099 ids = NULL;
5100 ext_blk_list = NULL;
5101 goto err_exit;
5102 }
5103
5104 /*
5105 * Complete initializing the extent configuration with the
5106 * allocated ids assigned to this function. The bitmask serves
5107 * as an index into the array and manages the available ids. The
5108 * array just stores the ids communicated to the port via the wqes.
5109 */
5110 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5111 if ((i % 2) == 0)
5112 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5113 &id_array[k]);
5114 else
5115 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5116 &id_array[k]);
5117
5118 rsrc_blks = kzalloc(length, GFP_KERNEL);
5119 if (unlikely(!rsrc_blks)) {
5120 rc = -ENOMEM;
5121 kfree(bmask);
5122 kfree(ids);
5123 goto err_exit;
5124 }
5125 rsrc_blks->rsrc_start = rsrc_id;
5126 rsrc_blks->rsrc_size = rsrc_size;
5127 list_add_tail(&rsrc_blks->list, ext_blk_list);
5128 rsrc_start = rsrc_id;
5129 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5130 phba->sli4_hba.scsi_xri_start = rsrc_start +
5131 lpfc_sli4_get_els_iocb_cnt(phba);
5132
5133 while (rsrc_id < (rsrc_start + rsrc_size)) {
5134 ids[j] = rsrc_id;
5135 rsrc_id++;
5136 j++;
5137 }
5138 /* Entire word processed. Get next word.*/
5139 if ((i % 2) == 1)
5140 k++;
5141 }
5142 err_exit:
5143 lpfc_sli4_mbox_cmd_free(phba, mbox);
5144 return rc;
5145}
5146
5147/**
5148 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5149 * @phba: Pointer to HBA context object.
5150 * @type: the extent's type.
5151 *
5152 * This function deallocates all extents of a particular resource type.
5153 * SLI4 does not allow for deallocating a particular extent range. It
5154 * is the caller's responsibility to release all kernel memory resources.
5155 **/
5156static int
5157lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5158{
5159 int rc;
5160 uint32_t length, mbox_tmo = 0;
5161 LPFC_MBOXQ_t *mbox;
5162 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5163 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5164
5165 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5166 if (!mbox)
5167 return -ENOMEM;
5168
5169 /*
5170 * This function sends an embedded mailbox because it only sends the
5171 * the resource type. All extents of this type are released by the
5172 * port.
5173 */
5174 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5175 sizeof(struct lpfc_sli4_cfg_mhdr));
5176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5177 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5178 length, LPFC_SLI4_MBX_EMBED);
5179
5180 /* Send an extents count of 0 - the dealloc doesn't use it. */
5181 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5182 LPFC_SLI4_MBX_EMBED);
5183 if (unlikely(rc)) {
5184 rc = -EIO;
5185 goto out_free_mbox;
5186 }
5187 if (!phba->sli4_hba.intr_enable)
5188 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5189 else {
5190 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox_tmo);
5191 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5192 }
5193 if (unlikely(rc)) {
5194 rc = -EIO;
5195 goto out_free_mbox;
5196 }
5197
5198 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5199 if (bf_get(lpfc_mbox_hdr_status,
5200 &dealloc_rsrc->header.cfg_shdr.response)) {
5201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5202 "2919 Failed to release resource extents "
5203 "for type %d - Status 0x%x Add'l Status 0x%x. "
5204 "Resource memory not released.\n",
5205 type,
5206 bf_get(lpfc_mbox_hdr_status,
5207 &dealloc_rsrc->header.cfg_shdr.response),
5208 bf_get(lpfc_mbox_hdr_add_status,
5209 &dealloc_rsrc->header.cfg_shdr.response));
5210 rc = -EIO;
5211 goto out_free_mbox;
5212 }
5213
5214 /* Release kernel memory resources for the specific type. */
5215 switch (type) {
5216 case LPFC_RSC_TYPE_FCOE_VPI:
5217 kfree(phba->vpi_bmask);
5218 kfree(phba->vpi_ids);
5219 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5220 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5221 &phba->lpfc_vpi_blk_list, list) {
5222 list_del_init(&rsrc_blk->list);
5223 kfree(rsrc_blk);
5224 }
5225 break;
5226 case LPFC_RSC_TYPE_FCOE_XRI:
5227 kfree(phba->sli4_hba.xri_bmask);
5228 kfree(phba->sli4_hba.xri_ids);
5229 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5230 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5231 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5232 list_del_init(&rsrc_blk->list);
5233 kfree(rsrc_blk);
5234 }
5235 break;
5236 case LPFC_RSC_TYPE_FCOE_VFI:
5237 kfree(phba->sli4_hba.vfi_bmask);
5238 kfree(phba->sli4_hba.vfi_ids);
5239 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5240 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5241 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5242 list_del_init(&rsrc_blk->list);
5243 kfree(rsrc_blk);
5244 }
5245 break;
5246 case LPFC_RSC_TYPE_FCOE_RPI:
5247 /* RPI bitmask and physical id array are cleaned up earlier. */
5248 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5249 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5250 list_del_init(&rsrc_blk->list);
5251 kfree(rsrc_blk);
5252 }
5253 break;
5254 default:
5255 break;
5256 }
5257
5258 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5259
5260 out_free_mbox:
5261 mempool_free(mbox, phba->mbox_mem_pool);
5262 return rc;
5263}
5264
5265/**
5266 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5267 * @phba: Pointer to HBA context object.
5268 *
5269 * This function allocates all SLI4 resource identifiers.
5270 **/
5271int
5272lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5273{
5274 int i, rc, error = 0;
5275 uint16_t count, base;
5276 unsigned long longs;
5277
5278 if (phba->sli4_hba.extents_in_use) {
5279 /*
5280 * The port supports resource extents. The XRI, VPI, VFI, RPI
5281 * resource extent count must be read and allocated before
5282 * provisioning the resource id arrays.
5283 */
5284 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5285 LPFC_IDX_RSRC_RDY) {
5286 /*
5287 * Extent-based resources are set - the driver could
5288 * be in a port reset. Figure out if any corrective
5289 * actions need to be taken.
5290 */
5291 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5292 LPFC_RSC_TYPE_FCOE_VFI);
5293 if (rc != 0)
5294 error++;
5295 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5296 LPFC_RSC_TYPE_FCOE_VPI);
5297 if (rc != 0)
5298 error++;
5299 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5300 LPFC_RSC_TYPE_FCOE_XRI);
5301 if (rc != 0)
5302 error++;
5303 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5304 LPFC_RSC_TYPE_FCOE_RPI);
5305 if (rc != 0)
5306 error++;
5307
5308 /*
5309 * It's possible that the number of resources
5310 * provided to this port instance changed between
5311 * resets. Detect this condition and reallocate
5312 * resources. Otherwise, there is no action.
5313 */
5314 if (error) {
5315 lpfc_printf_log(phba, KERN_INFO,
5316 LOG_MBOX | LOG_INIT,
5317 "2931 Detected extent resource "
5318 "change. Reallocating all "
5319 "extents.\n");
5320 rc = lpfc_sli4_dealloc_extent(phba,
5321 LPFC_RSC_TYPE_FCOE_VFI);
5322 rc = lpfc_sli4_dealloc_extent(phba,
5323 LPFC_RSC_TYPE_FCOE_VPI);
5324 rc = lpfc_sli4_dealloc_extent(phba,
5325 LPFC_RSC_TYPE_FCOE_XRI);
5326 rc = lpfc_sli4_dealloc_extent(phba,
5327 LPFC_RSC_TYPE_FCOE_RPI);
5328 } else
5329 return 0;
5330 }
5331
5332 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5333 if (unlikely(rc))
5334 goto err_exit;
5335
5336 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5337 if (unlikely(rc))
5338 goto err_exit;
5339
5340 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5341 if (unlikely(rc))
5342 goto err_exit;
5343
5344 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5345 if (unlikely(rc))
5346 goto err_exit;
5347 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5348 LPFC_IDX_RSRC_RDY);
5349 return rc;
5350 } else {
5351 /*
5352 * The port does not support resource extents. The XRI, VPI,
5353 * VFI, RPI resource ids were determined from READ_CONFIG.
5354 * Just allocate the bitmasks and provision the resource id
5355 * arrays. If a port reset is active, the resources don't
5356 * need any action - just exit.
5357 */
5358 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5359 LPFC_IDX_RSRC_RDY)
5360 return 0;
5361
5362 /* RPIs. */
5363 count = phba->sli4_hba.max_cfg_param.max_rpi;
5364 base = phba->sli4_hba.max_cfg_param.rpi_base;
5365 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5366 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5367 sizeof(unsigned long),
5368 GFP_KERNEL);
5369 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5370 rc = -ENOMEM;
5371 goto err_exit;
5372 }
5373 phba->sli4_hba.rpi_ids = kzalloc(count *
5374 sizeof(uint16_t),
5375 GFP_KERNEL);
5376 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5377 rc = -ENOMEM;
5378 goto free_rpi_bmask;
5379 }
5380
5381 for (i = 0; i < count; i++)
5382 phba->sli4_hba.rpi_ids[i] = base + i;
5383
5384 /* VPIs. */
5385 count = phba->sli4_hba.max_cfg_param.max_vpi;
5386 base = phba->sli4_hba.max_cfg_param.vpi_base;
5387 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5388 phba->vpi_bmask = kzalloc(longs *
5389 sizeof(unsigned long),
5390 GFP_KERNEL);
5391 if (unlikely(!phba->vpi_bmask)) {
5392 rc = -ENOMEM;
5393 goto free_rpi_ids;
5394 }
5395 phba->vpi_ids = kzalloc(count *
5396 sizeof(uint16_t),
5397 GFP_KERNEL);
5398 if (unlikely(!phba->vpi_ids)) {
5399 rc = -ENOMEM;
5400 goto free_vpi_bmask;
5401 }
5402
5403 for (i = 0; i < count; i++)
5404 phba->vpi_ids[i] = base + i;
5405
5406 /* XRIs. */
5407 count = phba->sli4_hba.max_cfg_param.max_xri;
5408 base = phba->sli4_hba.max_cfg_param.xri_base;
5409 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5410 phba->sli4_hba.xri_bmask = kzalloc(longs *
5411 sizeof(unsigned long),
5412 GFP_KERNEL);
5413 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5414 rc = -ENOMEM;
5415 goto free_vpi_ids;
5416 }
5417 phba->sli4_hba.xri_ids = kzalloc(count *
5418 sizeof(uint16_t),
5419 GFP_KERNEL);
5420 if (unlikely(!phba->sli4_hba.xri_ids)) {
5421 rc = -ENOMEM;
5422 goto free_xri_bmask;
5423 }
5424
5425 for (i = 0; i < count; i++)
5426 phba->sli4_hba.xri_ids[i] = base + i;
5427
5428 /* VFIs. */
5429 count = phba->sli4_hba.max_cfg_param.max_vfi;
5430 base = phba->sli4_hba.max_cfg_param.vfi_base;
5431 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5432 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5433 sizeof(unsigned long),
5434 GFP_KERNEL);
5435 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5436 rc = -ENOMEM;
5437 goto free_xri_ids;
5438 }
5439 phba->sli4_hba.vfi_ids = kzalloc(count *
5440 sizeof(uint16_t),
5441 GFP_KERNEL);
5442 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5443 rc = -ENOMEM;
5444 goto free_vfi_bmask;
5445 }
5446
5447 for (i = 0; i < count; i++)
5448 phba->sli4_hba.vfi_ids[i] = base + i;
5449
5450 /*
5451 * Mark all resources ready. An HBA reset doesn't need
5452 * to reset the initialization.
5453 */
5454 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5455 LPFC_IDX_RSRC_RDY);
5456 return 0;
5457 }
5458
5459 free_vfi_bmask:
5460 kfree(phba->sli4_hba.vfi_bmask);
5461 free_xri_ids:
5462 kfree(phba->sli4_hba.xri_ids);
5463 free_xri_bmask:
5464 kfree(phba->sli4_hba.xri_bmask);
5465 free_vpi_ids:
5466 kfree(phba->vpi_ids);
5467 free_vpi_bmask:
5468 kfree(phba->vpi_bmask);
5469 free_rpi_ids:
5470 kfree(phba->sli4_hba.rpi_ids);
5471 free_rpi_bmask:
5472 kfree(phba->sli4_hba.rpi_bmask);
5473 err_exit:
5474 return rc;
5475}
5476
5477/**
5478 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5479 * @phba: Pointer to HBA context object.
5480 *
5481 * This function allocates the number of elements for the specified
5482 * resource type.
5483 **/
5484int
5485lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5486{
5487 if (phba->sli4_hba.extents_in_use) {
5488 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5489 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5490 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5491 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5492 } else {
5493 kfree(phba->vpi_bmask);
5494 kfree(phba->vpi_ids);
5495 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5496 kfree(phba->sli4_hba.xri_bmask);
5497 kfree(phba->sli4_hba.xri_ids);
5498 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5499 kfree(phba->sli4_hba.vfi_bmask);
5500 kfree(phba->sli4_hba.vfi_ids);
5501 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5502 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5503 }
5504
5505 return 0;
5506}
5507
5508/**
James Smartda0436e2009-05-22 14:51:39 -04005509 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5510 * @phba: Pointer to HBA context object.
5511 *
5512 * This function is the main SLI4 device intialization PCI function. This
5513 * function is called by the HBA intialization code, HBA reset code and
5514 * HBA error attention handler code. Caller is not required to hold any
5515 * locks.
5516 **/
5517int
5518lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5519{
5520 int rc;
5521 LPFC_MBOXQ_t *mboxq;
5522 struct lpfc_mqe *mqe;
5523 uint8_t *vpd;
5524 uint32_t vpd_size;
5525 uint32_t ftr_rsp = 0;
5526 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5527 struct lpfc_vport *vport = phba->pport;
5528 struct lpfc_dmabuf *mp;
5529
5530 /* Perform a PCI function reset to start from clean */
5531 rc = lpfc_pci_function_reset(phba);
5532 if (unlikely(rc))
5533 return -ENODEV;
5534
5535 /* Check the HBA Host Status Register for readyness */
5536 rc = lpfc_sli4_post_status_check(phba);
5537 if (unlikely(rc))
5538 return -ENODEV;
5539 else {
5540 spin_lock_irq(&phba->hbalock);
5541 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5542 spin_unlock_irq(&phba->hbalock);
5543 }
5544
5545 /*
5546 * Allocate a single mailbox container for initializing the
5547 * port.
5548 */
5549 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5550 if (!mboxq)
5551 return -ENOMEM;
5552
5553 /*
5554 * Continue initialization with default values even if driver failed
5555 * to read FCoE param config regions
5556 */
5557 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
James Smart05580562011-05-24 11:40:48 -04005558 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04005559 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04005560
5561 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005562 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005563 vpd = kzalloc(vpd_size, GFP_KERNEL);
5564 if (!vpd) {
5565 rc = -ENOMEM;
5566 goto out_free_mbox;
5567 }
5568
5569 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005570 if (unlikely(rc)) {
5571 kfree(vpd);
5572 goto out_free_mbox;
5573 }
James Smartda0436e2009-05-22 14:51:39 -04005574 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005575 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5576 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005577 phba->hba_flag |= HBA_FCOE_MODE;
5578 else
5579 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005580
5581 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5582 LPFC_DCBX_CEE_MODE)
5583 phba->hba_flag |= HBA_FIP_SUPPORT;
5584 else
5585 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5586
James Smartc31098c2011-04-16 11:03:33 -04005587 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5589 "0376 READ_REV Error. SLI Level %d "
5590 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005591 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005592 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005593 kfree(vpd);
5594 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005595 }
James Smartda0436e2009-05-22 14:51:39 -04005596 /*
5597 * Evaluate the read rev and vpd data. Populate the driver
5598 * state with the results. If this routine fails, the failure
5599 * is not fatal as the driver will use generic values.
5600 */
5601 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5602 if (unlikely(!rc)) {
5603 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5604 "0377 Error %d parsing vpd. "
5605 "Using defaults.\n", rc);
5606 rc = 0;
5607 }
James Smart76a95d72010-11-20 23:11:48 -05005608 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005609
James Smartf1126682009-06-10 17:22:44 -04005610 /* Save information as VPD data */
5611 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5612 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5613 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5614 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5615 &mqe->un.read_rev);
5616 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5617 &mqe->un.read_rev);
5618 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5619 &mqe->un.read_rev);
5620 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5621 &mqe->un.read_rev);
5622 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5623 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5624 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5625 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5626 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5627 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5628 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5629 "(%d):0380 READ_REV Status x%x "
5630 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5631 mboxq->vport ? mboxq->vport->vpi : 0,
5632 bf_get(lpfc_mqe_status, mqe),
5633 phba->vpd.rev.opFwName,
5634 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5635 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04005636
5637 /*
5638 * Discover the port's supported feature set and match it against the
5639 * hosts requests.
5640 */
5641 lpfc_request_features(phba, mboxq);
5642 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5643 if (unlikely(rc)) {
5644 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005645 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005646 }
5647
5648 /*
5649 * The port must support FCP initiator mode as this is the
5650 * only mode running in the host.
5651 */
5652 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5653 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5654 "0378 No support for fcpi mode.\n");
5655 ftr_rsp++;
5656 }
James Smartfedd3b72011-02-16 12:39:24 -05005657 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5658 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5659 else
5660 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04005661 /*
5662 * If the port cannot support the host's requested features
5663 * then turn off the global config parameters to disable the
5664 * feature in the driver. This is not a fatal error.
5665 */
5666 if ((phba->cfg_enable_bg) &&
5667 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5668 ftr_rsp++;
5669
5670 if (phba->max_vpi && phba->cfg_enable_npiv &&
5671 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5672 ftr_rsp++;
5673
5674 if (ftr_rsp) {
5675 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5676 "0379 Feature Mismatch Data: x%08x %08x "
5677 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
5678 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
5679 phba->cfg_enable_npiv, phba->max_vpi);
5680 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5681 phba->cfg_enable_bg = 0;
5682 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5683 phba->cfg_enable_npiv = 0;
5684 }
5685
5686 /* These SLI3 features are assumed in SLI4 */
5687 spin_lock_irq(&phba->hbalock);
5688 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
5689 spin_unlock_irq(&phba->hbalock);
5690
James Smart6d368e52011-05-24 11:44:12 -04005691 /*
5692 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
5693 * calls depends on these resources to complete port setup.
5694 */
5695 rc = lpfc_sli4_alloc_resource_identifiers(phba);
5696 if (rc) {
5697 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5698 "2920 Failed to alloc Resource IDs "
5699 "rc = x%x\n", rc);
5700 goto out_free_mbox;
5701 }
5702
James Smartda0436e2009-05-22 14:51:39 -04005703 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05005704 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
5705 if (rc) {
5706 phba->link_state = LPFC_HBA_ERROR;
5707 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05005708 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05005709 }
5710
James Smartda0436e2009-05-22 14:51:39 -04005711 mboxq->vport = vport;
5712 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5713 mp = (struct lpfc_dmabuf *) mboxq->context1;
5714 if (rc == MBX_SUCCESS) {
5715 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
5716 rc = 0;
5717 }
5718
5719 /*
5720 * This memory was allocated by the lpfc_read_sparam routine. Release
5721 * it to the mbuf pool.
5722 */
5723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5724 kfree(mp);
5725 mboxq->context1 = NULL;
5726 if (unlikely(rc)) {
5727 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5728 "0382 READ_SPARAM command failed "
5729 "status %d, mbxStatus x%x\n",
5730 rc, bf_get(lpfc_mqe_status, mqe));
5731 phba->link_state = LPFC_HBA_ERROR;
5732 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005733 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005734 }
5735
James Smart05580562011-05-24 11:40:48 -04005736 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04005737
5738 /* Update the fc_host data structures with new wwn. */
5739 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
5740 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
5741
5742 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04005743 if (!phba->sli4_hba.extents_in_use) {
5744 rc = lpfc_sli4_post_els_sgl_list(phba);
5745 if (unlikely(rc)) {
5746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5747 "0582 Error %d during els sgl post "
5748 "operation\n", rc);
5749 rc = -ENODEV;
5750 goto out_free_mbox;
5751 }
5752 } else {
5753 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
5754 if (unlikely(rc)) {
5755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5756 "2560 Error %d during els sgl post "
5757 "operation\n", rc);
5758 rc = -ENODEV;
5759 goto out_free_mbox;
5760 }
James Smartda0436e2009-05-22 14:51:39 -04005761 }
5762
5763 /* Register SCSI SGL pool to the device */
5764 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
5765 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04005766 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04005767 "0383 Error %d during scsi sgl post "
5768 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005769 /* Some Scsi buffers were moved to the abort scsi list */
5770 /* A pci function reset will repost them */
5771 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005772 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005773 }
5774
5775 /* Post the rpi header region to the device. */
5776 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
5777 if (unlikely(rc)) {
5778 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5779 "0393 Error %d during rpi post operation\n",
5780 rc);
5781 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005782 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005783 }
James Smartda0436e2009-05-22 14:51:39 -04005784
5785 /* Set up all the queues to the device */
5786 rc = lpfc_sli4_queue_setup(phba);
5787 if (unlikely(rc)) {
5788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5789 "0381 Error %d during queue setup.\n ", rc);
5790 goto out_stop_timers;
5791 }
5792
5793 /* Arm the CQs and then EQs on device */
5794 lpfc_sli4_arm_cqeq_intr(phba);
5795
5796 /* Indicate device interrupt mode */
5797 phba->sli4_hba.intr_enable = 1;
5798
5799 /* Allow asynchronous mailbox command to go through */
5800 spin_lock_irq(&phba->hbalock);
5801 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5802 spin_unlock_irq(&phba->hbalock);
5803
5804 /* Post receive buffers to the device */
5805 lpfc_sli4_rb_setup(phba);
5806
James Smartfc2b9892010-02-26 14:15:29 -05005807 /* Reset HBA FCF states after HBA reset */
5808 phba->fcf.fcf_flag = 0;
5809 phba->fcf.current_rec.flag = 0;
5810
James Smartda0436e2009-05-22 14:51:39 -04005811 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04005812 mod_timer(&vport->els_tmofunc,
5813 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04005814
5815 /* Start heart beat timer */
5816 mod_timer(&phba->hb_tmofunc,
5817 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
5818 phba->hb_outstanding = 0;
5819 phba->last_completion_time = jiffies;
5820
5821 /* Start error attention (ERATT) polling timer */
5822 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
5823
James Smart75baf692010-06-08 18:31:21 -04005824 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5825 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5826 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5827 if (!rc) {
5828 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5829 "2829 This device supports "
5830 "Advanced Error Reporting (AER)\n");
5831 spin_lock_irq(&phba->hbalock);
5832 phba->hba_flag |= HBA_AER_ENABLED;
5833 spin_unlock_irq(&phba->hbalock);
5834 } else {
5835 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5836 "2830 This device does not support "
5837 "Advanced Error Reporting (AER)\n");
5838 phba->cfg_aer_support = 0;
5839 }
5840 }
5841
James Smart76a95d72010-11-20 23:11:48 -05005842 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5843 /*
5844 * The FC Port needs to register FCFI (index 0)
5845 */
5846 lpfc_reg_fcfi(phba, mboxq);
5847 mboxq->vport = phba->pport;
5848 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04005849 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05005850 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04005851 rc = 0;
5852 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
5853 &mboxq->u.mqe.un.reg_fcfi);
James Smart76a95d72010-11-20 23:11:48 -05005854 }
James Smartda0436e2009-05-22 14:51:39 -04005855 /*
5856 * The port is ready, set the host's link state to LINK_DOWN
5857 * in preparation for link interrupts.
5858 */
James Smartda0436e2009-05-22 14:51:39 -04005859 spin_lock_irq(&phba->hbalock);
5860 phba->link_state = LPFC_LINK_DOWN;
5861 spin_unlock_irq(&phba->hbalock);
James Smartfedd3b72011-02-16 12:39:24 -05005862 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5863 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart76a95d72010-11-20 23:11:48 -05005864out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04005865 /* Unset all the queues set up in this routine when error out */
5866 if (rc)
5867 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005868out_stop_timers:
5869 if (rc)
5870 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04005871out_free_mbox:
5872 mempool_free(mboxq, phba->mbox_mem_pool);
5873 return rc;
5874}
James Smarte59058c2008-08-24 21:49:00 -04005875
5876/**
James Smart3621a712009-04-06 18:47:14 -04005877 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04005878 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05005879 *
James Smarte59058c2008-08-24 21:49:00 -04005880 * This is the callback function for mailbox timer. The mailbox
5881 * timer is armed when a new mailbox command is issued and the timer
5882 * is deleted when the mailbox complete. The function is called by
5883 * the kernel timer code when a mailbox does not complete within
5884 * expected time. This function wakes up the worker thread to
5885 * process the mailbox timeout and returns. All the processing is
5886 * done by the worker thread function lpfc_mbox_timeout_handler.
5887 **/
dea31012005-04-17 16:05:31 -05005888void
5889lpfc_mbox_timeout(unsigned long ptr)
5890{
James Smart92d7f7b2007-06-17 19:56:38 -05005891 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05005892 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05005893 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005894
James Smart2e0fef82007-06-17 19:56:36 -05005895 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005896 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005897 if (!tmo_posted)
5898 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5899 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5900
James Smart5e9d9b82008-06-14 22:52:53 -04005901 if (!tmo_posted)
5902 lpfc_worker_wake_up(phba);
5903 return;
dea31012005-04-17 16:05:31 -05005904}
5905
James Smarte59058c2008-08-24 21:49:00 -04005906
5907/**
James Smart3621a712009-04-06 18:47:14 -04005908 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04005909 * @phba: Pointer to HBA context object.
5910 *
5911 * This function is called from worker thread when a mailbox command times out.
5912 * The caller is not required to hold any locks. This function will reset the
5913 * HBA and recover all the pending commands.
5914 **/
dea31012005-04-17 16:05:31 -05005915void
5916lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5917{
James Smart2e0fef82007-06-17 19:56:36 -05005918 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04005919 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04005920 struct lpfc_sli *psli = &phba->sli;
5921 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005922
James Smarta257bf92009-04-06 18:48:10 -04005923 /* Check the pmbox pointer first. There is a race condition
5924 * between the mbox timeout handler getting executed in the
5925 * worklist and the mailbox actually completing. When this
5926 * race condition occurs, the mbox_active will be NULL.
5927 */
5928 spin_lock_irq(&phba->hbalock);
5929 if (pmbox == NULL) {
5930 lpfc_printf_log(phba, KERN_WARNING,
5931 LOG_MBOX | LOG_SLI,
5932 "0353 Active Mailbox cleared - mailbox timeout "
5933 "exiting\n");
5934 spin_unlock_irq(&phba->hbalock);
5935 return;
5936 }
5937
dea31012005-04-17 16:05:31 -05005938 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05005939 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005940 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005941 mb->mbxCommand,
5942 phba->pport->port_state,
5943 phba->sli.sli_flag,
5944 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04005945 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005946
James Smart1dcb58e2007-04-25 09:51:30 -04005947 /* Setting state unknown so lpfc_sli_abort_iocb_ring
5948 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005949 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04005950 */
James Smart2e0fef82007-06-17 19:56:36 -05005951 spin_lock_irq(&phba->pport->work_port_lock);
5952 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5953 spin_unlock_irq(&phba->pport->work_port_lock);
5954 spin_lock_irq(&phba->hbalock);
5955 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04005956 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005957 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04005958
5959 pring = &psli->ring[psli->fcp_ring];
5960 lpfc_sli_abort_iocb_ring(phba, pring);
5961
5962 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005963 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04005964
5965 /* Reset the HBA device */
5966 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05005967}
5968
James Smarte59058c2008-08-24 21:49:00 -04005969/**
James Smart3772a992009-05-22 14:50:54 -04005970 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04005971 * @phba: Pointer to HBA context object.
5972 * @pmbox: Pointer to mailbox object.
5973 * @flag: Flag indicating how the mailbox need to be processed.
5974 *
5975 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04005976 * to submit a mailbox command to firmware with SLI-3 interface spec. This
5977 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04005978 * The mailbox command can be submitted in polling mode, in which case
5979 * this function will wait in a polling loop for the completion of the
5980 * mailbox.
5981 * If the mailbox is submitted in no_wait mode (not polling) the
5982 * function will submit the command and returns immediately without waiting
5983 * for the mailbox completion. The no_wait is supported only when HBA
5984 * is in SLI2/SLI3 mode - interrupts are enabled.
5985 * The SLI interface allows only one mailbox pending at a time. If the
5986 * mailbox is issued in polling mode and there is already a mailbox
5987 * pending, then the function will return an error. If the mailbox is issued
5988 * in NO_WAIT mode and there is a mailbox pending already, the function
5989 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5990 * The sli layer owns the mailbox object until the completion of mailbox
5991 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5992 * return codes the caller owns the mailbox command after the return of
5993 * the function.
5994 **/
James Smart3772a992009-05-22 14:50:54 -04005995static int
5996lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5997 uint32_t flag)
dea31012005-04-17 16:05:31 -05005998{
dea31012005-04-17 16:05:31 -05005999 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006000 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006001 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006002 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006003 int i;
James Smart09372822008-01-11 01:52:54 -05006004 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006005 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006006 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006007 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006008 int processing_queue = 0;
6009
6010 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6011 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006012 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006013 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006014 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6015 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6016 return MBX_SUCCESS;
6017 }
James Smart58da1ff2008-04-07 10:15:56 -04006018 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006019 pmbox = lpfc_mbox_get(phba);
6020 if (!pmbox) {
6021 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6022 return MBX_SUCCESS;
6023 }
6024 }
dea31012005-04-17 16:05:31 -05006025
James Smarted957682007-06-17 19:56:37 -05006026 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006027 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006028 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006029 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006030 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006031 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006032 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006033 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006034 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006035 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006036 }
6037 }
6038
Linas Vepstas8d63f372007-02-14 14:28:36 -06006039 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006040 if (unlikely(pci_channel_offline(phba->pcidev))) {
6041 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6042 goto out_not_finished;
6043 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006044
James Smarta257bf92009-04-06 18:48:10 -04006045 /* If HBA has a deferred error attention, fail the iocb. */
6046 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6047 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6048 goto out_not_finished;
6049 }
6050
dea31012005-04-17 16:05:31 -05006051 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006052
James Smart3772a992009-05-22 14:50:54 -04006053 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006054 status = MBX_SUCCESS;
6055
James Smart2e0fef82007-06-17 19:56:36 -05006056 if (phba->link_state == LPFC_HBA_ERROR) {
6057 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006058
6059 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006060 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6061 "(%d):0311 Mailbox command x%x cannot "
6062 "issue Data: x%x x%x\n",
6063 pmbox->vport ? pmbox->vport->vpi : 0,
6064 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006065 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006066 }
6067
James Smart9940b972011-03-11 16:06:12 -05006068 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6069 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6070 !(hc_copy & HC_MBINT_ENA)) {
6071 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6072 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006073 "(%d):2528 Mailbox command x%x cannot "
6074 "issue Data: x%x x%x\n",
6075 pmbox->vport ? pmbox->vport->vpi : 0,
6076 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006077 goto out_not_finished;
6078 }
James Smart92908312006-03-07 15:04:13 -05006079 }
6080
dea31012005-04-17 16:05:31 -05006081 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6082 /* Polling for a mbox command when another one is already active
6083 * is not allowed in SLI. Also, the driver must have established
6084 * SLI2 mode to queue and process multiple mbox commands.
6085 */
6086
6087 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006088 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006089
6090 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006091 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6092 "(%d):2529 Mailbox command x%x "
6093 "cannot issue Data: x%x x%x\n",
6094 pmbox->vport ? pmbox->vport->vpi : 0,
6095 pmbox->u.mb.mbxCommand,
6096 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006097 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006098 }
6099
James Smart3772a992009-05-22 14:50:54 -04006100 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006101 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006102 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006103 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6104 "(%d):2530 Mailbox command x%x "
6105 "cannot issue Data: x%x x%x\n",
6106 pmbox->vport ? pmbox->vport->vpi : 0,
6107 pmbox->u.mb.mbxCommand,
6108 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006109 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006110 }
6111
dea31012005-04-17 16:05:31 -05006112 /* Another mailbox command is still being processed, queue this
6113 * command to be processed later.
6114 */
6115 lpfc_mbox_put(phba, pmbox);
6116
6117 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006118 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006119 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006120 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006121 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6122 mb->mbxCommand, phba->pport->port_state,
6123 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006124
6125 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006126 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006127
James Smart858c9f62007-06-17 19:56:39 -05006128 if (pmbox->vport) {
6129 lpfc_debugfs_disc_trc(pmbox->vport,
6130 LPFC_DISC_TRC_MBOX_VPORT,
6131 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6132 (uint32_t)mb->mbxCommand,
6133 mb->un.varWords[0], mb->un.varWords[1]);
6134 }
6135 else {
6136 lpfc_debugfs_disc_trc(phba->pport,
6137 LPFC_DISC_TRC_MBOX,
6138 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6139 (uint32_t)mb->mbxCommand,
6140 mb->un.varWords[0], mb->un.varWords[1]);
6141 }
6142
James Smart2e0fef82007-06-17 19:56:36 -05006143 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006144 }
6145
dea31012005-04-17 16:05:31 -05006146 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6147
6148 /* If we are not polling, we MUST be in SLI2 mode */
6149 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006150 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006151 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006152 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006153 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006154 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6156 "(%d):2531 Mailbox command x%x "
6157 "cannot issue Data: x%x x%x\n",
6158 pmbox->vport ? pmbox->vport->vpi : 0,
6159 pmbox->u.mb.mbxCommand,
6160 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006161 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006162 }
6163 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006164 mod_timer(&psli->mbox_tmo, (jiffies +
6165 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05006166 }
6167
6168 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006169 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006170 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006171 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006172 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006173 mb->mbxCommand, phba->pport->port_state,
6174 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006175
James Smart858c9f62007-06-17 19:56:39 -05006176 if (mb->mbxCommand != MBX_HEARTBEAT) {
6177 if (pmbox->vport) {
6178 lpfc_debugfs_disc_trc(pmbox->vport,
6179 LPFC_DISC_TRC_MBOX_VPORT,
6180 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6181 (uint32_t)mb->mbxCommand,
6182 mb->un.varWords[0], mb->un.varWords[1]);
6183 }
6184 else {
6185 lpfc_debugfs_disc_trc(phba->pport,
6186 LPFC_DISC_TRC_MBOX,
6187 "MBOX Send: cmd:x%x mb:x%x x%x",
6188 (uint32_t)mb->mbxCommand,
6189 mb->un.varWords[0], mb->un.varWords[1]);
6190 }
6191 }
6192
dea31012005-04-17 16:05:31 -05006193 psli->slistat.mbox_cmd++;
6194 evtctr = psli->slistat.mbox_event;
6195
6196 /* next set own bit for the adapter and copy over command word */
6197 mb->mbxOwner = OWN_CHIP;
6198
James Smart3772a992009-05-22 14:50:54 -04006199 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006200 /* Populate mbox extension offset word. */
6201 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6202 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6203 = (uint8_t *)phba->mbox_ext
6204 - (uint8_t *)phba->mbox;
6205 }
6206
6207 /* Copy the mailbox extension data */
6208 if (pmbox->in_ext_byte_len && pmbox->context2) {
6209 lpfc_sli_pcimem_bcopy(pmbox->context2,
6210 (uint8_t *)phba->mbox_ext,
6211 pmbox->in_ext_byte_len);
6212 }
6213 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006214 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006215 } else {
James Smart7a470272010-03-15 11:25:20 -04006216 /* Populate mbox extension offset word. */
6217 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6218 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6219 = MAILBOX_HBA_EXT_OFFSET;
6220
6221 /* Copy the mailbox extension data */
6222 if (pmbox->in_ext_byte_len && pmbox->context2) {
6223 lpfc_memcpy_to_slim(phba->MBslimaddr +
6224 MAILBOX_HBA_EXT_OFFSET,
6225 pmbox->context2, pmbox->in_ext_byte_len);
6226
6227 }
James Smart92908312006-03-07 15:04:13 -05006228 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006229 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006230 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006231 }
6232
6233 /* First copy mbox command data to HBA SLIM, skip past first
6234 word */
6235 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6236 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6237 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6238
6239 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006240 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006241 to_slim = phba->MBslimaddr;
6242 writel(ldata, to_slim);
6243 readl(to_slim); /* flush */
6244
6245 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6246 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006247 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006248 }
6249 }
6250
6251 wmb();
dea31012005-04-17 16:05:31 -05006252
6253 switch (flag) {
6254 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006255 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006256 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006257 /* Interrupt board to do it */
6258 writel(CA_MBATT, phba->CAregaddr);
6259 readl(phba->CAregaddr); /* flush */
6260 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006261 break;
6262
6263 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006264 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006265 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006266 /* Interrupt board to do it */
6267 writel(CA_MBATT, phba->CAregaddr);
6268 readl(phba->CAregaddr); /* flush */
6269
James Smart3772a992009-05-22 14:50:54 -04006270 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006271 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006272 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006273 word0 = le32_to_cpu(word0);
6274 } else {
6275 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006276 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6277 spin_unlock_irqrestore(&phba->hbalock,
6278 drvr_flag);
6279 goto out_not_finished;
6280 }
dea31012005-04-17 16:05:31 -05006281 }
6282
6283 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006284 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6285 spin_unlock_irqrestore(&phba->hbalock,
6286 drvr_flag);
6287 goto out_not_finished;
6288 }
James Smart09372822008-01-11 01:52:54 -05006289 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6290 mb->mbxCommand) *
6291 1000) + jiffies;
6292 i = 0;
dea31012005-04-17 16:05:31 -05006293 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006294 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6295 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006296 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006297 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006298 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006299 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006300 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006301 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006302 }
6303
6304 /* Check if we took a mbox interrupt while we were
6305 polling */
6306 if (((word0 & OWN_CHIP) != OWN_CHIP)
6307 && (evtctr != psli->slistat.mbox_event))
6308 break;
6309
James Smart09372822008-01-11 01:52:54 -05006310 if (i++ > 10) {
6311 spin_unlock_irqrestore(&phba->hbalock,
6312 drvr_flag);
6313 msleep(1);
6314 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6315 }
dea31012005-04-17 16:05:31 -05006316
James Smart3772a992009-05-22 14:50:54 -04006317 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006318 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006319 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006320 word0 = le32_to_cpu(word0);
6321 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6322 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006323 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006324 /* Check real SLIM for any errors */
6325 slimword0 = readl(phba->MBslimaddr);
6326 slimmb = (MAILBOX_t *) & slimword0;
6327 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6328 && slimmb->mbxStatus) {
6329 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006330 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006331 word0 = slimword0;
6332 }
6333 }
6334 } else {
6335 /* First copy command data */
6336 word0 = readl(phba->MBslimaddr);
6337 }
6338 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006339 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6340 spin_unlock_irqrestore(&phba->hbalock,
6341 drvr_flag);
6342 goto out_not_finished;
6343 }
dea31012005-04-17 16:05:31 -05006344 }
6345
James Smart3772a992009-05-22 14:50:54 -04006346 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006347 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006348 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006349 /* Copy the mailbox extension data */
6350 if (pmbox->out_ext_byte_len && pmbox->context2) {
6351 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6352 pmbox->context2,
6353 pmbox->out_ext_byte_len);
6354 }
dea31012005-04-17 16:05:31 -05006355 } else {
6356 /* First copy command data */
6357 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6358 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006359 /* Copy the mailbox extension data */
6360 if (pmbox->out_ext_byte_len && pmbox->context2) {
6361 lpfc_memcpy_from_slim(pmbox->context2,
6362 phba->MBslimaddr +
6363 MAILBOX_HBA_EXT_OFFSET,
6364 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006365 }
6366 }
6367
6368 writel(HA_MBATT, phba->HAregaddr);
6369 readl(phba->HAregaddr); /* flush */
6370
6371 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6372 status = mb->mbxStatus;
6373 }
6374
James Smart2e0fef82007-06-17 19:56:36 -05006375 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6376 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006377
6378out_not_finished:
6379 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006380 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006381 lpfc_mbox_cmpl_put(phba, pmbox);
6382 }
6383 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006384}
6385
James Smarte59058c2008-08-24 21:49:00 -04006386/**
James Smartf1126682009-06-10 17:22:44 -04006387 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6388 * @phba: Pointer to HBA context object.
6389 *
6390 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6391 * the driver internal pending mailbox queue. It will then try to wait out the
6392 * possible outstanding mailbox command before return.
6393 *
6394 * Returns:
6395 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6396 * the outstanding mailbox command timed out.
6397 **/
6398static int
6399lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6400{
6401 struct lpfc_sli *psli = &phba->sli;
6402 uint8_t actcmd = MBX_HEARTBEAT;
6403 int rc = 0;
6404 unsigned long timeout;
6405
6406 /* Mark the asynchronous mailbox command posting as blocked */
6407 spin_lock_irq(&phba->hbalock);
6408 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6409 if (phba->sli.mbox_active)
6410 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
6411 spin_unlock_irq(&phba->hbalock);
6412 /* Determine how long we might wait for the active mailbox
6413 * command to be gracefully completed by firmware.
6414 */
6415 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
6416 jiffies;
6417 /* Wait for the outstnading mailbox command to complete */
6418 while (phba->sli.mbox_active) {
6419 /* Check active mailbox complete status every 2ms */
6420 msleep(2);
6421 if (time_after(jiffies, timeout)) {
6422 /* Timeout, marked the outstanding cmd not complete */
6423 rc = 1;
6424 break;
6425 }
6426 }
6427
6428 /* Can not cleanly block async mailbox command, fails it */
6429 if (rc) {
6430 spin_lock_irq(&phba->hbalock);
6431 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6432 spin_unlock_irq(&phba->hbalock);
6433 }
6434 return rc;
6435}
6436
6437/**
6438 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6439 * @phba: Pointer to HBA context object.
6440 *
6441 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6442 * commands from the driver internal pending mailbox queue. It makes sure
6443 * that there is no outstanding mailbox command before resuming posting
6444 * asynchronous mailbox commands. If, for any reason, there is outstanding
6445 * mailbox command, it will try to wait it out before resuming asynchronous
6446 * mailbox command posting.
6447 **/
6448static void
6449lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6450{
6451 struct lpfc_sli *psli = &phba->sli;
6452
6453 spin_lock_irq(&phba->hbalock);
6454 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6455 /* Asynchronous mailbox posting is not blocked, do nothing */
6456 spin_unlock_irq(&phba->hbalock);
6457 return;
6458 }
6459
6460 /* Outstanding synchronous mailbox command is guaranteed to be done,
6461 * successful or timeout, after timing-out the outstanding mailbox
6462 * command shall always be removed, so just unblock posting async
6463 * mailbox command and resume
6464 */
6465 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6466 spin_unlock_irq(&phba->hbalock);
6467
6468 /* wake up worker thread to post asynchronlous mailbox command */
6469 lpfc_worker_wake_up(phba);
6470}
6471
6472/**
James Smartda0436e2009-05-22 14:51:39 -04006473 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6474 * @phba: Pointer to HBA context object.
6475 * @mboxq: Pointer to mailbox object.
6476 *
6477 * The function posts a mailbox to the port. The mailbox is expected
6478 * to be comletely filled in and ready for the port to operate on it.
6479 * This routine executes a synchronous completion operation on the
6480 * mailbox by polling for its completion.
6481 *
6482 * The caller must not be holding any locks when calling this routine.
6483 *
6484 * Returns:
6485 * MBX_SUCCESS - mailbox posted successfully
6486 * Any of the MBX error values.
6487 **/
6488static int
6489lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6490{
6491 int rc = MBX_SUCCESS;
6492 unsigned long iflag;
6493 uint32_t db_ready;
6494 uint32_t mcqe_status;
6495 uint32_t mbx_cmnd;
6496 unsigned long timeout;
6497 struct lpfc_sli *psli = &phba->sli;
6498 struct lpfc_mqe *mb = &mboxq->u.mqe;
6499 struct lpfc_bmbx_create *mbox_rgn;
6500 struct dma_address *dma_address;
6501 struct lpfc_register bmbx_reg;
6502
6503 /*
6504 * Only one mailbox can be active to the bootstrap mailbox region
6505 * at a time and there is no queueing provided.
6506 */
6507 spin_lock_irqsave(&phba->hbalock, iflag);
6508 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6509 spin_unlock_irqrestore(&phba->hbalock, iflag);
6510 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6511 "(%d):2532 Mailbox command x%x (x%x) "
6512 "cannot issue Data: x%x x%x\n",
6513 mboxq->vport ? mboxq->vport->vpi : 0,
6514 mboxq->u.mb.mbxCommand,
6515 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6516 psli->sli_flag, MBX_POLL);
6517 return MBXERR_ERROR;
6518 }
6519 /* The server grabs the token and owns it until release */
6520 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6521 phba->sli.mbox_active = mboxq;
6522 spin_unlock_irqrestore(&phba->hbalock, iflag);
6523
6524 /*
6525 * Initialize the bootstrap memory region to avoid stale data areas
6526 * in the mailbox post. Then copy the caller's mailbox contents to
6527 * the bmbx mailbox region.
6528 */
6529 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6530 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6531 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6532 sizeof(struct lpfc_mqe));
6533
6534 /* Post the high mailbox dma address to the port and wait for ready. */
6535 dma_address = &phba->sli4_hba.bmbx.dma_address;
6536 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6537
6538 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6539 * 1000) + jiffies;
6540 do {
6541 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6542 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6543 if (!db_ready)
6544 msleep(2);
6545
6546 if (time_after(jiffies, timeout)) {
6547 rc = MBXERR_ERROR;
6548 goto exit;
6549 }
6550 } while (!db_ready);
6551
6552 /* Post the low mailbox dma address to the port. */
6553 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
6554 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6555 * 1000) + jiffies;
6556 do {
6557 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6558 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6559 if (!db_ready)
6560 msleep(2);
6561
6562 if (time_after(jiffies, timeout)) {
6563 rc = MBXERR_ERROR;
6564 goto exit;
6565 }
6566 } while (!db_ready);
6567
6568 /*
6569 * Read the CQ to ensure the mailbox has completed.
6570 * If so, update the mailbox status so that the upper layers
6571 * can complete the request normally.
6572 */
6573 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6574 sizeof(struct lpfc_mqe));
6575 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6576 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6577 sizeof(struct lpfc_mcqe));
6578 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006579 /*
6580 * When the CQE status indicates a failure and the mailbox status
6581 * indicates success then copy the CQE status into the mailbox status
6582 * (and prefix it with x4000).
6583 */
James Smartda0436e2009-05-22 14:51:39 -04006584 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006585 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6586 bf_set(lpfc_mqe_status, mb,
6587 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04006588 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04006589 } else
6590 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04006591
6592 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6593 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
6594 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6595 " x%x x%x CQ: x%x x%x x%x x%x\n",
6596 mboxq->vport ? mboxq->vport->vpi : 0,
6597 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
6598 bf_get(lpfc_mqe_status, mb),
6599 mb->un.mb_words[0], mb->un.mb_words[1],
6600 mb->un.mb_words[2], mb->un.mb_words[3],
6601 mb->un.mb_words[4], mb->un.mb_words[5],
6602 mb->un.mb_words[6], mb->un.mb_words[7],
6603 mb->un.mb_words[8], mb->un.mb_words[9],
6604 mb->un.mb_words[10], mb->un.mb_words[11],
6605 mb->un.mb_words[12], mboxq->mcqe.word0,
6606 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6607 mboxq->mcqe.trailer);
6608exit:
6609 /* We are holding the token, no needed for lock when release */
6610 spin_lock_irqsave(&phba->hbalock, iflag);
6611 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6612 phba->sli.mbox_active = NULL;
6613 spin_unlock_irqrestore(&phba->hbalock, iflag);
6614 return rc;
6615}
6616
6617/**
6618 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6619 * @phba: Pointer to HBA context object.
6620 * @pmbox: Pointer to mailbox object.
6621 * @flag: Flag indicating how the mailbox need to be processed.
6622 *
6623 * This function is called by discovery code and HBA management code to submit
6624 * a mailbox command to firmware with SLI-4 interface spec.
6625 *
6626 * Return codes the caller owns the mailbox command after the return of the
6627 * function.
6628 **/
6629static int
6630lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6631 uint32_t flag)
6632{
6633 struct lpfc_sli *psli = &phba->sli;
6634 unsigned long iflags;
6635 int rc;
6636
James Smart8fa38512009-07-19 10:01:03 -04006637 rc = lpfc_mbox_dev_check(phba);
6638 if (unlikely(rc)) {
6639 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6640 "(%d):2544 Mailbox command x%x (x%x) "
6641 "cannot issue Data: x%x x%x\n",
6642 mboxq->vport ? mboxq->vport->vpi : 0,
6643 mboxq->u.mb.mbxCommand,
6644 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6645 psli->sli_flag, flag);
6646 goto out_not_finished;
6647 }
6648
James Smartda0436e2009-05-22 14:51:39 -04006649 /* Detect polling mode and jump to a handler */
6650 if (!phba->sli4_hba.intr_enable) {
6651 if (flag == MBX_POLL)
6652 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6653 else
6654 rc = -EIO;
6655 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04006656 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04006657 "(%d):2541 Mailbox command x%x "
6658 "(x%x) cannot issue Data: x%x x%x\n",
6659 mboxq->vport ? mboxq->vport->vpi : 0,
6660 mboxq->u.mb.mbxCommand,
6661 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6662 psli->sli_flag, flag);
6663 return rc;
6664 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04006665 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6666 "(%d):2542 Try to issue mailbox command "
6667 "x%x (x%x) synchronously ahead of async"
6668 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04006669 mboxq->vport ? mboxq->vport->vpi : 0,
6670 mboxq->u.mb.mbxCommand,
6671 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6672 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04006673 /* Try to block the asynchronous mailbox posting */
6674 rc = lpfc_sli4_async_mbox_block(phba);
6675 if (!rc) {
6676 /* Successfully blocked, now issue sync mbox cmd */
6677 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6678 if (rc != MBX_SUCCESS)
6679 lpfc_printf_log(phba, KERN_ERR,
6680 LOG_MBOX | LOG_SLI,
6681 "(%d):2597 Mailbox command "
6682 "x%x (x%x) cannot issue "
6683 "Data: x%x x%x\n",
6684 mboxq->vport ?
6685 mboxq->vport->vpi : 0,
6686 mboxq->u.mb.mbxCommand,
6687 lpfc_sli4_mbox_opcode_get(phba,
6688 mboxq),
6689 psli->sli_flag, flag);
6690 /* Unblock the async mailbox posting afterward */
6691 lpfc_sli4_async_mbox_unblock(phba);
6692 }
6693 return rc;
James Smartda0436e2009-05-22 14:51:39 -04006694 }
6695
6696 /* Now, interrupt mode asynchrous mailbox command */
6697 rc = lpfc_mbox_cmd_check(phba, mboxq);
6698 if (rc) {
6699 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6700 "(%d):2543 Mailbox command x%x (x%x) "
6701 "cannot issue Data: x%x x%x\n",
6702 mboxq->vport ? mboxq->vport->vpi : 0,
6703 mboxq->u.mb.mbxCommand,
6704 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6705 psli->sli_flag, flag);
6706 goto out_not_finished;
6707 }
James Smartda0436e2009-05-22 14:51:39 -04006708
6709 /* Put the mailbox command to the driver internal FIFO */
6710 psli->slistat.mbox_busy++;
6711 spin_lock_irqsave(&phba->hbalock, iflags);
6712 lpfc_mbox_put(phba, mboxq);
6713 spin_unlock_irqrestore(&phba->hbalock, iflags);
6714 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6715 "(%d):0354 Mbox cmd issue - Enqueue Data: "
6716 "x%x (x%x) x%x x%x x%x\n",
6717 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
6718 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6719 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6720 phba->pport->port_state,
6721 psli->sli_flag, MBX_NOWAIT);
6722 /* Wake up worker thread to transport mailbox command from head */
6723 lpfc_worker_wake_up(phba);
6724
6725 return MBX_BUSY;
6726
6727out_not_finished:
6728 return MBX_NOT_FINISHED;
6729}
6730
6731/**
6732 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
6733 * @phba: Pointer to HBA context object.
6734 *
6735 * This function is called by worker thread to send a mailbox command to
6736 * SLI4 HBA firmware.
6737 *
6738 **/
6739int
6740lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
6741{
6742 struct lpfc_sli *psli = &phba->sli;
6743 LPFC_MBOXQ_t *mboxq;
6744 int rc = MBX_SUCCESS;
6745 unsigned long iflags;
6746 struct lpfc_mqe *mqe;
6747 uint32_t mbx_cmnd;
6748
6749 /* Check interrupt mode before post async mailbox command */
6750 if (unlikely(!phba->sli4_hba.intr_enable))
6751 return MBX_NOT_FINISHED;
6752
6753 /* Check for mailbox command service token */
6754 spin_lock_irqsave(&phba->hbalock, iflags);
6755 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6756 spin_unlock_irqrestore(&phba->hbalock, iflags);
6757 return MBX_NOT_FINISHED;
6758 }
6759 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6760 spin_unlock_irqrestore(&phba->hbalock, iflags);
6761 return MBX_NOT_FINISHED;
6762 }
6763 if (unlikely(phba->sli.mbox_active)) {
6764 spin_unlock_irqrestore(&phba->hbalock, iflags);
6765 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6766 "0384 There is pending active mailbox cmd\n");
6767 return MBX_NOT_FINISHED;
6768 }
6769 /* Take the mailbox command service token */
6770 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6771
6772 /* Get the next mailbox command from head of queue */
6773 mboxq = lpfc_mbox_get(phba);
6774
6775 /* If no more mailbox command waiting for post, we're done */
6776 if (!mboxq) {
6777 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6778 spin_unlock_irqrestore(&phba->hbalock, iflags);
6779 return MBX_SUCCESS;
6780 }
6781 phba->sli.mbox_active = mboxq;
6782 spin_unlock_irqrestore(&phba->hbalock, iflags);
6783
6784 /* Check device readiness for posting mailbox command */
6785 rc = lpfc_mbox_dev_check(phba);
6786 if (unlikely(rc))
6787 /* Driver clean routine will clean up pending mailbox */
6788 goto out_not_finished;
6789
6790 /* Prepare the mbox command to be posted */
6791 mqe = &mboxq->u.mqe;
6792 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
6793
6794 /* Start timer for the mbox_tmo and log some mailbox post messages */
6795 mod_timer(&psli->mbox_tmo, (jiffies +
6796 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
6797
6798 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6799 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
6800 "x%x x%x\n",
6801 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6802 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6803 phba->pport->port_state, psli->sli_flag);
6804
6805 if (mbx_cmnd != MBX_HEARTBEAT) {
6806 if (mboxq->vport) {
6807 lpfc_debugfs_disc_trc(mboxq->vport,
6808 LPFC_DISC_TRC_MBOX_VPORT,
6809 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6810 mbx_cmnd, mqe->un.mb_words[0],
6811 mqe->un.mb_words[1]);
6812 } else {
6813 lpfc_debugfs_disc_trc(phba->pport,
6814 LPFC_DISC_TRC_MBOX,
6815 "MBOX Send: cmd:x%x mb:x%x x%x",
6816 mbx_cmnd, mqe->un.mb_words[0],
6817 mqe->un.mb_words[1]);
6818 }
6819 }
6820 psli->slistat.mbox_cmd++;
6821
6822 /* Post the mailbox command to the port */
6823 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
6824 if (rc != MBX_SUCCESS) {
6825 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6826 "(%d):2533 Mailbox command x%x (x%x) "
6827 "cannot issue Data: x%x x%x\n",
6828 mboxq->vport ? mboxq->vport->vpi : 0,
6829 mboxq->u.mb.mbxCommand,
6830 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6831 psli->sli_flag, MBX_NOWAIT);
6832 goto out_not_finished;
6833 }
6834
6835 return rc;
6836
6837out_not_finished:
6838 spin_lock_irqsave(&phba->hbalock, iflags);
6839 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6840 __lpfc_mbox_cmpl_put(phba, mboxq);
6841 /* Release the token */
6842 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6843 phba->sli.mbox_active = NULL;
6844 spin_unlock_irqrestore(&phba->hbalock, iflags);
6845
6846 return MBX_NOT_FINISHED;
6847}
6848
6849/**
6850 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
6851 * @phba: Pointer to HBA context object.
6852 * @pmbox: Pointer to mailbox object.
6853 * @flag: Flag indicating how the mailbox need to be processed.
6854 *
6855 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
6856 * the API jump table function pointer from the lpfc_hba struct.
6857 *
6858 * Return codes the caller owns the mailbox command after the return of the
6859 * function.
6860 **/
6861int
6862lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
6863{
6864 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
6865}
6866
6867/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006868 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04006869 * @phba: The hba struct for which this call is being executed.
6870 * @dev_grp: The HBA PCI-Device group number.
6871 *
6872 * This routine sets up the mbox interface API function jump table in @phba
6873 * struct.
6874 * Returns: 0 - success, -ENODEV - failure.
6875 **/
6876int
6877lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6878{
6879
6880 switch (dev_grp) {
6881 case LPFC_PCI_DEV_LP:
6882 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6883 phba->lpfc_sli_handle_slow_ring_event =
6884 lpfc_sli_handle_slow_ring_event_s3;
6885 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6886 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6887 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6888 break;
6889 case LPFC_PCI_DEV_OC:
6890 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6891 phba->lpfc_sli_handle_slow_ring_event =
6892 lpfc_sli_handle_slow_ring_event_s4;
6893 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6894 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6895 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6896 break;
6897 default:
6898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6899 "1420 Invalid HBA PCI-device group: 0x%x\n",
6900 dev_grp);
6901 return -ENODEV;
6902 break;
6903 }
6904 return 0;
6905}
6906
6907/**
James Smart3621a712009-04-06 18:47:14 -04006908 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04006909 * @phba: Pointer to HBA context object.
6910 * @pring: Pointer to driver SLI ring object.
6911 * @piocb: Pointer to address of newly added command iocb.
6912 *
6913 * This function is called with hbalock held to add a command
6914 * iocb to the txq when SLI layer cannot submit the command iocb
6915 * to the ring.
6916 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006917void
James Smart92d7f7b2007-06-17 19:56:38 -05006918__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006919 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05006920{
6921 /* Insert the caller's iocb in the txq tail for later processing. */
6922 list_add_tail(&piocb->list, &pring->txq);
6923 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05006924}
6925
James Smarte59058c2008-08-24 21:49:00 -04006926/**
James Smart3621a712009-04-06 18:47:14 -04006927 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04006928 * @phba: Pointer to HBA context object.
6929 * @pring: Pointer to driver SLI ring object.
6930 * @piocb: Pointer to address of newly added command iocb.
6931 *
6932 * This function is called with hbalock held before a new
6933 * iocb is submitted to the firmware. This function checks
6934 * txq to flush the iocbs in txq to Firmware before
6935 * submitting new iocbs to the Firmware.
6936 * If there are iocbs in the txq which need to be submitted
6937 * to firmware, lpfc_sli_next_iocb returns the first element
6938 * of the txq after dequeuing it from txq.
6939 * If there is no iocb in the txq then the function will return
6940 * *piocb and *piocb is set to NULL. Caller needs to check
6941 * *piocb to find if there are more commands in the txq.
6942 **/
dea31012005-04-17 16:05:31 -05006943static struct lpfc_iocbq *
6944lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006945 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05006946{
6947 struct lpfc_iocbq * nextiocb;
6948
6949 nextiocb = lpfc_sli_ringtx_get(phba, pring);
6950 if (!nextiocb) {
6951 nextiocb = *piocb;
6952 *piocb = NULL;
6953 }
6954
6955 return nextiocb;
6956}
6957
James Smarte59058c2008-08-24 21:49:00 -04006958/**
James Smart3772a992009-05-22 14:50:54 -04006959 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006960 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04006961 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04006962 * @piocb: Pointer to command iocb.
6963 * @flag: Flag indicating if this command can be put into txq.
6964 *
James Smart3772a992009-05-22 14:50:54 -04006965 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6966 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6967 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6968 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6969 * this function allows only iocbs for posting buffers. This function finds
6970 * next available slot in the command ring and posts the command to the
6971 * available slot and writes the port attention register to request HBA start
6972 * processing new iocb. If there is no slot available in the ring and
6973 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6974 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04006975 *
James Smart3772a992009-05-22 14:50:54 -04006976 * This function is called with hbalock held. The function will return success
6977 * after it successfully submit the iocb to firmware or after adding to the
6978 * txq.
James Smarte59058c2008-08-24 21:49:00 -04006979 **/
James Smart98c9ea52007-10-27 13:37:33 -04006980static int
James Smart3772a992009-05-22 14:50:54 -04006981__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05006982 struct lpfc_iocbq *piocb, uint32_t flag)
6983{
6984 struct lpfc_iocbq *nextiocb;
6985 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04006986 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05006987
James Smart92d7f7b2007-06-17 19:56:38 -05006988 if (piocb->iocb_cmpl && (!piocb->vport) &&
6989 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6990 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6991 lpfc_printf_log(phba, KERN_ERR,
6992 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006993 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006994 piocb->iocb.ulpCommand);
6995 dump_stack();
6996 return IOCB_ERROR;
6997 }
6998
6999
Linas Vepstas8d63f372007-02-14 14:28:36 -06007000 /* If the PCI channel is in offline state, do not post iocbs. */
7001 if (unlikely(pci_channel_offline(phba->pcidev)))
7002 return IOCB_ERROR;
7003
James Smarta257bf92009-04-06 18:48:10 -04007004 /* If HBA has a deferred error attention, fail the iocb. */
7005 if (unlikely(phba->hba_flag & DEFER_ERATT))
7006 return IOCB_ERROR;
7007
dea31012005-04-17 16:05:31 -05007008 /*
7009 * We should never get an IOCB if we are in a < LINK_DOWN state
7010 */
James Smart2e0fef82007-06-17 19:56:36 -05007011 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007012 return IOCB_ERROR;
7013
7014 /*
7015 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007016 * outstanding event.
dea31012005-04-17 16:05:31 -05007017 */
James Smart0b727fe2007-10-27 13:37:25 -04007018 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007019 goto iocb_busy;
7020
James Smart2e0fef82007-06-17 19:56:36 -05007021 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007022 /*
James Smart2680eea2007-04-25 09:52:55 -04007023 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007024 * can be issued if the link is not up.
7025 */
7026 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007027 case CMD_GEN_REQUEST64_CR:
7028 case CMD_GEN_REQUEST64_CX:
7029 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7030 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007031 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007032 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7033 MENLO_TRANSPORT_TYPE))
7034
7035 goto iocb_busy;
7036 break;
dea31012005-04-17 16:05:31 -05007037 case CMD_QUE_RING_BUF_CN:
7038 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007039 /*
7040 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7041 * completion, iocb_cmpl MUST be 0.
7042 */
7043 if (piocb->iocb_cmpl)
7044 piocb->iocb_cmpl = NULL;
7045 /*FALLTHROUGH*/
7046 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007047 case CMD_CLOSE_XRI_CN:
7048 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007049 break;
7050 default:
7051 goto iocb_busy;
7052 }
7053
7054 /*
7055 * For FCP commands, we must be in a state where we can process link
7056 * attention events.
7057 */
7058 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007059 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007060 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007061 }
dea31012005-04-17 16:05:31 -05007062
dea31012005-04-17 16:05:31 -05007063 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7064 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7065 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7066
7067 if (iocb)
7068 lpfc_sli_update_ring(phba, pring);
7069 else
7070 lpfc_sli_update_full_ring(phba, pring);
7071
7072 if (!piocb)
7073 return IOCB_SUCCESS;
7074
7075 goto out_busy;
7076
7077 iocb_busy:
7078 pring->stats.iocb_cmd_delay++;
7079
7080 out_busy:
7081
7082 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007083 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007084 return IOCB_SUCCESS;
7085 }
7086
7087 return IOCB_BUSY;
7088}
7089
James Smart3772a992009-05-22 14:50:54 -04007090/**
James Smart4f774512009-05-22 14:52:35 -04007091 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7092 * @phba: Pointer to HBA context object.
7093 * @piocb: Pointer to command iocb.
7094 * @sglq: Pointer to the scatter gather queue object.
7095 *
7096 * This routine converts the bpl or bde that is in the IOCB
7097 * to a sgl list for the sli4 hardware. The physical address
7098 * of the bpl/bde is converted back to a virtual address.
7099 * If the IOCB contains a BPL then the list of BDE's is
7100 * converted to sli4_sge's. If the IOCB contains a single
7101 * BDE then it is converted to a single sli_sge.
7102 * The IOCB is still in cpu endianess so the contents of
7103 * the bpl can be used without byte swapping.
7104 *
7105 * Returns valid XRI = Success, NO_XRI = Failure.
7106**/
7107static uint16_t
7108lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7109 struct lpfc_sglq *sglq)
7110{
7111 uint16_t xritag = NO_XRI;
7112 struct ulp_bde64 *bpl = NULL;
7113 struct ulp_bde64 bde;
7114 struct sli4_sge *sgl = NULL;
7115 IOCB_t *icmd;
7116 int numBdes = 0;
7117 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007118 uint32_t offset = 0; /* accumulated offset in the sg request list */
7119 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007120
7121 if (!piocbq || !sglq)
7122 return xritag;
7123
7124 sgl = (struct sli4_sge *)sglq->sgl;
7125 icmd = &piocbq->iocb;
7126 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7127 numBdes = icmd->un.genreq64.bdl.bdeSize /
7128 sizeof(struct ulp_bde64);
7129 /* The addrHigh and addrLow fields within the IOCB
7130 * have not been byteswapped yet so there is no
7131 * need to swap them back.
7132 */
7133 bpl = (struct ulp_bde64 *)
7134 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7135
7136 if (!bpl)
7137 return xritag;
7138
7139 for (i = 0; i < numBdes; i++) {
7140 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007141 sgl->addr_hi = bpl->addrHigh;
7142 sgl->addr_lo = bpl->addrLow;
7143
James Smart05580562011-05-24 11:40:48 -04007144 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007145 if ((i+1) == numBdes)
7146 bf_set(lpfc_sli4_sge_last, sgl, 1);
7147 else
7148 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007149 /* swap the size field back to the cpu so we
7150 * can assign it to the sgl.
7151 */
7152 bde.tus.w = le32_to_cpu(bpl->tus.w);
7153 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007154 /* The offsets in the sgl need to be accumulated
7155 * separately for the request and reply lists.
7156 * The request is always first, the reply follows.
7157 */
7158 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7159 /* add up the reply sg entries */
7160 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7161 inbound++;
7162 /* first inbound? reset the offset */
7163 if (inbound == 1)
7164 offset = 0;
7165 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7166 offset += bde.tus.f.bdeSize;
7167 }
James Smart546fc852011-03-11 16:06:29 -05007168 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007169 bpl++;
7170 sgl++;
7171 }
7172 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7173 /* The addrHigh and addrLow fields of the BDE have not
7174 * been byteswapped yet so they need to be swapped
7175 * before putting them in the sgl.
7176 */
7177 sgl->addr_hi =
7178 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7179 sgl->addr_lo =
7180 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007181 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007182 bf_set(lpfc_sli4_sge_last, sgl, 1);
7183 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007184 sgl->sge_len =
7185 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007186 }
7187 return sglq->sli4_xritag;
7188}
7189
7190/**
7191 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7192 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007193 *
James Smarta93ff372010-10-22 11:06:08 -04007194 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007195 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7196 * held.
James Smart4f774512009-05-22 14:52:35 -04007197 *
7198 * Return: index into SLI4 fast-path FCP queue index.
7199 **/
7200static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007201lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007202{
James Smart8fa38512009-07-19 10:01:03 -04007203 ++phba->fcp_qidx;
7204 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7205 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007206
James Smart8fa38512009-07-19 10:01:03 -04007207 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007208}
7209
7210/**
7211 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7212 * @phba: Pointer to HBA context object.
7213 * @piocb: Pointer to command iocb.
7214 * @wqe: Pointer to the work queue entry.
7215 *
7216 * This routine converts the iocb command to its Work Queue Entry
7217 * equivalent. The wqe pointer should not have any fields set when
7218 * this routine is called because it will memcpy over them.
7219 * This routine does not set the CQ_ID or the WQEC bits in the
7220 * wqe.
7221 *
7222 * Returns: 0 = Success, IOCB_ERROR = Failure.
7223 **/
7224static int
7225lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7226 union lpfc_wqe *wqe)
7227{
James Smart5ffc2662009-11-18 15:39:44 -05007228 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007229 uint8_t ct = 0;
7230 uint32_t fip;
7231 uint32_t abort_tag;
7232 uint8_t command_type = ELS_COMMAND_NON_FIP;
7233 uint8_t cmnd;
7234 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007235 uint16_t abrt_iotag;
7236 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007237 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007238 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007239 int numBdes, i;
7240 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007241 struct lpfc_nodelist *ndlp;
James Smart4f774512009-05-22 14:52:35 -04007242
James Smart45ed1192009-10-02 15:17:02 -04007243 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007244 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007245 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007246 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007247 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007248 command_type = ELS_COMMAND_FIP;
7249 else
7250 command_type = ELS_COMMAND_NON_FIP;
7251
James Smart4f774512009-05-22 14:52:35 -04007252 /* Some of the fields are in the right position already */
7253 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7254 abort_tag = (uint32_t) iocbq->iotag;
7255 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007256 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007257 /* words0-2 bpl convert bde */
7258 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007259 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7260 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007261 bpl = (struct ulp_bde64 *)
7262 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7263 if (!bpl)
7264 return IOCB_ERROR;
7265
7266 /* Should already be byte swapped. */
7267 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7268 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7269 /* swap the size field back to the cpu so we
7270 * can assign it to the sgl.
7271 */
7272 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007273 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7274 total_len = 0;
7275 for (i = 0; i < numBdes; i++) {
7276 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7277 total_len += bde.tus.f.bdeSize;
7278 }
James Smart4f774512009-05-22 14:52:35 -04007279 } else
James Smart5ffc2662009-11-18 15:39:44 -05007280 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007281
7282 iocbq->iocb.ulpIoTag = iocbq->iotag;
7283 cmnd = iocbq->iocb.ulpCommand;
7284
7285 switch (iocbq->iocb.ulpCommand) {
7286 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007287 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007288 if (!iocbq->iocb.ulpLe) {
7289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7290 "2007 Only Limited Edition cmd Format"
7291 " supported 0x%x\n",
7292 iocbq->iocb.ulpCommand);
7293 return IOCB_ERROR;
7294 }
James Smart5ffc2662009-11-18 15:39:44 -05007295 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007296 /* Els_reguest64 has a TMO */
7297 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7298 iocbq->iocb.ulpTimeout);
7299 /* Need a VF for word 4 set the vf bit*/
7300 bf_set(els_req64_vf, &wqe->els_req, 0);
7301 /* And a VFID for word 12 */
7302 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007303 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007304 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7305 iocbq->iocb.ulpContext);
7306 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7307 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007308 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05007309 if (command_type == ELS_COMMAND_FIP) {
7310 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7311 >> LPFC_FIP_ELS_ID_SHIFT);
7312 }
James Smart6d368e52011-05-24 11:44:12 -04007313 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7314 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007315 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7316 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7317 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7318 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7319 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7320 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007321 break;
James Smart5ffc2662009-11-18 15:39:44 -05007322 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007323 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7324 iocbq->iocb.un.ulpWord[3]);
7325 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7326 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05007327 /* The entire sequence is transmitted for this IOCB */
7328 xmit_len = total_len;
7329 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007330 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007331 /* word3 iocb=io_tag32 wqe=reserved */
7332 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007333 /* word4 relative_offset memcpy */
7334 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007335 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7336 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7337 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7338 LPFC_WQE_IOD_WRITE);
7339 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7340 LPFC_WQE_LENLOC_WORD12);
7341 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007342 wqe->xmit_sequence.xmit_len = xmit_len;
7343 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04007344 break;
7345 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007346 /* word3 iocb=iotag32 wqe=seq_payload_len */
7347 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007348 /* word4 iocb=rsvd wqe=rsvd */
7349 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7350 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007351 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007352 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007353 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7354 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7355 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7356 LPFC_WQE_LENLOC_WORD3);
7357 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007358 break;
7359 case CMD_FCP_IWRITE64_CR:
7360 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007361 /* word3 iocb=iotag wqe=payload_offset_len */
7362 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7363 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007364 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007365 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7366 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7367 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7368 iocbq->iocb.ulpFCP2Rcvy);
7369 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7370 /* Always open the exchange */
7371 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7372 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7373 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7374 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7375 LPFC_WQE_LENLOC_WORD4);
7376 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7377 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7378 break;
7379 case CMD_FCP_IREAD64_CR:
7380 /* word3 iocb=iotag wqe=payload_offset_len */
7381 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7382 wqe->fcp_iread.payload_offset_len =
7383 xmit_len + sizeof(struct fcp_rsp);
7384 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7385 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7386 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7387 iocbq->iocb.ulpFCP2Rcvy);
7388 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007389 /* Always open the exchange */
7390 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007391 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7392 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7393 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7394 LPFC_WQE_LENLOC_WORD4);
7395 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7396 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7397 break;
James Smartf1126682009-06-10 17:22:44 -04007398 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007399 /* word3 iocb=IO_TAG wqe=reserved */
7400 wqe->fcp_icmd.rsrvd3 = 0;
7401 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007402 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007403 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7404 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7405 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7406 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7407 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7408 LPFC_WQE_LENLOC_NONE);
7409 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007410 break;
7411 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007412 /* For this command calculate the xmit length of the
7413 * request bde.
7414 */
7415 xmit_len = 0;
7416 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7417 sizeof(struct ulp_bde64);
7418 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007419 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007420 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7421 break;
James Smart63e801c2010-11-20 23:14:19 -05007422 xmit_len += bde.tus.f.bdeSize;
7423 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007424 /* word3 iocb=IO_TAG wqe=request_payload_len */
7425 wqe->gen_req.request_payload_len = xmit_len;
7426 /* word4 iocb=parameter wqe=relative_offset memcpy */
7427 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007428 /* word6 context tag copied in memcpy */
7429 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7430 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7431 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7432 "2015 Invalid CT %x command 0x%x\n",
7433 ct, iocbq->iocb.ulpCommand);
7434 return IOCB_ERROR;
7435 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007436 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7437 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7438 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7439 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7440 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7441 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7442 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7443 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007444 command_type = OTHER_COMMAND;
7445 break;
7446 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007447 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007448 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007449 /* word3 iocb=iotag32 wqe=response_payload_len */
7450 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007451 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007452 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007453 /* word5 iocb=rsvd wge=did */
7454 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7455 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007456 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7457 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7458 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7459 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7460 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04007461 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007462 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007463 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007464 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7465 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7466 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7467 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7468 LPFC_WQE_LENLOC_WORD3);
7469 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007470 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7471 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smart4f774512009-05-22 14:52:35 -04007472 command_type = OTHER_COMMAND;
7473 break;
7474 case CMD_CLOSE_XRI_CN:
7475 case CMD_ABORT_XRI_CN:
7476 case CMD_ABORT_XRI_CX:
7477 /* words 0-2 memcpy should be 0 rserved */
7478 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007479 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7480 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7481 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7482 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7483 } else
7484 fip = 0;
7485
7486 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007487 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007488 * The link is down, or the command was ELS_FIP
7489 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007490 * on the wire.
7491 */
7492 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7493 else
7494 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7495 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007496 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7497 wqe->abort_cmd.rsrvd5 = 0;
7498 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007499 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7500 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007501 /*
7502 * The abort handler will send us CMD_ABORT_XRI_CN or
7503 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7504 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007505 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7506 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7507 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7508 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007509 cmnd = CMD_ABORT_XRI_CX;
7510 command_type = OTHER_COMMAND;
7511 xritag = 0;
7512 break;
James Smart6669f9b2009-10-02 15:16:45 -04007513 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007514 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007515 * we re-construct this WQE here based on information in
7516 * iocbq from scratch.
7517 */
7518 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007519 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007520 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007521 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7522 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007523 LPFC_ABTS_UNSOL_INT) {
7524 /* ABTS sent by initiator to CT exchange, the
7525 * RX_ID field will be filled with the newly
7526 * allocated responder XRI.
7527 */
7528 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7529 iocbq->sli4_xritag);
7530 } else {
7531 /* ABTS sent by responder to CT exchange, the
7532 * RX_ID field will be filled with the responder
7533 * RX_ID from ABTS.
7534 */
7535 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007536 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007537 }
James Smart6669f9b2009-10-02 15:16:45 -04007538 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7539 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7540 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7541 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04007542 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7543 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7544 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04007545 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7546 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05007547 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7548 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7549 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7550 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7551 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7552 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7553 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7554 }
7555
James Smart6669f9b2009-10-02 15:16:45 -04007556 break;
James Smart4f774512009-05-22 14:52:35 -04007557 case CMD_XRI_ABORTED_CX:
7558 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04007559 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7560 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7561 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7562 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7563 default:
7564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7565 "2014 Invalid command 0x%x\n",
7566 iocbq->iocb.ulpCommand);
7567 return IOCB_ERROR;
7568 break;
James Smart4f774512009-05-22 14:52:35 -04007569 }
James Smart6d368e52011-05-24 11:44:12 -04007570
James Smartf0d9bcc2010-10-22 11:07:09 -04007571 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7572 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7573 wqe->generic.wqe_com.abort_tag = abort_tag;
7574 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7575 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7576 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7577 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04007578 return 0;
7579}
7580
7581/**
7582 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7583 * @phba: Pointer to HBA context object.
7584 * @ring_number: SLI ring number to issue iocb on.
7585 * @piocb: Pointer to command iocb.
7586 * @flag: Flag indicating if this command can be put into txq.
7587 *
7588 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7589 * an iocb command to an HBA with SLI-4 interface spec.
7590 *
7591 * This function is called with hbalock held. The function will return success
7592 * after it successfully submit the iocb to firmware or after adding to the
7593 * txq.
7594 **/
7595static int
7596__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7597 struct lpfc_iocbq *piocb, uint32_t flag)
7598{
7599 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04007600 union lpfc_wqe wqe;
7601 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04007602
7603 if (piocb->sli4_xritag == NO_XRI) {
7604 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05007605 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7606 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04007607 sglq = NULL;
7608 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04007609 if (pring->txq_cnt) {
7610 if (!(flag & SLI_IOCB_RET_IOCB)) {
7611 __lpfc_sli_ringtx_put(phba,
7612 pring, piocb);
7613 return IOCB_SUCCESS;
7614 } else {
7615 return IOCB_BUSY;
7616 }
7617 } else {
James Smart6d368e52011-05-24 11:44:12 -04007618 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04007619 if (!sglq) {
7620 if (!(flag & SLI_IOCB_RET_IOCB)) {
7621 __lpfc_sli_ringtx_put(phba,
7622 pring,
7623 piocb);
7624 return IOCB_SUCCESS;
7625 } else
7626 return IOCB_BUSY;
7627 }
7628 }
James Smart4f774512009-05-22 14:52:35 -04007629 }
7630 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04007631 /* These IO's already have an XRI and a mapped sgl. */
7632 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04007633 } else {
James Smart6d368e52011-05-24 11:44:12 -04007634 /*
7635 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04007636 * sglq is on the active list
7637 */
7638 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
7639 if (!sglq)
7640 return IOCB_ERROR;
7641 }
7642
7643 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04007644 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007645 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007646 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04007647 return IOCB_ERROR;
7648 }
7649
7650 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
7651 return IOCB_ERROR;
7652
James Smart341af102010-01-26 23:07:37 -05007653 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
7654 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05007655 /*
7656 * For FCP command IOCB, get a new WQ index to distribute
7657 * WQE across the WQsr. On the other hand, for abort IOCB,
7658 * it carries the same WQ index to the original command
7659 * IOCB.
7660 */
James Smart341af102010-01-26 23:07:37 -05007661 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05007662 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
7663 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
7664 &wqe))
James Smart4f774512009-05-22 14:52:35 -04007665 return IOCB_ERROR;
7666 } else {
7667 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
7668 return IOCB_ERROR;
7669 }
7670 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
7671
7672 return 0;
7673}
7674
7675/**
James Smart3772a992009-05-22 14:50:54 -04007676 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
7677 *
7678 * This routine wraps the actual lockless version for issusing IOCB function
7679 * pointer from the lpfc_hba struct.
7680 *
7681 * Return codes:
7682 * IOCB_ERROR - Error
7683 * IOCB_SUCCESS - Success
7684 * IOCB_BUSY - Busy
7685 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007686int
James Smart3772a992009-05-22 14:50:54 -04007687__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7688 struct lpfc_iocbq *piocb, uint32_t flag)
7689{
7690 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7691}
7692
7693/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007694 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04007695 * @phba: The hba struct for which this call is being executed.
7696 * @dev_grp: The HBA PCI-Device group number.
7697 *
7698 * This routine sets up the SLI interface API function jump table in @phba
7699 * struct.
7700 * Returns: 0 - success, -ENODEV - failure.
7701 **/
7702int
7703lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7704{
7705
7706 switch (dev_grp) {
7707 case LPFC_PCI_DEV_LP:
7708 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
7709 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
7710 break;
James Smart4f774512009-05-22 14:52:35 -04007711 case LPFC_PCI_DEV_OC:
7712 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
7713 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
7714 break;
James Smart3772a992009-05-22 14:50:54 -04007715 default:
7716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7717 "1419 Invalid HBA PCI-device group: 0x%x\n",
7718 dev_grp);
7719 return -ENODEV;
7720 break;
7721 }
7722 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
7723 return 0;
7724}
James Smart92d7f7b2007-06-17 19:56:38 -05007725
James Smarte59058c2008-08-24 21:49:00 -04007726/**
James Smart3621a712009-04-06 18:47:14 -04007727 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007728 * @phba: Pointer to HBA context object.
7729 * @pring: Pointer to driver SLI ring object.
7730 * @piocb: Pointer to command iocb.
7731 * @flag: Flag indicating if this command can be put into txq.
7732 *
7733 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
7734 * function. This function gets the hbalock and calls
7735 * __lpfc_sli_issue_iocb function and will return the error returned
7736 * by __lpfc_sli_issue_iocb function. This wrapper is used by
7737 * functions which do not hold hbalock.
7738 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007739int
James Smart3772a992009-05-22 14:50:54 -04007740lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05007741 struct lpfc_iocbq *piocb, uint32_t flag)
7742{
7743 unsigned long iflags;
7744 int rc;
7745
7746 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04007747 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05007748 spin_unlock_irqrestore(&phba->hbalock, iflags);
7749
7750 return rc;
7751}
7752
James Smarte59058c2008-08-24 21:49:00 -04007753/**
James Smart3621a712009-04-06 18:47:14 -04007754 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04007755 * @phba: Pointer to HBA context object.
7756 *
7757 * This function is called while driver attaches with the
7758 * HBA to setup the extra ring. The extra ring is used
7759 * only when driver needs to support target mode functionality
7760 * or IP over FC functionalities.
7761 *
7762 * This function is called with no lock held.
7763 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007764static int
7765lpfc_extra_ring_setup( struct lpfc_hba *phba)
7766{
7767 struct lpfc_sli *psli;
7768 struct lpfc_sli_ring *pring;
7769
7770 psli = &phba->sli;
7771
7772 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05007773
7774 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007775 pring = &psli->ring[psli->fcp_ring];
7776 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7777 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7778 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7779 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7780
James Smarta4bc3372006-12-02 13:34:16 -05007781 /* and give them to the extra ring */
7782 pring = &psli->ring[psli->extra_ring];
7783
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007784 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7785 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7786 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7787 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7788
7789 /* Setup default profile for this ring */
7790 pring->iotag_max = 4096;
7791 pring->num_mask = 1;
7792 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05007793 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
7794 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007795 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
7796 return 0;
7797}
7798
James Smarte59058c2008-08-24 21:49:00 -04007799/**
James Smart3621a712009-04-06 18:47:14 -04007800 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04007801 * @phba: Pointer to HBA context object.
7802 * @pring: Pointer to driver SLI ring object.
7803 * @iocbq: Pointer to iocb object.
7804 *
7805 * This function is called by the slow ring event handler
7806 * function when there is an ASYNC event iocb in the ring.
7807 * This function is called with no lock held.
7808 * Currently this function handles only temperature related
7809 * ASYNC events. The function decodes the temperature sensor
7810 * event message and posts events for the management applications.
7811 **/
James Smart98c9ea52007-10-27 13:37:33 -04007812static void
James Smart57127f12007-10-27 13:37:05 -04007813lpfc_sli_async_event_handler(struct lpfc_hba * phba,
7814 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
7815{
7816 IOCB_t *icmd;
7817 uint16_t evt_code;
7818 uint16_t temp;
7819 struct temp_event temp_event_data;
7820 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04007821 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04007822
7823 icmd = &iocbq->iocb;
7824 evt_code = icmd->un.asyncstat.evt_code;
7825 temp = icmd->ulpContext;
7826
7827 if ((evt_code != ASYNC_TEMP_WARN) &&
7828 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04007829 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04007830 lpfc_printf_log(phba,
7831 KERN_ERR,
7832 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007833 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04007834 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04007835 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
7836 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
7837 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
7838 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04007839 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04007840 icmd->un.asyncstat.evt_code,
7841 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
7842 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
7843 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
7844 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
7845
James Smart57127f12007-10-27 13:37:05 -04007846 return;
7847 }
7848 temp_event_data.data = (uint32_t)temp;
7849 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
7850 if (evt_code == ASYNC_TEMP_WARN) {
7851 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
7852 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05007853 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04007854 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04007855 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04007856 "corrective action. temperature : %d Celsius\n",
7857 temp);
7858 }
7859 if (evt_code == ASYNC_TEMP_SAFE) {
7860 temp_event_data.event_code = LPFC_NORMAL_TEMP;
7861 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05007862 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04007863 LOG_TEMP,
7864 "0340 Adapter temperature is OK now. "
7865 "temperature : %d Celsius\n",
7866 temp);
7867 }
7868
7869 /* Send temperature change event to applications */
7870 shost = lpfc_shost_from_vport(phba->pport);
7871 fc_host_post_vendor_event(shost, fc_get_event_number(),
7872 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05007873 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04007874
7875}
7876
7877
James Smarte59058c2008-08-24 21:49:00 -04007878/**
James Smart3621a712009-04-06 18:47:14 -04007879 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04007880 * @phba: Pointer to HBA context object.
7881 *
7882 * lpfc_sli_setup sets up rings of the SLI interface with
7883 * number of iocbs per ring and iotags. This function is
7884 * called while driver attach to the HBA and before the
7885 * interrupts are enabled. So there is no need for locking.
7886 *
7887 * This function always returns 0.
7888 **/
dea31012005-04-17 16:05:31 -05007889int
7890lpfc_sli_setup(struct lpfc_hba *phba)
7891{
James Smarted957682007-06-17 19:56:37 -05007892 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05007893 struct lpfc_sli *psli = &phba->sli;
7894 struct lpfc_sli_ring *pring;
7895
7896 psli->num_rings = MAX_CONFIGURED_RINGS;
7897 psli->sli_flag = 0;
7898 psli->fcp_ring = LPFC_FCP_RING;
7899 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05007900 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05007901
James Bottomley604a3e32005-10-29 10:28:33 -05007902 psli->iocbq_lookup = NULL;
7903 psli->iocbq_lookup_len = 0;
7904 psli->last_iotag = 0;
7905
dea31012005-04-17 16:05:31 -05007906 for (i = 0; i < psli->num_rings; i++) {
7907 pring = &psli->ring[i];
7908 switch (i) {
7909 case LPFC_FCP_RING: /* ring 0 - FCP */
7910 /* numCiocb and numRiocb are used in config_port */
7911 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7912 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7913 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7914 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7915 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7916 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007917 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007918 SLI3_IOCB_CMD_SIZE :
7919 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007920 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007921 SLI3_IOCB_RSP_SIZE :
7922 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007923 pring->iotag_ctr = 0;
7924 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05007925 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05007926 pring->fast_iotag = pring->iotag_max;
7927 pring->num_mask = 0;
7928 break;
James Smarta4bc3372006-12-02 13:34:16 -05007929 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05007930 /* numCiocb and numRiocb are used in config_port */
7931 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7932 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007933 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007934 SLI3_IOCB_CMD_SIZE :
7935 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007936 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007937 SLI3_IOCB_RSP_SIZE :
7938 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05007939 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05007940 pring->num_mask = 0;
7941 break;
7942 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
7943 /* numCiocb and numRiocb are used in config_port */
7944 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7945 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007946 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007947 SLI3_IOCB_CMD_SIZE :
7948 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007949 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007950 SLI3_IOCB_RSP_SIZE :
7951 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007952 pring->fast_iotag = 0;
7953 pring->iotag_ctr = 0;
7954 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04007955 pring->lpfc_sli_rcv_async_status =
7956 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04007957 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05007958 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04007959 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7960 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007961 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007962 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007963 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04007964 pring->prt[1].rctl = FC_RCTL_ELS_REP;
7965 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007966 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007967 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007968 pring->prt[2].profile = 0; /* Mask 2 */
7969 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04007970 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05007971 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007972 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007973 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007974 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05007975 pring->prt[3].profile = 0; /* Mask 3 */
7976 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04007977 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05007978 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007979 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007980 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007981 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04007982 /* abort unsolicited sequence */
7983 pring->prt[4].profile = 0; /* Mask 4 */
7984 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7985 pring->prt[4].type = FC_TYPE_BLS;
7986 pring->prt[4].lpfc_sli_rcv_unsol_event =
7987 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05007988 break;
7989 }
James Smarted957682007-06-17 19:56:37 -05007990 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05007991 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05007992 }
James Smarted957682007-06-17 19:56:37 -05007993 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05007994 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04007995 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7996 "SLI2 SLIM Data: x%x x%lx\n",
7997 phba->brd_no, totiocbsize,
7998 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05007999 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008000 if (phba->cfg_multi_ring_support == 2)
8001 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008002
8003 return 0;
8004}
8005
James Smarte59058c2008-08-24 21:49:00 -04008006/**
James Smart3621a712009-04-06 18:47:14 -04008007 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008008 * @phba: Pointer to HBA context object.
8009 *
8010 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8011 * ring. This function also initializes ring indices of each ring.
8012 * This function is called during the initialization of the SLI
8013 * interface of an HBA.
8014 * This function is called with no lock held and always returns
8015 * 1.
8016 **/
dea31012005-04-17 16:05:31 -05008017int
James Smart2e0fef82007-06-17 19:56:36 -05008018lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008019{
8020 struct lpfc_sli *psli;
8021 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008022 int i;
dea31012005-04-17 16:05:31 -05008023
8024 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008025 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008026 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008027 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008028 /* Initialize list headers for txq and txcmplq as double linked lists */
8029 for (i = 0; i < psli->num_rings; i++) {
8030 pring = &psli->ring[i];
8031 pring->ringno = i;
8032 pring->next_cmdidx = 0;
8033 pring->local_getidx = 0;
8034 pring->cmdidx = 0;
8035 INIT_LIST_HEAD(&pring->txq);
8036 INIT_LIST_HEAD(&pring->txcmplq);
8037 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008038 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008039 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008040 }
James Smart2e0fef82007-06-17 19:56:36 -05008041 spin_unlock_irq(&phba->hbalock);
8042 return 1;
dea31012005-04-17 16:05:31 -05008043}
8044
James Smarte59058c2008-08-24 21:49:00 -04008045/**
James Smart04c68492009-05-22 14:52:52 -04008046 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8047 * @phba: Pointer to HBA context object.
8048 *
8049 * This routine flushes the mailbox command subsystem. It will unconditionally
8050 * flush all the mailbox commands in the three possible stages in the mailbox
8051 * command sub-system: pending mailbox command queue; the outstanding mailbox
8052 * command; and completed mailbox command queue. It is caller's responsibility
8053 * to make sure that the driver is in the proper state to flush the mailbox
8054 * command sub-system. Namely, the posting of mailbox commands into the
8055 * pending mailbox command queue from the various clients must be stopped;
8056 * either the HBA is in a state that it will never works on the outstanding
8057 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8058 * mailbox command has been completed.
8059 **/
8060static void
8061lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8062{
8063 LIST_HEAD(completions);
8064 struct lpfc_sli *psli = &phba->sli;
8065 LPFC_MBOXQ_t *pmb;
8066 unsigned long iflag;
8067
8068 /* Flush all the mailbox commands in the mbox system */
8069 spin_lock_irqsave(&phba->hbalock, iflag);
8070 /* The pending mailbox command queue */
8071 list_splice_init(&phba->sli.mboxq, &completions);
8072 /* The outstanding active mailbox command */
8073 if (psli->mbox_active) {
8074 list_add_tail(&psli->mbox_active->list, &completions);
8075 psli->mbox_active = NULL;
8076 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8077 }
8078 /* The completed mailbox command queue */
8079 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8080 spin_unlock_irqrestore(&phba->hbalock, iflag);
8081
8082 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8083 while (!list_empty(&completions)) {
8084 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8085 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8086 if (pmb->mbox_cmpl)
8087 pmb->mbox_cmpl(phba, pmb);
8088 }
8089}
8090
8091/**
James Smart3621a712009-04-06 18:47:14 -04008092 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008093 * @vport: Pointer to virtual port object.
8094 *
8095 * lpfc_sli_host_down is called to clean up the resources
8096 * associated with a vport before destroying virtual
8097 * port data structures.
8098 * This function does following operations:
8099 * - Free discovery resources associated with this virtual
8100 * port.
8101 * - Free iocbs associated with this virtual port in
8102 * the txq.
8103 * - Send abort for all iocb commands associated with this
8104 * vport in txcmplq.
8105 *
8106 * This function is called with no lock held and always returns 1.
8107 **/
dea31012005-04-17 16:05:31 -05008108int
James Smart92d7f7b2007-06-17 19:56:38 -05008109lpfc_sli_host_down(struct lpfc_vport *vport)
8110{
James Smart858c9f62007-06-17 19:56:39 -05008111 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008112 struct lpfc_hba *phba = vport->phba;
8113 struct lpfc_sli *psli = &phba->sli;
8114 struct lpfc_sli_ring *pring;
8115 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008116 int i;
8117 unsigned long flags = 0;
8118 uint16_t prev_pring_flag;
8119
8120 lpfc_cleanup_discovery_resources(vport);
8121
8122 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008123 for (i = 0; i < psli->num_rings; i++) {
8124 pring = &psli->ring[i];
8125 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008126 /* Only slow rings */
8127 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008128 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008129 /* Set the lpfc data pending flag */
8130 set_bit(LPFC_DATA_READY, &phba->data_flags);
8131 }
James Smart92d7f7b2007-06-17 19:56:38 -05008132 /*
8133 * Error everything on the txq since these iocbs have not been
8134 * given to the FW yet.
8135 */
James Smart92d7f7b2007-06-17 19:56:38 -05008136 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8137 if (iocb->vport != vport)
8138 continue;
James Smart858c9f62007-06-17 19:56:39 -05008139 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008140 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008141 }
8142
8143 /* Next issue ABTS for everything on the txcmplq */
8144 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8145 list) {
8146 if (iocb->vport != vport)
8147 continue;
8148 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8149 }
8150
8151 pring->flag = prev_pring_flag;
8152 }
8153
8154 spin_unlock_irqrestore(&phba->hbalock, flags);
8155
James Smarta257bf92009-04-06 18:48:10 -04008156 /* Cancel all the IOCBs from the completions list */
8157 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8158 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008159 return 1;
8160}
8161
James Smarte59058c2008-08-24 21:49:00 -04008162/**
James Smart3621a712009-04-06 18:47:14 -04008163 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008164 * @phba: Pointer to HBA context object.
8165 *
8166 * This function cleans up all iocb, buffers, mailbox commands
8167 * while shutting down the HBA. This function is called with no
8168 * lock held and always returns 1.
8169 * This function does the following to cleanup driver resources:
8170 * - Free discovery resources for each virtual port
8171 * - Cleanup any pending fabric iocbs
8172 * - Iterate through the iocb txq and free each entry
8173 * in the list.
8174 * - Free up any buffer posted to the HBA
8175 * - Free mailbox commands in the mailbox queue.
8176 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008177int
James Smart2e0fef82007-06-17 19:56:36 -05008178lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008179{
James Smart2534ba72007-04-25 09:52:20 -04008180 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008181 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008182 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008183 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008184 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008185 int i;
8186
8187 /* Shutdown the mailbox command sub-system */
8188 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008189
dea31012005-04-17 16:05:31 -05008190 lpfc_hba_down_prep(phba);
8191
James Smart92d7f7b2007-06-17 19:56:38 -05008192 lpfc_fabric_abort_hba(phba);
8193
James Smart2e0fef82007-06-17 19:56:36 -05008194 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008195 for (i = 0; i < psli->num_rings; i++) {
8196 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008197 /* Only slow rings */
8198 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008199 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008200 /* Set the lpfc data pending flag */
8201 set_bit(LPFC_DATA_READY, &phba->data_flags);
8202 }
dea31012005-04-17 16:05:31 -05008203
8204 /*
8205 * Error everything on the txq since these iocbs have not been
8206 * given to the FW yet.
8207 */
James Smart2534ba72007-04-25 09:52:20 -04008208 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008209 pring->txq_cnt = 0;
8210
dea31012005-04-17 16:05:31 -05008211 }
James Smart2e0fef82007-06-17 19:56:36 -05008212 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008213
James Smarta257bf92009-04-06 18:48:10 -04008214 /* Cancel all the IOCBs from the completions list */
8215 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8216 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008217
James Smart0ff10d42008-01-11 01:52:36 -05008218 spin_lock_irqsave(&phba->hbalock, flags);
8219 list_splice_init(&phba->elsbuf, &completions);
8220 phba->elsbuf_cnt = 0;
8221 phba->elsbuf_prev_cnt = 0;
8222 spin_unlock_irqrestore(&phba->hbalock, flags);
8223
8224 while (!list_empty(&completions)) {
8225 list_remove_head(&completions, buf_ptr,
8226 struct lpfc_dmabuf, list);
8227 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8228 kfree(buf_ptr);
8229 }
8230
dea31012005-04-17 16:05:31 -05008231 /* Return any active mbox cmds */
8232 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008233
James Smartda0436e2009-05-22 14:51:39 -04008234 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008235 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008236 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008237
James Smartda0436e2009-05-22 14:51:39 -04008238 return 1;
8239}
James Smart92d7f7b2007-06-17 19:56:38 -05008240
James Smartda0436e2009-05-22 14:51:39 -04008241/**
James Smart3621a712009-04-06 18:47:14 -04008242 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008243 * @srcp: Source memory pointer.
8244 * @destp: Destination memory pointer.
8245 * @cnt: Number of words required to be copied.
8246 *
8247 * This function is used for copying data between driver memory
8248 * and the SLI memory. This function also changes the endianness
8249 * of each word if native endianness is different from SLI
8250 * endianness. This function can be called with or without
8251 * lock.
8252 **/
dea31012005-04-17 16:05:31 -05008253void
8254lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8255{
8256 uint32_t *src = srcp;
8257 uint32_t *dest = destp;
8258 uint32_t ldata;
8259 int i;
8260
8261 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8262 ldata = *src;
8263 ldata = le32_to_cpu(ldata);
8264 *dest = ldata;
8265 src++;
8266 dest++;
8267 }
8268}
8269
James Smarte59058c2008-08-24 21:49:00 -04008270
8271/**
James Smarta0c87cb2009-07-19 10:01:10 -04008272 * lpfc_sli_bemem_bcopy - SLI memory copy function
8273 * @srcp: Source memory pointer.
8274 * @destp: Destination memory pointer.
8275 * @cnt: Number of words required to be copied.
8276 *
8277 * This function is used for copying data between a data structure
8278 * with big endian representation to local endianness.
8279 * This function can be called with or without lock.
8280 **/
8281void
8282lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8283{
8284 uint32_t *src = srcp;
8285 uint32_t *dest = destp;
8286 uint32_t ldata;
8287 int i;
8288
8289 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8290 ldata = *src;
8291 ldata = be32_to_cpu(ldata);
8292 *dest = ldata;
8293 src++;
8294 dest++;
8295 }
8296}
8297
8298/**
James Smart3621a712009-04-06 18:47:14 -04008299 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008300 * @phba: Pointer to HBA context object.
8301 * @pring: Pointer to driver SLI ring object.
8302 * @mp: Pointer to driver buffer object.
8303 *
8304 * This function is called with no lock held.
8305 * It always return zero after adding the buffer to the postbufq
8306 * buffer list.
8307 **/
dea31012005-04-17 16:05:31 -05008308int
James Smart2e0fef82007-06-17 19:56:36 -05008309lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8310 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008311{
8312 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8313 later */
James Smart2e0fef82007-06-17 19:56:36 -05008314 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008315 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008316 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008317 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008318 return 0;
8319}
8320
James Smarte59058c2008-08-24 21:49:00 -04008321/**
James Smart3621a712009-04-06 18:47:14 -04008322 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008323 * @phba: Pointer to HBA context object.
8324 *
8325 * When HBQ is enabled, buffers are searched based on tags. This function
8326 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8327 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8328 * does not conflict with tags of buffer posted for unsolicited events.
8329 * The function returns the allocated tag. The function is called with
8330 * no locks held.
8331 **/
James Smart76bb24e2007-10-27 13:38:00 -04008332uint32_t
8333lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8334{
8335 spin_lock_irq(&phba->hbalock);
8336 phba->buffer_tag_count++;
8337 /*
8338 * Always set the QUE_BUFTAG_BIT to distiguish between
8339 * a tag assigned by HBQ.
8340 */
8341 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8342 spin_unlock_irq(&phba->hbalock);
8343 return phba->buffer_tag_count;
8344}
8345
James Smarte59058c2008-08-24 21:49:00 -04008346/**
James Smart3621a712009-04-06 18:47:14 -04008347 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008348 * @phba: Pointer to HBA context object.
8349 * @pring: Pointer to driver SLI ring object.
8350 * @tag: Buffer tag.
8351 *
8352 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8353 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8354 * iocb is posted to the response ring with the tag of the buffer.
8355 * This function searches the pring->postbufq list using the tag
8356 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8357 * iocb. If the buffer is found then lpfc_dmabuf object of the
8358 * buffer is returned to the caller else NULL is returned.
8359 * This function is called with no lock held.
8360 **/
James Smart76bb24e2007-10-27 13:38:00 -04008361struct lpfc_dmabuf *
8362lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8363 uint32_t tag)
8364{
8365 struct lpfc_dmabuf *mp, *next_mp;
8366 struct list_head *slp = &pring->postbufq;
8367
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008368 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008369 spin_lock_irq(&phba->hbalock);
8370 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8371 if (mp->buffer_tag == tag) {
8372 list_del_init(&mp->list);
8373 pring->postbufq_cnt--;
8374 spin_unlock_irq(&phba->hbalock);
8375 return mp;
8376 }
8377 }
8378
8379 spin_unlock_irq(&phba->hbalock);
8380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008381 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008382 "ring %d Data x%lx x%p x%p x%x\n",
8383 pring->ringno, (unsigned long) tag,
8384 slp->next, slp->prev, pring->postbufq_cnt);
8385
8386 return NULL;
8387}
dea31012005-04-17 16:05:31 -05008388
James Smarte59058c2008-08-24 21:49:00 -04008389/**
James Smart3621a712009-04-06 18:47:14 -04008390 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008391 * @phba: Pointer to HBA context object.
8392 * @pring: Pointer to driver SLI ring object.
8393 * @phys: DMA address of the buffer.
8394 *
8395 * This function searches the buffer list using the dma_address
8396 * of unsolicited event to find the driver's lpfc_dmabuf object
8397 * corresponding to the dma_address. The function returns the
8398 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8399 * This function is called by the ct and els unsolicited event
8400 * handlers to get the buffer associated with the unsolicited
8401 * event.
8402 *
8403 * This function is called with no lock held.
8404 **/
dea31012005-04-17 16:05:31 -05008405struct lpfc_dmabuf *
8406lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8407 dma_addr_t phys)
8408{
8409 struct lpfc_dmabuf *mp, *next_mp;
8410 struct list_head *slp = &pring->postbufq;
8411
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008412 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05008413 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008414 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8415 if (mp->phys == phys) {
8416 list_del_init(&mp->list);
8417 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05008418 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008419 return mp;
8420 }
8421 }
8422
James Smart2e0fef82007-06-17 19:56:36 -05008423 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04008425 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05008426 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008427 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05008428 slp->next, slp->prev, pring->postbufq_cnt);
8429 return NULL;
8430}
8431
James Smarte59058c2008-08-24 21:49:00 -04008432/**
James Smart3621a712009-04-06 18:47:14 -04008433 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04008434 * @phba: Pointer to HBA context object.
8435 * @cmdiocb: Pointer to driver command iocb object.
8436 * @rspiocb: Pointer to driver response iocb object.
8437 *
8438 * This function is the completion handler for the abort iocbs for
8439 * ELS commands. This function is called from the ELS ring event
8440 * handler with no lock held. This function frees memory resources
8441 * associated with the abort iocb.
8442 **/
dea31012005-04-17 16:05:31 -05008443static void
James Smart2e0fef82007-06-17 19:56:36 -05008444lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8445 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05008446{
James Smart2e0fef82007-06-17 19:56:36 -05008447 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04008448 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05008449 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04008450 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8451
8452 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04008453
8454 if (irsp->ulpStatus) {
8455 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8456 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8457
James Smart2e0fef82007-06-17 19:56:36 -05008458 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04008459 if (phba->sli_rev < LPFC_SLI_REV4) {
8460 if (abort_iotag != 0 &&
8461 abort_iotag <= phba->sli.last_iotag)
8462 abort_iocb =
8463 phba->sli.iocbq_lookup[abort_iotag];
8464 } else
8465 /* For sli4 the abort_tag is the XRI,
8466 * so the abort routine puts the iotag of the iocb
8467 * being aborted in the context field of the abort
8468 * IOCB.
8469 */
8470 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04008471
James Smart2680eea2007-04-25 09:52:55 -04008472 /*
James Smart58da1ff2008-04-07 10:15:56 -04008473 * If the iocb is not found in Firmware queue the iocb
8474 * might have completed already. Do not free it again.
8475 */
James Smart9b379602008-04-07 10:16:00 -04008476 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04008477 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8478 spin_unlock_irq(&phba->hbalock);
8479 lpfc_sli_release_iocbq(phba, cmdiocb);
8480 return;
8481 }
8482 /* For SLI4 the ulpContext field for abort IOCB
8483 * holds the iotag of the IOCB being aborted so
8484 * the local abort_context needs to be reset to
8485 * match the aborted IOCBs ulpContext.
8486 */
8487 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8488 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04008489 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008490
8491 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8492 "0327 Cannot abort els iocb %p "
8493 "with tag %x context %x, abort status %x, "
8494 "abort code %x\n",
8495 abort_iocb, abort_iotag, abort_context,
8496 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04008497 /*
James Smart2680eea2007-04-25 09:52:55 -04008498 * make sure we have the right iocbq before taking it
8499 * off the txcmplq and try to call completion routine.
8500 */
James Smart2e0fef82007-06-17 19:56:36 -05008501 if (!abort_iocb ||
8502 abort_iocb->iocb.ulpContext != abort_context ||
8503 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8504 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05008505 else if (phba->sli_rev < LPFC_SLI_REV4) {
8506 /*
8507 * leave the SLI4 aborted command on the txcmplq
8508 * list and the command complete WCQE's XB bit
8509 * will tell whether the SGL (XRI) can be released
8510 * immediately or to the aborted SGL list for the
8511 * following abort XRI from the HBA.
8512 */
James Smart92d7f7b2007-06-17 19:56:38 -05008513 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04008514 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8515 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8516 pring->txcmplq_cnt--;
8517 }
James Smart2680eea2007-04-25 09:52:55 -04008518
James Smart0ff10d42008-01-11 01:52:36 -05008519 /* Firmware could still be in progress of DMAing
8520 * payload, so don't free data buffer till after
8521 * a hbeat.
8522 */
8523 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05008524 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05008525 spin_unlock_irq(&phba->hbalock);
8526
James Smart92d7f7b2007-06-17 19:56:38 -05008527 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05008528 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05008529 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04008530 } else
8531 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04008532 }
8533
James Bottomley604a3e32005-10-29 10:28:33 -05008534 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05008535 return;
8536}
8537
James Smarte59058c2008-08-24 21:49:00 -04008538/**
James Smart3621a712009-04-06 18:47:14 -04008539 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04008540 * @phba: Pointer to HBA context object.
8541 * @cmdiocb: Pointer to driver command iocb object.
8542 * @rspiocb: Pointer to driver response iocb object.
8543 *
8544 * The function is called from SLI ring event handler with no
8545 * lock held. This function is the completion handler for ELS commands
8546 * which are aborted. The function frees memory resources used for
8547 * the aborted ELS commands.
8548 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008549static void
8550lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8551 struct lpfc_iocbq *rspiocb)
8552{
8553 IOCB_t *irsp = &rspiocb->iocb;
8554
8555 /* ELS cmd tag <ulpIoTag> completes */
8556 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04008557 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008558 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008559 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05008560 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05008561 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8562 lpfc_ct_free_iocb(phba, cmdiocb);
8563 else
8564 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05008565 return;
8566}
8567
James Smarte59058c2008-08-24 21:49:00 -04008568/**
James Smart5af5eee2010-10-22 11:06:38 -04008569 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04008570 * @phba: Pointer to HBA context object.
8571 * @pring: Pointer to driver SLI ring object.
8572 * @cmdiocb: Pointer to driver command iocb object.
8573 *
James Smart5af5eee2010-10-22 11:06:38 -04008574 * This function issues an abort iocb for the provided command iocb down to
8575 * the port. Other than the case the outstanding command iocb is an abort
8576 * request, this function issues abort out unconditionally. This function is
8577 * called with hbalock held. The function returns 0 when it fails due to
8578 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04008579 **/
James Smart5af5eee2010-10-22 11:06:38 -04008580static int
8581lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008582 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05008583{
James Smart2e0fef82007-06-17 19:56:36 -05008584 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008585 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05008586 IOCB_t *icmd = NULL;
8587 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04008588 int retval;
James Smart07951072007-04-25 09:51:38 -04008589
James Smart92d7f7b2007-06-17 19:56:38 -05008590 /*
8591 * There are certain command types we don't want to abort. And we
8592 * don't want to abort commands that are already in the process of
8593 * being aborted.
James Smart07951072007-04-25 09:51:38 -04008594 */
8595 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05008596 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05008597 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8598 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04008599 return 0;
8600
dea31012005-04-17 16:05:31 -05008601 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05008602 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008603 if (abtsiocbp == NULL)
8604 return 0;
dea31012005-04-17 16:05:31 -05008605
James Smart07951072007-04-25 09:51:38 -04008606 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05008607 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04008608 */
8609 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8610
dea31012005-04-17 16:05:31 -05008611 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04008612 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8613 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04008614 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04008615 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04008616 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8617 }
James Smartda0436e2009-05-22 14:51:39 -04008618 else
8619 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008620 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04008621 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05008622
James Smart5ffc2662009-11-18 15:39:44 -05008623 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8624 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008625 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
8626 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008627
James Smart2e0fef82007-06-17 19:56:36 -05008628 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04008629 iabt->ulpCommand = CMD_ABORT_XRI_CN;
8630 else
8631 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
8632
8633 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04008634
James Smarte8b62012007-08-02 11:10:09 -04008635 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
8636 "0339 Abort xri x%x, original iotag x%x, "
8637 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04008638 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04008639 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04008640 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04008641 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04008642
James Smartd7c255b2008-08-24 21:50:00 -04008643 if (retval)
8644 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04008645
8646 /*
8647 * Caller to this routine should check for IOCB_ERROR
8648 * and handle it properly. This routine no longer removes
8649 * iocb off txcmplq and call compl in case of IOCB_ERROR.
8650 */
8651 return retval;
8652}
8653
8654/**
8655 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
8656 * @phba: Pointer to HBA context object.
8657 * @pring: Pointer to driver SLI ring object.
8658 * @cmdiocb: Pointer to driver command iocb object.
8659 *
8660 * This function issues an abort iocb for the provided command iocb. In case
8661 * of unloading, the abort iocb will not be issued to commands on the ELS
8662 * ring. Instead, the callback function shall be changed to those commands
8663 * so that nothing happens when them finishes. This function is called with
8664 * hbalock held. The function returns 0 when the command iocb is an abort
8665 * request.
8666 **/
8667int
8668lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8669 struct lpfc_iocbq *cmdiocb)
8670{
8671 struct lpfc_vport *vport = cmdiocb->vport;
8672 int retval = IOCB_ERROR;
8673 IOCB_t *icmd = NULL;
8674
8675 /*
8676 * There are certain command types we don't want to abort. And we
8677 * don't want to abort commands that are already in the process of
8678 * being aborted.
8679 */
8680 icmd = &cmdiocb->iocb;
8681 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8682 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8683 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8684 return 0;
8685
8686 /*
8687 * If we're unloading, don't abort iocb on the ELS ring, but change
8688 * the callback so that nothing happens when it finishes.
8689 */
8690 if ((vport->load_flag & FC_UNLOADING) &&
8691 (pring->ringno == LPFC_ELS_RING)) {
8692 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
8693 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
8694 else
8695 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
8696 goto abort_iotag_exit;
8697 }
8698
8699 /* Now, we try to issue the abort to the cmdiocb out */
8700 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
8701
James Smart07951072007-04-25 09:51:38 -04008702abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05008703 /*
8704 * Caller to this routine should check for IOCB_ERROR
8705 * and handle it properly. This routine no longer removes
8706 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04008707 */
James Smart2e0fef82007-06-17 19:56:36 -05008708 return retval;
dea31012005-04-17 16:05:31 -05008709}
8710
James Smarte59058c2008-08-24 21:49:00 -04008711/**
James Smart5af5eee2010-10-22 11:06:38 -04008712 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
8713 * @phba: Pointer to HBA context object.
8714 * @pring: Pointer to driver SLI ring object.
8715 *
8716 * This function aborts all iocbs in the given ring and frees all the iocb
8717 * objects in txq. This function issues abort iocbs unconditionally for all
8718 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
8719 * to complete before the return of this function. The caller is not required
8720 * to hold any locks.
8721 **/
8722static void
8723lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
8724{
8725 LIST_HEAD(completions);
8726 struct lpfc_iocbq *iocb, *next_iocb;
8727
8728 if (pring->ringno == LPFC_ELS_RING)
8729 lpfc_fabric_abort_hba(phba);
8730
8731 spin_lock_irq(&phba->hbalock);
8732
8733 /* Take off all the iocbs on txq for cancelling */
8734 list_splice_init(&pring->txq, &completions);
8735 pring->txq_cnt = 0;
8736
8737 /* Next issue ABTS for everything on the txcmplq */
8738 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
8739 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
8740
8741 spin_unlock_irq(&phba->hbalock);
8742
8743 /* Cancel all the IOCBs from the completions list */
8744 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8745 IOERR_SLI_ABORTED);
8746}
8747
8748/**
8749 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
8750 * @phba: pointer to lpfc HBA data structure.
8751 *
8752 * This routine will abort all pending and outstanding iocbs to an HBA.
8753 **/
8754void
8755lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
8756{
8757 struct lpfc_sli *psli = &phba->sli;
8758 struct lpfc_sli_ring *pring;
8759 int i;
8760
8761 for (i = 0; i < psli->num_rings; i++) {
8762 pring = &psli->ring[i];
8763 lpfc_sli_iocb_ring_abort(phba, pring);
8764 }
8765}
8766
8767/**
James Smart3621a712009-04-06 18:47:14 -04008768 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04008769 * @iocbq: Pointer to driver iocb object.
8770 * @vport: Pointer to driver virtual port object.
8771 * @tgt_id: SCSI ID of the target.
8772 * @lun_id: LUN ID of the scsi device.
8773 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
8774 *
James Smart3621a712009-04-06 18:47:14 -04008775 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04008776 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
8777 * 0 if the filtering criteria is met for the given iocb and will return
8778 * 1 if the filtering criteria is not met.
8779 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
8780 * given iocb is for the SCSI device specified by vport, tgt_id and
8781 * lun_id parameter.
8782 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
8783 * given iocb is for the SCSI target specified by vport and tgt_id
8784 * parameters.
8785 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
8786 * given iocb is for the SCSI host associated with the given vport.
8787 * This function is called with no locks held.
8788 **/
dea31012005-04-17 16:05:31 -05008789static int
James Smart51ef4c22007-08-02 11:10:31 -04008790lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
8791 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008792 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05008793{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008794 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008795 int rc = 1;
8796
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008797 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
8798 return rc;
8799
James Smart51ef4c22007-08-02 11:10:31 -04008800 if (iocbq->vport != vport)
8801 return rc;
8802
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008803 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008804
James Smart495a7142008-06-14 22:52:59 -04008805 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05008806 return rc;
8807
8808 switch (ctx_cmd) {
8809 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04008810 if ((lpfc_cmd->rdata->pnode) &&
8811 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
8812 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05008813 rc = 0;
8814 break;
8815 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04008816 if ((lpfc_cmd->rdata->pnode) &&
8817 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05008818 rc = 0;
8819 break;
dea31012005-04-17 16:05:31 -05008820 case LPFC_CTX_HOST:
8821 rc = 0;
8822 break;
8823 default:
8824 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07008825 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05008826 break;
8827 }
8828
8829 return rc;
8830}
8831
James Smarte59058c2008-08-24 21:49:00 -04008832/**
James Smart3621a712009-04-06 18:47:14 -04008833 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04008834 * @vport: Pointer to virtual port.
8835 * @tgt_id: SCSI ID of the target.
8836 * @lun_id: LUN ID of the scsi device.
8837 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8838 *
8839 * This function returns number of FCP commands pending for the vport.
8840 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
8841 * commands pending on the vport associated with SCSI device specified
8842 * by tgt_id and lun_id parameters.
8843 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
8844 * commands pending on the vport associated with SCSI target specified
8845 * by tgt_id parameter.
8846 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
8847 * commands pending on the vport.
8848 * This function returns the number of iocbs which satisfy the filter.
8849 * This function is called without any lock held.
8850 **/
dea31012005-04-17 16:05:31 -05008851int
James Smart51ef4c22007-08-02 11:10:31 -04008852lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
8853 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05008854{
James Smart51ef4c22007-08-02 11:10:31 -04008855 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008856 struct lpfc_iocbq *iocbq;
8857 int sum, i;
dea31012005-04-17 16:05:31 -05008858
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008859 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
8860 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008861
James Smart51ef4c22007-08-02 11:10:31 -04008862 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
8863 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008864 sum++;
dea31012005-04-17 16:05:31 -05008865 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008866
dea31012005-04-17 16:05:31 -05008867 return sum;
8868}
8869
James Smarte59058c2008-08-24 21:49:00 -04008870/**
James Smart3621a712009-04-06 18:47:14 -04008871 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04008872 * @phba: Pointer to HBA context object
8873 * @cmdiocb: Pointer to command iocb object.
8874 * @rspiocb: Pointer to response iocb object.
8875 *
8876 * This function is called when an aborted FCP iocb completes. This
8877 * function is called by the ring event handler with no lock held.
8878 * This function frees the iocb.
8879 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008880void
James Smart2e0fef82007-06-17 19:56:36 -05008881lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8882 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008883{
James Bottomley604a3e32005-10-29 10:28:33 -05008884 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008885 return;
8886}
8887
James Smarte59058c2008-08-24 21:49:00 -04008888/**
James Smart3621a712009-04-06 18:47:14 -04008889 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04008890 * @vport: Pointer to virtual port.
8891 * @pring: Pointer to driver SLI ring object.
8892 * @tgt_id: SCSI ID of the target.
8893 * @lun_id: LUN ID of the scsi device.
8894 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8895 *
8896 * This function sends an abort command for every SCSI command
8897 * associated with the given virtual port pending on the ring
8898 * filtered by lpfc_sli_validate_fcp_iocb function.
8899 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8900 * FCP iocbs associated with lun specified by tgt_id and lun_id
8901 * parameters
8902 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8903 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8904 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8905 * FCP iocbs associated with virtual port.
8906 * This function returns number of iocbs it failed to abort.
8907 * This function is called with no locks held.
8908 **/
dea31012005-04-17 16:05:31 -05008909int
James Smart51ef4c22007-08-02 11:10:31 -04008910lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8911 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05008912{
James Smart51ef4c22007-08-02 11:10:31 -04008913 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008914 struct lpfc_iocbq *iocbq;
8915 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05008916 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05008917 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008918 int i;
dea31012005-04-17 16:05:31 -05008919
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008920 for (i = 1; i <= phba->sli.last_iotag; i++) {
8921 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008922
James Smart51ef4c22007-08-02 11:10:31 -04008923 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05008924 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05008925 continue;
8926
8927 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008928 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008929 if (abtsiocb == NULL) {
8930 errcnt++;
8931 continue;
8932 }
dea31012005-04-17 16:05:31 -05008933
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008934 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05008935 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8936 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04008937 if (phba->sli_rev == LPFC_SLI_REV4)
8938 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8939 else
8940 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008941 abtsiocb->iocb.ulpLe = 1;
8942 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05008943 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05008944
James Smart5ffc2662009-11-18 15:39:44 -05008945 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8946 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008947 if (iocbq->iocb_flag & LPFC_IO_FCP)
8948 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008949
James Smart2e0fef82007-06-17 19:56:36 -05008950 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05008951 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8952 else
8953 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8954
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008955 /* Setup callback routine and issue the command. */
8956 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04008957 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8958 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05008959 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05008960 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05008961 errcnt++;
8962 continue;
8963 }
8964 }
8965
8966 return errcnt;
8967}
8968
James Smarte59058c2008-08-24 21:49:00 -04008969/**
James Smart3621a712009-04-06 18:47:14 -04008970 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04008971 * @phba: Pointer to HBA context object.
8972 * @cmdiocbq: Pointer to command iocb.
8973 * @rspiocbq: Pointer to response iocb.
8974 *
8975 * This function is the completion handler for iocbs issued using
8976 * lpfc_sli_issue_iocb_wait function. This function is called by the
8977 * ring event handler function without any lock held. This function
8978 * can be called from both worker thread context and interrupt
8979 * context. This function also can be called from other thread which
8980 * cleans up the SLI layer objects.
8981 * This function copy the contents of the response iocb to the
8982 * response iocb memory object provided by the caller of
8983 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8984 * sleeps for the iocb completion.
8985 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008986static void
8987lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8988 struct lpfc_iocbq *cmdiocbq,
8989 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05008990{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008991 wait_queue_head_t *pdone_q;
8992 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05008993 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008994
James Smart2e0fef82007-06-17 19:56:36 -05008995 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008996 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8997 if (cmdiocbq->context2 && rspiocbq)
8998 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8999 &rspiocbq->iocb, sizeof(IOCB_t));
9000
James Smart0f65ff62010-02-26 14:14:23 -05009001 /* Set the exchange busy flag for task management commands */
9002 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9003 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9004 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9005 cur_iocbq);
9006 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9007 }
9008
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009009 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009010 if (pdone_q)
9011 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009012 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009013 return;
9014}
9015
James Smarte59058c2008-08-24 21:49:00 -04009016/**
James Smartd11e31d2009-06-10 17:23:06 -04009017 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9018 * @phba: Pointer to HBA context object..
9019 * @piocbq: Pointer to command iocb.
9020 * @flag: Flag to test.
9021 *
9022 * This routine grabs the hbalock and then test the iocb_flag to
9023 * see if the passed in flag is set.
9024 * Returns:
9025 * 1 if flag is set.
9026 * 0 if flag is not set.
9027 **/
9028static int
9029lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9030 struct lpfc_iocbq *piocbq, uint32_t flag)
9031{
9032 unsigned long iflags;
9033 int ret;
9034
9035 spin_lock_irqsave(&phba->hbalock, iflags);
9036 ret = piocbq->iocb_flag & flag;
9037 spin_unlock_irqrestore(&phba->hbalock, iflags);
9038 return ret;
9039
9040}
9041
9042/**
James Smart3621a712009-04-06 18:47:14 -04009043 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009044 * @phba: Pointer to HBA context object..
9045 * @pring: Pointer to sli ring.
9046 * @piocb: Pointer to command iocb.
9047 * @prspiocbq: Pointer to response iocb.
9048 * @timeout: Timeout in number of seconds.
9049 *
9050 * This function issues the iocb to firmware and waits for the
9051 * iocb to complete. If the iocb command is not
9052 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9053 * Caller should not free the iocb resources if this function
9054 * returns IOCB_TIMEDOUT.
9055 * The function waits for the iocb completion using an
9056 * non-interruptible wait.
9057 * This function will sleep while waiting for iocb completion.
9058 * So, this function should not be called from any context which
9059 * does not allow sleeping. Due to the same reason, this function
9060 * cannot be called with interrupt disabled.
9061 * This function assumes that the iocb completions occur while
9062 * this function sleep. So, this function cannot be called from
9063 * the thread which process iocb completion for this ring.
9064 * This function clears the iocb_flag of the iocb object before
9065 * issuing the iocb and the iocb completion handler sets this
9066 * flag and wakes this thread when the iocb completes.
9067 * The contents of the response iocb will be copied to prspiocbq
9068 * by the completion handler when the command completes.
9069 * This function returns IOCB_SUCCESS when success.
9070 * This function is called with no lock held.
9071 **/
dea31012005-04-17 16:05:31 -05009072int
James Smart2e0fef82007-06-17 19:56:36 -05009073lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009074 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009075 struct lpfc_iocbq *piocb,
9076 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009077 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009078{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009079 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009080 long timeleft, timeout_req = 0;
9081 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009082 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009083 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009084 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009085 * If the caller has provided a response iocbq buffer, then context2
9086 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009087 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009088 if (prspiocbq) {
9089 if (piocb->context2)
9090 return IOCB_ERROR;
9091 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009092 }
9093
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009094 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9095 piocb->context_un.wait_queue = &done_q;
9096 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009097
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009098 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009099 if (lpfc_readl(phba->HCregaddr, &creg_val))
9100 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009101 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9102 writel(creg_val, phba->HCregaddr);
9103 readl(phba->HCregaddr); /* flush */
9104 }
9105
James Smart2a9bf3d2010-06-07 15:24:45 -04009106 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9107 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009108 if (retval == IOCB_SUCCESS) {
9109 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009110 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009111 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009112 timeout_req);
dea31012005-04-17 16:05:31 -05009113
James Smart7054a602007-04-25 09:52:34 -04009114 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9115 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009116 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009117 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009119 "0338 IOCB wait timeout error - no "
9120 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009121 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009122 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009124 "0330 IOCB wake NOT set, "
9125 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009126 timeout, (timeleft / jiffies));
9127 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009128 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009129 } else if (retval == IOCB_BUSY) {
9130 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9131 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9132 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9133 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009134 } else {
9135 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009136 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009137 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009138 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009139 }
9140
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009141 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009142 if (lpfc_readl(phba->HCregaddr, &creg_val))
9143 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009144 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9145 writel(creg_val, phba->HCregaddr);
9146 readl(phba->HCregaddr); /* flush */
9147 }
9148
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009149 if (prspiocbq)
9150 piocb->context2 = NULL;
9151
9152 piocb->context_un.wait_queue = NULL;
9153 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009154 return retval;
9155}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009156
James Smarte59058c2008-08-24 21:49:00 -04009157/**
James Smart3621a712009-04-06 18:47:14 -04009158 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009159 * @phba: Pointer to HBA context object.
9160 * @pmboxq: Pointer to driver mailbox object.
9161 * @timeout: Timeout in number of seconds.
9162 *
9163 * This function issues the mailbox to firmware and waits for the
9164 * mailbox command to complete. If the mailbox command is not
9165 * completed within timeout seconds, it returns MBX_TIMEOUT.
9166 * The function waits for the mailbox completion using an
9167 * interruptible wait. If the thread is woken up due to a
9168 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9169 * should not free the mailbox resources, if this function returns
9170 * MBX_TIMEOUT.
9171 * This function will sleep while waiting for mailbox completion.
9172 * So, this function should not be called from any context which
9173 * does not allow sleeping. Due to the same reason, this function
9174 * cannot be called with interrupt disabled.
9175 * This function assumes that the mailbox completion occurs while
9176 * this function sleep. So, this function cannot be called from
9177 * the worker thread which processes mailbox completion.
9178 * This function is called in the context of HBA management
9179 * applications.
9180 * This function returns MBX_SUCCESS when successful.
9181 * This function is called with no lock held.
9182 **/
dea31012005-04-17 16:05:31 -05009183int
James Smart2e0fef82007-06-17 19:56:36 -05009184lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009185 uint32_t timeout)
9186{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009187 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009188 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009189 unsigned long flag;
dea31012005-04-17 16:05:31 -05009190
9191 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009192 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009193 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009194
James Smart495a7142008-06-14 22:52:59 -04009195 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009196 /* setup wake call as IOCB callback */
9197 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9198 /* setup context field to pass wait_queue pointer to wake function */
9199 pmboxq->context1 = &done_q;
9200
dea31012005-04-17 16:05:31 -05009201 /* now issue the command */
9202 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9203
9204 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009205 wait_event_interruptible_timeout(done_q,
9206 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9207 timeout * HZ);
9208
James Smart858c9f62007-06-17 19:56:39 -05009209 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009210 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009211 /*
9212 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9213 * else do not free the resources.
9214 */
James Smartd7c47992010-06-08 18:31:54 -04009215 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009216 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009217 lpfc_sli4_swap_str(phba, pmboxq);
9218 } else {
James Smart7054a602007-04-25 09:52:34 -04009219 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009220 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9221 }
9222 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009223 }
9224
dea31012005-04-17 16:05:31 -05009225 return retval;
9226}
9227
James Smarte59058c2008-08-24 21:49:00 -04009228/**
James Smart3772a992009-05-22 14:50:54 -04009229 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009230 * @phba: Pointer to HBA context.
9231 *
James Smart3772a992009-05-22 14:50:54 -04009232 * This function is called to shutdown the driver's mailbox sub-system.
9233 * It first marks the mailbox sub-system is in a block state to prevent
9234 * the asynchronous mailbox command from issued off the pending mailbox
9235 * command queue. If the mailbox command sub-system shutdown is due to
9236 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9237 * the mailbox sub-system flush routine to forcefully bring down the
9238 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9239 * as with offline or HBA function reset), this routine will wait for the
9240 * outstanding mailbox command to complete before invoking the mailbox
9241 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009242 **/
James Smart3772a992009-05-22 14:50:54 -04009243void
9244lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009245{
James Smart3772a992009-05-22 14:50:54 -04009246 struct lpfc_sli *psli = &phba->sli;
9247 uint8_t actcmd = MBX_HEARTBEAT;
9248 unsigned long timeout;
9249
9250 spin_lock_irq(&phba->hbalock);
9251 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9252 spin_unlock_irq(&phba->hbalock);
9253
9254 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9255 spin_lock_irq(&phba->hbalock);
9256 if (phba->sli.mbox_active)
9257 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
9258 spin_unlock_irq(&phba->hbalock);
9259 /* Determine how long we might wait for the active mailbox
9260 * command to be gracefully completed by firmware.
9261 */
9262 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
9263 1000) + jiffies;
9264 while (phba->sli.mbox_active) {
9265 /* Check active mailbox complete status every 2ms */
9266 msleep(2);
9267 if (time_after(jiffies, timeout))
9268 /* Timeout, let the mailbox flush routine to
9269 * forcefully release active mailbox command
9270 */
9271 break;
9272 }
9273 }
9274 lpfc_sli_mbox_sys_flush(phba);
9275}
9276
9277/**
9278 * lpfc_sli_eratt_read - read sli-3 error attention events
9279 * @phba: Pointer to HBA context.
9280 *
9281 * This function is called to read the SLI3 device error attention registers
9282 * for possible error attention events. The caller must hold the hostlock
9283 * with spin_lock_irq().
9284 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009285 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009286 * Register and returns 0 otherwise.
9287 **/
9288static int
9289lpfc_sli_eratt_read(struct lpfc_hba *phba)
9290{
James Smarted957682007-06-17 19:56:37 -05009291 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009292
James Smart3772a992009-05-22 14:50:54 -04009293 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009294 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9295 goto unplug_err;
9296
James Smart3772a992009-05-22 14:50:54 -04009297 if (ha_copy & HA_ERATT) {
9298 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009299 if (lpfc_sli_read_hs(phba))
9300 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009301
James Smart3772a992009-05-22 14:50:54 -04009302 /* Check if there is a deferred error condition is active */
9303 if ((HS_FFER1 & phba->work_hs) &&
9304 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009305 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009306 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009307 /* Clear all interrupt enable conditions */
9308 writel(0, phba->HCregaddr);
9309 readl(phba->HCregaddr);
9310 }
9311
9312 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009313 phba->work_ha |= HA_ERATT;
9314 /* Indicate polling handles this ERATT */
9315 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009316 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009317 }
James Smart3772a992009-05-22 14:50:54 -04009318 return 0;
James Smart9940b972011-03-11 16:06:12 -05009319
9320unplug_err:
9321 /* Set the driver HS work bitmap */
9322 phba->work_hs |= UNPLUG_ERR;
9323 /* Set the driver HA work bitmap */
9324 phba->work_ha |= HA_ERATT;
9325 /* Indicate polling handles this ERATT */
9326 phba->hba_flag |= HBA_ERATT_HANDLED;
9327 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009328}
9329
James Smarte59058c2008-08-24 21:49:00 -04009330/**
James Smartda0436e2009-05-22 14:51:39 -04009331 * lpfc_sli4_eratt_read - read sli-4 error attention events
9332 * @phba: Pointer to HBA context.
9333 *
9334 * This function is called to read the SLI4 device error attention registers
9335 * for possible error attention events. The caller must hold the hostlock
9336 * with spin_lock_irq().
9337 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009338 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009339 * Register and returns 0 otherwise.
9340 **/
9341static int
9342lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9343{
9344 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009345 uint32_t if_type, portsmphr;
9346 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009347
James Smart2fcee4b2010-12-15 17:57:46 -05009348 /*
9349 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009350 * registers for error attention. This can be changed later.
9351 */
James Smart2fcee4b2010-12-15 17:57:46 -05009352 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9353 switch (if_type) {
9354 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009355 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9356 &uerr_sta_lo) ||
9357 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9358 &uerr_sta_hi)) {
9359 phba->work_hs |= UNPLUG_ERR;
9360 phba->work_ha |= HA_ERATT;
9361 phba->hba_flag |= HBA_ERATT_HANDLED;
9362 return 1;
9363 }
James Smart2fcee4b2010-12-15 17:57:46 -05009364 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9365 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9367 "1423 HBA Unrecoverable error: "
9368 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9369 "ue_mask_lo_reg=0x%x, "
9370 "ue_mask_hi_reg=0x%x\n",
9371 uerr_sta_lo, uerr_sta_hi,
9372 phba->sli4_hba.ue_mask_lo,
9373 phba->sli4_hba.ue_mask_hi);
9374 phba->work_status[0] = uerr_sta_lo;
9375 phba->work_status[1] = uerr_sta_hi;
9376 phba->work_ha |= HA_ERATT;
9377 phba->hba_flag |= HBA_ERATT_HANDLED;
9378 return 1;
9379 }
9380 break;
9381 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009382 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9383 &portstat_reg.word0) ||
9384 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9385 &portsmphr)){
9386 phba->work_hs |= UNPLUG_ERR;
9387 phba->work_ha |= HA_ERATT;
9388 phba->hba_flag |= HBA_ERATT_HANDLED;
9389 return 1;
9390 }
James Smart2fcee4b2010-12-15 17:57:46 -05009391 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9392 phba->work_status[0] =
9393 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9394 phba->work_status[1] =
9395 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9397 "2885 Port Error Detected: "
9398 "port status reg 0x%x, "
9399 "port smphr reg 0x%x, "
9400 "error 1=0x%x, error 2=0x%x\n",
9401 portstat_reg.word0,
9402 portsmphr,
9403 phba->work_status[0],
9404 phba->work_status[1]);
9405 phba->work_ha |= HA_ERATT;
9406 phba->hba_flag |= HBA_ERATT_HANDLED;
9407 return 1;
9408 }
9409 break;
9410 case LPFC_SLI_INTF_IF_TYPE_1:
9411 default:
James Smarta747c9c2009-11-18 15:41:10 -05009412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009413 "2886 HBA Error Attention on unsupported "
9414 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009415 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009416 }
James Smart2fcee4b2010-12-15 17:57:46 -05009417
James Smartda0436e2009-05-22 14:51:39 -04009418 return 0;
9419}
9420
9421/**
James Smart3621a712009-04-06 18:47:14 -04009422 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009423 * @phba: Pointer to HBA context.
9424 *
James Smart3772a992009-05-22 14:50:54 -04009425 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009426 * error attention register bit for error attention events.
9427 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009428 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009429 * Register and returns 0 otherwise.
9430 **/
9431int
9432lpfc_sli_check_eratt(struct lpfc_hba *phba)
9433{
9434 uint32_t ha_copy;
9435
9436 /* If somebody is waiting to handle an eratt, don't process it
9437 * here. The brdkill function will do this.
9438 */
9439 if (phba->link_flag & LS_IGNORE_ERATT)
9440 return 0;
9441
9442 /* Check if interrupt handler handles this ERATT */
9443 spin_lock_irq(&phba->hbalock);
9444 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9445 /* Interrupt handler has handled ERATT */
9446 spin_unlock_irq(&phba->hbalock);
9447 return 0;
9448 }
9449
James Smarta257bf92009-04-06 18:48:10 -04009450 /*
9451 * If there is deferred error attention, do not check for error
9452 * attention
9453 */
9454 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9455 spin_unlock_irq(&phba->hbalock);
9456 return 0;
9457 }
9458
James Smart3772a992009-05-22 14:50:54 -04009459 /* If PCI channel is offline, don't process it */
9460 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04009461 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009462 return 0;
9463 }
9464
9465 switch (phba->sli_rev) {
9466 case LPFC_SLI_REV2:
9467 case LPFC_SLI_REV3:
9468 /* Read chip Host Attention (HA) register */
9469 ha_copy = lpfc_sli_eratt_read(phba);
9470 break;
James Smartda0436e2009-05-22 14:51:39 -04009471 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05009472 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04009473 ha_copy = lpfc_sli4_eratt_read(phba);
9474 break;
James Smart3772a992009-05-22 14:50:54 -04009475 default:
9476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9477 "0299 Invalid SLI revision (%d)\n",
9478 phba->sli_rev);
9479 ha_copy = 0;
9480 break;
James Smart93996272008-08-24 21:50:30 -04009481 }
9482 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009483
9484 return ha_copy;
9485}
9486
9487/**
9488 * lpfc_intr_state_check - Check device state for interrupt handling
9489 * @phba: Pointer to HBA context.
9490 *
9491 * This inline routine checks whether a device or its PCI slot is in a state
9492 * that the interrupt should be handled.
9493 *
9494 * This function returns 0 if the device or the PCI slot is in a state that
9495 * interrupt should be handled, otherwise -EIO.
9496 */
9497static inline int
9498lpfc_intr_state_check(struct lpfc_hba *phba)
9499{
9500 /* If the pci channel is offline, ignore all the interrupts */
9501 if (unlikely(pci_channel_offline(phba->pcidev)))
9502 return -EIO;
9503
9504 /* Update device level interrupt statistics */
9505 phba->sli.slistat.sli_intr++;
9506
9507 /* Ignore all interrupts during initialization. */
9508 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9509 return -EIO;
9510
James Smart93996272008-08-24 21:50:30 -04009511 return 0;
9512}
9513
9514/**
James Smart3772a992009-05-22 14:50:54 -04009515 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04009516 * @irq: Interrupt number.
9517 * @dev_id: The device context pointer.
9518 *
James Smart93996272008-08-24 21:50:30 -04009519 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009520 * service routine when device with SLI-3 interface spec is enabled with
9521 * MSI-X multi-message interrupt mode and there are slow-path events in
9522 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9523 * interrupt mode, this function is called as part of the device-level
9524 * interrupt handler. When the PCI slot is in error recovery or the HBA
9525 * is undergoing initialization, the interrupt handler will not process
9526 * the interrupt. The link attention and ELS ring attention events are
9527 * handled by the worker thread. The interrupt handler signals the worker
9528 * thread and returns for these events. This function is called without
9529 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04009530 * structures.
9531 *
9532 * This function returns IRQ_HANDLED when interrupt is handled else it
9533 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04009534 **/
dea31012005-04-17 16:05:31 -05009535irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009536lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05009537{
James Smart2e0fef82007-06-17 19:56:36 -05009538 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05009539 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009540 uint32_t work_ha_copy;
9541 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05009542 unsigned long iflag;
dea31012005-04-17 16:05:31 -05009543 uint32_t control;
9544
James Smart92d7f7b2007-06-17 19:56:38 -05009545 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05009546 struct lpfc_vport *vport;
9547 struct lpfc_nodelist *ndlp;
9548 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05009549 LPFC_MBOXQ_t *pmb;
9550 int rc;
9551
dea31012005-04-17 16:05:31 -05009552 /*
9553 * Get the driver's phba structure from the dev_id and
9554 * assume the HBA is not interrupting.
9555 */
James Smart93996272008-08-24 21:50:30 -04009556 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05009557
9558 if (unlikely(!phba))
9559 return IRQ_NONE;
9560
dea31012005-04-17 16:05:31 -05009561 /*
James Smart93996272008-08-24 21:50:30 -04009562 * Stuff needs to be attented to when this function is invoked as an
9563 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009564 */
James Smart93996272008-08-24 21:50:30 -04009565 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009566 /* Check device state for handling interrupt */
9567 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009568 return IRQ_NONE;
9569 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05009570 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009571 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9572 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -04009573 /* If somebody is waiting to handle an eratt don't process it
9574 * here. The brdkill function will do this.
9575 */
9576 if (phba->link_flag & LS_IGNORE_ERATT)
9577 ha_copy &= ~HA_ERATT;
9578 /* Check the need for handling ERATT in interrupt handler */
9579 if (ha_copy & HA_ERATT) {
9580 if (phba->hba_flag & HBA_ERATT_HANDLED)
9581 /* ERATT polling has handled ERATT */
9582 ha_copy &= ~HA_ERATT;
9583 else
9584 /* Indicate interrupt handler handles ERATT */
9585 phba->hba_flag |= HBA_ERATT_HANDLED;
9586 }
James Smarta257bf92009-04-06 18:48:10 -04009587
9588 /*
9589 * If there is deferred error attention, do not check for any
9590 * interrupt.
9591 */
9592 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009593 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009594 return IRQ_NONE;
9595 }
9596
James Smart93996272008-08-24 21:50:30 -04009597 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -05009598 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9599 goto unplug_error;
9600
James Smarta747c9c2009-11-18 15:41:10 -05009601 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9602 HC_LAINT_ENA | HC_ERINT_ENA),
9603 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009604 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9605 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009606 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009607 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009608 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009609 } else
9610 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05009611
dea31012005-04-17 16:05:31 -05009612 work_ha_copy = ha_copy & phba->work_ha_mask;
9613
James Smart93996272008-08-24 21:50:30 -04009614 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05009615 if (work_ha_copy & HA_LATT) {
9616 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9617 /*
9618 * Turn off Link Attention interrupts
9619 * until CLEAR_LA done
9620 */
James Smart5b75da22008-12-04 22:39:35 -05009621 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009622 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -05009623 if (lpfc_readl(phba->HCregaddr, &control))
9624 goto unplug_error;
dea31012005-04-17 16:05:31 -05009625 control &= ~HC_LAINT_ENA;
9626 writel(control, phba->HCregaddr);
9627 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009628 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009629 }
9630 else
9631 work_ha_copy &= ~HA_LATT;
9632 }
9633
James Smart93996272008-08-24 21:50:30 -04009634 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05009635 /*
9636 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
9637 * the only slow ring.
9638 */
9639 status = (work_ha_copy &
9640 (HA_RXMASK << (4*LPFC_ELS_RING)));
9641 status >>= (4*LPFC_ELS_RING);
9642 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05009643 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009644 if (lpfc_readl(phba->HCregaddr, &control))
9645 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -04009646
9647 lpfc_debugfs_slow_ring_trc(phba,
9648 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
9649 control, status,
9650 (uint32_t)phba->sli.slistat.sli_intr);
9651
James Smart858c9f62007-06-17 19:56:39 -05009652 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04009653 lpfc_debugfs_slow_ring_trc(phba,
9654 "ISR Disable ring:"
9655 "pwork:x%x hawork:x%x wait:x%x",
9656 phba->work_ha, work_ha_copy,
9657 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009658 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009659
James Smart858c9f62007-06-17 19:56:39 -05009660 control &=
9661 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05009662 writel(control, phba->HCregaddr);
9663 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05009664 }
James Smarta58cbd52007-08-02 11:09:43 -04009665 else {
9666 lpfc_debugfs_slow_ring_trc(phba,
9667 "ISR slow ring: pwork:"
9668 "x%x hawork:x%x wait:x%x",
9669 phba->work_ha, work_ha_copy,
9670 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009671 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009672 }
James Smart5b75da22008-12-04 22:39:35 -05009673 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009674 }
9675 }
James Smart5b75da22008-12-04 22:39:35 -05009676 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009677 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -05009678 if (lpfc_sli_read_hs(phba))
9679 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -04009680 /*
9681 * Check if there is a deferred error condition
9682 * is active
9683 */
9684 if ((HS_FFER1 & phba->work_hs) &&
9685 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009686 HS_FFER6 | HS_FFER7 | HS_FFER8) &
9687 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04009688 phba->hba_flag |= DEFER_ERATT;
9689 /* Clear all interrupt enable conditions */
9690 writel(0, phba->HCregaddr);
9691 readl(phba->HCregaddr);
9692 }
9693 }
9694
James Smart93996272008-08-24 21:50:30 -04009695 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009696 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04009697 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04009698 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05009699 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05009700
9701 /* First check out the status word */
9702 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
9703 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05009704 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05009705 /*
9706 * Stray Mailbox Interrupt, mbxCommand <cmd>
9707 * mbxStatus <status>
9708 */
James Smart09372822008-01-11 01:52:54 -05009709 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05009710 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009711 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05009712 "Interrupt mbxCommand x%x "
9713 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009714 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05009715 pmbox->mbxCommand,
9716 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05009717 /* clear mailbox attention bit */
9718 work_ha_copy &= ~HA_MBATT;
9719 } else {
James Smart97eab632008-04-07 10:16:05 -04009720 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05009721 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05009722 phba->last_completion_time = jiffies;
9723 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05009724 if (pmb->mbox_cmpl) {
9725 lpfc_sli_pcimem_bcopy(mbox, pmbox,
9726 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009727 if (pmb->out_ext_byte_len &&
9728 pmb->context2)
9729 lpfc_sli_pcimem_bcopy(
9730 phba->mbox_ext,
9731 pmb->context2,
9732 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05009733 }
James Smart09372822008-01-11 01:52:54 -05009734 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9735 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9736
9737 lpfc_debugfs_disc_trc(vport,
9738 LPFC_DISC_TRC_MBOX_VPORT,
9739 "MBOX dflt rpi: : "
9740 "status:x%x rpi:x%x",
9741 (uint32_t)pmbox->mbxStatus,
9742 pmbox->un.varWords[0], 0);
9743
9744 if (!pmbox->mbxStatus) {
9745 mp = (struct lpfc_dmabuf *)
9746 (pmb->context1);
9747 ndlp = (struct lpfc_nodelist *)
9748 pmb->context2;
9749
9750 /* Reg_LOGIN of dflt RPI was
9751 * successful. new lets get
9752 * rid of the RPI using the
9753 * same mbox buffer.
9754 */
9755 lpfc_unreg_login(phba,
9756 vport->vpi,
9757 pmbox->un.varWords[0],
9758 pmb);
9759 pmb->mbox_cmpl =
9760 lpfc_mbx_cmpl_dflt_rpi;
9761 pmb->context1 = mp;
9762 pmb->context2 = ndlp;
9763 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04009764 rc = lpfc_sli_issue_mbox(phba,
9765 pmb,
9766 MBX_NOWAIT);
9767 if (rc != MBX_BUSY)
9768 lpfc_printf_log(phba,
9769 KERN_ERR,
9770 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009771 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04009772 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04009773 if (rc != MBX_NOT_FINISHED)
9774 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05009775 }
9776 }
James Smart5b75da22008-12-04 22:39:35 -05009777 spin_lock_irqsave(
9778 &phba->pport->work_port_lock,
9779 iflag);
James Smart09372822008-01-11 01:52:54 -05009780 phba->pport->work_port_events &=
9781 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05009782 spin_unlock_irqrestore(
9783 &phba->pport->work_port_lock,
9784 iflag);
James Smart09372822008-01-11 01:52:54 -05009785 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05009786 }
James Smart97eab632008-04-07 10:16:05 -04009787 } else
James Smart5b75da22008-12-04 22:39:35 -05009788 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009789
James Smart92d7f7b2007-06-17 19:56:38 -05009790 if ((work_ha_copy & HA_MBATT) &&
9791 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05009792send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05009793 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04009794 do {
9795 rc = lpfc_sli_issue_mbox(phba, NULL,
9796 MBX_NOWAIT);
9797 } while (rc == MBX_NOT_FINISHED);
9798 if (rc != MBX_SUCCESS)
9799 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9800 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04009801 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05009802 }
9803
James Smart5b75da22008-12-04 22:39:35 -05009804 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009805 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05009806 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04009807 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05009808 }
James Smart93996272008-08-24 21:50:30 -04009809 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -05009810unplug_error:
9811 spin_unlock_irqrestore(&phba->hbalock, iflag);
9812 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05009813
James Smart3772a992009-05-22 14:50:54 -04009814} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04009815
9816/**
James Smart3772a992009-05-22 14:50:54 -04009817 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04009818 * @irq: Interrupt number.
9819 * @dev_id: The device context pointer.
9820 *
9821 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009822 * service routine when device with SLI-3 interface spec is enabled with
9823 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9824 * ring event in the HBA. However, when the device is enabled with either
9825 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9826 * device-level interrupt handler. When the PCI slot is in error recovery
9827 * or the HBA is undergoing initialization, the interrupt handler will not
9828 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9829 * the intrrupt context. This function is called without any lock held.
9830 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04009831 *
9832 * This function returns IRQ_HANDLED when interrupt is handled else it
9833 * returns IRQ_NONE.
9834 **/
9835irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009836lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04009837{
9838 struct lpfc_hba *phba;
9839 uint32_t ha_copy;
9840 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05009841 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04009842
9843 /* Get the driver's phba structure from the dev_id and
9844 * assume the HBA is not interrupting.
9845 */
9846 phba = (struct lpfc_hba *) dev_id;
9847
9848 if (unlikely(!phba))
9849 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05009850
9851 /*
James Smart93996272008-08-24 21:50:30 -04009852 * Stuff needs to be attented to when this function is invoked as an
9853 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009854 */
James Smart93996272008-08-24 21:50:30 -04009855 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009856 /* Check device state for handling interrupt */
9857 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009858 return IRQ_NONE;
9859 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -05009860 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9861 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04009862 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05009863 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009864 /*
9865 * If there is deferred error attention, do not check for
9866 * any interrupt.
9867 */
9868 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009869 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009870 return IRQ_NONE;
9871 }
James Smart93996272008-08-24 21:50:30 -04009872 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
9873 phba->HAregaddr);
9874 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009875 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009876 } else
9877 ha_copy = phba->ha_copy;
9878
9879 /*
9880 * Process all events on FCP ring. Take the optimized path for FCP IO.
9881 */
9882 ha_copy &= ~(phba->work_ha_mask);
9883
9884 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05009885 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05009886 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05009887 lpfc_sli_handle_fast_ring_event(phba,
9888 &phba->sli.ring[LPFC_FCP_RING],
9889 status);
James Smarta4bc3372006-12-02 13:34:16 -05009890
9891 if (phba->cfg_multi_ring_support == 2) {
9892 /*
James Smart93996272008-08-24 21:50:30 -04009893 * Process all events on extra ring. Take the optimized path
9894 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05009895 */
James Smart93996272008-08-24 21:50:30 -04009896 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05009897 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05009898 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05009899 lpfc_sli_handle_fast_ring_event(phba,
9900 &phba->sli.ring[LPFC_EXTRA_RING],
9901 status);
9902 }
9903 }
dea31012005-04-17 16:05:31 -05009904 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009905} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05009906
James Smart93996272008-08-24 21:50:30 -04009907/**
James Smart3772a992009-05-22 14:50:54 -04009908 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04009909 * @irq: Interrupt number.
9910 * @dev_id: The device context pointer.
9911 *
James Smart3772a992009-05-22 14:50:54 -04009912 * This function is the HBA device-level interrupt handler to device with
9913 * SLI-3 interface spec, called from the PCI layer when either MSI or
9914 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
9915 * requires driver attention. This function invokes the slow-path interrupt
9916 * attention handling function and fast-path interrupt attention handling
9917 * function in turn to process the relevant HBA attention events. This
9918 * function is called without any lock held. It gets the hbalock to access
9919 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04009920 *
9921 * This function returns IRQ_HANDLED when interrupt is handled, else it
9922 * returns IRQ_NONE.
9923 **/
9924irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009925lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04009926{
9927 struct lpfc_hba *phba;
9928 irqreturn_t sp_irq_rc, fp_irq_rc;
9929 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05009930 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04009931
9932 /*
9933 * Get the driver's phba structure from the dev_id and
9934 * assume the HBA is not interrupting.
9935 */
9936 phba = (struct lpfc_hba *) dev_id;
9937
9938 if (unlikely(!phba))
9939 return IRQ_NONE;
9940
James Smart3772a992009-05-22 14:50:54 -04009941 /* Check device state for handling interrupt */
9942 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009943 return IRQ_NONE;
9944
9945 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05009946 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
9947 spin_unlock(&phba->hbalock);
9948 return IRQ_HANDLED;
9949 }
9950
James Smart93996272008-08-24 21:50:30 -04009951 if (unlikely(!phba->ha_copy)) {
9952 spin_unlock(&phba->hbalock);
9953 return IRQ_NONE;
9954 } else if (phba->ha_copy & HA_ERATT) {
9955 if (phba->hba_flag & HBA_ERATT_HANDLED)
9956 /* ERATT polling has handled ERATT */
9957 phba->ha_copy &= ~HA_ERATT;
9958 else
9959 /* Indicate interrupt handler handles ERATT */
9960 phba->hba_flag |= HBA_ERATT_HANDLED;
9961 }
9962
James Smarta257bf92009-04-06 18:48:10 -04009963 /*
9964 * If there is deferred error attention, do not check for any interrupt.
9965 */
9966 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02009967 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04009968 return IRQ_NONE;
9969 }
9970
James Smart93996272008-08-24 21:50:30 -04009971 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -05009972 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
9973 spin_unlock(&phba->hbalock);
9974 return IRQ_HANDLED;
9975 }
James Smarta747c9c2009-11-18 15:41:10 -05009976 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9977 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9978 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009979 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009980 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009981 readl(phba->HAregaddr); /* flush */
9982 spin_unlock(&phba->hbalock);
9983
9984 /*
9985 * Invokes slow-path host attention interrupt handling as appropriate.
9986 */
9987
9988 /* status of events with mailbox and link attention */
9989 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9990
9991 /* status of events with ELS ring */
9992 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
9993 status2 >>= (4*LPFC_ELS_RING);
9994
9995 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009996 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009997 else
9998 sp_irq_rc = IRQ_NONE;
9999
10000 /*
10001 * Invoke fast-path host attention interrupt handling as appropriate.
10002 */
10003
10004 /* status of events with FCP ring */
10005 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10006 status1 >>= (4*LPFC_FCP_RING);
10007
10008 /* status of events with extra ring */
10009 if (phba->cfg_multi_ring_support == 2) {
10010 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10011 status2 >>= (4*LPFC_EXTRA_RING);
10012 } else
10013 status2 = 0;
10014
10015 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010016 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010017 else
10018 fp_irq_rc = IRQ_NONE;
10019
10020 /* Return device-level interrupt handling status */
10021 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010022} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010023
10024/**
10025 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10026 * @phba: pointer to lpfc hba data structure.
10027 *
10028 * This routine is invoked by the worker thread to process all the pending
10029 * SLI4 FCP abort XRI events.
10030 **/
10031void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10032{
10033 struct lpfc_cq_event *cq_event;
10034
10035 /* First, declare the fcp xri abort event has been handled */
10036 spin_lock_irq(&phba->hbalock);
10037 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10038 spin_unlock_irq(&phba->hbalock);
10039 /* Now, handle all the fcp xri abort events */
10040 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10041 /* Get the first event from the head of the event queue */
10042 spin_lock_irq(&phba->hbalock);
10043 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10044 cq_event, struct lpfc_cq_event, list);
10045 spin_unlock_irq(&phba->hbalock);
10046 /* Notify aborted XRI for FCP work queue */
10047 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10048 /* Free the event processed back to the free pool */
10049 lpfc_sli4_cq_event_release(phba, cq_event);
10050 }
10051}
10052
10053/**
10054 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10055 * @phba: pointer to lpfc hba data structure.
10056 *
10057 * This routine is invoked by the worker thread to process all the pending
10058 * SLI4 els abort xri events.
10059 **/
10060void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10061{
10062 struct lpfc_cq_event *cq_event;
10063
10064 /* First, declare the els xri abort event has been handled */
10065 spin_lock_irq(&phba->hbalock);
10066 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10067 spin_unlock_irq(&phba->hbalock);
10068 /* Now, handle all the els xri abort events */
10069 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10070 /* Get the first event from the head of the event queue */
10071 spin_lock_irq(&phba->hbalock);
10072 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10073 cq_event, struct lpfc_cq_event, list);
10074 spin_unlock_irq(&phba->hbalock);
10075 /* Notify aborted XRI for ELS work queue */
10076 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10077 /* Free the event processed back to the free pool */
10078 lpfc_sli4_cq_event_release(phba, cq_event);
10079 }
10080}
10081
James Smart341af102010-01-26 23:07:37 -050010082/**
10083 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10084 * @phba: pointer to lpfc hba data structure
10085 * @pIocbIn: pointer to the rspiocbq
10086 * @pIocbOut: pointer to the cmdiocbq
10087 * @wcqe: pointer to the complete wcqe
10088 *
10089 * This routine transfers the fields of a command iocbq to a response iocbq
10090 * by copying all the IOCB fields from command iocbq and transferring the
10091 * completion status information from the complete wcqe.
10092 **/
James Smart4f774512009-05-22 14:52:35 -040010093static void
James Smart341af102010-01-26 23:07:37 -050010094lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10095 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010096 struct lpfc_iocbq *pIocbOut,
10097 struct lpfc_wcqe_complete *wcqe)
10098{
James Smart341af102010-01-26 23:07:37 -050010099 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010100 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10101
10102 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10103 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010104 /* Map WCQE parameters into irspiocb parameters */
10105 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10106 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10107 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10108 pIocbIn->iocb.un.fcpi.fcpi_parm =
10109 pIocbOut->iocb.un.fcpi.fcpi_parm -
10110 wcqe->total_data_placed;
10111 else
10112 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010113 else {
James Smart4f774512009-05-22 14:52:35 -040010114 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010115 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10116 }
James Smart341af102010-01-26 23:07:37 -050010117
10118 /* Pick up HBA exchange busy condition */
10119 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10120 spin_lock_irqsave(&phba->hbalock, iflags);
10121 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10122 spin_unlock_irqrestore(&phba->hbalock, iflags);
10123 }
James Smart4f774512009-05-22 14:52:35 -040010124}
10125
10126/**
James Smart45ed1192009-10-02 15:17:02 -040010127 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10128 * @phba: Pointer to HBA context object.
10129 * @wcqe: Pointer to work-queue completion queue entry.
10130 *
10131 * This routine handles an ELS work-queue completion event and construct
10132 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10133 * discovery engine to handle.
10134 *
10135 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10136 **/
10137static struct lpfc_iocbq *
10138lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10139 struct lpfc_iocbq *irspiocbq)
10140{
10141 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10142 struct lpfc_iocbq *cmdiocbq;
10143 struct lpfc_wcqe_complete *wcqe;
10144 unsigned long iflags;
10145
10146 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10147 spin_lock_irqsave(&phba->hbalock, iflags);
10148 pring->stats.iocb_event++;
10149 /* Look up the ELS command IOCB and create pseudo response IOCB */
10150 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10151 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10152 spin_unlock_irqrestore(&phba->hbalock, iflags);
10153
10154 if (unlikely(!cmdiocbq)) {
10155 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10156 "0386 ELS complete with no corresponding "
10157 "cmdiocb: iotag (%d)\n",
10158 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10159 lpfc_sli_release_iocbq(phba, irspiocbq);
10160 return NULL;
10161 }
10162
10163 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010164 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010165
10166 return irspiocbq;
10167}
10168
10169/**
James Smart04c68492009-05-22 14:52:52 -040010170 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10171 * @phba: Pointer to HBA context object.
10172 * @cqe: Pointer to mailbox completion queue entry.
10173 *
10174 * This routine process a mailbox completion queue entry with asynchrous
10175 * event.
10176 *
10177 * Return: true if work posted to worker thread, otherwise false.
10178 **/
10179static bool
10180lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10181{
10182 struct lpfc_cq_event *cq_event;
10183 unsigned long iflags;
10184
10185 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10186 "0392 Async Event: word0:x%x, word1:x%x, "
10187 "word2:x%x, word3:x%x\n", mcqe->word0,
10188 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10189
10190 /* Allocate a new internal CQ_EVENT entry */
10191 cq_event = lpfc_sli4_cq_event_alloc(phba);
10192 if (!cq_event) {
10193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10194 "0394 Failed to allocate CQ_EVENT entry\n");
10195 return false;
10196 }
10197
10198 /* Move the CQE into an asynchronous event entry */
10199 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10200 spin_lock_irqsave(&phba->hbalock, iflags);
10201 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10202 /* Set the async event flag */
10203 phba->hba_flag |= ASYNC_EVENT;
10204 spin_unlock_irqrestore(&phba->hbalock, iflags);
10205
10206 return true;
10207}
10208
10209/**
10210 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10211 * @phba: Pointer to HBA context object.
10212 * @cqe: Pointer to mailbox completion queue entry.
10213 *
10214 * This routine process a mailbox completion queue entry with mailbox
10215 * completion event.
10216 *
10217 * Return: true if work posted to worker thread, otherwise false.
10218 **/
10219static bool
10220lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10221{
10222 uint32_t mcqe_status;
10223 MAILBOX_t *mbox, *pmbox;
10224 struct lpfc_mqe *mqe;
10225 struct lpfc_vport *vport;
10226 struct lpfc_nodelist *ndlp;
10227 struct lpfc_dmabuf *mp;
10228 unsigned long iflags;
10229 LPFC_MBOXQ_t *pmb;
10230 bool workposted = false;
10231 int rc;
10232
10233 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10234 if (!bf_get(lpfc_trailer_completed, mcqe))
10235 goto out_no_mqe_complete;
10236
10237 /* Get the reference to the active mbox command */
10238 spin_lock_irqsave(&phba->hbalock, iflags);
10239 pmb = phba->sli.mbox_active;
10240 if (unlikely(!pmb)) {
10241 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10242 "1832 No pending MBOX command to handle\n");
10243 spin_unlock_irqrestore(&phba->hbalock, iflags);
10244 goto out_no_mqe_complete;
10245 }
10246 spin_unlock_irqrestore(&phba->hbalock, iflags);
10247 mqe = &pmb->u.mqe;
10248 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10249 mbox = phba->mbox;
10250 vport = pmb->vport;
10251
10252 /* Reset heartbeat timer */
10253 phba->last_completion_time = jiffies;
10254 del_timer(&phba->sli.mbox_tmo);
10255
10256 /* Move mbox data to caller's mailbox region, do endian swapping */
10257 if (pmb->mbox_cmpl && mbox)
10258 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
10259 /* Set the mailbox status with SLI4 range 0x4000 */
10260 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10261 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
10262 bf_set(lpfc_mqe_status, mqe,
10263 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10264
10265 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10266 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10267 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10268 "MBOX dflt rpi: status:x%x rpi:x%x",
10269 mcqe_status,
10270 pmbox->un.varWords[0], 0);
10271 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10272 mp = (struct lpfc_dmabuf *)(pmb->context1);
10273 ndlp = (struct lpfc_nodelist *)pmb->context2;
10274 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10275 * RID of the PPI using the same mbox buffer.
10276 */
10277 lpfc_unreg_login(phba, vport->vpi,
10278 pmbox->un.varWords[0], pmb);
10279 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10280 pmb->context1 = mp;
10281 pmb->context2 = ndlp;
10282 pmb->vport = vport;
10283 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10284 if (rc != MBX_BUSY)
10285 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10286 LOG_SLI, "0385 rc should "
10287 "have been MBX_BUSY\n");
10288 if (rc != MBX_NOT_FINISHED)
10289 goto send_current_mbox;
10290 }
10291 }
10292 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10293 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10294 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10295
10296 /* There is mailbox completion work to do */
10297 spin_lock_irqsave(&phba->hbalock, iflags);
10298 __lpfc_mbox_cmpl_put(phba, pmb);
10299 phba->work_ha |= HA_MBATT;
10300 spin_unlock_irqrestore(&phba->hbalock, iflags);
10301 workposted = true;
10302
10303send_current_mbox:
10304 spin_lock_irqsave(&phba->hbalock, iflags);
10305 /* Release the mailbox command posting token */
10306 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10307 /* Setting active mailbox pointer need to be in sync to flag clear */
10308 phba->sli.mbox_active = NULL;
10309 spin_unlock_irqrestore(&phba->hbalock, iflags);
10310 /* Wake up worker thread to post the next pending mailbox command */
10311 lpfc_worker_wake_up(phba);
10312out_no_mqe_complete:
10313 if (bf_get(lpfc_trailer_consumed, mcqe))
10314 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10315 return workposted;
10316}
10317
10318/**
10319 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10320 * @phba: Pointer to HBA context object.
10321 * @cqe: Pointer to mailbox completion queue entry.
10322 *
10323 * This routine process a mailbox completion queue entry, it invokes the
10324 * proper mailbox complete handling or asynchrous event handling routine
10325 * according to the MCQE's async bit.
10326 *
10327 * Return: true if work posted to worker thread, otherwise false.
10328 **/
10329static bool
10330lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10331{
10332 struct lpfc_mcqe mcqe;
10333 bool workposted;
10334
10335 /* Copy the mailbox MCQE and convert endian order as needed */
10336 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10337
10338 /* Invoke the proper event handling routine */
10339 if (!bf_get(lpfc_trailer_async, &mcqe))
10340 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10341 else
10342 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10343 return workposted;
10344}
10345
10346/**
James Smart4f774512009-05-22 14:52:35 -040010347 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10348 * @phba: Pointer to HBA context object.
10349 * @wcqe: Pointer to work-queue completion queue entry.
10350 *
10351 * This routine handles an ELS work-queue completion event.
10352 *
10353 * Return: true if work posted to worker thread, otherwise false.
10354 **/
10355static bool
10356lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10357 struct lpfc_wcqe_complete *wcqe)
10358{
James Smart4f774512009-05-22 14:52:35 -040010359 struct lpfc_iocbq *irspiocbq;
10360 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010361 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010362
James Smart45ed1192009-10-02 15:17:02 -040010363 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010364 irspiocbq = lpfc_sli_get_iocbq(phba);
10365 if (!irspiocbq) {
10366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010367 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10368 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10369 pring->txq_cnt, phba->iocb_cnt,
10370 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10371 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010372 return false;
James Smart4f774512009-05-22 14:52:35 -040010373 }
James Smart4f774512009-05-22 14:52:35 -040010374
James Smart45ed1192009-10-02 15:17:02 -040010375 /* Save off the slow-path queue event for work thread to process */
10376 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010377 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010378 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010379 &phba->sli4_hba.sp_queue_event);
10380 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010381 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010382
James Smart45ed1192009-10-02 15:17:02 -040010383 return true;
James Smart4f774512009-05-22 14:52:35 -040010384}
10385
10386/**
10387 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10388 * @phba: Pointer to HBA context object.
10389 * @wcqe: Pointer to work-queue completion queue entry.
10390 *
10391 * This routine handles slow-path WQ entry comsumed event by invoking the
10392 * proper WQ release routine to the slow-path WQ.
10393 **/
10394static void
10395lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10396 struct lpfc_wcqe_release *wcqe)
10397{
10398 /* Check for the slow-path ELS work queue */
10399 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10400 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10401 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10402 else
10403 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10404 "2579 Slow-path wqe consume event carries "
10405 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10406 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10407 phba->sli4_hba.els_wq->queue_id);
10408}
10409
10410/**
10411 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10412 * @phba: Pointer to HBA context object.
10413 * @cq: Pointer to a WQ completion queue.
10414 * @wcqe: Pointer to work-queue completion queue entry.
10415 *
10416 * This routine handles an XRI abort event.
10417 *
10418 * Return: true if work posted to worker thread, otherwise false.
10419 **/
10420static bool
10421lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10422 struct lpfc_queue *cq,
10423 struct sli4_wcqe_xri_aborted *wcqe)
10424{
10425 bool workposted = false;
10426 struct lpfc_cq_event *cq_event;
10427 unsigned long iflags;
10428
10429 /* Allocate a new internal CQ_EVENT entry */
10430 cq_event = lpfc_sli4_cq_event_alloc(phba);
10431 if (!cq_event) {
10432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10433 "0602 Failed to allocate CQ_EVENT entry\n");
10434 return false;
10435 }
10436
10437 /* Move the CQE into the proper xri abort event list */
10438 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10439 switch (cq->subtype) {
10440 case LPFC_FCP:
10441 spin_lock_irqsave(&phba->hbalock, iflags);
10442 list_add_tail(&cq_event->list,
10443 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10444 /* Set the fcp xri abort event flag */
10445 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10446 spin_unlock_irqrestore(&phba->hbalock, iflags);
10447 workposted = true;
10448 break;
10449 case LPFC_ELS:
10450 spin_lock_irqsave(&phba->hbalock, iflags);
10451 list_add_tail(&cq_event->list,
10452 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10453 /* Set the els xri abort event flag */
10454 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10455 spin_unlock_irqrestore(&phba->hbalock, iflags);
10456 workposted = true;
10457 break;
10458 default:
10459 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10460 "0603 Invalid work queue CQE subtype (x%x)\n",
10461 cq->subtype);
10462 workposted = false;
10463 break;
10464 }
10465 return workposted;
10466}
10467
10468/**
James Smart4d9ab992009-10-02 15:16:39 -040010469 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040010470 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040010471 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010472 *
James Smart4d9ab992009-10-02 15:16:39 -040010473 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010474 *
10475 * Return: true if work posted to worker thread, otherwise false.
10476 **/
10477static bool
James Smart4d9ab992009-10-02 15:16:39 -040010478lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10479{
10480 bool workposted = false;
10481 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10482 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10483 struct hbq_dmabuf *dma_buf;
10484 uint32_t status;
10485 unsigned long iflags;
10486
James Smart4d9ab992009-10-02 15:16:39 -040010487 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
10488 goto out;
10489
10490 status = bf_get(lpfc_rcqe_status, rcqe);
10491 switch (status) {
10492 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10494 "2537 Receive Frame Truncated!!\n");
10495 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050010496 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040010497 spin_lock_irqsave(&phba->hbalock, iflags);
10498 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10499 if (!dma_buf) {
10500 spin_unlock_irqrestore(&phba->hbalock, iflags);
10501 goto out;
10502 }
10503 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10504 /* save off the frame for the word thread to process */
10505 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010506 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040010507 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040010508 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040010509 spin_unlock_irqrestore(&phba->hbalock, iflags);
10510 workposted = true;
10511 break;
10512 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10513 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10514 /* Post more buffers if possible */
10515 spin_lock_irqsave(&phba->hbalock, iflags);
10516 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10517 spin_unlock_irqrestore(&phba->hbalock, iflags);
10518 workposted = true;
10519 break;
10520 }
10521out:
10522 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040010523}
10524
10525/**
10526 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10527 * @phba: Pointer to HBA context object.
10528 * @cq: Pointer to the completion queue.
10529 * @wcqe: Pointer to a completion queue entry.
10530 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010531 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040010532 * entry.
10533 *
10534 * Return: true if work posted to worker thread, otherwise false.
10535 **/
10536static bool
10537lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040010538 struct lpfc_cqe *cqe)
10539{
James Smart45ed1192009-10-02 15:17:02 -040010540 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040010541 bool workposted = false;
10542
10543 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040010544 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040010545
10546 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040010547 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040010548 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040010549 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040010550 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010551 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010552 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010553 break;
10554 case CQE_CODE_RELEASE_WQE:
10555 /* Process the WQ release event */
10556 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010557 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010558 break;
10559 case CQE_CODE_XRI_ABORTED:
10560 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010561 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010562 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040010563 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010564 break;
James Smart4d9ab992009-10-02 15:16:39 -040010565 case CQE_CODE_RECEIVE:
10566 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040010567 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040010568 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010569 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040010570 break;
James Smart4f774512009-05-22 14:52:35 -040010571 default:
10572 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10573 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040010574 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040010575 break;
10576 }
10577 return workposted;
10578}
10579
10580/**
James Smart4f774512009-05-22 14:52:35 -040010581 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10582 * @phba: Pointer to HBA context object.
10583 * @eqe: Pointer to fast-path event queue entry.
10584 *
10585 * This routine process a event queue entry from the slow-path event queue.
10586 * It will check the MajorCode and MinorCode to determine this is for a
10587 * completion event on a completion queue, if not, an error shall be logged
10588 * and just return. Otherwise, it will get to the corresponding completion
10589 * queue and process all the entries on that completion queue, rearm the
10590 * completion queue, and then return.
10591 *
10592 **/
10593static void
10594lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10595{
10596 struct lpfc_queue *cq = NULL, *childq, *speq;
10597 struct lpfc_cqe *cqe;
10598 bool workposted = false;
10599 int ecount = 0;
10600 uint16_t cqid;
10601
James Smartcb5172e2010-03-15 11:25:07 -040010602 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040010603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10604 "0359 Not a valid slow-path completion "
10605 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010606 bf_get_le32(lpfc_eqe_major_code, eqe),
10607 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010608 return;
10609 }
10610
10611 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010612 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040010613
10614 /* Search for completion queue pointer matching this cqid */
10615 speq = phba->sli4_hba.sp_eq;
10616 list_for_each_entry(childq, &speq->child_list, list) {
10617 if (childq->queue_id == cqid) {
10618 cq = childq;
10619 break;
10620 }
10621 }
10622 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040010623 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10625 "0365 Slow-path CQ identifier "
10626 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040010627 return;
10628 }
10629
10630 /* Process all the entries to the CQ */
10631 switch (cq->type) {
10632 case LPFC_MCQ:
10633 while ((cqe = lpfc_sli4_cq_get(cq))) {
10634 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
10635 if (!(++ecount % LPFC_GET_QE_REL_INT))
10636 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10637 }
10638 break;
10639 case LPFC_WCQ:
10640 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040010641 if (cq->subtype == LPFC_FCP)
10642 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
10643 cqe);
10644 else
10645 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
10646 cqe);
James Smart4f774512009-05-22 14:52:35 -040010647 if (!(++ecount % LPFC_GET_QE_REL_INT))
10648 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10649 }
10650 break;
10651 default:
10652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10653 "0370 Invalid completion queue type (%d)\n",
10654 cq->type);
10655 return;
10656 }
10657
10658 /* Catch the no cq entry condition, log an error */
10659 if (unlikely(ecount == 0))
10660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10661 "0371 No entry from the CQ: identifier "
10662 "(x%x), type (%d)\n", cq->queue_id, cq->type);
10663
10664 /* In any case, flash and re-arm the RCQ */
10665 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10666
10667 /* wake up worker thread if there are works to be done */
10668 if (workposted)
10669 lpfc_worker_wake_up(phba);
10670}
10671
10672/**
10673 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
10674 * @eqe: Pointer to fast-path completion queue entry.
10675 *
10676 * This routine process a fast-path work queue completion entry from fast-path
10677 * event queue for FCP command response completion.
10678 **/
10679static void
10680lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
10681 struct lpfc_wcqe_complete *wcqe)
10682{
10683 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10684 struct lpfc_iocbq *cmdiocbq;
10685 struct lpfc_iocbq irspiocbq;
10686 unsigned long iflags;
10687
10688 spin_lock_irqsave(&phba->hbalock, iflags);
10689 pring->stats.iocb_event++;
10690 spin_unlock_irqrestore(&phba->hbalock, iflags);
10691
10692 /* Check for response status */
10693 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
10694 /* If resource errors reported from HBA, reduce queue
10695 * depth of the SCSI device.
10696 */
10697 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
10698 IOSTAT_LOCAL_REJECT) &&
10699 (wcqe->parameter == IOERR_NO_RESOURCES)) {
10700 phba->lpfc_rampdown_queue_depth(phba);
10701 }
10702 /* Log the error status */
10703 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10704 "0373 FCP complete error: status=x%x, "
10705 "hw_status=x%x, total_data_specified=%d, "
10706 "parameter=x%x, word3=x%x\n",
10707 bf_get(lpfc_wcqe_c_status, wcqe),
10708 bf_get(lpfc_wcqe_c_hw_status, wcqe),
10709 wcqe->total_data_placed, wcqe->parameter,
10710 wcqe->word3);
10711 }
10712
10713 /* Look up the FCP command IOCB and create pseudo response IOCB */
10714 spin_lock_irqsave(&phba->hbalock, iflags);
10715 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10716 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10717 spin_unlock_irqrestore(&phba->hbalock, iflags);
10718 if (unlikely(!cmdiocbq)) {
10719 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10720 "0374 FCP complete with no corresponding "
10721 "cmdiocb: iotag (%d)\n",
10722 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10723 return;
10724 }
10725 if (unlikely(!cmdiocbq->iocb_cmpl)) {
10726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10727 "0375 FCP cmdiocb not callback function "
10728 "iotag: (%d)\n",
10729 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10730 return;
10731 }
10732
10733 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010734 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040010735
James Smart0f65ff62010-02-26 14:14:23 -050010736 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
10737 spin_lock_irqsave(&phba->hbalock, iflags);
10738 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
10739 spin_unlock_irqrestore(&phba->hbalock, iflags);
10740 }
10741
James Smart4f774512009-05-22 14:52:35 -040010742 /* Pass the cmd_iocb and the rsp state to the upper layer */
10743 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
10744}
10745
10746/**
10747 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
10748 * @phba: Pointer to HBA context object.
10749 * @cq: Pointer to completion queue.
10750 * @wcqe: Pointer to work-queue completion queue entry.
10751 *
10752 * This routine handles an fast-path WQ entry comsumed event by invoking the
10753 * proper WQ release routine to the slow-path WQ.
10754 **/
10755static void
10756lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10757 struct lpfc_wcqe_release *wcqe)
10758{
10759 struct lpfc_queue *childwq;
10760 bool wqid_matched = false;
10761 uint16_t fcp_wqid;
10762
10763 /* Check for fast-path FCP work queue release */
10764 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
10765 list_for_each_entry(childwq, &cq->child_list, list) {
10766 if (childwq->queue_id == fcp_wqid) {
10767 lpfc_sli4_wq_release(childwq,
10768 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10769 wqid_matched = true;
10770 break;
10771 }
10772 }
10773 /* Report warning log message if no match found */
10774 if (wqid_matched != true)
10775 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10776 "2580 Fast-path wqe consume event carries "
10777 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
10778}
10779
10780/**
10781 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
10782 * @cq: Pointer to the completion queue.
10783 * @eqe: Pointer to fast-path completion queue entry.
10784 *
10785 * This routine process a fast-path work queue completion entry from fast-path
10786 * event queue for FCP command response completion.
10787 **/
10788static int
10789lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10790 struct lpfc_cqe *cqe)
10791{
10792 struct lpfc_wcqe_release wcqe;
10793 bool workposted = false;
10794
10795 /* Copy the work queue CQE and convert endian order if needed */
10796 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
10797
10798 /* Check and process for different type of WCQE and dispatch */
10799 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
10800 case CQE_CODE_COMPL_WQE:
10801 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040010802 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010803 lpfc_sli4_fp_handle_fcp_wcqe(phba,
10804 (struct lpfc_wcqe_complete *)&wcqe);
10805 break;
10806 case CQE_CODE_RELEASE_WQE:
10807 /* Process the WQ release event */
10808 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
10809 (struct lpfc_wcqe_release *)&wcqe);
10810 break;
10811 case CQE_CODE_XRI_ABORTED:
10812 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010813 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010814 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
10815 (struct sli4_wcqe_xri_aborted *)&wcqe);
10816 break;
10817 default:
10818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10819 "0144 Not a valid WCQE code: x%x\n",
10820 bf_get(lpfc_wcqe_c_code, &wcqe));
10821 break;
10822 }
10823 return workposted;
10824}
10825
10826/**
10827 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
10828 * @phba: Pointer to HBA context object.
10829 * @eqe: Pointer to fast-path event queue entry.
10830 *
10831 * This routine process a event queue entry from the fast-path event queue.
10832 * It will check the MajorCode and MinorCode to determine this is for a
10833 * completion event on a completion queue, if not, an error shall be logged
10834 * and just return. Otherwise, it will get to the corresponding completion
10835 * queue and process all the entries on the completion queue, rearm the
10836 * completion queue, and then return.
10837 **/
10838static void
10839lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
10840 uint32_t fcp_cqidx)
10841{
10842 struct lpfc_queue *cq;
10843 struct lpfc_cqe *cqe;
10844 bool workposted = false;
10845 uint16_t cqid;
10846 int ecount = 0;
10847
James Smartcb5172e2010-03-15 11:25:07 -040010848 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040010849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10850 "0366 Not a valid fast-path completion "
10851 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010852 bf_get_le32(lpfc_eqe_major_code, eqe),
10853 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010854 return;
10855 }
10856
10857 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
10858 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040010859 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10860 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10861 "0367 Fast-path completion queue "
10862 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040010863 return;
10864 }
10865
10866 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010867 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040010868 if (unlikely(cqid != cq->queue_id)) {
10869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10870 "0368 Miss-matched fast-path completion "
10871 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
10872 cqid, cq->queue_id);
10873 return;
10874 }
10875
10876 /* Process all the entries to the CQ */
10877 while ((cqe = lpfc_sli4_cq_get(cq))) {
10878 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
10879 if (!(++ecount % LPFC_GET_QE_REL_INT))
10880 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10881 }
10882
10883 /* Catch the no cq entry condition */
10884 if (unlikely(ecount == 0))
10885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10886 "0369 No entry from fast-path completion "
10887 "queue fcpcqid=%d\n", cq->queue_id);
10888
10889 /* In any case, flash and re-arm the CQ */
10890 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10891
10892 /* wake up worker thread if there are works to be done */
10893 if (workposted)
10894 lpfc_worker_wake_up(phba);
10895}
10896
10897static void
10898lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
10899{
10900 struct lpfc_eqe *eqe;
10901
10902 /* walk all the EQ entries and drop on the floor */
10903 while ((eqe = lpfc_sli4_eq_get(eq)))
10904 ;
10905
10906 /* Clear and re-arm the EQ */
10907 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
10908}
10909
10910/**
10911 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
10912 * @irq: Interrupt number.
10913 * @dev_id: The device context pointer.
10914 *
10915 * This function is directly called from the PCI layer as an interrupt
10916 * service routine when device with SLI-4 interface spec is enabled with
10917 * MSI-X multi-message interrupt mode and there are slow-path events in
10918 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10919 * interrupt mode, this function is called as part of the device-level
10920 * interrupt handler. When the PCI slot is in error recovery or the HBA is
10921 * undergoing initialization, the interrupt handler will not process the
10922 * interrupt. The link attention and ELS ring attention events are handled
10923 * by the worker thread. The interrupt handler signals the worker thread
10924 * and returns for these events. This function is called without any lock
10925 * held. It gets the hbalock to access and update SLI data structures.
10926 *
10927 * This function returns IRQ_HANDLED when interrupt is handled else it
10928 * returns IRQ_NONE.
10929 **/
10930irqreturn_t
10931lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
10932{
10933 struct lpfc_hba *phba;
10934 struct lpfc_queue *speq;
10935 struct lpfc_eqe *eqe;
10936 unsigned long iflag;
10937 int ecount = 0;
10938
10939 /*
10940 * Get the driver's phba structure from the dev_id
10941 */
10942 phba = (struct lpfc_hba *)dev_id;
10943
10944 if (unlikely(!phba))
10945 return IRQ_NONE;
10946
10947 /* Get to the EQ struct associated with this vector */
10948 speq = phba->sli4_hba.sp_eq;
10949
10950 /* Check device state for handling interrupt */
10951 if (unlikely(lpfc_intr_state_check(phba))) {
10952 /* Check again for link_state with lock held */
10953 spin_lock_irqsave(&phba->hbalock, iflag);
10954 if (phba->link_state < LPFC_LINK_DOWN)
10955 /* Flush, clear interrupt, and rearm the EQ */
10956 lpfc_sli4_eq_flush(phba, speq);
10957 spin_unlock_irqrestore(&phba->hbalock, iflag);
10958 return IRQ_NONE;
10959 }
10960
10961 /*
10962 * Process all the event on FCP slow-path EQ
10963 */
10964 while ((eqe = lpfc_sli4_eq_get(speq))) {
10965 lpfc_sli4_sp_handle_eqe(phba, eqe);
10966 if (!(++ecount % LPFC_GET_QE_REL_INT))
10967 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10968 }
10969
10970 /* Always clear and re-arm the slow-path EQ */
10971 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10972
10973 /* Catch the no cq entry condition */
10974 if (unlikely(ecount == 0)) {
10975 if (phba->intr_type == MSIX)
10976 /* MSI-X treated interrupt served as no EQ share INT */
10977 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10978 "0357 MSI-X interrupt with no EQE\n");
10979 else
10980 /* Non MSI-X treated on interrupt as EQ share INT */
10981 return IRQ_NONE;
10982 }
10983
10984 return IRQ_HANDLED;
10985} /* lpfc_sli4_sp_intr_handler */
10986
10987/**
10988 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10989 * @irq: Interrupt number.
10990 * @dev_id: The device context pointer.
10991 *
10992 * This function is directly called from the PCI layer as an interrupt
10993 * service routine when device with SLI-4 interface spec is enabled with
10994 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10995 * ring event in the HBA. However, when the device is enabled with either
10996 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10997 * device-level interrupt handler. When the PCI slot is in error recovery
10998 * or the HBA is undergoing initialization, the interrupt handler will not
10999 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11000 * the intrrupt context. This function is called without any lock held.
11001 * It gets the hbalock to access and update SLI data structures. Note that,
11002 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11003 * equal to that of FCP CQ index.
11004 *
11005 * This function returns IRQ_HANDLED when interrupt is handled else it
11006 * returns IRQ_NONE.
11007 **/
11008irqreturn_t
11009lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11010{
11011 struct lpfc_hba *phba;
11012 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11013 struct lpfc_queue *fpeq;
11014 struct lpfc_eqe *eqe;
11015 unsigned long iflag;
11016 int ecount = 0;
11017 uint32_t fcp_eqidx;
11018
11019 /* Get the driver's phba structure from the dev_id */
11020 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11021 phba = fcp_eq_hdl->phba;
11022 fcp_eqidx = fcp_eq_hdl->idx;
11023
11024 if (unlikely(!phba))
11025 return IRQ_NONE;
11026
11027 /* Get to the EQ struct associated with this vector */
11028 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11029
11030 /* Check device state for handling interrupt */
11031 if (unlikely(lpfc_intr_state_check(phba))) {
11032 /* Check again for link_state with lock held */
11033 spin_lock_irqsave(&phba->hbalock, iflag);
11034 if (phba->link_state < LPFC_LINK_DOWN)
11035 /* Flush, clear interrupt, and rearm the EQ */
11036 lpfc_sli4_eq_flush(phba, fpeq);
11037 spin_unlock_irqrestore(&phba->hbalock, iflag);
11038 return IRQ_NONE;
11039 }
11040
11041 /*
11042 * Process all the event on FCP fast-path EQ
11043 */
11044 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11045 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
11046 if (!(++ecount % LPFC_GET_QE_REL_INT))
11047 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11048 }
11049
11050 /* Always clear and re-arm the fast-path EQ */
11051 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11052
11053 if (unlikely(ecount == 0)) {
11054 if (phba->intr_type == MSIX)
11055 /* MSI-X treated interrupt served as no EQ share INT */
11056 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11057 "0358 MSI-X interrupt with no EQE\n");
11058 else
11059 /* Non MSI-X treated on interrupt as EQ share INT */
11060 return IRQ_NONE;
11061 }
11062
11063 return IRQ_HANDLED;
11064} /* lpfc_sli4_fp_intr_handler */
11065
11066/**
11067 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11068 * @irq: Interrupt number.
11069 * @dev_id: The device context pointer.
11070 *
11071 * This function is the device-level interrupt handler to device with SLI-4
11072 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11073 * interrupt mode is enabled and there is an event in the HBA which requires
11074 * driver attention. This function invokes the slow-path interrupt attention
11075 * handling function and fast-path interrupt attention handling function in
11076 * turn to process the relevant HBA attention events. This function is called
11077 * without any lock held. It gets the hbalock to access and update SLI data
11078 * structures.
11079 *
11080 * This function returns IRQ_HANDLED when interrupt is handled, else it
11081 * returns IRQ_NONE.
11082 **/
11083irqreturn_t
11084lpfc_sli4_intr_handler(int irq, void *dev_id)
11085{
11086 struct lpfc_hba *phba;
11087 irqreturn_t sp_irq_rc, fp_irq_rc;
11088 bool fp_handled = false;
11089 uint32_t fcp_eqidx;
11090
11091 /* Get the driver's phba structure from the dev_id */
11092 phba = (struct lpfc_hba *)dev_id;
11093
11094 if (unlikely(!phba))
11095 return IRQ_NONE;
11096
11097 /*
11098 * Invokes slow-path host attention interrupt handling as appropriate.
11099 */
11100 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11101
11102 /*
11103 * Invoke fast-path host attention interrupt handling as appropriate.
11104 */
11105 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11106 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11107 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11108 if (fp_irq_rc == IRQ_HANDLED)
11109 fp_handled |= true;
11110 }
11111
11112 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11113} /* lpfc_sli4_intr_handler */
11114
11115/**
11116 * lpfc_sli4_queue_free - free a queue structure and associated memory
11117 * @queue: The queue structure to free.
11118 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011119 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011120 * the host resident queue. This function must be called after destroying the
11121 * queue on the HBA.
11122 **/
11123void
11124lpfc_sli4_queue_free(struct lpfc_queue *queue)
11125{
11126 struct lpfc_dmabuf *dmabuf;
11127
11128 if (!queue)
11129 return;
11130
11131 while (!list_empty(&queue->page_list)) {
11132 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11133 list);
James Smart49198b32010-04-06 15:04:33 -040011134 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011135 dmabuf->virt, dmabuf->phys);
11136 kfree(dmabuf);
11137 }
11138 kfree(queue);
11139 return;
11140}
11141
11142/**
11143 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11144 * @phba: The HBA that this queue is being created on.
11145 * @entry_size: The size of each queue entry for this queue.
11146 * @entry count: The number of entries that this queue will handle.
11147 *
11148 * This function allocates a queue structure and the DMAable memory used for
11149 * the host resident queue. This function must be called before creating the
11150 * queue on the HBA.
11151 **/
11152struct lpfc_queue *
11153lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11154 uint32_t entry_count)
11155{
11156 struct lpfc_queue *queue;
11157 struct lpfc_dmabuf *dmabuf;
11158 int x, total_qe_count;
11159 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011160 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011161
James Smartcb5172e2010-03-15 11:25:07 -040011162 if (!phba->sli4_hba.pc_sli4_params.supported)
11163 hw_page_size = SLI4_PAGE_SIZE;
11164
James Smart4f774512009-05-22 14:52:35 -040011165 queue = kzalloc(sizeof(struct lpfc_queue) +
11166 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11167 if (!queue)
11168 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011169 queue->page_count = (ALIGN(entry_size * entry_count,
11170 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011171 INIT_LIST_HEAD(&queue->list);
11172 INIT_LIST_HEAD(&queue->page_list);
11173 INIT_LIST_HEAD(&queue->child_list);
11174 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11175 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11176 if (!dmabuf)
11177 goto out_fail;
11178 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011179 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011180 GFP_KERNEL);
11181 if (!dmabuf->virt) {
11182 kfree(dmabuf);
11183 goto out_fail;
11184 }
James Smartcb5172e2010-03-15 11:25:07 -040011185 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011186 dmabuf->buffer_tag = x;
11187 list_add_tail(&dmabuf->list, &queue->page_list);
11188 /* initialize queue's entry array */
11189 dma_pointer = dmabuf->virt;
11190 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011191 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011192 total_qe_count++, dma_pointer += entry_size) {
11193 queue->qe[total_qe_count].address = dma_pointer;
11194 }
11195 }
11196 queue->entry_size = entry_size;
11197 queue->entry_count = entry_count;
11198 queue->phba = phba;
11199
11200 return queue;
11201out_fail:
11202 lpfc_sli4_queue_free(queue);
11203 return NULL;
11204}
11205
11206/**
11207 * lpfc_eq_create - Create an Event Queue on the HBA
11208 * @phba: HBA structure that indicates port to create a queue on.
11209 * @eq: The queue structure to use to create the event queue.
11210 * @imax: The maximum interrupt per second limit.
11211 *
11212 * This function creates an event queue, as detailed in @eq, on a port,
11213 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11214 *
11215 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11216 * is used to get the entry count and entry size that are necessary to
11217 * determine the number of pages to allocate and use for this queue. This
11218 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11219 * event queue. This function is asynchronous and will wait for the mailbox
11220 * command to finish before continuing.
11221 *
11222 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011223 * memory this function will return -ENOMEM. If the queue create mailbox command
11224 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011225 **/
11226uint32_t
11227lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11228{
11229 struct lpfc_mbx_eq_create *eq_create;
11230 LPFC_MBOXQ_t *mbox;
11231 int rc, length, status = 0;
11232 struct lpfc_dmabuf *dmabuf;
11233 uint32_t shdr_status, shdr_add_status;
11234 union lpfc_sli4_cfg_shdr *shdr;
11235 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011236 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11237
11238 if (!phba->sli4_hba.pc_sli4_params.supported)
11239 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011240
11241 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11242 if (!mbox)
11243 return -ENOMEM;
11244 length = (sizeof(struct lpfc_mbx_eq_create) -
11245 sizeof(struct lpfc_sli4_cfg_mhdr));
11246 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11247 LPFC_MBOX_OPCODE_EQ_CREATE,
11248 length, LPFC_SLI4_MBX_EMBED);
11249 eq_create = &mbox->u.mqe.un.eq_create;
11250 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11251 eq->page_count);
11252 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11253 LPFC_EQE_SIZE);
11254 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11255 /* Calculate delay multiper from maximum interrupt per second */
11256 dmult = LPFC_DMULT_CONST/imax - 1;
11257 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11258 dmult);
11259 switch (eq->entry_count) {
11260 default:
11261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11262 "0360 Unsupported EQ count. (%d)\n",
11263 eq->entry_count);
11264 if (eq->entry_count < 256)
11265 return -EINVAL;
11266 /* otherwise default to smallest count (drop through) */
11267 case 256:
11268 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11269 LPFC_EQ_CNT_256);
11270 break;
11271 case 512:
11272 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11273 LPFC_EQ_CNT_512);
11274 break;
11275 case 1024:
11276 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11277 LPFC_EQ_CNT_1024);
11278 break;
11279 case 2048:
11280 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11281 LPFC_EQ_CNT_2048);
11282 break;
11283 case 4096:
11284 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11285 LPFC_EQ_CNT_4096);
11286 break;
11287 }
11288 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011289 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011290 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11291 putPaddrLow(dmabuf->phys);
11292 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11293 putPaddrHigh(dmabuf->phys);
11294 }
11295 mbox->vport = phba->pport;
11296 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11297 mbox->context1 = NULL;
11298 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11299 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11300 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11301 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11302 if (shdr_status || shdr_add_status || rc) {
11303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11304 "2500 EQ_CREATE mailbox failed with "
11305 "status x%x add_status x%x, mbx status x%x\n",
11306 shdr_status, shdr_add_status, rc);
11307 status = -ENXIO;
11308 }
11309 eq->type = LPFC_EQ;
11310 eq->subtype = LPFC_NONE;
11311 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11312 if (eq->queue_id == 0xFFFF)
11313 status = -ENXIO;
11314 eq->host_index = 0;
11315 eq->hba_index = 0;
11316
James Smart8fa38512009-07-19 10:01:03 -040011317 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011318 return status;
11319}
11320
11321/**
11322 * lpfc_cq_create - Create a Completion Queue on the HBA
11323 * @phba: HBA structure that indicates port to create a queue on.
11324 * @cq: The queue structure to use to create the completion queue.
11325 * @eq: The event queue to bind this completion queue to.
11326 *
11327 * This function creates a completion queue, as detailed in @wq, on a port,
11328 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11329 *
11330 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11331 * is used to get the entry count and entry size that are necessary to
11332 * determine the number of pages to allocate and use for this queue. The @eq
11333 * is used to indicate which event queue to bind this completion queue to. This
11334 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11335 * completion queue. This function is asynchronous and will wait for the mailbox
11336 * command to finish before continuing.
11337 *
11338 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011339 * memory this function will return -ENOMEM. If the queue create mailbox command
11340 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011341 **/
11342uint32_t
11343lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11344 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11345{
11346 struct lpfc_mbx_cq_create *cq_create;
11347 struct lpfc_dmabuf *dmabuf;
11348 LPFC_MBOXQ_t *mbox;
11349 int rc, length, status = 0;
11350 uint32_t shdr_status, shdr_add_status;
11351 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011352 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11353
11354 if (!phba->sli4_hba.pc_sli4_params.supported)
11355 hw_page_size = SLI4_PAGE_SIZE;
11356
James Smart4f774512009-05-22 14:52:35 -040011357 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11358 if (!mbox)
11359 return -ENOMEM;
11360 length = (sizeof(struct lpfc_mbx_cq_create) -
11361 sizeof(struct lpfc_sli4_cfg_mhdr));
11362 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11363 LPFC_MBOX_OPCODE_CQ_CREATE,
11364 length, LPFC_SLI4_MBX_EMBED);
11365 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011366 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011367 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11368 cq->page_count);
11369 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11370 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011371 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11372 phba->sli4_hba.pc_sli4_params.cqv);
11373 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011374 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11375 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011376 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11377 eq->queue_id);
11378 } else {
11379 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11380 eq->queue_id);
11381 }
James Smart4f774512009-05-22 14:52:35 -040011382 switch (cq->entry_count) {
11383 default:
11384 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11385 "0361 Unsupported CQ count. (%d)\n",
11386 cq->entry_count);
11387 if (cq->entry_count < 256)
11388 return -EINVAL;
11389 /* otherwise default to smallest count (drop through) */
11390 case 256:
11391 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11392 LPFC_CQ_CNT_256);
11393 break;
11394 case 512:
11395 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11396 LPFC_CQ_CNT_512);
11397 break;
11398 case 1024:
11399 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11400 LPFC_CQ_CNT_1024);
11401 break;
11402 }
11403 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011404 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011405 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11406 putPaddrLow(dmabuf->phys);
11407 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11408 putPaddrHigh(dmabuf->phys);
11409 }
11410 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11411
11412 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011413 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11414 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11415 if (shdr_status || shdr_add_status || rc) {
11416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11417 "2501 CQ_CREATE mailbox failed with "
11418 "status x%x add_status x%x, mbx status x%x\n",
11419 shdr_status, shdr_add_status, rc);
11420 status = -ENXIO;
11421 goto out;
11422 }
11423 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11424 if (cq->queue_id == 0xFFFF) {
11425 status = -ENXIO;
11426 goto out;
11427 }
11428 /* link the cq onto the parent eq child list */
11429 list_add_tail(&cq->list, &eq->child_list);
11430 /* Set up completion queue's type and subtype */
11431 cq->type = type;
11432 cq->subtype = subtype;
11433 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050011434 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011435 cq->host_index = 0;
11436 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040011437
James Smart8fa38512009-07-19 10:01:03 -040011438out:
11439 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011440 return status;
11441}
11442
11443/**
James Smartb19a0612010-04-06 14:48:51 -040011444 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040011445 * @phba: HBA structure that indicates port to create a queue on.
11446 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040011447 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11448 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040011449 *
James Smartb19a0612010-04-06 14:48:51 -040011450 * This function provides failback (fb) functionality when the
11451 * mq_create_ext fails on older FW generations. It's purpose is identical
11452 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040011453 *
James Smartb19a0612010-04-06 14:48:51 -040011454 * This routine cannot fail as all attributes were previously accessed and
11455 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040011456 **/
James Smartb19a0612010-04-06 14:48:51 -040011457static void
11458lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11459 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040011460{
11461 struct lpfc_mbx_mq_create *mq_create;
11462 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040011463 int length;
James Smart04c68492009-05-22 14:52:52 -040011464
James Smart04c68492009-05-22 14:52:52 -040011465 length = (sizeof(struct lpfc_mbx_mq_create) -
11466 sizeof(struct lpfc_sli4_cfg_mhdr));
11467 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11468 LPFC_MBOX_OPCODE_MQ_CREATE,
11469 length, LPFC_SLI4_MBX_EMBED);
11470 mq_create = &mbox->u.mqe.un.mq_create;
11471 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040011472 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040011473 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040011474 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040011475 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11476 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040011477 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011478 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11479 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040011480 break;
11481 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011482 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11483 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040011484 break;
11485 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011486 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11487 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040011488 break;
11489 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011490 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11491 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040011492 break;
11493 }
11494 list_for_each_entry(dmabuf, &mq->page_list, list) {
11495 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040011496 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040011497 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040011498 putPaddrHigh(dmabuf->phys);
11499 }
11500}
11501
11502/**
11503 * lpfc_mq_create - Create a mailbox Queue on the HBA
11504 * @phba: HBA structure that indicates port to create a queue on.
11505 * @mq: The queue structure to use to create the mailbox queue.
11506 * @cq: The completion queue to associate with this cq.
11507 * @subtype: The queue's subtype.
11508 *
11509 * This function creates a mailbox queue, as detailed in @mq, on a port,
11510 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11511 *
11512 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11513 * is used to get the entry count and entry size that are necessary to
11514 * determine the number of pages to allocate and use for this queue. This
11515 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11516 * mailbox queue. This function is asynchronous and will wait for the mailbox
11517 * command to finish before continuing.
11518 *
11519 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011520 * memory this function will return -ENOMEM. If the queue create mailbox command
11521 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040011522 **/
11523int32_t
11524lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11525 struct lpfc_queue *cq, uint32_t subtype)
11526{
11527 struct lpfc_mbx_mq_create *mq_create;
11528 struct lpfc_mbx_mq_create_ext *mq_create_ext;
11529 struct lpfc_dmabuf *dmabuf;
11530 LPFC_MBOXQ_t *mbox;
11531 int rc, length, status = 0;
11532 uint32_t shdr_status, shdr_add_status;
11533 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011534 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040011535
James Smart49198b32010-04-06 15:04:33 -040011536 if (!phba->sli4_hba.pc_sli4_params.supported)
11537 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040011538
11539 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11540 if (!mbox)
11541 return -ENOMEM;
11542 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11543 sizeof(struct lpfc_sli4_cfg_mhdr));
11544 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11545 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11546 length, LPFC_SLI4_MBX_EMBED);
11547
11548 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050011549 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050011550 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11551 &mq_create_ext->u.request, mq->page_count);
11552 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11553 &mq_create_ext->u.request, 1);
11554 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040011555 &mq_create_ext->u.request, 1);
11556 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11557 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050011558 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11559 &mq_create_ext->u.request, 1);
11560 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11561 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040011562 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011563 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11564 phba->sli4_hba.pc_sli4_params.mqv);
11565 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11566 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11567 cq->queue_id);
11568 else
11569 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11570 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040011571 switch (mq->entry_count) {
11572 default:
11573 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11574 "0362 Unsupported MQ count. (%d)\n",
11575 mq->entry_count);
11576 if (mq->entry_count < 16)
11577 return -EINVAL;
11578 /* otherwise default to smallest count (drop through) */
11579 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011580 bf_set(lpfc_mq_context_ring_size,
11581 &mq_create_ext->u.request.context,
11582 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040011583 break;
11584 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011585 bf_set(lpfc_mq_context_ring_size,
11586 &mq_create_ext->u.request.context,
11587 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040011588 break;
11589 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011590 bf_set(lpfc_mq_context_ring_size,
11591 &mq_create_ext->u.request.context,
11592 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040011593 break;
11594 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011595 bf_set(lpfc_mq_context_ring_size,
11596 &mq_create_ext->u.request.context,
11597 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040011598 break;
11599 }
11600 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011601 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040011602 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
11603 putPaddrLow(dmabuf->phys);
11604 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040011605 putPaddrHigh(dmabuf->phys);
11606 }
11607 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040011608 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11609 &mq_create_ext->u.response);
11610 if (rc != MBX_SUCCESS) {
11611 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11612 "2795 MQ_CREATE_EXT failed with "
11613 "status x%x. Failback to MQ_CREATE.\n",
11614 rc);
11615 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
11616 mq_create = &mbox->u.mqe.un.mq_create;
11617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11618 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
11619 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11620 &mq_create->u.response);
11621 }
11622
James Smart04c68492009-05-22 14:52:52 -040011623 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040011624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11626 if (shdr_status || shdr_add_status || rc) {
11627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11628 "2502 MQ_CREATE mailbox failed with "
11629 "status x%x add_status x%x, mbx status x%x\n",
11630 shdr_status, shdr_add_status, rc);
11631 status = -ENXIO;
11632 goto out;
11633 }
James Smart04c68492009-05-22 14:52:52 -040011634 if (mq->queue_id == 0xFFFF) {
11635 status = -ENXIO;
11636 goto out;
11637 }
11638 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050011639 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040011640 mq->subtype = subtype;
11641 mq->host_index = 0;
11642 mq->hba_index = 0;
11643
11644 /* link the mq onto the parent cq child list */
11645 list_add_tail(&mq->list, &cq->child_list);
11646out:
James Smart8fa38512009-07-19 10:01:03 -040011647 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011648 return status;
11649}
11650
11651/**
James Smart4f774512009-05-22 14:52:35 -040011652 * lpfc_wq_create - Create a Work Queue on the HBA
11653 * @phba: HBA structure that indicates port to create a queue on.
11654 * @wq: The queue structure to use to create the work queue.
11655 * @cq: The completion queue to bind this work queue to.
11656 * @subtype: The subtype of the work queue indicating its functionality.
11657 *
11658 * This function creates a work queue, as detailed in @wq, on a port, described
11659 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
11660 *
11661 * The @phba struct is used to send mailbox command to HBA. The @wq struct
11662 * is used to get the entry count and entry size that are necessary to
11663 * determine the number of pages to allocate and use for this queue. The @cq
11664 * is used to indicate which completion queue to bind this work queue to. This
11665 * function will send the WQ_CREATE mailbox command to the HBA to setup the
11666 * work queue. This function is asynchronous and will wait for the mailbox
11667 * command to finish before continuing.
11668 *
11669 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011670 * memory this function will return -ENOMEM. If the queue create mailbox command
11671 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011672 **/
11673uint32_t
11674lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
11675 struct lpfc_queue *cq, uint32_t subtype)
11676{
11677 struct lpfc_mbx_wq_create *wq_create;
11678 struct lpfc_dmabuf *dmabuf;
11679 LPFC_MBOXQ_t *mbox;
11680 int rc, length, status = 0;
11681 uint32_t shdr_status, shdr_add_status;
11682 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011683 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050011684 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040011685
11686 if (!phba->sli4_hba.pc_sli4_params.supported)
11687 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011688
11689 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11690 if (!mbox)
11691 return -ENOMEM;
11692 length = (sizeof(struct lpfc_mbx_wq_create) -
11693 sizeof(struct lpfc_sli4_cfg_mhdr));
11694 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11695 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
11696 length, LPFC_SLI4_MBX_EMBED);
11697 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011698 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011699 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
11700 wq->page_count);
11701 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
11702 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050011703 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11704 phba->sli4_hba.pc_sli4_params.wqv);
11705 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
11706 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
11707 wq->entry_count);
11708 switch (wq->entry_size) {
11709 default:
11710 case 64:
11711 bf_set(lpfc_mbx_wq_create_wqe_size,
11712 &wq_create->u.request_1,
11713 LPFC_WQ_WQE_SIZE_64);
11714 break;
11715 case 128:
11716 bf_set(lpfc_mbx_wq_create_wqe_size,
11717 &wq_create->u.request_1,
11718 LPFC_WQ_WQE_SIZE_128);
11719 break;
11720 }
11721 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
11722 (PAGE_SIZE/SLI4_PAGE_SIZE));
11723 page = wq_create->u.request_1.page;
11724 } else {
11725 page = wq_create->u.request.page;
11726 }
James Smart4f774512009-05-22 14:52:35 -040011727 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011728 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050011729 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
11730 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040011731 }
11732 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11733 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011734 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11735 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11736 if (shdr_status || shdr_add_status || rc) {
11737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11738 "2503 WQ_CREATE mailbox failed with "
11739 "status x%x add_status x%x, mbx status x%x\n",
11740 shdr_status, shdr_add_status, rc);
11741 status = -ENXIO;
11742 goto out;
11743 }
11744 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
11745 if (wq->queue_id == 0xFFFF) {
11746 status = -ENXIO;
11747 goto out;
11748 }
11749 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050011750 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011751 wq->subtype = subtype;
11752 wq->host_index = 0;
11753 wq->hba_index = 0;
11754
11755 /* link the wq onto the parent cq child list */
11756 list_add_tail(&wq->list, &cq->child_list);
11757out:
James Smart8fa38512009-07-19 10:01:03 -040011758 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011759 return status;
11760}
11761
11762/**
11763 * lpfc_rq_create - Create a Receive Queue on the HBA
11764 * @phba: HBA structure that indicates port to create a queue on.
11765 * @hrq: The queue structure to use to create the header receive queue.
11766 * @drq: The queue structure to use to create the data receive queue.
11767 * @cq: The completion queue to bind this work queue to.
11768 *
11769 * This function creates a receive buffer queue pair , as detailed in @hrq and
11770 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
11771 * to the HBA.
11772 *
11773 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
11774 * struct is used to get the entry count that is necessary to determine the
11775 * number of pages to use for this queue. The @cq is used to indicate which
11776 * completion queue to bind received buffers that are posted to these queues to.
11777 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
11778 * receive queue pair. This function is asynchronous and will wait for the
11779 * mailbox command to finish before continuing.
11780 *
11781 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011782 * memory this function will return -ENOMEM. If the queue create mailbox command
11783 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011784 **/
11785uint32_t
11786lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11787 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
11788{
11789 struct lpfc_mbx_rq_create *rq_create;
11790 struct lpfc_dmabuf *dmabuf;
11791 LPFC_MBOXQ_t *mbox;
11792 int rc, length, status = 0;
11793 uint32_t shdr_status, shdr_add_status;
11794 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011795 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11796
11797 if (!phba->sli4_hba.pc_sli4_params.supported)
11798 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011799
11800 if (hrq->entry_count != drq->entry_count)
11801 return -EINVAL;
11802 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11803 if (!mbox)
11804 return -ENOMEM;
11805 length = (sizeof(struct lpfc_mbx_rq_create) -
11806 sizeof(struct lpfc_sli4_cfg_mhdr));
11807 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11808 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11809 length, LPFC_SLI4_MBX_EMBED);
11810 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011811 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11812 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11813 phba->sli4_hba.pc_sli4_params.rqv);
11814 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11815 bf_set(lpfc_rq_context_rqe_count_1,
11816 &rq_create->u.request.context,
11817 hrq->entry_count);
11818 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040011819 bf_set(lpfc_rq_context_rqe_size,
11820 &rq_create->u.request.context,
11821 LPFC_RQE_SIZE_8);
11822 bf_set(lpfc_rq_context_page_size,
11823 &rq_create->u.request.context,
11824 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050011825 } else {
11826 switch (hrq->entry_count) {
11827 default:
11828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11829 "2535 Unsupported RQ count. (%d)\n",
11830 hrq->entry_count);
11831 if (hrq->entry_count < 512)
11832 return -EINVAL;
11833 /* otherwise default to smallest count (drop through) */
11834 case 512:
11835 bf_set(lpfc_rq_context_rqe_count,
11836 &rq_create->u.request.context,
11837 LPFC_RQ_RING_SIZE_512);
11838 break;
11839 case 1024:
11840 bf_set(lpfc_rq_context_rqe_count,
11841 &rq_create->u.request.context,
11842 LPFC_RQ_RING_SIZE_1024);
11843 break;
11844 case 2048:
11845 bf_set(lpfc_rq_context_rqe_count,
11846 &rq_create->u.request.context,
11847 LPFC_RQ_RING_SIZE_2048);
11848 break;
11849 case 4096:
11850 bf_set(lpfc_rq_context_rqe_count,
11851 &rq_create->u.request.context,
11852 LPFC_RQ_RING_SIZE_4096);
11853 break;
11854 }
11855 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11856 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040011857 }
11858 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11859 cq->queue_id);
11860 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11861 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040011862 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011863 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011864 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11865 putPaddrLow(dmabuf->phys);
11866 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11867 putPaddrHigh(dmabuf->phys);
11868 }
11869 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11870 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011871 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11872 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11873 if (shdr_status || shdr_add_status || rc) {
11874 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11875 "2504 RQ_CREATE mailbox failed with "
11876 "status x%x add_status x%x, mbx status x%x\n",
11877 shdr_status, shdr_add_status, rc);
11878 status = -ENXIO;
11879 goto out;
11880 }
11881 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11882 if (hrq->queue_id == 0xFFFF) {
11883 status = -ENXIO;
11884 goto out;
11885 }
11886 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050011887 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011888 hrq->subtype = subtype;
11889 hrq->host_index = 0;
11890 hrq->hba_index = 0;
11891
11892 /* now create the data queue */
11893 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11894 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11895 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050011896 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11897 phba->sli4_hba.pc_sli4_params.rqv);
11898 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11899 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040011900 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050011901 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040011902 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
11903 LPFC_RQE_SIZE_8);
11904 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
11905 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050011906 } else {
11907 switch (drq->entry_count) {
11908 default:
11909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11910 "2536 Unsupported RQ count. (%d)\n",
11911 drq->entry_count);
11912 if (drq->entry_count < 512)
11913 return -EINVAL;
11914 /* otherwise default to smallest count (drop through) */
11915 case 512:
11916 bf_set(lpfc_rq_context_rqe_count,
11917 &rq_create->u.request.context,
11918 LPFC_RQ_RING_SIZE_512);
11919 break;
11920 case 1024:
11921 bf_set(lpfc_rq_context_rqe_count,
11922 &rq_create->u.request.context,
11923 LPFC_RQ_RING_SIZE_1024);
11924 break;
11925 case 2048:
11926 bf_set(lpfc_rq_context_rqe_count,
11927 &rq_create->u.request.context,
11928 LPFC_RQ_RING_SIZE_2048);
11929 break;
11930 case 4096:
11931 bf_set(lpfc_rq_context_rqe_count,
11932 &rq_create->u.request.context,
11933 LPFC_RQ_RING_SIZE_4096);
11934 break;
11935 }
11936 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11937 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040011938 }
11939 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11940 cq->queue_id);
11941 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11942 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040011943 list_for_each_entry(dmabuf, &drq->page_list, list) {
11944 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11945 putPaddrLow(dmabuf->phys);
11946 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11947 putPaddrHigh(dmabuf->phys);
11948 }
11949 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11950 /* The IOCTL status is embedded in the mailbox subheader. */
11951 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11952 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11953 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11954 if (shdr_status || shdr_add_status || rc) {
11955 status = -ENXIO;
11956 goto out;
11957 }
11958 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11959 if (drq->queue_id == 0xFFFF) {
11960 status = -ENXIO;
11961 goto out;
11962 }
11963 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050011964 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011965 drq->subtype = subtype;
11966 drq->host_index = 0;
11967 drq->hba_index = 0;
11968
11969 /* link the header and data RQs onto the parent cq child list */
11970 list_add_tail(&hrq->list, &cq->child_list);
11971 list_add_tail(&drq->list, &cq->child_list);
11972
11973out:
James Smart8fa38512009-07-19 10:01:03 -040011974 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011975 return status;
11976}
11977
11978/**
11979 * lpfc_eq_destroy - Destroy an event Queue on the HBA
11980 * @eq: The queue structure associated with the queue to destroy.
11981 *
11982 * This function destroys a queue, as detailed in @eq by sending an mailbox
11983 * command, specific to the type of queue, to the HBA.
11984 *
11985 * The @eq struct is used to get the queue ID of the queue to destroy.
11986 *
11987 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011988 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011989 **/
11990uint32_t
11991lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
11992{
11993 LPFC_MBOXQ_t *mbox;
11994 int rc, length, status = 0;
11995 uint32_t shdr_status, shdr_add_status;
11996 union lpfc_sli4_cfg_shdr *shdr;
11997
11998 if (!eq)
11999 return -ENODEV;
12000 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12001 if (!mbox)
12002 return -ENOMEM;
12003 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12004 sizeof(struct lpfc_sli4_cfg_mhdr));
12005 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12006 LPFC_MBOX_OPCODE_EQ_DESTROY,
12007 length, LPFC_SLI4_MBX_EMBED);
12008 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12009 eq->queue_id);
12010 mbox->vport = eq->phba->pport;
12011 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12012
12013 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12014 /* The IOCTL status is embedded in the mailbox subheader. */
12015 shdr = (union lpfc_sli4_cfg_shdr *)
12016 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12017 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12018 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12019 if (shdr_status || shdr_add_status || rc) {
12020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12021 "2505 EQ_DESTROY mailbox failed with "
12022 "status x%x add_status x%x, mbx status x%x\n",
12023 shdr_status, shdr_add_status, rc);
12024 status = -ENXIO;
12025 }
12026
12027 /* Remove eq from any list */
12028 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012029 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012030 return status;
12031}
12032
12033/**
12034 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12035 * @cq: The queue structure associated with the queue to destroy.
12036 *
12037 * This function destroys a queue, as detailed in @cq by sending an mailbox
12038 * command, specific to the type of queue, to the HBA.
12039 *
12040 * The @cq struct is used to get the queue ID of the queue to destroy.
12041 *
12042 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012043 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012044 **/
12045uint32_t
12046lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12047{
12048 LPFC_MBOXQ_t *mbox;
12049 int rc, length, status = 0;
12050 uint32_t shdr_status, shdr_add_status;
12051 union lpfc_sli4_cfg_shdr *shdr;
12052
12053 if (!cq)
12054 return -ENODEV;
12055 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12056 if (!mbox)
12057 return -ENOMEM;
12058 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12059 sizeof(struct lpfc_sli4_cfg_mhdr));
12060 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12061 LPFC_MBOX_OPCODE_CQ_DESTROY,
12062 length, LPFC_SLI4_MBX_EMBED);
12063 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12064 cq->queue_id);
12065 mbox->vport = cq->phba->pport;
12066 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12067 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12068 /* The IOCTL status is embedded in the mailbox subheader. */
12069 shdr = (union lpfc_sli4_cfg_shdr *)
12070 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12071 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12072 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12073 if (shdr_status || shdr_add_status || rc) {
12074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12075 "2506 CQ_DESTROY mailbox failed with "
12076 "status x%x add_status x%x, mbx status x%x\n",
12077 shdr_status, shdr_add_status, rc);
12078 status = -ENXIO;
12079 }
12080 /* Remove cq from any list */
12081 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012082 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012083 return status;
12084}
12085
12086/**
James Smart04c68492009-05-22 14:52:52 -040012087 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12088 * @qm: The queue structure associated with the queue to destroy.
12089 *
12090 * This function destroys a queue, as detailed in @mq by sending an mailbox
12091 * command, specific to the type of queue, to the HBA.
12092 *
12093 * The @mq struct is used to get the queue ID of the queue to destroy.
12094 *
12095 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012096 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012097 **/
12098uint32_t
12099lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12100{
12101 LPFC_MBOXQ_t *mbox;
12102 int rc, length, status = 0;
12103 uint32_t shdr_status, shdr_add_status;
12104 union lpfc_sli4_cfg_shdr *shdr;
12105
12106 if (!mq)
12107 return -ENODEV;
12108 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12109 if (!mbox)
12110 return -ENOMEM;
12111 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12112 sizeof(struct lpfc_sli4_cfg_mhdr));
12113 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12114 LPFC_MBOX_OPCODE_MQ_DESTROY,
12115 length, LPFC_SLI4_MBX_EMBED);
12116 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12117 mq->queue_id);
12118 mbox->vport = mq->phba->pport;
12119 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12120 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12121 /* The IOCTL status is embedded in the mailbox subheader. */
12122 shdr = (union lpfc_sli4_cfg_shdr *)
12123 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12126 if (shdr_status || shdr_add_status || rc) {
12127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12128 "2507 MQ_DESTROY mailbox failed with "
12129 "status x%x add_status x%x, mbx status x%x\n",
12130 shdr_status, shdr_add_status, rc);
12131 status = -ENXIO;
12132 }
12133 /* Remove mq from any list */
12134 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012135 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012136 return status;
12137}
12138
12139/**
James Smart4f774512009-05-22 14:52:35 -040012140 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12141 * @wq: The queue structure associated with the queue to destroy.
12142 *
12143 * This function destroys a queue, as detailed in @wq by sending an mailbox
12144 * command, specific to the type of queue, to the HBA.
12145 *
12146 * The @wq struct is used to get the queue ID of the queue to destroy.
12147 *
12148 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012149 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012150 **/
12151uint32_t
12152lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12153{
12154 LPFC_MBOXQ_t *mbox;
12155 int rc, length, status = 0;
12156 uint32_t shdr_status, shdr_add_status;
12157 union lpfc_sli4_cfg_shdr *shdr;
12158
12159 if (!wq)
12160 return -ENODEV;
12161 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12162 if (!mbox)
12163 return -ENOMEM;
12164 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12165 sizeof(struct lpfc_sli4_cfg_mhdr));
12166 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12167 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12168 length, LPFC_SLI4_MBX_EMBED);
12169 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12170 wq->queue_id);
12171 mbox->vport = wq->phba->pport;
12172 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12173 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12174 shdr = (union lpfc_sli4_cfg_shdr *)
12175 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12176 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12177 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12178 if (shdr_status || shdr_add_status || rc) {
12179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12180 "2508 WQ_DESTROY mailbox failed with "
12181 "status x%x add_status x%x, mbx status x%x\n",
12182 shdr_status, shdr_add_status, rc);
12183 status = -ENXIO;
12184 }
12185 /* Remove wq from any list */
12186 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012187 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012188 return status;
12189}
12190
12191/**
12192 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12193 * @rq: The queue structure associated with the queue to destroy.
12194 *
12195 * This function destroys a queue, as detailed in @rq by sending an mailbox
12196 * command, specific to the type of queue, to the HBA.
12197 *
12198 * The @rq struct is used to get the queue ID of the queue to destroy.
12199 *
12200 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012201 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012202 **/
12203uint32_t
12204lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12205 struct lpfc_queue *drq)
12206{
12207 LPFC_MBOXQ_t *mbox;
12208 int rc, length, status = 0;
12209 uint32_t shdr_status, shdr_add_status;
12210 union lpfc_sli4_cfg_shdr *shdr;
12211
12212 if (!hrq || !drq)
12213 return -ENODEV;
12214 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12215 if (!mbox)
12216 return -ENOMEM;
12217 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012218 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012219 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12220 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12221 length, LPFC_SLI4_MBX_EMBED);
12222 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12223 hrq->queue_id);
12224 mbox->vport = hrq->phba->pport;
12225 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12226 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12227 /* The IOCTL status is embedded in the mailbox subheader. */
12228 shdr = (union lpfc_sli4_cfg_shdr *)
12229 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12230 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12231 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12232 if (shdr_status || shdr_add_status || rc) {
12233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12234 "2509 RQ_DESTROY mailbox failed with "
12235 "status x%x add_status x%x, mbx status x%x\n",
12236 shdr_status, shdr_add_status, rc);
12237 if (rc != MBX_TIMEOUT)
12238 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12239 return -ENXIO;
12240 }
12241 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12242 drq->queue_id);
12243 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12244 shdr = (union lpfc_sli4_cfg_shdr *)
12245 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12246 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12247 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12248 if (shdr_status || shdr_add_status || rc) {
12249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12250 "2510 RQ_DESTROY mailbox failed with "
12251 "status x%x add_status x%x, mbx status x%x\n",
12252 shdr_status, shdr_add_status, rc);
12253 status = -ENXIO;
12254 }
12255 list_del_init(&hrq->list);
12256 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012257 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012258 return status;
12259}
12260
12261/**
12262 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12263 * @phba: The virtual port for which this call being executed.
12264 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12265 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12266 * @xritag: the xritag that ties this io to the SGL pages.
12267 *
12268 * This routine will post the sgl pages for the IO that has the xritag
12269 * that is in the iocbq structure. The xritag is assigned during iocbq
12270 * creation and persists for as long as the driver is loaded.
12271 * if the caller has fewer than 256 scatter gather segments to map then
12272 * pdma_phys_addr1 should be 0.
12273 * If the caller needs to map more than 256 scatter gather segment then
12274 * pdma_phys_addr1 should be a valid physical address.
12275 * physical address for SGLs must be 64 byte aligned.
12276 * If you are going to map 2 SGL's then the first one must have 256 entries
12277 * the second sgl can have between 1 and 256 entries.
12278 *
12279 * Return codes:
12280 * 0 - Success
12281 * -ENXIO, -ENOMEM - Failure
12282 **/
12283int
12284lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12285 dma_addr_t pdma_phys_addr0,
12286 dma_addr_t pdma_phys_addr1,
12287 uint16_t xritag)
12288{
12289 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12290 LPFC_MBOXQ_t *mbox;
12291 int rc;
12292 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012293 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012294 union lpfc_sli4_cfg_shdr *shdr;
12295
12296 if (xritag == NO_XRI) {
12297 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12298 "0364 Invalid param:\n");
12299 return -EINVAL;
12300 }
12301
12302 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12303 if (!mbox)
12304 return -ENOMEM;
12305
12306 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12307 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12308 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012309 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012310
12311 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12312 &mbox->u.mqe.un.post_sgl_pages;
12313 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12314 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12315
12316 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12317 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12318 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12319 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12320
12321 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12322 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12323 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12324 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12325 if (!phba->sli4_hba.intr_enable)
12326 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012327 else {
12328 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12329 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12330 }
James Smart4f774512009-05-22 14:52:35 -040012331 /* The IOCTL status is embedded in the mailbox subheader. */
12332 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12333 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12334 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12335 if (rc != MBX_TIMEOUT)
12336 mempool_free(mbox, phba->mbox_mem_pool);
12337 if (shdr_status || shdr_add_status || rc) {
12338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12339 "2511 POST_SGL mailbox failed with "
12340 "status x%x add_status x%x, mbx status x%x\n",
12341 shdr_status, shdr_add_status, rc);
12342 rc = -ENXIO;
12343 }
12344 return 0;
12345}
James Smart4f774512009-05-22 14:52:35 -040012346
12347/**
James Smart88a2cfb2011-07-22 18:36:33 -040012348 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012349 * @phba: pointer to lpfc hba data structure.
12350 *
12351 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012352 * HBA consistent with the SLI-4 interface spec. This routine
12353 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12354 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012355 *
James Smart88a2cfb2011-07-22 18:36:33 -040012356 * Returns
12357 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12358 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12359 **/
James Smart6d368e52011-05-24 11:44:12 -040012360uint16_t
12361lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12362{
12363 unsigned long xri;
12364
12365 /*
12366 * Fetch the next logical xri. Because this index is logical,
12367 * the driver starts at 0 each time.
12368 */
12369 spin_lock_irq(&phba->hbalock);
12370 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12371 phba->sli4_hba.max_cfg_param.max_xri, 0);
12372 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12373 spin_unlock_irq(&phba->hbalock);
12374 return NO_XRI;
12375 } else {
12376 set_bit(xri, phba->sli4_hba.xri_bmask);
12377 phba->sli4_hba.max_cfg_param.xri_used++;
12378 phba->sli4_hba.xri_count++;
12379 }
12380
12381 spin_unlock_irq(&phba->hbalock);
12382 return xri;
12383}
12384
12385/**
12386 * lpfc_sli4_free_xri - Release an xri for reuse.
12387 * @phba: pointer to lpfc hba data structure.
12388 *
12389 * This routine is invoked to release an xri to the pool of
12390 * available rpis maintained by the driver.
12391 **/
12392void
12393__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12394{
12395 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12396 phba->sli4_hba.xri_count--;
12397 phba->sli4_hba.max_cfg_param.xri_used--;
12398 }
12399}
12400
12401/**
12402 * lpfc_sli4_free_xri - Release an xri for reuse.
12403 * @phba: pointer to lpfc hba data structure.
12404 *
12405 * This routine is invoked to release an xri to the pool of
12406 * available rpis maintained by the driver.
12407 **/
12408void
12409lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12410{
12411 spin_lock_irq(&phba->hbalock);
12412 __lpfc_sli4_free_xri(phba, xri);
12413 spin_unlock_irq(&phba->hbalock);
12414}
12415
12416/**
James Smart4f774512009-05-22 14:52:35 -040012417 * lpfc_sli4_next_xritag - Get an xritag for the io
12418 * @phba: Pointer to HBA context object.
12419 *
12420 * This function gets an xritag for the iocb. If there is no unused xritag
12421 * it will return 0xffff.
12422 * The function returns the allocated xritag if successful, else returns zero.
12423 * Zero is not a valid xritag.
12424 * The caller is not required to hold any lock.
12425 **/
12426uint16_t
12427lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12428{
James Smart6d368e52011-05-24 11:44:12 -040012429 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040012430
James Smart6d368e52011-05-24 11:44:12 -040012431 xri_index = lpfc_sli4_alloc_xri(phba);
12432 if (xri_index != NO_XRI)
12433 return xri_index;
12434
12435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040012436 "2004 Failed to allocate XRI.last XRITAG is %d"
12437 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040012438 xri_index,
James Smart4f774512009-05-22 14:52:35 -040012439 phba->sli4_hba.max_cfg_param.max_xri,
12440 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040012441 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040012442}
12443
12444/**
James Smart6d368e52011-05-24 11:44:12 -040012445 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040012446 * @phba: pointer to lpfc hba data structure.
12447 *
12448 * This routine is invoked to post a block of driver's sgl pages to the
12449 * HBA using non-embedded mailbox command. No Lock is held. This routine
12450 * is only called when the driver is loading and after all IO has been
12451 * stopped.
12452 **/
12453int
James Smart6d368e52011-05-24 11:44:12 -040012454lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040012455{
12456 struct lpfc_sglq *sglq_entry;
12457 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12458 struct sgl_page_pairs *sgl_pg_pairs;
12459 void *viraddr;
12460 LPFC_MBOXQ_t *mbox;
12461 uint32_t reqlen, alloclen, pg_pairs;
12462 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040012463 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040012464 int els_xri_cnt, rc = 0;
12465 uint32_t shdr_status, shdr_add_status;
12466 union lpfc_sli4_cfg_shdr *shdr;
12467
12468 /* The number of sgls to be posted */
12469 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12470
12471 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12472 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012473 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012474 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12475 "2559 Block sgl registration required DMA "
12476 "size (%d) great than a page\n", reqlen);
12477 return -ENOMEM;
12478 }
12479 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040012480 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040012481 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040012482
12483 /* Allocate DMA memory and set up the non-embedded mailbox command */
12484 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12485 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12486 LPFC_SLI4_MBX_NEMBED);
12487
12488 if (alloclen < reqlen) {
12489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12490 "0285 Allocated DMA memory size (%d) is "
12491 "less than the requested DMA memory "
12492 "size (%d)\n", alloclen, reqlen);
12493 lpfc_sli4_mbox_cmd_free(phba, mbox);
12494 return -ENOMEM;
12495 }
James Smart4f774512009-05-22 14:52:35 -040012496 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040012497 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040012498 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12499 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12500
12501 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12502 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040012503
12504 /*
12505 * Assign the sglq a physical xri only if the driver has not
12506 * initialized those resources. A port reset only needs
12507 * the sglq's posted.
12508 */
12509 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12510 LPFC_XRI_RSRC_RDY) {
12511 lxri = lpfc_sli4_next_xritag(phba);
12512 if (lxri == NO_XRI) {
12513 lpfc_sli4_mbox_cmd_free(phba, mbox);
12514 return -ENOMEM;
12515 }
12516 sglq_entry->sli4_lxritag = lxri;
12517 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12518 }
12519
James Smart4f774512009-05-22 14:52:35 -040012520 /* Set up the sge entry */
12521 sgl_pg_pairs->sgl_pg0_addr_lo =
12522 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12523 sgl_pg_pairs->sgl_pg0_addr_hi =
12524 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12525 sgl_pg_pairs->sgl_pg1_addr_lo =
12526 cpu_to_le32(putPaddrLow(0));
12527 sgl_pg_pairs->sgl_pg1_addr_hi =
12528 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040012529
James Smart4f774512009-05-22 14:52:35 -040012530 /* Keep the first xritag on the list */
12531 if (pg_pairs == 0)
12532 xritag_start = sglq_entry->sli4_xritag;
12533 sgl_pg_pairs++;
12534 }
James Smart6d368e52011-05-24 11:44:12 -040012535
12536 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040012537 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040012538 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040012539 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040012540 if (!phba->sli4_hba.intr_enable)
12541 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12542 else {
12543 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12544 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12545 }
12546 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12547 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12548 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12549 if (rc != MBX_TIMEOUT)
12550 lpfc_sli4_mbox_cmd_free(phba, mbox);
12551 if (shdr_status || shdr_add_status || rc) {
12552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12553 "2513 POST_SGL_BLOCK mailbox command failed "
12554 "status x%x add_status x%x mbx status x%x\n",
12555 shdr_status, shdr_add_status, rc);
12556 rc = -ENXIO;
12557 }
James Smart6d368e52011-05-24 11:44:12 -040012558
12559 if (rc == 0)
12560 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12561 LPFC_XRI_RSRC_RDY);
12562 return rc;
12563}
12564
12565/**
12566 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12567 * @phba: pointer to lpfc hba data structure.
12568 *
12569 * This routine is invoked to post a block of driver's sgl pages to the
12570 * HBA using non-embedded mailbox command. No Lock is held. This routine
12571 * is only called when the driver is loading and after all IO has been
12572 * stopped.
12573 **/
12574int
12575lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
12576{
12577 struct lpfc_sglq *sglq_entry;
12578 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12579 struct sgl_page_pairs *sgl_pg_pairs;
12580 void *viraddr;
12581 LPFC_MBOXQ_t *mbox;
12582 uint32_t reqlen, alloclen, index;
12583 uint32_t mbox_tmo;
12584 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
12585 uint16_t xritag_start = 0, lxri = 0;
12586 struct lpfc_rsrc_blks *rsrc_blk;
12587 int cnt, ttl_cnt, rc = 0;
12588 int loop_cnt;
12589 uint32_t shdr_status, shdr_add_status;
12590 union lpfc_sli4_cfg_shdr *shdr;
12591
12592 /* The number of sgls to be posted */
12593 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12594
12595 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12596 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12597 if (reqlen > SLI4_PAGE_SIZE) {
12598 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12599 "2989 Block sgl registration required DMA "
12600 "size (%d) great than a page\n", reqlen);
12601 return -ENOMEM;
12602 }
12603
12604 cnt = 0;
12605 ttl_cnt = 0;
12606 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12607 list) {
12608 rsrc_start = rsrc_blk->rsrc_start;
12609 rsrc_size = rsrc_blk->rsrc_size;
12610
12611 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12612 "3014 Working ELS Extent start %d, cnt %d\n",
12613 rsrc_start, rsrc_size);
12614
12615 loop_cnt = min(els_xri_cnt, rsrc_size);
12616 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
12617 loop_cnt = els_xri_cnt - ttl_cnt;
12618 ttl_cnt = els_xri_cnt;
12619 }
12620
12621 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12622 if (!mbox)
12623 return -ENOMEM;
12624 /*
12625 * Allocate DMA memory and set up the non-embedded mailbox
12626 * command.
12627 */
12628 alloclen = lpfc_sli4_config(phba, mbox,
12629 LPFC_MBOX_SUBSYSTEM_FCOE,
12630 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12631 reqlen, LPFC_SLI4_MBX_NEMBED);
12632 if (alloclen < reqlen) {
12633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12634 "2987 Allocated DMA memory size (%d) "
12635 "is less than the requested DMA memory "
12636 "size (%d)\n", alloclen, reqlen);
12637 lpfc_sli4_mbox_cmd_free(phba, mbox);
12638 return -ENOMEM;
12639 }
12640
12641 /* Set up the SGL pages in the non-embedded DMA pages */
12642 viraddr = mbox->sge_array->addr[0];
12643 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12644 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12645
12646 /*
12647 * The starting resource may not begin at zero. Control
12648 * the loop variants via the block resource parameters,
12649 * but handle the sge pointers with a zero-based index
12650 * that doesn't get reset per loop pass.
12651 */
12652 for (index = rsrc_start;
12653 index < rsrc_start + loop_cnt;
12654 index++) {
12655 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
12656
12657 /*
12658 * Assign the sglq a physical xri only if the driver
12659 * has not initialized those resources. A port reset
12660 * only needs the sglq's posted.
12661 */
12662 if (bf_get(lpfc_xri_rsrc_rdy,
12663 &phba->sli4_hba.sli4_flags) !=
12664 LPFC_XRI_RSRC_RDY) {
12665 lxri = lpfc_sli4_next_xritag(phba);
12666 if (lxri == NO_XRI) {
12667 lpfc_sli4_mbox_cmd_free(phba, mbox);
12668 rc = -ENOMEM;
12669 goto err_exit;
12670 }
12671 sglq_entry->sli4_lxritag = lxri;
12672 sglq_entry->sli4_xritag =
12673 phba->sli4_hba.xri_ids[lxri];
12674 }
12675
12676 /* Set up the sge entry */
12677 sgl_pg_pairs->sgl_pg0_addr_lo =
12678 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12679 sgl_pg_pairs->sgl_pg0_addr_hi =
12680 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12681 sgl_pg_pairs->sgl_pg1_addr_lo =
12682 cpu_to_le32(putPaddrLow(0));
12683 sgl_pg_pairs->sgl_pg1_addr_hi =
12684 cpu_to_le32(putPaddrHigh(0));
12685
12686 /* Track the starting physical XRI for the mailbox. */
12687 if (index == rsrc_start)
12688 xritag_start = sglq_entry->sli4_xritag;
12689 sgl_pg_pairs++;
12690 cnt++;
12691 }
12692
12693 /* Complete initialization and perform endian conversion. */
12694 rsrc_blk->rsrc_used += loop_cnt;
12695 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12696 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
12697 sgl->word0 = cpu_to_le32(sgl->word0);
12698
12699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12700 "3015 Post ELS Extent SGL, start %d, "
12701 "cnt %d, used %d\n",
12702 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
12703 if (!phba->sli4_hba.intr_enable)
12704 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12705 else {
12706 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12707 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12708 }
12709 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12710 shdr_status = bf_get(lpfc_mbox_hdr_status,
12711 &shdr->response);
12712 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12713 &shdr->response);
12714 if (rc != MBX_TIMEOUT)
12715 lpfc_sli4_mbox_cmd_free(phba, mbox);
12716 if (shdr_status || shdr_add_status || rc) {
12717 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12718 "2988 POST_SGL_BLOCK mailbox "
12719 "command failed status x%x "
12720 "add_status x%x mbx status x%x\n",
12721 shdr_status, shdr_add_status, rc);
12722 rc = -ENXIO;
12723 goto err_exit;
12724 }
12725 if (ttl_cnt >= els_xri_cnt)
12726 break;
12727 }
12728
12729 err_exit:
12730 if (rc == 0)
12731 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12732 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040012733 return rc;
12734}
12735
12736/**
12737 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
12738 * @phba: pointer to lpfc hba data structure.
12739 * @sblist: pointer to scsi buffer list.
12740 * @count: number of scsi buffers on the list.
12741 *
12742 * This routine is invoked to post a block of @count scsi sgl pages from a
12743 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12744 * No Lock is held.
12745 *
12746 **/
12747int
12748lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
12749 int cnt)
12750{
12751 struct lpfc_scsi_buf *psb;
12752 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12753 struct sgl_page_pairs *sgl_pg_pairs;
12754 void *viraddr;
12755 LPFC_MBOXQ_t *mbox;
12756 uint32_t reqlen, alloclen, pg_pairs;
12757 uint32_t mbox_tmo;
12758 uint16_t xritag_start = 0;
12759 int rc = 0;
12760 uint32_t shdr_status, shdr_add_status;
12761 dma_addr_t pdma_phys_bpl1;
12762 union lpfc_sli4_cfg_shdr *shdr;
12763
12764 /* Calculate the requested length of the dma memory */
12765 reqlen = cnt * sizeof(struct sgl_page_pairs) +
12766 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012767 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012768 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12769 "0217 Block sgl registration required DMA "
12770 "size (%d) great than a page\n", reqlen);
12771 return -ENOMEM;
12772 }
12773 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12774 if (!mbox) {
12775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12776 "0283 Failed to allocate mbox cmd memory\n");
12777 return -ENOMEM;
12778 }
12779
12780 /* Allocate DMA memory and set up the non-embedded mailbox command */
12781 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12782 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12783 LPFC_SLI4_MBX_NEMBED);
12784
12785 if (alloclen < reqlen) {
12786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12787 "2561 Allocated DMA memory size (%d) is "
12788 "less than the requested DMA memory "
12789 "size (%d)\n", alloclen, reqlen);
12790 lpfc_sli4_mbox_cmd_free(phba, mbox);
12791 return -ENOMEM;
12792 }
James Smart6d368e52011-05-24 11:44:12 -040012793
James Smart4f774512009-05-22 14:52:35 -040012794 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040012795 viraddr = mbox->sge_array->addr[0];
12796
12797 /* Set up the SGL pages in the non-embedded DMA pages */
12798 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12799 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12800
12801 pg_pairs = 0;
12802 list_for_each_entry(psb, sblist, list) {
12803 /* Set up the sge entry */
12804 sgl_pg_pairs->sgl_pg0_addr_lo =
12805 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
12806 sgl_pg_pairs->sgl_pg0_addr_hi =
12807 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
12808 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
12809 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
12810 else
12811 pdma_phys_bpl1 = 0;
12812 sgl_pg_pairs->sgl_pg1_addr_lo =
12813 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
12814 sgl_pg_pairs->sgl_pg1_addr_hi =
12815 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
12816 /* Keep the first xritag on the list */
12817 if (pg_pairs == 0)
12818 xritag_start = psb->cur_iocbq.sli4_xritag;
12819 sgl_pg_pairs++;
12820 pg_pairs++;
12821 }
12822 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12823 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
12824 /* Perform endian conversion if necessary */
12825 sgl->word0 = cpu_to_le32(sgl->word0);
12826
12827 if (!phba->sli4_hba.intr_enable)
12828 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12829 else {
12830 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12831 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12832 }
12833 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12834 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12835 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12836 if (rc != MBX_TIMEOUT)
12837 lpfc_sli4_mbox_cmd_free(phba, mbox);
12838 if (shdr_status || shdr_add_status || rc) {
12839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12840 "2564 POST_SGL_BLOCK mailbox command failed "
12841 "status x%x add_status x%x mbx status x%x\n",
12842 shdr_status, shdr_add_status, rc);
12843 rc = -ENXIO;
12844 }
12845 return rc;
12846}
12847
12848/**
James Smart6d368e52011-05-24 11:44:12 -040012849 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
12850 * @phba: pointer to lpfc hba data structure.
12851 * @sblist: pointer to scsi buffer list.
12852 * @count: number of scsi buffers on the list.
12853 *
12854 * This routine is invoked to post a block of @count scsi sgl pages from a
12855 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12856 * No Lock is held.
12857 *
12858 **/
12859int
12860lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
12861 int cnt)
12862{
12863 struct lpfc_scsi_buf *psb = NULL;
12864 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12865 struct sgl_page_pairs *sgl_pg_pairs;
12866 void *viraddr;
12867 LPFC_MBOXQ_t *mbox;
12868 uint32_t reqlen, alloclen, pg_pairs;
12869 uint32_t mbox_tmo;
12870 uint16_t xri_start = 0, scsi_xri_start;
12871 uint16_t rsrc_range;
12872 int rc = 0, avail_cnt;
12873 uint32_t shdr_status, shdr_add_status;
12874 dma_addr_t pdma_phys_bpl1;
12875 union lpfc_sli4_cfg_shdr *shdr;
12876 struct lpfc_rsrc_blks *rsrc_blk;
12877 uint32_t xri_cnt = 0;
12878
12879 /* Calculate the total requested length of the dma memory */
12880 reqlen = cnt * sizeof(struct sgl_page_pairs) +
12881 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12882 if (reqlen > SLI4_PAGE_SIZE) {
12883 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12884 "2932 Block sgl registration required DMA "
12885 "size (%d) great than a page\n", reqlen);
12886 return -ENOMEM;
12887 }
12888
12889 /*
12890 * The use of extents requires the driver to post the sgl headers
12891 * in multiple postings to meet the contiguous resource assignment.
12892 */
12893 psb = list_prepare_entry(psb, sblist, list);
12894 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
12895 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12896 list) {
12897 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
12898 if (rsrc_range < scsi_xri_start)
12899 continue;
12900 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
12901 continue;
12902 else
12903 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
12904
12905 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
12906 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12907 /*
12908 * Allocate DMA memory and set up the non-embedded mailbox
12909 * command. The mbox is used to post an SGL page per loop
12910 * but the DMA memory has a use-once semantic so the mailbox
12911 * is used and freed per loop pass.
12912 */
12913 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12914 if (!mbox) {
12915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12916 "2933 Failed to allocate mbox cmd "
12917 "memory\n");
12918 return -ENOMEM;
12919 }
12920 alloclen = lpfc_sli4_config(phba, mbox,
12921 LPFC_MBOX_SUBSYSTEM_FCOE,
12922 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12923 reqlen,
12924 LPFC_SLI4_MBX_NEMBED);
12925 if (alloclen < reqlen) {
12926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12927 "2934 Allocated DMA memory size (%d) "
12928 "is less than the requested DMA memory "
12929 "size (%d)\n", alloclen, reqlen);
12930 lpfc_sli4_mbox_cmd_free(phba, mbox);
12931 return -ENOMEM;
12932 }
12933
12934 /* Get the first SGE entry from the non-embedded DMA memory */
12935 viraddr = mbox->sge_array->addr[0];
12936
12937 /* Set up the SGL pages in the non-embedded DMA pages */
12938 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12939 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12940
12941 /* pg_pairs tracks posted SGEs per loop iteration. */
12942 pg_pairs = 0;
12943 list_for_each_entry_continue(psb, sblist, list) {
12944 /* Set up the sge entry */
12945 sgl_pg_pairs->sgl_pg0_addr_lo =
12946 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
12947 sgl_pg_pairs->sgl_pg0_addr_hi =
12948 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
12949 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
12950 pdma_phys_bpl1 = psb->dma_phys_bpl +
12951 SGL_PAGE_SIZE;
12952 else
12953 pdma_phys_bpl1 = 0;
12954 sgl_pg_pairs->sgl_pg1_addr_lo =
12955 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
12956 sgl_pg_pairs->sgl_pg1_addr_hi =
12957 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
12958 /* Keep the first xri for this extent. */
12959 if (pg_pairs == 0)
12960 xri_start = psb->cur_iocbq.sli4_xritag;
12961 sgl_pg_pairs++;
12962 pg_pairs++;
12963 xri_cnt++;
12964
12965 /*
12966 * Track two exit conditions - the loop has constructed
12967 * all of the caller's SGE pairs or all available
12968 * resource IDs in this extent are consumed.
12969 */
12970 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
12971 break;
12972 }
12973 rsrc_blk->rsrc_used += pg_pairs;
12974 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
12975 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
12976
12977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12978 "3016 Post SCSI Extent SGL, start %d, cnt %d "
12979 "blk use %d\n",
12980 xri_start, pg_pairs, rsrc_blk->rsrc_used);
12981 /* Perform endian conversion if necessary */
12982 sgl->word0 = cpu_to_le32(sgl->word0);
12983 if (!phba->sli4_hba.intr_enable)
12984 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12985 else {
12986 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12987 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12988 }
12989 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12990 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12991 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12992 &shdr->response);
12993 if (rc != MBX_TIMEOUT)
12994 lpfc_sli4_mbox_cmd_free(phba, mbox);
12995 if (shdr_status || shdr_add_status || rc) {
12996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12997 "2935 POST_SGL_BLOCK mailbox command "
12998 "failed status x%x add_status x%x "
12999 "mbx status x%x\n",
13000 shdr_status, shdr_add_status, rc);
13001 return -ENXIO;
13002 }
13003
13004 /* Post only what is requested. */
13005 if (xri_cnt >= cnt)
13006 break;
13007 }
13008 return rc;
13009}
13010
13011/**
James Smart4f774512009-05-22 14:52:35 -040013012 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13013 * @phba: pointer to lpfc_hba struct that the frame was received on
13014 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13015 *
13016 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13017 * valid type of frame that the LPFC driver will handle. This function will
13018 * return a zero if the frame is a valid frame or a non zero value when the
13019 * frame does not pass the check.
13020 **/
13021static int
13022lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13023{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013024 /* make rctl_names static to save stack space */
13025 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013026 char *type_names[] = FC_TYPE_NAMES_INIT;
13027 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013028 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013029
13030 switch (fc_hdr->fh_r_ctl) {
13031 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13032 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13033 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13034 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13035 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13036 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13037 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13038 case FC_RCTL_DD_CMD_STATUS: /* command status */
13039 case FC_RCTL_ELS_REQ: /* extended link services request */
13040 case FC_RCTL_ELS_REP: /* extended link services reply */
13041 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13042 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13043 case FC_RCTL_BA_NOP: /* basic link service NOP */
13044 case FC_RCTL_BA_ABTS: /* basic link service abort */
13045 case FC_RCTL_BA_RMC: /* remove connection */
13046 case FC_RCTL_BA_ACC: /* basic accept */
13047 case FC_RCTL_BA_RJT: /* basic reject */
13048 case FC_RCTL_BA_PRMT:
13049 case FC_RCTL_ACK_1: /* acknowledge_1 */
13050 case FC_RCTL_ACK_0: /* acknowledge_0 */
13051 case FC_RCTL_P_RJT: /* port reject */
13052 case FC_RCTL_F_RJT: /* fabric reject */
13053 case FC_RCTL_P_BSY: /* port busy */
13054 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13055 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13056 case FC_RCTL_LCR: /* link credit reset */
13057 case FC_RCTL_END: /* end */
13058 break;
13059 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13060 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13061 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13062 return lpfc_fc_frame_check(phba, fc_hdr);
13063 default:
13064 goto drop;
13065 }
13066 switch (fc_hdr->fh_type) {
13067 case FC_TYPE_BLS:
13068 case FC_TYPE_ELS:
13069 case FC_TYPE_FCP:
13070 case FC_TYPE_CT:
13071 break;
13072 case FC_TYPE_IP:
13073 case FC_TYPE_ILS:
13074 default:
13075 goto drop;
13076 }
James Smart546fc852011-03-11 16:06:29 -050013077
James Smart4f774512009-05-22 14:52:35 -040013078 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013079 "2538 Received frame rctl:%s type:%s "
13080 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013081 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013082 type_names[fc_hdr->fh_type],
13083 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13084 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13085 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013086 return 0;
13087drop:
13088 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13089 "2539 Dropped frame rctl:%s type:%s\n",
13090 rctl_names[fc_hdr->fh_r_ctl],
13091 type_names[fc_hdr->fh_type]);
13092 return 1;
13093}
13094
13095/**
13096 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13097 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13098 *
13099 * This function processes the FC header to retrieve the VFI from the VF
13100 * header, if one exists. This function will return the VFI if one exists
13101 * or 0 if no VSAN Header exists.
13102 **/
13103static uint32_t
13104lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13105{
13106 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13107
13108 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13109 return 0;
13110 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13111}
13112
13113/**
13114 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13115 * @phba: Pointer to the HBA structure to search for the vport on
13116 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13117 * @fcfi: The FC Fabric ID that the frame came from
13118 *
13119 * This function searches the @phba for a vport that matches the content of the
13120 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13121 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13122 * returns the matching vport pointer or NULL if unable to match frame to a
13123 * vport.
13124 **/
13125static struct lpfc_vport *
13126lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13127 uint16_t fcfi)
13128{
13129 struct lpfc_vport **vports;
13130 struct lpfc_vport *vport = NULL;
13131 int i;
13132 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13133 fc_hdr->fh_d_id[1] << 8 |
13134 fc_hdr->fh_d_id[2]);
13135
13136 vports = lpfc_create_vport_work_array(phba);
13137 if (vports != NULL)
13138 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13139 if (phba->fcf.fcfi == fcfi &&
13140 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13141 vports[i]->fc_myDID == did) {
13142 vport = vports[i];
13143 break;
13144 }
13145 }
13146 lpfc_destroy_vport_work_array(phba, vports);
13147 return vport;
13148}
13149
13150/**
James Smart45ed1192009-10-02 15:17:02 -040013151 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13152 * @vport: The vport to work on.
13153 *
13154 * This function updates the receive sequence time stamp for this vport. The
13155 * receive sequence time stamp indicates the time that the last frame of the
13156 * the sequence that has been idle for the longest amount of time was received.
13157 * the driver uses this time stamp to indicate if any received sequences have
13158 * timed out.
13159 **/
13160void
13161lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13162{
13163 struct lpfc_dmabuf *h_buf;
13164 struct hbq_dmabuf *dmabuf = NULL;
13165
13166 /* get the oldest sequence on the rcv list */
13167 h_buf = list_get_first(&vport->rcv_buffer_list,
13168 struct lpfc_dmabuf, list);
13169 if (!h_buf)
13170 return;
13171 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13172 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13173}
13174
13175/**
13176 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13177 * @vport: The vport that the received sequences were sent to.
13178 *
13179 * This function cleans up all outstanding received sequences. This is called
13180 * by the driver when a link event or user action invalidates all the received
13181 * sequences.
13182 **/
13183void
13184lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13185{
13186 struct lpfc_dmabuf *h_buf, *hnext;
13187 struct lpfc_dmabuf *d_buf, *dnext;
13188 struct hbq_dmabuf *dmabuf = NULL;
13189
13190 /* start with the oldest sequence on the rcv list */
13191 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13192 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13193 list_del_init(&dmabuf->hbuf.list);
13194 list_for_each_entry_safe(d_buf, dnext,
13195 &dmabuf->dbuf.list, list) {
13196 list_del_init(&d_buf->list);
13197 lpfc_in_buf_free(vport->phba, d_buf);
13198 }
13199 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13200 }
13201}
13202
13203/**
13204 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13205 * @vport: The vport that the received sequences were sent to.
13206 *
13207 * This function determines whether any received sequences have timed out by
13208 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13209 * indicates that there is at least one timed out sequence this routine will
13210 * go through the received sequences one at a time from most inactive to most
13211 * active to determine which ones need to be cleaned up. Once it has determined
13212 * that a sequence needs to be cleaned up it will simply free up the resources
13213 * without sending an abort.
13214 **/
13215void
13216lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13217{
13218 struct lpfc_dmabuf *h_buf, *hnext;
13219 struct lpfc_dmabuf *d_buf, *dnext;
13220 struct hbq_dmabuf *dmabuf = NULL;
13221 unsigned long timeout;
13222 int abort_count = 0;
13223
13224 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13225 vport->rcv_buffer_time_stamp);
13226 if (list_empty(&vport->rcv_buffer_list) ||
13227 time_before(jiffies, timeout))
13228 return;
13229 /* start with the oldest sequence on the rcv list */
13230 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13231 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13232 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13233 dmabuf->time_stamp);
13234 if (time_before(jiffies, timeout))
13235 break;
13236 abort_count++;
13237 list_del_init(&dmabuf->hbuf.list);
13238 list_for_each_entry_safe(d_buf, dnext,
13239 &dmabuf->dbuf.list, list) {
13240 list_del_init(&d_buf->list);
13241 lpfc_in_buf_free(vport->phba, d_buf);
13242 }
13243 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13244 }
13245 if (abort_count)
13246 lpfc_update_rcv_time_stamp(vport);
13247}
13248
13249/**
James Smart4f774512009-05-22 14:52:35 -040013250 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13251 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13252 *
13253 * This function searches through the existing incomplete sequences that have
13254 * been sent to this @vport. If the frame matches one of the incomplete
13255 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13256 * make up that sequence. If no sequence is found that matches this frame then
13257 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13258 * This function returns a pointer to the first dmabuf in the sequence list that
13259 * the frame was linked to.
13260 **/
13261static struct hbq_dmabuf *
13262lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13263{
13264 struct fc_frame_header *new_hdr;
13265 struct fc_frame_header *temp_hdr;
13266 struct lpfc_dmabuf *d_buf;
13267 struct lpfc_dmabuf *h_buf;
13268 struct hbq_dmabuf *seq_dmabuf = NULL;
13269 struct hbq_dmabuf *temp_dmabuf = NULL;
13270
James Smart4d9ab992009-10-02 15:16:39 -040013271 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013272 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013273 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13274 /* Use the hdr_buf to find the sequence that this frame belongs to */
13275 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13276 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13277 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13278 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13279 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13280 continue;
13281 /* found a pending sequence that matches this frame */
13282 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13283 break;
13284 }
13285 if (!seq_dmabuf) {
13286 /*
13287 * This indicates first frame received for this sequence.
13288 * Queue the buffer on the vport's rcv_buffer_list.
13289 */
13290 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013291 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013292 return dmabuf;
13293 }
13294 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013295 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13296 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013297 list_del_init(&seq_dmabuf->hbuf.list);
13298 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13299 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013300 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013301 return dmabuf;
13302 }
James Smart45ed1192009-10-02 15:17:02 -040013303 /* move this sequence to the tail to indicate a young sequence */
13304 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13305 seq_dmabuf->time_stamp = jiffies;
13306 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013307 if (list_empty(&seq_dmabuf->dbuf.list)) {
13308 temp_hdr = dmabuf->hbuf.virt;
13309 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13310 return seq_dmabuf;
13311 }
James Smart4f774512009-05-22 14:52:35 -040013312 /* find the correct place in the sequence to insert this frame */
13313 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13314 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13315 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13316 /*
13317 * If the frame's sequence count is greater than the frame on
13318 * the list then insert the frame right after this frame
13319 */
James Smarteeead812009-12-21 17:01:23 -050013320 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13321 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013322 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13323 return seq_dmabuf;
13324 }
13325 }
13326 return NULL;
13327}
13328
13329/**
James Smart6669f9b2009-10-02 15:16:45 -040013330 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13331 * @vport: pointer to a vitural port
13332 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13333 *
13334 * This function tries to abort from the partially assembed sequence, described
13335 * by the information from basic abbort @dmabuf. It checks to see whether such
13336 * partially assembled sequence held by the driver. If so, it shall free up all
13337 * the frames from the partially assembled sequence.
13338 *
13339 * Return
13340 * true -- if there is matching partially assembled sequence present and all
13341 * the frames freed with the sequence;
13342 * false -- if there is no matching partially assembled sequence present so
13343 * nothing got aborted in the lower layer driver
13344 **/
13345static bool
13346lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13347 struct hbq_dmabuf *dmabuf)
13348{
13349 struct fc_frame_header *new_hdr;
13350 struct fc_frame_header *temp_hdr;
13351 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13352 struct hbq_dmabuf *seq_dmabuf = NULL;
13353
13354 /* Use the hdr_buf to find the sequence that matches this frame */
13355 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13356 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13357 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13358 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13359 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13360 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13361 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13362 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13363 continue;
13364 /* found a pending sequence that matches this frame */
13365 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13366 break;
13367 }
13368
13369 /* Free up all the frames from the partially assembled sequence */
13370 if (seq_dmabuf) {
13371 list_for_each_entry_safe(d_buf, n_buf,
13372 &seq_dmabuf->dbuf.list, list) {
13373 list_del_init(&d_buf->list);
13374 lpfc_in_buf_free(vport->phba, d_buf);
13375 }
13376 return true;
13377 }
13378 return false;
13379}
13380
13381/**
James Smart546fc852011-03-11 16:06:29 -050013382 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013383 * @phba: Pointer to HBA context object.
13384 * @cmd_iocbq: pointer to the command iocbq structure.
13385 * @rsp_iocbq: pointer to the response iocbq structure.
13386 *
James Smart546fc852011-03-11 16:06:29 -050013387 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013388 * event. It properly releases the memory allocated to the sequence abort
13389 * accept iocb.
13390 **/
13391static void
James Smart546fc852011-03-11 16:06:29 -050013392lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013393 struct lpfc_iocbq *cmd_iocbq,
13394 struct lpfc_iocbq *rsp_iocbq)
13395{
13396 if (cmd_iocbq)
13397 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13398}
13399
13400/**
James Smart6d368e52011-05-24 11:44:12 -040013401 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13402 * @phba: Pointer to HBA context object.
13403 * @xri: xri id in transaction.
13404 *
13405 * This function validates the xri maps to the known range of XRIs allocated an
13406 * used by the driver.
13407 **/
13408static uint16_t
13409lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13410 uint16_t xri)
13411{
13412 int i;
13413
13414 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13415 if (xri == phba->sli4_hba.xri_ids[i])
13416 return i;
13417 }
13418 return NO_XRI;
13419}
13420
13421
13422/**
James Smart546fc852011-03-11 16:06:29 -050013423 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013424 * @phba: Pointer to HBA context object.
13425 * @fc_hdr: pointer to a FC frame header.
13426 *
James Smart546fc852011-03-11 16:06:29 -050013427 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013428 * event after aborting the sequence handling.
13429 **/
13430static void
James Smart546fc852011-03-11 16:06:29 -050013431lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013432 struct fc_frame_header *fc_hdr)
13433{
13434 struct lpfc_iocbq *ctiocb = NULL;
13435 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050013436 uint16_t oxid, rxid;
13437 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013438 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013439 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013440
13441 if (!lpfc_is_link_up(phba))
13442 return;
13443
13444 sid = sli4_sid_from_fc_hdr(fc_hdr);
13445 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050013446 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040013447
13448 ndlp = lpfc_findnode_did(phba->pport, sid);
13449 if (!ndlp) {
13450 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13451 "1268 Find ndlp returned NULL for oxid:x%x "
13452 "SID:x%x\n", oxid, sid);
13453 return;
13454 }
James Smart6d368e52011-05-24 11:44:12 -040013455 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050013456 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040013457
James Smart546fc852011-03-11 16:06:29 -050013458 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040013459 ctiocb = lpfc_sli_get_iocbq(phba);
13460 if (!ctiocb)
13461 return;
13462
James Smart5ffc2662009-11-18 15:39:44 -050013463 /* Extract the F_CTL field from FC_HDR */
13464 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13465
James Smart6669f9b2009-10-02 15:16:45 -040013466 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040013467 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050013468 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040013469 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13470 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13471 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13472
13473 /* Fill in the rest of iocb fields */
13474 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13475 icmd->ulpBdeCount = 0;
13476 icmd->ulpLe = 1;
13477 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040013478 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050013479 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040013480
James Smart6669f9b2009-10-02 15:16:45 -040013481 ctiocb->iocb_cmpl = NULL;
13482 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050013483 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040013484 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050013485 ctiocb->sli4_xritag = NO_XRI;
13486
13487 /* If the oxid maps to the FCP XRI range or if it is out of range,
13488 * send a BLS_RJT. The driver no longer has that exchange.
13489 * Override the IOCB for a BA_RJT.
13490 */
13491 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13492 phba->sli4_hba.max_cfg_param.xri_base) ||
13493 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13494 phba->sli4_hba.max_cfg_param.xri_base)) {
13495 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13496 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13497 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13498 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13499 }
James Smart6669f9b2009-10-02 15:16:45 -040013500
James Smart5ffc2662009-11-18 15:39:44 -050013501 if (fctl & FC_FC_EX_CTX) {
13502 /* ABTS sent by responder to CT exchange, construction
13503 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13504 * field and RX_ID from ABTS for RX_ID field.
13505 */
James Smart546fc852011-03-11 16:06:29 -050013506 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13507 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050013508 } else {
13509 /* ABTS sent by initiator to CT exchange, construction
13510 * of BA_ACC will need to allocate a new XRI as for the
13511 * XRI_TAG and RX_ID fields.
13512 */
James Smart546fc852011-03-11 16:06:29 -050013513 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13514 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050013515 }
James Smart546fc852011-03-11 16:06:29 -050013516 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050013517
James Smart546fc852011-03-11 16:06:29 -050013518 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040013519 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013520 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13521 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13522
13523 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13524 if (rc == IOCB_ERROR) {
13525 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13526 "2925 Failed to issue CT ABTS RSP x%x on "
13527 "xri x%x, Data x%x\n",
13528 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13529 phba->link_state);
13530 lpfc_sli_release_iocbq(phba, ctiocb);
13531 }
James Smart6669f9b2009-10-02 15:16:45 -040013532}
13533
13534/**
13535 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13536 * @vport: Pointer to the vport on which this sequence was received
13537 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13538 *
13539 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13540 * receive sequence is only partially assembed by the driver, it shall abort
13541 * the partially assembled frames for the sequence. Otherwise, if the
13542 * unsolicited receive sequence has been completely assembled and passed to
13543 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13544 * unsolicited sequence has been aborted. After that, it will issue a basic
13545 * accept to accept the abort.
13546 **/
13547void
13548lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13549 struct hbq_dmabuf *dmabuf)
13550{
13551 struct lpfc_hba *phba = vport->phba;
13552 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050013553 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013554 bool abts_par;
13555
James Smart6669f9b2009-10-02 15:16:45 -040013556 /* Make a copy of fc_hdr before the dmabuf being released */
13557 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050013558 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013559
James Smart5ffc2662009-11-18 15:39:44 -050013560 if (fctl & FC_FC_EX_CTX) {
13561 /*
13562 * ABTS sent by responder to exchange, just free the buffer
13563 */
James Smart6669f9b2009-10-02 15:16:45 -040013564 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050013565 } else {
13566 /*
13567 * ABTS sent by initiator to exchange, need to do cleanup
13568 */
13569 /* Try to abort partially assembled seq */
13570 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13571
13572 /* Send abort to ULP if partially seq abort failed */
13573 if (abts_par == false)
13574 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13575 else
13576 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13577 }
James Smart6669f9b2009-10-02 15:16:45 -040013578 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050013579 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013580}
13581
13582/**
James Smart4f774512009-05-22 14:52:35 -040013583 * lpfc_seq_complete - Indicates if a sequence is complete
13584 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13585 *
13586 * This function checks the sequence, starting with the frame described by
13587 * @dmabuf, to see if all the frames associated with this sequence are present.
13588 * the frames associated with this sequence are linked to the @dmabuf using the
13589 * dbuf list. This function looks for two major things. 1) That the first frame
13590 * has a sequence count of zero. 2) There is a frame with last frame of sequence
13591 * set. 3) That there are no holes in the sequence count. The function will
13592 * return 1 when the sequence is complete, otherwise it will return 0.
13593 **/
13594static int
13595lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
13596{
13597 struct fc_frame_header *hdr;
13598 struct lpfc_dmabuf *d_buf;
13599 struct hbq_dmabuf *seq_dmabuf;
13600 uint32_t fctl;
13601 int seq_count = 0;
13602
13603 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13604 /* make sure first fame of sequence has a sequence count of zero */
13605 if (hdr->fh_seq_cnt != seq_count)
13606 return 0;
13607 fctl = (hdr->fh_f_ctl[0] << 16 |
13608 hdr->fh_f_ctl[1] << 8 |
13609 hdr->fh_f_ctl[2]);
13610 /* If last frame of sequence we can return success. */
13611 if (fctl & FC_FC_END_SEQ)
13612 return 1;
13613 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
13614 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13615 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13616 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050013617 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040013618 return 0;
13619 fctl = (hdr->fh_f_ctl[0] << 16 |
13620 hdr->fh_f_ctl[1] << 8 |
13621 hdr->fh_f_ctl[2]);
13622 /* If last frame of sequence we can return success. */
13623 if (fctl & FC_FC_END_SEQ)
13624 return 1;
13625 }
13626 return 0;
13627}
13628
13629/**
13630 * lpfc_prep_seq - Prep sequence for ULP processing
13631 * @vport: Pointer to the vport on which this sequence was received
13632 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13633 *
13634 * This function takes a sequence, described by a list of frames, and creates
13635 * a list of iocbq structures to describe the sequence. This iocbq list will be
13636 * used to issue to the generic unsolicited sequence handler. This routine
13637 * returns a pointer to the first iocbq in the list. If the function is unable
13638 * to allocate an iocbq then it throw out the received frames that were not
13639 * able to be described and return a pointer to the first iocbq. If unable to
13640 * allocate any iocbqs (including the first) this function will return NULL.
13641 **/
13642static struct lpfc_iocbq *
13643lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
13644{
13645 struct lpfc_dmabuf *d_buf, *n_buf;
13646 struct lpfc_iocbq *first_iocbq, *iocbq;
13647 struct fc_frame_header *fc_hdr;
13648 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050013649 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040013650
13651 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13652 /* remove from receive buffer list */
13653 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013654 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013655 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040013656 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040013657 /* Get an iocbq struct to fill in. */
13658 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
13659 if (first_iocbq) {
13660 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040013661 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040013662 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
13663 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
13664 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart6d368e52011-05-24 11:44:12 -040013665 /* iocbq is prepped for internal consumption. Logical vpi. */
13666 first_iocbq->iocb.unsli3.rcvsli3.vpi = vport->vpi;
James Smart4f774512009-05-22 14:52:35 -040013667 /* put the first buffer into the first IOCBq */
13668 first_iocbq->context2 = &seq_dmabuf->dbuf;
13669 first_iocbq->context3 = NULL;
13670 first_iocbq->iocb.ulpBdeCount = 1;
13671 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13672 LPFC_DATA_BUF_SIZE;
13673 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040013674 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040013675 bf_get(lpfc_rcqe_length,
13676 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040013677 }
13678 iocbq = first_iocbq;
13679 /*
13680 * Each IOCBq can have two Buffers assigned, so go through the list
13681 * of buffers for this sequence and save two buffers in each IOCBq
13682 */
13683 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
13684 if (!iocbq) {
13685 lpfc_in_buf_free(vport->phba, d_buf);
13686 continue;
13687 }
13688 if (!iocbq->context3) {
13689 iocbq->context3 = d_buf;
13690 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050013691 pbde = (struct ulp_bde64 *)
13692 &iocbq->iocb.unsli3.sli3Words[4];
13693 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040013694 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040013695 bf_get(lpfc_rcqe_length,
13696 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040013697 } else {
13698 iocbq = lpfc_sli_get_iocbq(vport->phba);
13699 if (!iocbq) {
13700 if (first_iocbq) {
13701 first_iocbq->iocb.ulpStatus =
13702 IOSTAT_FCP_RSP_ERROR;
13703 first_iocbq->iocb.un.ulpWord[4] =
13704 IOERR_NO_RESOURCES;
13705 }
13706 lpfc_in_buf_free(vport->phba, d_buf);
13707 continue;
13708 }
13709 iocbq->context2 = d_buf;
13710 iocbq->context3 = NULL;
13711 iocbq->iocb.ulpBdeCount = 1;
13712 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13713 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040013714 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040013715 bf_get(lpfc_rcqe_length,
13716 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040013717 iocbq->iocb.un.rcvels.remoteID = sid;
13718 list_add_tail(&iocbq->list, &first_iocbq->list);
13719 }
13720 }
13721 return first_iocbq;
13722}
13723
James Smart6669f9b2009-10-02 15:16:45 -040013724static void
13725lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
13726 struct hbq_dmabuf *seq_dmabuf)
13727{
13728 struct fc_frame_header *fc_hdr;
13729 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
13730 struct lpfc_hba *phba = vport->phba;
13731
13732 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13733 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
13734 if (!iocbq) {
13735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13736 "2707 Ring %d handler: Failed to allocate "
13737 "iocb Rctl x%x Type x%x received\n",
13738 LPFC_ELS_RING,
13739 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13740 return;
13741 }
13742 if (!lpfc_complete_unsol_iocb(phba,
13743 &phba->sli.ring[LPFC_ELS_RING],
13744 iocbq, fc_hdr->fh_r_ctl,
13745 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040013746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040013747 "2540 Ring %d handler: unexpected Rctl "
13748 "x%x Type x%x received\n",
13749 LPFC_ELS_RING,
13750 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13751
13752 /* Free iocb created in lpfc_prep_seq */
13753 list_for_each_entry_safe(curr_iocb, next_iocb,
13754 &iocbq->list, list) {
13755 list_del_init(&curr_iocb->list);
13756 lpfc_sli_release_iocbq(phba, curr_iocb);
13757 }
13758 lpfc_sli_release_iocbq(phba, iocbq);
13759}
13760
James Smart4f774512009-05-22 14:52:35 -040013761/**
13762 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
13763 * @phba: Pointer to HBA context object.
13764 *
13765 * This function is called with no lock held. This function processes all
13766 * the received buffers and gives it to upper layers when a received buffer
13767 * indicates that it is the final frame in the sequence. The interrupt
13768 * service routine processes received buffers at interrupt contexts and adds
13769 * received dma buffers to the rb_pend_list queue and signals the worker thread.
13770 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
13771 * appropriate receive function when the final frame in a sequence is received.
13772 **/
James Smart4d9ab992009-10-02 15:16:39 -040013773void
13774lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
13775 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040013776{
James Smart4d9ab992009-10-02 15:16:39 -040013777 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040013778 struct fc_frame_header *fc_hdr;
13779 struct lpfc_vport *vport;
13780 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040013781
James Smart4f774512009-05-22 14:52:35 -040013782 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040013783 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13784 /* check to see if this a valid type of frame */
13785 if (lpfc_fc_frame_check(phba, fc_hdr)) {
13786 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13787 return;
13788 }
13789 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
13790 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050013791 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040013792 /* throw out the frame */
13793 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13794 return;
13795 }
James Smart6669f9b2009-10-02 15:16:45 -040013796 /* Handle the basic abort sequence (BA_ABTS) event */
13797 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
13798 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
13799 return;
13800 }
13801
James Smart4d9ab992009-10-02 15:16:39 -040013802 /* Link this frame */
13803 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
13804 if (!seq_dmabuf) {
13805 /* unable to add frame to vport - throw it out */
13806 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13807 return;
13808 }
13809 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050013810 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040013811 return;
James Smartdef9c7a2009-12-21 17:02:28 -050013812
James Smart6669f9b2009-10-02 15:16:45 -040013813 /* Send the complete sequence to the upper layer protocol */
13814 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040013815}
James Smart6fb120a2009-05-22 14:52:59 -040013816
13817/**
13818 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
13819 * @phba: pointer to lpfc hba data structure.
13820 *
13821 * This routine is invoked to post rpi header templates to the
13822 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040013823 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13824 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040013825 *
13826 * This routine does not require any locks. It's usage is expected
13827 * to be driver load or reset recovery when the driver is
13828 * sequential.
13829 *
13830 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020013831 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040013832 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040013833 * When this error occurs, the driver is not guaranteed
13834 * to have any rpi regions posted to the device and
13835 * must either attempt to repost the regions or take a
13836 * fatal error.
13837 **/
13838int
13839lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
13840{
13841 struct lpfc_rpi_hdr *rpi_page;
13842 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040013843 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040013844
James Smart6d368e52011-05-24 11:44:12 -040013845 /* SLI4 ports that support extents do not require RPI headers. */
13846 if (!phba->sli4_hba.rpi_hdrs_in_use)
13847 goto exit;
13848 if (phba->sli4_hba.extents_in_use)
13849 return -EIO;
13850
James Smart6fb120a2009-05-22 14:52:59 -040013851 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040013852 /*
13853 * Assign the rpi headers a physical rpi only if the driver
13854 * has not initialized those resources. A port reset only
13855 * needs the headers posted.
13856 */
13857 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13858 LPFC_RPI_RSRC_RDY)
13859 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
13860
James Smart6fb120a2009-05-22 14:52:59 -040013861 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
13862 if (rc != MBX_SUCCESS) {
13863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13864 "2008 Error %d posting all rpi "
13865 "headers\n", rc);
13866 rc = -EIO;
13867 break;
13868 }
13869 }
13870
James Smart6d368e52011-05-24 11:44:12 -040013871 exit:
13872 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13873 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040013874 return rc;
13875}
13876
13877/**
13878 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
13879 * @phba: pointer to lpfc hba data structure.
13880 * @rpi_page: pointer to the rpi memory region.
13881 *
13882 * This routine is invoked to post a single rpi header to the
13883 * HBA consistent with the SLI-4 interface spec. This memory region
13884 * maps up to 64 rpi context regions.
13885 *
13886 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020013887 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040013888 * -ENOMEM - No available memory
13889 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040013890 **/
13891int
13892lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
13893{
13894 LPFC_MBOXQ_t *mboxq;
13895 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
13896 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040013897 uint32_t shdr_status, shdr_add_status;
13898 union lpfc_sli4_cfg_shdr *shdr;
13899
James Smart6d368e52011-05-24 11:44:12 -040013900 /* SLI4 ports that support extents do not require RPI headers. */
13901 if (!phba->sli4_hba.rpi_hdrs_in_use)
13902 return rc;
13903 if (phba->sli4_hba.extents_in_use)
13904 return -EIO;
13905
James Smart6fb120a2009-05-22 14:52:59 -040013906 /* The port is notified of the header region via a mailbox command. */
13907 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13908 if (!mboxq) {
13909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13910 "2001 Unable to allocate memory for issuing "
13911 "SLI_CONFIG_SPECIAL mailbox command\n");
13912 return -ENOMEM;
13913 }
13914
13915 /* Post all rpi memory regions to the port. */
13916 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040013917 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
13918 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
13919 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050013920 sizeof(struct lpfc_sli4_cfg_mhdr),
13921 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040013922
13923
13924 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040013925 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
13926 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040013927 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
13928 hdr_tmpl, rpi_page->page_count);
13929
James Smart6fb120a2009-05-22 14:52:59 -040013930 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
13931 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040013932 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040013933 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
13934 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13935 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13936 if (rc != MBX_TIMEOUT)
13937 mempool_free(mboxq, phba->mbox_mem_pool);
13938 if (shdr_status || shdr_add_status || rc) {
13939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13940 "2514 POST_RPI_HDR mailbox failed with "
13941 "status x%x add_status x%x, mbx status x%x\n",
13942 shdr_status, shdr_add_status, rc);
13943 rc = -ENXIO;
13944 }
13945 return rc;
13946}
13947
13948/**
13949 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
13950 * @phba: pointer to lpfc hba data structure.
13951 *
13952 * This routine is invoked to post rpi header templates to the
13953 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040013954 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13955 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040013956 *
13957 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020013958 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040013959 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13960 **/
13961int
13962lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
13963{
James Smart6d368e52011-05-24 11:44:12 -040013964 unsigned long rpi;
13965 uint16_t max_rpi, rpi_limit;
13966 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040013967 struct lpfc_rpi_hdr *rpi_hdr;
13968
13969 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040013970 rpi_limit = phba->sli4_hba.next_rpi;
13971
13972 /*
James Smart6d368e52011-05-24 11:44:12 -040013973 * Fetch the next logical rpi. Because this index is logical,
13974 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040013975 */
13976 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040013977 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
13978 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040013979 rpi = LPFC_RPI_ALLOC_ERROR;
13980 else {
13981 set_bit(rpi, phba->sli4_hba.rpi_bmask);
13982 phba->sli4_hba.max_cfg_param.rpi_used++;
13983 phba->sli4_hba.rpi_count++;
13984 }
13985
13986 /*
13987 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040013988 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040013989 */
13990 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
13991 (phba->sli4_hba.rpi_count >= max_rpi)) {
13992 spin_unlock_irq(&phba->hbalock);
13993 return rpi;
13994 }
13995
13996 /*
James Smart6d368e52011-05-24 11:44:12 -040013997 * RPI header postings are not required for SLI4 ports capable of
13998 * extents.
13999 */
14000 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14001 spin_unlock_irq(&phba->hbalock);
14002 return rpi;
14003 }
14004
14005 /*
James Smart6fb120a2009-05-22 14:52:59 -040014006 * If the driver is running low on rpi resources, allocate another
14007 * page now. Note that the next_rpi value is used because
14008 * it represents how many are actually in use whereas max_rpi notes
14009 * how many are supported max by the device.
14010 */
James Smart6d368e52011-05-24 11:44:12 -040014011 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014012 spin_unlock_irq(&phba->hbalock);
14013 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14014 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14015 if (!rpi_hdr) {
14016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14017 "2002 Error Could not grow rpi "
14018 "count\n");
14019 } else {
James Smart6d368e52011-05-24 11:44:12 -040014020 lrpi = rpi_hdr->start_rpi;
14021 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014022 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14023 }
14024 }
14025
14026 return rpi;
14027}
14028
14029/**
14030 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14031 * @phba: pointer to lpfc hba data structure.
14032 *
14033 * This routine is invoked to release an rpi to the pool of
14034 * available rpis maintained by the driver.
14035 **/
14036void
James Smartd7c47992010-06-08 18:31:54 -040014037__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14038{
14039 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14040 phba->sli4_hba.rpi_count--;
14041 phba->sli4_hba.max_cfg_param.rpi_used--;
14042 }
14043}
14044
14045/**
14046 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14047 * @phba: pointer to lpfc hba data structure.
14048 *
14049 * This routine is invoked to release an rpi to the pool of
14050 * available rpis maintained by the driver.
14051 **/
14052void
James Smart6fb120a2009-05-22 14:52:59 -040014053lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14054{
14055 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014056 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014057 spin_unlock_irq(&phba->hbalock);
14058}
14059
14060/**
14061 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14062 * @phba: pointer to lpfc hba data structure.
14063 *
14064 * This routine is invoked to remove the memory region that
14065 * provided rpi via a bitmask.
14066 **/
14067void
14068lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14069{
14070 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014071 kfree(phba->sli4_hba.rpi_ids);
14072 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014073}
14074
14075/**
14076 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14077 * @phba: pointer to lpfc hba data structure.
14078 *
14079 * This routine is invoked to remove the memory region that
14080 * provided rpi via a bitmask.
14081 **/
14082int
14083lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14084{
14085 LPFC_MBOXQ_t *mboxq;
14086 struct lpfc_hba *phba = ndlp->phba;
14087 int rc;
14088
14089 /* The port is notified of the header region via a mailbox command. */
14090 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14091 if (!mboxq)
14092 return -ENOMEM;
14093
14094 /* Post all rpi memory regions to the port. */
14095 lpfc_resume_rpi(mboxq, ndlp);
14096 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14097 if (rc == MBX_NOT_FINISHED) {
14098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14099 "2010 Resume RPI Mailbox failed "
14100 "status %d, mbxStatus x%x\n", rc,
14101 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14102 mempool_free(mboxq, phba->mbox_mem_pool);
14103 return -EIO;
14104 }
14105 return 0;
14106}
14107
14108/**
14109 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014110 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014111 *
James Smart76a95d72010-11-20 23:11:48 -050014112 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014113 *
14114 * Returns:
14115 * 0 success
14116 * -Evalue otherwise
14117 **/
14118int
James Smart76a95d72010-11-20 23:11:48 -050014119lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014120{
14121 LPFC_MBOXQ_t *mboxq;
14122 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014123 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014124 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014125 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014126 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14127 if (!mboxq)
14128 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014129 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040014130 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
14131 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014132 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014133 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014134 "2022 INIT VPI Mailbox failed "
14135 "status %d, mbxStatus x%x\n", rc,
14136 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014137 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014138 }
James Smart6a9c52c2009-10-02 15:16:51 -040014139 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014140 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014141
14142 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014143}
14144
14145/**
14146 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14147 * @phba: pointer to lpfc hba data structure.
14148 * @mboxq: Pointer to mailbox object.
14149 *
14150 * This routine is invoked to manually add a single FCF record. The caller
14151 * must pass a completely initialized FCF_Record. This routine takes
14152 * care of the nonembedded mailbox operations.
14153 **/
14154static void
14155lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14156{
14157 void *virt_addr;
14158 union lpfc_sli4_cfg_shdr *shdr;
14159 uint32_t shdr_status, shdr_add_status;
14160
14161 virt_addr = mboxq->sge_array->addr[0];
14162 /* The IOCTL status is embedded in the mailbox subheader. */
14163 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14164 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14165 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14166
14167 if ((shdr_status || shdr_add_status) &&
14168 (shdr_status != STATUS_FCF_IN_USE))
14169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14170 "2558 ADD_FCF_RECORD mailbox failed with "
14171 "status x%x add_status x%x\n",
14172 shdr_status, shdr_add_status);
14173
14174 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14175}
14176
14177/**
14178 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14179 * @phba: pointer to lpfc hba data structure.
14180 * @fcf_record: pointer to the initialized fcf record to add.
14181 *
14182 * This routine is invoked to manually add a single FCF record. The caller
14183 * must pass a completely initialized FCF_Record. This routine takes
14184 * care of the nonembedded mailbox operations.
14185 **/
14186int
14187lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14188{
14189 int rc = 0;
14190 LPFC_MBOXQ_t *mboxq;
14191 uint8_t *bytep;
14192 void *virt_addr;
14193 dma_addr_t phys_addr;
14194 struct lpfc_mbx_sge sge;
14195 uint32_t alloc_len, req_len;
14196 uint32_t fcfindex;
14197
14198 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14199 if (!mboxq) {
14200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14201 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14202 return -ENOMEM;
14203 }
14204
14205 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14206 sizeof(uint32_t);
14207
14208 /* Allocate DMA memory and set up the non-embedded mailbox command */
14209 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14210 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14211 req_len, LPFC_SLI4_MBX_NEMBED);
14212 if (alloc_len < req_len) {
14213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14214 "2523 Allocated DMA memory size (x%x) is "
14215 "less than the requested DMA memory "
14216 "size (x%x)\n", alloc_len, req_len);
14217 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14218 return -ENOMEM;
14219 }
14220
14221 /*
14222 * Get the first SGE entry from the non-embedded DMA memory. This
14223 * routine only uses a single SGE.
14224 */
14225 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14226 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014227 virt_addr = mboxq->sge_array->addr[0];
14228 /*
14229 * Configure the FCF record for FCFI 0. This is the driver's
14230 * hardcoded default and gets used in nonFIP mode.
14231 */
14232 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14233 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14234 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14235
14236 /*
14237 * Copy the fcf_index and the FCF Record Data. The data starts after
14238 * the FCoE header plus word10. The data copy needs to be endian
14239 * correct.
14240 */
14241 bytep += sizeof(uint32_t);
14242 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14243 mboxq->vport = phba->pport;
14244 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14246 if (rc == MBX_NOT_FINISHED) {
14247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14248 "2515 ADD_FCF_RECORD mailbox failed with "
14249 "status 0x%x\n", rc);
14250 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14251 rc = -EIO;
14252 } else
14253 rc = 0;
14254
14255 return rc;
14256}
14257
14258/**
14259 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14260 * @phba: pointer to lpfc hba data structure.
14261 * @fcf_record: pointer to the fcf record to write the default data.
14262 * @fcf_index: FCF table entry index.
14263 *
14264 * This routine is invoked to build the driver's default FCF record. The
14265 * values used are hardcoded. This routine handles memory initialization.
14266 *
14267 **/
14268void
14269lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14270 struct fcf_record *fcf_record,
14271 uint16_t fcf_index)
14272{
14273 memset(fcf_record, 0, sizeof(struct fcf_record));
14274 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14275 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14276 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14277 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14278 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14279 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14280 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14281 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14282 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14283 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14284 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14285 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14286 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014287 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014288 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14289 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14290 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14291 /* Set the VLAN bit map */
14292 if (phba->valid_vlan) {
14293 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14294 = 1 << (phba->vlan_id % 8);
14295 }
14296}
14297
14298/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014299 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014300 * @phba: pointer to lpfc hba data structure.
14301 * @fcf_index: FCF table entry offset.
14302 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014303 * This routine is invoked to scan the entire FCF table by reading FCF
14304 * record and processing it one at a time starting from the @fcf_index
14305 * for initial FCF discovery or fast FCF failover rediscovery.
14306 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014307 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014308 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014309 **/
14310int
James Smart0c9ab6f2010-02-26 14:15:57 -050014311lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014312{
14313 int rc = 0, error;
14314 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014315
James Smart32b97932009-07-19 10:01:21 -040014316 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014317 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14318 if (!mboxq) {
14319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14320 "2000 Failed to allocate mbox for "
14321 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014322 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014323 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014324 }
James Smartecfd03c2010-02-12 14:41:27 -050014325 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014326 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014327 if (rc) {
14328 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014329 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014330 }
James Smartecfd03c2010-02-12 14:41:27 -050014331 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014332 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014333 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014334
14335 spin_lock_irq(&phba->hbalock);
14336 phba->hba_flag |= FCF_TS_INPROG;
14337 spin_unlock_irq(&phba->hbalock);
14338
James Smart6fb120a2009-05-22 14:52:59 -040014339 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014340 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014341 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014342 else {
James Smart38b92ef2010-08-04 16:11:39 -040014343 /* Reset eligible FCF count for new scan */
14344 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014345 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014346 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014347 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014348fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014349 if (error) {
14350 if (mboxq)
14351 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014352 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014353 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014354 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014355 spin_unlock_irq(&phba->hbalock);
14356 }
James Smart6fb120a2009-05-22 14:52:59 -040014357 return error;
14358}
James Smarta0c87cb2009-07-19 10:01:10 -040014359
14360/**
James Smarta93ff372010-10-22 11:06:08 -040014361 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014362 * @phba: pointer to lpfc hba data structure.
14363 * @fcf_index: FCF table entry offset.
14364 *
14365 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014366 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014367 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014368 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014369 * otherwise.
14370 **/
14371int
14372lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14373{
14374 int rc = 0, error;
14375 LPFC_MBOXQ_t *mboxq;
14376
14377 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14378 if (!mboxq) {
14379 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14380 "2763 Failed to allocate mbox for "
14381 "READ_FCF cmd\n");
14382 error = -ENOMEM;
14383 goto fail_fcf_read;
14384 }
14385 /* Construct the read FCF record mailbox command */
14386 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14387 if (rc) {
14388 error = -EINVAL;
14389 goto fail_fcf_read;
14390 }
14391 /* Issue the mailbox command asynchronously */
14392 mboxq->vport = phba->pport;
14393 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14394 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14395 if (rc == MBX_NOT_FINISHED)
14396 error = -EIO;
14397 else
14398 error = 0;
14399
14400fail_fcf_read:
14401 if (error && mboxq)
14402 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14403 return error;
14404}
14405
14406/**
14407 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14408 * @phba: pointer to lpfc hba data structure.
14409 * @fcf_index: FCF table entry offset.
14410 *
14411 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040014412 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050014413 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014414 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014415 * otherwise.
14416 **/
14417int
14418lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14419{
14420 int rc = 0, error;
14421 LPFC_MBOXQ_t *mboxq;
14422
14423 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14424 if (!mboxq) {
14425 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14426 "2758 Failed to allocate mbox for "
14427 "READ_FCF cmd\n");
14428 error = -ENOMEM;
14429 goto fail_fcf_read;
14430 }
14431 /* Construct the read FCF record mailbox command */
14432 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14433 if (rc) {
14434 error = -EINVAL;
14435 goto fail_fcf_read;
14436 }
14437 /* Issue the mailbox command asynchronously */
14438 mboxq->vport = phba->pport;
14439 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14440 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14441 if (rc == MBX_NOT_FINISHED)
14442 error = -EIO;
14443 else
14444 error = 0;
14445
14446fail_fcf_read:
14447 if (error && mboxq)
14448 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14449 return error;
14450}
14451
14452/**
14453 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14454 * @phba: pointer to lpfc hba data structure.
14455 *
14456 * This routine is to get the next eligible FCF record index in a round
14457 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040014458 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050014459 * shall be returned, otherwise, the next eligible FCF record's index
14460 * shall be returned.
14461 **/
14462uint16_t
14463lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14464{
14465 uint16_t next_fcf_index;
14466
James Smart3804dc82010-07-14 15:31:37 -040014467 /* Search start from next bit of currently registered FCF index */
14468 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
14469 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050014470 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14471 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040014472 next_fcf_index);
14473
James Smart0c9ab6f2010-02-26 14:15:57 -050014474 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
14475 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
14476 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14477 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050014478
James Smart3804dc82010-07-14 15:31:37 -040014479 /* Check roundrobin failover list empty condition */
14480 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14481 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14482 "2844 No roundrobin failover FCF available\n");
14483 return LPFC_FCOE_FCF_NEXT_NONE;
14484 }
14485
James Smart3804dc82010-07-14 15:31:37 -040014486 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014487 "2845 Get next roundrobin failover FCF (x%x)\n",
14488 next_fcf_index);
14489
James Smart0c9ab6f2010-02-26 14:15:57 -050014490 return next_fcf_index;
14491}
14492
14493/**
14494 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
14495 * @phba: pointer to lpfc hba data structure.
14496 *
14497 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014498 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014499 * does not go beyond the range of the driver allocated bmask dimension
14500 * before setting the bit.
14501 *
14502 * Returns 0 if the index bit successfully set, otherwise, it returns
14503 * -EINVAL.
14504 **/
14505int
14506lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
14507{
14508 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14509 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014510 "2610 FCF (x%x) reached driver's book "
14511 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014512 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14513 return -EINVAL;
14514 }
14515 /* Set the eligible FCF record index bmask */
14516 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14517
James Smart3804dc82010-07-14 15:31:37 -040014518 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014519 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040014520 "bmask\n", fcf_index);
14521
James Smart0c9ab6f2010-02-26 14:15:57 -050014522 return 0;
14523}
14524
14525/**
James Smart3804dc82010-07-14 15:31:37 -040014526 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050014527 * @phba: pointer to lpfc hba data structure.
14528 *
14529 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014530 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014531 * does not go beyond the range of the driver allocated bmask dimension
14532 * before clearing the bit.
14533 **/
14534void
14535lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
14536{
14537 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14538 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014539 "2762 FCF (x%x) reached driver's book "
14540 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014541 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14542 return;
14543 }
14544 /* Clear the eligible FCF record index bmask */
14545 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040014546
14547 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014548 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040014549 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050014550}
14551
14552/**
James Smartecfd03c2010-02-12 14:41:27 -050014553 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
14554 * @phba: pointer to lpfc hba data structure.
14555 *
14556 * This routine is the completion routine for the rediscover FCF table mailbox
14557 * command. If the mailbox command returned failure, it will try to stop the
14558 * FCF rediscover wait timer.
14559 **/
14560void
14561lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
14562{
14563 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14564 uint32_t shdr_status, shdr_add_status;
14565
14566 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14567
14568 shdr_status = bf_get(lpfc_mbox_hdr_status,
14569 &redisc_fcf->header.cfg_shdr.response);
14570 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
14571 &redisc_fcf->header.cfg_shdr.response);
14572 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050014573 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050014574 "2746 Requesting for FCF rediscovery failed "
14575 "status x%x add_status x%x\n",
14576 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050014577 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050014578 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014579 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014580 spin_unlock_irq(&phba->hbalock);
14581 /*
14582 * CVL event triggered FCF rediscover request failed,
14583 * last resort to re-try current registered FCF entry.
14584 */
14585 lpfc_retry_pport_discovery(phba);
14586 } else {
14587 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014588 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014589 spin_unlock_irq(&phba->hbalock);
14590 /*
14591 * DEAD FCF event triggered FCF rediscover request
14592 * failed, last resort to fail over as a link down
14593 * to FCF registration.
14594 */
14595 lpfc_sli4_fcf_dead_failthrough(phba);
14596 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014597 } else {
14598 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014599 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050014600 /*
14601 * Start FCF rediscovery wait timer for pending FCF
14602 * before rescan FCF record table.
14603 */
14604 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050014605 }
James Smartecfd03c2010-02-12 14:41:27 -050014606
14607 mempool_free(mbox, phba->mbox_mem_pool);
14608}
14609
14610/**
James Smart3804dc82010-07-14 15:31:37 -040014611 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050014612 * @phba: pointer to lpfc hba data structure.
14613 *
14614 * This routine is invoked to request for rediscovery of the entire FCF table
14615 * by the port.
14616 **/
14617int
14618lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
14619{
14620 LPFC_MBOXQ_t *mbox;
14621 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14622 int rc, length;
14623
James Smart0c9ab6f2010-02-26 14:15:57 -050014624 /* Cancel retry delay timers to all vports before FCF rediscover */
14625 lpfc_cancel_all_vport_retry_delay_timer(phba);
14626
James Smartecfd03c2010-02-12 14:41:27 -050014627 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14628 if (!mbox) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14630 "2745 Failed to allocate mbox for "
14631 "requesting FCF rediscover.\n");
14632 return -ENOMEM;
14633 }
14634
14635 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
14636 sizeof(struct lpfc_sli4_cfg_mhdr));
14637 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14638 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
14639 length, LPFC_SLI4_MBX_EMBED);
14640
14641 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14642 /* Set count to 0 for invalidating the entire FCF database */
14643 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
14644
14645 /* Issue the mailbox command asynchronously */
14646 mbox->vport = phba->pport;
14647 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
14648 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
14649
14650 if (rc == MBX_NOT_FINISHED) {
14651 mempool_free(mbox, phba->mbox_mem_pool);
14652 return -EIO;
14653 }
14654 return 0;
14655}
14656
14657/**
James Smartfc2b9892010-02-26 14:15:29 -050014658 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
14659 * @phba: pointer to lpfc hba data structure.
14660 *
14661 * This function is the failover routine as a last resort to the FCF DEAD
14662 * event when driver failed to perform fast FCF failover.
14663 **/
14664void
14665lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
14666{
14667 uint32_t link_state;
14668
14669 /*
14670 * Last resort as FCF DEAD event failover will treat this as
14671 * a link down, but save the link state because we don't want
14672 * it to be changed to Link Down unless it is already down.
14673 */
14674 link_state = phba->link_state;
14675 lpfc_linkdown(phba);
14676 phba->link_state = link_state;
14677
14678 /* Unregister FCF if no devices connected to it */
14679 lpfc_unregister_unused_fcf(phba);
14680}
14681
14682/**
James Smarta0c87cb2009-07-19 10:01:10 -040014683 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
14684 * @phba: pointer to lpfc hba data structure.
14685 *
14686 * This function read region 23 and parse TLV for port status to
14687 * decide if the user disaled the port. If the TLV indicates the
14688 * port is disabled, the hba_flag is set accordingly.
14689 **/
14690void
14691lpfc_sli_read_link_ste(struct lpfc_hba *phba)
14692{
14693 LPFC_MBOXQ_t *pmb = NULL;
14694 MAILBOX_t *mb;
14695 uint8_t *rgn23_data = NULL;
14696 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
14697 int rc;
14698
14699 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14700 if (!pmb) {
14701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14702 "2600 lpfc_sli_read_serdes_param failed to"
14703 " allocate mailbox memory\n");
14704 goto out;
14705 }
14706 mb = &pmb->u.mb;
14707
14708 /* Get adapter Region 23 data */
14709 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
14710 if (!rgn23_data)
14711 goto out;
14712
14713 do {
14714 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
14715 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
14716
14717 if (rc != MBX_SUCCESS) {
14718 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14719 "2601 lpfc_sli_read_link_ste failed to"
14720 " read config region 23 rc 0x%x Status 0x%x\n",
14721 rc, mb->mbxStatus);
14722 mb->un.varDmp.word_cnt = 0;
14723 }
14724 /*
14725 * dump mem may return a zero when finished or we got a
14726 * mailbox error, either way we are done.
14727 */
14728 if (mb->un.varDmp.word_cnt == 0)
14729 break;
14730 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
14731 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
14732
14733 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
14734 rgn23_data + offset,
14735 mb->un.varDmp.word_cnt);
14736 offset += mb->un.varDmp.word_cnt;
14737 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
14738
14739 data_size = offset;
14740 offset = 0;
14741
14742 if (!data_size)
14743 goto out;
14744
14745 /* Check the region signature first */
14746 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
14747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14748 "2619 Config region 23 has bad signature\n");
14749 goto out;
14750 }
14751 offset += 4;
14752
14753 /* Check the data structure version */
14754 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
14755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14756 "2620 Config region 23 has bad version\n");
14757 goto out;
14758 }
14759 offset += 4;
14760
14761 /* Parse TLV entries in the region */
14762 while (offset < data_size) {
14763 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
14764 break;
14765 /*
14766 * If the TLV is not driver specific TLV or driver id is
14767 * not linux driver id, skip the record.
14768 */
14769 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
14770 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
14771 (rgn23_data[offset + 3] != 0)) {
14772 offset += rgn23_data[offset + 1] * 4 + 4;
14773 continue;
14774 }
14775
14776 /* Driver found a driver specific TLV in the config region */
14777 sub_tlv_len = rgn23_data[offset + 1] * 4;
14778 offset += 4;
14779 tlv_offset = 0;
14780
14781 /*
14782 * Search for configured port state sub-TLV.
14783 */
14784 while ((offset < data_size) &&
14785 (tlv_offset < sub_tlv_len)) {
14786 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
14787 offset += 4;
14788 tlv_offset += 4;
14789 break;
14790 }
14791 if (rgn23_data[offset] != PORT_STE_TYPE) {
14792 offset += rgn23_data[offset + 1] * 4 + 4;
14793 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
14794 continue;
14795 }
14796
14797 /* This HBA contains PORT_STE configured */
14798 if (!rgn23_data[offset + 2])
14799 phba->hba_flag |= LINK_DISABLED;
14800
14801 goto out;
14802 }
14803 }
14804out:
14805 if (pmb)
14806 mempool_free(pmb, phba->mbox_mem_pool);
14807 kfree(rgn23_data);
14808 return;
14809}
James Smart695a8142010-01-26 23:08:03 -050014810
14811/**
James Smart52d52442011-05-24 11:42:45 -040014812 * lpfc_wr_object - write an object to the firmware
14813 * @phba: HBA structure that indicates port to create a queue on.
14814 * @dmabuf_list: list of dmabufs to write to the port.
14815 * @size: the total byte value of the objects to write to the port.
14816 * @offset: the current offset to be used to start the transfer.
14817 *
14818 * This routine will create a wr_object mailbox command to send to the port.
14819 * the mailbox command will be constructed using the dma buffers described in
14820 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
14821 * BDEs that the imbedded mailbox can support. The @offset variable will be
14822 * used to indicate the starting offset of the transfer and will also return
14823 * the offset after the write object mailbox has completed. @size is used to
14824 * determine the end of the object and whether the eof bit should be set.
14825 *
14826 * Return 0 is successful and offset will contain the the new offset to use
14827 * for the next write.
14828 * Return negative value for error cases.
14829 **/
14830int
14831lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
14832 uint32_t size, uint32_t *offset)
14833{
14834 struct lpfc_mbx_wr_object *wr_object;
14835 LPFC_MBOXQ_t *mbox;
14836 int rc = 0, i = 0;
14837 uint32_t shdr_status, shdr_add_status;
14838 uint32_t mbox_tmo;
14839 union lpfc_sli4_cfg_shdr *shdr;
14840 struct lpfc_dmabuf *dmabuf;
14841 uint32_t written = 0;
14842
14843 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14844 if (!mbox)
14845 return -ENOMEM;
14846
14847 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14848 LPFC_MBOX_OPCODE_WRITE_OBJECT,
14849 sizeof(struct lpfc_mbx_wr_object) -
14850 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
14851
14852 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
14853 wr_object->u.request.write_offset = *offset;
14854 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
14855 wr_object->u.request.object_name[0] =
14856 cpu_to_le32(wr_object->u.request.object_name[0]);
14857 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
14858 list_for_each_entry(dmabuf, dmabuf_list, list) {
14859 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
14860 break;
14861 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
14862 wr_object->u.request.bde[i].addrHigh =
14863 putPaddrHigh(dmabuf->phys);
14864 if (written + SLI4_PAGE_SIZE >= size) {
14865 wr_object->u.request.bde[i].tus.f.bdeSize =
14866 (size - written);
14867 written += (size - written);
14868 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
14869 } else {
14870 wr_object->u.request.bde[i].tus.f.bdeSize =
14871 SLI4_PAGE_SIZE;
14872 written += SLI4_PAGE_SIZE;
14873 }
14874 i++;
14875 }
14876 wr_object->u.request.bde_count = i;
14877 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
14878 if (!phba->sli4_hba.intr_enable)
14879 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14880 else {
14881 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
14882 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14883 }
14884 /* The IOCTL status is embedded in the mailbox subheader. */
14885 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
14886 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14887 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14888 if (rc != MBX_TIMEOUT)
14889 mempool_free(mbox, phba->mbox_mem_pool);
14890 if (shdr_status || shdr_add_status || rc) {
14891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14892 "3025 Write Object mailbox failed with "
14893 "status x%x add_status x%x, mbx status x%x\n",
14894 shdr_status, shdr_add_status, rc);
14895 rc = -ENXIO;
14896 } else
14897 *offset += wr_object->u.response.actual_write_length;
14898 return rc;
14899}
14900
14901/**
James Smart695a8142010-01-26 23:08:03 -050014902 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
14903 * @vport: pointer to vport data structure.
14904 *
14905 * This function iterate through the mailboxq and clean up all REG_LOGIN
14906 * and REG_VPI mailbox commands associated with the vport. This function
14907 * is called when driver want to restart discovery of the vport due to
14908 * a Clear Virtual Link event.
14909 **/
14910void
14911lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
14912{
14913 struct lpfc_hba *phba = vport->phba;
14914 LPFC_MBOXQ_t *mb, *nextmb;
14915 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040014916 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040014917 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040014918 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040014919 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050014920 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050014921
James Smartd439d282010-09-29 11:18:45 -040014922 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050014923 spin_lock_irq(&phba->hbalock);
14924 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
14925 if (mb->vport != vport)
14926 continue;
14927
14928 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
14929 (mb->u.mb.mbxCommand != MBX_REG_VPI))
14930 continue;
14931
James Smartd439d282010-09-29 11:18:45 -040014932 list_del(&mb->list);
14933 list_add_tail(&mb->list, &mbox_cmd_list);
14934 }
14935 /* Clean up active mailbox command with the vport */
14936 mb = phba->sli.mbox_active;
14937 if (mb && (mb->vport == vport)) {
14938 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
14939 (mb->u.mb.mbxCommand == MBX_REG_VPI))
14940 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14941 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14942 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
14943 /* Put reference count for delayed processing */
14944 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
14945 /* Unregister the RPI when mailbox complete */
14946 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
14947 }
14948 }
James Smart63e801c2010-11-20 23:14:19 -050014949 /* Cleanup any mailbox completions which are not yet processed */
14950 do {
14951 restart_loop = 0;
14952 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
14953 /*
14954 * If this mailox is already processed or it is
14955 * for another vport ignore it.
14956 */
14957 if ((mb->vport != vport) ||
14958 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
14959 continue;
14960
14961 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
14962 (mb->u.mb.mbxCommand != MBX_REG_VPI))
14963 continue;
14964
14965 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14966 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14967 ndlp = (struct lpfc_nodelist *)mb->context2;
14968 /* Unregister the RPI when mailbox complete */
14969 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
14970 restart_loop = 1;
14971 spin_unlock_irq(&phba->hbalock);
14972 spin_lock(shost->host_lock);
14973 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
14974 spin_unlock(shost->host_lock);
14975 spin_lock_irq(&phba->hbalock);
14976 break;
14977 }
14978 }
14979 } while (restart_loop);
14980
James Smartd439d282010-09-29 11:18:45 -040014981 spin_unlock_irq(&phba->hbalock);
14982
14983 /* Release the cleaned-up mailbox commands */
14984 while (!list_empty(&mbox_cmd_list)) {
14985 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050014986 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
14987 mp = (struct lpfc_dmabuf *) (mb->context1);
14988 if (mp) {
14989 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
14990 kfree(mp);
14991 }
James Smart78730cf2010-04-06 15:06:30 -040014992 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040014993 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040014994 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020014995 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040014996 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020014997 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040014998 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040014999 }
James Smart695a8142010-01-26 23:08:03 -050015000 }
James Smart695a8142010-01-26 23:08:03 -050015001 mempool_free(mb, phba->mbox_mem_pool);
15002 }
James Smartd439d282010-09-29 11:18:45 -040015003
15004 /* Release the ndlp with the cleaned-up active mailbox command */
15005 if (act_mbx_ndlp) {
15006 spin_lock(shost->host_lock);
15007 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15008 spin_unlock(shost->host_lock);
15009 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015010 }
James Smart695a8142010-01-26 23:08:03 -050015011}
15012
James Smart2a9bf3d2010-06-07 15:24:45 -040015013/**
15014 * lpfc_drain_txq - Drain the txq
15015 * @phba: Pointer to HBA context object.
15016 *
15017 * This function attempt to submit IOCBs on the txq
15018 * to the adapter. For SLI4 adapters, the txq contains
15019 * ELS IOCBs that have been deferred because the there
15020 * are no SGLs. This congestion can occur with large
15021 * vport counts during node discovery.
15022 **/
15023
15024uint32_t
15025lpfc_drain_txq(struct lpfc_hba *phba)
15026{
15027 LIST_HEAD(completions);
15028 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15029 struct lpfc_iocbq *piocbq = 0;
15030 unsigned long iflags = 0;
15031 char *fail_msg = NULL;
15032 struct lpfc_sglq *sglq;
15033 union lpfc_wqe wqe;
15034
15035 spin_lock_irqsave(&phba->hbalock, iflags);
15036 if (pring->txq_cnt > pring->txq_max)
15037 pring->txq_max = pring->txq_cnt;
15038
15039 spin_unlock_irqrestore(&phba->hbalock, iflags);
15040
15041 while (pring->txq_cnt) {
15042 spin_lock_irqsave(&phba->hbalock, iflags);
15043
James Smart19ca7602010-11-20 23:11:55 -050015044 piocbq = lpfc_sli_ringtx_get(phba, pring);
15045 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015046 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015047 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015048 spin_unlock_irqrestore(&phba->hbalock, iflags);
15049 break;
15050 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015051 if (!piocbq) {
15052 /* The txq_cnt out of sync. This should
15053 * never happen
15054 */
15055 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015056 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015057 spin_unlock_irqrestore(&phba->hbalock, iflags);
15058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15059 "2823 txq empty and txq_cnt is %d\n ",
15060 pring->txq_cnt);
15061 break;
15062 }
15063 }
15064
15065 /* The xri and iocb resources secured,
15066 * attempt to issue request
15067 */
James Smart6d368e52011-05-24 11:44:12 -040015068 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015069 piocbq->sli4_xritag = sglq->sli4_xritag;
15070 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15071 fail_msg = "to convert bpl to sgl";
15072 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15073 fail_msg = "to convert iocb to wqe";
15074 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15075 fail_msg = " - Wq is full";
15076 else
15077 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15078
15079 if (fail_msg) {
15080 /* Failed means we can't issue and need to cancel */
15081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15082 "2822 IOCB failed %s iotag 0x%x "
15083 "xri 0x%x\n",
15084 fail_msg,
15085 piocbq->iotag, piocbq->sli4_xritag);
15086 list_add_tail(&piocbq->list, &completions);
15087 }
15088 spin_unlock_irqrestore(&phba->hbalock, iflags);
15089 }
15090
James Smart2a9bf3d2010-06-07 15:24:45 -040015091 /* Cancel all the IOCBs that cannot be issued */
15092 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15093 IOERR_SLI_ABORTED);
15094
15095 return pring->txq_cnt;
15096}